quantumcoin 7.0.1 → 7.0.3

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 (43) hide show
  1. package/.gitignore +3 -0
  2. package/README-SDK.md +64 -10
  3. package/README.md +27 -4
  4. package/SPEC.md +3843 -0
  5. package/examples/AllSolidityTypes.sol +184 -0
  6. package/examples/SimpleIERC20.sol +74 -0
  7. package/examples/example-generator-sdk-js.js +95 -0
  8. package/examples/example-generator-sdk-ts.js +95 -0
  9. package/examples/example.js +2 -2
  10. package/examples/offline-signing.js +73 -0
  11. package/examples/package-lock.json +10 -1103
  12. package/examples/package.json +1 -2
  13. package/examples/read-operations.js +1 -2
  14. package/examples/sdk-generator-erc20.inline.json +251 -0
  15. package/examples/solidity-types.ts +43 -0
  16. package/generate-sdk.js +689 -87
  17. package/package.json +30 -9
  18. package/src/abi/interface.d.ts +18 -0
  19. package/src/abi/interface.js +247 -9
  20. package/src/abi/js-abi-coder.js +474 -0
  21. package/src/contract/contract-factory.d.ts +1 -1
  22. package/src/contract/contract-factory.js +14 -2
  23. package/src/contract/contract.d.ts +10 -1
  24. package/src/contract/contract.js +42 -0
  25. package/src/generator/index.js +1041 -63
  26. package/src/index.d.ts +16 -0
  27. package/src/providers/provider.d.ts +20 -11
  28. package/src/providers/provider.js +12 -0
  29. package/src/types/index.d.ts +462 -0
  30. package/src/types/index.js +9 -0
  31. package/test/e2e/all-solidity-types.dynamic.test.js +200 -0
  32. package/test/e2e/all-solidity-types.fixtures.js +231 -0
  33. package/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +368 -0
  34. package/test/e2e/simple-erc20.generated-sdks.e2e.test.js +151 -0
  35. package/test/e2e/transactional.test.js +4 -4
  36. package/test/e2e/typed-generator.e2e.test.js +8 -6
  37. package/test/integration/ws-provider.test.js +1 -1
  38. package/test/unit/generate-contract-cli.test.js +2 -1
  39. package/test/unit/generate-sdk-artifacts-json.test.js +45 -0
  40. package/test/unit/generator.test.js +1 -0
  41. package/test/unit/populate-transaction.test.js +62 -0
  42. package/test/unit/solidity-types.test.js +46 -0
  43. package/test/unit/utils.test.js +1 -1
package/package.json CHANGED
@@ -1,20 +1,41 @@
1
1
  {
2
2
  "name": "quantumcoin",
3
- "version": "7.0.1",
3
+ "version": "7.0.3",
4
4
  "description": "QuantumCoin SDK - a complete SDK for dapps",
5
5
  "main": "index.js",
6
+ "types": "src/index.d.ts",
7
+ "typesVersions": {
8
+ "*": {
9
+ "types": [
10
+ "src/types/index.d.ts"
11
+ ],
12
+ "config": [
13
+ "config.d.ts"
14
+ ]
15
+ }
16
+ },
6
17
  "exports": {
7
- ".": "./index.js",
8
- "./config": "./config.js",
18
+ ".": {
19
+ "types": "./src/index.d.ts",
20
+ "default": "./index.js"
21
+ },
22
+ "./config": {
23
+ "types": "./config.d.ts",
24
+ "default": "./config.js"
25
+ },
26
+ "./types": {
27
+ "types": "./src/types/index.d.ts",
28
+ "default": "./src/types/index.js"
29
+ },
9
30
  "./package.json": "./package.json"
10
31
  },
11
32
  "scripts": {
12
- "test": "node --test \"test/**/*.test.js\"",
33
+ "test": "node --test \"test/unit/**/*.test.js\" \"test/integration/**/*.test.js\" \"test/security/**/*.test.js\" \"test/e2e/*.test.js\"",
13
34
  "test:unit": "node --test \"test/unit/**/*.test.js\"",
14
35
  "test:integration": "node --test \"test/integration/**/*.test.js\"",
15
36
  "test:security": "node --test \"test/security/**/*.test.js\"",
16
37
  "test:non-transactional": "node --test \"test/unit/**/*.test.js\" \"test/integration/**/*.test.js\" \"test/security/**/*.test.js\"",
17
- "test:e2e": "node --test --test-concurrency=1 \"test/e2e/**/*.test.js\"",
38
+ "test:e2e": "node --test --test-concurrency=1 \"test/e2e/*.test.js\"",
18
39
  "example": "node examples/example.js",
19
40
  "example:provider": "node examples/example.js",
20
41
  "example:wallet": "node examples/wallet-offline.js",
@@ -44,11 +65,11 @@
44
65
  },
45
66
  "homepage": "https://github.com/quantumcoinproject/quantumcoin.js#readme",
46
67
  "bin": {
47
- "quantumcoin-sdk-generator": "./generate-sdk.js"
68
+ "sdkgen": "./generate-sdk.js"
48
69
  },
49
70
  "dependencies": {
50
- "quantum-coin-pqc-js-sdk": "^1.0.0",
51
- "seed-words": "^1.0.1",
52
- "quantum-coin-js-sdk": "1.0.27"
71
+ "quantum-coin-pqc-js-sdk": "^1.0.5",
72
+ "seed-words": "^1.0.2",
73
+ "quantum-coin-js-sdk": "^1.0.28"
53
74
  }
54
75
  }
@@ -93,6 +93,15 @@ export class AbiCoder {
93
93
  * @returns {string}
94
94
  */
95
95
  encode(types: (string | any)[], values: any[]): string;
96
+ /**
97
+ * Typed ABI encode (Solidity ABI type strings).
98
+ * @param {TTypes} types
99
+ * @param {TValues} values
100
+ */
101
+ encode<TTypes extends readonly import("../types").SolidityTypeName[]>(
102
+ types: TTypes,
103
+ values: { [K in keyof TTypes]: import("../types").SolidityInputValue<TTypes[K]> },
104
+ ): string;
96
105
  /**
97
106
  * Decode ABI data by output types.
98
107
  * @param {(string|any)[]} types
@@ -100,6 +109,15 @@ export class AbiCoder {
100
109
  * @returns {any}
101
110
  */
102
111
  decode(types: (string | any)[], data: string): any;
112
+ /**
113
+ * Typed ABI decode (Solidity ABI type strings).
114
+ * @param {TTypes} types
115
+ * @param {string} data
116
+ */
117
+ decode<TTypes extends readonly import("../types").SolidityTypeName[]>(
118
+ types: TTypes,
119
+ data: string,
120
+ ): { [K in keyof TTypes]: import("../types").SolidityOutputValue<TTypes[K]> };
103
121
  /**
104
122
  * Return a default value for types.
105
123
  * @param {(string|any)[]} types
@@ -8,10 +8,11 @@
8
8
 
9
9
  const qcsdk = require("quantum-coin-js-sdk");
10
10
  const { makeError, assertArgument } = require("../errors");
11
- const { normalizeHex } = require("../internal/hex");
11
+ const { normalizeHex, arrayify, bytesToHex } = require("../internal/hex");
12
12
  const { EventFragment, FunctionFragment, ErrorFragment, ConstructorFragment } = require("./fragments");
13
13
  const { Result } = require("../utils/result");
14
14
  const { id } = require("../utils/hashing");
15
+ const jsAbi = require("./js-abi-coder");
15
16
 
16
17
  function _requireInitialized() {
17
18
  // eslint-disable-next-line global-require
@@ -48,6 +49,190 @@ function _asAbiArray(abi) {
48
49
  throw makeError("invalid ABI", "INVALID_ARGUMENT", { abi });
49
50
  }
50
51
 
52
+ function _isNumericString(v) {
53
+ return typeof v === "string" && /^-?\d+$/.test(v.trim());
54
+ }
55
+
56
+ function _normalizeParamTypeForQcsdk(type, components) {
57
+ if (typeof type !== "string") return type;
58
+
59
+ // Arrays preserve their shape; normalize the element type.
60
+ if (type.endsWith("]")) {
61
+ const inner = type.slice(0, type.lastIndexOf("["));
62
+ const suffix = type.slice(type.lastIndexOf("["));
63
+ return _normalizeParamTypeForQcsdk(inner, components) + suffix;
64
+ }
65
+
66
+ // Tuples: normalize component types recursively.
67
+ if (type === "tuple") return "tuple";
68
+
69
+ // quantum-coin-js-sdk currently only reliably packs int256/uint256.
70
+ if (type === "uint" || type.startsWith("uint")) return "uint256";
71
+ if (type === "int" || type.startsWith("int")) return "int256";
72
+
73
+ // Fixed-size bytes (bytes1..bytes32) are encoded as a single 32-byte word.
74
+ // Encode them via uint256 (big-endian), with right-padding applied.
75
+ if (type.startsWith("bytes")) {
76
+ const m = type.match(/^bytes(\d+)$/);
77
+ if (m) return "uint256";
78
+ }
79
+
80
+ return type;
81
+ }
82
+
83
+ function _normalizeAbiForQcsdk(abi) {
84
+ const out = Array.isArray(abi) ? abi.map((f) => ({ ...(f || {}) })) : [];
85
+ for (const f of out) {
86
+ if (!f || typeof f !== "object") continue;
87
+ const inputs = Array.isArray(f.inputs) ? f.inputs : [];
88
+ const outputs = Array.isArray(f.outputs) ? f.outputs : [];
89
+ const normalizeParams = (params) =>
90
+ params.map((p) => {
91
+ const np = { ...(p || {}) };
92
+ np.type = _normalizeParamTypeForQcsdk(String(np.type || ""), np.components);
93
+ if (Array.isArray(np.components) && np.components.length > 0) {
94
+ np.components = normalizeParams(np.components);
95
+ }
96
+ return np;
97
+ });
98
+ f.inputs = normalizeParams(inputs);
99
+ if (f.type === "function") f.outputs = normalizeParams(outputs);
100
+ if (f.type === "event") f.inputs = normalizeParams(inputs);
101
+ if (f.type === "error") f.inputs = normalizeParams(inputs);
102
+ if (f.type === "constructor") f.inputs = normalizeParams(inputs);
103
+ }
104
+ return out;
105
+ }
106
+
107
+ function _fixedBytesToUint256Decimal(type, value) {
108
+ // Allow already-normalized numeric values.
109
+ if (typeof value === "number") return value;
110
+ if (_isNumericString(value)) return value.trim();
111
+
112
+ const m = String(type || "").match(/^bytes(\d+)$/);
113
+ const n = m ? Number(m[1]) : 0;
114
+ if (!Number.isFinite(n) || n < 1 || n > 32) return value;
115
+
116
+ const b = arrayify(value);
117
+ if (b.length > n) throw makeError("fixed bytes value exceeds length", "INVALID_ARGUMENT", { type, length: b.length });
118
+
119
+ // Right-pad to N bytes, then right-pad to 32 bytes (Solidity fixed bytes encoding).
120
+ const fixed = new Uint8Array(n);
121
+ fixed.set(b, 0);
122
+ const word = new Uint8Array(32);
123
+ word.set(fixed, 0);
124
+
125
+ const hexWord = bytesToHex(word); // 0x + 64 hex
126
+ // Convert to decimal string for qcsdk (it can parse uint256 from decimal string).
127
+ return BigInt(hexWord).toString();
128
+ }
129
+
130
+ function _uint256ToFixedBytesHex(type, value) {
131
+ const m = String(type || "").match(/^bytes(\d+)$/);
132
+ const n = m ? Number(m[1]) : 0;
133
+ if (!Number.isFinite(n) || n < 1 || n > 32) return value;
134
+ if (value == null) return value;
135
+
136
+ const bi = typeof value === "bigint" ? value : BigInt(value);
137
+ let hex = bi.toString(16);
138
+ if (hex.length > 64) hex = hex.slice(-64);
139
+ hex = hex.padStart(64, "0");
140
+ // bytesN is the first N bytes (right-padded in the 32-byte word).
141
+ return normalizeHex("0x" + hex.slice(0, n * 2));
142
+ }
143
+
144
+ function _convertInputValueForQcsdk(param, value) {
145
+ const type = String(param && param.type ? param.type : "");
146
+
147
+ // Apply bigint sanitization early.
148
+ const v = _sanitizeArg(value);
149
+
150
+ // Arrays
151
+ if (type.endsWith("]")) {
152
+ const innerType = type.slice(0, type.lastIndexOf("["));
153
+ const innerParam = { ...(param || {}), type: innerType };
154
+ if (!Array.isArray(v)) return v;
155
+ return v.map((x) => _convertInputValueForQcsdk(innerParam, x));
156
+ }
157
+
158
+ // Tuples
159
+ if (type === "tuple") {
160
+ const comps = Array.isArray(param && param.components) ? param.components : [];
161
+ if (Array.isArray(v)) {
162
+ return v.map((x, idx) => _convertInputValueForQcsdk(comps[idx] || { type: "uint256" }, x));
163
+ }
164
+ if (v && typeof v === "object") {
165
+ const out = {};
166
+ for (let i = 0; i < comps.length; i++) {
167
+ const c = comps[i];
168
+ const name = c && typeof c.name === "string" && c.name ? c.name : String(i);
169
+ out[name] = _convertInputValueForQcsdk(c, v[name] != null ? v[name] : v[String(i)]);
170
+ }
171
+ return out;
172
+ }
173
+ return v;
174
+ }
175
+
176
+ // Fixed bytes
177
+ const mBytes = type.match(/^bytes(\d+)$/);
178
+ if (mBytes) {
179
+ return _fixedBytesToUint256Decimal(type, v);
180
+ }
181
+
182
+ return v;
183
+ }
184
+
185
+ function _convertOutputValueFromQcsdk(param, value) {
186
+ const type = String(param && param.type ? param.type : "");
187
+
188
+ if (type.endsWith("]")) {
189
+ const innerType = type.slice(0, type.lastIndexOf("["));
190
+ const innerParam = { ...(param || {}), type: innerType };
191
+ if (!Array.isArray(value)) return value;
192
+ return value.map((x) => _convertOutputValueFromQcsdk(innerParam, x));
193
+ }
194
+
195
+ if (type === "tuple") {
196
+ const comps = Array.isArray(param && param.components) ? param.components : [];
197
+ if (Array.isArray(value)) {
198
+ return value.map((x, idx) => _convertOutputValueFromQcsdk(comps[idx] || { type: "uint256" }, x));
199
+ }
200
+ if (value && typeof value === "object") {
201
+ const out = {};
202
+ for (let i = 0; i < comps.length; i++) {
203
+ const c = comps[i];
204
+ const name = c && typeof c.name === "string" && c.name ? c.name : String(i);
205
+ const raw = Object.prototype.hasOwnProperty.call(value, name)
206
+ ? value[name]
207
+ : Object.prototype.hasOwnProperty.call(value, String(i))
208
+ ? value[String(i)]
209
+ : undefined;
210
+ out[name] = _convertOutputValueFromQcsdk(c, raw);
211
+ }
212
+ return out;
213
+ }
214
+ return value;
215
+ }
216
+
217
+ const mBytes = type.match(/^bytes(\d+)$/);
218
+ if (mBytes) {
219
+ return _uint256ToFixedBytesHex(type, value);
220
+ }
221
+
222
+ // Normalize integer outputs to bigint for consistency with core typings.
223
+ if (type === "uint" || type.startsWith("uint") || type === "int" || type.startsWith("int")) {
224
+ if (value == null) return value;
225
+ if (typeof value === "bigint") return value;
226
+ try {
227
+ return BigInt(value);
228
+ } catch {
229
+ return value;
230
+ }
231
+ }
232
+
233
+ return value;
234
+ }
235
+
51
236
  class Interface {
52
237
  /**
53
238
  * @param {any[]|Interface} abi
@@ -55,6 +240,8 @@ class Interface {
55
240
  constructor(abi) {
56
241
  this.abi = _asAbiArray(abi);
57
242
  this._abiJson = JSON.stringify(this.abi);
243
+ this._qcsdkAbi = _normalizeAbiForQcsdk(this.abi);
244
+ this._qcsdkAbiJson = JSON.stringify(this._qcsdkAbi);
58
245
  }
59
246
 
60
247
  /**
@@ -65,6 +252,26 @@ class Interface {
65
252
  return this._abiJson;
66
253
  }
67
254
 
255
+ /**
256
+ * Internal: normalized ABI JSON for qcsdk.
257
+ * @returns {string}
258
+ */
259
+ _qcsdkFormatJson() {
260
+ return this._qcsdkAbiJson;
261
+ }
262
+
263
+ /**
264
+ * Internal: normalize argument values for qcsdk based on original ABI params.
265
+ * @param {Array<any>} params
266
+ * @param {Array<any>} values
267
+ * @returns {Array<any>}
268
+ */
269
+ _qcsdkNormalizeValues(params, values) {
270
+ const ps = Array.isArray(params) ? params : [];
271
+ const vs = Array.isArray(values) ? values : [];
272
+ return ps.map((p, i) => _convertInputValueForQcsdk(p, vs[i]));
273
+ }
274
+
68
275
  /**
69
276
  * Basic formatter (ethers supports multiple formats).
70
277
  * @param {string=} format
@@ -130,8 +337,17 @@ class Interface {
130
337
  _requireInitialized();
131
338
  const name = typeof functionFragment === "string" ? functionFragment : functionFragment?.name;
132
339
  assertArgument(typeof name === "string" && name.length > 0, "invalid function", "functionFragment", functionFragment);
133
- const args = _sanitizeArgs(values);
134
- const res = qcsdk.packMethodData(this._abiJson, name, ...args);
340
+ const frag = this.getFunction(name);
341
+ const inputs = Array.isArray(frag.inputs) ? frag.inputs : [];
342
+ const rawArgs = Array.isArray(values) ? values : [];
343
+
344
+ // Fallback for complex ABI surfaces where qcsdk packing is unreliable.
345
+ if (jsAbi.needsJsAbi(inputs)) {
346
+ return jsAbi.encodeFunctionData(name, inputs, rawArgs);
347
+ }
348
+
349
+ const args = inputs.map((p, idx) => _convertInputValueForQcsdk(p, rawArgs[idx]));
350
+ const res = qcsdk.packMethodData(this._qcsdkAbiJson, name, ...args);
135
351
  if (!res || typeof res.error !== "string") throw makeError("packMethodData failed", "UNKNOWN_ERROR", {});
136
352
  if (res.error) throw makeError(res.error, "UNKNOWN_ERROR", { operation: "packMethodData", function: name });
137
353
  return normalizeHex(res.result);
@@ -148,11 +364,24 @@ class Interface {
148
364
  const name = typeof functionFragment === "string" ? functionFragment : functionFragment?.name;
149
365
  assertArgument(typeof name === "string" && name.length > 0, "invalid function", "functionFragment", functionFragment);
150
366
  assertArgument(typeof data === "string", "data must be a hex string", "data", data);
151
- const res = qcsdk.unpackMethodData(this._abiJson, name, data);
367
+ const frag = this.getFunction(name);
368
+ const outputs = Array.isArray(frag.outputs) ? frag.outputs : [];
369
+
370
+ // Fallback for complex output surfaces.
371
+ if (jsAbi.needsJsAbi(outputs)) {
372
+ return jsAbi.decodeFunctionResult(outputs, data);
373
+ }
374
+
375
+ const res = qcsdk.unpackMethodData(this._qcsdkAbiJson, name, data);
152
376
  if (!res || typeof res.error !== "string") throw makeError("unpackMethodData failed", "UNKNOWN_ERROR", {});
153
377
  if (res.error) throw makeError(res.error, "UNKNOWN_ERROR", { operation: "unpackMethodData", function: name });
154
378
  try {
155
- return JSON.parse(res.result);
379
+ const decoded = JSON.parse(res.result);
380
+ // qcsdk generally returns an array for function outputs; convert bytesN back to hex.
381
+ if (Array.isArray(decoded)) {
382
+ return decoded.map((v, i) => _convertOutputValueFromQcsdk(outputs[i] || { type: "uint256" }, v));
383
+ }
384
+ return _convertOutputValueFromQcsdk(outputs[0] || { type: "uint256" }, decoded);
156
385
  } catch {
157
386
  return res.result;
158
387
  }
@@ -168,8 +397,11 @@ class Interface {
168
397
  _requireInitialized();
169
398
  const name = typeof eventFragment === "string" ? eventFragment : eventFragment?.name;
170
399
  assertArgument(typeof name === "string" && name.length > 0, "invalid event", "eventFragment", eventFragment);
171
- const args = _sanitizeArgs(values);
172
- const res = qcsdk.encodeEventLog(this._abiJson, name, ...args);
400
+ const frag = this.getEvent(name);
401
+ const inputs = Array.isArray(frag.inputs) ? frag.inputs : [];
402
+ const rawArgs = Array.isArray(values) ? values : [];
403
+ const args = inputs.map((p, idx) => _convertInputValueForQcsdk(p, rawArgs[idx]));
404
+ const res = qcsdk.encodeEventLog(this._qcsdkAbiJson, name, ...args);
173
405
  if (!res || typeof res.error !== "string") throw makeError("encodeEventLog failed", "UNKNOWN_ERROR", {});
174
406
  if (res.error) throw makeError(res.error, "UNKNOWN_ERROR", { operation: "encodeEventLog", event: name });
175
407
  return res.result;
@@ -188,11 +420,17 @@ class Interface {
188
420
  assertArgument(typeof name === "string" && name.length > 0, "invalid event", "eventFragment", eventFragment);
189
421
  assertArgument(Array.isArray(topics), "topics must be an array", "topics", topics);
190
422
  assertArgument(typeof data === "string", "data must be a string", "data", data);
191
- const res = qcsdk.decodeEventLog(this._abiJson, name, topics, data);
423
+ const frag = this.getEvent(name);
424
+ const inputs = Array.isArray(frag.inputs) ? frag.inputs : [];
425
+ const res = qcsdk.decodeEventLog(this._qcsdkAbiJson, name, topics, data);
192
426
  if (!res || typeof res.error !== "string") throw makeError("decodeEventLog failed", "UNKNOWN_ERROR", {});
193
427
  if (res.error) throw makeError(res.error, "UNKNOWN_ERROR", { operation: "decodeEventLog", event: name });
194
428
  try {
195
- return JSON.parse(res.result);
429
+ const decoded = JSON.parse(res.result);
430
+ if (Array.isArray(decoded)) {
431
+ return decoded.map((v, i) => _convertOutputValueFromQcsdk(inputs[i] || { type: "uint256" }, v));
432
+ }
433
+ return decoded;
196
434
  } catch {
197
435
  return res.result;
198
436
  }