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,98 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('chai').should();
|
4
|
+
const BigNumber = require('bignumber.js');
|
5
|
+
const co = require('co');
|
6
|
+
|
7
|
+
const OpenChainSDK = require('../index');
|
8
|
+
|
9
|
+
const sdk = new OpenChainSDK({
|
10
|
+
host: 'localhost/node/api',
|
11
|
+
// host: '192.168.1.34:36002',
|
12
|
+
});
|
13
|
+
|
14
|
+
describe('Test ctp10Token Transfer Operation', function() {
|
15
|
+
|
16
|
+
it('Test ctp10Token Transfer Operation', async() => {
|
17
|
+
const privateKey = 'private key';
|
18
|
+
let sourceAddress = 'adxScpCtbeLP2KGRaCkbtrmz8iB5mu6DQcW3r';
|
19
|
+
const result = await sdk.account.getNonce(sourceAddress);
|
20
|
+
|
21
|
+
if (result.errorCode !== 0) {
|
22
|
+
console.log(result);
|
23
|
+
return;
|
24
|
+
}
|
25
|
+
let nonce = result.result.nonce;
|
26
|
+
|
27
|
+
nonce = new BigNumber(nonce).plus(1).toString(10);
|
28
|
+
|
29
|
+
let ctp10TokenTransferOperation = await sdk.operation.ctp10TokenTransferOperation({
|
30
|
+
contractAddress: 'adxSqKcX8wGCMKhzNUBoDWfbeQaMhfnGdtyG2',
|
31
|
+
destAddress: 'adxSqKcX8wGCMKhzNUBoDWfbeQaMhfnGdtyG2',
|
32
|
+
tokenAmount: '3',
|
33
|
+
sourceAddress,
|
34
|
+
// metadata: '',
|
35
|
+
});
|
36
|
+
|
37
|
+
if (ctp10TokenTransferOperation.errorCode !== 0) {
|
38
|
+
console.log(ctp10TokenTransferOperation);
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
|
42
|
+
const operationItem = ctp10TokenTransferOperation.result.operation;
|
43
|
+
|
44
|
+
const args = {
|
45
|
+
sourceAddress,
|
46
|
+
nonce,
|
47
|
+
operations: [operationItem],
|
48
|
+
signtureNumber: '100',
|
49
|
+
// metadata: 'Test evaluation fee',
|
50
|
+
};
|
51
|
+
|
52
|
+
let feeData = await sdk.transaction.evaluateFee(args);
|
53
|
+
if (feeData.errorCode !== 0) {
|
54
|
+
console.log(feeData);
|
55
|
+
return;
|
56
|
+
}
|
57
|
+
|
58
|
+
let feeLimit = feeData.result.feeLimit;
|
59
|
+
let gasPrice = feeData.result.gasPrice;
|
60
|
+
|
61
|
+
// 2. build blob
|
62
|
+
let blobInfo = sdk.transaction.buildBlob({
|
63
|
+
sourceAddress: sourceAddress,
|
64
|
+
gasPrice,
|
65
|
+
feeLimit,
|
66
|
+
nonce: nonce,
|
67
|
+
operations: [ operationItem ],
|
68
|
+
});
|
69
|
+
|
70
|
+
if (blobInfo.errorCode !== 0) {
|
71
|
+
console.log(blobInfo);
|
72
|
+
return;
|
73
|
+
}
|
74
|
+
|
75
|
+
let blob = blobInfo.result.transactionBlob;
|
76
|
+
|
77
|
+
// 3. sign blob
|
78
|
+
let signatureInfo = sdk.transaction.sign({
|
79
|
+
privateKeys: [ privateKey ],
|
80
|
+
blob,
|
81
|
+
});
|
82
|
+
|
83
|
+
if (signatureInfo.errorCode !== 0) {
|
84
|
+
console.log(signatureInfo);
|
85
|
+
return;
|
86
|
+
}
|
87
|
+
|
88
|
+
let signature = signatureInfo.result.signatures;
|
89
|
+
// 4. submit transaction
|
90
|
+
let transactionInfo = await sdk.transaction.submit({
|
91
|
+
blob,
|
92
|
+
signature: signature,
|
93
|
+
});
|
94
|
+
|
95
|
+
console.log(transactionInfo);
|
96
|
+
});
|
97
|
+
|
98
|
+
});
|
@@ -0,0 +1,103 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('chai').should();
|
4
|
+
const BigNumber = require('bignumber.js');
|
5
|
+
const co = require('co');
|
6
|
+
|
7
|
+
const OpenChainSDK = require('../index');
|
8
|
+
|
9
|
+
const sdk = new OpenChainSDK({
|
10
|
+
host: 'localhost/node/api',
|
11
|
+
// host: '192.168.1.34:36002',
|
12
|
+
});
|
13
|
+
|
14
|
+
describe('Test create contract account transaction', function() {
|
15
|
+
|
16
|
+
it('test create contract account ', function() {
|
17
|
+
const privateKey = 'private key';
|
18
|
+
const sourceAddress = 'adxScpCtbeLP2KGRaCkbtrmz8iB5mu6DQcW3r';
|
19
|
+
|
20
|
+
co(function* () {
|
21
|
+
const result = yield sdk.account.getNonce(sourceAddress);
|
22
|
+
|
23
|
+
if (result.errorCode !== 0) {
|
24
|
+
console.log(result);
|
25
|
+
return;
|
26
|
+
}
|
27
|
+
let nonce = result.result.nonce;
|
28
|
+
|
29
|
+
nonce = new BigNumber(nonce).plus(1).toString(10);
|
30
|
+
|
31
|
+
let logCreateOperation = sdk.operation.logCreateOperation({
|
32
|
+
sourceAddress,
|
33
|
+
topic: 'test topic',
|
34
|
+
data: 'test data',
|
35
|
+
// metadata: '',
|
36
|
+
});
|
37
|
+
|
38
|
+
if (logCreateOperation.errorCode !== 0) {
|
39
|
+
console.log(logCreateOperation);
|
40
|
+
return;
|
41
|
+
}
|
42
|
+
|
43
|
+
const operationItem = logCreateOperation.result.operation;
|
44
|
+
|
45
|
+
const args = {
|
46
|
+
sourceAddress,
|
47
|
+
nonce,
|
48
|
+
operations: [operationItem],
|
49
|
+
signtureNumber: '100',
|
50
|
+
// metadata: 'Test evaluation fee',
|
51
|
+
};
|
52
|
+
|
53
|
+
let feeData = yield sdk.transaction.evaluateFee(args);
|
54
|
+
if (feeData.errorCode !== 0) {
|
55
|
+
console.log(feeData);
|
56
|
+
return;
|
57
|
+
}
|
58
|
+
|
59
|
+
let feeLimit = feeData.result.feeLimit;
|
60
|
+
let gasPrice = feeData.result.gasPrice;
|
61
|
+
|
62
|
+
// 2. build blob
|
63
|
+
let blobInfo = sdk.transaction.buildBlob({
|
64
|
+
sourceAddress: sourceAddress,
|
65
|
+
gasPrice,
|
66
|
+
feeLimit,
|
67
|
+
nonce: nonce,
|
68
|
+
operations: [ operationItem ],
|
69
|
+
});
|
70
|
+
|
71
|
+
if (blobInfo.errorCode !== 0) {
|
72
|
+
console.log(blobInfo);
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
|
76
|
+
let blob = blobInfo.result.transactionBlob;
|
77
|
+
|
78
|
+
// 3. sign blob
|
79
|
+
let signatureInfo = sdk.transaction.sign({
|
80
|
+
privateKeys: [ privateKey ],
|
81
|
+
blob,
|
82
|
+
});
|
83
|
+
|
84
|
+
if (signatureInfo.errorCode !== 0) {
|
85
|
+
console.log(signatureInfo);
|
86
|
+
return;
|
87
|
+
}
|
88
|
+
|
89
|
+
let signature = signatureInfo.result.signatures;
|
90
|
+
// 4. submit transaction
|
91
|
+
let transactionInfo = yield sdk.transaction.submit({
|
92
|
+
blob,
|
93
|
+
signature: signature,
|
94
|
+
});
|
95
|
+
|
96
|
+
console.log(transactionInfo);
|
97
|
+
|
98
|
+
}).catch(err => {
|
99
|
+
console.log(err);
|
100
|
+
});
|
101
|
+
});
|
102
|
+
|
103
|
+
});
|
@@ -0,0 +1,166 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('chai').should();
|
4
|
+
const BigNumber = require('bignumber.js');
|
5
|
+
const co = require('co');
|
6
|
+
|
7
|
+
const OpenChainSDK = require('../index');
|
8
|
+
|
9
|
+
const sdk = new OpenChainSDK({
|
10
|
+
host: 'localhost/node/api',
|
11
|
+
// host: '192.168.1.162:56002',
|
12
|
+
});
|
13
|
+
|
14
|
+
describe('Test transaction', function() {
|
15
|
+
|
16
|
+
|
17
|
+
it('evaluateFee', function() {
|
18
|
+
const sourceAddress = 'adxSqKcX8wGCMKhzNUBoDWfbeQaMhfnGdtyG2';
|
19
|
+
const destAddress = 'adxSk4rkz84a4fh5xYxfSqmAsnhcWZzrTfG2t';
|
20
|
+
|
21
|
+
sdk.account.getNonce(sourceAddress).then(info => {
|
22
|
+
|
23
|
+
|
24
|
+
if (info.errorCode !== 0) {
|
25
|
+
console.log(info);
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
const nonce = new BigNumber(info.result.nonce).plus(1).toString(10);
|
29
|
+
|
30
|
+
let sendBuOperation = sdk.operation.buSendOperation({
|
31
|
+
destAddress,
|
32
|
+
buAmount: '60000',
|
33
|
+
// metadata: 'oh my send bu',
|
34
|
+
});
|
35
|
+
|
36
|
+
|
37
|
+
if (sendBuOperation.errorCode !== 0) {
|
38
|
+
console.log(sendBuOperation);
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
|
42
|
+
const operationItem = sendBuOperation.result.operation;
|
43
|
+
|
44
|
+
// evaluation fee
|
45
|
+
const args = {
|
46
|
+
sourceAddress,
|
47
|
+
nonce,
|
48
|
+
operations: [operationItem],
|
49
|
+
signtureNumber: '1',
|
50
|
+
// ceilLedgerSeq: '30',
|
51
|
+
// metadata: 'Test evaluation fee',
|
52
|
+
};
|
53
|
+
|
54
|
+
|
55
|
+
sdk.transaction.evaluateFee(args).then(feeData => {
|
56
|
+
console.log(feeData)
|
57
|
+
}).catch(err => {
|
58
|
+
console.log(err);
|
59
|
+
});
|
60
|
+
|
61
|
+
}).catch(err => {
|
62
|
+
console.log(err);
|
63
|
+
});
|
64
|
+
});
|
65
|
+
|
66
|
+
it('test getInfo', function() {
|
67
|
+
const hash = 'e21fb61a66e251ec2cb593ab09781daaacaea6701f2ac2f26eb867d8e60324ba';
|
68
|
+
sdk.transaction.getInfo(hash).then(data => {
|
69
|
+
// console.log(data);
|
70
|
+
})
|
71
|
+
});
|
72
|
+
|
73
|
+
it('transaction', function() {
|
74
|
+
const privateKey = 'private key';
|
75
|
+
const sourceAddress = 'adxSYvndiFG4zpbLGugX3j93fDn9nWZfWp8Gd';
|
76
|
+
const destAddress = 'adxScpCtbeLP2KGRaCkbtrmz8iB5mu6DQcW3r';
|
77
|
+
|
78
|
+
co(function* () {
|
79
|
+
const result = yield sdk.account.getNonce(sourceAddress);
|
80
|
+
|
81
|
+
if (result.errorCode !== 0) {
|
82
|
+
console.log(result);
|
83
|
+
return;
|
84
|
+
}
|
85
|
+
let nonce = result.result.nonce;
|
86
|
+
nonce = new BigNumber(nonce).plus(1).toString(10);
|
87
|
+
|
88
|
+
// 1.build operation
|
89
|
+
let sendBuInfo = sdk.operation.buSendOperation({
|
90
|
+
// sourceAddress,
|
91
|
+
destAddress,
|
92
|
+
buAmount: '10000000',
|
93
|
+
// metadata: 'oh my send bu',
|
94
|
+
});
|
95
|
+
|
96
|
+
if (sendBuInfo.errorCode !== 0) {
|
97
|
+
console.log(sendBuInfo);
|
98
|
+
return;
|
99
|
+
}
|
100
|
+
|
101
|
+
const sendBuOperation = sendBuInfo.result.operation;
|
102
|
+
// console.log(sendBuOperation);
|
103
|
+
// return;
|
104
|
+
// evaluation fee
|
105
|
+
// const args = {
|
106
|
+
// sourceAddress,
|
107
|
+
// nonce,
|
108
|
+
// operations: [sendBuOperation],
|
109
|
+
// signtureNumber: '100',
|
110
|
+
// // metadata: 'Test evaluation fee',
|
111
|
+
// };
|
112
|
+
//
|
113
|
+
// let feeData = yield sdk.transaction.evaluateFee(args);
|
114
|
+
//
|
115
|
+
// if (feeData.errorCode !== 0) {
|
116
|
+
// console.log(feeData);
|
117
|
+
// return;
|
118
|
+
// }
|
119
|
+
// const feeLimit = feeData.result.feeLimit;
|
120
|
+
// const gasPrice = feeData.result.gasPrice;
|
121
|
+
|
122
|
+
// 2. build blob
|
123
|
+
let blobInfo = sdk.transaction.buildBlob({
|
124
|
+
sourceAddress,
|
125
|
+
gasPrice: '1000',
|
126
|
+
feeLimit: '14119000',
|
127
|
+
nonce,
|
128
|
+
// ceilLedgerSeq: '',
|
129
|
+
operations: [ sendBuOperation ],
|
130
|
+
// metadata: '6f68206d79207478',
|
131
|
+
});
|
132
|
+
|
133
|
+
if (blobInfo.errorCode !== 0) {
|
134
|
+
console.log(blobInfo);
|
135
|
+
return;
|
136
|
+
}
|
137
|
+
|
138
|
+
let blob = blobInfo.result.transactionBlob;
|
139
|
+
|
140
|
+
// 3. sign blob
|
141
|
+
let signatureInfo = sdk.transaction.sign({
|
142
|
+
privateKeys: [ privateKey ],
|
143
|
+
blob,
|
144
|
+
});
|
145
|
+
|
146
|
+
if (signatureInfo.errorCode !== 0) {
|
147
|
+
signatureInfo.errorCode.should.equal(11058);
|
148
|
+
// console.log(signatureInfo);
|
149
|
+
return;
|
150
|
+
}
|
151
|
+
|
152
|
+
let signature = signatureInfo.result.signatures;
|
153
|
+
// 4. submit transaction
|
154
|
+
let transactionInfo = yield sdk.transaction.submit({
|
155
|
+
blob,
|
156
|
+
signature: signature,
|
157
|
+
});
|
158
|
+
|
159
|
+
console.log(transactionInfo);
|
160
|
+
|
161
|
+
}).catch(err => {
|
162
|
+
console.log(err);
|
163
|
+
});
|
164
|
+
});
|
165
|
+
|
166
|
+
});
|
@@ -0,0 +1,93 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('chai').should();
|
4
|
+
const OpenChainSDK = require('../index');
|
5
|
+
|
6
|
+
|
7
|
+
const sdk = new OpenChainSDK({
|
8
|
+
host: 'localhost/node/api',
|
9
|
+
});
|
10
|
+
|
11
|
+
describe('Test util', function() {
|
12
|
+
|
13
|
+
it('test utfToHex(string)', function() {
|
14
|
+
let data = sdk.util.utfToHex('hello, world');
|
15
|
+
data.should.equal('68656c6c6f2c20776f726c64');
|
16
|
+
|
17
|
+
data = sdk.util.utfToHex('');
|
18
|
+
data.should.equal('');
|
19
|
+
|
20
|
+
data = sdk.util.utfToHex();
|
21
|
+
let result = (data === undefined);
|
22
|
+
result.should.equal(true);
|
23
|
+
});
|
24
|
+
|
25
|
+
it('test hexToUtf(string)', function() {
|
26
|
+
let data = sdk.util.hexToUtf('68656c6c6f2c20776f726c64');
|
27
|
+
data.should.equal('hello, world');
|
28
|
+
|
29
|
+
let result;
|
30
|
+
data = sdk.util.hexToUtf('');
|
31
|
+
result = (data === undefined);
|
32
|
+
result.should.equal(true);
|
33
|
+
|
34
|
+
data = sdk.util.hexToUtf();
|
35
|
+
result = (data === undefined);
|
36
|
+
result.should.equal(true);
|
37
|
+
|
38
|
+
data = sdk.util.hexToUtf('hello, world');
|
39
|
+
result = (data === undefined);
|
40
|
+
result.should.equal(true);
|
41
|
+
});
|
42
|
+
|
43
|
+
it('test buToMo(string)', function() {
|
44
|
+
let data = sdk.util.buToMo('5');
|
45
|
+
data.should.equal('500000000');
|
46
|
+
|
47
|
+
data = sdk.util.buToMo(5);
|
48
|
+
data.should.equal('');
|
49
|
+
|
50
|
+
data = sdk.util.buToMo();
|
51
|
+
data.should.equal('');
|
52
|
+
|
53
|
+
data = sdk.util.buToMo('');
|
54
|
+
data.should.equal('');
|
55
|
+
|
56
|
+
data = sdk.util.buToMo('abc');
|
57
|
+
data.should.equal('');
|
58
|
+
|
59
|
+
data = sdk.util.buToMo('0.1');
|
60
|
+
data.should.equal('10000000');
|
61
|
+
|
62
|
+
data = sdk.util.buToMo('-1');
|
63
|
+
data.should.equal('');
|
64
|
+
});
|
65
|
+
|
66
|
+
it('test moToBu(string)', function() {
|
67
|
+
let data = sdk.util.moToBu('500000000');
|
68
|
+
data.should.equal('5');
|
69
|
+
|
70
|
+
data = sdk.util.moToBu('0');
|
71
|
+
data.should.equal('0');
|
72
|
+
|
73
|
+
data = sdk.util.moToBu('');
|
74
|
+
data.should.equal('');
|
75
|
+
|
76
|
+
data = sdk.util.moToBu();
|
77
|
+
data.should.equal('');
|
78
|
+
|
79
|
+
data = sdk.util.moToBu('abc');
|
80
|
+
data.should.equal('');
|
81
|
+
|
82
|
+
data = sdk.util.moToBu('-1');
|
83
|
+
data.should.equal('');
|
84
|
+
|
85
|
+
data = sdk.util.moToBu(-1);
|
86
|
+
data.should.equal('');
|
87
|
+
|
88
|
+
data = sdk.util.moToBu('0.1');
|
89
|
+
data.should.equal('');
|
90
|
+
});
|
91
|
+
|
92
|
+
|
93
|
+
});
|