openchain-nodejs-ts-yxl 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +66 -0
- package/_config.yml +1 -0
- package/doc/SDK_CN.md +2003 -0
- package/example/atp10TokenDemo.js +319 -0
- package/example/exchange.js +184 -0
- package/example/offlineSignatureDemo.js +83 -0
- package/example/submitTransactionDemo.js +92 -0
- package/index.d.ts +186 -0
- package/index.js +9 -0
- package/lib/account/index.js +234 -0
- package/lib/blockchain/block.js +380 -0
- package/lib/blockchain/transaction.js +264 -0
- package/lib/common/ctp.js +5 -0
- package/lib/common/operation/accountSetMetadata.js +55 -0
- package/lib/common/operation/accountSetPrivilege.js +90 -0
- package/lib/common/operation/activateAccount.js +58 -0
- package/lib/common/operation/contractCreate.js +72 -0
- package/lib/common/operation/contractInvokeByAsset.js +75 -0
- package/lib/common/operation/contractInvokeByBU.js +53 -0
- package/lib/common/operation/createLog.js +44 -0
- package/lib/common/operation/ctp10TokenApprove.js +59 -0
- package/lib/common/operation/ctp10TokenAssign.js +59 -0
- package/lib/common/operation/ctp10TokenChangeOwner.js +58 -0
- package/lib/common/operation/ctp10TokenIssue.js +78 -0
- package/lib/common/operation/ctp10TokenTransfer.js +59 -0
- package/lib/common/operation/ctp10TokenTransferFrom.js +60 -0
- package/lib/common/operation/issueAsset.js +35 -0
- package/lib/common/operation/payAsset.js +62 -0
- package/lib/common/operation/payCoin.js +44 -0
- package/lib/common/util.js +880 -0
- package/lib/contract/index.js +212 -0
- package/lib/crypto/protobuf/bundle.json +1643 -0
- package/lib/exception/customErrors.js +56 -0
- package/lib/exception/errors.js +240 -0
- package/lib/exception/index.js +8 -0
- package/lib/operation/account.js +193 -0
- package/lib/operation/asset.js +106 -0
- package/lib/operation/bu.js +52 -0
- package/lib/operation/contract.js +184 -0
- package/lib/operation/ctp10Token.js +394 -0
- package/lib/operation/index.js +30 -0
- package/lib/operation/log.js +47 -0
- package/lib/sdk.js +70 -0
- package/lib/token/asset.js +79 -0
- package/lib/token/ctp10Token.js +301 -0
- package/lib/token/index.js +17 -0
- package/lib/util/index.js +86 -0
- package/openchain-sdk-nodejs.iml +9 -0
- package/package.json +39 -0
- package/test/Ctp10Token.test.js +96 -0
- package/test/account.test.js +132 -0
- package/test/accountActivateOperation.test.js +99 -0
- package/test/accountSetMetadata.test.js +102 -0
- package/test/accountSetPrivilege.test.js +66 -0
- package/test/asset.test.js +63 -0
- package/test/assetIssueOperation.test.js +77 -0
- package/test/assetSendOperation.test.js +103 -0
- package/test/blob.test.js +128 -0
- package/test/block.test.js +165 -0
- package/test/buSendOperation.test.js +64 -0
- package/test/contract.test.js +64 -0
- package/test/contractCreateOperation.test.js +41 -0
- package/test/contractCreateTransaction.test.js +116 -0
- package/test/contractInvokeByAssetOperation.test.js +109 -0
- package/test/contractInvokeByBUOperation.test.js +107 -0
- package/test/ctp10TokenApproveOperation.test.js +98 -0
- package/test/ctp10TokenAssignOperation.test.js +99 -0
- package/test/ctp10TokenChangeOwnerOperation.test.js +98 -0
- package/test/ctp10TokenIssueOperation.test.js +40 -0
- package/test/ctp10TokenIssueOperationTransaction.test.js +106 -0
- package/test/ctp10TokenTransferFromOperation.test.js +101 -0
- package/test/ctp10TokenTransferOperation.test.js +98 -0
- package/test/log.transaction.test.js +103 -0
- package/test/transaction.test.js +166 -0
- package/test/util.test.js +93 -0
@@ -0,0 +1,319 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('chai').should();
|
4
|
+
const encryption = require('yxchain-encryption-nodejs');
|
5
|
+
const OpenChainSDK = require('openchain-sdk-nodejs');
|
6
|
+
// const OpenChainSDK = require('../index');
|
7
|
+
|
8
|
+
const sdk = new OpenChainSDK({
|
9
|
+
host: 'localhost/node/api',
|
10
|
+
});
|
11
|
+
|
12
|
+
describe('The demo of atp10Token', function() {
|
13
|
+
|
14
|
+
// Issue Unlimited Atp10Token
|
15
|
+
it('IssueUnlimitedAtp10Token', async() => {
|
16
|
+
const issuerPrivateKey = 'issuer private key';
|
17
|
+
// The token name
|
18
|
+
const name = 'MYTESTTOKEN';
|
19
|
+
// The token code
|
20
|
+
const code = 'MYTESTTOKEN';
|
21
|
+
// The apt token icon
|
22
|
+
const icon = '';
|
23
|
+
// The apt token version
|
24
|
+
const version = '1.0';
|
25
|
+
// The token total supply number
|
26
|
+
const totalSupply = 0;
|
27
|
+
// The token decimals
|
28
|
+
const decimals = 8;
|
29
|
+
// The token now supply number, which includes the dicimals.
|
30
|
+
// If decimals is 8 and you want to issue 10 tokens now, the nowSupply must be 10 * 10 ^ 8, like below.
|
31
|
+
const nowSupply = sdk.utils.unitWithDecimals('10', decimals);
|
32
|
+
// Description
|
33
|
+
const description = 'test unlimited issuance of apt1.0 token';
|
34
|
+
// The operation notes
|
35
|
+
const metadata = 'test the unlimited issuance of apt1.0 token';
|
36
|
+
// the unit is MO
|
37
|
+
const gasPrice = '1000';
|
38
|
+
// feeLimit, the unit is MO
|
39
|
+
const feeLimit = '5003000000';
|
40
|
+
// Transaction initiation account's Nonce + 1
|
41
|
+
const nonce = '87';
|
42
|
+
|
43
|
+
// Get the account address
|
44
|
+
const issuerAddress = getAddressByPrivateKey(issuerPrivateKey);
|
45
|
+
|
46
|
+
|
47
|
+
// Build operation
|
48
|
+
const assetIssueOperation = sdk.operation.assetIssueOperation({
|
49
|
+
sourceAddress: issuerAddress,
|
50
|
+
code,
|
51
|
+
assetAmount: nowSupply,
|
52
|
+
metadata,
|
53
|
+
});
|
54
|
+
|
55
|
+
if (assetIssueOperation.errorCode !== 0) {
|
56
|
+
console.log(assetIssueOperation);
|
57
|
+
return assetIssueOperation;
|
58
|
+
}
|
59
|
+
|
60
|
+
const apt10Obj = {
|
61
|
+
name,
|
62
|
+
code,
|
63
|
+
description,
|
64
|
+
decimals,
|
65
|
+
totalSupply,
|
66
|
+
icon,
|
67
|
+
};
|
68
|
+
const key = `asset_property_${code}`;
|
69
|
+
const value = JSON.stringify(apt10Obj);
|
70
|
+
const accountSetMetadataOperation = sdk.operation.accountSetMetadataOperation({
|
71
|
+
key,
|
72
|
+
value,
|
73
|
+
version,
|
74
|
+
});
|
75
|
+
|
76
|
+
if (accountSetMetadataOperation.errorCode !== 0) {
|
77
|
+
console.log(accountSetMetadataOperation);
|
78
|
+
return accountSetMetadataOperation;
|
79
|
+
}
|
80
|
+
|
81
|
+
let args = {
|
82
|
+
privateKey: issuerPrivateKey,
|
83
|
+
sourceAddress: issuerAddress,
|
84
|
+
gasPrice,
|
85
|
+
feeLimit,
|
86
|
+
nonce,
|
87
|
+
operation: [ assetIssueOperation, accountSetMetadataOperation ],
|
88
|
+
metadata,
|
89
|
+
};
|
90
|
+
const result = await submitTransaction(args);
|
91
|
+
console.log(result);
|
92
|
+
});
|
93
|
+
|
94
|
+
// Issue limited Atp10Token
|
95
|
+
it('IssuelimitedAtp10Token', async() => {
|
96
|
+
const issuerPrivateKey = 'issuer private key';
|
97
|
+
// The token name
|
98
|
+
const name = 'MYTESTTOKEN';
|
99
|
+
// The token code
|
100
|
+
const code = 'MYTESTTOKEN';
|
101
|
+
// The apt token icon
|
102
|
+
const icon = '';
|
103
|
+
// The apt token version
|
104
|
+
const version = '1.0';
|
105
|
+
// The token decimals
|
106
|
+
const decimals = 1;
|
107
|
+
// The token total supply number
|
108
|
+
const totalSupply = 1000000000;
|
109
|
+
// The token now supply number
|
110
|
+
// If decimals is 1 and you want to issue 10 tokens now, the nowSupply must be 10 * 10 ^ 1, like below.
|
111
|
+
const nowSupply = sdk.utils.unitWithDecimals('10', decimals);
|
112
|
+
// Description
|
113
|
+
const description = 'test limited issuance of apt1.0 token';
|
114
|
+
// The operation notes
|
115
|
+
const metadata = 'test limited issuance of apt1.0 token';
|
116
|
+
// the unit is MO
|
117
|
+
const gasPrice = '1000';
|
118
|
+
// feeLimit, the unit is MO
|
119
|
+
const feeLimit = '5003000000';
|
120
|
+
// Transaction initiation account's Nonce + 1
|
121
|
+
const nonce = '88';
|
122
|
+
|
123
|
+
// Get the account address
|
124
|
+
const issuerAddress = getAddressByPrivateKey(issuerPrivateKey);
|
125
|
+
|
126
|
+
|
127
|
+
// Build operation
|
128
|
+
const assetIssueOperation = sdk.operation.assetIssueOperation({
|
129
|
+
sourceAddress: issuerAddress,
|
130
|
+
code,
|
131
|
+
assetAmount: nowSupply,
|
132
|
+
metadata,
|
133
|
+
});
|
134
|
+
|
135
|
+
if (assetIssueOperation.errorCode !== 0) {
|
136
|
+
console.log(assetIssueOperation);
|
137
|
+
return assetIssueOperation;
|
138
|
+
}
|
139
|
+
|
140
|
+
const apt10Obj = {
|
141
|
+
name,
|
142
|
+
code,
|
143
|
+
description,
|
144
|
+
decimals,
|
145
|
+
totalSupply,
|
146
|
+
icon,
|
147
|
+
};
|
148
|
+
const key = `asset_property_${code}`;
|
149
|
+
const value = JSON.stringify(apt10Obj);
|
150
|
+
const accountSetMetadataOperation = sdk.operation.accountSetMetadataOperation({
|
151
|
+
key,
|
152
|
+
value,
|
153
|
+
version,
|
154
|
+
});
|
155
|
+
|
156
|
+
if (accountSetMetadataOperation.errorCode !== 0) {
|
157
|
+
console.log(accountSetMetadataOperation);
|
158
|
+
return accountSetMetadataOperation;
|
159
|
+
}
|
160
|
+
|
161
|
+
let args = {
|
162
|
+
privateKey: issuerPrivateKey,
|
163
|
+
sourceAddress: issuerAddress,
|
164
|
+
gasPrice,
|
165
|
+
feeLimit,
|
166
|
+
nonce,
|
167
|
+
operation: [ assetIssueOperation, accountSetMetadataOperation ],
|
168
|
+
metadata,
|
169
|
+
};
|
170
|
+
const result = await submitTransaction(args);
|
171
|
+
console.log(result);
|
172
|
+
});
|
173
|
+
|
174
|
+
|
175
|
+
it('SendAtp10Token', async() => {
|
176
|
+
// The account private key to send atp1.0 token
|
177
|
+
const senderPrivateKey = 'Sender Private Key';
|
178
|
+
// The account to receive atp 1.0 token
|
179
|
+
const destAddress = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
180
|
+
// The token code
|
181
|
+
const code = 'MYTESTTOKEN';
|
182
|
+
// The token amount to be sent
|
183
|
+
const amount = '1000000000';
|
184
|
+
// The operation notes
|
185
|
+
const metadata = 'test one off issue apt1.0 token';
|
186
|
+
// the unit is MO
|
187
|
+
const gasPrice = '1000';
|
188
|
+
// maximum cost, the unit is MO
|
189
|
+
const feeLimit = '1000000';
|
190
|
+
// Transaction initiation account's Nonce + 1
|
191
|
+
const nonce = '100';
|
192
|
+
|
193
|
+
// Get the account address
|
194
|
+
const senderAddress = getAddressByPrivateKey(senderPrivateKey);
|
195
|
+
// Check whether the destination account is activated
|
196
|
+
let status = await checkAccountStatus(destAddress);
|
197
|
+
|
198
|
+
if (!status) {
|
199
|
+
let accountInfo = await accountActivate({
|
200
|
+
privateKey: senderPrivateKey,
|
201
|
+
sourceAddress: senderAddress,
|
202
|
+
destAddress: destAddress,
|
203
|
+
nonce: '22',
|
204
|
+
});
|
205
|
+
}
|
206
|
+
|
207
|
+
const operation = sdk.operation.assetSendOperation({
|
208
|
+
sourceAddress: senderAddress,
|
209
|
+
destAddress: destAddress,
|
210
|
+
code: code,
|
211
|
+
issuer: senderAddress,
|
212
|
+
assetAmount: amount,
|
213
|
+
metadata: metadata,
|
214
|
+
});
|
215
|
+
|
216
|
+
if (operation.errorCode === 0) {
|
217
|
+
let args = {
|
218
|
+
privateKey: senderPrivateKey,
|
219
|
+
sourceAddress: senderAddress,
|
220
|
+
gasPrice,
|
221
|
+
feeLimit,
|
222
|
+
nonce,
|
223
|
+
operation,
|
224
|
+
};
|
225
|
+
const result = await submitTransaction(args);
|
226
|
+
console.log(result);
|
227
|
+
}
|
228
|
+
|
229
|
+
});
|
230
|
+
|
231
|
+
// Check account status
|
232
|
+
async function checkAccountStatus(address) {
|
233
|
+
const data = await sdk.account.isActivated(address);
|
234
|
+
return data.result.isActivated;
|
235
|
+
}
|
236
|
+
|
237
|
+
// Activate account
|
238
|
+
async function accountActivate(args) {
|
239
|
+
let operation = sdk.operation.accountActivateOperation({
|
240
|
+
destAddress: args.destAddress,
|
241
|
+
initBalance: '1000000',
|
242
|
+
});
|
243
|
+
|
244
|
+
const result = await submitTransaction({
|
245
|
+
privateKey: args.privateKey,
|
246
|
+
sourceAddress: args.sourceAddress,
|
247
|
+
gasPrice: '1000',
|
248
|
+
feeLimit: '1000000',
|
249
|
+
nonce: args.nonce,
|
250
|
+
operation,
|
251
|
+
});
|
252
|
+
|
253
|
+
return result.errorCode === 0;
|
254
|
+
}
|
255
|
+
|
256
|
+
// Get address from private key
|
257
|
+
function getAddressByPrivateKey(privatekey) {
|
258
|
+
const KeyPair = encryption.keypair;
|
259
|
+
let publicKey = KeyPair.getEncPublicKey(privatekey);
|
260
|
+
return KeyPair.getAddress(publicKey);
|
261
|
+
}
|
262
|
+
|
263
|
+
// Submit
|
264
|
+
async function submitTransaction(args) {
|
265
|
+
let operations = [];
|
266
|
+
|
267
|
+
if (Array.isArray(args.operation)) {
|
268
|
+
args.operation.forEach(function(item) {
|
269
|
+
operations.push(item.result.operation);
|
270
|
+
})
|
271
|
+
} else {
|
272
|
+
operations.push(args.operation.result.operation);
|
273
|
+
}
|
274
|
+
|
275
|
+
// // 1. Build Operation
|
276
|
+
// const operationItem = args.operation.result.operation;
|
277
|
+
|
278
|
+
let opt = {
|
279
|
+
sourceAddress: args.sourceAddress,
|
280
|
+
gasPrice: args.gasPrice,
|
281
|
+
feeLimit: args.feeLimit,
|
282
|
+
nonce: args.nonce,
|
283
|
+
operations,
|
284
|
+
};
|
285
|
+
|
286
|
+
if (args.metadata) {
|
287
|
+
opt.metadata = JSON.stringify(args.metadata);
|
288
|
+
}
|
289
|
+
|
290
|
+
// 2. Build blob
|
291
|
+
let blobInfo = sdk.transaction.buildBlob(opt);
|
292
|
+
|
293
|
+
if (blobInfo.errorCode !== 0) {
|
294
|
+
console.log(blobInfo);
|
295
|
+
return;
|
296
|
+
}
|
297
|
+
|
298
|
+
let blob = blobInfo.result.transactionBlob;
|
299
|
+
|
300
|
+
// 3. Sign blob
|
301
|
+
let signatureInfo = sdk.transaction.sign({
|
302
|
+
privateKeys: [ args.privateKey ],
|
303
|
+
blob,
|
304
|
+
});
|
305
|
+
|
306
|
+
if (signatureInfo.errorCode !== 0) {
|
307
|
+
console.log(signatureInfo);
|
308
|
+
return;
|
309
|
+
}
|
310
|
+
|
311
|
+
let signature = signatureInfo.result.signatures;
|
312
|
+
// 4. Submit transaction
|
313
|
+
return await sdk.transaction.submit({
|
314
|
+
blob,
|
315
|
+
signature: signature,
|
316
|
+
});
|
317
|
+
}
|
318
|
+
|
319
|
+
});
|
@@ -0,0 +1,184 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('chai').should();
|
4
|
+
const BigNumber = require('bignumber.js');
|
5
|
+
const OpenChainSDK = require('openchain-sdk');
|
6
|
+
|
7
|
+
const sdk = new OpenChainSDK({
|
8
|
+
host: 'localhost/node/api',
|
9
|
+
});
|
10
|
+
|
11
|
+
describe('The demo of openchain-sdk for exchange ', function() {
|
12
|
+
|
13
|
+
it('Create account', async() => {
|
14
|
+
const keypair = await sdk.account.create();
|
15
|
+
console.log(keypair);
|
16
|
+
});
|
17
|
+
|
18
|
+
it('Get account information', async() => {
|
19
|
+
const address = 'adxSqKcX8wGCMKhzNUBoDWfbeQaMhfnGdtyG2';
|
20
|
+
const info = await sdk.account.getInfo(address);
|
21
|
+
console.log(info);
|
22
|
+
});
|
23
|
+
|
24
|
+
it('Check address validity', async() => {
|
25
|
+
const address = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
26
|
+
const data = await sdk.account.checkValid(address);
|
27
|
+
console.log(data);
|
28
|
+
});
|
29
|
+
|
30
|
+
it('Get account balance', async() => {
|
31
|
+
const address = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
32
|
+
const data = await sdk.account.getBalance(address);
|
33
|
+
console.log(data);
|
34
|
+
});
|
35
|
+
|
36
|
+
it('Get account nonce', async() => {
|
37
|
+
const address = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
38
|
+
const data = await sdk.account.getNonce(address);
|
39
|
+
console.log(data);
|
40
|
+
});
|
41
|
+
|
42
|
+
it('Get the latest block number', async() => {
|
43
|
+
const data = await sdk.block.getNumber();
|
44
|
+
console.log(data);
|
45
|
+
});
|
46
|
+
|
47
|
+
it('Check local node block synchronization status', async() => {
|
48
|
+
const data = await sdk.block.checkStatus();
|
49
|
+
console.log(data);
|
50
|
+
});
|
51
|
+
|
52
|
+
it('Get transactions for a blockNumber', async() => {
|
53
|
+
const blockNumber = '100';
|
54
|
+
const data = await sdk.block.getTransactions(blockNumber);
|
55
|
+
console.log(data);
|
56
|
+
});
|
57
|
+
|
58
|
+
it('Get block information', async() => {
|
59
|
+
const data = await sdk.block.getInfo('100');
|
60
|
+
console.log(data);
|
61
|
+
});
|
62
|
+
|
63
|
+
it('Get the latest block information', async() => {
|
64
|
+
const data = await sdk.block.getLatestInfo();
|
65
|
+
console.log(data);
|
66
|
+
});
|
67
|
+
|
68
|
+
it('Get the validators in the specified blockNumber', async() => {
|
69
|
+
const data = await sdk.block.getValidators('100');
|
70
|
+
console.log(data);
|
71
|
+
});
|
72
|
+
|
73
|
+
it('Get the latest validators', async() => {
|
74
|
+
const data = await sdk.block.getLatestValidators();
|
75
|
+
console.log(data);
|
76
|
+
});
|
77
|
+
|
78
|
+
it('Get block rewards and validator rewards in the specified blockNumber', async() => {
|
79
|
+
const data = await sdk.block.getReward('100');
|
80
|
+
console.log(data);
|
81
|
+
});
|
82
|
+
|
83
|
+
it('Get block rewards and validator rewards in the latest blockNumber', async() => {
|
84
|
+
const data = await sdk.block.getLatestReward();
|
85
|
+
console.log(data);
|
86
|
+
});
|
87
|
+
|
88
|
+
it('Get fees in the specified blockNumber', async() => {
|
89
|
+
const data = await sdk.block.getFees('100');
|
90
|
+
console.log(data);
|
91
|
+
});
|
92
|
+
|
93
|
+
it('Get fees in the latest blockNumber', async() => {
|
94
|
+
const data = await sdk.block.getLatestFees();
|
95
|
+
console.log(data);
|
96
|
+
});
|
97
|
+
|
98
|
+
// ====================================
|
99
|
+
// Send BU contains 4 steps:
|
100
|
+
// 1. build operation (buSendOperation)
|
101
|
+
// 2. build blob
|
102
|
+
// 3. sign blob with sender private key
|
103
|
+
// 4. submit transaction
|
104
|
+
// ====================================
|
105
|
+
it('Send BU', async() => {
|
106
|
+
const senderPrivateKey = 'sender private key';
|
107
|
+
const senderAddress = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
108
|
+
const receiverAddress = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
109
|
+
|
110
|
+
const accountInfo = await sdk.account.getNonce(senderAddress);
|
111
|
+
|
112
|
+
if (accountInfo.errorCode !== 0) {
|
113
|
+
console.log(accountInfo);
|
114
|
+
return;
|
115
|
+
}
|
116
|
+
let nonce = accountInfo.result.nonce;
|
117
|
+
// nonce + 1
|
118
|
+
nonce = new BigNumber(nonce).plus(1).toString(10);
|
119
|
+
|
120
|
+
// ====================================
|
121
|
+
// 1. build operation (buSendOperation)
|
122
|
+
// ====================================
|
123
|
+
const operationInfo = sdk.operation.buSendOperation({
|
124
|
+
sourceAddress: senderAddress,
|
125
|
+
destAddress: receiverAddress,
|
126
|
+
buAmount: '7000',
|
127
|
+
metadata: 'send bu demo',
|
128
|
+
});
|
129
|
+
|
130
|
+
if (operationInfo.errorCode !== 0) {
|
131
|
+
console.log(operationInfo);
|
132
|
+
return;
|
133
|
+
}
|
134
|
+
|
135
|
+
const operationItem = operationInfo.result.operation;
|
136
|
+
|
137
|
+
// ====================================
|
138
|
+
// 2. build blob
|
139
|
+
// ====================================
|
140
|
+
const blobInfo = sdk.transaction.buildBlob({
|
141
|
+
sourceAddress: senderAddress,
|
142
|
+
gasPrice: '1000',
|
143
|
+
feeLimit: '306000',
|
144
|
+
nonce,
|
145
|
+
operations: [ operationItem ],
|
146
|
+
});
|
147
|
+
|
148
|
+
if (blobInfo.errorCode !== 0) {
|
149
|
+
console.log(blobInfo);
|
150
|
+
return;
|
151
|
+
}
|
152
|
+
|
153
|
+
const blob = blobInfo.result.transactionBlob;
|
154
|
+
|
155
|
+
// ====================================
|
156
|
+
// 3. sign blob with sender private key
|
157
|
+
// ====================================
|
158
|
+
let signatureInfo = sdk.transaction.sign({
|
159
|
+
privateKeys: [ senderPrivateKey ],
|
160
|
+
blob,
|
161
|
+
});
|
162
|
+
|
163
|
+
if (signatureInfo.errorCode !== 0) {
|
164
|
+
console.log(signatureInfo);
|
165
|
+
return;
|
166
|
+
}
|
167
|
+
|
168
|
+
const signature = signatureInfo.result.signatures;
|
169
|
+
|
170
|
+
// ====================================
|
171
|
+
// 4. submit transaction
|
172
|
+
// ====================================
|
173
|
+
const transactionInfo = await sdk.transaction.submit({
|
174
|
+
blob,
|
175
|
+
signature: signature,
|
176
|
+
});
|
177
|
+
|
178
|
+
if (transactionInfo.errorCode !== 0) {
|
179
|
+
console.log(transactionInfo);
|
180
|
+
}
|
181
|
+
console.log(transactionInfo);
|
182
|
+
});
|
183
|
+
|
184
|
+
});
|
@@ -0,0 +1,83 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('chai').should();
|
4
|
+
const OpenChainSDK = require('openchain-sdk');
|
5
|
+
|
6
|
+
const sdk = new OpenChainSDK({
|
7
|
+
host: 'localhost/node/api',
|
8
|
+
});
|
9
|
+
|
10
|
+
describe('The demo of offline signature', function() {
|
11
|
+
|
12
|
+
// ====================================
|
13
|
+
// Take `buSendOperation` as an example
|
14
|
+
// Offline signature contains 2 steps:
|
15
|
+
// step1. Get blob
|
16
|
+
// step2. Sign blob with sender private key
|
17
|
+
// ====================================
|
18
|
+
it('The demo of offline signature', async() => {
|
19
|
+
const senderPrivateKey = 'sender private key';
|
20
|
+
const senderAddress = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
21
|
+
const receiverAddress = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
22
|
+
// The unit is MO
|
23
|
+
const gasPrice = '1000';
|
24
|
+
// The unit is MO
|
25
|
+
const feeLimit = '306000';
|
26
|
+
// The unit is MO
|
27
|
+
const buAmount = '7000';
|
28
|
+
// Transaction initiation account's Nonce + 1
|
29
|
+
const nonce = '10';
|
30
|
+
const metadata = 'send bu demo';
|
31
|
+
|
32
|
+
// build operation (buSendOperation)
|
33
|
+
const operationInfo = sdk.operation.buSendOperation({
|
34
|
+
sourceAddress: senderAddress,
|
35
|
+
destAddress: receiverAddress,
|
36
|
+
buAmount,
|
37
|
+
metadata,
|
38
|
+
});
|
39
|
+
|
40
|
+
if (operationInfo.errorCode !== 0) {
|
41
|
+
console.log(operationInfo);
|
42
|
+
return;
|
43
|
+
}
|
44
|
+
|
45
|
+
const operationItem = operationInfo.result.operation;
|
46
|
+
|
47
|
+
// ====================================
|
48
|
+
// step1. Get blob
|
49
|
+
// ====================================
|
50
|
+
const blobInfo = sdk.transaction.buildBlob({
|
51
|
+
sourceAddress: senderAddress,
|
52
|
+
gasPrice,
|
53
|
+
feeLimit,
|
54
|
+
nonce,
|
55
|
+
operations: [ operationItem ],
|
56
|
+
});
|
57
|
+
|
58
|
+
if (blobInfo.errorCode !== 0) {
|
59
|
+
console.log(blobInfo);
|
60
|
+
return;
|
61
|
+
}
|
62
|
+
|
63
|
+
const blob = blobInfo.result.transactionBlob;
|
64
|
+
|
65
|
+
// ====================================
|
66
|
+
// step2. Sign blob with sender private key
|
67
|
+
// ====================================
|
68
|
+
let signatureInfo = sdk.transaction.sign({
|
69
|
+
privateKeys: [ senderPrivateKey ],
|
70
|
+
blob,
|
71
|
+
});
|
72
|
+
|
73
|
+
if (signatureInfo.errorCode !== 0) {
|
74
|
+
console.log(signatureInfo);
|
75
|
+
return;
|
76
|
+
}
|
77
|
+
|
78
|
+
const signature = signatureInfo.result.signatures;
|
79
|
+
|
80
|
+
console.log(signature);
|
81
|
+
});
|
82
|
+
|
83
|
+
});
|
@@ -0,0 +1,92 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('chai').should();
|
4
|
+
const BigNumber = require('bignumber.js');
|
5
|
+
const OpenChainSDK = require('openchain-sdk');
|
6
|
+
|
7
|
+
const sdk = new OpenChainSDK({
|
8
|
+
host: 'localhost/node/api',
|
9
|
+
});
|
10
|
+
|
11
|
+
describe('The demo of submit transaction ', function() {
|
12
|
+
|
13
|
+
it('The demo of submit transaction', async() => {
|
14
|
+
const senderPrivateKey = 'sender private key';
|
15
|
+
const senderAddress = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
16
|
+
const receiverAddress = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
17
|
+
|
18
|
+
const accountInfo = await sdk.account.getNonce(senderAddress);
|
19
|
+
|
20
|
+
if (accountInfo.errorCode !== 0) {
|
21
|
+
console.log(accountInfo);
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
let nonce = accountInfo.result.nonce;
|
25
|
+
// nonce + 1
|
26
|
+
nonce = new BigNumber(nonce).plus(1).toString(10);
|
27
|
+
|
28
|
+
// ====================================
|
29
|
+
// 1. build operation (buSendOperation)
|
30
|
+
// ====================================
|
31
|
+
const operationInfo = sdk.operation.buSendOperation({
|
32
|
+
sourceAddress: senderAddress,
|
33
|
+
destAddress: receiverAddress,
|
34
|
+
buAmount: '7000',
|
35
|
+
metadata: 'send bu demo',
|
36
|
+
});
|
37
|
+
|
38
|
+
if (operationInfo.errorCode !== 0) {
|
39
|
+
console.log(operationInfo);
|
40
|
+
return;
|
41
|
+
}
|
42
|
+
|
43
|
+
const operationItem = operationInfo.result.operation;
|
44
|
+
|
45
|
+
// ====================================
|
46
|
+
// 2. build blob
|
47
|
+
// ====================================
|
48
|
+
const blobInfo = sdk.transaction.buildBlob({
|
49
|
+
sourceAddress: senderAddress,
|
50
|
+
gasPrice: '1000',
|
51
|
+
feeLimit: '306000',
|
52
|
+
nonce,
|
53
|
+
operations: [ operationItem ],
|
54
|
+
});
|
55
|
+
|
56
|
+
if (blobInfo.errorCode !== 0) {
|
57
|
+
console.log(blobInfo);
|
58
|
+
return;
|
59
|
+
}
|
60
|
+
|
61
|
+
const blob = blobInfo.result.transactionBlob;
|
62
|
+
|
63
|
+
// ====================================
|
64
|
+
// 3. sign blob with sender private key
|
65
|
+
// ====================================
|
66
|
+
let signatureInfo = sdk.transaction.sign({
|
67
|
+
privateKeys: [ senderPrivateKey ],
|
68
|
+
blob,
|
69
|
+
});
|
70
|
+
|
71
|
+
if (signatureInfo.errorCode !== 0) {
|
72
|
+
console.log(signatureInfo);
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
|
76
|
+
const signature = signatureInfo.result.signatures;
|
77
|
+
|
78
|
+
// ====================================
|
79
|
+
// 4. submit transaction
|
80
|
+
// ====================================
|
81
|
+
const transactionInfo = await sdk.transaction.submit({
|
82
|
+
blob,
|
83
|
+
signature: signature,
|
84
|
+
});
|
85
|
+
|
86
|
+
if (transactionInfo.errorCode !== 0) {
|
87
|
+
console.log(transactionInfo);
|
88
|
+
}
|
89
|
+
console.log(transactionInfo);
|
90
|
+
});
|
91
|
+
|
92
|
+
});
|