quantumcoin 7.0.2 → 7.0.4
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.
- package/.github/workflows/publish-npmjs.yaml +22 -22
- package/.gitignore +15 -15
- package/LICENSE +21 -21
- package/README-SDK.md +756 -756
- package/README.md +165 -152
- package/SPEC.md +3845 -3845
- package/config.d.ts +50 -50
- package/config.js +115 -115
- package/examples/AllSolidityTypes.sol +184 -184
- package/examples/SimpleIERC20.sol +74 -74
- package/examples/events.js +41 -35
- package/examples/events.ts +35 -0
- package/examples/example-generator-sdk-js.js +100 -95
- package/examples/example-generator-sdk-js.ts +77 -0
- package/examples/example-generator-sdk-ts.js +100 -95
- package/examples/example-generator-sdk-ts.ts +77 -0
- package/examples/example.js +72 -61
- package/examples/example.ts +61 -0
- package/examples/offline-signing.js +79 -0
- package/examples/offline-signing.ts +66 -0
- package/examples/package-lock.json +48 -57
- package/examples/package.json +32 -16
- package/examples/read-operations.js +32 -27
- package/examples/read-operations.ts +31 -0
- package/examples/sdk-generator-erc20.inline.json +251 -251
- package/examples/solidity-types.ts +43 -43
- package/examples/wallet-offline.js +35 -29
- package/examples/wallet-offline.ts +34 -0
- package/generate-sdk.js +1824 -1383
- package/index.js +12 -12
- package/package.json +95 -75
- package/scripts/copy-declarations.js +31 -0
- package/scripts/run-all-one-by-one.js +151 -0
- package/src/abi/fragments.d.ts +42 -42
- package/src/abi/fragments.js +63 -63
- package/src/abi/index.d.ts +13 -13
- package/src/abi/index.js +9 -9
- package/src/abi/interface.d.ts +128 -132
- package/src/abi/interface.js +590 -590
- package/src/abi/js-abi-coder.d.ts +8 -0
- package/src/abi/js-abi-coder.js +474 -474
- package/src/constants.d.ts +66 -61
- package/src/constants.js +101 -94
- package/src/contract/contract-factory.d.ts +28 -28
- package/src/contract/contract-factory.js +105 -105
- package/src/contract/contract.d.ts +113 -105
- package/src/contract/contract.js +354 -312
- package/src/contract/index.d.ts +9 -9
- package/src/contract/index.js +9 -9
- package/src/errors/index.d.ts +92 -92
- package/src/errors/index.js +188 -188
- package/src/generator/index.d.ts +74 -0
- package/src/generator/index.js +1404 -1201
- package/src/index.d.ts +125 -127
- package/src/index.js +41 -41
- package/src/internal/hex.d.ts +61 -61
- package/src/internal/hex.js +144 -144
- package/src/providers/extra-providers.d.ts +139 -128
- package/src/providers/extra-providers.js +600 -575
- package/src/providers/index.d.ts +17 -16
- package/src/providers/index.js +10 -10
- package/src/providers/json-rpc-provider.d.ts +12 -12
- package/src/providers/json-rpc-provider.js +79 -79
- package/src/providers/provider.d.ts +207 -196
- package/src/providers/provider.js +392 -359
- package/src/types/index.d.ts +214 -462
- package/src/types/index.js +9 -9
- package/src/utils/address.d.ts +72 -72
- package/src/utils/address.js +181 -182
- package/src/utils/encoding.d.ts +120 -120
- package/src/utils/encoding.js +306 -306
- package/src/utils/hashing.d.ts +82 -76
- package/src/utils/hashing.js +313 -298
- package/src/utils/index.d.ts +65 -55
- package/src/utils/index.js +13 -13
- package/src/utils/result.d.ts +57 -57
- package/src/utils/result.js +128 -128
- package/src/utils/rlp.d.ts +12 -12
- package/src/utils/rlp.js +200 -200
- package/src/utils/units.d.ts +29 -29
- package/src/utils/units.js +107 -107
- package/src/wallet/index.d.ts +10 -10
- package/src/wallet/index.js +8 -8
- package/src/wallet/wallet.d.ts +160 -160
- package/src/wallet/wallet.js +483 -489
- package/test/e2e/all-solidity-types.dynamic.test.js +207 -200
- package/test/e2e/all-solidity-types.dynamic.test.ts +191 -0
- package/test/e2e/all-solidity-types.fixtures.js +231 -231
- package/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +387 -361
- package/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +350 -0
- package/test/e2e/helpers.js +59 -47
- package/test/e2e/signing-context-and-fee.e2e.test.js +137 -0
- package/test/e2e/signing-context-and-fee.e2e.test.ts +128 -0
- package/test/e2e/simple-erc20.generated-sdks.e2e.test.js +168 -144
- package/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +141 -0
- package/test/e2e/transactional.test.js +245 -191
- package/test/e2e/transactional.test.ts +208 -0
- package/test/e2e/typed-generator.e2e.test.js +407 -402
- package/test/e2e/typed-generator.e2e.test.ts +337 -0
- package/test/fixtures/ConstructorParam.sol +23 -23
- package/test/fixtures/MultiContracts.sol +37 -37
- package/test/fixtures/SimpleStorage.sol +18 -18
- package/test/fixtures/StakingContract.abi.json +1 -1
- package/test/integration/ipc-provider.test.js +49 -44
- package/test/integration/ipc-provider.test.ts +44 -0
- package/test/integration/provider.test.js +88 -72
- package/test/integration/provider.test.ts +85 -0
- package/test/integration/ws-provider.test.js +41 -33
- package/test/integration/ws-provider.test.ts +38 -0
- package/test/security/malformed-input.test.js +37 -31
- package/test/security/malformed-input.test.ts +35 -0
- package/test/unit/_encrypted-output.txt +6 -0
- package/test/unit/_log-encrypted-jsons.js +45 -0
- package/test/unit/_write-keystore-fixture.js +16 -0
- package/test/unit/abi-interface.test.js +103 -98
- package/test/unit/abi-interface.test.ts +102 -0
- package/test/unit/address-wallet.test.js +355 -257
- package/test/unit/address-wallet.test.ts +342 -0
- package/test/unit/browser-provider.test.js +85 -82
- package/test/unit/browser-provider.test.ts +79 -0
- package/test/unit/contract.test.js +85 -82
- package/test/unit/contract.test.ts +83 -0
- package/test/unit/encoding-units-rlp.test.js +92 -89
- package/test/unit/encoding-units-rlp.test.ts +91 -0
- package/test/unit/errors.test.js +77 -74
- package/test/unit/errors.test.ts +76 -0
- package/test/unit/filter-by-blockhash.test.js +55 -52
- package/test/unit/filter-by-blockhash.test.ts +54 -0
- package/test/unit/fixtures/encrypted-keystores-48-32-36.js +9 -0
- package/test/unit/generate-contract-cli.test.js +42 -39
- package/test/unit/generate-contract-cli.test.ts +41 -0
- package/test/unit/generate-sdk-artifacts-json.test.js +113 -110
- package/test/unit/generate-sdk-artifacts-json.test.ts +110 -0
- package/test/unit/generator.test.js +102 -98
- package/test/unit/generator.test.ts +101 -0
- package/test/unit/hashing.test.js +68 -54
- package/test/unit/hashing.test.ts +67 -0
- package/test/unit/init.test.js +39 -36
- package/test/unit/init.test.ts +38 -0
- package/test/unit/interface.test.js +56 -53
- package/test/unit/interface.test.ts +54 -0
- package/test/unit/internal-hex.test.js +50 -47
- package/test/unit/internal-hex.test.ts +49 -0
- package/test/unit/populate-transaction.test.js +65 -0
- package/test/unit/populate-transaction.test.ts +64 -0
- package/test/unit/providers.test.js +200 -144
- package/test/unit/providers.test.ts +196 -0
- package/test/unit/result.test.js +80 -77
- package/test/unit/result.test.ts +79 -0
- package/test/unit/solidity-types.test.js +49 -46
- package/test/unit/solidity-types.test.ts +39 -0
- package/test/unit/utils.test.js +57 -54
- package/test/unit/utils.test.ts +56 -0
- package/test/verbose-logger.js +74 -0
- package/tsconfig.build.json +14 -0
package/src/types/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Runtime placeholder for `require("quantumcoin/types")`.
|
|
3
|
-
*
|
|
4
|
-
* This module is primarily for TypeScript type exports (`index.d.ts`).
|
|
5
|
-
* At runtime, it intentionally exports nothing.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
module.exports = {};
|
|
9
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Runtime placeholder for `require("quantumcoin/types")`.
|
|
3
|
+
*
|
|
4
|
+
* This module is primarily for TypeScript type exports (`index.d.ts`).
|
|
5
|
+
* At runtime, it intentionally exports nothing.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
module.exports = {};
|
|
9
|
+
|
package/src/utils/address.d.ts
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks if string is a valid address (32 bytes, 66 hex characters).
|
|
3
|
-
* @param {string} address
|
|
4
|
-
* @returns {boolean}
|
|
5
|
-
*/
|
|
6
|
-
export function isAddress(address: string): boolean;
|
|
7
|
-
/**
|
|
8
|
-
* Returns normalized address.
|
|
9
|
-
* Note: QuantumCoin checksumming uses QuantumCoin conventions; currently this normalizes to lowercase.
|
|
10
|
-
* @param {string} address
|
|
11
|
-
* @returns {string}
|
|
12
|
-
*/
|
|
13
|
-
export function getAddress(address: string): string;
|
|
14
|
-
/**
|
|
15
|
-
* Returns true if value is an object implementing Addressable (has getAddress()).
|
|
16
|
-
* @param {any} value
|
|
17
|
-
* @returns {boolean}
|
|
18
|
-
*/
|
|
19
|
-
export function isAddressable(value: any): boolean;
|
|
20
|
-
/**
|
|
21
|
-
* Resolve an AddressLike into a string address.
|
|
22
|
-
* For QuantumCoin, ENS is not supported.
|
|
23
|
-
* @param {any} target
|
|
24
|
-
* @returns {string|Promise<string>}
|
|
25
|
-
*/
|
|
26
|
-
export function resolveAddress(target: any): string | Promise<string>;
|
|
27
|
-
/**
|
|
28
|
-
* Calculates contract address from deployer and nonce.
|
|
29
|
-
* @param {{ from: string, nonce: number }} tx
|
|
30
|
-
* @returns {string}
|
|
31
|
-
*/
|
|
32
|
-
export function getContractAddress(tx: {
|
|
33
|
-
from: string;
|
|
34
|
-
nonce: number;
|
|
35
|
-
}): string;
|
|
36
|
-
/**
|
|
37
|
-
* Alias for getContractAddress.
|
|
38
|
-
* @param {{ from: string, nonce: number }} tx
|
|
39
|
-
* @returns {string}
|
|
40
|
-
*/
|
|
41
|
-
export function getCreateAddress(tx: {
|
|
42
|
-
from: string;
|
|
43
|
-
nonce: number;
|
|
44
|
-
}): string;
|
|
45
|
-
/**
|
|
46
|
-
* Calculates CREATE2 contract address.
|
|
47
|
-
* @param {string} from
|
|
48
|
-
* @param {string} salt
|
|
49
|
-
* @param {string} initCodeHash
|
|
50
|
-
* @returns {string}
|
|
51
|
-
*/
|
|
52
|
-
export function getCreate2Address(from: string, salt: string, initCodeHash: string): string;
|
|
53
|
-
/**
|
|
54
|
-
* Computes address from a public key.
|
|
55
|
-
* @param {string|Uint8Array} key
|
|
56
|
-
* @returns {string}
|
|
57
|
-
*/
|
|
58
|
-
export function computeAddress(key: string | Uint8Array): string;
|
|
59
|
-
/**
|
|
60
|
-
* Verifies a message signature and recovers the address.
|
|
61
|
-
* @param {string|Uint8Array} message
|
|
62
|
-
* @param {string} signature Hex string signature
|
|
63
|
-
* @returns {string}
|
|
64
|
-
*/
|
|
65
|
-
export function verifyMessage(message: string | Uint8Array, signature: string): string;
|
|
66
|
-
/**
|
|
67
|
-
* Recovers the address from a message signature.
|
|
68
|
-
* @param {string|Uint8Array} message
|
|
69
|
-
* @param {string} signature Hex string signature
|
|
70
|
-
* @returns {string}
|
|
71
|
-
*/
|
|
72
|
-
export function recoverAddress(message: string | Uint8Array, signature: string): string;
|
|
1
|
+
/**
|
|
2
|
+
* Checks if string is a valid address (32 bytes, 66 hex characters).
|
|
3
|
+
* @param {string} address
|
|
4
|
+
* @returns {boolean}
|
|
5
|
+
*/
|
|
6
|
+
export function isAddress(address: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Returns normalized address.
|
|
9
|
+
* Note: QuantumCoin checksumming uses QuantumCoin conventions; currently this normalizes to lowercase.
|
|
10
|
+
* @param {string} address
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
export function getAddress(address: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if value is an object implementing Addressable (has getAddress()).
|
|
16
|
+
* @param {any} value
|
|
17
|
+
* @returns {boolean}
|
|
18
|
+
*/
|
|
19
|
+
export function isAddressable(value: any): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Resolve an AddressLike into a string address.
|
|
22
|
+
* For QuantumCoin, ENS is not supported.
|
|
23
|
+
* @param {any} target
|
|
24
|
+
* @returns {string|Promise<string>}
|
|
25
|
+
*/
|
|
26
|
+
export function resolveAddress(target: any): string | Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Calculates contract address from deployer and nonce.
|
|
29
|
+
* @param {{ from: string, nonce: number }} tx
|
|
30
|
+
* @returns {string}
|
|
31
|
+
*/
|
|
32
|
+
export function getContractAddress(tx: {
|
|
33
|
+
from: string;
|
|
34
|
+
nonce: number;
|
|
35
|
+
}): string;
|
|
36
|
+
/**
|
|
37
|
+
* Alias for getContractAddress.
|
|
38
|
+
* @param {{ from: string, nonce: number }} tx
|
|
39
|
+
* @returns {string}
|
|
40
|
+
*/
|
|
41
|
+
export function getCreateAddress(tx: {
|
|
42
|
+
from: string;
|
|
43
|
+
nonce: number;
|
|
44
|
+
}): string;
|
|
45
|
+
/**
|
|
46
|
+
* Calculates CREATE2 contract address.
|
|
47
|
+
* @param {string} from
|
|
48
|
+
* @param {string} salt
|
|
49
|
+
* @param {string} initCodeHash
|
|
50
|
+
* @returns {string}
|
|
51
|
+
*/
|
|
52
|
+
export function getCreate2Address(from: string, salt: string, initCodeHash: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* Computes address from a public key.
|
|
55
|
+
* @param {string|Uint8Array} key
|
|
56
|
+
* @returns {string}
|
|
57
|
+
*/
|
|
58
|
+
export function computeAddress(key: string | Uint8Array): string;
|
|
59
|
+
/**
|
|
60
|
+
* Verifies a message signature and recovers the address.
|
|
61
|
+
* @param {string|Uint8Array} message
|
|
62
|
+
* @param {string} signature Hex string signature
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
export function verifyMessage(message: string | Uint8Array, signature: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Recovers the address from a message signature.
|
|
68
|
+
* @param {string|Uint8Array} message
|
|
69
|
+
* @param {string} signature Hex string signature
|
|
70
|
+
* @returns {string}
|
|
71
|
+
*/
|
|
72
|
+
export function recoverAddress(message: string | Uint8Array, signature: string): string;
|
package/src/utils/address.js
CHANGED
|
@@ -1,182 +1,181 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Address utilities (QuantumCoin 32-byte address format).
|
|
3
|
-
*
|
|
4
|
-
* QuantumCoin addresses are 32 bytes (66 hex characters including 0x).
|
|
5
|
-
* Most helpers here are thin wrappers around `quantum-coin-js-sdk`.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const qcsdk = require("quantum-coin-js-sdk");
|
|
9
|
-
const { assertArgument, makeError } = require("../errors");
|
|
10
|
-
const { arrayify, bytesToHex, hexToBytes, isHexString, normalizeHex } = require("../internal/hex");
|
|
11
|
-
const {
|
|
12
|
-
|
|
13
|
-
function _requireInitialized() {
|
|
14
|
-
// The spec requires Initialize() to be called before using the SDK.
|
|
15
|
-
// config.js tracks initialization state for this package.
|
|
16
|
-
// eslint-disable-next-line global-require
|
|
17
|
-
const { isInitialized } = require("../../config");
|
|
18
|
-
if (!isInitialized()) {
|
|
19
|
-
throw makeError("QuantumCoin SDK not initialized. Call Initialize() first.", "UNKNOWN_ERROR", {
|
|
20
|
-
operation: "address-utils",
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Checks if string is a valid address (32 bytes, 66 hex characters).
|
|
27
|
-
* @param {string} address
|
|
28
|
-
* @returns {boolean}
|
|
29
|
-
*/
|
|
30
|
-
function isAddress(address) {
|
|
31
|
-
if (typeof address !== "string") return false;
|
|
32
|
-
_requireInitialized();
|
|
33
|
-
const result = qcsdk.isAddressValid(address);
|
|
34
|
-
return result === true;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Returns normalized address.
|
|
39
|
-
* Note: QuantumCoin checksumming uses QuantumCoin conventions; currently this normalizes to lowercase.
|
|
40
|
-
* @param {string} address
|
|
41
|
-
* @returns {string}
|
|
42
|
-
*/
|
|
43
|
-
function getAddress(address) {
|
|
44
|
-
assertArgument(typeof address === "string", "address must be a string", "address", address);
|
|
45
|
-
_requireInitialized();
|
|
46
|
-
const norm = normalizeHex(address);
|
|
47
|
-
assertArgument(qcsdk.isAddressValid(norm) === true, "invalid address", "address", address);
|
|
48
|
-
return norm;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Returns true if value is an object implementing Addressable (has getAddress()).
|
|
53
|
-
* @param {any} value
|
|
54
|
-
* @returns {boolean}
|
|
55
|
-
*/
|
|
56
|
-
function isAddressable(value) {
|
|
57
|
-
return Boolean(value && typeof value === "object" && typeof value.getAddress === "function");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Resolve an AddressLike into a string address.
|
|
62
|
-
* For QuantumCoin, ENS is not supported.
|
|
63
|
-
* @param {any} target
|
|
64
|
-
* @returns {string|Promise<string>}
|
|
65
|
-
*/
|
|
66
|
-
function resolveAddress(target) {
|
|
67
|
-
if (typeof target === "string") return getAddress(target);
|
|
68
|
-
if (isAddressable(target)) return target.getAddress();
|
|
69
|
-
if (target && typeof target.then === "function") {
|
|
70
|
-
return Promise.resolve(target).then((v) => resolveAddress(v));
|
|
71
|
-
}
|
|
72
|
-
throw makeError("unsupported AddressLike", "INVALID_ARGUMENT", { target });
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Calculates contract address from deployer and nonce.
|
|
77
|
-
* @param {{ from: string, nonce: number }} tx
|
|
78
|
-
* @returns {string}
|
|
79
|
-
*/
|
|
80
|
-
function getContractAddress(tx) {
|
|
81
|
-
_requireInitialized();
|
|
82
|
-
assertArgument(tx && typeof tx === "object", "invalid tx", "tx", tx);
|
|
83
|
-
const from = getAddress(tx.from);
|
|
84
|
-
assertArgument(Number.isInteger(tx.nonce) && tx.nonce >= 0, "invalid nonce", "nonce", tx.nonce);
|
|
85
|
-
const out = qcsdk.createAddress(from, tx.nonce);
|
|
86
|
-
if (typeof out !== "string") throw makeError("createAddress failed", "UNKNOWN_ERROR", { from, nonce: tx.nonce });
|
|
87
|
-
return normalizeHex(out);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Alias for getContractAddress.
|
|
92
|
-
* @param {{ from: string, nonce: number }} tx
|
|
93
|
-
* @returns {string}
|
|
94
|
-
*/
|
|
95
|
-
function getCreateAddress(tx) {
|
|
96
|
-
return getContractAddress(tx);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Calculates CREATE2 contract address.
|
|
101
|
-
* @param {string} from
|
|
102
|
-
* @param {string} salt
|
|
103
|
-
* @param {string} initCodeHash
|
|
104
|
-
* @returns {string}
|
|
105
|
-
*/
|
|
106
|
-
function getCreate2Address(from, salt, initCodeHash) {
|
|
107
|
-
_requireInitialized();
|
|
108
|
-
const f = getAddress(from);
|
|
109
|
-
assertArgument(isHexString(salt), "invalid salt", "salt", salt);
|
|
110
|
-
assertArgument(isHexString(initCodeHash), "invalid initCodeHash", "initCodeHash", initCodeHash);
|
|
111
|
-
const out = qcsdk.createAddress2(f, normalizeHex(salt), normalizeHex(initCodeHash));
|
|
112
|
-
if (typeof out !== "string") throw makeError("createAddress2 failed", "UNKNOWN_ERROR", { from: f });
|
|
113
|
-
return normalizeHex(out);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Computes address from a public key.
|
|
118
|
-
* @param {string|Uint8Array} key
|
|
119
|
-
* @returns {string}
|
|
120
|
-
*/
|
|
121
|
-
function computeAddress(key) {
|
|
122
|
-
_requireInitialized();
|
|
123
|
-
const bytes = typeof key === "string" ? hexToBytes(key) : arrayify(key);
|
|
124
|
-
const pubArr = Array.from(bytes);
|
|
125
|
-
const out = qcsdk.addressFromPublicKey(pubArr);
|
|
126
|
-
if (typeof out !== "string") throw makeError("addressFromPublicKey failed", "UNKNOWN_ERROR", {});
|
|
127
|
-
return normalizeHex(out);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function _digestMessage(message) {
|
|
131
|
-
const
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
assertArgument(
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
*
|
|
146
|
-
* @param {string
|
|
147
|
-
* @
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
*
|
|
156
|
-
* @param {string
|
|
157
|
-
* @
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Address utilities (QuantumCoin 32-byte address format).
|
|
3
|
+
*
|
|
4
|
+
* QuantumCoin addresses are 32 bytes (66 hex characters including 0x).
|
|
5
|
+
* Most helpers here are thin wrappers around `quantum-coin-js-sdk`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const qcsdk = require("quantum-coin-js-sdk");
|
|
9
|
+
const { assertArgument, makeError } = require("../errors");
|
|
10
|
+
const { arrayify, bytesToHex, hexToBytes, isHexString, normalizeHex } = require("../internal/hex");
|
|
11
|
+
const { hashMessage } = require("./hashing");
|
|
12
|
+
|
|
13
|
+
function _requireInitialized() {
|
|
14
|
+
// The spec requires Initialize() to be called before using the SDK.
|
|
15
|
+
// config.js tracks initialization state for this package.
|
|
16
|
+
// eslint-disable-next-line global-require
|
|
17
|
+
const { isInitialized } = require("../../config");
|
|
18
|
+
if (!isInitialized()) {
|
|
19
|
+
throw makeError("QuantumCoin SDK not initialized. Call Initialize() first.", "UNKNOWN_ERROR", {
|
|
20
|
+
operation: "address-utils",
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Checks if string is a valid address (32 bytes, 66 hex characters).
|
|
27
|
+
* @param {string} address
|
|
28
|
+
* @returns {boolean}
|
|
29
|
+
*/
|
|
30
|
+
function isAddress(address) {
|
|
31
|
+
if (typeof address !== "string") return false;
|
|
32
|
+
_requireInitialized();
|
|
33
|
+
const result = qcsdk.isAddressValid(address);
|
|
34
|
+
return result === true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Returns normalized address.
|
|
39
|
+
* Note: QuantumCoin checksumming uses QuantumCoin conventions; currently this normalizes to lowercase.
|
|
40
|
+
* @param {string} address
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
43
|
+
function getAddress(address) {
|
|
44
|
+
assertArgument(typeof address === "string", "address must be a string", "address", address);
|
|
45
|
+
_requireInitialized();
|
|
46
|
+
const norm = normalizeHex(address);
|
|
47
|
+
assertArgument(qcsdk.isAddressValid(norm) === true, "invalid address", "address", address);
|
|
48
|
+
return norm;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns true if value is an object implementing Addressable (has getAddress()).
|
|
53
|
+
* @param {any} value
|
|
54
|
+
* @returns {boolean}
|
|
55
|
+
*/
|
|
56
|
+
function isAddressable(value) {
|
|
57
|
+
return Boolean(value && typeof value === "object" && typeof value.getAddress === "function");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Resolve an AddressLike into a string address.
|
|
62
|
+
* For QuantumCoin, ENS is not supported.
|
|
63
|
+
* @param {any} target
|
|
64
|
+
* @returns {string|Promise<string>}
|
|
65
|
+
*/
|
|
66
|
+
function resolveAddress(target) {
|
|
67
|
+
if (typeof target === "string") return getAddress(target);
|
|
68
|
+
if (isAddressable(target)) return target.getAddress();
|
|
69
|
+
if (target && typeof target.then === "function") {
|
|
70
|
+
return Promise.resolve(target).then((v) => resolveAddress(v));
|
|
71
|
+
}
|
|
72
|
+
throw makeError("unsupported AddressLike", "INVALID_ARGUMENT", { target });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Calculates contract address from deployer and nonce.
|
|
77
|
+
* @param {{ from: string, nonce: number }} tx
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
function getContractAddress(tx) {
|
|
81
|
+
_requireInitialized();
|
|
82
|
+
assertArgument(tx && typeof tx === "object", "invalid tx", "tx", tx);
|
|
83
|
+
const from = getAddress(tx.from);
|
|
84
|
+
assertArgument(Number.isInteger(tx.nonce) && tx.nonce >= 0, "invalid nonce", "nonce", tx.nonce);
|
|
85
|
+
const out = qcsdk.createAddress(from, tx.nonce);
|
|
86
|
+
if (typeof out !== "string") throw makeError("createAddress failed", "UNKNOWN_ERROR", { from, nonce: tx.nonce });
|
|
87
|
+
return normalizeHex(out);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Alias for getContractAddress.
|
|
92
|
+
* @param {{ from: string, nonce: number }} tx
|
|
93
|
+
* @returns {string}
|
|
94
|
+
*/
|
|
95
|
+
function getCreateAddress(tx) {
|
|
96
|
+
return getContractAddress(tx);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Calculates CREATE2 contract address.
|
|
101
|
+
* @param {string} from
|
|
102
|
+
* @param {string} salt
|
|
103
|
+
* @param {string} initCodeHash
|
|
104
|
+
* @returns {string}
|
|
105
|
+
*/
|
|
106
|
+
function getCreate2Address(from, salt, initCodeHash) {
|
|
107
|
+
_requireInitialized();
|
|
108
|
+
const f = getAddress(from);
|
|
109
|
+
assertArgument(isHexString(salt), "invalid salt", "salt", salt);
|
|
110
|
+
assertArgument(isHexString(initCodeHash), "invalid initCodeHash", "initCodeHash", initCodeHash);
|
|
111
|
+
const out = qcsdk.createAddress2(f, normalizeHex(salt), normalizeHex(initCodeHash));
|
|
112
|
+
if (typeof out !== "string") throw makeError("createAddress2 failed", "UNKNOWN_ERROR", { from: f });
|
|
113
|
+
return normalizeHex(out);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Computes address from a public key.
|
|
118
|
+
* @param {string|Uint8Array} key
|
|
119
|
+
* @returns {string}
|
|
120
|
+
*/
|
|
121
|
+
function computeAddress(key) {
|
|
122
|
+
_requireInitialized();
|
|
123
|
+
const bytes = typeof key === "string" ? hexToBytes(key) : arrayify(key);
|
|
124
|
+
const pubArr = Array.from(bytes);
|
|
125
|
+
const out = qcsdk.addressFromPublicKey(pubArr);
|
|
126
|
+
if (typeof out !== "string") throw makeError("addressFromPublicKey failed", "UNKNOWN_ERROR", {});
|
|
127
|
+
return normalizeHex(out);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function _digestMessage(message) {
|
|
131
|
+
const digestHex = hashMessage(message);
|
|
132
|
+
const digest = Array.from(hexToBytes(digestHex));
|
|
133
|
+
assertArgument(digest.length === 32, "invalid digest length", "digest", digest.length);
|
|
134
|
+
return digest;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function _signatureToBytes(signature) {
|
|
138
|
+
assertArgument(typeof signature === "string", "signature must be a hex string", "signature", signature);
|
|
139
|
+
assertArgument(isHexString(signature), "invalid signature hex", "signature", signature);
|
|
140
|
+
return Array.from(hexToBytes(signature));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Verifies a message signature and recovers the address.
|
|
145
|
+
* @param {string|Uint8Array} message
|
|
146
|
+
* @param {string} signature Hex string signature
|
|
147
|
+
* @returns {string}
|
|
148
|
+
*/
|
|
149
|
+
function verifyMessage(message, signature) {
|
|
150
|
+
return recoverAddress(message, signature);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Recovers the address from a message signature.
|
|
155
|
+
* @param {string|Uint8Array} message
|
|
156
|
+
* @param {string} signature Hex string signature
|
|
157
|
+
* @returns {string}
|
|
158
|
+
*/
|
|
159
|
+
function recoverAddress(message, signature) {
|
|
160
|
+
_requireInitialized();
|
|
161
|
+
const digest = _digestMessage(message);
|
|
162
|
+
const sigBytes = _signatureToBytes(signature);
|
|
163
|
+
const pubHex = qcsdk.publicKeyFromSignature(digest, sigBytes);
|
|
164
|
+
if (typeof pubHex !== "string") throw makeError("publicKeyFromSignature failed", "UNKNOWN_ERROR", {});
|
|
165
|
+
const addr = computeAddress(pubHex);
|
|
166
|
+
return addr;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
module.exports = {
|
|
170
|
+
isAddress,
|
|
171
|
+
getAddress,
|
|
172
|
+
isAddressable,
|
|
173
|
+
resolveAddress,
|
|
174
|
+
getContractAddress,
|
|
175
|
+
getCreateAddress,
|
|
176
|
+
getCreate2Address,
|
|
177
|
+
computeAddress,
|
|
178
|
+
verifyMessage,
|
|
179
|
+
recoverAddress,
|
|
180
|
+
};
|
|
181
|
+
|