8月18日(周六)成都源创会火热报名中,四位一线行业大牛与你面对面,探讨区块链技术热潮下的冷思考。
Watch Star Fork

Bytom Blockchain / node-sdkNodeJS

加入码云
与超过 300 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
比原链(Bytom)的 Node 开发包,比原链是一种多样性比特资产的区块链交互协议
Loading...
README.md 3.95 KB

Bytom Node.js SDK

Terminology

Keys

Cryptographic keys are the primary authorization mechanism on a blockchain.

To create accounts or assets, xpub of keys are required. With this sdk, we can create/delete/list/resetPassword the key. Please check the API doc if you want to operate with keys.

Account

An account is an object in Bytom that tracks ownership of assets on a blockchain. It's defined under one Bytom node created with one or serveral keys.

Related API

Asset

An asset is a type of value that can be issued on a blockchain. All units of a given asset are fungible. Units of an asset can be transacted directly between parties without the involvement of the issuer.

Related API

Transaction

Blockchain is chain of blocks, while block consists of numbers of transactions.

Related API

Unspent Output(UTXO)

Bytom is UTXO based blockchain. One transaction spend some UTXOs, and produces new UTXOs.

Related API

Balance

Any balance on the blockchain is simply a summation of UTXOs. In one bytomd, balance means summation of UTXOs of one account.

Related API

Usage

In your code

const bytom = require('bytom-sdk')
const url = 'http://localhost:9888'

// access token is required when client is not in same origin
// with the request bytom node
const accessToken = ''

const client = new bytom.Client(url, accessToken)

Interaction with bytom

We will walk you through the process to issue some assets.

Step 1: create a key

const keyPromise = client.keys.create('alias', 'password')

It will create a key whose alias is 'alias' while password is 'password'.

Step 2: create a account

const accountPromise = keyPromise.then(key => {
 client.accounts.create([key.xpub], 1, 'account')
})

Step 3: create account address

const addressPromise = accountPromise.then(account => {
  return client.accounts.createReceiverById(account.id)
})

Step 4: create asset

const definition = {
  name: "GOLD",
  symobol: "GOLD",
  decimals: 8,
  description: {}
}
const assetPromise = keyPromise.then(key => {
  return client.assets.create([key.xpub], 1, 'asset', definition)
})

Step 5: issue asset

First, build the transaction

const buildPromise = Promise.all([
  accountPromise,
  addressPromise,
  assetPromise]
  ).then(([account, address, asset]) => {
  const issueAction = {
    amount: 10000000000,
    asset_alias: asset.alias,
    type: 'issue'
  }

  const gasAction = {
    type: 'spend_account',
    account_alias: account.alias,
    asset_alias: 'BTM',
    amount: 50000000
  }

  const controlAction = {
    type: 'control_address',
    amount: 10000000000,
    asset_alias: asset.alias,
    address: address.address
  }
  
  return client.transactions.build(null,
  [issueAction, gasAction, controlAction])
})

Second, sign the transaction

const signPromise = buildPromise.then(transactionTemplate => {
  return client.transactions.sign(transactionTemplate, 'password')
})

Finally, submit the signed transaction to the bytom network

signPromise.then(signed => {
  return client.transactions.submit(signed.transaction.raw_transaction)
})

项目点评 ( 0 )

你可以在登录后,发表评论