@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,373 @@
1
+ "use strict";
2
+
3
+ import { arrayify, BytesLike } from "../bytes";
4
+
5
+ import { Logger } from "../../logger";
6
+ const version = "utf8/5.7.0";
7
+ const logger = new Logger(version);
8
+
9
+ ///////////////////////////////
10
+
11
+ export enum UnicodeNormalizationForm {
12
+ current = "",
13
+ NFC = "NFC",
14
+ NFD = "NFD",
15
+ NFKC = "NFKC",
16
+ NFKD = "NFKD",
17
+ }
18
+
19
+ export enum Utf8ErrorReason {
20
+ // A continuation byte was present where there was nothing to continue
21
+ // - offset = the index the codepoint began in
22
+ UNEXPECTED_CONTINUE = "unexpected continuation byte",
23
+
24
+ // An invalid (non-continuation) byte to start a UTF-8 codepoint was found
25
+ // - offset = the index the codepoint began in
26
+ BAD_PREFIX = "bad codepoint prefix",
27
+
28
+ // The string is too short to process the expected codepoint
29
+ // - offset = the index the codepoint began in
30
+ OVERRUN = "string overrun",
31
+
32
+ // A missing continuation byte was expected but not found
33
+ // - offset = the index the continuation byte was expected at
34
+ MISSING_CONTINUE = "missing continuation byte",
35
+
36
+ // The computed code point is outside the range for UTF-8
37
+ // - offset = start of this codepoint
38
+ // - badCodepoint = the computed codepoint; outside the UTF-8 range
39
+ OUT_OF_RANGE = "out of UTF-8 range",
40
+
41
+ // UTF-8 strings may not contain UTF-16 surrogate pairs
42
+ // - offset = start of this codepoint
43
+ // - badCodepoint = the computed codepoint; inside the UTF-16 surrogate range
44
+ UTF16_SURROGATE = "UTF-16 surrogate",
45
+
46
+ // The string is an overlong representation
47
+ // - offset = start of this codepoint
48
+ // - badCodepoint = the computed codepoint; already bounds checked
49
+ OVERLONG = "overlong representation",
50
+ }
51
+
52
+ export type Utf8ErrorFunc = (
53
+ reason: Utf8ErrorReason,
54
+ offset: number,
55
+ bytes: ArrayLike<number>,
56
+ output: Array<number>,
57
+ badCodepoint?: number,
58
+ ) => number;
59
+
60
+ function errorFunc(
61
+ reason: Utf8ErrorReason,
62
+ offset: number,
63
+ bytes: ArrayLike<number>,
64
+ output: Array<number>,
65
+ badCodepoint?: number,
66
+ ): number {
67
+ return logger.throwArgumentError(
68
+ `invalid codepoint at offset ${offset}; ${reason}`,
69
+ "bytes",
70
+ bytes,
71
+ );
72
+ }
73
+
74
+ function ignoreFunc(
75
+ reason: Utf8ErrorReason,
76
+ offset: number,
77
+ bytes: ArrayLike<number>,
78
+ output: Array<number>,
79
+ badCodepoint?: number,
80
+ ): number {
81
+ // If there is an invalid prefix (including stray continuation), skip any additional continuation bytes
82
+ if (
83
+ reason === Utf8ErrorReason.BAD_PREFIX ||
84
+ reason === Utf8ErrorReason.UNEXPECTED_CONTINUE
85
+ ) {
86
+ let i = 0;
87
+ for (let o = offset + 1; o < bytes.length; o++) {
88
+ if (bytes[o] >> 6 !== 0x02) {
89
+ break;
90
+ }
91
+ i++;
92
+ }
93
+ return i;
94
+ }
95
+
96
+ // This byte runs us past the end of the string, so just jump to the end
97
+ // (but the first byte was read already read and therefore skipped)
98
+ if (reason === Utf8ErrorReason.OVERRUN) {
99
+ return bytes.length - offset - 1;
100
+ }
101
+
102
+ // Nothing to skip
103
+ return 0;
104
+ }
105
+
106
+ function replaceFunc(
107
+ reason: Utf8ErrorReason,
108
+ offset: number,
109
+ bytes: ArrayLike<number>,
110
+ output: Array<number>,
111
+ badCodepoint?: number,
112
+ ): number {
113
+ // Overlong representations are otherwise "valid" code points; just non-deistingtished
114
+ if (reason === Utf8ErrorReason.OVERLONG) {
115
+ output.push(badCodepoint);
116
+ return 0;
117
+ }
118
+
119
+ // Put the replacement character into the output
120
+ output.push(0xfffd);
121
+
122
+ // Otherwise, process as if ignoring errors
123
+ return ignoreFunc(reason, offset, bytes, output, badCodepoint);
124
+ }
125
+
126
+ // Common error handing strategies
127
+ export const Utf8ErrorFuncs: { [name: string]: Utf8ErrorFunc } = Object.freeze({
128
+ error: errorFunc,
129
+ ignore: ignoreFunc,
130
+ replace: replaceFunc,
131
+ });
132
+
133
+ // http://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript#13691499
134
+ function getUtf8CodePoints(
135
+ bytes: BytesLike,
136
+ onError?: Utf8ErrorFunc,
137
+ ): Array<number> {
138
+ if (onError == null) {
139
+ onError = Utf8ErrorFuncs.error;
140
+ }
141
+
142
+ bytes = arrayify(bytes);
143
+
144
+ const result: Array<number> = [];
145
+ let i = 0;
146
+
147
+ // Invalid bytes are ignored
148
+ while (i < bytes.length) {
149
+ const c = bytes[i++];
150
+
151
+ // 0xxx xxxx
152
+ if (c >> 7 === 0) {
153
+ result.push(c);
154
+ continue;
155
+ }
156
+
157
+ // Multibyte; how many bytes left for this character?
158
+ let extraLength = null;
159
+ let overlongMask = null;
160
+
161
+ // 110x xxxx 10xx xxxx
162
+ if ((c & 0xe0) === 0xc0) {
163
+ extraLength = 1;
164
+ overlongMask = 0x7f;
165
+
166
+ // 1110 xxxx 10xx xxxx 10xx xxxx
167
+ } else if ((c & 0xf0) === 0xe0) {
168
+ extraLength = 2;
169
+ overlongMask = 0x7ff;
170
+
171
+ // 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
172
+ } else if ((c & 0xf8) === 0xf0) {
173
+ extraLength = 3;
174
+ overlongMask = 0xffff;
175
+ } else {
176
+ if ((c & 0xc0) === 0x80) {
177
+ i += onError(Utf8ErrorReason.UNEXPECTED_CONTINUE, i - 1, bytes, result);
178
+ } else {
179
+ i += onError(Utf8ErrorReason.BAD_PREFIX, i - 1, bytes, result);
180
+ }
181
+ continue;
182
+ }
183
+
184
+ // Do we have enough bytes in our data?
185
+ if (i - 1 + extraLength >= bytes.length) {
186
+ i += onError(Utf8ErrorReason.OVERRUN, i - 1, bytes, result);
187
+ continue;
188
+ }
189
+
190
+ // Remove the length prefix from the char
191
+ let res = c & ((1 << (8 - extraLength - 1)) - 1);
192
+
193
+ for (let j = 0; j < extraLength; j++) {
194
+ let nextChar = bytes[i];
195
+
196
+ // Invalid continuation byte
197
+ if ((nextChar & 0xc0) != 0x80) {
198
+ i += onError(Utf8ErrorReason.MISSING_CONTINUE, i, bytes, result);
199
+ res = null;
200
+ break;
201
+ }
202
+
203
+ res = (res << 6) | (nextChar & 0x3f);
204
+ i++;
205
+ }
206
+
207
+ // See above loop for invalid continuation byte
208
+ if (res === null) {
209
+ continue;
210
+ }
211
+
212
+ // Maximum code point
213
+ if (res > 0x10ffff) {
214
+ i += onError(
215
+ Utf8ErrorReason.OUT_OF_RANGE,
216
+ i - 1 - extraLength,
217
+ bytes,
218
+ result,
219
+ res,
220
+ );
221
+ continue;
222
+ }
223
+
224
+ // Reserved for UTF-16 surrogate halves
225
+ if (res >= 0xd800 && res <= 0xdfff) {
226
+ i += onError(
227
+ Utf8ErrorReason.UTF16_SURROGATE,
228
+ i - 1 - extraLength,
229
+ bytes,
230
+ result,
231
+ res,
232
+ );
233
+ continue;
234
+ }
235
+
236
+ // Check for overlong sequences (more bytes than needed)
237
+ if (res <= overlongMask) {
238
+ i += onError(
239
+ Utf8ErrorReason.OVERLONG,
240
+ i - 1 - extraLength,
241
+ bytes,
242
+ result,
243
+ res,
244
+ );
245
+ continue;
246
+ }
247
+
248
+ result.push(res);
249
+ }
250
+
251
+ return result;
252
+ }
253
+
254
+ // http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array
255
+ export function toUtf8Bytes(
256
+ str: string,
257
+ form: UnicodeNormalizationForm = UnicodeNormalizationForm.current,
258
+ ): Uint8Array {
259
+ if (form != UnicodeNormalizationForm.current) {
260
+ logger.checkNormalize();
261
+ str = str.normalize(form);
262
+ }
263
+
264
+ let result = [];
265
+ for (let i = 0; i < str.length; i++) {
266
+ const c = str.charCodeAt(i);
267
+
268
+ if (c < 0x80) {
269
+ result.push(c);
270
+ } else if (c < 0x800) {
271
+ result.push((c >> 6) | 0xc0);
272
+ result.push((c & 0x3f) | 0x80);
273
+ } else if ((c & 0xfc00) == 0xd800) {
274
+ i++;
275
+ const c2 = str.charCodeAt(i);
276
+
277
+ if (i >= str.length || (c2 & 0xfc00) !== 0xdc00) {
278
+ throw new Error("invalid utf-8 string");
279
+ }
280
+
281
+ // Surrogate Pair
282
+ const pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
283
+ result.push((pair >> 18) | 0xf0);
284
+ result.push(((pair >> 12) & 0x3f) | 0x80);
285
+ result.push(((pair >> 6) & 0x3f) | 0x80);
286
+ result.push((pair & 0x3f) | 0x80);
287
+ } else {
288
+ result.push((c >> 12) | 0xe0);
289
+ result.push(((c >> 6) & 0x3f) | 0x80);
290
+ result.push((c & 0x3f) | 0x80);
291
+ }
292
+ }
293
+
294
+ return arrayify(result);
295
+ }
296
+
297
+ function escapeChar(value: number) {
298
+ const hex = "0000" + value.toString(16);
299
+ return "\\u" + hex.substring(hex.length - 4);
300
+ }
301
+
302
+ export function _toEscapedUtf8String(
303
+ bytes: BytesLike,
304
+ onError?: Utf8ErrorFunc,
305
+ ): string {
306
+ return (
307
+ '"' +
308
+ getUtf8CodePoints(bytes, onError)
309
+ .map((codePoint) => {
310
+ if (codePoint < 256) {
311
+ switch (codePoint) {
312
+ case 8:
313
+ return "\\b";
314
+ case 9:
315
+ return "\\t";
316
+ case 10:
317
+ return "\\n";
318
+ case 13:
319
+ return "\\r";
320
+ case 34:
321
+ return '\\"';
322
+ case 92:
323
+ return "\\\\";
324
+ }
325
+
326
+ if (codePoint >= 32 && codePoint < 127) {
327
+ return String.fromCharCode(codePoint);
328
+ }
329
+ }
330
+
331
+ if (codePoint <= 0xffff) {
332
+ return escapeChar(codePoint);
333
+ }
334
+
335
+ codePoint -= 0x10000;
336
+ return (
337
+ escapeChar(((codePoint >> 10) & 0x3ff) + 0xd800) +
338
+ escapeChar((codePoint & 0x3ff) + 0xdc00)
339
+ );
340
+ })
341
+ .join("") +
342
+ '"'
343
+ );
344
+ }
345
+
346
+ export function _toUtf8String(codePoints: Array<number>): string {
347
+ return codePoints
348
+ .map((codePoint) => {
349
+ if (codePoint <= 0xffff) {
350
+ return String.fromCharCode(codePoint);
351
+ }
352
+ codePoint -= 0x10000;
353
+ return String.fromCharCode(
354
+ ((codePoint >> 10) & 0x3ff) + 0xd800,
355
+ (codePoint & 0x3ff) + 0xdc00,
356
+ );
357
+ })
358
+ .join("");
359
+ }
360
+
361
+ export function toUtf8String(
362
+ bytes: BytesLike,
363
+ onError?: Utf8ErrorFunc,
364
+ ): string {
365
+ return _toUtf8String(getUtf8CodePoints(bytes, onError));
366
+ }
367
+
368
+ export function toUtf8CodePoints(
369
+ str: string,
370
+ form: UnicodeNormalizationForm = UnicodeNormalizationForm.current,
371
+ ): Array<number> {
372
+ return getUtf8CodePoints(toUtf8Bytes(str, form));
373
+ }