SPK Network Client Side Package
We're looking for a better name than spk-js. Let us know if you have any suggestions. It should generally serve the same purpose as hive-js or dhive as an allegory. In meat space it would be closest to an advocate: breaking down complex rules and requirements into simple documents the the end user can sign to manage their affairs.
Account Management
The code to deconstruct accounts has been set up and the code for using Hive Keychain to sign has also been set up. There is a function that allows you (the developer) to BYO Signing mechanism to support the many multitude of ways to get your users to sign and broadcast transactions.
<script type="module" src="/js/spk-js.js"></script>
Is the only thing on the window currently, this module returns a default export so it's named whatever you name your script. in this case spk-js => spkJs
Sign / Broadcast
You can specify equivalent functions as below, taking a three part [account, op/buffer, key_choice] argument and returning the broadcast txid or signature.
sign: function (op) {
return new Promise((res, rej) => {
window.hive_keychain.requestSignBuffer(
op[0],
`${op[0]}:${op[1]}`,
op[2],
(sig) => {
if (sig.error) rej(sig);
else res(sig.result);
}
);
});
}
spkJs.api.setSignFunction(newFunc)
broadcast: function (op) {
return new Promise((resolve, reject) => {
if (window.hive_keychain) {
if(typeof op[1] == "string") op[1] = JSON.parse(op[1])
try {
window.hive_keychain.requestBroadcast(
op[0],
op[1],
op[2],
function (response) {
resolve(response);
}
);
} catch (e) {
reject(e);
}
} else {
reject({ error: "Hive Keychain is not installed." });
}
});
},
spkJs.api.setBroadcastFunction(newFunc)
So, for example:
function newFunc (op){
return promise
if(user_opt.hive_keychain){
forward_toHKC(op).then resolve return
} else if (user_opt.hive_auth){
forwardtoHive_Auth(op).then resolve return
} //etc
}
Account Constructor
You can:
spkJs.account.setAccount('dlux-io')
Which returns a promise. It consumes the getAccount and getStats API, and uses a few helpers to construct meaningful account data:
balance: 122548
behind: 1
broca: "2605365,4Uc4d"
broca_now: 2611261
channels: {}
claim: 0
contracts: []
down: {}
drop: {availible: {…}, last_claim: 0, total_claims: 0}
file_contracts: {}
gov: 0
gov_downs: {}
granted: {}
granting: {}
head_block: 75245832
heldCollateral: 0
node: "spk-test"
power_downs: {}
poweredUp: 0
pubKey: "STM765pPE6tnKYqEr2Y4khRhZwauoZMyQSs1CvXhgLGtgMhuVxY53"
spk: 0
spk_block: 73431764
spk_power: 2850
spk_vote: {}
storage: "12D3KooWMJQEzggotBCznPM1cVohMQx2puLuKTgLU8z9SzKLG6fa"
tick: "0.015000"
up: {}
This includes pending upload contracts, finalized upload contracts, current balances, power down information, the current price of larynx, (the price of spk will be included in the future), the validators you vote for, etc.
Things like broca: "2605365,4Uc4d"
have been deconstructed into live broca_now: 2611261
... which should be easier for a developer to plug directly into the UI.
File
Still porting the vue.dlux.io implementation. Missing is a generalized way to petition for a contract from a provider list, or building your own contract. This is informing some work on SPK Network APIs as well. What is written is selecting an existing contract, dynamically setting the upload API address, and the client side code to handle resumable uploads.
The functions should perform the following:
- Build a Storage Contract
- Choose IPFS upload agent
- Get a list of API to contact to get a storage contract, with the terms of each contract
- Attach fileReader logic to a file input element
- Set the user up to sign their file choice
- Trigger the upload process
- Perform management functions for the files
These functions are all written on our test front end and generalizing them hopefully to work within this self-contained file has been... fun. Hoping to get this out to you all very soon.