quantumcoin 7.0.10 → 7.0.11

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 (172) hide show
  1. package/README-SDK.md +0 -5
  2. package/README.md +1 -3
  3. package/SPEC.md +2 -63
  4. package/config.js +10 -2
  5. package/examples/example.js +0 -5
  6. package/examples/example.ts +0 -5
  7. package/examples/node_modules/.bin/esbuild +16 -0
  8. package/examples/node_modules/.bin/esbuild.cmd +17 -0
  9. package/examples/node_modules/.bin/esbuild.ps1 +28 -0
  10. package/examples/node_modules/.bin/sdkgen +16 -0
  11. package/examples/node_modules/.bin/sdkgen.cmd +17 -0
  12. package/examples/node_modules/.bin/sdkgen.ps1 +28 -0
  13. package/examples/node_modules/.bin/tsx +16 -0
  14. package/examples/node_modules/.bin/tsx.cmd +17 -0
  15. package/examples/node_modules/.bin/tsx.ps1 +28 -0
  16. package/examples/node_modules/.package-lock.json +235 -0
  17. package/examples/node_modules/@esbuild/win32-x64/README.md +3 -0
  18. package/examples/node_modules/@esbuild/win32-x64/esbuild.exe +0 -0
  19. package/examples/node_modules/@esbuild/win32-x64/package.json +20 -0
  20. package/examples/node_modules/esbuild/LICENSE.md +21 -0
  21. package/examples/node_modules/esbuild/README.md +3 -0
  22. package/examples/node_modules/esbuild/bin/esbuild +223 -0
  23. package/examples/node_modules/esbuild/install.js +289 -0
  24. package/examples/node_modules/esbuild/lib/main.d.ts +716 -0
  25. package/examples/node_modules/esbuild/lib/main.js +2532 -0
  26. package/examples/node_modules/esbuild/package.json +49 -0
  27. package/examples/node_modules/get-tsconfig/LICENSE +21 -0
  28. package/examples/node_modules/get-tsconfig/README.md +235 -0
  29. package/examples/node_modules/get-tsconfig/dist/index.cjs +7 -0
  30. package/examples/node_modules/get-tsconfig/dist/index.d.cts +2088 -0
  31. package/examples/node_modules/get-tsconfig/dist/index.d.mts +2088 -0
  32. package/examples/node_modules/get-tsconfig/dist/index.mjs +7 -0
  33. package/examples/node_modules/get-tsconfig/package.json +46 -0
  34. package/examples/node_modules/quantum-coin-js-sdk/.github/workflows/publish-npmjs.yaml +22 -0
  35. package/examples/node_modules/quantum-coin-js-sdk/LICENSE +21 -0
  36. package/examples/node_modules/quantum-coin-js-sdk/LICENSE-wasm_exec.js.txt +30 -0
  37. package/examples/node_modules/quantum-coin-js-sdk/README.md +1665 -0
  38. package/examples/node_modules/quantum-coin-js-sdk/example/README.md +14 -0
  39. package/examples/node_modules/quantum-coin-js-sdk/example/conversion-example.js +19 -0
  40. package/examples/node_modules/quantum-coin-js-sdk/example/example-create-contract.js +396 -0
  41. package/examples/node_modules/quantum-coin-js-sdk/example/example-encode-decode-rlp.js +225 -0
  42. package/examples/node_modules/quantum-coin-js-sdk/example/example-event-pack-unpack.js +391 -0
  43. package/examples/node_modules/quantum-coin-js-sdk/example/example-misc.js +101 -0
  44. package/examples/node_modules/quantum-coin-js-sdk/example/example-rpc-send-signRawTransaction.js +318 -0
  45. package/examples/node_modules/quantum-coin-js-sdk/example/example-rpc-send.js +116 -0
  46. package/examples/node_modules/quantum-coin-js-sdk/example/example-send.js +70 -0
  47. package/examples/node_modules/quantum-coin-js-sdk/example/example-token-pack-unpack.js +961 -0
  48. package/examples/node_modules/quantum-coin-js-sdk/example/example-wallet-version4.js +35 -0
  49. package/examples/node_modules/quantum-coin-js-sdk/example/example-wallet.js +43 -0
  50. package/examples/node_modules/quantum-coin-js-sdk/example/example.js +405 -0
  51. package/examples/node_modules/quantum-coin-js-sdk/example/package-lock.json +134 -0
  52. package/examples/node_modules/quantum-coin-js-sdk/example/package.json +15 -0
  53. package/examples/node_modules/quantum-coin-js-sdk/index.d.ts +1024 -0
  54. package/examples/node_modules/quantum-coin-js-sdk/index.js +3062 -0
  55. package/examples/node_modules/quantum-coin-js-sdk/package.json +34 -0
  56. package/examples/node_modules/quantum-coin-js-sdk/tests/encrypted-32.json +1 -0
  57. package/examples/node_modules/quantum-coin-js-sdk/tests/encrypted-36.json +1 -0
  58. package/examples/node_modules/quantum-coin-js-sdk/tests/encrypted-48.json +1 -0
  59. package/examples/node_modules/quantum-coin-js-sdk/tests/generate-verify-vectors.js +91 -0
  60. package/examples/node_modules/quantum-coin-js-sdk/tests/non-transactional.preinit.test.js +41 -0
  61. package/examples/node_modules/quantum-coin-js-sdk/tests/non-transactional.test.js +686 -0
  62. package/examples/node_modules/quantum-coin-js-sdk/tests/sign-raw-keytype5-context-null.test.js +107 -0
  63. package/examples/node_modules/quantum-coin-js-sdk/tests/sign-raw-transaction.test.js +196 -0
  64. package/examples/node_modules/quantum-coin-js-sdk/tests/sign-verify.test.js +311 -0
  65. package/examples/node_modules/quantum-coin-js-sdk/tests/transactional.relay.test.js +131 -0
  66. package/examples/node_modules/quantum-coin-js-sdk/tests/transactional.rpc.test.js +103 -0
  67. package/examples/node_modules/quantum-coin-js-sdk/tests/verify-vectors.json +95035 -0
  68. package/examples/node_modules/quantum-coin-js-sdk/wasmBase64.d.ts +9 -0
  69. package/examples/node_modules/quantum-coin-js-sdk/wasmBase64.js +16 -0
  70. package/examples/node_modules/quantum-coin-js-sdk/wasm_exec.d.ts +0 -0
  71. package/examples/node_modules/quantum-coin-js-sdk/wasm_exec.js +587 -0
  72. package/examples/node_modules/resolve-pkg-maps/LICENSE +21 -0
  73. package/examples/node_modules/resolve-pkg-maps/README.md +216 -0
  74. package/examples/node_modules/resolve-pkg-maps/dist/index.cjs +1 -0
  75. package/examples/node_modules/resolve-pkg-maps/dist/index.d.cts +11 -0
  76. package/examples/node_modules/resolve-pkg-maps/dist/index.d.mts +11 -0
  77. package/examples/node_modules/resolve-pkg-maps/dist/index.mjs +1 -0
  78. package/examples/node_modules/resolve-pkg-maps/package.json +42 -0
  79. package/examples/node_modules/seed-words/.github/workflows/publish-npmjs.yaml +22 -0
  80. package/examples/node_modules/seed-words/BUILD.md +7 -0
  81. package/examples/node_modules/seed-words/LICENSE +121 -0
  82. package/examples/node_modules/seed-words/README.md +67 -0
  83. package/examples/node_modules/seed-words/dist/seedwords.d.ts +39 -0
  84. package/examples/node_modules/seed-words/package.json +27 -0
  85. package/examples/node_modules/seed-words/seedwords.js +315 -0
  86. package/examples/node_modules/seed-words/seedwords.txt +65536 -0
  87. package/examples/node_modules/seed-words/tsconfig.json +21 -0
  88. package/examples/node_modules/tsx/LICENSE +21 -0
  89. package/examples/node_modules/tsx/README.md +32 -0
  90. package/examples/node_modules/tsx/dist/cjs/api/index.cjs +1 -0
  91. package/examples/node_modules/tsx/dist/cjs/api/index.d.cts +35 -0
  92. package/examples/node_modules/tsx/dist/cjs/api/index.d.mts +35 -0
  93. package/examples/node_modules/tsx/dist/cjs/api/index.mjs +1 -0
  94. package/examples/node_modules/tsx/dist/cjs/index.cjs +1 -0
  95. package/examples/node_modules/tsx/dist/cjs/index.mjs +1 -0
  96. package/examples/node_modules/tsx/dist/cli.cjs +54 -0
  97. package/examples/node_modules/tsx/dist/cli.mjs +55 -0
  98. package/examples/node_modules/tsx/dist/client-BQVF1NaW.mjs +1 -0
  99. package/examples/node_modules/tsx/dist/client-D6NvIMSC.cjs +1 -0
  100. package/examples/node_modules/tsx/dist/esm/api/index.cjs +1 -0
  101. package/examples/node_modules/tsx/dist/esm/api/index.d.cts +35 -0
  102. package/examples/node_modules/tsx/dist/esm/api/index.d.mts +35 -0
  103. package/examples/node_modules/tsx/dist/esm/api/index.mjs +1 -0
  104. package/examples/node_modules/tsx/dist/esm/index.cjs +2 -0
  105. package/examples/node_modules/tsx/dist/esm/index.mjs +2 -0
  106. package/examples/node_modules/tsx/dist/get-pipe-path-BHW2eJdv.mjs +1 -0
  107. package/examples/node_modules/tsx/dist/get-pipe-path-BoR10qr8.cjs +1 -0
  108. package/examples/node_modules/tsx/dist/index-7AaEi15b.mjs +14 -0
  109. package/examples/node_modules/tsx/dist/index-BWFBUo6r.cjs +1 -0
  110. package/examples/node_modules/tsx/dist/index-gbaejti9.mjs +1 -0
  111. package/examples/node_modules/tsx/dist/index-gckBtVBf.cjs +14 -0
  112. package/examples/node_modules/tsx/dist/lexer-DQCqS3nf.mjs +3 -0
  113. package/examples/node_modules/tsx/dist/lexer-DgIbo0BU.cjs +3 -0
  114. package/examples/node_modules/tsx/dist/loader.cjs +1 -0
  115. package/examples/node_modules/tsx/dist/loader.mjs +1 -0
  116. package/examples/node_modules/tsx/dist/node-features-_8ZFwP_x.mjs +1 -0
  117. package/examples/node_modules/tsx/dist/node-features-roYmp9jK.cjs +1 -0
  118. package/examples/node_modules/tsx/dist/package-CeBgXWuR.mjs +1 -0
  119. package/examples/node_modules/tsx/dist/package-Dxt5kIHw.cjs +1 -0
  120. package/examples/node_modules/tsx/dist/patch-repl.cjs +1 -0
  121. package/examples/node_modules/tsx/dist/patch-repl.mjs +1 -0
  122. package/examples/node_modules/tsx/dist/preflight.cjs +1 -0
  123. package/examples/node_modules/tsx/dist/preflight.mjs +1 -0
  124. package/examples/node_modules/tsx/dist/register-2sWVXuRQ.cjs +1 -0
  125. package/examples/node_modules/tsx/dist/register-B7jrtLTO.mjs +1 -0
  126. package/examples/node_modules/tsx/dist/register-CFH5oNdT.mjs +4 -0
  127. package/examples/node_modules/tsx/dist/register-D46fvsV_.cjs +4 -0
  128. package/examples/node_modules/tsx/dist/repl.cjs +3 -0
  129. package/examples/node_modules/tsx/dist/repl.mjs +3 -0
  130. package/examples/node_modules/tsx/dist/require-D4F1Lv60.cjs +1 -0
  131. package/examples/node_modules/tsx/dist/require-DQxpCAr4.mjs +1 -0
  132. package/examples/node_modules/tsx/dist/suppress-warnings.cjs +1 -0
  133. package/examples/node_modules/tsx/dist/suppress-warnings.mjs +1 -0
  134. package/examples/node_modules/tsx/dist/temporary-directory-B83uKxJF.cjs +1 -0
  135. package/examples/node_modules/tsx/dist/temporary-directory-CwHp0_NW.mjs +1 -0
  136. package/examples/node_modules/tsx/dist/types-Cxp8y2TL.d.ts +5 -0
  137. package/examples/node_modules/tsx/package.json +68 -0
  138. package/examples/offline-signing.js +0 -2
  139. package/examples/offline-signing.ts +0 -1
  140. package/examples/package-lock.json +422 -73
  141. package/examples/package.json +1 -1
  142. package/examples/wallet-offline.js +1 -9
  143. package/examples/wallet-offline.ts +1 -9
  144. package/generate-sdk.js +4 -6
  145. package/package.json +2 -2
  146. package/src/abi/interface.js +13 -7
  147. package/src/abi/js-abi-coder.js +23 -18
  148. package/src/constants.d.ts +0 -5
  149. package/src/constants.js +0 -7
  150. package/src/contract/contract-factory.js +9 -3
  151. package/src/contract/contract.js +9 -3
  152. package/src/errors/index.js +12 -0
  153. package/src/index.d.ts +0 -3
  154. package/src/providers/extra-providers.js +20 -6
  155. package/src/providers/json-rpc-provider.js +15 -5
  156. package/src/providers/provider.d.ts +0 -2
  157. package/src/providers/provider.js +1 -3
  158. package/src/utils/address.d.ts +0 -14
  159. package/src/utils/address.js +12 -49
  160. package/src/utils/hashing.d.ts +0 -6
  161. package/src/utils/hashing.js +8 -23
  162. package/src/utils/index.d.ts +0 -3
  163. package/src/utils/rlp.js +7 -4
  164. package/src/wallet/wallet.d.ts +2 -13
  165. package/src/wallet/wallet.js +116 -97
  166. package/test/security/malformed-input.test.js +295 -1
  167. package/test/unit/address-wallet.test.js +188 -129
  168. package/test/unit/address-wallet.test.ts +187 -128
  169. package/test/unit/hashing.test.js +0 -11
  170. package/test/unit/hashing.test.ts +0 -11
  171. package/test/unit/providers.test.js +3 -1
  172. package/test/unit/providers.test.ts +3 -1
@@ -0,0 +1,3062 @@
1
+ //index.js
2
+
3
+ /**
4
+ * Quantum Coin Blockchain SDK
5
+ * @module quantum-coin-js-sdk
6
+ * @description Quantum Coin JS SDK provides low level functionality to interact with the Quantum Coin Blockchain.
7
+ * {@link https://github.com/quantumcoinproject/quantum-coin-js-sdk/tree/main/example|Example Project}
8
+ * @example
9
+ *
10
+ * Requires Node.js version v20.18.1 or higher
11
+ *
12
+ * Installation:
13
+ * npm install quantum-coin-js-sdk --save
14
+ *
15
+ * //Adding reference:
16
+ * var qcsdk = require('quantum-coin-js-sdk');
17
+ *
18
+ * //Example initialization with defaults for mainnet
19
+ * //Initialize the SDK first before invoking any other function
20
+ * qcsdk.initialize(null).then((initResult) => {
21
+ *
22
+ * }
23
+ *
24
+ * //Example initialization with specific values
25
+ * //Initialize the SDK first before invoking any other function
26
+ * var clientConfigVal = new qcsdk.Config("https://sdk.readrelay.quantumcoinapi.com", "https://sdk.writerelay.quantumcoinapi.com", 123123, "", ""); //Initialization with Mainnet Config (Block Explorer: https://QuantumScan.com)
27
+ * qcsdk.initialize(clientConfigVal).then((initResult) => {
28
+ *
29
+ * }
30
+ * Example Project: https://github.com/quantumcoinproject/quantum-coin-js-sdk/tree/main/example
31
+ *
32
+ */
33
+
34
+ var wasmexec = require('./wasm_exec');
35
+ var wasmBase64 = require('./wasmBase64');
36
+ const crypto = require("crypto");
37
+ var seedwords = require('seed-words');
38
+
39
+ var config = null;
40
+ var isInitialized = false;
41
+ /** CIRCL WASM namespace (set after InitAccountsWebAssembly). Use getCircl() for access. */
42
+ var circl = null;
43
+ const DEFAULT_GAS = 21000;
44
+ const API_KEY_HEADER_NAME = "X-API-KEY";
45
+ const REQUEST_ID_HEADER_NAME = "X-REQUEST-ID";
46
+
47
+ // Key type and seed constants (CIRCL migration; see pqc-circl-migration.md)
48
+ const KEY_TYPE_HYBRIDEDMLDSASLHDSA = 3;
49
+ const KEY_TYPE_HYBRIDEDMLDSASLHDSA5 = 5;
50
+ const SEED_WORD_LIST_LENGTH_HYBRIDEDS = 48;
51
+ const SEED_WORD_LIST_LENGTH_HYBRIDEDMLDSASLHDSA5 = 36;
52
+ const SEED_WORD_LIST_LENGTH_HYBRIDEDMLDSASLHDSA = 32;
53
+ const BASE_SEED_BYTES_HYBRIDEDS = 96;
54
+ const BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA5 = 72;
55
+ const BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA = 64;
56
+ const MIN_PASSPHRASE_LENGTH = 12;
57
+ const INVALID_KEY_TYPE = -1001;
58
+ const CIRCL_CRYPTO_FAILURE = -1002;
59
+
60
+ /**
61
+ * @class
62
+ * @constructor
63
+ * @public
64
+ * @classdesc This is the configuration class required to initialize and interact with Quantum Coin blockchain
65
+ */
66
+ class Config {
67
+ /**
68
+ * Creates a config class
69
+ * @param {string} readUrl - The Read API URL pointing to a read relay. See https://github.com/quantumcoinproject/quantum-coin-go/tree/dogep/relay. The following URLs are community maintained. Please use your own relay service. Mainnet: https://sdk.readrelay.quantumcoinapi.com
70
+ * @param {string} writeUrl - The Write API URL pointing to a write relay. See https://github.com/quantumcoinproject/quantum-coin-go/tree/dogep/relay. The following URLs are community maintained. Please use your own relay service. Mainnet: https://sdk.writerelay.quantumcoinapi.com
71
+ * @param {number} chainId - The chain id of the blockchain. Mainnet chainId is 123123. Testnet T4 chainId is 310324.
72
+ * @param {string} readApiKey - Optional parameter if authorization is enabled for the relay service. API Key for authorization. Defaults to null which indicates no authorization.
73
+ * @param {string} writeApiKey - Optional parameter if authorization is enabled for the relay service. API Key for authorization. Defaults to null which indicates no authorization.
74
+ */
75
+
76
+ constructor(readUrl, writeUrl, chainId, readApiKey, writeApiKey) {
77
+ /**
78
+ * The Read API URL pointing to a read relay. See https://github.com/quantumcoinproject/quantum-coin-go/tree/dogep/relay
79
+ * @type {string}
80
+ * @public
81
+ */
82
+ this.readUrl = readUrl;
83
+
84
+ /**
85
+ * The Read API URL pointing to a read relay. See https://github.com/quantumcoinproject/quantum-coin-go/tree/dogep/relay
86
+ * @type {string}
87
+ * @public
88
+ */
89
+ this.writeUrl = writeUrl;
90
+
91
+ /**
92
+ * The chain id of the blockchain. Mainnet chainId is 123123. Testnet T4 chainId is 310324.
93
+ * @type {number}
94
+ * @public
95
+ */
96
+ this.chainId = chainId;
97
+
98
+ /**
99
+ * API Key for authorization if authorization is enabled for the relay service. Defaults to null which indicates no authorization.
100
+ * @type {string}
101
+ * @public
102
+ */
103
+ this.readApiKey = readApiKey;
104
+
105
+ /**
106
+ * API Key for authorization if authorization is enabled for the relay service. Defaults to null which indicates no authorization.
107
+ * @type {string}
108
+ * @public
109
+ */
110
+ this.writeApiKey = writeApiKey;
111
+ }
112
+ }
113
+
114
+ /**
115
+ * @class
116
+ * @constructor
117
+ * @public
118
+ * @classdesc This class represents a Wallet. Use the verifyWallet function to verify if a wallet is valid. Verifying the wallet is highly recommended, especially if it comes from an untrusted source. For more details on the underlying cryptography of the Wallet, see https://github.com/QuantumCoinProject/hybrid-pqc
119
+ */
120
+ class Wallet {
121
+ /**
122
+ * Creates a Wallet class. The constructor does not verify the wallet. To verify a wallet, call the verifyWallet function explicitly.
123
+ * @param {string} address - Address of the wallet
124
+ * @param {number[]} privateKey - Private Key byte array of the wallet
125
+ * @param {number[]} publicKey - The chain id of the blockchain. Mainnet chainId is 123123. Testnet T4 chainId is 310324.
126
+ */
127
+ constructor(address, privateKey, publicKey) {
128
+
129
+ /**
130
+ * Address of the wallet. Is 66 bytes in length including 0x (if the wallet is valid).
131
+ * @type {string}
132
+ * @public
133
+ */
134
+ this.address = address;
135
+
136
+ /**
137
+ * Private Key byte array of the wallet. Is 4064 bytes in length (if the wallet is valid).
138
+ * @type {number[]}
139
+ * @public
140
+ */
141
+ this.privateKey = privateKey;
142
+
143
+ /**
144
+ * Public Key byte array of the wallet. Is 1408 bytes in length (if the wallet is valid).
145
+ * @type {number[]}
146
+ * @public
147
+ */
148
+ this.publicKey = publicKey;
149
+ }
150
+ }
151
+
152
+ /**
153
+ * @class
154
+ * @constructor
155
+ * @public
156
+ * @classdesc This class represents a Block.
157
+ */
158
+ class BlockDetails {
159
+ constructor(blockNumber) {
160
+ /**
161
+ * Block Number of the block
162
+ * @type {number}
163
+ * @public
164
+ */
165
+ this.blockNumber = blockNumber;
166
+ }
167
+ }
168
+
169
+ /**
170
+ * @class
171
+ * @constructor
172
+ * @public
173
+ * @classdesc This class represents a result from invoking the getLatestBlock function.
174
+ */
175
+ class LatestBlockDetailsResult {
176
+ constructor(resultCode, blockDetails, response, requestId, err) {
177
+ /**
178
+ * Represents the result of the operation. A value of 0 represents that the operation succeeded. Any other value indicates the operation failed. See the result code section for more details.
179
+ * @type {number}
180
+ * @public
181
+ */
182
+ this.resultCode = resultCode;
183
+
184
+ /**
185
+ * An object of type BlockDetails representing the block. This value is null if the value of resultCode is not 0.
186
+ * @type {BlockDetails}
187
+ * @public
188
+ */
189
+ this.blockDetails = blockDetails;
190
+
191
+ /**
192
+ * An object of representing the raw Response returned by the service. For details, see https://developer.mozilla.org/en-US/docs/Web/API/Response. This value can be null if the value of resultCode is not 0.
193
+ * @type {Object}
194
+ * @public
195
+ */
196
+ this.response = response;
197
+
198
+ /**
199
+ * An unique id to represent the request. This can be null if request failed before it could be sent.
200
+ * @type {string}
201
+ * @public
202
+ */
203
+ this.requestId = requestId;
204
+
205
+ /**
206
+ * An error object if the operation resulted in an error and there was no response. This property is defined only if the resultCode is -10000.
207
+ * @type {Error}
208
+ * @public
209
+ */
210
+ this.err = err;
211
+ }
212
+ }
213
+
214
+ /**
215
+ * @class
216
+ * @constructor
217
+ * @public
218
+ * @classdesc This class represents an Account.
219
+ */
220
+ class AccountDetails {
221
+ constructor(address, balance, nonce, blockNumber) {
222
+ /**
223
+ * Address of the wallet. Is 66 bytes in length including 0x.
224
+ * @type {string}
225
+ * @public
226
+ */
227
+ this.address = address;
228
+
229
+ /**
230
+ * Balance of the account in wei. To convert this to ethers, see https://docs.ethers.org/v4/api-utils.html#ether-strings-and-wei
231
+ * @type {string}
232
+ * @public
233
+ */
234
+ this.balance = balance;
235
+
236
+ /**
237
+ * A monotonically increasing number representing the nonce of the account. After each transaction from the account that gets registered in the blockchain, the nonce increases by 1.
238
+ * @type {number}
239
+ * @public
240
+ */
241
+ this.nonce = nonce;
242
+
243
+ /**
244
+ * The block number as of which the Account details was retrieved.
245
+ * @type {number}
246
+ * @public
247
+ */
248
+ this.blockNumber = blockNumber;
249
+ }
250
+ }
251
+
252
+ /**
253
+ * @class
254
+ * @constructor
255
+ * @public
256
+ * @classdesc This class represents a result from invoking the getAccountDetails function.
257
+ */
258
+ class AccountDetailsResult {
259
+ constructor(resultCode, accountDetails, response, requestId, err) {
260
+ /**
261
+ * Represents the result of the operation. A value of 0 represents that the operation succeeded. Any other value indicates the operation failed. See the result code section for more details.
262
+ * @type {number}
263
+ * @public
264
+ */
265
+ this.resultCode = resultCode;
266
+
267
+ /**
268
+ * An object of type AccountDetails representing the block. This value is null if the value of resultCode is not 0.
269
+ * @type {AccountDetails}
270
+ * @public
271
+ */
272
+ this.accountDetails = accountDetails;
273
+
274
+ /**
275
+ * An object of representing the raw Response returned by the service. For details, see https://developer.mozilla.org/en-US/docs/Web/API/Response. This value can be null if the value of resultCode is not 0.
276
+ * @type {Object}
277
+ * @public
278
+ */
279
+ this.response = response;
280
+
281
+ /**
282
+ * An unique id to represent the request. This can be null if request failed before it could be sent.
283
+ * @type {string}
284
+ * @public
285
+ */
286
+ this.requestId = requestId;
287
+
288
+ /**
289
+ * An error object if the operation resulted in an error and there was no response. This property is defined only if the resultCode is -10000.
290
+ * @type {Error}
291
+ * @public
292
+ */
293
+ this.err = err;
294
+ }
295
+ }
296
+
297
+ /**
298
+ * @class
299
+ * @constructor
300
+ * @public
301
+ * @classdesc This class represents a result from invoking the signSendCoinTransaction function.
302
+ */
303
+ class SignResult {
304
+ constructor(resultCode, txnHash, txnData) {
305
+ /**
306
+ * Represents the result of the operation. A value of 0 represents that the operation succeeded. Any other value indicates the operation failed. See the result code section for more details.
307
+ * @type {number}
308
+ * @public
309
+ */
310
+ this.resultCode = resultCode;
311
+
312
+ /**
313
+ * Hash of the Transaction, to uniquely identify it. Is 66 bytes in length including 0x. This value is null if the value of resultCode is not 0.
314
+ * @type {string}
315
+ * @public
316
+ */
317
+ this.txnHash = txnHash;
318
+
319
+ /**
320
+ * A payload representing the signed transaction.
321
+ * To actually send a transaction, this payload can then be taken to to a different device that is connected to the blockchain relay and then sent using the postTransaction function.
322
+ * This value is null if the value of resultCode is not 0.
323
+ * @type {string}
324
+ * @public
325
+ */
326
+ this.txnData = txnData;
327
+ }
328
+ }
329
+
330
+ /**
331
+ * @class
332
+ * @constructor
333
+ * @public
334
+ * @classdesc This class represents a result from invoking the sendCoins function.
335
+ */
336
+ class SendResult {
337
+ constructor(resultCode, txnHash, response, requestId, err) {
338
+ /**
339
+ * Represents the result of the operation. A value of 0 represents that the operation succeeded. Any other value indicates the operation failed. See the result code section for more details.
340
+ * @type {number}
341
+ * @public
342
+ */
343
+ this.resultCode = resultCode;
344
+
345
+ /**
346
+ * Hash of the Transaction, to uniquely identify it. Is 66 bytes in length including 0x. This value is null if the value of resultCode is not 0.
347
+ * @type {string}
348
+ * @public
349
+ */
350
+ this.txnHash = txnHash;
351
+
352
+ /**
353
+ * An object of representing the raw Response returned by the service. For details, see https://developer.mozilla.org/en-US/docs/Web/API/Response. This value can be null if the value of resultCode is not 0.
354
+ * @type {Object}
355
+ * @public
356
+ */
357
+ this.response = response;
358
+
359
+ /**
360
+ * An unique id to represent the request. This can be null if request failed before it could be sent.
361
+ * @type {string}
362
+ * @public
363
+ */
364
+ this.requestId = requestId;
365
+
366
+ /**
367
+ * An error object if the operation resulted in an error and there was no response. This property is defined only if the resultCode is -10000.
368
+ * @type {Error}
369
+ * @public
370
+ */
371
+ this.err = err;
372
+ }
373
+ }
374
+
375
+ /**
376
+ * @class
377
+ * @constructor
378
+ * @public
379
+ * @classdesc This class represents a Receipt of a transaction that is registered in the blockchain. The transactionReceipt field can be null unless the transaction is registered with the blockchain.
380
+ * While the transaction is pending, this field will be null. You should consider the transaction as succeeded only if the status field's value is 0x1 (success).
381
+ */
382
+ class TransactionReceipt {
383
+ constructor() {
384
+ /**
385
+ * A hexadecimal string representing the total amount of gas used when this transaction was executed in the block.
386
+ * @type {string}
387
+ * @public
388
+ */
389
+ this.cumulativeGasUsed = null;
390
+
391
+ /**
392
+ * A hexadecimal string representing the sum of the base fee and tip paid per unit of gas.
393
+ * @type {string}
394
+ * @public
395
+ */
396
+ this.effectiveGasPrice = null;
397
+
398
+ /**
399
+ * A hexadecimal string representing the amount of gas used by this specific transaction alone.
400
+ * @type {string}
401
+ * @public
402
+ */
403
+ this.gasUsed = null;
404
+
405
+ /**
406
+ * A hexadecimal string representing either 0x1 (success) or 0x0 (failure). Failed transactions can also incur gas fee. You should consider the transaction as succeeded only if the status value is 0x1 (success).
407
+ * @type {string}
408
+ * @public
409
+ */
410
+ this.status = null;
411
+
412
+ /**
413
+ * Hash of the Transaction, to uniquely identify it. Is 66 bytes in length including 0x.
414
+ * @type {string}
415
+ * @public
416
+ */
417
+ this.hash = null;
418
+
419
+ /**
420
+ * A hexadecimal string representing the transaction type. 0x0 is DefaultFeeTxType.
421
+ * @type {string}
422
+ * @public
423
+ */
424
+ this.type = null;
425
+ }
426
+ }
427
+
428
+ /**
429
+ * @class
430
+ * @constructor
431
+ * @public
432
+ * @classdesc This class represents details of a transaction. You should consider the transaction as succeeded only if the status field of the receipt object is 0x1 (success).
433
+ */
434
+ class TransactionDetails {
435
+ constructor() {
436
+ /**
437
+ * A hexadecimal string representing the hash of the block that registered the transaction. This field can be null if the transaction was not registered in the blockchain.
438
+ * @type {string}
439
+ * @public
440
+ */
441
+ this.blockHash = null;
442
+
443
+ /**
444
+ * The number of the block that registered the transaction. This field can be null if the transaction was not registered in the blockchain.
445
+ * @type {number}
446
+ * @public
447
+ */
448
+ this.blockNumber = null;
449
+
450
+ /**
451
+ * A 66 character hexadecimal string representing the address the transaction is sent from.
452
+ * @type {string}
453
+ * @public
454
+ */
455
+ this.from = null;
456
+
457
+ /**
458
+ * A hexadecimal string representing the gas provided for the transaction execution.
459
+ * @type {string}
460
+ * @public
461
+ */
462
+ this.gas = null;
463
+
464
+ /**
465
+ * A hexadecimal string representing the gasPrice used for each paid gas, in Wei.
466
+ * @type {string}
467
+ * @public
468
+ */
469
+ this.gasPrice = null;
470
+
471
+ /**
472
+ * A 66 character hexadecimal string representing the hash of the transaction.
473
+ * @type {string}
474
+ * @public
475
+ */
476
+ this.hash = null;
477
+
478
+ /**
479
+ * A hexadecimal string representing the compiled code of a contract OR the hash of the invoked method signature and encoded parameters.
480
+ * @type {string}
481
+ * @public
482
+ */
483
+ this.input = null;
484
+
485
+ /**
486
+ * A monotonically increasing number representing the nonce of the account. After each transaction from the account that gets registered in the blockchain, the nonce increases by 1.
487
+ * @type {number}
488
+ * @public
489
+ */
490
+ this.nonce = null;
491
+
492
+ /**
493
+ * A 66 character hexadecimal string representing address the transaction is directed to.
494
+ * @type {string}
495
+ * @public
496
+ */
497
+ this.to = null;
498
+
499
+ /**
500
+ * A hexadecimal string representing the value sent with this transaction. The value can be 0 for smart contract transactions, since it only represents the number of coins sent.
501
+ * @type {string}
502
+ * @public
503
+ */
504
+ this.value = null;
505
+
506
+ /**
507
+ * The receipt of the transaction. This field will be null while the transaction is pending (not yet registered in the blockchain).
508
+ * @type {TransactionReceipt}
509
+ * @public
510
+ */
511
+ this.receipt = null;
512
+ }
513
+
514
+ }
515
+
516
+ /**
517
+ * @class
518
+ * @constructor
519
+ * @public
520
+ * @classdesc This class represents a result from invoking the getTransactionDetails function. If transactions get discarded by the blockchain, for reasons such as due to lower than minimum gas fees or invalid nonce, the resultCode will always contain a non-zero value (failure).
521
+ */
522
+ class TransactionDetailsResult {
523
+ constructor(resultCode, transactionDetails, response, requestId, err) {
524
+ /**
525
+ * Represents the result of the operation. A value of 0 represents that the operation succeeded. Any other value indicates the operation failed. See the result code section for more details.
526
+ * @type {number}
527
+ * @public
528
+ */
529
+
530
+ this.resultCode = resultCode;
531
+
532
+ /**
533
+ * An object of type TransactionDetails representing the transaction. This value is null if the value of resultCode is not 0.
534
+ * @type {TransactionDetails}
535
+ * @public
536
+ */
537
+ this.transactionDetails = transactionDetails;
538
+
539
+ /**
540
+ * An object of representing the raw Response returned by the service. For details, see https://developer.mozilla.org/en-US/docs/Web/API/Response. This value can be null if the value of resultCode is not 0.
541
+ * @type {Object}
542
+ * @public
543
+ */
544
+ this.response = response;
545
+
546
+ /**
547
+ * An unique id to represent the request. This can be null if request failed before it could be sent.
548
+ * @type {string}
549
+ * @public
550
+ */
551
+ this.requestId = requestId;
552
+
553
+ /**
554
+ * An error object if the operation resulted in an error and there was no response. This property is defined only if the resultCode is -10000.
555
+ * @type {Error}
556
+ * @public
557
+ */
558
+ this.err = err;
559
+ }
560
+ }
561
+
562
+ /**
563
+ * @class
564
+ * @constructor
565
+ * @public
566
+ * @classdesc This class represents a transaction of an account. You should consider the transaction as succeeded only if the status field is 0x1 (success).
567
+ */
568
+ class AccountTransactionCompact {
569
+ constructor() {
570
+ /**
571
+ * The number of the block that registered the transaction. This field can be null if the transaction was not registered in the blockchain.
572
+ * @type {number}
573
+ * @public
574
+ */
575
+ this.blockNumber = null;
576
+
577
+ /**
578
+ * A 66 character hexadecimal string representing the address the transaction is sent from.
579
+ * @type {string}
580
+ * @public
581
+ */
582
+ this.from = null;
583
+
584
+ /**
585
+ * A 66 character hexadecimal string representing the hash of the transaction.
586
+ * @type {string}
587
+ * @public
588
+ */
589
+ this.hash = null;
590
+
591
+ /**
592
+ * A 66 character hexadecimal string representing address the transaction is directed to.
593
+ * @type {string}
594
+ * @public
595
+ */
596
+ this.to = null;
597
+
598
+ /**
599
+ * A hexadecimal string representing the value sent with this transaction. The value can be 0 for smart contract transactions, since it only represents the number of coins sent.
600
+ * @type {string}
601
+ * @public
602
+ */
603
+ this.value = null;
604
+
605
+ /**
606
+ * A hexadecimal string representing either 0x1 (success) or 0x0 (failure). Failed transactions can also incur gas fee. You should consider the transaction as succeeded only if the status value is 0x1 (success).
607
+ * @type {string}
608
+ * @public
609
+ */
610
+ this.status = null;
611
+ }
612
+
613
+ }
614
+
615
+ /**
616
+ * @class
617
+ * @constructor
618
+ * @public
619
+ * @classdesc This class represents a list of account transactions returned by the listAccountTransactionDetails function.
620
+ */
621
+ class ListAccountTransactionsResponse {
622
+ constructor() {
623
+ /**
624
+ * The number of pages available for listing.
625
+ * @type {number}
626
+ * @public
627
+ */
628
+ this.pageCount = null;
629
+
630
+ /**
631
+ * An array of type AccountTransactionCompact, containing the list of transactions. Can be null if no items are available.
632
+ * @type {(AccountTransactionCompact|Array)}
633
+ * @public
634
+ */
635
+ this.items = null;
636
+ }
637
+
638
+ }
639
+
640
+ /**
641
+ * @class
642
+ * @constructor
643
+ * @public
644
+ * @classdesc This class represents a result from invoking the listAccountTransactionDetails function.
645
+ */
646
+ class AccountTransactionsResult {
647
+ constructor(resultCode, listAccountTransactionsResponse, response, requestId, err) {
648
+ /**
649
+ * Represents the result of the operation. A value of 0 represents that the operation succeeded. Any other value indicates the operation failed. See the result code section for more details.
650
+ * @type {number}
651
+ * @public
652
+ */
653
+ this.resultCode = resultCode;
654
+
655
+ /**
656
+ * An object of type ListAccountTransactionsResponse representing the list of transactions along with metadata. This value is null if the value of resultCode is not 0.
657
+ * @type {ListAccountTransactionsResponse}
658
+ * @public
659
+ */
660
+ this.listAccountTransactionsResponse = listAccountTransactionsResponse;
661
+
662
+ /**
663
+ * An object of representing the raw Response returned by the service. For details, see https://developer.mozilla.org/en-US/docs/Web/API/Response. This value can be null if the value of resultCode is not 0.
664
+ * @type {Object}
665
+ * @public
666
+ */
667
+ this.response = response;
668
+
669
+ /**
670
+ * An unique id to represent the request. This can be null if request failed before it could be sent.
671
+ * @type {string}
672
+ * @public
673
+ */
674
+ this.requestId = requestId;
675
+
676
+ /**
677
+ * An error object if the operation resulted in an error and there was no response. This property is defined only if the resultCode is -10000.
678
+ * @type {Error}
679
+ * @public
680
+ */
681
+ this.err = err;
682
+ }
683
+ }
684
+
685
+ /**
686
+ * @class
687
+ * @constructor
688
+ * @public
689
+ * @classdesc This class represents a signing request that can be passed to signTransaction.
690
+ */
691
+ class TransactionSigningRequest {
692
+ /**
693
+ * Creates a TransactionSigningRequest class.
694
+ * @param {Wallet} wallet - The wallet with which the transaction has to be signed. The constructor does not verify the wallet. To verify a wallet, call the verifyWallet function explicitly.
695
+ * @param {string} toAddress - The address to which the transaction request is made. Can be null (for example, for contract creation).
696
+ * @param {string|BigInt} valueInWei - The value in wei-units. Can be provided as either a hex string (including 0x prefix) or a BigInt. For example, to represent 1 coin, which is 1000000000000000000 in wei-units, set the value to "0xDE0B6B3A7640000" or BigInt("1000000000000000000"). {@link /example/conversion-example.js|Conversion Examples}
697
+ * @param {number} nonce - A monotonically increasing number representing the nonce of the account signing the transaction. After each transaction from the account that gets registered in the blockchain, the nonce increases by 1.
698
+ * @param {string} data - An optional hex string (including 0x) that represents the contract data. Can be null if not invoking or creating a contract.
699
+ * @param {number} gasLimit - A limit of gas to be used. Set 21000 for basic non smart contract transactions.
700
+ * @param {string} remarks - An optional hex string (including 0x) that represents a remark (such as a comment). Maximum 32 bytes length (in bytes). Warning, do not store any sensitive information in this field.
701
+ * @param {number|null} chainId - The chain id of the blockchain. Mainnet chainId is 123123. Testnet T4 chainId is 310324. If null, the chainId specified in the initialize() function will be used.
702
+ * @param {number|null} signingContext - It is recommended that you pass null for this parameter, unless the context needs to be set explicitly. Signing context determines the cryptographic scheme used to sign. The wallet key type should compatible with the signing context. Applicable values are 0,1,2. Default value if not specified will be determined dynamically from the wallet key type. Signing context 1,2 will incur additional gas fee. For information on the schemes, see https://github.com/quantumcoinproject/circl?tab=readme-ov-file#hybrid-schemes
703
+ * Signing context 0: Scheme used is hybrid-ed-mldsa-slhdsa compact (scheme id 3)
704
+ * Signing context 1: Scheme used is hybrid-ed-mldsa-slhdsa-5 (scheme id 5 : 20x the gas fee of scheme 0)
705
+ * Signing context 2: hybrid-ed-mldsa-slhdsa full (scheme id 4 : 30x the gas fee of scheme 0)
706
+ */
707
+ constructor(wallet, toAddress, valueInWei, nonce, data, gasLimit, remarks, chainId, signingContext) {
708
+ /**
709
+ * The wallet that should be used to sign the transaction.
710
+ * @type {Wallet}
711
+ * @public
712
+ */
713
+ this.wallet = wallet;
714
+
715
+ /**
716
+ * The address to which the transaction request is made. Can be null (for example, for contract creation).
717
+ * @type {string|null}
718
+ * @public
719
+ */
720
+ this.toAddress = toAddress;
721
+
722
+ /**
723
+ * The value in wei-units. Can be provided as either a hex string (including 0x prefix) or a BigInt. For example, to represent 1 coin, which is 1000000000000000000 in wei-units, set the value to "0xDE0B6B3A7640000" or BigInt("1000000000000000000"). {@link /example/conversion-example.js|Conversion Examples}
724
+ * @type {string|BigInt|null}
725
+ * @public
726
+ */
727
+ this.valueInWei = valueInWei;
728
+
729
+ /**
730
+ * A monotonically increasing number representing the nonce of the account signing the transaction. After each transaction from the account that gets registered in the blockchain, the nonce increases by 1.
731
+ * @type {number}
732
+ * @public
733
+ */
734
+ this.nonce = nonce;
735
+
736
+ /**
737
+ * An optional hex string (including 0x) that represents the contract data. Can be null if not invoking or creating a contract.
738
+ * @type {string|null}
739
+ * @public
740
+ */
741
+ this.data = data;
742
+
743
+ /**
744
+ * A limit of gas to be used. Set 21000 for basic non smart contract transactions.
745
+ * @type {number}
746
+ * @public
747
+ */
748
+ this.gasLimit = gasLimit;
749
+
750
+ /**
751
+ * An optional hex string (including 0x) that represents a remark (such as a comment). Maximum 32 bytes length (in bytes). Warning, do not store any sensitive information in this field.
752
+ * @type {string|null}
753
+ * @public
754
+ */
755
+ this.remarks = remarks;
756
+
757
+ /**
758
+ * The chain id of the blockchain. Mainnet chainId is 123123. If null, the chainId specified in the initialize() function will be used.
759
+ * @type {number|null}
760
+ * @public
761
+ */
762
+ this.chainId = chainId;
763
+
764
+ /**
765
+ * It is recommended that you pass null for this parameter, unless the context needs to be set explicitly. Signing context determines the cryptographic scheme used to sign. Gas fee varies by context.
766
+ * @type {number|null}
767
+ * @public
768
+ */
769
+ this.signingContext = signingContext;
770
+ }
771
+ }
772
+
773
+ /**
774
+ * @class
775
+ * @constructor
776
+ * @public
777
+ * @classdesc This class represents a result from invoking the packMethodData or unpackMethodData functions.
778
+ */
779
+ class PackUnpackResult {
780
+ /**
781
+ * Creates a PackUnpackResult class.
782
+ * @param {string} error - Error message if any. Empty string if no error.
783
+ * @param {string} result - The actual result as a string. Empty string if there was an error.
784
+ */
785
+ constructor(error, result) {
786
+ /**
787
+ * Error message if any. Empty string if no error.
788
+ * @type {string}
789
+ * @public
790
+ */
791
+ this.error = error;
792
+
793
+ /**
794
+ * The actual result as a string. Empty string if there was an error.
795
+ * @type {string}
796
+ * @public
797
+ */
798
+ this.result = result;
799
+ }
800
+ }
801
+
802
+ /**
803
+ * @class EventLogEncodeResult
804
+ * @classdesc This class represents a result from invoking the encodeEventLog function.
805
+ */
806
+ class EventLogEncodeResult {
807
+ /**
808
+ * Creates an EventLogEncodeResult class.
809
+ * @param {string} error - Error message if any. Empty string if no error.
810
+ * @param {Object|null} result - The actual result object with topics and data. Null if there was an error.
811
+ * @param {string[]} result.topics - Array of topic hex strings (with 0x prefix)
812
+ * @param {string} result.data - Hex-encoded data string (with 0x prefix)
813
+ */
814
+ constructor(error, result) {
815
+ /**
816
+ * Error message if any. Empty string if no error.
817
+ * @type {string}
818
+ * @public
819
+ */
820
+ this.error = error;
821
+
822
+ /**
823
+ * The actual result object with topics and data. Null if there was an error.
824
+ * @type {Object|null}
825
+ * @property {string[]} topics - Array of topic hex strings (with 0x prefix)
826
+ * @property {string} data - Hex-encoded data string (with 0x prefix)
827
+ * @public
828
+ */
829
+ this.result = result;
830
+ }
831
+ }
832
+
833
+ function isLargeNumber(val) {
834
+ if (val === null) {
835
+ return false;
836
+ }
837
+ if (typeof val === 'string' || val instanceof String) {
838
+ var rgx = /^([0-9]+([.][0-9]*)?|[.][0-9]+)$/;
839
+ return Boolean(val.match(rgx));
840
+ }
841
+ return false;
842
+ }
843
+
844
+ async function InitAccountsWebAssembly() {
845
+ const go = new global.Go();
846
+ let mod, inst;
847
+ var base64wasm = wasmBase64.getBase64Wasm();
848
+
849
+ const base64bytes = atob(base64wasm);
850
+ if (base64bytes === null || base64bytes === undefined || base64bytes.length === 0) {
851
+ throw new Error("Error parsing base64");
852
+ }
853
+ const wasmBytes = Uint8Array.from(atob(base64wasm), c => c.charCodeAt(0));
854
+ let result = await WebAssembly.instantiate(wasmBytes, go.importObject);
855
+ mod = result.module;
856
+ inst = result.instance;
857
+ go.run(inst);
858
+ const g = (typeof globalThis !== 'undefined' ? globalThis : typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this);
859
+ if (g && g.circl) {
860
+ circl = g.circl;
861
+ }
862
+ }
863
+
864
+ /**
865
+ * The initialize function has to be called before attempting to invoke any other function. This function should be called only once.
866
+ *
867
+ * @async
868
+ * @function initialize
869
+ * @param {Config|undefined} clientConfig - A configuration represented by the Config class. A default configuration is used, if not specified.
870
+ * @return {Promise<boolean>} Returns a promise of type boolean; true if the initialization succeeded, else false.
871
+ */
872
+ async function initialize(clientConfig) {
873
+ if (isInitialized === true) {
874
+ return false;
875
+ }
876
+ if (clientConfig === null || clientConfig === undefined) {
877
+ clientConfig = new Config("https://sdk.readrelay.quantumcoinapi.com", "https://sdk.writerelay.quantumcoinapi.com", 123123, "", ""); //default
878
+ }
879
+ if (clientConfig.readUrl === null || clientConfig.writeUrl === null || clientConfig.chainId === null) {
880
+ return false;
881
+ }
882
+ await InitAccountsWebAssembly();
883
+ config = clientConfig;
884
+ isInitialized = await seedwords.initialize();
885
+
886
+ return isInitialized;
887
+ }
888
+
889
+ /**
890
+ * The isAddressValid function validates whether an address is valid or not. An address is of length 66 characters including 0x.
891
+ *
892
+ * @function isAddressValid
893
+ * @param {string} address - A string representing the address to validate.
894
+ * @return {boolean} Returns true if the address validation succeeded, else returns false.
895
+ */
896
+ function isAddressValid(address) {
897
+ if (isInitialized === false) {
898
+ return -1000;
899
+ }
900
+
901
+ if (address === null) {
902
+ return false;
903
+ }
904
+
905
+ if (address.length !== 66) {
906
+ return false;
907
+ }
908
+
909
+ if (typeof address === 'string' || address instanceof String) {
910
+ return IsValidAddress(address);
911
+ }
912
+
913
+ return false;
914
+ }
915
+
916
+ /**
917
+ * Internal: get key type (KEY_TYPE_HYBRIDEDMLDSASLHDSA or KEY_TYPE_HYBRIDEDMLDSASLHDSA5) from private key length.
918
+ * @param {number[]|Uint8Array} privateKey - Wallet private key bytes.
919
+ * @returns {number|null} KEY_TYPE_HYBRIDEDMLDSASLHDSA (3), KEY_TYPE_HYBRIDEDMLDSASLHDSA5 (5), or null on error.
920
+ */
921
+ function getKeyTypeFromPrivateKey(privateKey) {
922
+ if (circl == null || !privateKey || typeof privateKey.length !== 'number') {
923
+ return null;
924
+ }
925
+ const len = privateKey.length;
926
+ const hybridNs = circl.hybridedmldsaslhdsa;
927
+ const hybrid5Ns = circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5;
928
+ if (hybridNs && len === hybridNs.PrivateKeySize) {
929
+ return KEY_TYPE_HYBRIDEDMLDSASLHDSA;
930
+ }
931
+ if (hybrid5Ns && len === hybrid5Ns.PrivateKeySize) {
932
+ return KEY_TYPE_HYBRIDEDMLDSASLHDSA5;
933
+ }
934
+ return null;
935
+ }
936
+
937
+ /**
938
+ * Internal: get key type (KEY_TYPE_HYBRIDEDMLDSASLHDSA or KEY_TYPE_HYBRIDEDMLDSASLHDSA5) from public key length.
939
+ * @param {number[]|Uint8Array} publicKey - Public key bytes.
940
+ * @returns {number|null} KEY_TYPE_HYBRIDEDMLDSASLHDSA (3), KEY_TYPE_HYBRIDEDMLDSASLHDSA5 (5), or null on error.
941
+ */
942
+ function getKeyTypeFromPublicKey(publicKey) {
943
+ if (circl == null || !publicKey || typeof publicKey.length !== 'number') {
944
+ return null;
945
+ }
946
+ const len = publicKey.byteLength !== undefined ? publicKey.byteLength : publicKey.length;
947
+ const hybridNs = circl.hybridedmldsaslhdsa;
948
+ const hybrid5Ns = circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5;
949
+ if (hybridNs && typeof hybridNs.PublicKeySize === 'number' && len === hybridNs.PublicKeySize) {
950
+ return KEY_TYPE_HYBRIDEDMLDSASLHDSA;
951
+ }
952
+ if (hybrid5Ns && typeof hybrid5Ns.PublicKeySize === 'number' && len === hybrid5Ns.PublicKeySize) {
953
+ return KEY_TYPE_HYBRIDEDMLDSASLHDSA5;
954
+ }
955
+ return null;
956
+ }
957
+
958
+ /**
959
+ * Convert key (number[] or Uint8Array) to Uint8Array for CIRCL.
960
+ * @param {number[]|Uint8Array} key - Key bytes.
961
+ * @returns {Uint8Array}
962
+ */
963
+ function toUint8Array(key) {
964
+ if (key instanceof Uint8Array) return key;
965
+ return new Uint8Array(key);
966
+ }
967
+
968
+ /**
969
+ * The newWallet function creates a new Wallet.
970
+ * @function newWallet
971
+ * @param {number|null} keyType - Optional. KEY_TYPE_HYBRIDEDMLDSASLHDSA (3) or KEY_TYPE_HYBRIDEDMLDSASLHDSA5 (5). null/undefined defaults to 3.
972
+ * @return {Wallet|number} Returns a Wallet object, or -1000 (not initialized), -1001 (invalid key type), -1002 (crypto failure).
973
+ */
974
+ function newWallet(keyType) {
975
+ if (isInitialized === false) {
976
+ return -1000;
977
+ }
978
+ if (circl == null) {
979
+ return CIRCL_CRYPTO_FAILURE;
980
+ }
981
+ if (keyType === null || keyType === undefined) {
982
+ keyType = KEY_TYPE_HYBRIDEDMLDSASLHDSA;
983
+ }
984
+ const hybridNs = circl.hybridedmldsaslhdsa;
985
+ const hybrid5Ns = circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5;
986
+ let res;
987
+ if (keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA && hybridNs) {
988
+ res = hybridNs.generateKey();
989
+ } else if (keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA5 && hybrid5Ns) {
990
+ res = hybrid5Ns.generateKey();
991
+ } else {
992
+ return INVALID_KEY_TYPE;
993
+ }
994
+ if (res && res.error) {
995
+ return CIRCL_CRYPTO_FAILURE;
996
+ }
997
+ if (!res || !res.result || !res.result.publicKey || !res.result.privateKey) {
998
+ return CIRCL_CRYPTO_FAILURE;
999
+ }
1000
+ const publicKey = res.result.publicKey instanceof Uint8Array ? Array.from(res.result.publicKey) : res.result.publicKey;
1001
+ const privateKey = res.result.privateKey instanceof Uint8Array ? Array.from(res.result.privateKey) : res.result.privateKey;
1002
+ const address = PublicKeyToAddress(publicKey);
1003
+ return new Wallet(address, privateKey, publicKey);
1004
+ }
1005
+
1006
+ /**
1007
+ * The newWalletSeed function creates a new Wallet seed word list. The return array can then be passed to the openWalletFromSeedWords function to create a new wallet.
1008
+ *
1009
+ * @function newWalletSeed
1010
+ * @param {number|null} keyType - Optional. KEY_TYPE_HYBRIDEDMLDSASLHDSA (3) or KEY_TYPE_HYBRIDEDMLDSASLHDSA5 (5). null/undefined defaults to 3.
1011
+ * @return {array|number|null} Returns an array of seed words (32 or 36 words). Returns -1000 if not initialized, null on failure.
1012
+ */
1013
+ function newWalletSeed(keyType) {
1014
+ if (isInitialized === false) {
1015
+ return -1000;
1016
+ }
1017
+ if (circl == null || !circl.cryptoRandom) {
1018
+ return null;
1019
+ }
1020
+ if (keyType === null || keyType === undefined) {
1021
+ keyType = KEY_TYPE_HYBRIDEDMLDSASLHDSA;
1022
+ }
1023
+ if (keyType !== KEY_TYPE_HYBRIDEDMLDSASLHDSA && keyType !== KEY_TYPE_HYBRIDEDMLDSASLHDSA5) {
1024
+ return null;
1025
+ }
1026
+ const baseSeedLen = keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA5 ? BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA5 : BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA;
1027
+ const res = circl.cryptoRandom(baseSeedLen);
1028
+ if (res && res.error) {
1029
+ return null;
1030
+ }
1031
+ if (!res || !res.result || res.result.length !== baseSeedLen) {
1032
+ return null;
1033
+ }
1034
+ const seedArray = res.result instanceof Uint8Array ? res.result : new Uint8Array(res.result);
1035
+ const wordList = seedwords.getWordListFromSeedArray(seedArray);
1036
+ const expectedLen = keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA5 ? SEED_WORD_LIST_LENGTH_HYBRIDEDMLDSASLHDSA5 : SEED_WORD_LIST_LENGTH_HYBRIDEDMLDSASLHDSA;
1037
+ if (wordList == null || wordList.length !== expectedLen) {
1038
+ return null;
1039
+ }
1040
+ return wordList;
1041
+ }
1042
+
1043
+ /**
1044
+ * The openWalletFromSeed function creates a wallet from a raw seed byte array.
1045
+ * Determines the key scheme from the array length: 96 bytes (hybrideds), 72 bytes (hybrid5), or 64 bytes (hybrid).
1046
+ *
1047
+ * @function openWalletFromSeed
1048
+ * @param {Array<number>|Uint8Array} seedArray - The raw seed bytes. Length 96, 72, or 64 depending on scheme.
1049
+ * @return {Wallet|number} Returns a Wallet object. Returns -1000 if not initialized, null if the operation failed.
1050
+ */
1051
+ function openWalletFromSeed(seedArray) {
1052
+ if (isInitialized === false) {
1053
+ return -1000;
1054
+ }
1055
+ if (seedArray == null || typeof seedArray.length !== 'number') {
1056
+ return null;
1057
+ }
1058
+ const len = seedArray.length;
1059
+ if (len !== BASE_SEED_BYTES_HYBRIDEDS && len !== BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA5 && len !== BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA) {
1060
+ return null;
1061
+ }
1062
+ if (circl == null) {
1063
+ return null;
1064
+ }
1065
+ let expandedRes;
1066
+ let keyPairRes;
1067
+ const seedU8 = seedArray instanceof Uint8Array ? seedArray : new Uint8Array(seedArray);
1068
+ if (len === BASE_SEED_BYTES_HYBRIDEDS) {
1069
+ const ns = circl.hybrideds;
1070
+ if (!ns) return null;
1071
+ expandedRes = ns.expandSeed(seedU8);
1072
+ if (expandedRes && expandedRes.error) return null;
1073
+ if (!expandedRes || !expandedRes.result) return null;
1074
+ keyPairRes = ns.newKeyFromSeed(expandedRes.result);
1075
+ } else if (len === BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA5) {
1076
+ const ns = circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5;
1077
+ if (!ns) return null;
1078
+ expandedRes = ns.expandSeed(seedU8);
1079
+ if (expandedRes && expandedRes.error) return null;
1080
+ if (!expandedRes || !expandedRes.result) return null;
1081
+ keyPairRes = ns.newKeyFromSeed(expandedRes.result);
1082
+ } else {
1083
+ const ns = circl.hybridedmldsaslhdsa;
1084
+ if (!ns) return null;
1085
+ expandedRes = ns.expandSeed(seedU8);
1086
+ if (expandedRes && expandedRes.error) return null;
1087
+ if (!expandedRes || !expandedRes.result) return null;
1088
+ keyPairRes = ns.newKeyFromSeed(expandedRes.result);
1089
+ }
1090
+ if (keyPairRes && keyPairRes.error) return null;
1091
+ if (!keyPairRes || !keyPairRes.result || !keyPairRes.result.publicKey || !keyPairRes.result.privateKey) return null;
1092
+ const publicKey = keyPairRes.result.publicKey instanceof Uint8Array ? Array.from(keyPairRes.result.publicKey) : keyPairRes.result.publicKey;
1093
+ const privateKey = keyPairRes.result.privateKey instanceof Uint8Array ? Array.from(keyPairRes.result.privateKey) : keyPairRes.result.privateKey;
1094
+ const address = PublicKeyToAddress(publicKey);
1095
+ return new Wallet(address, privateKey, publicKey);
1096
+ }
1097
+
1098
+ /**
1099
+ * The openWalletFromSeedWords function creates a wallet from a seed word list. The seed word list is available for wallets created from Desktop/Web/Mobile wallets.
1100
+ * Supports 48 words (hybrideds), 36 words (hybrid5), or 32 words (hybrid) per seed length.
1101
+ *
1102
+ * @function openWalletFromSeedWords
1103
+ * @param {array} seedWordList - An array of seed words. Length 48, 36, or 32 depending on scheme.
1104
+ * @return {Wallet|number} Returns a Wallet object. Returns -1000 if not initialized, null if the operation failed.
1105
+ */
1106
+ function openWalletFromSeedWords(seedWordList) {
1107
+ if (isInitialized === false) {
1108
+ return -1000;
1109
+ }
1110
+ if (seedWordList == null || typeof seedWordList.length !== 'number') {
1111
+ return null;
1112
+ }
1113
+ const len = seedWordList.length;
1114
+ if (len !== SEED_WORD_LIST_LENGTH_HYBRIDEDS && len !== SEED_WORD_LIST_LENGTH_HYBRIDEDMLDSASLHDSA5 && len !== SEED_WORD_LIST_LENGTH_HYBRIDEDMLDSASLHDSA) {
1115
+ return null;
1116
+ }
1117
+ const seedArray = seedwords.getSeedArrayFromWordList(seedWordList);
1118
+ if (seedArray == null || seedArray.length == null) {
1119
+ return null;
1120
+ }
1121
+ return openWalletFromSeed(seedArray);
1122
+ }
1123
+
1124
+ /**
1125
+ * The deserializeEncryptedWallet function opens a wallet backed-up using an application such as the Desktop/Mobile/CLI/Web wallet. This function can take upto a minute or so to execute. You should open wallets only from trusted sources.
1126
+ *
1127
+ * @function deserializeEncryptedWallet
1128
+ * @param {string} walletJsonString - The json string from a wallet file.
1129
+ * @param {string} passphrase - The passphrase used to encrypt the wallet.
1130
+ * @return {Wallet} Returns a Wallet object. Returns null if opening the wallet fails.
1131
+ */
1132
+ function deserializeEncryptedWallet(walletJsonString, passphrase) {
1133
+ if (isInitialized === false) {
1134
+ console.error('deserializeEncryptedWallet: SDK not initialized');
1135
+ return -1000;
1136
+ }
1137
+
1138
+ if (walletJsonString == null || passphrase == null) {
1139
+ console.error('deserializeEncryptedWallet: walletJsonString or passphrase is null');
1140
+ return null;
1141
+ }
1142
+
1143
+ if (typeof walletJsonString === 'string' || walletJsonString instanceof String) {
1144
+
1145
+ } else {
1146
+ console.error('deserializeEncryptedWallet: walletJsonString is not a string');
1147
+ return null;
1148
+ }
1149
+
1150
+ if (typeof passphrase === 'string' || passphrase instanceof String) {
1151
+
1152
+ } else {
1153
+ console.error('deserializeEncryptedWallet: passphrase is not a string');
1154
+ return null;
1155
+ }
1156
+
1157
+ let walletJsonObj;
1158
+ try {
1159
+ walletJsonObj = JSON.parse(walletJsonString);
1160
+ } catch (e) {
1161
+ console.error('deserializeEncryptedWallet: JSON parse failed', e.message);
1162
+ return null;
1163
+ }
1164
+
1165
+ if (walletJsonObj == null) {
1166
+ console.error('deserializeEncryptedWallet: parsed JSON is null');
1167
+ return null;
1168
+ }
1169
+
1170
+ if (walletJsonObj.address == null) {
1171
+ console.error('deserializeEncryptedWallet: wallet JSON missing address');
1172
+ return null;
1173
+ }
1174
+
1175
+ let keyPairString = JsonToWalletKeyPair(walletJsonString, passphrase);
1176
+ if (keyPairString == null) {
1177
+ console.error('deserializeEncryptedWallet: failed to derive key pair (wrong passphrase or invalid crypto)');
1178
+ return null;
1179
+ }
1180
+
1181
+ let keyPairSplit = keyPairString.split(",");
1182
+ if (keyPairSplit.length < 2) {
1183
+ console.error('deserializeEncryptedWallet: key pair format invalid');
1184
+ return null;
1185
+ }
1186
+
1187
+ let privateKeyArray = base64ToBytes(keyPairSplit[0]);
1188
+ let publicKeyArray = base64ToBytes(keyPairSplit[1]);
1189
+ let address = PublicKeyToAddress(publicKeyArray);
1190
+ if (address == null) {
1191
+ console.error('deserializeEncryptedWallet: failed to derive address from public key');
1192
+ return null;
1193
+ }
1194
+
1195
+ if (typeof address === 'string' || address instanceof String) {
1196
+
1197
+ } else {
1198
+ console.error('deserializeEncryptedWallet: address is not a string');
1199
+ return null;
1200
+ }
1201
+
1202
+ let addressCheck = "0x" + walletJsonObj.address.toLowerCase();
1203
+ if (addressCheck != address.toLowerCase()) {
1204
+ console.error('deserializeEncryptedWallet: address mismatch with wallet JSON');
1205
+ return null;
1206
+ }
1207
+
1208
+ let wallet = new Wallet(address, privateKeyArray, publicKeyArray);
1209
+
1210
+ return wallet;
1211
+ }
1212
+
1213
+ /**
1214
+ * The serializeEncryptedWallet function encrypts and serializes a Wallet object to a JSON string readable by the Desktop/Mobile/Web/CLI wallet applications. You can save this string to a file and open the file in one of these wallet applications. You may also open this string using the deserializeEncryptedWallet function. If you loose the passphrase, you will be unable to open the wallet. This function can take upto a minute or so to execute.
1215
+ *
1216
+ * @function serializeEncryptedWallet
1217
+ * @param {Wallet} wallet - A Wallet object representing the wallet to serialize.
1218
+ * @param {string} passphrase - A passphrase used to encrypt the wallet. It should atleast be 12 characters long.
1219
+ * @return {string} Returns the Wallet in JSON string format. If the wallet is invalid, null is returned.
1220
+ */
1221
+ function serializeEncryptedWallet(wallet, passphrase) {
1222
+ if(verifyWallet(wallet) === false) {
1223
+ return null;
1224
+ }
1225
+
1226
+ if (passphrase == null) {
1227
+ return null;
1228
+ }
1229
+
1230
+ if (typeof passphrase === 'string' || passphrase instanceof String) {
1231
+
1232
+ } else {
1233
+ return null;
1234
+ }
1235
+
1236
+ if (passphrase.length < MIN_PASSPHRASE_LENGTH) {
1237
+ return null;
1238
+ }
1239
+
1240
+ let walletJsonString = KeyPairToWalletJson(wallet.privateKey, wallet.publicKey, passphrase);
1241
+
1242
+ let walletJson = JSON.parse(walletJsonString);
1243
+ let addressCheck = "0x" + walletJson.address;
1244
+ if (addressCheck.toLowerCase() != wallet.address.toLowerCase()) {
1245
+ return null;
1246
+ }
1247
+
1248
+ return walletJsonString;
1249
+ }
1250
+
1251
+ /**
1252
+ * The serializeSeedAsEncryptedWallet function encrypts a raw seed byte array into a wallet JSON string
1253
+ * that can be opened with deserializeEncryptedWallet or Desktop/Mobile/Web/CLI wallet applications.
1254
+ * The seed is stored in its pre-expansion form (version 5 wallet format). This function can take
1255
+ * up to a minute or so to execute due to key derivation.
1256
+ *
1257
+ * @function serializeSeedAsEncryptedWallet
1258
+ * @param {Array<number>|Uint8Array} seedArray - The raw seed bytes. Length must be 96, 72, or 64 depending on scheme.
1259
+ * @param {string} passphrase - A passphrase used to encrypt the wallet. Must be at least 12 characters long.
1260
+ * @return {string|number|null} Returns the encrypted wallet JSON string. Returns -1000 if not initialized, null if the operation failed.
1261
+ */
1262
+ function serializeSeedAsEncryptedWallet(seedArray, passphrase) {
1263
+ if (isInitialized === false) {
1264
+ return -1000;
1265
+ }
1266
+ if (seedArray == null || typeof seedArray.length !== 'number') {
1267
+ return null;
1268
+ }
1269
+ const len = seedArray.length;
1270
+ if (len !== BASE_SEED_BYTES_HYBRIDEDS && len !== BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA5 && len !== BASE_SEED_BYTES_HYBRIDEDMLDSASLHDSA) {
1271
+ return null;
1272
+ }
1273
+ if (passphrase == null) {
1274
+ return null;
1275
+ }
1276
+ if (typeof passphrase !== 'string' && !(passphrase instanceof String)) {
1277
+ return null;
1278
+ }
1279
+ if (passphrase.length < MIN_PASSPHRASE_LENGTH) {
1280
+ return null;
1281
+ }
1282
+ const seedU8 = seedArray instanceof Uint8Array ? seedArray : new Uint8Array(seedArray);
1283
+ const result = EncryptPreExpansionSeed(seedU8, passphrase);
1284
+ if (result == null || result instanceof Error) {
1285
+ return null;
1286
+ }
1287
+ return result;
1288
+ }
1289
+
1290
+ function base64ToBytes(base64) {
1291
+ const binString = atob(base64);
1292
+ return Uint8Array.from(binString, (m) => m.codePointAt(0));
1293
+ }
1294
+
1295
+ function bytesToBase64(bytes) {
1296
+ const binString = Array.from(bytes, (byte) =>
1297
+ String.fromCodePoint(byte),
1298
+ ).join("");
1299
+ return btoa(binString);
1300
+ }
1301
+
1302
+ function getRandomRequestId() {
1303
+ return crypto.randomBytes(20).toString('hex');
1304
+ }
1305
+
1306
+ function isByteArray(array) {
1307
+ if (!array) return false;
1308
+ if (array.byteLength !== undefined) return true;
1309
+ if (typeof array.length === 'number' && array.length >= 0) return true;
1310
+ return false;
1311
+ }
1312
+
1313
+ /**
1314
+ * The verifyWallet function verifies whether a Wallet is valid or not. To mitigate spoofing and other attachs, it is highly recommended to verify a wallet, especially if it is from an untrusted source.
1315
+ *
1316
+ * @function verifyWallet
1317
+ * @param {Wallet} wallet - A Wallet object representing the wallet to verify.
1318
+ * @return {boolean} Returns true if the Wallet verification succeeded, else returns false.
1319
+ */
1320
+ function verifyWallet(wallet) {
1321
+ if (isInitialized === false) {
1322
+ console.log('[verifyWallet] FAIL: not initialized');
1323
+ return -1000;
1324
+ }
1325
+ if (wallet === null || wallet.address === null || wallet.privateKey === null || wallet.publicKey === null) {
1326
+ console.log('[verifyWallet] FAIL: wallet or wallet.address/privateKey/publicKey is null');
1327
+ return false;
1328
+ }
1329
+ if (isAddressValid(wallet.address) === false) {
1330
+ console.log('[verifyWallet] FAIL: isAddressValid(wallet.address) === false');
1331
+ return false;
1332
+ }
1333
+ if (isByteArray(wallet.privateKey) === false) {
1334
+ console.log('[verifyWallet] FAIL: isByteArray(wallet.privateKey) === false');
1335
+ return false;
1336
+ }
1337
+ if (isByteArray(wallet.publicKey) === false) {
1338
+ console.log('[verifyWallet] FAIL: isByteArray(wallet.publicKey) === false');
1339
+ return false;
1340
+ }
1341
+ const keyType = getKeyTypeFromPrivateKey(wallet.privateKey);
1342
+ if (keyType == null) {
1343
+ console.log('[verifyWallet] FAIL: getKeyTypeFromPrivateKey returned null (privateKey.length=' + wallet.privateKey.length + ', circl.hybridedmldsaslhdsa.PrivateKeySize=' + (circl && circl.hybridedmldsaslhdsa ? circl.hybridedmldsaslhdsa.PrivateKeySize : 'N/A') + ', hybrid5.PrivateKeySize=' + (circl && (circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5) ? (circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5).PrivateKeySize : 'N/A') + ')');
1344
+ return false;
1345
+ }
1346
+ const hybridNs = circl && circl.hybridedmldsaslhdsa;
1347
+ const hybrid5Ns = circl && (circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5);
1348
+ if (keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA && hybridNs && wallet.privateKey.length !== hybridNs.PrivateKeySize) {
1349
+ console.log('[verifyWallet] FAIL: keyType 3 but privateKey.length !== hybridNs.PrivateKeySize');
1350
+ return false;
1351
+ }
1352
+ if (keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA5 && hybrid5Ns && wallet.privateKey.length !== hybrid5Ns.PrivateKeySize) {
1353
+ console.log('[verifyWallet] FAIL: keyType 5 but privateKey.length !== hybrid5Ns.PrivateKeySize');
1354
+ return false;
1355
+ }
1356
+ const address = PublicKeyToAddress(wallet.publicKey);
1357
+ if (address !== wallet.address) {
1358
+ console.log('[verifyWallet] FAIL: PublicKeyToAddress(wallet.publicKey) !== wallet.address');
1359
+ return false;
1360
+ }
1361
+ const message = new TextEncoder().encode("verifyverifyverifyverifyverifyok");
1362
+ const privU8 = toUint8Array(wallet.privateKey);
1363
+ const pubU8 = toUint8Array(wallet.publicKey);
1364
+ let sigRes;
1365
+ let verRes;
1366
+ if (keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA && hybridNs) {
1367
+ sigRes = hybridNs.sign(privU8, message);
1368
+ if (sigRes && sigRes.error) {
1369
+ console.log('[verifyWallet] FAIL: hybridNs.sign returned error:', sigRes.error);
1370
+ return false;
1371
+ }
1372
+ verRes = hybridNs.verify(pubU8, message, sigRes.result);
1373
+ } else if (keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA5 && hybrid5Ns) {
1374
+ sigRes = hybrid5Ns.sign(privU8, message);
1375
+ if (sigRes && sigRes.error) {
1376
+ console.log('[verifyWallet] FAIL: hybrid5Ns.sign returned error:', sigRes.error);
1377
+ return false;
1378
+ }
1379
+ verRes = hybrid5Ns.verify(pubU8, message, sigRes.result);
1380
+ } else {
1381
+ console.log('[verifyWallet] FAIL: no CIRCL namespace for keyType (keyType=' + keyType + ', hybridNs=' + !!hybridNs + ', hybrid5Ns=' + !!hybrid5Ns + ')');
1382
+ return false;
1383
+ }
1384
+ if (verRes && verRes.error) {
1385
+ console.log('[verifyWallet] FAIL: verify returned error:', verRes.error);
1386
+ return false;
1387
+ }
1388
+ if (!(verRes && verRes.result === true)) {
1389
+ console.log('[verifyWallet] FAIL: verRes.result !== true (verRes=', verRes, ')');
1390
+ return false;
1391
+ }
1392
+ return true;
1393
+ }
1394
+
1395
+ /**
1396
+ * The serializeWallet function serializes a Wallet object to a JSON string. You should encrypt the string before saving it to disk or a database.
1397
+ *
1398
+ * @function serializeWallet
1399
+ * @param {Wallet} wallet - A Wallet object representing the wallet to serialize.
1400
+ * @return {string} Returns the Wallet in JSON string format. If the wallet is invalid, null is returned.
1401
+ */
1402
+ function serializeWallet(wallet) {
1403
+ if (isInitialized === false) {
1404
+ return -1000;
1405
+ }
1406
+
1407
+ if(verifyWallet(wallet) === false) {
1408
+ return null;
1409
+ }
1410
+
1411
+ var walletJson = {
1412
+ "address": wallet.address,
1413
+ "privateKey": bytesToBase64(wallet.privateKey),
1414
+ "publicKey": bytesToBase64(wallet.publicKey),
1415
+ }
1416
+
1417
+ return JSON.stringify(walletJson);
1418
+ }
1419
+
1420
+ /**
1421
+ * The deserializeWallet function creates a Wallet object from a JSON string.
1422
+ *
1423
+ * @function deserializeWallet
1424
+ * @param {string} walletJson - A Wallet object representing the wallet to deserialize.
1425
+ * @return {Wallet} Returns the Wallet corresponding to the walletJson. If the wallet is invalid, null is returned.
1426
+ */
1427
+ function deserializeWallet(walletJson) {
1428
+ if (isInitialized === false) {
1429
+ return -1000;
1430
+ }
1431
+
1432
+ var tempWallet = JSON.parse(walletJson);
1433
+
1434
+ var wallet = new Wallet(tempWallet.address, base64ToBytes(tempWallet.privateKey), base64ToBytes(tempWallet.publicKey));
1435
+
1436
+ if (verifyWallet(wallet) == false) {
1437
+ return null;
1438
+ }
1439
+
1440
+ return wallet;
1441
+ }
1442
+
1443
+ function transactionGetSigningHash(fromaddress, nonce, toaddress, amount, gas, chainid, data) {
1444
+ let messageData = TxnSigningHash(fromaddress, nonce, toaddress, amount, gas, chainid, data);
1445
+ var messageBytes = [];
1446
+ for (var i = 0; i < messageData.length; ++i) {
1447
+ messageBytes.push(messageData.charCodeAt(i));
1448
+ }
1449
+ return messageBytes;
1450
+ }
1451
+
1452
+ function transactionGetTransactionHash(fromaddress, nonce, toaddress, amount, gas, chainid, data, pkkey, sig) {
1453
+ const arrayPkDataToPass = pkkey.toString().split(",");
1454
+ const typedPkArray = new Uint8Array(arrayPkDataToPass.length);
1455
+ for (let i = 0; i < arrayPkDataToPass.length; i++) {
1456
+ typedPkArray[i] = arrayPkDataToPass[i];
1457
+ }
1458
+ const arraySigDataToPass = sig.toString().split(",");
1459
+ const typedSigArray = new Uint8Array(arraySigDataToPass.length);
1460
+ for (let i = 0; i < arraySigDataToPass.length; i++) {
1461
+ typedSigArray[i] = arraySigDataToPass[i];
1462
+ }
1463
+ var txnHash = TxnHash(fromaddress, nonce, toaddress, amount, gas, chainid, data, typedPkArray, typedSigArray)
1464
+ return txnHash;
1465
+ }
1466
+
1467
+ function transactionGetData(fromaddress, nonce, toaddress, amount, gas, chainid, data, pkkey, sig) {
1468
+ const arrayPkDataToPass = pkkey.toString().split(",");
1469
+ const typedPkArray = new Uint8Array(arrayPkDataToPass.length);
1470
+ for (let i = 0; i < arrayPkDataToPass.length; i++) {
1471
+ typedPkArray[i] = arrayPkDataToPass[i];
1472
+ }
1473
+ const arraySigDataToPass = sig.toString().split(",");
1474
+ const typedSigArray = new Uint8Array(arraySigDataToPass.length);
1475
+ for (let i = 0; i < arraySigDataToPass.length; i++) {
1476
+ typedSigArray[i] = arraySigDataToPass[i];
1477
+ }
1478
+ var txnData = TxnData(fromaddress, nonce, toaddress, amount, gas, chainid, data, typedPkArray, typedSigArray)
1479
+ return txnData;
1480
+ }
1481
+
1482
+ function transactionGetSigningHash2(fromaddress, nonce, toaddress, valueInWeiHex, gas, chainid, data, remarks, signingContext) {
1483
+ let messageDataReturn = TxnSigningHash2(fromaddress, nonce, toaddress, valueInWeiHex, gas, chainid, data, remarks, signingContext);
1484
+ if (messageDataReturn && messageDataReturn.error) {
1485
+ console.log('[transactionGetSigningHash2] FAIL: messageDataReturn returned error:', messageDataReturn.error);
1486
+ return null;
1487
+ }
1488
+ var messageData = messageDataReturn.result;
1489
+ var messageBytes = [];
1490
+ for (var i = 0; i < messageData.length; ++i) {
1491
+ messageBytes.push(messageData.charCodeAt(i));
1492
+ }
1493
+ return messageBytes;
1494
+ }
1495
+
1496
+ function transactionGetTransactionHash2(fromaddress, nonce, toaddress, valueInWeiHex, gas, chainid, data, remarks, signingContext, pkkey, sig) {
1497
+ const arrayPkDataToPass = pkkey.toString().split(",");
1498
+ const typedPkArray = new Uint8Array(arrayPkDataToPass.length);
1499
+ for (let i = 0; i < arrayPkDataToPass.length; i++) {
1500
+ typedPkArray[i] = arrayPkDataToPass[i];
1501
+ }
1502
+ const arraySigDataToPass = sig.toString().split(",");
1503
+ const typedSigArray = new Uint8Array(arraySigDataToPass.length);
1504
+ for (let i = 0; i < arraySigDataToPass.length; i++) {
1505
+ typedSigArray[i] = arraySigDataToPass[i];
1506
+ }
1507
+ var txnHashReturn = TxnHash2(fromaddress, nonce, toaddress, valueInWeiHex, gas, chainid, data, remarks, signingContext, typedPkArray, typedSigArray)
1508
+ if (txnHashReturn && txnHashReturn.error) {
1509
+ console.log('[transactionGetTransactionHash2] FAIL: TxnHash2 returned error:', txnHashReturn.error);
1510
+ return null;
1511
+ }
1512
+ return txnHashReturn.result;
1513
+ }
1514
+
1515
+ function transactionGetData2(fromaddress, nonce, toaddress, valueInWeiHex, gas, chainid, data, remarks, signingContext, pkkey, sig) {
1516
+ const arrayPkDataToPass = pkkey.toString().split(",");
1517
+ const typedPkArray = new Uint8Array(arrayPkDataToPass.length);
1518
+ for (let i = 0; i < arrayPkDataToPass.length; i++) {
1519
+ typedPkArray[i] = arrayPkDataToPass[i];
1520
+ }
1521
+ const arraySigDataToPass = sig.toString().split(",");
1522
+ const typedSigArray = new Uint8Array(arraySigDataToPass.length);
1523
+ for (let i = 0; i < arraySigDataToPass.length; i++) {
1524
+ typedSigArray[i] = arraySigDataToPass[i];
1525
+ }
1526
+ var txnDataReturn = TxnData2(fromaddress, nonce, toaddress, valueInWeiHex, gas, chainid, data, remarks, signingContext, typedPkArray, typedSigArray)
1527
+ if (txnDataReturn && txnDataReturn.error) {
1528
+ console.log('[transactionGetData2] FAIL: TxnData2 returned error:', txnDataReturn.error);
1529
+ return null;
1530
+ }
1531
+ return txnDataReturn.result;
1532
+ }
1533
+
1534
+ /**
1535
+ * The postTransaction function posts a signed transaction to the blockchain.
1536
+ * This method can be used in conjunction with the signSendCoinTransaction method to submit a transaction that was signed using a cold wallet (offline or disconnected or air-gapped wallet).
1537
+ *
1538
+ * @async
1539
+ * @function postTransaction
1540
+ * @param {string} txnData - A signed transaction string returned by the signSendCoinTransaction function.
1541
+ * @return {Promise<SendResult>} Returns a promise of type SendResult. txnHash will be null in SendResult.
1542
+ */
1543
+ async function postTransaction(txnData) {
1544
+ if (isInitialized === false) {
1545
+ return -1000;
1546
+ }
1547
+
1548
+ if (txnData == null) {
1549
+ return new SendResult(-600, null, null, null);
1550
+ }
1551
+ var url = config.writeUrl + "/transactions";
1552
+
1553
+ let requestId = getRandomRequestId();
1554
+
1555
+ let reqHeaders = new Headers({
1556
+ "Content-Type": "application/json"
1557
+ });
1558
+ reqHeaders.append(REQUEST_ID_HEADER_NAME, requestId);
1559
+ if (config.writeApiKey !== null) {
1560
+ reqHeaders.append(API_KEY_HEADER_NAME, config.writeApiKey); }
1561
+
1562
+
1563
+ let txnDataJson = JSON.stringify({ txnData: txnData });
1564
+
1565
+ try {
1566
+ const response = await fetch(url, {
1567
+ method: 'POST',
1568
+ headers: reqHeaders,
1569
+ body: txnDataJson
1570
+ });
1571
+
1572
+ if (response == null) {
1573
+ return new SendResult(-601, null, null, requestId);
1574
+ }
1575
+
1576
+ if (response.status == 200 || response.status == 204) {
1577
+ return new SendResult(0, null, response, requestId);
1578
+ }
1579
+
1580
+ return new SendResult(-602, null, response, requestId);
1581
+ } catch(error) {
1582
+ return new SendResult(-10000, null, null, requestId, error);
1583
+ }
1584
+ }
1585
+
1586
+ /**
1587
+ * The getLatestBlockDetails function returns details of the latest block of the blockchain.
1588
+ *
1589
+ * @async
1590
+ * @function getLatestBlockDetails
1591
+ * @return {Promise<LatestBlockDetailsResult>} Returns a promise of an object of type BlockDetailsResult.
1592
+ */
1593
+ async function getLatestBlockDetails() {
1594
+ if (isInitialized === false) {
1595
+ return -1000;
1596
+ }
1597
+
1598
+ let requestId = getRandomRequestId();
1599
+
1600
+ let reqHeaders = new Headers({
1601
+ "Content-Type": "application/json"
1602
+ });
1603
+ reqHeaders.append(REQUEST_ID_HEADER_NAME, requestId);
1604
+ if (config.readApiKey !== null) {
1605
+ reqHeaders.append(API_KEY_HEADER_NAME, config.readApiKey);
1606
+ }
1607
+
1608
+ var url = config.readUrl + "/latestblock";
1609
+
1610
+ try {
1611
+ const response = await fetch(url, {
1612
+ headers: reqHeaders,
1613
+ });
1614
+
1615
+ if (response == null) {
1616
+ return new LatestBlockDetailsResult(-100, null, response, requestId);
1617
+ }
1618
+ if (response.status !== 200) {
1619
+ return new LatestBlockDetailsResult(-101, null, response, requestId);
1620
+ }
1621
+
1622
+ const jsonObj = await response.json();
1623
+ if (jsonObj == null) {
1624
+ return new LatestBlockDetailsResult(-102, null, response, requestId);
1625
+ }
1626
+ const result = jsonObj.result;
1627
+
1628
+ if (result == null) {
1629
+ return new LatestBlockDetailsResult(-103, null, response, requestId);
1630
+ }
1631
+
1632
+ if (result.blockNumber == null) {
1633
+ return new LatestBlockDetailsResult(-104, null, response, requestId);
1634
+ }
1635
+
1636
+ let blockNumber = parseInt(result.blockNumber);
1637
+ if (Number.isInteger(blockNumber) == false) {
1638
+ return new LatestBlockDetailsResult(-105, null, response, requestId);
1639
+ }
1640
+
1641
+ var blockDetails = new BlockDetails(blockNumber);
1642
+ return new LatestBlockDetailsResult(0, blockDetails, response, requestId);
1643
+ } catch (error) {
1644
+ return new LatestBlockDetailsResult(-10000, null, null, requestId, error);
1645
+ }
1646
+ }
1647
+
1648
+ /**
1649
+ * The getAccountDetails function returns details of an account corresponding to the address.
1650
+ *
1651
+ * @async
1652
+ * @function getAccountDetails
1653
+ * @param {string} address - The address of the account of which the details have to be retrieved.
1654
+ * @return {Promise<AccountDetailsResult>} Returns a promise of type AccountDetailsResult.
1655
+ */
1656
+ async function getAccountDetails(address) {
1657
+ if (isInitialized === false) {
1658
+ return -1000;
1659
+ }
1660
+
1661
+ if (address == null) {
1662
+ return new AccountDetailsResult(-200, null, null, null);
1663
+ }
1664
+ if (isAddressValid(address) == false) {
1665
+ return new AccountDetailsResult(-201, null, null, null);
1666
+ }
1667
+
1668
+ let nonce = 0;
1669
+ let balance = "0";
1670
+ let requestId = getRandomRequestId();
1671
+
1672
+ let reqHeaders = new Headers({
1673
+ "Content-Type": "application/json"
1674
+ });
1675
+ reqHeaders.append(REQUEST_ID_HEADER_NAME, requestId);
1676
+ if(config.readApiKey !== null) {
1677
+ reqHeaders.append(API_KEY_HEADER_NAME, config.readApiKey);
1678
+ }
1679
+
1680
+ var url = config.readUrl + "/account/" + address;
1681
+
1682
+ try {
1683
+ const response = await fetch(url, {
1684
+ headers: reqHeaders,
1685
+ });
1686
+
1687
+ if(response == null) {
1688
+ return new AccountDetailsResult(-202, null, response, requestId);
1689
+ }
1690
+ if (response.status !== 200) {
1691
+ return new AccountDetailsResult(-203, null, response, requestId);
1692
+ }
1693
+
1694
+ const jsonObj = await response.json();
1695
+ if (jsonObj == null) {
1696
+ return new AccountDetailsResult(-204, null, response, requestId);
1697
+ }
1698
+ const result = jsonObj.result;
1699
+
1700
+ if (result == null) {
1701
+ return new AccountDetailsResult(-205, null, response, requestId);
1702
+ }
1703
+
1704
+ if(result.blockNumber == null) {
1705
+ return new AccountDetailsResult(-206, null, response, requestId);
1706
+ }
1707
+
1708
+ let blockNumber = parseInt(result.blockNumber);
1709
+ if (Number.isInteger(blockNumber) == false) {
1710
+ return new AccountDetailsResult(-207, null, response, requestId);
1711
+ }
1712
+
1713
+ if (result.nonce === null) {
1714
+ nonce = 0;
1715
+ } else {
1716
+ let tempNonce = parseInt(result.nonce);
1717
+ if (Number.isInteger(tempNonce) == true) {
1718
+ nonce = tempNonce;
1719
+ if (nonce < 0) {
1720
+ return new AccountDetailsResult(-208, null, response, requestId);
1721
+ }
1722
+ } else {
1723
+ return new AccountDetailsResult(-209, null, response, requestId);
1724
+ }
1725
+ }
1726
+
1727
+ if (result.balance != null) {
1728
+ if (isLargeNumber(result.balance) == false) {
1729
+ return new AccountDetailsResult(-210, null, response, requestId);
1730
+ } else {
1731
+ balance = result.balance;
1732
+ }
1733
+ }
1734
+
1735
+ var accountDetails = new AccountDetails(address, balance, nonce, blockNumber);
1736
+ return new AccountDetailsResult(0, accountDetails, response, requestId);
1737
+
1738
+ } catch (error) {
1739
+ return new AccountDetailsResult(0, null, null, requestId, error);
1740
+ }
1741
+ }
1742
+
1743
+ /**
1744
+ * The getTransactionDetails function returns details of a transaction posted to the blockchain.
1745
+ * Transactions may take a while to get registered in the blockchain. After a transaction is submitted, it may take a while before it is available for reading.
1746
+ * Some transactions that have lower balance than the minimum required for gas fees may be discarded.
1747
+ * In these cases, the transactions may not be returned when invoking the getTransactionDetails function.
1748
+ * You should consider the transaction as succeeded only if the status field of the transactionReceipt object is 0x1 (success).
1749
+ * The transactionReceipt field can be null unless the transaction is registered with the blockchain.
1750
+ * @async
1751
+ * @function getTransactionDetails
1752
+ * @param {string} txnHash - The hash of the transaction to retrieve.
1753
+ * @return {Promise<TransactionDetailsResult>} Returns a promise of type type TransactionDetailsResult.
1754
+ */
1755
+ async function getTransactionDetails(txnHash) {
1756
+ if (isInitialized === false) {
1757
+ return -1000;
1758
+ }
1759
+
1760
+ if (txnHash == null) {
1761
+ return new TransactionDetailsResult(-300, null, null, null);
1762
+ }
1763
+ if (isAddressValid(txnHash) == false) {
1764
+ return new TransactionDetailsResult(-301, null, null, null);
1765
+ }
1766
+
1767
+ let requestId = getRandomRequestId();
1768
+
1769
+ let reqHeaders = new Headers({
1770
+ "Content-Type": "application/json"
1771
+ });
1772
+ reqHeaders.append(REQUEST_ID_HEADER_NAME, requestId);
1773
+ if (config.readApiKey !== null) {
1774
+ reqHeaders.append(API_KEY_HEADER_NAME, config.readApiKey);
1775
+ }
1776
+
1777
+ var url = config.readUrl + "/transaction/" + txnHash;
1778
+
1779
+ try {
1780
+ const response = await fetch(url, {
1781
+ headers: reqHeaders,
1782
+ });
1783
+
1784
+ if (response == null) {
1785
+ return new TransactionDetailsResult(-302, null, response, requestId);
1786
+ }
1787
+ if (response.status !== 200) {
1788
+ return new TransactionDetailsResult(-303, null, response, requestId);
1789
+ }
1790
+
1791
+ const jsonObj = await response.json();
1792
+ if (jsonObj == null) {
1793
+ return new TransactionDetailsResult(-304, null, response, requestId);
1794
+ }
1795
+ const result = jsonObj.result;
1796
+
1797
+ if (result == null) {
1798
+ return new TransactionDetailsResult(-305, null, response, requestId);
1799
+ }
1800
+
1801
+ var transactionDetails = new TransactionDetails();
1802
+
1803
+ if (result.blockNumber !== null) {
1804
+ let tempBlockNumber = parseInt(result.blockNumber);
1805
+ if (Number.isInteger(tempBlockNumber) == true) {
1806
+ transactionDetails.blockNumber = tempBlockNumber;
1807
+ }
1808
+ if (tempBlockNumber < 0) {
1809
+ return new TransactionDetailsResult(-306, null, response, requestId);
1810
+ }
1811
+ }
1812
+
1813
+ transactionDetails.blockHash = result.blockHash;
1814
+ transactionDetails.from = result.from;
1815
+ transactionDetails.gas = result.gas;
1816
+ transactionDetails.gasPrice = result.gasPrice;
1817
+ transactionDetails.hash = result.hash;
1818
+ transactionDetails.input = result.input;
1819
+ transactionDetails.nonce = result.nonce;
1820
+ transactionDetails.to = result.to;
1821
+ transactionDetails.value = result.value;
1822
+
1823
+ if(result.receipt !== null) {
1824
+ transactionDetails.receipt = new TransactionReceipt();
1825
+ transactionDetails.receipt.cumulativeGasUsed = result.receipt.cumulativeGasUsed;
1826
+ transactionDetails.receipt.effectiveGasPrice = result.receipt.effectiveGasPrice;
1827
+ transactionDetails.receipt.gasUsed = result.receipt.gasUsed;
1828
+ transactionDetails.receipt.status = result.receipt.status;
1829
+ transactionDetails.receipt.hash = result.receipt.hash;
1830
+ transactionDetails.receipt.type = result.receipt.type;
1831
+ }
1832
+
1833
+ return new TransactionDetailsResult(0, transactionDetails, response, requestId);
1834
+ } catch (error) {
1835
+ return new TransactionDetailsResult(0, null, null, requestId, error);
1836
+ }
1837
+ }
1838
+
1839
+ /**
1840
+ * The listAccountTransactions function returns a list of transactions for a specific account.
1841
+ * Transactions may take a while to get registered in the blockchain. After a transaction is submitted, it may take a while before it is available for listing.
1842
+ * Some transactions that have lower balance than the minimum required for gas fees may be discarded.
1843
+ * In these cases, the transactions may not be returned when invoking the listAccountTransactions function.
1844
+ * You should consider the transaction as succeeded only if the status field AccountDetailsCompact object is 0x1 (success).
1845
+ * Both transactions from and transactions to the address will be returned in the list.
1846
+ * Use the getTransactionDetails function, passing the hash of the transaction to get detailed information about the transaction.
1847
+ * @async
1848
+ * @function listAccountTransactions
1849
+ * @param {string} address - The address for which the transactions have to be listed.
1850
+ * @param {number} pageNumber - The page number for which the transactions has to be listed for the account. Pass 0 to list the latest page. Pass 1 to list the oldest page. A maximum of 20 transactions are returned in each page. The response of this API includes a field that shows the pageCount (total number of pages available). You can pass any number between 1 to pageCount to get the corresponding page.
1851
+ * @return {Promise<ListAccountTransactionsResponse>} Returns a promise of type type ListAccountTransactionsResponse.
1852
+ */
1853
+ async function listAccountTransactions(address, pageNumber) {
1854
+ if (isInitialized === false) {
1855
+ return -1000;
1856
+ }
1857
+
1858
+ if (address === null) {
1859
+ return new AccountTransactionsResult(-700, null, null, null);
1860
+ }
1861
+ if (isAddressValid(address) === false) {
1862
+ return new AccountTransactionsResult(-701, null, null, null);
1863
+ }
1864
+
1865
+ if (pageNumber === null) {
1866
+ pageNumber = 0;
1867
+ } else if (Number.isInteger(pageNumber) === false) {
1868
+ return new AccountTransactionsResult(-702, null, null, null);
1869
+ }
1870
+
1871
+ let requestId = getRandomRequestId();
1872
+
1873
+ let reqHeaders = new Headers({
1874
+ "Content-Type": "application/json"
1875
+ });
1876
+ reqHeaders.append(REQUEST_ID_HEADER_NAME, requestId);
1877
+ if (config.readApiKey !== null) {
1878
+ reqHeaders.append(API_KEY_HEADER_NAME, config.readApiKey);
1879
+ }
1880
+
1881
+ var url = config.readUrl + "/account/" + address + "/transactions/" + pageNumber;
1882
+
1883
+ try {
1884
+ const response = await fetch(url, {
1885
+ headers: reqHeaders,
1886
+ });
1887
+
1888
+ if (response === null) {
1889
+ return new AccountTransactionsResult(-703, null, response, requestId);
1890
+ }
1891
+ if (response.status !== 200) {
1892
+ return new AccountTransactionsResult(-704, null, response, requestId);
1893
+ }
1894
+
1895
+ const jsonObj = await response.json();
1896
+
1897
+ if (jsonObj === null) {
1898
+ return new AccountTransactionsResult(-705, null, response, requestId);
1899
+ }
1900
+ const result = jsonObj;
1901
+
1902
+ if (result.pageCount === null) {
1903
+ return new AccountTransactionsResult(-706, null, response, requestId);
1904
+ }
1905
+
1906
+ var listAccountDetailsResponse = new ListAccountTransactionsResponse();
1907
+
1908
+ let tempPageCount = parseInt(result.pageCount);
1909
+ if (Number.isInteger(tempPageCount) === true) {
1910
+ listAccountDetailsResponse.pageCount = tempPageCount;
1911
+ }
1912
+
1913
+ if (result.items !== null) {
1914
+ if (Array.isArray(result.items) === false) {
1915
+ return new AccountTransactionsResult(-707, null, response, requestId);
1916
+ }
1917
+ listAccountDetailsResponse.items = new Array();
1918
+ for (const item of result.items) {
1919
+ let txn = new AccountTransactionCompact();
1920
+ txn.blockNumber = item.blockNumber;
1921
+ txn.from = item.from;
1922
+ txn.hash = item.hash;
1923
+ txn.to = item.to;
1924
+ txn.value = item.value;
1925
+ txn.status = item.status;
1926
+ listAccountDetailsResponse.items.push(txn);
1927
+ }
1928
+ }
1929
+
1930
+ return new AccountTransactionsResult(0, listAccountDetailsResponse, response, requestId);
1931
+ } catch (error) {
1932
+ return new AccountTransactionsResult(0, null, null, requestId, error);
1933
+ }
1934
+ }
1935
+
1936
+ /**
1937
+ * The signSendCoinTransaction function returns a signed transaction. The chainId used for signing should be provided in the initialize() function.
1938
+ * Since the gas fee for sending coins is fixed at 1000 coins, there is no option to set the gas fee explicitly.
1939
+ * This function is useful for offline (cold storage) wallets, where you can sign a transaction offline and then use the postTransaction function to post it on a connected device.
1940
+ * Another usecase for this function is when you want to first store a signed transaction to a database, then queue it and finally submit the transaction by calling the postTransaction function.
1941
+ *
1942
+ * @deprecated Use signRawTransaction instead.
1943
+ * @function signSendCoinTransaction
1944
+ * @param {Wallet} wallet - A Wallet object from which the transaction has to be sent. The address corresponding to the Wallet should have enough coins to cover gas fees as well. A minimum of 1000 coins (1000000000000000000000 wei) are required for gas fees.
1945
+ * @param {string} toAddress - The address to which the coins should be sent.
1946
+ * @param {string} coins - The string representing the number of coins (in ether) to send. To convert between ethers and wei, see https://docs.ethers.org/v4/api-utils.html#ether-strings-and-wei
1947
+ * @param {number} nonce - The nonce of the account retrieved by invoking the getAccountDetails function. You have to carefully manage state of the nonce to avoid sending the coins multiple times, such as when retrying sendCoins after a network error.
1948
+ * @return {SignResult} Returns a promise of type SignResult.
1949
+ */
1950
+ async function signSendCoinTransaction(wallet, toAddress, coins, nonce) {
1951
+ if (isInitialized === false) {
1952
+ return -1000;
1953
+ }
1954
+
1955
+ if (wallet == null || toAddress == null || coins == null || nonce == null) {
1956
+ return new SignResult(-500, null, null);
1957
+ }
1958
+
1959
+ if (isAddressValid(toAddress) == false) {
1960
+ return new SignResult(-501, null, null);
1961
+ }
1962
+
1963
+ if (verifyWallet(wallet) == false) {
1964
+ return new SignResult(-502, null, null);
1965
+ }
1966
+
1967
+ if (isLargeNumber(coins) == false) {
1968
+ return new SignResult(-503, null, null);
1969
+ }
1970
+
1971
+ let tempNonce = parseInt(nonce);
1972
+ if (Number.isInteger(tempNonce) == false) {
1973
+ return new SignResult(-504, null, null);
1974
+ }
1975
+ nonce = tempNonce;
1976
+ if (nonce < 0) {
1977
+ return new SignResult(-505, null, null);
1978
+ }
1979
+
1980
+ const contractData = null;
1981
+
1982
+ var txSigningHash = transactionGetSigningHash(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData);
1983
+ if (txSigningHash == null) {
1984
+ return new SignResult(-506, null, null);
1985
+ }
1986
+ const txSigningHashU8 = toUint8Array(txSigningHash);
1987
+ const hybridedsNs = circl && circl.hybrideds;
1988
+ if (!hybridedsNs) {
1989
+ return new SignResult(-507, null, null);
1990
+ }
1991
+ const sigRes = hybridedsNs.signCompact(toUint8Array(wallet.privateKey), txSigningHashU8);
1992
+ if (sigRes && sigRes.error) {
1993
+ return new SignResult(-507, null, null);
1994
+ }
1995
+ const verRes = hybridedsNs.verifyCompact(toUint8Array(wallet.publicKey), txSigningHashU8, sigRes.result);
1996
+ if (verRes && verRes.error) {
1997
+ return new SignResult(-507, null, null);
1998
+ }
1999
+ if (verRes.result !== true) {
2000
+ return new SignResult(-507, null, null);
2001
+ }
2002
+ const quantumSig = sigRes.result instanceof Uint8Array ? Array.from(sigRes.result) : sigRes.result;
2003
+
2004
+ var txHashHex = transactionGetTransactionHash(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData, wallet.publicKey, quantumSig);
2005
+ if (txHashHex == null) {
2006
+ return new SignResult(-508, null, null);
2007
+ }
2008
+
2009
+ var txnData = transactionGetData(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData, wallet.publicKey, quantumSig);
2010
+ if (txnData == null) {
2011
+ return new SignResult(-509, null, null);
2012
+ }
2013
+
2014
+ return new SignResult(0, txHashHex, txnData);
2015
+ }
2016
+
2017
+ /**
2018
+ * The signTransaction function returns a signed transaction. The chainId used for signing should be provided in the initialize() function.
2019
+ * Since the gas fee for sending coins is fixed at 1000 coins, there is no option to set the gas fee explicitly.
2020
+ * This function is useful for offline (cold storage) wallets, where you can sign a transaction offline and then use the postTransaction function to post it on a connected device.
2021
+ * Another usecase for this function is when you want to first store a signed transaction to a database, then queue it and finally submit the transaction by calling the postTransaction function.
2022
+ *
2023
+ * @deprecated Use signRawTransaction instead.
2024
+ * @function signTransaction
2025
+ * @param {Wallet} wallet - A Wallet object from which the transaction has to be sent. The address corresponding to the Wallet should have enough coins to cover gas fees as well. A minimum of 1000 coins (1000000000000000000000 wei) are required for gas fees.
2026
+ * @param {string} toAddress - The address to which the coins should be sent.
2027
+ * @param {string} coins - The string representing the number of coins (in ether) to send. To convert between ethers and wei, see https://docs.ethers.org/v4/api-utils.html#ether-strings-and-wei
2028
+ * @param {number} nonce - The nonce of the account retrieved by invoking the getAccountDetails function. You have to carefully manage state of the nonce to avoid sending the coins multiple times, such as when retrying sendCoins after a network error.
2029
+ * @param {number} data - The data to be signed.
2030
+ * @return {SignResult} Returns a promise of type SignResult.
2031
+ */
2032
+ async function signTransaction(wallet, toAddress, coins, nonce, data) {
2033
+ if (isInitialized === false) {
2034
+ return -1000;
2035
+ }
2036
+
2037
+ if (wallet == null || toAddress == null || coins == null || nonce == null) {
2038
+ return new SignResult(-500, null, null);
2039
+ }
2040
+
2041
+ if (isAddressValid(toAddress) == false) {
2042
+ return new SignResult(-501, null, null);
2043
+ }
2044
+
2045
+ if (verifyWallet(wallet) == false) {
2046
+ return new SignResult(-502, null, null);
2047
+ }
2048
+
2049
+ if (isLargeNumber(coins) == false) {
2050
+ return new SignResult(-503, null, null);
2051
+ }
2052
+
2053
+ let tempNonce = parseInt(nonce);
2054
+ if (Number.isInteger(tempNonce) == false) {
2055
+ return new SignResult(-504, null, null);
2056
+ }
2057
+ nonce = tempNonce;
2058
+ if (nonce < 0) {
2059
+ return new SignResult(-505, null, null);
2060
+ }
2061
+
2062
+ const contractData = null;
2063
+
2064
+ var txSigningHash = transactionGetSigningHash(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData);
2065
+ if (txSigningHash == null) {
2066
+ return new SignResult(-506, null, null);
2067
+ }
2068
+ const txSigningHashU8 = toUint8Array(txSigningHash);
2069
+ const hybridedsNs = circl && circl.hybrideds;
2070
+ if (!hybridedsNs) {
2071
+ return new SignResult(-507, null, null);
2072
+ }
2073
+ const sigRes = hybridedsNs.signCompact(toUint8Array(wallet.privateKey), txSigningHashU8);
2074
+ if (sigRes && sigRes.error) {
2075
+ return new SignResult(-507, null, null);
2076
+ }
2077
+ const verRes = hybridedsNs.verifyCompact(toUint8Array(wallet.publicKey), txSigningHashU8, sigRes.result);
2078
+ if (verRes && verRes.error) {
2079
+ return new SignResult(-507, null, null);
2080
+ }
2081
+ if (verRes.result !== true) {
2082
+ return new SignResult(-507, null, null);
2083
+ }
2084
+ const quantumSig = sigRes.result instanceof Uint8Array ? Array.from(sigRes.result) : sigRes.result;
2085
+
2086
+ var txHashHex = transactionGetTransactionHash(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData, wallet.publicKey, quantumSig);
2087
+ if (txHashHex == null) {
2088
+ return new SignResult(-508, null, null);
2089
+ }
2090
+
2091
+ var txnData = transactionGetData(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData, wallet.publicKey, quantumSig);
2092
+ if (txnData == null) {
2093
+ return new SignResult(-509, null, null);
2094
+ }
2095
+
2096
+ return new SignResult(0, txHashHex, txnData);
2097
+ }
2098
+
2099
+ /**
2100
+ * Helper function to convert a hex string to Uint8Array
2101
+ * @param {string} hex - Hex string with or without 0x prefix
2102
+ * @return {Uint8Array} Uint8Array representation of the hex string, or empty array if null/undefined
2103
+ */
2104
+ function hexStringToUint8Array(hex) {
2105
+ // Return empty array if null or undefined
2106
+ if (hex === null || hex === undefined) {
2107
+ return new Uint8Array(0);
2108
+ }
2109
+
2110
+ // Remove 0x prefix if present
2111
+ if (hex.startsWith('0x')) {
2112
+ hex = hex.slice(2);
2113
+ }
2114
+
2115
+ // Ensure even length (pad with leading zero if needed)
2116
+ if (hex.length % 2 !== 0) {
2117
+ hex = '0' + hex;
2118
+ }
2119
+
2120
+ // Convert hex string to Uint8Array
2121
+ const bytes = new Uint8Array(hex.length / 2);
2122
+ for (let i = 0; i < hex.length; i += 2) {
2123
+ bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
2124
+ }
2125
+
2126
+ return bytes;
2127
+ }
2128
+
2129
+ /**
2130
+ * The signRawTransaction function returns a signed transaction. The chainId used for signing can be provided in the TransactionSigningRequest, or if null, the chainId specified in the initialize() function will be used.
2131
+ * With this function, you can set the gasLimit explicitly compared to signTransaction.
2132
+ * You can also pass data to be signed, such as when creating or invoking a smart contract.
2133
+ * Since the gas fee is fixed at 1000 coins for 21000 units of gas, there is no option to set the gas fee explicitly.
2134
+ * This function is useful for offline (cold storage) wallets, where you can sign a transaction offline and then use the postTransaction function to post it on a connected device.
2135
+ * Another usecase for this function is when you want to first store a signed transaction to a database, then queue it and finally submit the transaction by calling the postTransaction function.
2136
+ *
2137
+ * @function signRawTransaction
2138
+ * @param {TransactionSigningRequest} transactionSigningRequest - An object of type TransactionSigningRequest with the transaction signing details.
2139
+ * @return {SignResult} Returns a promise of type SignResult.
2140
+ */
2141
+ function signRawTransaction(transactionSigningRequest) {
2142
+ if (isInitialized === false) {
2143
+ return -1000;
2144
+ }
2145
+
2146
+ if (transactionSigningRequest.wallet == null || transactionSigningRequest.nonce == null || transactionSigningRequest.gasLimit == null) {
2147
+ return new SignResult(-900, null, null);
2148
+ }
2149
+
2150
+ if (transactionSigningRequest.toAddress !== null && isAddressValid(transactionSigningRequest.toAddress) === false) {
2151
+ return new SignResult(-901, null, null);
2152
+ }
2153
+
2154
+ if (verifyWallet(transactionSigningRequest.wallet) === false) {
2155
+ return new SignResult(-902, null, null);
2156
+ }
2157
+
2158
+ // Convert valueInWei to hex if needed
2159
+ let valueInWeiHex = null;
2160
+ if (transactionSigningRequest.valueInWei !== null) {
2161
+ if (typeof transactionSigningRequest.valueInWei === 'bigint') {
2162
+ // Convert BigInt to hex string
2163
+ valueInWeiHex = '0x' + transactionSigningRequest.valueInWei.toString(16);
2164
+ } else if (typeof transactionSigningRequest.valueInWei === 'string') {
2165
+ if (transactionSigningRequest.valueInWei.startsWith('0x') === false) {
2166
+ return new SignResult(-903, null, null);
2167
+ }
2168
+ valueInWeiHex = transactionSigningRequest.valueInWei;
2169
+ } else {
2170
+ return new SignResult(-923, null, null);
2171
+ }
2172
+ }
2173
+
2174
+ let tempNonce = parseInt(transactionSigningRequest.nonce);
2175
+ if (Number.isInteger(tempNonce) === false) {
2176
+ return new SignResult(-904, null, null);
2177
+ }
2178
+ let nonce = tempNonce;
2179
+ if (nonce < 0) {
2180
+ return new SignResult(-905, null, null);
2181
+ }
2182
+
2183
+ let data = null;
2184
+ if (transactionSigningRequest.data !== null) {
2185
+ if (typeof transactionSigningRequest.data !== 'string' || transactionSigningRequest.data.startsWith('0x') === false) {
2186
+ return new SignResult(-906, null, null);
2187
+ }
2188
+ data = hexStringToUint8Array(transactionSigningRequest.data);
2189
+ }
2190
+
2191
+ let tempGasLimit = parseInt(transactionSigningRequest.gasLimit);
2192
+ if (Number.isInteger(tempGasLimit) === false) {
2193
+ return new SignResult(-907, null, null);
2194
+ }
2195
+ let gasLimit = tempGasLimit;
2196
+ if (gasLimit < 0) {
2197
+ return new SignResult(-908, null, null);
2198
+ }
2199
+
2200
+ let remarks = null;
2201
+ if (transactionSigningRequest.remarks !== null) {
2202
+ if (typeof transactionSigningRequest.remarks !== 'string' || transactionSigningRequest.remarks.startsWith('0x') === false) {
2203
+ return new SignResult(-909, null, null);
2204
+ }
2205
+ remarks = hexStringToUint8Array(transactionSigningRequest.remarks);
2206
+ if (remarks.length > 32) {
2207
+ return new SignResult(-910, null, null);
2208
+ }
2209
+ }
2210
+
2211
+ // Use chainId from request if provided, otherwise use the one from initialize()
2212
+ let chainId;
2213
+ if (transactionSigningRequest.chainId !== null && transactionSigningRequest.chainId !== undefined) {
2214
+ // Validate chainId if provided
2215
+ let tempChainId = parseInt(transactionSigningRequest.chainId);
2216
+ if (Number.isInteger(tempChainId) === false) {
2217
+ return new SignResult(-911, null, null);
2218
+ }
2219
+ chainId = tempChainId;
2220
+ if (chainId < 0) {
2221
+ return new SignResult(-912, null, null);
2222
+ }
2223
+ } else {
2224
+ // Use chainId from initialize()
2225
+ chainId = config.chainId;
2226
+ }
2227
+
2228
+ let signingContext = transactionSigningRequest.signingContext;
2229
+ const keyTypeForContext = getKeyTypeFromPrivateKey(transactionSigningRequest.wallet.privateKey);
2230
+ if (signingContext === null || signingContext === undefined) {
2231
+ if (keyTypeForContext === KEY_TYPE_HYBRIDEDMLDSASLHDSA) {
2232
+ signingContext = 0;
2233
+ } else if (keyTypeForContext === KEY_TYPE_HYBRIDEDMLDSASLHDSA5) {
2234
+ signingContext = 1;
2235
+ } else {
2236
+ return new SignResult(-913, null, null);
2237
+ }
2238
+ }
2239
+
2240
+ let txSigningHash = transactionGetSigningHash2(transactionSigningRequest.wallet.address, nonce, transactionSigningRequest.toAddress, valueInWeiHex, gasLimit, chainId, data, remarks, signingContext);
2241
+ if (txSigningHash == null) {
2242
+ return new SignResult(-914, null, null);
2243
+ }
2244
+ const txSigningHashU8 = toUint8Array(txSigningHash);
2245
+ const wallet = transactionSigningRequest.wallet;
2246
+ const privU8 = toUint8Array(wallet.privateKey);
2247
+ const pubU8 = toUint8Array(wallet.publicKey);
2248
+ let sigRes;
2249
+ let verRes;
2250
+ const hybridNs = circl && circl.hybridedmldsaslhdsa;
2251
+ const hybrid5Ns = circl && (circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5);
2252
+ if (signingContext === 0 && hybridNs) {
2253
+ sigRes = hybridNs.signCompact(privU8, txSigningHashU8);
2254
+ if (sigRes && sigRes.error) return new SignResult(-915, null, null);
2255
+ verRes = hybridNs.verifyCompact(pubU8, txSigningHashU8, sigRes.result);
2256
+ } else if (signingContext === 1 && hybrid5Ns) {
2257
+ sigRes = hybrid5Ns.sign(privU8, txSigningHashU8);
2258
+ if (sigRes && sigRes.error) return new SignResult(-916, null, null);
2259
+ verRes = hybrid5Ns.verify(pubU8, txSigningHashU8, sigRes.result);
2260
+ } else if (signingContext === 2 && hybridNs) {
2261
+ sigRes = hybridNs.sign(privU8, txSigningHashU8);
2262
+ if (sigRes && sigRes.error) return new SignResult(-917, null, null);
2263
+ verRes = hybridNs.verify(pubU8, txSigningHashU8, sigRes.result);
2264
+ } else {
2265
+ return new SignResult(-918, null, null);
2266
+ }
2267
+ if (verRes && verRes.error) return new SignResult(-919, null, null);
2268
+ if (verRes.result !== true) return new SignResult(-920, null, null);
2269
+ const quantumSig = sigRes.result instanceof Uint8Array ? Array.from(sigRes.result) : sigRes.result;
2270
+
2271
+ let txHashHex = transactionGetTransactionHash2(transactionSigningRequest.wallet.address, nonce, transactionSigningRequest.toAddress, valueInWeiHex, gasLimit, chainId, data, remarks, signingContext, transactionSigningRequest.wallet.publicKey, quantumSig);
2272
+ if (txHashHex == null) {
2273
+ return new SignResult(-921, null, null);
2274
+ }
2275
+
2276
+ let txnData = transactionGetData2(transactionSigningRequest.wallet.address, nonce, transactionSigningRequest.toAddress, valueInWeiHex, gasLimit, chainId, data, remarks, signingContext, transactionSigningRequest.wallet.publicKey, quantumSig);
2277
+ if (txnData == null) {
2278
+ return new SignResult(-922, null, null);
2279
+ }
2280
+
2281
+ return new SignResult(0, txHashHex, txnData);
2282
+ }
2283
+
2284
+ /**
2285
+ * Sign a message with a private key. Optional signingContext selects algorithm (same pattern as signRawTransaction); if null/omitted, derived from private key type.
2286
+ * @param {number[]|Uint8Array} privateKey - Private key bytes.
2287
+ * @param {number[]|Uint8Array} message - Message bytes (e.g. 32-byte hash).
2288
+ * @param {number|null} [signingContext] - Optional. 0 = hybridedmldsaslhdsa compact, 1 = hybridedmldsaslhdsa5, 2 = hybridedmldsaslhdsa full. If null/omitted, derived from private key type.
2289
+ * @returns {{ resultCode: number, signature: Uint8Array|null }} resultCode 0 on success, signature bytes; negative on error (e.g. -1000 not initialized, -700 invalid args, -701 unknown key type, -702/-703 CIRCL sign error, -704 unsupported key type or context).
2290
+ */
2291
+ function sign(privateKey, message, signingContext) {
2292
+ if (isInitialized === false) {
2293
+ return { resultCode: -1000, signature: null };
2294
+ }
2295
+ if (!isByteArray(privateKey) || !isByteArray(message)) {
2296
+ return { resultCode: -700, signature: null };
2297
+ }
2298
+ const privU8 = toUint8Array(privateKey);
2299
+ const messageU8 = toUint8Array(message);
2300
+ const hybridNs = circl && circl.hybridedmldsaslhdsa;
2301
+ const hybrid5Ns = circl && (circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5);
2302
+ let sigRes;
2303
+ const ctx = signingContext === null || signingContext === undefined ? null : signingContext;
2304
+ if (ctx === null) {
2305
+ const keyType = getKeyTypeFromPrivateKey(privateKey);
2306
+ if (keyType == null) {
2307
+ return { resultCode: -701, signature: null };
2308
+ }
2309
+ if (keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA && hybridNs) {
2310
+ sigRes = hybridNs.signCompact(privU8, messageU8);
2311
+ if (sigRes && sigRes.error) return { resultCode: -702, signature: null };
2312
+ } else if (keyType === KEY_TYPE_HYBRIDEDMLDSASLHDSA5 && hybrid5Ns) {
2313
+ sigRes = hybrid5Ns.sign(privU8, messageU8);
2314
+ if (sigRes && sigRes.error) return { resultCode: -703, signature: null };
2315
+ } else {
2316
+ return { resultCode: -704, signature: null };
2317
+ }
2318
+ } else if (ctx === 0 && hybridNs) {
2319
+ sigRes = hybridNs.signCompact(privU8, messageU8);
2320
+ if (sigRes && sigRes.error) return { resultCode: -702, signature: null };
2321
+ } else if (ctx === 1 && hybrid5Ns) {
2322
+ sigRes = hybrid5Ns.sign(privU8, messageU8);
2323
+ if (sigRes && sigRes.error) return { resultCode: -703, signature: null };
2324
+ } else if (ctx === 2 && hybridNs) {
2325
+ sigRes = hybridNs.sign(privU8, messageU8);
2326
+ if (sigRes && sigRes.error) return { resultCode: -702, signature: null };
2327
+ } else {
2328
+ return { resultCode: -704, signature: null };
2329
+ }
2330
+ const sig = sigRes.result instanceof Uint8Array ? sigRes.result : new Uint8Array(sigRes.result);
2331
+ return { resultCode: 0, signature: sig };
2332
+ }
2333
+
2334
+ /**
2335
+ * Verify a signature over a message with a public key. Algorithm is determined by the first byte of the signature: 1=hybrideds verifyCompact, 2=hybrideds verify, 3=hybridedmldsaslhdsa verifyCompact, 4=hybridedmldsaslhdsa verify, 5=hybridedmldsaslhdsa5 verify.
2336
+ * @param {number[]|Uint8Array} publicKey - Public key bytes.
2337
+ * @param {number[]|Uint8Array} signature - Signature bytes from sign(); first byte selects verify function (1-5).
2338
+ * @param {number[]|Uint8Array} message - Message bytes (same as passed to sign).
2339
+ * @returns {{ resultCode: number, valid: boolean }} resultCode 0 and valid true if signature is valid; negative on error (e.g. -1000 not initialized, -715 invalid args, -717 CIRCL verify error, -719 signature invalid, -718 unknown signature type).
2340
+ */
2341
+ function verify(publicKey, signature, message) {
2342
+ if (isInitialized === false) {
2343
+ return { resultCode: -1000, valid: false };
2344
+ }
2345
+ if (!isByteArray(publicKey) || !isByteArray(signature) || !isByteArray(message)) {
2346
+ return { resultCode: -715, valid: false };
2347
+ }
2348
+ const sigLen = signature.byteLength !== undefined ? signature.byteLength : signature.length;
2349
+ if (sigLen < 1) {
2350
+ return { resultCode: -715, valid: false };
2351
+ }
2352
+ const sigType = signature[0];
2353
+ const pubU8 = toUint8Array(publicKey);
2354
+ const sigU8 = toUint8Array(signature);
2355
+ const messageU8 = toUint8Array(message);
2356
+ const hybridedsNs = circl && circl.hybrideds;
2357
+ const hybridNs = circl && circl.hybridedmldsaslhdsa;
2358
+ const hybrid5Ns = circl && (circl.hybridedmldsaslhdsa5 || circl.hybridedmldsaslhds5);
2359
+ let verRes;
2360
+ if (sigType === 1 && hybridedsNs) {
2361
+ verRes = hybridedsNs.verifyCompact(pubU8, messageU8, sigU8);
2362
+ } else if (sigType === 2 && hybridedsNs) {
2363
+ verRes = hybridedsNs.verify(pubU8, messageU8, sigU8);
2364
+ } else if (sigType === 3 && hybridNs) {
2365
+ verRes = hybridNs.verifyCompact(pubU8, messageU8, sigU8);
2366
+ } else if (sigType === 4 && hybridNs) {
2367
+ verRes = hybridNs.verify(pubU8, messageU8, sigU8);
2368
+ } else if (sigType === 5 && hybrid5Ns) {
2369
+ verRes = hybrid5Ns.verify(pubU8, messageU8, sigU8);
2370
+ } else {
2371
+ return { resultCode: -718, valid: false };
2372
+ }
2373
+ if (verRes && verRes.error) return { resultCode: -717, valid: false };
2374
+ if (verRes.result !== true) return { resultCode: -719, valid: false };
2375
+ return { resultCode: 0, valid: true };
2376
+ }
2377
+
2378
+ /**
2379
+ * The sendCoins function posts a send-coin transaction to the blockchain. The chainId used for signing should be provided in the initialize() function.
2380
+ * Since the gas fee for sending coins is fixed at 1000 coins, there is no option to set the gas fee explicitly.
2381
+ * It may take many seconds after submitting a transaction before the transaction is returned by the getTransactionDetails function.
2382
+ * Transactions are usually committed in less than 30 seconds.
2383
+ *
2384
+ * @deprecated Use signRawTransaction and postTransaction instead.
2385
+ * @async
2386
+ * @function sendCoins
2387
+ * @param {Wallet} wallet - A Wallet object from which the transaction has to be sent. The address corresponding to the Wallet should have enough coins to cover gas fees as well. A minimum of 1000 coins (1000000000000000000000 wei) are required for gas fees.
2388
+ * @param {string} toAddress - The address to which the coins should be sent.
2389
+ * @param {string} coins - The string representing the number of coins (in ether) to send. To convert between ethers and wei, see https://docs.ethers.org/v4/api-utils.html#ether-strings-and-wei
2390
+ * @param {number} nonce - The nonce of the account retrieved by invoking the getAccountDetails function. You have to carefully manage state of the nonce to avoid sending the coins multiple times, such as when retrying sendCoins after a network error.
2391
+ * @return {Promise<SendResult>} Returns a promise of type SendResult.
2392
+ */
2393
+ async function sendCoins(wallet, toAddress, coins, nonce) {
2394
+ if (isInitialized === false) {
2395
+ return -1000;
2396
+ }
2397
+
2398
+ if (wallet == null || toAddress == null || coins == null || nonce == null) {
2399
+ return new SendResult(-1, null, null, null);
2400
+ }
2401
+
2402
+ if (isAddressValid(toAddress) == false) {
2403
+ return new SendResult(-2, null, null, null);
2404
+ }
2405
+
2406
+ if (verifyWallet(wallet) == false) {
2407
+ return new SendResult(-3, null, null, null);
2408
+ }
2409
+
2410
+ if (isLargeNumber(coins) == false) {
2411
+ return new SendResult(-4, null, null, null);
2412
+ }
2413
+
2414
+ let tempNonce = parseInt(nonce);
2415
+ if (Number.isInteger(tempNonce) == false) {
2416
+ return new SendResult(-5, null, null, null);
2417
+ }
2418
+ nonce = tempNonce;
2419
+ if (nonce < 0) {
2420
+ return new SendResult(-6, null, null, null);
2421
+ }
2422
+
2423
+ let accountDetailsResult = await getAccountDetails(wallet.address);
2424
+ if (accountDetailsResult === null) {
2425
+ return new SendResult(-7, null, null, null);
2426
+ }
2427
+
2428
+ if (accountDetailsResult.resultCode !== 0) {
2429
+ return new SendResult(-7, null, null, null);
2430
+ }
2431
+
2432
+ if (accountDetailsResult.accountDetails.nonce !== nonce) {
2433
+ return new SendResult(-8, null, null, null);
2434
+ }
2435
+
2436
+ const contractData = null;
2437
+
2438
+ var txSigningHash = transactionGetSigningHash(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData);
2439
+ if (txSigningHash == null) {
2440
+ return new SendResult(-9, null, null, null);
2441
+ }
2442
+ const txSigningHashU8 = toUint8Array(txSigningHash);
2443
+ const hybridedsNs = circl && circl.hybrideds;
2444
+ if (!hybridedsNs) {
2445
+ return new SendResult(-10, null, null, null);
2446
+ }
2447
+ const sigRes = hybridedsNs.signCompact(toUint8Array(wallet.privateKey), txSigningHashU8);
2448
+ if (sigRes && sigRes.error) {
2449
+ return new SendResult(-10, null, null, null);
2450
+ }
2451
+ const verRes = hybridedsNs.verifyCompact(toUint8Array(wallet.publicKey), txSigningHashU8, sigRes.result);
2452
+ if (verRes && verRes.error) {
2453
+ return new SendResult(-10, null, null, null);
2454
+ }
2455
+ if (verRes.result !== true) {
2456
+ return new SendResult(-10, null, null, null);
2457
+ }
2458
+ const quantumSig = sigRes.result instanceof Uint8Array ? Array.from(sigRes.result) : sigRes.result;
2459
+
2460
+ var txHashHex = transactionGetTransactionHash(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData, wallet.publicKey, quantumSig);
2461
+ if (txHashHex == null) {
2462
+ return new SendResult(-11, null, null, null);
2463
+ }
2464
+
2465
+ var txnData = transactionGetData(wallet.address, nonce, toAddress, coins, DEFAULT_GAS, config.chainId, contractData, wallet.publicKey, quantumSig);
2466
+ if (txnData == null) {
2467
+ return new SendResult(-12, null, null, null);
2468
+ }
2469
+
2470
+ let sendResult = await postTransaction(txnData);
2471
+ sendResult.txnHash = txHashHex;
2472
+
2473
+ return sendResult;
2474
+ }
2475
+
2476
+ /**
2477
+ * The publicKeyFromSignature extracts the public key from a signature.
2478
+ *
2479
+ * @function publicKeyFromSignature
2480
+ * @param {number[]} digest - An array of bytes containing the digestHash. Should be of length 32.
2481
+ * @param {number[]} signature - An array of bytes containing the signature.
2482
+ * @return {string} - Returns the public key as a hex string. Returns null if the operation failed.
2483
+ */
2484
+ function publicKeyFromSignature(digest, signature) {
2485
+ if (isInitialized === false) {
2486
+ return -1000;
2487
+ }
2488
+
2489
+ if (digest === undefined || digest === null || digest.length === undefined || digest.length === null || digest.length !== 32) {
2490
+ return null;
2491
+ }
2492
+
2493
+ if (signature === undefined || signature === null || signature.length === undefined || signature.length === null) {
2494
+ return null;
2495
+ }
2496
+
2497
+ let publicKeyHex = PublicKeyFromSignature(digest, signature);
2498
+
2499
+ return publicKeyHex;
2500
+ }
2501
+
2502
+ /**
2503
+ * The publicKeyFromPrivateKey extracts the public key from a private key.
2504
+ *
2505
+ * @function publicKeyFromPrivateKey
2506
+ * @param {number[]} privateKey - An array of bytes containing the privateKey.
2507
+ * @return {string} - Returns the public key as a hex string. Returns null if the operation failed.
2508
+ */
2509
+ function publicKeyFromPrivateKey(privateKey) {
2510
+ if (isInitialized === false) {
2511
+ return -1000;
2512
+ }
2513
+
2514
+ if (privateKey === undefined || privateKey === null || privateKey.length === undefined || privateKey.length === null) {
2515
+ return null;
2516
+ }
2517
+
2518
+ let publicKeyHex = PublicKeyFromPrivateKey(privateKey);
2519
+
2520
+ return publicKeyHex;
2521
+ }
2522
+
2523
+ /**
2524
+ * The addressFromPublicKey returns the address corresponding to the public key.
2525
+ *
2526
+ * @function addressFromPublicKey
2527
+ * @param {number[]} publicKey - An array of bytes containing the public key.
2528
+ * @return {string} - Returns the address corresponding to the public key as a hex string. Returns null if the operation failed.
2529
+ */
2530
+ function addressFromPublicKey(publicKey) {
2531
+ if (isInitialized === false) {
2532
+ return -1000;
2533
+ }
2534
+
2535
+ if (publicKey === undefined || publicKey === null || publicKey.length === undefined || publicKey.length === null) {
2536
+ return null;
2537
+ }
2538
+
2539
+ return PublicKeyToAddress(publicKey);
2540
+ }
2541
+
2542
+ /**
2543
+ * The combinePublicKeySignature combines the public key and signature.
2544
+ *
2545
+ * @function combinePublicKeySignature
2546
+ * @param {number[]} publicKey - An array of bytes containing the public key.
2547
+ * @param {number[]} signature - An array of bytes containing the signature.
2548
+ * @return {string} - Returns a hex string corresponding to combined signature. Returns null if the operation failed.
2549
+ */
2550
+ function combinePublicKeySignature(publicKey, signature) {
2551
+ if (isInitialized === false) {
2552
+ return -1000;
2553
+ }
2554
+
2555
+ if (publicKey === undefined || publicKey === null || publicKey.length === undefined || publicKey.length === null) {
2556
+ return null;
2557
+ }
2558
+
2559
+ if (signature === undefined || signature === null || signature.length === undefined || signature.length === null) {
2560
+ return null;
2561
+ }
2562
+
2563
+ return CombinePublicKeySignature(publicKey, signature);
2564
+ }
2565
+
2566
+ /**
2567
+ * The packMethodData function packs a Solidity method call with the given ABI, method name, and arguments.
2568
+ * It returns the transaction data as a hex string that can be included in a transaction.
2569
+ *
2570
+ * @function packMethodData
2571
+ * @param {string} abiJSON - The Solidity ABI file content as a JSON string
2572
+ * @param {string} methodName - The name of the method to call
2573
+ * @param {...*} args - The parameters to pass to the method (variable arguments)
2574
+ * @return {PackUnpackResult} - Returns a PackUnpackResult object containing the error (if any) and the packed transaction data as a hex string.
2575
+ */
2576
+ function packMethodData(abiJSON, methodName, ...args) {
2577
+ if (isInitialized === false) {
2578
+ return new PackUnpackResult('SDK not initialized. Call initialize() first.', '');
2579
+ }
2580
+
2581
+ if (abiJSON === undefined || abiJSON === null || typeof abiJSON !== 'string') {
2582
+ return new PackUnpackResult('Invalid abiJSON parameter. Expected a string.', '');
2583
+ }
2584
+
2585
+ if (methodName === undefined || methodName === null || typeof methodName !== 'string') {
2586
+ return new PackUnpackResult('Invalid methodName parameter. Expected a string.', '');
2587
+ }
2588
+
2589
+ try {
2590
+ // Call WASM PackMethodData function
2591
+ // The WASM function expects: (abiJSON, methodName, ...args)
2592
+ const result = PackMethodData(abiJSON, methodName, ...args);
2593
+
2594
+ // Check if WASM returned an Error object
2595
+ if (result instanceof Error) {
2596
+ return new PackUnpackResult(result.message || 'Unknown error from WASM', '');
2597
+ }
2598
+
2599
+ if (result === null || result === undefined) {
2600
+ return new PackUnpackResult('PackMethodData returned null or undefined', '');
2601
+ }
2602
+
2603
+ // The WASM returns a string where each character is a byte
2604
+ // Convert to hex string
2605
+ let hex = '0x';
2606
+ for (let i = 0; i < result.length; i++) {
2607
+ const byte = result.charCodeAt(i);
2608
+ const hexByte = byte.toString(16);
2609
+ hex += hexByte.length === 1 ? '0' + hexByte : hexByte;
2610
+ }
2611
+
2612
+ return new PackUnpackResult('', hex);
2613
+ } catch (error) {
2614
+ const errorMessage = error instanceof Error ? error.message : String(error);
2615
+ return new PackUnpackResult(errorMessage, '');
2616
+ }
2617
+ }
2618
+
2619
+ /**
2620
+ * The unpackMethodData function unpacks the return values of a Solidity method call.
2621
+ * It returns the unpacked values as a JavaScript array or object.
2622
+ *
2623
+ * @function unpackMethodData
2624
+ * @param {string} abiJSON - The Solidity ABI file content as a JSON string
2625
+ * @param {string} methodName - The name of the method whose return values to unpack
2626
+ * @param {string} hexData - The hex-encoded return data (with or without 0x prefix)
2627
+ * @return {PackUnpackResult} - Returns a PackUnpackResult object containing the error (if any) and the unpacked return values as a JSON string.
2628
+ */
2629
+ function unpackMethodData(abiJSON, methodName, hexData) {
2630
+ if (isInitialized === false) {
2631
+ return new PackUnpackResult('SDK not initialized. Call initialize() first.', '');
2632
+ }
2633
+
2634
+ if (abiJSON === undefined || abiJSON === null || typeof abiJSON !== 'string') {
2635
+ return new PackUnpackResult('Invalid abiJSON parameter. Expected a string.', '');
2636
+ }
2637
+
2638
+ if (methodName === undefined || methodName === null || typeof methodName !== 'string') {
2639
+ return new PackUnpackResult('Invalid methodName parameter. Expected a string.', '');
2640
+ }
2641
+
2642
+ if (hexData === undefined || hexData === null || typeof hexData !== 'string') {
2643
+ return new PackUnpackResult('Invalid hexData parameter. Expected a string.', '');
2644
+ }
2645
+
2646
+ try {
2647
+ // Call WASM UnpackMethodData function
2648
+ const result = UnpackMethodData(abiJSON, methodName, hexData);
2649
+
2650
+ // Check if WASM returned an Error object
2651
+ if (result instanceof Error) {
2652
+ return new PackUnpackResult(result.message || 'Unknown error from WASM', '');
2653
+ }
2654
+
2655
+ if (result === null || result === undefined) {
2656
+ return new PackUnpackResult('UnpackMethodData returned null or undefined', '');
2657
+ }
2658
+
2659
+ // Return the JSON string result directly
2660
+ return new PackUnpackResult('', result);
2661
+ } catch (error) {
2662
+ const errorMessage = error instanceof Error ? error.message : String(error);
2663
+ return new PackUnpackResult(errorMessage, '');
2664
+ }
2665
+ }
2666
+
2667
+ /**
2668
+ * The packCreateContractData function packs constructor data for contract creation.
2669
+ * It combines the contract bytecode with the ABI-encoded constructor parameters.
2670
+ * This matches the Go pattern: Pack("", params...) and append(bytecode, input...)
2671
+ *
2672
+ * @function packCreateContractData
2673
+ * @param {string} abiJSON - The Solidity ABI file content as a JSON string
2674
+ * @param {string} bytecode - The contract bytecode as a hex string (with or without 0x prefix)
2675
+ * @param {...*} args - The constructor parameters (variable arguments, can be 0 or more)
2676
+ * @return {PackUnpackResult} - Returns a PackUnpackResult object containing the error (if any) and the packed contract creation data as a hex string.
2677
+ */
2678
+ function packCreateContractData(abiJSON, bytecode, ...args) {
2679
+ if (isInitialized === false) {
2680
+ return new PackUnpackResult('SDK not initialized. Call initialize() first.', '');
2681
+ }
2682
+
2683
+ if (abiJSON === undefined || abiJSON === null || typeof abiJSON !== 'string') {
2684
+ return new PackUnpackResult('Invalid abiJSON parameter. Expected a string.', '');
2685
+ }
2686
+
2687
+ if (bytecode === undefined || bytecode === null || typeof bytecode !== 'string') {
2688
+ return new PackUnpackResult('Invalid bytecode parameter. Expected a string.', '');
2689
+ }
2690
+
2691
+ try {
2692
+ // Call WASM PackCreateContractData function
2693
+ // The WASM function expects: (abiJSON, bytecode, ...args)
2694
+ const result = PackCreateContractData(abiJSON, bytecode, ...args);
2695
+
2696
+ // Check if WASM returned an Error object
2697
+ if (result instanceof Error) {
2698
+ return new PackUnpackResult(result.message || 'Unknown error from WASM', '');
2699
+ }
2700
+
2701
+ if (result === null || result === undefined) {
2702
+ return new PackUnpackResult('PackCreateContractData returned null or undefined', '');
2703
+ }
2704
+
2705
+ // The WASM returns a string where each character is a byte
2706
+ // Convert to hex string
2707
+ let hex = '0x';
2708
+ for (let i = 0; i < result.length; i++) {
2709
+ const byte = result.charCodeAt(i);
2710
+ const hexByte = byte.toString(16);
2711
+ hex += hexByte.length === 1 ? '0' + hexByte : hexByte;
2712
+ }
2713
+
2714
+ return new PackUnpackResult('', hex);
2715
+ } catch (error) {
2716
+ const errorMessage = error instanceof Error ? error.message : String(error);
2717
+ return new PackUnpackResult(errorMessage, '');
2718
+ }
2719
+ }
2720
+
2721
+ /**
2722
+ * The encodeEventLog function encodes event parameters into topics and data according to the ABI specification.
2723
+ * It returns the topics array and data hex string that can be used to create event logs.
2724
+ *
2725
+ * @function encodeEventLog
2726
+ * @param {string} abiJSON - The Solidity ABI file content as a JSON string
2727
+ * @param {string} eventName - The name of the event to encode
2728
+ * @param {...*} args - The event parameter values (variable arguments)
2729
+ * @return {EventLogEncodeResult} - Returns an EventLogEncodeResult object containing the error (if any) and the encoded event log with topics and data.
2730
+ */
2731
+ function encodeEventLog(abiJSON, eventName, ...args) {
2732
+ if (isInitialized === false) {
2733
+ return new EventLogEncodeResult('SDK not initialized. Call initialize() first.', null);
2734
+ }
2735
+
2736
+ if (abiJSON === undefined || abiJSON === null || typeof abiJSON !== 'string') {
2737
+ return new EventLogEncodeResult('Invalid abiJSON parameter. Expected a string.', null);
2738
+ }
2739
+
2740
+ if (eventName === undefined || eventName === null || typeof eventName !== 'string') {
2741
+ return new EventLogEncodeResult('Invalid eventName parameter. Expected a string.', null);
2742
+ }
2743
+
2744
+ try {
2745
+ // Call WASM EncodeEventLog function
2746
+ // The WASM function expects: (abiJSON, eventName, ...args)
2747
+ const result = EncodeEventLog(abiJSON, eventName, ...args);
2748
+
2749
+ // Check if WASM returned an Error object
2750
+ if (result instanceof Error) {
2751
+ return new EventLogEncodeResult(result.message || 'Unknown error from WASM', null);
2752
+ }
2753
+
2754
+ if (result === null || result === undefined) {
2755
+ return new EventLogEncodeResult('EncodeEventLog returned null or undefined', null);
2756
+ }
2757
+
2758
+ // The WASM returns a JavaScript object with topics (array) and data (hex string)
2759
+ // Extract topics array and data
2760
+ const topics = result.topics || [];
2761
+ const data = result.data || '0x';
2762
+
2763
+ // Ensure topics is an array and data is a string
2764
+ if (!Array.isArray(topics)) {
2765
+ return new EventLogEncodeResult('EncodeEventLog returned invalid topics (not an array)', null);
2766
+ }
2767
+
2768
+ if (typeof data !== 'string') {
2769
+ return new EventLogEncodeResult('EncodeEventLog returned invalid data (not a string)', null);
2770
+ }
2771
+
2772
+ return new EventLogEncodeResult('', { topics, data });
2773
+ } catch (error) {
2774
+ const errorMessage = error instanceof Error ? error.message : String(error);
2775
+ return new EventLogEncodeResult(errorMessage, null);
2776
+ }
2777
+ }
2778
+
2779
+ /**
2780
+ * The decodeEventLog function decodes event log topics and data back into event parameters.
2781
+ * It returns the decoded values as a JavaScript object.
2782
+ *
2783
+ * @function decodeEventLog
2784
+ * @param {string} abiJSON - The Solidity ABI file content as a JSON string
2785
+ * @param {string} eventName - The name of the event to decode
2786
+ * @param {string[]} topics - Array of topic hex strings (with or without 0x prefix)
2787
+ * @param {string} data - Hex-encoded data string (with or without 0x prefix)
2788
+ * @return {PackUnpackResult} - Returns a PackUnpackResult object containing the error (if any) and the decoded event parameters as a JSON string.
2789
+ */
2790
+ function decodeEventLog(abiJSON, eventName, topics, data) {
2791
+ if (isInitialized === false) {
2792
+ return new PackUnpackResult('SDK not initialized. Call initialize() first.', '');
2793
+ }
2794
+
2795
+ if (abiJSON === undefined || abiJSON === null || typeof abiJSON !== 'string') {
2796
+ return new PackUnpackResult('Invalid abiJSON parameter. Expected a string.', '');
2797
+ }
2798
+
2799
+ if (eventName === undefined || eventName === null || typeof eventName !== 'string') {
2800
+ return new PackUnpackResult('Invalid eventName parameter. Expected a string.', '');
2801
+ }
2802
+
2803
+ if (topics === undefined || topics === null || !Array.isArray(topics)) {
2804
+ return new PackUnpackResult('Invalid topics parameter. Expected an array of strings.', '');
2805
+ }
2806
+
2807
+ if (data === undefined || data === null || typeof data !== 'string') {
2808
+ return new PackUnpackResult('Invalid data parameter. Expected a string.', '');
2809
+ }
2810
+
2811
+ try {
2812
+ // Call WASM DecodeEventLog function
2813
+ const result = DecodeEventLog(abiJSON, eventName, topics, data);
2814
+
2815
+ // Check if WASM returned an Error object
2816
+ if (result instanceof Error) {
2817
+ return new PackUnpackResult(result.message || 'Unknown error from WASM', '');
2818
+ }
2819
+
2820
+ if (result === null || result === undefined) {
2821
+ return new PackUnpackResult('DecodeEventLog returned null or undefined', '');
2822
+ }
2823
+
2824
+ // The WASM returns a JSON string
2825
+ if (typeof result !== 'string') {
2826
+ return new PackUnpackResult('DecodeEventLog returned invalid result (not a string)', '');
2827
+ }
2828
+
2829
+ return new PackUnpackResult('', result);
2830
+ } catch (error) {
2831
+ const errorMessage = error instanceof Error ? error.message : String(error);
2832
+ return new PackUnpackResult(errorMessage, '');
2833
+ }
2834
+ }
2835
+
2836
+ /**
2837
+ * The encodeRlp function encodes a JavaScript value to RLP (Recursive Length Prefix) format.
2838
+ * Supports: strings, numbers, booleans, arrays, objects (maps), and hex-encoded bytes.
2839
+ * Returns a hex-encoded string of the RLP-encoded data.
2840
+ *
2841
+ * @function encodeRlp
2842
+ * @param {*} value - The value to encode (can be string, number, boolean, array, object, etc.)
2843
+ * @return {PackUnpackResult} - Returns a PackUnpackResult object containing the error (if any) and the RLP-encoded data as a hex string.
2844
+ */
2845
+ function encodeRlp(value) {
2846
+ if (isInitialized === false) {
2847
+ return new PackUnpackResult('SDK not initialized. Call initialize() first.', '');
2848
+ }
2849
+
2850
+ // Note: null and undefined are valid inputs and will be encoded as empty bytes
2851
+
2852
+ try {
2853
+ // Call WASM EncodeRlp function
2854
+ const result = EncodeRlp(value);
2855
+
2856
+ // Check if WASM returned an Error object
2857
+ if (result instanceof Error) {
2858
+ return new PackUnpackResult(result.message || 'Unknown error from WASM', '');
2859
+ }
2860
+
2861
+ if (result === null || result === undefined) {
2862
+ return new PackUnpackResult('EncodeRlp returned null or undefined', '');
2863
+ }
2864
+
2865
+ // The WASM returns a hex string
2866
+ if (typeof result !== 'string') {
2867
+ return new PackUnpackResult('EncodeRlp returned invalid result (not a string)', '');
2868
+ }
2869
+
2870
+ return new PackUnpackResult('', result);
2871
+ } catch (error) {
2872
+ const errorMessage = error instanceof Error ? error.message : String(error);
2873
+ return new PackUnpackResult(errorMessage, '');
2874
+ }
2875
+ }
2876
+
2877
+ /**
2878
+ * The decodeRlp function decodes RLP-encoded data back to a JavaScript-compatible value.
2879
+ * Takes a hex-encoded string and returns a JSON string representation of the decoded value.
2880
+ *
2881
+ * @function decodeRlp
2882
+ * @param {string} data - The hex-encoded RLP data (with or without 0x prefix)
2883
+ * @return {PackUnpackResult} - Returns a PackUnpackResult object containing the error (if any) and the decoded value as a JSON string.
2884
+ */
2885
+ function decodeRlp(data) {
2886
+ if (isInitialized === false) {
2887
+ return new PackUnpackResult('SDK not initialized. Call initialize() first.', '');
2888
+ }
2889
+
2890
+ if (data === undefined || data === null || typeof data !== 'string') {
2891
+ return new PackUnpackResult('Invalid data parameter. Expected a string.', '');
2892
+ }
2893
+
2894
+ try {
2895
+ // Call WASM DecodeRlp function
2896
+ const result = DecodeRlp(data);
2897
+
2898
+ // Check if WASM returned an Error object
2899
+ if (result instanceof Error) {
2900
+ return new PackUnpackResult(result.message || 'Unknown error from WASM', '');
2901
+ }
2902
+
2903
+ if (result === null || result === undefined) {
2904
+ return new PackUnpackResult('DecodeRlp returned null or undefined', '');
2905
+ }
2906
+
2907
+ // The WASM returns a JSON string
2908
+ if (typeof result !== 'string') {
2909
+ return new PackUnpackResult('DecodeRlp returned invalid result (not a string)', '');
2910
+ }
2911
+
2912
+ return new PackUnpackResult('', result);
2913
+ } catch (error) {
2914
+ const errorMessage = error instanceof Error ? error.message : String(error);
2915
+ return new PackUnpackResult(errorMessage, '');
2916
+ }
2917
+ }
2918
+
2919
+ /**
2920
+ * The createAddress function calculates the contract address that will be created by a transaction.
2921
+ * This uses the CREATE opcode address calculation: keccak256(RLP(sender, nonce))
2922
+ *
2923
+ * @function createAddress
2924
+ * @param {string} address - The address of the account that will create the contract (hex string with 0x prefix)
2925
+ * @param {number} nonce - The nonce of the account at the time of contract creation
2926
+ * @return {string|null} - Returns the contract address as a hex string, or null if an error occurred.
2927
+ */
2928
+ function createAddress(address, nonce) {
2929
+ if (isInitialized === false) {
2930
+ return null;
2931
+ }
2932
+
2933
+ if (address === undefined || address === null || typeof address !== 'string') {
2934
+ return null;
2935
+ }
2936
+
2937
+ if (nonce === undefined || nonce === null || typeof nonce !== 'number') {
2938
+ return null;
2939
+ }
2940
+
2941
+ try {
2942
+ // Call WASM CreateAddress function
2943
+ // The WASM function expects: (address, nonce as string)
2944
+ const result = CreateAddress(address, nonce.toString());
2945
+
2946
+ // Check if WASM returned an Error object
2947
+ if (result instanceof Error) {
2948
+ return null;
2949
+ }
2950
+
2951
+ if (result === null || result === undefined) {
2952
+ return null;
2953
+ }
2954
+
2955
+ // Return the address string directly
2956
+ return result;
2957
+ } catch (error) {
2958
+ return null;
2959
+ }
2960
+ }
2961
+
2962
+ /**
2963
+ * The createAddress2 function calculates the contract address using the CREATE2 opcode.
2964
+ * This allows deterministic contract address calculation: keccak256(0xff || sender || salt || keccak256(init_code))
2965
+ *
2966
+ * @function createAddress2
2967
+ * @param {string} address - The address of the account that will create the contract (hex string with 0x prefix)
2968
+ * @param {string} salt - A 32-byte salt value as a hex string (with 0x prefix)
2969
+ * @param {string} initHash - The keccak256 hash of the contract initialization code as a hex string (with 0x prefix)
2970
+ * @return {string|null} - Returns the contract address as a hex string, or null if an error occurred.
2971
+ */
2972
+ function createAddress2(address, salt, initHash) {
2973
+ if (isInitialized === false) {
2974
+ return null;
2975
+ }
2976
+
2977
+ if (address === undefined || address === null || typeof address !== 'string') {
2978
+ return null;
2979
+ }
2980
+
2981
+ if (salt === undefined || salt === null || typeof salt !== 'string') {
2982
+ return null;
2983
+ }
2984
+
2985
+ if (initHash === undefined || initHash === null || typeof initHash !== 'string') {
2986
+ return null;
2987
+ }
2988
+
2989
+ try {
2990
+ // Call WASM CreateAddress2 function
2991
+ // The WASM function expects: (address, salt, initHash)
2992
+ const result = CreateAddress2(address, salt, initHash);
2993
+
2994
+ // Check if WASM returned an Error object
2995
+ if (result instanceof Error) {
2996
+ return null;
2997
+ }
2998
+
2999
+ if (result === null || result === undefined) {
3000
+ return null;
3001
+ }
3002
+
3003
+ // Return the address string directly
3004
+ return result;
3005
+ } catch (error) {
3006
+ return null;
3007
+ }
3008
+ }
3009
+
3010
+ module.exports = {
3011
+ initialize,
3012
+ serializeWallet,
3013
+ deserializeWallet,
3014
+ serializeEncryptedWallet,
3015
+ serializeSeedAsEncryptedWallet,
3016
+ deserializeEncryptedWallet,
3017
+ verifyWallet,
3018
+ newWallet,
3019
+ sendCoins,
3020
+ getAccountDetails,
3021
+ getTransactionDetails,
3022
+ isAddressValid,
3023
+ getLatestBlockDetails,
3024
+ signSendCoinTransaction,
3025
+ listAccountTransactions,
3026
+ postTransaction,
3027
+ Config,
3028
+ Wallet,
3029
+ BlockDetails,
3030
+ LatestBlockDetailsResult,
3031
+ AccountDetails,
3032
+ AccountDetailsResult,
3033
+ SendResult,
3034
+ TransactionReceipt,
3035
+ TransactionDetails,
3036
+ TransactionDetailsResult,
3037
+ AccountTransactionsResult,
3038
+ ListAccountTransactionsResponse,
3039
+ AccountTransactionCompact,
3040
+ newWalletSeed,
3041
+ openWalletFromSeed,
3042
+ openWalletFromSeedWords,
3043
+ publicKeyFromSignature,
3044
+ publicKeyFromPrivateKey,
3045
+ addressFromPublicKey,
3046
+ combinePublicKeySignature,
3047
+ TransactionSigningRequest,
3048
+ signRawTransaction,
3049
+ sign,
3050
+ verify,
3051
+ packMethodData,
3052
+ unpackMethodData,
3053
+ packCreateContractData,
3054
+ encodeEventLog,
3055
+ decodeEventLog,
3056
+ encodeRlp,
3057
+ decodeRlp,
3058
+ createAddress,
3059
+ createAddress2,
3060
+ PackUnpackResult,
3061
+ EventLogEncodeResult
3062
+ };