Menu

#158 WIP: Propose the db3js sdk

M2
open
nobody
2022-12-09
2022-11-18
Anonymous
No

Originally created by: xiyangjun
Originally owned by: xiyangjun

Motivations

In web3, typescript is the top-class program language, we should provide the typescript sdk to the web3 developers, some principles should be considered

  • easy to setup, the developers can use npm and yarn to install db3js
  • easy to use, db3js should has the clear method definition
  • friendly error message, the developers can know what is the reason that causes the execeptions by the error message for example Exception Out of Gas

With db3js, the developers can develop web3 native application acting as full-stack web3 developer

The methods of db3js

Method Description Parameters Return
submitMutation this method is used for submitting mutation to db3 Mutation, call_back The Hash of Mutation
batchGet the method is used to query a batch of keys Session id, call_back, Keys Key Values
batchRange this method is used for query a batch of range Range, call_back key values
genRandKey this method which for used for testing will generate private key randomly None Private Key, Public Key, Seed
getAccountStatus get the status of current account addr Account
batchGetWithProof the method is used to query a batch of keys Session id, call_back, Keys Key Values, Proofs
batchRangeWithProof this method is used for query a batch of range Range, call_back key values, Proofs
getNonce get the latest nonce for current account account addr number
estimateGas estimate the gas that mutation needs mutation gas

the openSession and closeSession should not be exposed to developers and move them to the internal of batchGet and batchRange

The methods of doc store

Solution

How to use db3js

install

yarn add db3js

example to use

submit the mutation to db3

// import the db3js
import { DB3, generateKey, sign } from "db3";
// config the db3 node
const db3_instance = new DB3("http://node1.db3.network");
const [sk, public_key] = await generateKey();
async function _sign(data: Uint8Array) {
     return [await sign(data, sk), public_key];
}
const response = await db3_instance.submitMutation({ns: 'my_namespace', gasLimit: 0.3, data: [{key: value}]}, _sign);

Additional context

Discussion

  • Anonymous

    Anonymous - 2022-11-18

    Originally posted by: xiyangjun

    db3js API method

    DB3 constructor

    return db3 instance when new DB3 constructor

    db3_instance.submitMutation

    insert data to db3

    example

    const db3_instance = new DB3("http://node1.db3.network");
    const response = await db3_instance.submitMutation({ns: 'my_namespace', gasLimit: 0.3, data: [{key: value}]});
    

    Browser supports

    Chrome, Safari, Edge

     
  • Anonymous

    Anonymous - 2022-11-18

    Originally posted by: imotai

    db3js API method

    DB3 constructor

    return db3 instance when new DB3 constructor

    db3_instance.submitMutation

    insert data to db3

    example

    const db3_instance = new DB3("http://node1.db3.network"); const response = await db3_instance.submitMutation({ns: 'my_namespace', gasLimit: 0.3, data: [{key: value}]});

    Browser supports

    Chrome, Safari, Edge

    Can the value be a JSON object?

     
  • Anonymous

    Anonymous - 2022-11-18
     
  • Anonymous

    Anonymous - 2022-11-18

    Originally posted by: imotai

    @xiyangjun you can try https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string to encode uint8array to base64 string

    function uint8ArrayFromBase64(s) {
      // 1. Call atob()
      var b = atob(s), b_at = b.charCodeAt.bind(b);
      // 2. Construct Uint8Array from String
      return Uint8Array.from({
        [Symbol.iterator]() {
          var end = b.length, i = 0;
          return ({
            next() {
              return ({value: b_at(i++), done: i>end});
            }
          });
        }
      });
    }
    
    function uint8ArrayToBase64(a) {
      // 1. Preprocess Uint8Array into String
      // (TODO: fix RAM usage from intermediate array creation)
      var a_s = Array.prototype.map.call(a, c => String.fromCharCode(c)).join(String());
      // 2. Call btoa()
      return btoa(a_s);
    }
    Demo:
    
    <form action="javascript:" onsubmit="(({target:form,submitter:{value:action}})=>{eval(action)(form)})(event)">
    <input name="b64" value="AAAAB3NzaC1yc2E=">
    <button type="submit" value="({b64:{value:s},u8a:e})=>{e.value=`[${uint8ArrayFromBase64(s)}]`;}">Convert to Uint8Array</button>
    <br />
    <input name="u8a" value="">
    <button type="submit" value="({u8a:{value:x},b64:e})=>{e.value=(uint8ArrayToBase64(x.replace(/(?:^\[|\]$)/g, '').split(',')));}">Convert to Base64</button>
    </form>
    
     

Log in to post a comment.

MongoDB Logo MongoDB