1.6.4 • Published 3 years ago

@volt.id/sdk v1.6.4

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

@volt.id/sdk

Volt wallet sdk for browser. currently only support Bitcoin SV.

Install

npm i -S @volt.id/sdk

Usage

Init

import { Bsv } from "@volt.id/sdk";
const bsv = new Bsv();

Connect to volt

Connect

Connect to volt wallet, if user has connected before and session is not expired, volt sdk will reconnect automatically on page loaded, otherwise, volt sdk will popup a qrcode to user to scan to connect.

await bsv.connectAccount({ network: "mainnet" }); // network: 'mainnet' | 'testnet'

note: don't call connectAccount on page load event, call connectAccount when use click your connect button and listen the accountChanged Event.

// bad
window.onload = function() {
  await bsv.connectAccount({ network: "mainnet" });
}



// good
bsv.on('accountChanged', (depositAddress) => {
  if (depositAddress) {
    console.log('connected', depositAddress)
    // your can transfer now
  } else {
    console.log('not connected')
  }
})

yourConnectButton.onclick = async function() {
  await bsv.connectAccount({network: 'mainnet'})
}

// change a account should disconnect before connecting
yourChangeAccountButton.onclick = async function() {
  await bsv.disconnectAccount()
  await bsv.connectAccount({network: 'mainnet'})
}

Disconnect

await bsv.disconnectAccount();

Get connection info

Get current network

const network = await bsv.getNetwork();

Get current deposit address

const depositAddress = await bsv.getDepositAddress();

Get current wallet bsv balance

const bsvBalance = await bsv.getBsvBalance(); // returns the current wallet balance can transfer (unit: sat)   e.g., {free: 300222}

Get current wallet sensible fungible token balance (list)

const sensibleFtBalance = await bsv.getSensibleFtBalance();

/**

return :

[
  {
    codehash: "777e4dd291059c9f7a0fd563f7204576dcceb791",
    free: "4167139",
    genesis: "c52b5ee305834e3ceb97ee931ed5e453543ba2d8",
    tokenDecimal: 8,
    tokenName: "bsv/TSC lp token",
  }
]

 * /

Transfer

Transfer bsv

note: where transfer bsv, list.length should be 1 and receivers.length should be 1.

const transferRes = await bsv.batchTransfer({
  noBroadcast: false, //  if set true, will not broadcast to bsv network, you need to broadcast it manually
  list: [
    {
      type: "bsv",
      data: {
        receivers: [{ address: depositeAddress, amount: "8000" }],
      },
    },
  ],
});

Transfer sensible fungible token

note: where transfer sensible fungible token, list.length should be 1 and receivers.length should be 1.

const transferRes = await bsv.batchTransfer({
  noBroadcast: true,
  list: [
    {
      type: "sensibleFt",
      data: {
        codehash: "514776383faa66e4a65808904d4d6724e4774fbe",
        genesis: "c57dd8e75cd6a3d11c4628328c9fa5f2d9d452b2",
        receivers: [{ address: depositeAddress, amount: "303" }],
      },
    },
  ],
});

Transfer bsv and sensible fungible token

note: where transfer sensible fungible token, list.length should be 2 and receivers.length should be 1.

const transferRes = await bsv.batchTransfer({
  noBroadcast: true,
  list: [
    {
      type: "bsv",
      data: {
        receivers: [{ address: depositeAddress, amount: "8000" }],
      },
    },
    {
      type: "sensibleFt",
      data: {
        codehash: "514776383faa66e4a65808904d4d6724e4774fbe",
        genesis: "c57dd8e75cd6a3d11c4628328c9fa5f2d9d452b2",
        receivers: [{ address: depositeAddress, amount: "303" }],
      },
    },
  ],
});

Sign

signTx

Get signature and publicKey, currently do not support sensibleFt utxo, p2pkh utxo.

const res = await await bsv.signTx({
  txHex: "xxx", // rawtx
  scriptHex: "xxx", // input locking script hex
  satoshis: "10000", // input satoshis
  inputIndex: 0, // input index
  address: "", // user address, usally deposit address
  sigHashType: 65, // optinal, default
});

/**

return:

{
  publicKey: 'xxx',
  sig: 'xxx'
}

 */

Event

accountChanged

Emitted after connected/disconnected/account changed

bsv.on('accountChanged', funciton(depositAddress) {
  // depositAddress is a bsv address (string) or null
})

networkChanged

Emitted when network changed

bsv.on('networkChanged', function(network) {})

close

Emitted after disconnected

bsv.on('close', function() {})