create account

SPK Client Side Package by disregardfiat

View this thread on: hive.blogpeakd.comecency.com
· @disregardfiat ·
$18.75
SPK Client Side Package
# 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. 


![Craiyon: Robot Advocate on Wallstreet](https://files.peakd.com/file/peakd-hive/disregardfiat/AJkRoL2K7hVzpMdtyPDSMMPodVX37BqhtUmjswzxVH6gWq68k3ChjmzW9zMKE2f.png)


## 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.

```js
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.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 27 others
properties (23)
authordisregardfiat
permlinkspk-client-side-package
categoryspk
json_metadata"{"app":"peakd/2023.5.1","format":"markdown","description":"Further Progress on spk network client side package","tags":["spk","dlux","dev"],"users":[],"image":["https://files.peakd.com/file/peakd-hive/disregardfiat/AJkRoL2K7hVzpMdtyPDSMMPodVX37BqhtUmjswzxVH6gWq68k3ChjmzW9zMKE2f.png"]}"
created2023-05-27 22:22:15
last_update2023-05-27 22:22:15
depth0
children2
last_payout2023-06-03 22:22:15
cashout_time1969-12-31 23:59:59
total_payout_value9.382 HBD
curator_payout_value9.367 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,413
author_reputation345,191,366,639,512
root_title"SPK Client Side Package"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id123,899,714
net_rshares40,646,748,426,393
author_curate_reward""
vote details (91)
@crrdlx ·
spkd (?)
properties (22)
authorcrrdlx
permlinkre-disregardfiat-rvdsk8
categoryspk
json_metadata{"tags":["spk"],"app":"peakd/2023.5.1"}
created2023-05-28 18:26:33
last_update2023-05-28 18:26:33
depth1
children0
last_payout2023-06-04 18:26:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8
author_reputation66,353,892,476,578
root_title"SPK Client Side Package"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id123,925,961
net_rshares0
@rafzat ·
This seem to be a very good one
Thumbs up!!
properties (22)
authorrafzat
permlinkrvdfqw
categoryspk
json_metadata{"app":"hiveblog/0.1"}
created2023-05-28 13:50:30
last_update2023-05-28 13:50:30
depth1
children0
last_payout2023-06-04 13:50:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length43
author_reputation183,560,271,702,716
root_title"SPK Client Side Package"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id123,917,149
net_rshares0