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.
Files changed (76) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +66 -0
  3. package/_config.yml +1 -0
  4. package/doc/SDK_CN.md +2003 -0
  5. package/example/atp10TokenDemo.js +319 -0
  6. package/example/exchange.js +184 -0
  7. package/example/offlineSignatureDemo.js +83 -0
  8. package/example/submitTransactionDemo.js +92 -0
  9. package/index.d.ts +186 -0
  10. package/index.js +9 -0
  11. package/lib/account/index.js +234 -0
  12. package/lib/blockchain/block.js +380 -0
  13. package/lib/blockchain/transaction.js +264 -0
  14. package/lib/common/ctp.js +5 -0
  15. package/lib/common/operation/accountSetMetadata.js +55 -0
  16. package/lib/common/operation/accountSetPrivilege.js +90 -0
  17. package/lib/common/operation/activateAccount.js +58 -0
  18. package/lib/common/operation/contractCreate.js +72 -0
  19. package/lib/common/operation/contractInvokeByAsset.js +75 -0
  20. package/lib/common/operation/contractInvokeByBU.js +53 -0
  21. package/lib/common/operation/createLog.js +44 -0
  22. package/lib/common/operation/ctp10TokenApprove.js +59 -0
  23. package/lib/common/operation/ctp10TokenAssign.js +59 -0
  24. package/lib/common/operation/ctp10TokenChangeOwner.js +58 -0
  25. package/lib/common/operation/ctp10TokenIssue.js +78 -0
  26. package/lib/common/operation/ctp10TokenTransfer.js +59 -0
  27. package/lib/common/operation/ctp10TokenTransferFrom.js +60 -0
  28. package/lib/common/operation/issueAsset.js +35 -0
  29. package/lib/common/operation/payAsset.js +62 -0
  30. package/lib/common/operation/payCoin.js +44 -0
  31. package/lib/common/util.js +880 -0
  32. package/lib/contract/index.js +212 -0
  33. package/lib/crypto/protobuf/bundle.json +1643 -0
  34. package/lib/exception/customErrors.js +56 -0
  35. package/lib/exception/errors.js +240 -0
  36. package/lib/exception/index.js +8 -0
  37. package/lib/operation/account.js +193 -0
  38. package/lib/operation/asset.js +106 -0
  39. package/lib/operation/bu.js +52 -0
  40. package/lib/operation/contract.js +184 -0
  41. package/lib/operation/ctp10Token.js +394 -0
  42. package/lib/operation/index.js +30 -0
  43. package/lib/operation/log.js +47 -0
  44. package/lib/sdk.js +70 -0
  45. package/lib/token/asset.js +79 -0
  46. package/lib/token/ctp10Token.js +301 -0
  47. package/lib/token/index.js +17 -0
  48. package/lib/util/index.js +86 -0
  49. package/openchain-sdk-nodejs.iml +9 -0
  50. package/package.json +39 -0
  51. package/test/Ctp10Token.test.js +96 -0
  52. package/test/account.test.js +132 -0
  53. package/test/accountActivateOperation.test.js +99 -0
  54. package/test/accountSetMetadata.test.js +102 -0
  55. package/test/accountSetPrivilege.test.js +66 -0
  56. package/test/asset.test.js +63 -0
  57. package/test/assetIssueOperation.test.js +77 -0
  58. package/test/assetSendOperation.test.js +103 -0
  59. package/test/blob.test.js +128 -0
  60. package/test/block.test.js +165 -0
  61. package/test/buSendOperation.test.js +64 -0
  62. package/test/contract.test.js +64 -0
  63. package/test/contractCreateOperation.test.js +41 -0
  64. package/test/contractCreateTransaction.test.js +116 -0
  65. package/test/contractInvokeByAssetOperation.test.js +109 -0
  66. package/test/contractInvokeByBUOperation.test.js +107 -0
  67. package/test/ctp10TokenApproveOperation.test.js +98 -0
  68. package/test/ctp10TokenAssignOperation.test.js +99 -0
  69. package/test/ctp10TokenChangeOwnerOperation.test.js +98 -0
  70. package/test/ctp10TokenIssueOperation.test.js +40 -0
  71. package/test/ctp10TokenIssueOperationTransaction.test.js +106 -0
  72. package/test/ctp10TokenTransferFromOperation.test.js +101 -0
  73. package/test/ctp10TokenTransferOperation.test.js +98 -0
  74. package/test/log.transaction.test.js +103 -0
  75. package/test/transaction.test.js +166 -0
  76. package/test/util.test.js +93 -0
package/index.d.ts ADDED
@@ -0,0 +1,186 @@
1
+ declare class OpenChainSDK {
2
+ constructor(config: { host: string });
3
+
4
+ account: {
5
+ create(): Promise<{ errorCode: number; result: { privateKey: string; publicKey: string; address: string } }>;
6
+ getInfo(address: string): Promise<{ errorCode: number; result: AccountInfoResult }>;
7
+ checkValid(address: string): Promise<{ errorCode: number; result: { isValid: boolean } }>;
8
+ getBalance(address: string): Promise<{ errorCode: number; result: { balance: string } }>;
9
+ getNonce(address: string): Promise<{ errorCode: number; result: { nonce: string } }>;
10
+ getMetadata(params: { address: string; key: string }): Promise<{ errorCode: number; result: MetadataResult }>;
11
+ isActivated(address: string): Promise<{ errorCode: number; result: { isActivated: boolean } }>;
12
+ };
13
+
14
+ contract: {
15
+ getInfo(contractAddress: string): Promise<{ errorCode: number; result: ContractInfoResult }>;
16
+ getAddress(hash: string): Promise<{ errorCode: number; result: ContractAddressInfo[] }>;
17
+ };
18
+
19
+ block: {
20
+ getNumber(): Promise<{ errorCode: number; result: BlockNumberResult }>;
21
+ checkStatus(): Promise<{ errorCode: number; result: BlockStatusResult }>;
22
+ getTransactions(blockNumber: string): Promise<{ errorCode: number; result: TransactionListResult }>;
23
+ };
24
+
25
+ operation: {
26
+ assetIssueOperation(params: AssetIssueParams): { errorCode: number; result: OperationResult };
27
+ accountActivateOperation(params: AccountActivateParams): { errorCode: number; result: OperationResult };
28
+ accountSetMetadataOperation(params: MetadataParams): { errorCode: number; result: OperationResult };
29
+ };
30
+
31
+ transaction: {
32
+ buildBlob(params: TransactionParams): Promise<{ errorCode: number; result: TransactionBuildBlobResult }>;
33
+ sign(params: TransactionSignParams): Promise<{ errorCode: number; result: TransactionSignResult }>;
34
+ submit(params: TransactionSubmitParams): Promise<{ errorCode: number; result: TransactionSubmitResult }>;
35
+ getInfo(hash: string): Promise<{ errorCode: number; result: TransactionResult }>;
36
+ };
37
+ }
38
+
39
+ interface AccountInfoResult {
40
+ address: string;
41
+ balance: string;
42
+ nonce: string;
43
+ type_thresholds: Array<{
44
+ type: string;
45
+ threshold: string;
46
+ }>;
47
+ }
48
+
49
+ interface BlockNumberResult {
50
+ header: {
51
+ blockNumber: string;
52
+ timestamp: string;
53
+ txCount: number;
54
+ };
55
+ }
56
+
57
+ interface BlockStatusResult {
58
+ isSynchronous: boolean;
59
+ ledgerVersion: string;
60
+ }
61
+
62
+ interface TransactionListResult {
63
+ total_count: number;
64
+ transactions: Array<{
65
+ hash: string;
66
+ sourceAddress: string;
67
+ feeLimit: string;
68
+ gasPrice: string;
69
+ }>;
70
+ }
71
+
72
+ interface ContractCreateParams {
73
+ sourceAddress: string;
74
+ initBalance: string;
75
+ type: number;
76
+ payload: string;
77
+ initInput?: string;
78
+ metadata?: string;
79
+ }
80
+
81
+ interface OperationResult {
82
+ operation: {
83
+ type: string;
84
+ data: Record<string, any>;
85
+ };
86
+ }
87
+
88
+ interface ContractInfoResult {
89
+ contract: {
90
+ type: number;
91
+ payload: string;
92
+ };
93
+ }
94
+
95
+ interface ContractAddressInfo {
96
+ contract_address: string;
97
+ operation_index: number;
98
+ }
99
+
100
+ interface AssetIssueParams {
101
+ sourceAddress: string;
102
+ code: string;
103
+ assetAmount: string;
104
+ metadata?: string;
105
+ }
106
+
107
+ interface MetadataResult {
108
+ version: string;
109
+ value: string;
110
+ }
111
+
112
+ interface MetadataParams {
113
+ key: string;
114
+ value: string;
115
+ version?: string;
116
+ deleteFlag?: number;
117
+ }
118
+
119
+ interface AccountActivateParams {
120
+ sourceAddress: string;
121
+ destAddress: string;
122
+ initBalance: string;
123
+ metadata?: string;
124
+ }
125
+
126
+ interface TransactionParams {
127
+ sourceAddress: string;
128
+ nonce: string;
129
+ operations: OperationResult[];
130
+ gasPrice?: string;
131
+ feeLimit?: string;
132
+ metadata?: string;
133
+ }
134
+
135
+ interface TransactionBuildBlobResult {
136
+ transactionBlob: string;
137
+ hash: string;
138
+ }
139
+
140
+ interface TransactionSignParams {
141
+ transactionBlob: string;
142
+ privateKeys: string[];
143
+ }
144
+
145
+ interface TransactionSignResult {
146
+ signatures: Array<{
147
+ signData: string;
148
+ publicKey: string;
149
+ }>;
150
+ }
151
+
152
+ interface TransactionSubmitParams {
153
+ transactionBlob: string;
154
+ signatures: Array<{
155
+ signData: string;
156
+ publicKey: string;
157
+ }>;
158
+ }
159
+
160
+ interface TransactionSubmitResult {
161
+ hash: string;
162
+ }
163
+
164
+ interface TransactionResult {
165
+ totalCount: number;
166
+ transactions: Array<{
167
+ closeTime: string;
168
+ errorCode: number;
169
+ errorDesc: string;
170
+ hash: string;
171
+ ledgerSeq: string;
172
+ signatures: Array<{
173
+ signData: string;
174
+ publicKey: string;
175
+ }>;
176
+ transaction: {
177
+ sourceAddress: string;
178
+ nonce: string;
179
+ gasPrice: string;
180
+ feeLimit: string;
181
+ metadata?: string;
182
+ operations: OperationResult[];
183
+ };
184
+ txSize: number;
185
+ }>;
186
+ }
package/index.js ADDED
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const OpenChainSDK = require('./lib/sdk');
4
+
5
+ if (typeof window !== 'undefined' && typeof window.OpenChainSDK === 'undefined') {
6
+ window.OpenChainSDK = OpenChainSDK;
7
+ }
8
+
9
+ module.exports = OpenChainSDK;
@@ -0,0 +1,234 @@
1
+ 'use strict';
2
+
3
+ const wrap = require('co-wrap-all');
4
+ const is = require('is-type-of');
5
+ const merge = require('merge-descriptors');
6
+ const long = require('long');
7
+ const JSONbig = require('json-bigint');
8
+ const { keypair } = require('yxchain-encryption-nodejs');
9
+ const errors = require('../exception');
10
+
11
+ module.exports = Account;
12
+
13
+ function Account(options) {
14
+ if (!(this instanceof Account)) {
15
+ return new Account(options);
16
+ }
17
+
18
+ this.options = options;
19
+ }
20
+
21
+ const proto = Account.prototype;
22
+
23
+ merge(proto, require('../common/util'));
24
+
25
+ /**
26
+ * Create account
27
+ * @return {Object}
28
+ */
29
+ proto.create = function* () {
30
+ const kp = keypair.getKeyPair();
31
+ const privateKey = kp.encPrivateKey;
32
+ const publicKey = kp.encPublicKey;
33
+ const address = kp.address;
34
+
35
+ return this._responseData({
36
+ privateKey,
37
+ publicKey,
38
+ address,
39
+ });
40
+ };
41
+
42
+ /**
43
+ * Check address
44
+ * @param {String} address
45
+ * @return {Boolean}
46
+ */
47
+ proto.checkValid = function* (address) {
48
+ const isValid = keypair.checkAddress(address);
49
+ return this._responseData({
50
+ isValid,
51
+ });
52
+ };
53
+
54
+ /**
55
+ * Get account information
56
+ * @param {String} address
57
+ * @return {Object}
58
+ */
59
+ proto.getInfo = function* (address) {
60
+ if (!keypair.checkAddress(address)) {
61
+ return this._responseError(errors.INVALID_ADDRESS_ERROR);
62
+ }
63
+
64
+ const res = yield this._request('get', 'getAccount', { address });
65
+ if (res.error_code !== 0) {
66
+ return this._responseError(errors.ACCOUNT_NOT_EXIST);
67
+ }
68
+
69
+ let nonce = res.result.nonce;
70
+
71
+ nonce = nonce ? nonce : '0';
72
+ return this._responseData({
73
+ address: res.result.address,
74
+ balance: res.result.balance,
75
+ nonce: nonce,
76
+ assets: res.result.assets || [],
77
+ priv: res.result.priv || {},
78
+ });
79
+ };
80
+
81
+ /**
82
+ * Get account balance
83
+ * @param {String} address
84
+ * @return {Object}
85
+ */
86
+ proto.getBalance = function* (address) {
87
+ if (!keypair.checkAddress(address)) {
88
+ return this._responseError(errors.INVALID_ADDRESS_ERROR);
89
+ }
90
+
91
+ let info = yield this.getInfo(address);
92
+
93
+ if (info.errorCode === 0) {
94
+ return this._responseData({
95
+ balance: info.result.balance,
96
+ });
97
+ }
98
+
99
+ return this._responseError(errors.ACCOUNT_NOT_EXIST);
100
+ };
101
+
102
+ /**
103
+ * Get nonce
104
+ * @param {String} address
105
+ * @return {Object}
106
+ */
107
+ proto.getNonce = function* (address) {
108
+ if (!keypair.checkAddress(address)) {
109
+ return this._responseError(errors.INVALID_ADDRESS_ERROR);
110
+ }
111
+
112
+ let info = yield this.getInfo(address);
113
+
114
+ if (info.errorCode === 0) {
115
+ return this._responseData({
116
+ nonce: info.result.nonce,
117
+ });
118
+ }
119
+
120
+ return this._responseError(errors.ACCOUNT_NOT_EXIST);
121
+ };
122
+
123
+ proto.getAssets = function* (address) {
124
+ if (!keypair.checkAddress(address)) {
125
+ return this._responseError(errors.INVALID_ADDRESS_ERROR);
126
+ }
127
+
128
+ let info = yield this.getInfo(address);
129
+ if (info.errorCode === 0) {
130
+ return this._responseData({
131
+ assets: info.result.assets,
132
+ });
133
+ }
134
+
135
+ return this._responseError(errors.ACCOUNT_NOT_EXIST);
136
+ };
137
+
138
+ /**
139
+ * Get account metadata
140
+ * @param args
141
+ * @return {Object}
142
+ */
143
+ proto.getMetadata = function* (args) {
144
+
145
+ if (is.array(args) || !is.object(args)) {
146
+ return this._responseError(errors.INVALID_ARGUMENTS);
147
+ }
148
+
149
+ const schema = {
150
+ address: {
151
+ required: true,
152
+ address: true,
153
+ },
154
+ key: {
155
+ required: true,
156
+ string: true,
157
+ },
158
+ };
159
+
160
+ if (!this._validate(args, schema).tag) {
161
+ const msg = this._validate(args, schema).msg;
162
+ return this._responseError(errors[msg]);
163
+ }
164
+
165
+ const address = args.address;
166
+ const key = args.key;
167
+
168
+ const info = yield this._request('get', 'getAccount', {
169
+ address,
170
+ key,
171
+ });
172
+
173
+ if (info.error_code !== 0) {
174
+ return this._responseError(errors.ACCOUNT_NOT_EXIST);
175
+ }
176
+
177
+ const data = info.result;
178
+ const metadata = {};
179
+ const metadatas = data.metadatas;
180
+
181
+ if (metadatas && is.array(metadatas) && metadatas.length > 0) {
182
+ metadatas.some(item => {
183
+ if (item.key === key) {
184
+ metadata.key = item.key;
185
+ metadata.value = item.value;
186
+ metadata.version = item.version;
187
+ return true;
188
+ }
189
+ });
190
+ }
191
+
192
+ return this._responseData({
193
+ metadata,
194
+ });
195
+ };
196
+
197
+ proto.isActivated = function* (address) {
198
+
199
+ if (is.undefined(address)) {
200
+ return this._responseError(errors.INVALID_ARGUMENTS);
201
+ }
202
+
203
+ const schema = {
204
+ address: {
205
+ required: true,
206
+ address: true,
207
+ },
208
+ };
209
+
210
+ const arg = {
211
+ address
212
+ };
213
+
214
+ if (!this._validate(arg, schema).tag) {
215
+ const msg = this._validate(arg, schema).msg;
216
+ return this._responseError(errors[msg]);
217
+ }
218
+
219
+ const info = yield this._request('get', 'getAccount', {
220
+ address,
221
+ });
222
+
223
+ let isActivated = false;
224
+
225
+ if (info.error_code === 0) {
226
+ isActivated = true;
227
+ }
228
+
229
+ return this._responseData({
230
+ isActivated,
231
+ });
232
+ };
233
+
234
+ wrap(proto);