OSDN Git Service

prepare for publish
[bytom/bytom-node-sdk.git] / README.md
1 # Bytom Node.js SDK
2
3 ## Terminology
4
5 ### [Keys](https://bytom.github.io/node-sdk/global.html#Key__anchor)
6
7 Cryptographic keys are the primary authorization mechanism on a blockchain.
8
9 To create accounts or assets, xpub of keys are required. With this sdk, we can
10 `create/delete/list/resetPassword` the key. Please check the 
11 [API doc](https://bytom.github.io/node-sdk/module-KeysApi.html) if you want
12 to operate with keys.
13
14 ### [Account](https://bytom.github.io/node-sdk/global.html#Account__anchor)
15
16 An account is an object in Bytom that tracks ownership of assets on a blockchain. 
17 It's defined under one Bytom node created with one or serveral keys.  
18
19 [Related API](https://bytom.github.io/node-sdk/module-AccountsApi.html)
20
21 ### [Asset](https://bytom.github.io/node-sdk/global.html#Asset__anchor)
22
23 An asset is a type of value that can be issued on a blockchain. All units of
24 a given asset are fungible. Units of an asset can be transacted directly
25 between parties without the involvement of the issuer.
26
27 [Related API](https://bytom.github.io/node-sdk/module-AssetsApi.html)
28
29 ### [Transaction](https://bytom.github.io/node-sdk/global.html#Transaction__anchor)
30
31 Blockchain is chain of blocks, while block consists of numbers of transactions.
32
33 [Related API](https://bytom.github.io/node-sdk/module-TransactionsApi.html)
34
35 ### [Unspent Output(UTXO)](https://bytom.github.io/node-sdk/global.html#UnspentOutput__anchor)
36
37 Bytom is UTXO based blockchain. One transaction spend some UTXOs, and produces new UTXOs.
38
39 [Related API](https://bytom.github.io/node-sdk/module-UnspentOutputsApi.html)
40
41 ### [Balance](https://bytom.github.io/node-sdk/global.html#Balance__anchor)
42
43 Any balance on the blockchain is simply a summation of UTXOs. In one bytomd, balance means
44 summation of UTXOs of one account.
45
46 [Related API](https://bytom.github.io/node-sdk/module-BalancesApi.html)
47
48 ## Usage
49
50 ### In your code
51
52 ```
53 const bytom = require('bytom-sdk')
54 const url = 'http://localhost:9888'
55
56 // access token is required when client is not in same origin with the request bytom node
57 const accessToken = ''
58
59 const client = new bytom.Client(url, accessToken)
60 ```
61
62 ## Interaction with bytom
63
64 We will walk you through the process to issue some assets.
65
66 ### Step 1: create a key
67
68 ```javascript
69 const keyPromise = client.keys.create('alias', 'password')
70 ```
71
72 It will create a key whose alias is 'alias' while password is 'password'.
73
74 ### Step 2: create a account
75
76 ```javascript
77 const accountPromise = keyPromise.then(key => client.accounts.create([key.xpub], 1, 'account'))
78 ```
79
80 ### Step 3: create account address
81
82 const addressPromise = accountPromise.then(account => {
83   return client.accounts.createReceiverById(account.id)
84 })
85
86 ### Step 4: create asset
87
88 ```javascript
89 const definition = {name: "GOLD", symobol: "GOLD", decimals: 8, description: {}}
90 const assetPromise = keyPromise.then(key => {
91   return client.assets.create([key.xpub], 1, 'asset', definition)
92 })
93 ```
94
95 ### Step 5: issue asset
96
97 #### First, build the transaction
98
99 ```javascript
100 const buildPromise = Promise.all([accountPromise, addressPromise, assetPromise]).then(([account, address, asset]) => {
101   const issueAction = {
102     amount: 10000000000,
103     asset_alias: asset.alias,
104     type: 'issue'
105   }
106
107   const gasAction = {
108     type: 'spend_account',
109     account_alias: account.alias,
110     asset_alias: 'BTM',
111     amount: 50000000
112   }
113
114   const controlAction = {
115     type: 'control_address',
116     amount: 10000000000,
117     asset_alias: asset.alias,
118     address: address.address
119   }
120   
121   return client.transactions.build(null, [issueAction, gasAction, controlAction])
122 })
123
124 ```
125
126 #### Second, sign the transaction
127
128 ```javascript
129 const signPromise = buildPromise.then(transactionTemplate => {
130   return client.transactions.sign(transactionTemplate, 'password')
131 })
132 ```
133
134 #### Finally, submit the signed transaction to the bytom network
135
136 ```javascript
137 signPromise.then(signed => {
138   return client.transactions.submit(signed.transaction.raw_transaction)
139 })
140 ```