@caict/bop-typescript-sdk 1.0.0

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 (115) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +5 -0
  4. package/README.md +112 -0
  5. package/package.json +90 -0
  6. package/src/bop/bop-base/README.md +5 -0
  7. package/src/bop/bop-base/base.ts +366 -0
  8. package/src/bop/bop-base/index.ts +83 -0
  9. package/src/bop/bop-base/permit.ts +116 -0
  10. package/src/bop/bop-base/test/base.test.ts +867 -0
  11. package/src/bop/bop-base/test/index.test.ts +69 -0
  12. package/src/bop/bop-base/test/permit.test.ts +125 -0
  13. package/src/bop/bop-base/tsconfig.json +8 -0
  14. package/src/bop/bop-proto/README.md +2 -0
  15. package/src/bop/bop-proto/bop-ws.proto +266 -0
  16. package/src/bop/bop-proto/bop-ws.ts +4447 -0
  17. package/src/bop/bop-proto/bop.proto +898 -0
  18. package/src/bop/bop-proto/bop.ts +12890 -0
  19. package/src/bop/bop-proto/tsconfig.json +8 -0
  20. package/src/bop/bop-ws/index.ts +315 -0
  21. package/src/bop/bop-ws/sample/sample.ts +107 -0
  22. package/src/bop/bop-ws/tsconfig.json +8 -0
  23. package/src/bop/index.ts +14 -0
  24. package/src/bop-sdk/index.ts +22 -0
  25. package/src/bop-sdk/interface/abstract-service/abstract-account.ts +23 -0
  26. package/src/bop-sdk/interface/abstract-service/abstract-chain.ts +9 -0
  27. package/src/bop-sdk/interface/abstract-service/abstract-contract.ts +16 -0
  28. package/src/bop-sdk/interface/abstract-service/abstract-ledger.ts +15 -0
  29. package/src/bop-sdk/interface/abstract-service/abstract-provider.ts +13 -0
  30. package/src/bop-sdk/interface/abstract-service/abstract-signer.ts +133 -0
  31. package/src/bop-sdk/interface/abstract-service/abstract-transaction.ts +86 -0
  32. package/src/bop-sdk/interface/provider-ws.ts +10 -0
  33. package/src/bop-sdk/interface/provider.ts +22 -0
  34. package/src/bop-sdk/interface/service/3rd.ts +54 -0
  35. package/src/bop-sdk/interface/service/account.ts +86 -0
  36. package/src/bop-sdk/interface/service/chain.ts +27 -0
  37. package/src/bop-sdk/interface/service/contract.ts +51 -0
  38. package/src/bop-sdk/interface/service/helper.ts +426 -0
  39. package/src/bop-sdk/interface/service/ledger.ts +75 -0
  40. package/src/bop-sdk/interface/service/offline/check/index.js +138 -0
  41. package/src/bop-sdk/interface/service/offline/common/constant.js +6 -0
  42. package/src/bop-sdk/interface/service/offline/common/snowFlakeUtils.js +192 -0
  43. package/src/bop-sdk/interface/service/offline/common/util.js +511 -0
  44. package/src/bop-sdk/interface/service/offline/exception/customErrors.js +29 -0
  45. package/src/bop-sdk/interface/service/offline/exception/errors.js +90 -0
  46. package/src/bop-sdk/interface/service/offline/exception/index.js +5 -0
  47. package/src/bop-sdk/interface/service/offline/operation/accountCreateOperation.js +28 -0
  48. package/src/bop-sdk/interface/service/offline/operation/accountSetMetadataOperation.js +44 -0
  49. package/src/bop-sdk/interface/service/offline/operation/accountSetPrivilegeOperation.js +49 -0
  50. package/src/bop-sdk/interface/service/offline/operation/contractCreateOperation.js +43 -0
  51. package/src/bop-sdk/interface/service/offline/operation/contractInvokeOperation.js +42 -0
  52. package/src/bop-sdk/interface/service/offline/operation/contractInvokeRequestOperation.js +69 -0
  53. package/src/bop-sdk/interface/service/offline/operation/gasSendOperation.js +29 -0
  54. package/src/bop-sdk/interface/service/offline/operation/gasSendRequestOperation.js +61 -0
  55. package/src/bop-sdk/interface/service/offline/operation/index.js +20 -0
  56. package/src/bop-sdk/interface/service/offline/proto/chain_pb.js +12079 -0
  57. package/src/bop-sdk/interface/service/offline/proto/common_pb.js +2867 -0
  58. package/src/bop-sdk/interface/service/offline/transaction/index.js +988 -0
  59. package/src/bop-sdk/interface/service/offline/transaction/operations.js +188 -0
  60. package/src/bop-sdk/interface/service/offline/validate/account.js +152 -0
  61. package/src/bop-sdk/interface/service/offline/validate/contract.js +101 -0
  62. package/src/bop-sdk/interface/service/offline/validate/index.js +10 -0
  63. package/src/bop-sdk/interface/service/offline/validate/transaction.js +51 -0
  64. package/src/bop-sdk/interface/service/transaction.ts +731 -0
  65. package/src/bop-sdk/interface/signer.ts +46 -0
  66. package/src/bop-sdk/interface/test/3rd.test.ts +1571 -0
  67. package/src/bop-sdk/interface/test/account.test.ts +284 -0
  68. package/src/bop-sdk/interface/test/chain.test.ts +45 -0
  69. package/src/bop-sdk/interface/test/contract.test.ts +283 -0
  70. package/src/bop-sdk/interface/test/ledger.test.ts +166 -0
  71. package/src/bop-sdk/interface/test/provider.test.ts +407 -0
  72. package/src/bop-sdk/interface/test/signer.test.ts +105 -0
  73. package/src/bop-sdk/interface/test/transaction.test.ts +713 -0
  74. package/src/bop-sdk/proto/bop-sdk-common.proto +159 -0
  75. package/src/bop-sdk/proto/bop-sdk-common.ts +829 -0
  76. package/src/bop-sdk/proto/bop-sdk-interface.proto +581 -0
  77. package/src/bop-sdk/proto/bop-sdk-interface.ts +10839 -0
  78. package/src/bop-sdk/proto/bop-sdk-utils.proto +86 -0
  79. package/src/bop-sdk/proto/bop-sdk-utils.ts +1495 -0
  80. package/src/bop-sdk/utils/abi/abi-coder.ts +147 -0
  81. package/src/bop-sdk/utils/abi/address.ts +109 -0
  82. package/src/bop-sdk/utils/abi/bignumber.ts +403 -0
  83. package/src/bop-sdk/utils/abi/bytes.ts +565 -0
  84. package/src/bop-sdk/utils/abi/coders/abstract-coder.ts +239 -0
  85. package/src/bop-sdk/utils/abi/coders/address.ts +58 -0
  86. package/src/bop-sdk/utils/abi/coders/anonymous.ts +25 -0
  87. package/src/bop-sdk/utils/abi/coders/array.ts +275 -0
  88. package/src/bop-sdk/utils/abi/coders/boolean.ts +21 -0
  89. package/src/bop-sdk/utils/abi/coders/bytes.ts +36 -0
  90. package/src/bop-sdk/utils/abi/coders/fixed-bytes.ts +35 -0
  91. package/src/bop-sdk/utils/abi/coders/null.ts +25 -0
  92. package/src/bop-sdk/utils/abi/coders/number.ts +56 -0
  93. package/src/bop-sdk/utils/abi/coders/string.ts +24 -0
  94. package/src/bop-sdk/utils/abi/coders/tuple.ts +73 -0
  95. package/src/bop-sdk/utils/abi/constants/addresses.ts +1 -0
  96. package/src/bop-sdk/utils/abi/constants/bignumbers.ts +30 -0
  97. package/src/bop-sdk/utils/abi/constants/hashes.ts +2 -0
  98. package/src/bop-sdk/utils/abi/constants/index.ts +15 -0
  99. package/src/bop-sdk/utils/abi/constants/strings.ts +2 -0
  100. package/src/bop-sdk/utils/abi/fragments.ts +1243 -0
  101. package/src/bop-sdk/utils/abi/id.ts +6 -0
  102. package/src/bop-sdk/utils/abi/index.ts +46 -0
  103. package/src/bop-sdk/utils/abi/interface.ts +921 -0
  104. package/src/bop-sdk/utils/abi/keccak256.ts +9 -0
  105. package/src/bop-sdk/utils/abi/properties.ts +172 -0
  106. package/src/bop-sdk/utils/abi/strings/bytes32.ts +40 -0
  107. package/src/bop-sdk/utils/abi/strings/idna.ts +258 -0
  108. package/src/bop-sdk/utils/abi/strings/index.ts +28 -0
  109. package/src/bop-sdk/utils/abi/strings/utf8.ts +373 -0
  110. package/src/bop-sdk/utils/encryption-address.ts +501 -0
  111. package/src/bop-sdk/utils/encryption-crypto.ts +427 -0
  112. package/src/bop-sdk/utils/encryption-mnemonics.ts +120 -0
  113. package/src/bop-sdk/utils/index.ts +27 -0
  114. package/src/bop-sdk/utils/logger.ts +480 -0
  115. package/src/index.ts +35 -0
@@ -0,0 +1,731 @@
1
+ import {
2
+ GetTransactionCacheRequest,
3
+ GetTransactionHistoryRequest,
4
+ StatusCodes,
5
+ } from "bop/bop-proto/bop";
6
+ import {
7
+ GetTxPoolSizeResponse,
8
+ GetTransactionCacheResponse,
9
+ GetTransactionHistoryResponse,
10
+ OffLineTxResponse,
11
+ SignerSignResponse,
12
+ SubmitTransactionParams,
13
+ SubmitTransactionResponse,
14
+ OpCreateAccountParams,
15
+ OpCreateContractParams,
16
+ OpGasSendParams,
17
+ OpContractInvokeParams,
18
+ OpSetMetadataParams,
19
+ OpSetPrivParams,
20
+ OpBatchGasSend,
21
+ OpBatchContractInvoke,
22
+ TestTransactionRequest,
23
+ TestTransactionResponse,
24
+ } from "../../../bop-sdk/proto/bop-sdk-interface";
25
+ import { Signer } from "../abstract-service/abstract-signer";
26
+ import { BopInterface } from "../../../bop/bop-base";
27
+ import { transaction } from "../abstract-service/abstract-transaction";
28
+ import * as trans from "./helper";
29
+ import {
30
+ SdkStatusCode,
31
+ sdkStatusCodeFromJSON,
32
+ } from "../../../bop-sdk/proto/bop-sdk-common";
33
+
34
+ const Transaction = require("./offline/transaction");
35
+
36
+ export class TransactionByBop extends transaction {
37
+ readonly bopInterface: BopInterface;
38
+ constructor(bop: BopInterface) {
39
+ super();
40
+ this.bopInterface = bop;
41
+ }
42
+
43
+ public async getTxPoolSize(): Promise<GetTxPoolSizeResponse> {
44
+ return trans.TxTxCacheSizeToTxPoolSizeResponse(
45
+ await this.bopInterface.callService("base_getTxCacheSize"),
46
+ );
47
+ }
48
+
49
+ public async getTxPoolTransactions(
50
+ limit?: number,
51
+ address?: string,
52
+ hash?: string,
53
+ ): Promise<GetTransactionCacheResponse> {
54
+ const request: GetTransactionCacheRequest = {};
55
+ if (limit !== undefined) request.limit = limit;
56
+ if (hash !== undefined) request.hash = hash;
57
+ if (address !== undefined) request.address = address;
58
+ return trans.TxTxCacheChainToTxCacheResponse(
59
+ await this.bopInterface.callService(
60
+ "base_getTransactionCacheChain",
61
+ request,
62
+ ),
63
+ );
64
+ }
65
+
66
+ public async getTransactionHistory(
67
+ seq?: number,
68
+ start?: number,
69
+ limit?: number,
70
+ hash?: string,
71
+ ): Promise<GetTransactionHistoryResponse> {
72
+ const request: GetTransactionHistoryRequest = {};
73
+ if (hash !== undefined) request.hash = hash;
74
+ if (seq !== undefined) request.ledgerSeq = seq;
75
+ if (start !== undefined) request.start = start;
76
+ if (limit !== undefined) request.limit = limit;
77
+
78
+ return trans.TxTxHistoryToTxHistoryResponse(
79
+ await this.bopInterface.callService(
80
+ "base_getTransactionHistory",
81
+ request,
82
+ ),
83
+ );
84
+ }
85
+
86
+ public async submitTransaction(
87
+ params: SubmitTransactionParams,
88
+ ): Promise<SubmitTransactionResponse> {
89
+ return trans.TxSubmitTxToSubmitTransactionResponse(
90
+ await this.bopInterface.callService("base_submitTransaction", params),
91
+ );
92
+ }
93
+
94
+ public async estimateGas(
95
+ transaction: TestTransactionRequest,
96
+ ): Promise<TestTransactionResponse> {
97
+ return await this.bopInterface.callService(
98
+ "base_testTransaction",
99
+ transaction,
100
+ );
101
+ }
102
+
103
+ //离线接口
104
+ public async buildAccountCreateTx(
105
+ params: OpCreateAccountParams,
106
+ signer: Signer[],
107
+ sourceAddress?: string,
108
+ ): Promise<OffLineTxResponse> {
109
+ const response: OffLineTxResponse = {};
110
+ //sourceAddress, ceilLedgerSeq, remarks, destAddress, initBalance, feeLimit, gasPrice, nonceType
111
+ if (signer.length === 0) {
112
+ response.errorCode = SdkStatusCode.NO_PROVIDER_SET;
113
+ response.errorDesc = "please set provider";
114
+ return response;
115
+ }
116
+
117
+ let obj = {
118
+ sourceAddress:
119
+ sourceAddress === undefined ? signer[0].getAddress() : sourceAddress,
120
+ destAddress: params.destAddress === undefined ? "" : params.destAddress,
121
+ initBalance: params.amount === undefined ? "0" : params.amount.toString(),
122
+ remarks:
123
+ params.params?.remarks === undefined ? "" : params.params?.remarks,
124
+ feeLimit:
125
+ params.params?.feeLimit === undefined
126
+ ? "0"
127
+ : params.params?.feeLimit.toString(),
128
+ gasPrice:
129
+ params.params?.gasPrice === undefined
130
+ ? "0"
131
+ : params.params?.gasPrice.toString(),
132
+ ceilLedgerSeq:
133
+ params.params?.ceilLedgerSeq === undefined
134
+ ? ""
135
+ : params.params?.ceilLedgerSeq.toString(),
136
+ nonceType:
137
+ params.params?.nonceType === undefined
138
+ ? "0"
139
+ : Number(params.params?.nonceType).toString(),
140
+ };
141
+
142
+ let offlineTx = new Transaction();
143
+ let tx = (await offlineTx.createAccount(obj, signer[0])) as {
144
+ errorCode;
145
+ errorDesc;
146
+ };
147
+ if (tx.errorCode !== 0) {
148
+ response.errorCode = sdkStatusCodeFromJSON(tx.errorCode);
149
+ response.errorDesc = tx.errorDesc;
150
+ } else {
151
+ response.result = {
152
+ transactionBlob: "",
153
+ signatures: [],
154
+ };
155
+ for (let element of signer) {
156
+ let signResponse = (await element.signTransaction(
157
+ offlineTx.getBlob(),
158
+ )) as SignerSignResponse;
159
+
160
+ if (signResponse.errorCode !== SdkStatusCode.SUCCESS) {
161
+ response.errorCode = signResponse.errorCode;
162
+ response.errorDesc = signResponse.errorDesc;
163
+ response.result = undefined;
164
+ break;
165
+ }
166
+ response.result.signatures.push(signResponse.result);
167
+ }
168
+ response.errorCode = SdkStatusCode.SUCCESS;
169
+ response.errorDesc = "ok";
170
+ response.result.transactionBlob = offlineTx.getBlob();
171
+ }
172
+ return response;
173
+ }
174
+
175
+ public async buildContractCreateTx(
176
+ params: OpCreateContractParams,
177
+ signer: Signer[],
178
+ sourceAddress?: string,
179
+ ): Promise<OffLineTxResponse> {
180
+ const response: OffLineTxResponse = {};
181
+ if (signer.length === 0) {
182
+ response.errorCode = SdkStatusCode.NO_PROVIDER_SET;
183
+ response.errorDesc = "please set provider";
184
+ return response;
185
+ }
186
+ //sourceAddress, payload, initBalance, remarks, type, feeLimit, gasPrice, ceilLedgerSeq, initInput, nonceType
187
+ let obj = {
188
+ sourceAddress:
189
+ sourceAddress === undefined ? signer[0].getAddress() : sourceAddress,
190
+ payload: params.payload === undefined ? "" : params.payload,
191
+ initBalance:
192
+ params.initBalance === undefined ? "0" : params.initBalance.toString(),
193
+ remarks:
194
+ params.params?.remarks === undefined ? "" : params.params?.remarks,
195
+ type: params.type === undefined ? "" : Number(params.type),
196
+ feeLimit:
197
+ params.params?.feeLimit === undefined
198
+ ? "0"
199
+ : params.params?.feeLimit.toString(),
200
+ gasPrice:
201
+ params.params?.gasPrice === undefined
202
+ ? "0"
203
+ : params.params?.gasPrice.toString(),
204
+ ceilLedgerSeq:
205
+ params.params?.ceilLedgerSeq === undefined
206
+ ? ""
207
+ : params.params?.ceilLedgerSeq.toString(),
208
+ initInput: params.initInput === undefined ? "" : params.initInput,
209
+ nonceType:
210
+ params.params?.nonceType === undefined
211
+ ? "0"
212
+ : Number(params.params?.nonceType).toString(),
213
+ };
214
+
215
+ let offlineTx = new Transaction();
216
+ let tx = (await offlineTx.createContract(obj, signer[0])) as {
217
+ errorCode;
218
+ errorDesc;
219
+ };
220
+ if (tx.errorCode !== 0) {
221
+ response.errorCode = sdkStatusCodeFromJSON(tx.errorCode);
222
+ response.errorDesc = tx.errorDesc;
223
+ } else {
224
+ response.result = {
225
+ transactionBlob: "",
226
+ signatures: [],
227
+ };
228
+ for (let element of signer) {
229
+ let signResponse = (await element.signTransaction(
230
+ offlineTx.getBlob(),
231
+ )) as SignerSignResponse;
232
+
233
+ if (signResponse.errorCode !== SdkStatusCode.SUCCESS) {
234
+ response.errorCode = signResponse.errorCode;
235
+ response.errorDesc = signResponse.errorDesc;
236
+ response.result = undefined;
237
+ break;
238
+ }
239
+ response.result.signatures.push(signResponse.result);
240
+ }
241
+ response.errorCode = SdkStatusCode.SUCCESS;
242
+ response.errorDesc = "ok";
243
+ response.result.transactionBlob = offlineTx.getBlob();
244
+ }
245
+
246
+ return response;
247
+ }
248
+
249
+ public async buildGasSendTx(
250
+ params: OpGasSendParams,
251
+ signer: Signer[],
252
+ sourceAddress?: string,
253
+ ): Promise<OffLineTxResponse> {
254
+ let response: OffLineTxResponse = {};
255
+ if (signer.length === 0) {
256
+ response.errorCode = SdkStatusCode.NO_PROVIDER_SET;
257
+ response.errorDesc = "please set provider";
258
+ return response;
259
+ }
260
+ // sourceAddress, destAddress, remarks, amount, ceilLedgerSeq, gasPrice, feeLimit, nonceType
261
+ let obj = {
262
+ sourceAddress:
263
+ sourceAddress === undefined ? signer[0].getAddress() : sourceAddress,
264
+ destAddress: params.destAddress === undefined ? "" : params.destAddress,
265
+ remarks:
266
+ params.params?.remarks === undefined ? "" : params.params?.remarks,
267
+ amount: params.amount === undefined ? "0" : params.amount.toString(),
268
+ ceilLedgerSeq:
269
+ params.params?.ceilLedgerSeq === undefined
270
+ ? ""
271
+ : params.params?.ceilLedgerSeq.toString(),
272
+ gasPrice:
273
+ params.params?.gasPrice === undefined
274
+ ? "0"
275
+ : params.params?.gasPrice.toString(),
276
+ feeLimit:
277
+ params.params?.feeLimit === undefined
278
+ ? "0"
279
+ : params.params?.feeLimit.toString(),
280
+ nonceType:
281
+ params.params?.nonceType === undefined
282
+ ? "0"
283
+ : Number(params.params?.nonceType).toString(),
284
+ };
285
+
286
+ let offlineTx = new Transaction();
287
+ let tx = (await offlineTx.gasSend(obj, signer[0])) as {
288
+ errorCode;
289
+ errorDesc;
290
+ };
291
+ if (tx.errorCode !== 0) {
292
+ response.errorCode = sdkStatusCodeFromJSON(tx.errorCode);
293
+ response.errorDesc = tx.errorDesc;
294
+ } else {
295
+ response.result = {
296
+ transactionBlob: "",
297
+ signatures: [],
298
+ };
299
+ for (let element of signer) {
300
+ let signResponse = (await element.signTransaction(
301
+ offlineTx.getBlob(),
302
+ )) as SignerSignResponse;
303
+
304
+ if (signResponse.errorCode !== SdkStatusCode.SUCCESS) {
305
+ response.errorCode = signResponse.errorCode;
306
+ response.errorDesc = signResponse.errorDesc;
307
+ response.result = undefined;
308
+ break;
309
+ }
310
+ response.result.signatures.push(signResponse.result);
311
+ }
312
+ response.errorCode = SdkStatusCode.SUCCESS;
313
+ response.errorDesc = "ok";
314
+ response.result.transactionBlob = offlineTx.getBlob();
315
+ }
316
+
317
+ return response;
318
+ }
319
+
320
+ public async buildContractInvokeTx(
321
+ params: OpContractInvokeParams,
322
+ signer: Signer[],
323
+ sourceAddress?: string,
324
+ ): Promise<OffLineTxResponse> {
325
+ const response: OffLineTxResponse = {};
326
+ if (signer.length === 0) {
327
+ response.errorCode = SdkStatusCode.NO_PROVIDER_SET;
328
+ response.errorDesc = "please set provider";
329
+ return response;
330
+ }
331
+ //sourceAddress, feeLimit, gasPrice, contractAddress, remarks, amount, ceilLedgerSeq, input, nonceType
332
+ let obj = {
333
+ sourceAddress:
334
+ sourceAddress === undefined ? signer[0].getAddress() : sourceAddress,
335
+ feeLimit:
336
+ params.params?.feeLimit === undefined
337
+ ? "0"
338
+ : params.params?.feeLimit.toString(),
339
+ gasPrice:
340
+ params.params?.gasPrice === undefined
341
+ ? "0"
342
+ : params.params?.gasPrice.toString(),
343
+ contractAddress:
344
+ params.contractAddress === undefined ? "0" : params.contractAddress,
345
+ remarks:
346
+ params.params?.remarks === undefined ? "" : params.params?.remarks,
347
+ amount: params.amount === undefined ? "0" : params.amount.toString(),
348
+ ceilLedgerSeq:
349
+ params.params?.ceilLedgerSeq === undefined
350
+ ? ""
351
+ : params.params?.ceilLedgerSeq.toString(),
352
+ input: params.input === undefined ? "" : params.input,
353
+ nonceType:
354
+ params.params?.nonceType === undefined
355
+ ? "0"
356
+ : Number(params.params?.nonceType).toString(),
357
+ };
358
+
359
+ let offlineTx = new Transaction();
360
+ let tx = (await offlineTx.contractInvoke(obj, signer[0])) as {
361
+ errorCode;
362
+ errorDesc;
363
+ };
364
+ if (tx.errorCode !== 0) {
365
+ response.errorCode = sdkStatusCodeFromJSON(tx.errorCode);
366
+ response.errorDesc = tx.errorDesc;
367
+ } else {
368
+ response.result = {
369
+ transactionBlob: "",
370
+ signatures: [],
371
+ };
372
+ for (let element of signer) {
373
+ let signResponse = (await element.signTransaction(
374
+ offlineTx.getBlob(),
375
+ )) as SignerSignResponse;
376
+
377
+ if (signResponse.errorCode !== SdkStatusCode.SUCCESS) {
378
+ response.errorCode = signResponse.errorCode;
379
+ response.errorDesc = signResponse.errorDesc;
380
+ response.result = undefined;
381
+ break;
382
+ }
383
+ response.result.signatures.push(signResponse.result);
384
+ }
385
+ response.errorCode = SdkStatusCode.SUCCESS;
386
+ response.errorDesc = "ok";
387
+ response.result.transactionBlob = offlineTx.getBlob();
388
+ }
389
+ return response;
390
+ }
391
+
392
+ public async buildSetMetadataTx(
393
+ params: OpSetMetadataParams,
394
+ signer: Signer[],
395
+ sourceAddress?: string,
396
+ ): Promise<OffLineTxResponse> {
397
+ const response: OffLineTxResponse = {};
398
+ if (signer.length === 0) {
399
+ response.errorCode = SdkStatusCode.NO_PROVIDER_SET;
400
+ response.errorDesc = "please set provider";
401
+ return response;
402
+ }
403
+ //sourceAddress, remarks, key, value, version, deleteFlag, feeLimit, gasPrice, ceilLedgerSeq, nonceType
404
+ let obj = {
405
+ sourceAddress:
406
+ sourceAddress === undefined ? signer[0].getAddress() : sourceAddress,
407
+ key: params.key === undefined ? "" : params.key,
408
+ value: params.value === undefined ? "" : params.value,
409
+ version: params.version === undefined ? "1" : params.version.toString(),
410
+ deleteFlag: params.deleteFlag === undefined ? false : params.deleteFlag,
411
+ remarks:
412
+ params.params?.remarks === undefined ? "" : params.params?.remarks,
413
+ feeLimit:
414
+ params.params?.feeLimit === undefined
415
+ ? "0"
416
+ : params.params?.feeLimit.toString(),
417
+ gasPrice:
418
+ params.params?.gasPrice === undefined
419
+ ? "0"
420
+ : params.params?.gasPrice.toString(),
421
+ ceilLedgerSeq:
422
+ params.params?.ceilLedgerSeq === undefined
423
+ ? ""
424
+ : params.params?.ceilLedgerSeq.toString(),
425
+ nonceType:
426
+ params.params?.nonceType === undefined
427
+ ? "0"
428
+ : Number(params.params?.nonceType).toString(),
429
+ };
430
+
431
+ let offlineTx = new Transaction();
432
+ let tx = (await offlineTx.setMetadatas(obj, signer[0])) as {
433
+ errorCode;
434
+ errorDesc;
435
+ };
436
+ if (tx.errorCode !== 0) {
437
+ response.errorCode = sdkStatusCodeFromJSON(tx.errorCode);
438
+ response.errorDesc = tx.errorDesc;
439
+ } else {
440
+ response.result = {
441
+ transactionBlob: "",
442
+ signatures: [],
443
+ };
444
+ for (let element of signer) {
445
+ let signResponse = (await element.signTransaction(
446
+ offlineTx.getBlob(),
447
+ )) as SignerSignResponse;
448
+
449
+ if (signResponse.errorCode !== SdkStatusCode.SUCCESS) {
450
+ response.errorCode = signResponse.errorCode;
451
+ response.errorDesc = signResponse.errorDesc;
452
+ response.result = undefined;
453
+ break;
454
+ }
455
+ response.result.signatures.push(signResponse.result);
456
+ }
457
+ response.errorCode = SdkStatusCode.SUCCESS;
458
+ response.errorDesc = "ok";
459
+ response.result.transactionBlob = offlineTx.getBlob();
460
+ }
461
+ return response;
462
+ }
463
+
464
+ public async buildSetPrivilegeTx(
465
+ params: OpSetPrivParams,
466
+ signer: Signer[],
467
+ sourceAddress?: string,
468
+ ): Promise<OffLineTxResponse> {
469
+ const response: OffLineTxResponse = {};
470
+ if (signer.length === 0) {
471
+ response.errorCode = SdkStatusCode.NO_PROVIDER_SET;
472
+ response.errorDesc = "please set provider";
473
+ return response;
474
+ }
475
+ //sourceAddress, txThreshold, signers, typeThresholds, feeLimit, gasPrice, ceilLedgerSeq, remarks, masterWeight, nonceType
476
+
477
+ let signers = [];
478
+ if (params?.signers !== undefined) {
479
+ for (let element of params?.signers) {
480
+ let sign = {
481
+ address: element.address === undefined ? "" : element.address,
482
+ weight:
483
+ element.weight === undefined ? "0" : element.weight.toString(),
484
+ };
485
+ console.log(sign);
486
+ signers.push(sign);
487
+ }
488
+ }
489
+
490
+ let typeThresholds = [];
491
+ if (params?.typeThresholds !== undefined) {
492
+ for (let element of params?.typeThresholds) {
493
+ let threshold = {
494
+ type:
495
+ element.type === undefined ? "0" : Number(element.type).toString(),
496
+ threshold:
497
+ element.threshold === undefined
498
+ ? "0"
499
+ : Number(element.threshold).toString(),
500
+ };
501
+ console.log(threshold);
502
+ typeThresholds.push(threshold);
503
+ }
504
+ }
505
+ let obj = {
506
+ sourceAddress:
507
+ sourceAddress === undefined ? signer[0].getAddress() : sourceAddress,
508
+ txThreshold: params.txThreshold,
509
+ masterWeight: params.masterWeight,
510
+ signers: signers,
511
+ typeThresholds: typeThresholds,
512
+ remarks:
513
+ params.params?.remarks === undefined ? "" : params.params?.remarks,
514
+ feeLimit:
515
+ params.params?.feeLimit === undefined
516
+ ? "0"
517
+ : params.params?.feeLimit.toString(),
518
+ gasPrice:
519
+ params.params?.gasPrice === undefined
520
+ ? "0"
521
+ : params.params?.gasPrice.toString(),
522
+ ceilLedgerSeq:
523
+ params.params?.ceilLedgerSeq === undefined
524
+ ? ""
525
+ : params.params?.ceilLedgerSeq.toString(),
526
+ nonceType:
527
+ params.params?.nonceType === undefined
528
+ ? "0"
529
+ : Number(params.params?.nonceType).toString(),
530
+ };
531
+
532
+ let offlineTx = new Transaction();
533
+ let tx = (await offlineTx.setPrivilege(obj, signer[0])) as {
534
+ errorCode;
535
+ errorDesc;
536
+ };
537
+ if (tx.errorCode !== 0) {
538
+ response.errorCode = sdkStatusCodeFromJSON(tx.errorCode);
539
+ response.errorDesc = tx.errorDesc;
540
+ } else {
541
+ response.result = {
542
+ transactionBlob: "",
543
+ signatures: [],
544
+ };
545
+ for (let element of signer) {
546
+ let signResponse = (await element.signTransaction(
547
+ offlineTx.getBlob(),
548
+ )) as SignerSignResponse;
549
+
550
+ if (signResponse.errorCode !== SdkStatusCode.SUCCESS) {
551
+ response.errorCode = signResponse.errorCode;
552
+ response.errorDesc = signResponse.errorDesc;
553
+ response.result = undefined;
554
+ break;
555
+ }
556
+ response.result.signatures.push(signResponse.result);
557
+ }
558
+ response.errorCode = SdkStatusCode.SUCCESS;
559
+ response.errorDesc = "ok";
560
+ response.result.transactionBlob = offlineTx.getBlob();
561
+ }
562
+ return response;
563
+ }
564
+
565
+ public async buildBatchGasSend(
566
+ params: OpBatchGasSend,
567
+ signer: Signer[],
568
+ sourceAddress?: string,
569
+ ): Promise<OffLineTxResponse> {
570
+ let response: OffLineTxResponse = {};
571
+ if (signer.length === 0) {
572
+ response.errorCode = SdkStatusCode.NO_PROVIDER_SET;
573
+ response.errorDesc = "please set provider";
574
+ return response;
575
+ }
576
+
577
+ //协议转换,转换为js JSON格式结构
578
+ let operations = [];
579
+ if (params.params !== undefined) {
580
+ for (let element of params?.params) {
581
+ let gassend = {
582
+ destAddress:
583
+ element.destAddress === undefined ? "" : element.destAddress,
584
+ amount:
585
+ element.amount === undefined ? "0" : element.amount.toString(),
586
+ };
587
+ console.log(gassend);
588
+ operations.push(gassend);
589
+ }
590
+ }
591
+ let obj = {
592
+ senderAddress:
593
+ sourceAddress === undefined ? signer[0].getAddress() : sourceAddress,
594
+ remarks: params.base?.remarks === undefined ? "" : params.base?.remarks,
595
+ ceilLedgerSeq:
596
+ params.base?.ceilLedgerSeq === undefined
597
+ ? ""
598
+ : params.base?.ceilLedgerSeq.toString(),
599
+ gasPrice:
600
+ params.base?.gasPrice === undefined
601
+ ? "0"
602
+ : params.base?.gasPrice.toString(),
603
+ feeLimit:
604
+ params.base?.feeLimit === undefined
605
+ ? "0"
606
+ : params.base?.feeLimit.toString(),
607
+ nonceType:
608
+ params.base?.nonceType === undefined
609
+ ? "0"
610
+ : Number(params.base?.nonceType).toString(),
611
+ operations: operations,
612
+ };
613
+
614
+ let offlineTx = new Transaction();
615
+ let tx = (await offlineTx.batchGasSend(obj, signer[0])) as {
616
+ errorCode;
617
+ errorDesc;
618
+ };
619
+ if (tx.errorCode !== 0) {
620
+ response.errorCode = sdkStatusCodeFromJSON(tx.errorCode);
621
+ response.errorDesc = tx.errorDesc;
622
+ } else {
623
+ response.result = {
624
+ transactionBlob: "",
625
+ signatures: [],
626
+ };
627
+ for (let element of signer) {
628
+ let signResponse = (await element.signTransaction(
629
+ offlineTx.getBlob(),
630
+ )) as SignerSignResponse;
631
+
632
+ if (signResponse.errorCode !== SdkStatusCode.SUCCESS) {
633
+ response.errorCode = signResponse.errorCode;
634
+ response.errorDesc = signResponse.errorDesc;
635
+ response.result = undefined;
636
+ break;
637
+ }
638
+ response.result.signatures.push(signResponse.result);
639
+ }
640
+ response.errorCode = SdkStatusCode.SUCCESS;
641
+ response.errorDesc = "ok";
642
+ response.result.transactionBlob = offlineTx.getBlob();
643
+ }
644
+
645
+ return response;
646
+ }
647
+
648
+ public async buildBatchContractInvoke(
649
+ params: OpBatchContractInvoke,
650
+ signer: Signer[],
651
+ sourceAddress?: string,
652
+ ): Promise<OffLineTxResponse> {
653
+ const response: OffLineTxResponse = {};
654
+ if (signer.length === 0) {
655
+ response.errorCode = SdkStatusCode.NO_PROVIDER_SET;
656
+ response.errorDesc = "please set provider";
657
+ return response;
658
+ }
659
+ let operations = [];
660
+ if (params.params !== undefined) {
661
+ for (let element of params?.params) {
662
+ let invoke = {
663
+ contractAddress:
664
+ element.contractAddress === undefined
665
+ ? ""
666
+ : element.contractAddress,
667
+ input: element.input === undefined ? "" : element.input,
668
+ amount:
669
+ element.amount === undefined ? "0" : element.amount.toString(),
670
+ };
671
+ console.log(invoke);
672
+ operations.push(invoke);
673
+ }
674
+ }
675
+ //senderAddress, feeLimit, gasPrice, remarks, ceilLedgerSeq, operations, nonceType
676
+ let obj = {
677
+ senderAddress:
678
+ sourceAddress === undefined ? signer[0].getAddress() : sourceAddress,
679
+ remarks: params.base?.remarks === undefined ? "" : params.base?.remarks,
680
+ ceilLedgerSeq:
681
+ params.base?.ceilLedgerSeq === undefined
682
+ ? ""
683
+ : params.base?.ceilLedgerSeq.toString(),
684
+ gasPrice:
685
+ params.base?.gasPrice === undefined
686
+ ? "0"
687
+ : params.base?.gasPrice.toString(),
688
+ feeLimit:
689
+ params.base?.feeLimit === undefined
690
+ ? "0"
691
+ : params.base?.feeLimit.toString(),
692
+ nonceType:
693
+ params.base?.nonceType === undefined
694
+ ? "0"
695
+ : Number(params.base?.nonceType).toString(),
696
+ operations: operations,
697
+ };
698
+
699
+ let offlineTx = new Transaction();
700
+ let tx = (await offlineTx.batchContractInvoke(obj, signer[0])) as {
701
+ errorCode;
702
+ errorDesc;
703
+ };
704
+ if (tx.errorCode !== 0) {
705
+ response.errorCode = sdkStatusCodeFromJSON(tx.errorCode);
706
+ response.errorDesc = tx.errorDesc;
707
+ } else {
708
+ response.result = {
709
+ transactionBlob: "",
710
+ signatures: [],
711
+ };
712
+ for (let element of signer) {
713
+ let signResponse = (await element.signTransaction(
714
+ offlineTx.getBlob(),
715
+ )) as SignerSignResponse;
716
+
717
+ if (signResponse.errorCode !== SdkStatusCode.SUCCESS) {
718
+ response.errorCode = signResponse.errorCode;
719
+ response.errorDesc = signResponse.errorDesc;
720
+ response.result = undefined;
721
+ break;
722
+ }
723
+ response.result.signatures.push(signResponse.result);
724
+ }
725
+ response.errorCode = SdkStatusCode.SUCCESS;
726
+ response.errorDesc = "ok";
727
+ response.result.transactionBlob = offlineTx.getBlob();
728
+ }
729
+ return response;
730
+ }
731
+ }