Skip to main content

Use SDK with web

Import DO SDK into your web dapp to enable your users to easily connect to the browser extension and Mobile.

Steps​

1. Import the SDK​

In your project directory, Extract the downloaded package to the project directory.

Reference library files between pages

<script src="/don_sdk_wasm_v0.1.0.js"></script>

2. Use the SDK​

Use the SDK by calling any provider API methods. Instantiate all methods that exist in the SDK first.

<script>
Module.onRuntimeInitialized = async (_) => {
const api = {
export_new_seed: Module.cwrap('export_new_seed', 'string', []),

import_prikey_handler_from_seed: Module.cwrap('import_prikey_handler_from_seed', 'number', ['string']),

import_prikey_handler_from_hex: Module.cwrap('import_prikey_handler_from_hex', 'number', ['string']),

export_new_prikey_to_hex: Module.cwrap('export_new_prikey_to_hex', 'string', ['number']),

export_mnemonic_from_seed: Module.cwrap('export_mnemonic_from_seed', 'string', ['string']),

import_seed_from_mnemonic: Module.cwrap('import_seed_from_mnemonic', 'string', ['string']),

get_addr: Module.cwrap('get_addr', 'string', ['number']),

get_pubstr_base64: Module.cwrap('get_pubstr_base64', 'string', ['number']),

txJsonSign: Module.cwrap('txJsonSign', 'string', ['string', 'number']),

free_prikey_handler: Module.cwrap('free_prikey_handler', '', ['number']),

sign: Module.cwrap('sign', 'string', ['string', 'number', 'number']),

verif_by_public_str: Module.cwrap('verif_by_public_str', 'number', ['string', 'number', 'string', 'number', 'string', 'number']),
get_version: Module.cwrap('get_version', 'string', []) //Return SDK version
};
console.log(api.get_version());
};

</script>

SDK Function​

export_new_seed()​

Generate seeds and return the hexadecimal string of the seeds

seed = api.export_new_seed();

import_prikey_handler_from_seed()​

Parameter input in the seed and return the private key handle

privatePkey = api.import_prikey_handler_from_seed(seed);

export_new_prikey_to_hex()​

Parameter input the private key handle and return the hexadecimal encoding of the private key

hexprikey = api.export_new_prikey_to_hex(privatePkey);

import_prikey_handler_from_hex()​

Parameter input in the hexadecimal encoding of the private key and return the handle of the private key

api.import_prikey_handler_from_hex(hexprikey);

get_addr()​

Pass the parameter into the private key handle and return the addr account address of the private key

addr = api.get_addr(privatePkey);
resultaddr = api.get_addr(hexresultHandler);
console.assert(addr == resultaddr, "fail to recover private key");

free_prikey_handler()​

Release the private key handle

api.free_prikey_handler(hexresultHandler);

export_mnemonic_from_seed()​

Parameter input in hexadecimal seed and return seed mnemonic words

mnemonic = api.export_mnemonic_from_seed(seed);

import_seed_from_mnemonic()​

Parameter input seed mnemonic words and return hexadecimal seeds

mnemonicresultHandler = api.import_seed_from_mnemonic(mnemonic);

get_pubstr_base64()​

Parameter input in the private key handle and return the base64 encoding of the public key

base64pubkey = api.get_pubstr_base64(privatePkey);

1.txJsonSign()​

This method is used to sign the transaction entity

Sign the txJson in the result field of the transaction return value.

txJson = ...;
message = txJson;
message_size = message.length;
signature = api.txJsonSign(message,privatePkey);

2.sig_tx()​

Sign the transaction body returned by GetTransaction(GetStakeTransaction,GetUnStakeTransaction,GetInvestTransaction,GetDisInvestTransaction,GetBouns), retrieve the transaction body field txjson for signature, and return the signed message.

message = tx;
message_size = message.length;
signature = api.sig_tx(message,message_size,privatePkey);

3.sig_contract_tx()​

Sign the returned transaction entity related to the contract(GetDeployContractTransaction,GetCallContractTransaction),retrieve the transaction body field txjson for signature, and return the signed message.

message = tx;
message_size = message.length;
signature = api.sig_contract_tx(message,message_size,privatePkey);

verif_by_public_str()​

Check if the signature is correct.
Pass in the public key, message, and signed message, and return the verification result of the signed message, with 0 indicating success and -1 indicating failure

result = api.verif_by_public_str(base64pubkey, base64pubkey.length, message, message_size, signature, signature.length);

get_version()​

Return SDK version number

version = api.get_version()

Example​

You can copy the full web example to get started:

<script src="/don_sdk_wasm_v0.1.0.js"></script>

<script>
Module.onRuntimeInitialized = async (_) => {
const api = {
export_new_seed: Module.cwrap('export_new_seed', 'string', []), //Generate seeds and return the hexadecimal string of the seeds

import_prikey_handler_from_seed: Module.cwrap('import_prikey_handler_from_seed', 'number', ['string']),//Pass in the seed and return the private key handle

import_prikey_handler_from_hex: Module.cwrap('import_prikey_handler_from_hex', 'number', ['string']), //Pass in the hexadecimal encoding of the private key and return the handle of the private key

export_new_prikey_to_hex: Module.cwrap('export_new_prikey_to_hex', 'string', ['number']), //Pass in the private key handle and return the hexadecimal encoding of the private key

export_mnemonic_from_seed: Module.cwrap('export_mnemonic_from_seed', 'string', ['string']), //Pass in hexadecimal seed and return seed mnemonic words

import_seed_from_mnemonic: Module.cwrap('import_seed_from_mnemonic', 'string', ['string']), //Enter seed mnemonic words and return hexadecimal seeds

get_addr: Module.cwrap('get_addr', 'string', ['number']), //Pass in the private key handle and return the addr account address of the private key

get_pubstr_base64: Module.cwrap('get_pubstr_base64', 'string', ['number']), //Pass in the private key handle and return the base64 encoding of the public key

txJsonSign: Module.cwrap('txJsonSign', 'string', ['string', 'number']),//Pass in the transaction txjson field and private key handle, only return the txjson field

free_prikey_handler: Module.cwrap('free_prikey_handler', '', ['number']), //Release the private key handle

sign: Module.cwrap('sign', 'string', ['string', 'number', 'number']), //Pass in the message and private key handle, return the signed message

verif_by_public_str: Module.cwrap('verif_by_public_str', 'number', ['string', 'number', 'string', 'number', 'string', 'number']), //Pass in the public key, message, and signed message, and return the verification result of the signed message, with 0 indicating success and -1 indicating failure
get_version: Module.cwrap('get_version', 'string', []) //Return SDK version
};
console.log(api.get_version());

//seed base 64
seedBase64 = api.export_new_seed();
console.log(seedBase64)
console.log("seedis:")

//get prikey by seed;
privatePkey = api.import_prikey_handler_from_seed(seedBase64);
console.log("privatePkey")
console.log(privatePkey)
hexprikey = api.export_new_prikey_to_hex(privatePkey);
console.log(hexprikey);
hexresultHandler = api.import_prikey_handler_from_hex(hexprikey);

addr = api.get_addr(privatePkey);
console.log(addr);
resultaddr = api.get_addr(hexresultHandler);
console.assert(addr == resultaddr, "fail to recover private key");
api.free_prikey_handler(hexresultHandler);

mnemonic = api.export_mnemonic_from_seed(seedBase64);
console.log(mnemonic);
mnemonicresultHandler = api.import_seed_from_mnemonic(mnemonic);

console.log(mnemonicresultHandler)
base64pubkey = api.get_pubstr_base64(privatePkey);
console.log(base64pubkey);

//resultBase64pubkey = api.get_pubstr_base64(mnemonicresultHandler);
//console.assert(base64pubkey == resultBase64pubkey, "fail to recover private key");
//api.free_prikey_handler(mnemonicresultHandler);

message = "hello";
message_size = message.length;
signature = api.sign(message, message_size, privatePkey);

//rptTxIn The content filled in here is from the txjson field in tx (transaction). This example is for reference only
rpcTxIn = "{\"time\":\"1717148103333839\",\"identity\":\"2eb2F635320c3Dbf29eadD35E894c13EE3F20bd5\",\"utxo\":{\"owner\":[\"cED97dA085527Fe7e1772CA59Aa1e64A78143128\"],\"vin\":[{\"prevOut\":[{\"hash\":\"dea1b44a641928d6a09db9a3d9da274abe4ef13eb23833213144a5b46c612136\"}],\"vinSign\":{\"sign\":\"3o82Rq7taGO+VRtrhtxWOVPAolo/pVxuNXEBdklDpDVd8tJcTjNrnmaMFixdav2wCyvCb+AFOd02ryPI4JV1Cw==\",\"pub\":\"MCowBQYDK2VwAyEA89VSBQU7d4uL+jvqAKeCRbgYz2tzk/Rf8DNRhB0sLbg=\"}}],\"vout\":[{\"value\":\"480\",\"addr\":\"VirtualDeployContractBurnGas\"},{\"value\":\"1982446498797\",\"addr\":\"cED97dA085527Fe7e1772CA59Aa1e64A78143128\"},{\"value\":\"493200\",\"addr\":\"VirtualBurnGas\"}],\"multiSign\":[{\"sign\":\"YVcOA5gZhVNaQx7eoduw4lOI4d1HNtPRWEUoMzDK6D2UnZXDbRziXV66XiaRxKOzXF3kF/N4j8PEe5CMjkf3BQ==\",\"pub\":\"MCowBQYDK2VwAyEA89VSBQU7d4uL+jvqAKeCRbgYz2tzk/Rf8DNRhB0sLbg=\"}]},\"type\":\"Tx\",\"consensus\":7,\"txType\":7,\"data\":\"{\\\"TxInfo\\\":{\\\"blockPrevRandao\\\":4406,\\\"blockTimestamp\\\":1717148110,\\\"input\\\":\\\"608060405234801561001057600080fd5b506108b3806100206000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80630483a7f61461005c5780631629614e1461008c57806327e235e3146100a85780637ad9ad7c146100d8578063afc58189146100f4575b600080fd5b610076600480360381019061007191906105a8565b610110565b60405161008391906106d7565b60405180910390f35b6100a660048036038101906100a19190610615565b610128565b005b6100c260048036038101906100bd91906105a8565b6102a7565b6040516100cf91906106d7565b60405180910390f35b6100f260048036038101906100ed91906105d5565b6102bf565b005b61010e60048036038101906101099190610615565b61037d565b005b60016020528060005260406000206000915090505481565b80806000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156101aa576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101a1906106b7565b60405180910390fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546101f89190610759565b9250508190555081600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825461024e9190610703565b925050819055503373ffffffffffffffffffffffffffffffffffffffff167f3a14c6aa3e15c61c97825b026647b989a91e18aa33b689769475a298922480428360405161029b91906106d7565b60405180910390a25050565b60006020528060005260406000206000915090505481565b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825461030d9190610703565b925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f322d0c4befd4c2dd440740b711488a1638fd7d8eeb25f9dacede84083db428c98360405161037191906106d7565b60405180910390a35050565b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541415610400576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016103f790610697565b60405180910390fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610482576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161047990610697565b60405180910390fd5b806000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546104d09190610703565b9250508190555080600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546105269190610759565b925050819055503373ffffffffffffffffffffffffffffffffffffffff167f867b353f032680758428983522443995b88120a470e436b962c6a9d0d8940af78260405161057391906106d7565b60405180910390a250565b60008135905061058d8161084f565b92915050565b6000813590506105a281610866565b92915050565b6000602082840312156105be576105bd6107f8565b5b60006105cc8482850161057e565b91505092915050565b600080604083850312156105ec576105eb6107f8565b5b60006105fa8582860161057e565b925050602061060b85828601610593565b9150509250929050565b60006020828403121561062b5761062a6107f8565b5b600061063984828501610593565b91505092915050565b600061064f6011836106f2565b915061065a826107fd565b602082019050919050565b60006106726014836106f2565b915061067d82610826565b602082019050919050565b610691816107bf565b82525050565b600060208201905081810360008301526106b081610642565b9050919050565b600060208201905081810360008301526106d081610665565b9050919050565b60006020820190506106ec6000830184610688565b92915050565b600082825260208201905092915050565b600061070e826107bf565b9150610719836107bf565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561074e5761074d6107c9565b5b828201905092915050565b6000610764826107bf565b915061076f836107bf565b925082821015610782576107816107c9565b5b828203905092915050565b60006107988261079f565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b7f4e6f206c6f636b65642062616c616e6365000000000000000000000000000000600082015250565b7f496e73756666696369656e742062616c616e6365000000000000000000000000600082015250565b6108588161078d565b811461086357600080fd5b50565b61086f816107bf565b811461087a57600080fd5b5056fea264697066735822122067e67aed3152cf78a07c4f8842540cf5803e421699b10fbf8f66c015550b0ed464736f6c63430008070033\\\",\\\"recipient\\\":\\\"A8B8Ae6F84709fF80E08aD9d26575c161aCC95f1\\\",\\\"sender\\\":\\\"cED97dA085527Fe7e1772CA59Aa1e64A78143128\\\",\\\"version\\\":0,\\\"virtualMachine\\\":0}}\"}"
txOut = api.txJsonSign(rpcTxIn, privatePkey);
console.log(txOut);
result = api.verif_by_public_str(base64pubkey, base64pubkey.length, message, message_size, signature, signature.length);
console.assert(result == 0, "verify sign fail")
};

</script>