@unlink-xyz/core 0.1.2 → 0.1.3-canary.0877bfe
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/dist/account/{zkAccount.d.ts → account.d.ts} +7 -5
- package/dist/account/account.d.ts.map +1 -0
- package/dist/account/{zkAccount.js → account.js} +57 -43
- package/dist/browser/index.js +108202 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/circuits.json +74 -0
- package/dist/clients/broadcaster.d.ts +7 -2
- package/dist/clients/broadcaster.d.ts.map +1 -1
- package/dist/clients/broadcaster.js +9 -1
- package/dist/clients/http.d.ts +6 -0
- package/dist/clients/http.d.ts.map +1 -1
- package/dist/clients/http.js +24 -9
- package/dist/clients/indexer.d.ts +11 -0
- package/dist/clients/indexer.d.ts.map +1 -1
- package/dist/clients/indexer.js +40 -11
- package/dist/config.d.ts +28 -9
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +33 -26
- package/dist/constants.d.ts +6 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +5 -0
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +5 -2
- package/dist/crypto-adapters/auto-init.d.ts +2 -0
- package/dist/crypto-adapters/auto-init.d.ts.map +1 -0
- package/dist/crypto-adapters/auto-init.js +7 -0
- package/dist/crypto-adapters/index.d.ts +22 -0
- package/dist/crypto-adapters/index.d.ts.map +1 -0
- package/dist/crypto-adapters/index.js +47 -0
- package/dist/crypto-adapters/polyfills.d.ts +5 -0
- package/dist/crypto-adapters/polyfills.d.ts.map +1 -0
- package/dist/crypto-adapters/polyfills.js +8 -0
- package/dist/errors.d.ts +9 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +18 -0
- package/dist/history/index.d.ts +3 -0
- package/dist/history/index.d.ts.map +1 -0
- package/dist/history/index.js +2 -0
- package/dist/history/service.d.ts +46 -0
- package/dist/history/service.d.ts.map +1 -0
- package/dist/history/service.js +354 -0
- package/dist/history/types.d.ts +21 -0
- package/dist/history/types.d.ts.map +1 -0
- package/dist/index.d.ts +12 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -4
- package/dist/keys/address.d.ts +13 -0
- package/dist/keys/address.d.ts.map +1 -0
- package/dist/keys/address.js +55 -0
- package/dist/keys/derive.d.ts +37 -0
- package/dist/keys/derive.d.ts.map +1 -0
- package/dist/keys/derive.js +112 -0
- package/dist/keys/hex.d.ts +17 -0
- package/dist/keys/hex.d.ts.map +1 -0
- package/dist/keys/hex.js +66 -0
- package/dist/keys/index.d.ts +5 -0
- package/dist/keys/index.d.ts.map +1 -0
- package/dist/keys/index.js +4 -0
- package/dist/keys/mnemonic.d.ts +8 -0
- package/dist/keys/mnemonic.d.ts.map +1 -0
- package/dist/keys/mnemonic.js +23 -0
- package/dist/keys.d.ts +4 -1
- package/dist/keys.d.ts.map +1 -1
- package/dist/keys.js +4 -0
- package/dist/prover/config.d.ts +1 -15
- package/dist/prover/config.d.ts.map +1 -1
- package/dist/prover/config.js +1 -11
- package/dist/prover/prover.d.ts +15 -4
- package/dist/prover/prover.d.ts.map +1 -1
- package/dist/prover/prover.js +115 -98
- package/dist/prover/registry.d.ts +3 -30
- package/dist/prover/registry.d.ts.map +1 -1
- package/dist/prover/registry.js +12 -51
- package/dist/state/merkle/hydrator.d.ts.map +1 -1
- package/dist/state/merkle/hydrator.js +3 -2
- package/dist/state/merkle/index.d.ts +1 -1
- package/dist/state/merkle/index.d.ts.map +1 -1
- package/dist/state/merkle/index.js +1 -1
- package/dist/state/merkle/merkle-tree.d.ts +8 -0
- package/dist/state/merkle/merkle-tree.d.ts.map +1 -1
- package/dist/state/merkle/merkle-tree.js +16 -7
- package/dist/state/store/ciphertext-store.d.ts +4 -0
- package/dist/state/store/ciphertext-store.d.ts.map +1 -1
- package/dist/state/store/ciphertext-store.js +12 -0
- package/dist/state/store/history-store.d.ts +24 -0
- package/dist/state/store/history-store.d.ts.map +1 -0
- package/dist/state/store/history-store.js +53 -0
- package/dist/state/store/index.d.ts +3 -2
- package/dist/state/store/index.d.ts.map +1 -1
- package/dist/state/store/index.js +1 -0
- package/dist/state/store/job-store.d.ts +7 -7
- package/dist/state/store/job-store.d.ts.map +1 -1
- package/dist/state/store/job-store.js +65 -39
- package/dist/state/store/jobs.d.ts +65 -18
- package/dist/state/store/jobs.d.ts.map +1 -1
- package/dist/state/store/leaf-store.d.ts.map +1 -1
- package/dist/state/store/leaf-store.js +0 -3
- package/dist/state/store/note-store.d.ts +7 -7
- package/dist/state/store/note-store.d.ts.map +1 -1
- package/dist/state/store/note-store.js +38 -34
- package/dist/state/store/nullifier-store.d.ts +9 -0
- package/dist/state/store/nullifier-store.d.ts.map +1 -1
- package/dist/state/store/nullifier-store.js +32 -2
- package/dist/state/store/records.d.ts +31 -2
- package/dist/state/store/records.d.ts.map +1 -1
- package/dist/state/store/root-store.d.ts.map +1 -1
- package/dist/state/store/root-store.js +0 -4
- package/dist/state/store/store.d.ts +61 -27
- package/dist/state/store/store.d.ts.map +1 -1
- package/dist/state/store/store.js +92 -1
- package/dist/storage/indexeddb.js +1 -1
- package/dist/storage/memory.d.ts.map +1 -1
- package/dist/storage/memory.js +5 -1
- package/dist/transactions/deposit.d.ts +12 -15
- package/dist/transactions/deposit.d.ts.map +1 -1
- package/dist/transactions/deposit.js +203 -152
- package/dist/transactions/index.d.ts +7 -4
- package/dist/transactions/index.d.ts.map +1 -1
- package/dist/transactions/index.js +7 -2
- package/dist/transactions/note-selection.d.ts +17 -0
- package/dist/transactions/note-selection.d.ts.map +1 -0
- package/dist/transactions/note-selection.js +201 -0
- package/dist/transactions/note-sync.d.ts +5 -33
- package/dist/transactions/note-sync.d.ts.map +1 -1
- package/dist/transactions/note-sync.js +320 -155
- package/dist/transactions/reconcile.d.ts +10 -12
- package/dist/transactions/reconcile.d.ts.map +1 -1
- package/dist/transactions/reconcile.js +53 -7
- package/dist/transactions/transact.d.ts +13 -24
- package/dist/transactions/transact.d.ts.map +1 -1
- package/dist/transactions/transact.js +393 -507
- package/dist/transactions/transaction-planner.d.ts +34 -0
- package/dist/transactions/transaction-planner.d.ts.map +1 -0
- package/dist/transactions/transaction-planner.js +116 -0
- package/dist/transactions/transfer-planner.d.ts +36 -0
- package/dist/transactions/transfer-planner.d.ts.map +1 -0
- package/dist/transactions/transfer-planner.js +85 -0
- package/dist/transactions/types/deposit.d.ts +67 -0
- package/dist/transactions/types/deposit.d.ts.map +1 -0
- package/dist/transactions/types/domain.d.ts +67 -0
- package/dist/transactions/types/domain.d.ts.map +1 -0
- package/dist/transactions/types/domain.js +4 -0
- package/dist/transactions/types/index.d.ts +18 -0
- package/dist/transactions/types/index.d.ts.map +1 -0
- package/dist/transactions/types/index.js +17 -0
- package/dist/transactions/types/options.d.ts +45 -0
- package/dist/transactions/types/options.d.ts.map +1 -0
- package/dist/transactions/types/options.js +1 -0
- package/dist/transactions/types/planning.d.ts +80 -0
- package/dist/transactions/types/planning.d.ts.map +1 -0
- package/dist/transactions/types/planning.js +1 -0
- package/dist/transactions/types/state-stores.d.ts +103 -0
- package/dist/transactions/types/state-stores.d.ts.map +1 -0
- package/dist/transactions/types/state-stores.js +1 -0
- package/dist/transactions/types/transact.d.ts +76 -0
- package/dist/transactions/types/transact.d.ts.map +1 -0
- package/dist/transactions/types/transact.js +1 -0
- package/dist/transactions/withdrawal-planner.d.ts +58 -0
- package/dist/transactions/withdrawal-planner.d.ts.map +1 -0
- package/dist/transactions/withdrawal-planner.js +128 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/tsup.browser.config.d.ts +7 -0
- package/dist/tsup.browser.config.d.ts.map +1 -0
- package/dist/tsup.browser.config.js +34 -0
- package/dist/utils/amounts.d.ts +39 -0
- package/dist/utils/amounts.d.ts.map +1 -0
- package/dist/utils/amounts.js +89 -0
- package/dist/utils/async.d.ts +9 -0
- package/dist/utils/async.d.ts.map +1 -1
- package/dist/utils/async.js +24 -0
- package/dist/utils/bigint.js +7 -7
- package/dist/utils/crypto.d.ts +11 -5
- package/dist/utils/crypto.d.ts.map +1 -1
- package/dist/utils/crypto.js +12 -6
- package/dist/utils/format.d.ts +25 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/format.js +33 -0
- package/dist/utils/json-codec.js +1 -1
- package/dist/utils/notes.d.ts +15 -0
- package/dist/utils/notes.d.ts.map +1 -0
- package/dist/utils/notes.js +14 -0
- package/dist/utils/polling.d.ts +5 -0
- package/dist/utils/polling.d.ts.map +1 -1
- package/dist/utils/polling.js +5 -0
- package/dist/utils/random.d.ts +13 -0
- package/dist/utils/random.d.ts.map +1 -0
- package/dist/utils/random.js +27 -0
- package/dist/utils/secure-memory.d.ts +25 -0
- package/dist/utils/secure-memory.d.ts.map +1 -0
- package/dist/utils/secure-memory.js +28 -0
- package/dist/utils/signature.d.ts +6 -0
- package/dist/utils/signature.d.ts.map +1 -1
- package/dist/utils/signature.js +8 -6
- package/dist/utils/validators.d.ts +21 -10
- package/dist/utils/validators.d.ts.map +1 -1
- package/dist/utils/validators.js +37 -11
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +13 -0
- package/package.json +28 -11
- package/.eslintrc.json +0 -4
- package/account/zkAccount.test.ts +0 -316
- package/account/zkAccount.ts +0 -222
- package/clients/broadcaster.ts +0 -67
- package/clients/http.ts +0 -94
- package/clients/indexer.ts +0 -150
- package/config.ts +0 -39
- package/core.ts +0 -17
- package/dist/account/railgun-imports-prototype.d.ts +0 -12
- package/dist/account/railgun-imports-prototype.d.ts.map +0 -1
- package/dist/account/railgun-imports-prototype.js +0 -30
- package/dist/account/zkAccount.d.ts.map +0 -1
- package/dist/key-derivation/babyjubjub.d.ts +0 -9
- package/dist/key-derivation/babyjubjub.d.ts.map +0 -1
- package/dist/key-derivation/babyjubjub.js +0 -9
- package/dist/key-derivation/bech32.d.ts +0 -22
- package/dist/key-derivation/bech32.d.ts.map +0 -1
- package/dist/key-derivation/bech32.js +0 -86
- package/dist/key-derivation/bip32.d.ts +0 -17
- package/dist/key-derivation/bip32.d.ts.map +0 -1
- package/dist/key-derivation/bip32.js +0 -41
- package/dist/key-derivation/bip39.d.ts +0 -22
- package/dist/key-derivation/bip39.d.ts.map +0 -1
- package/dist/key-derivation/bip39.js +0 -56
- package/dist/key-derivation/bytes.d.ts +0 -19
- package/dist/key-derivation/bytes.d.ts.map +0 -1
- package/dist/key-derivation/bytes.js +0 -92
- package/dist/key-derivation/hash.d.ts +0 -3
- package/dist/key-derivation/hash.d.ts.map +0 -1
- package/dist/key-derivation/hash.js +0 -10
- package/dist/key-derivation/index.d.ts +0 -8
- package/dist/key-derivation/index.d.ts.map +0 -1
- package/dist/key-derivation/index.js +0 -7
- package/dist/key-derivation/wallet-node.d.ts +0 -45
- package/dist/key-derivation/wallet-node.d.ts.map +0 -1
- package/dist/key-derivation/wallet-node.js +0 -109
- package/dist/state/ciphertext-store.d.ts +0 -12
- package/dist/state/ciphertext-store.d.ts.map +0 -1
- package/dist/state/ciphertext-store.js +0 -25
- package/dist/state/hydrator.d.ts +0 -16
- package/dist/state/hydrator.d.ts.map +0 -1
- package/dist/state/hydrator.js +0 -18
- package/dist/state/job-store.d.ts +0 -12
- package/dist/state/job-store.d.ts.map +0 -1
- package/dist/state/job-store.js +0 -118
- package/dist/state/jobs.d.ts +0 -50
- package/dist/state/jobs.d.ts.map +0 -1
- package/dist/state/jobs.js +0 -1
- package/dist/state/leaf-store.d.ts +0 -17
- package/dist/state/leaf-store.d.ts.map +0 -1
- package/dist/state/leaf-store.js +0 -35
- package/dist/state/merkle-tree.d.ts +0 -34
- package/dist/state/merkle-tree.d.ts.map +0 -1
- package/dist/state/merkle-tree.js +0 -104
- package/dist/state/note-store.d.ts +0 -37
- package/dist/state/note-store.d.ts.map +0 -1
- package/dist/state/note-store.js +0 -133
- package/dist/state/nullifier-store.d.ts +0 -13
- package/dist/state/nullifier-store.d.ts.map +0 -1
- package/dist/state/nullifier-store.js +0 -21
- package/dist/state/records.d.ts +0 -57
- package/dist/state/records.d.ts.map +0 -1
- package/dist/state/root-store.d.ts +0 -13
- package/dist/state/root-store.d.ts.map +0 -1
- package/dist/state/root-store.js +0 -30
- package/dist/state/store.d.ts +0 -26
- package/dist/state/store.d.ts.map +0 -1
- package/dist/state/store.js +0 -19
- package/dist/state.d.ts +0 -83
- package/dist/state.d.ts.map +0 -1
- package/dist/state.js +0 -171
- package/dist/transactions/shield.d.ts +0 -5
- package/dist/transactions/shield.d.ts.map +0 -1
- package/dist/transactions/shield.js +0 -93
- package/dist/transactions/types.d.ts +0 -114
- package/dist/transactions/types.d.ts.map +0 -1
- package/dist/transactions/utils.d.ts +0 -10
- package/dist/transactions/utils.d.ts.map +0 -1
- package/dist/transactions/utils.js +0 -17
- package/dist/utils/time.d.ts +0 -2
- package/dist/utils/time.d.ts.map +0 -1
- package/dist/utils/time.js +0 -3
- package/dist/utils/witness.d.ts +0 -11
- package/dist/utils/witness.d.ts.map +0 -1
- package/dist/utils/witness.js +0 -19
- package/errors.ts +0 -20
- package/index.ts +0 -17
- package/key-derivation/babyjubjub.ts +0 -11
- package/key-derivation/bech32.test.ts +0 -90
- package/key-derivation/bech32.ts +0 -124
- package/key-derivation/bip32.ts +0 -56
- package/key-derivation/bip39.ts +0 -76
- package/key-derivation/bytes.ts +0 -118
- package/key-derivation/hash.ts +0 -13
- package/key-derivation/index.ts +0 -7
- package/key-derivation/wallet-node.ts +0 -155
- package/keys.ts +0 -47
- package/prover/config.ts +0 -104
- package/prover/index.ts +0 -1
- package/prover/prover.integration.test.ts +0 -162
- package/prover/prover.test.ts +0 -309
- package/prover/prover.ts +0 -405
- package/prover/registry.test.ts +0 -90
- package/prover/registry.ts +0 -82
- package/schema.ts +0 -17
- package/setup-artifacts.sh +0 -57
- package/state/index.ts +0 -2
- package/state/merkle/hydrator.ts +0 -69
- package/state/merkle/index.ts +0 -12
- package/state/merkle/merkle-tree.test.ts +0 -50
- package/state/merkle/merkle-tree.ts +0 -163
- package/state/store/ciphertext-store.ts +0 -28
- package/state/store/index.ts +0 -24
- package/state/store/job-store.ts +0 -162
- package/state/store/jobs.ts +0 -64
- package/state/store/leaf-store.ts +0 -39
- package/state/store/note-store.ts +0 -177
- package/state/store/nullifier-store.ts +0 -39
- package/state/store/records.ts +0 -61
- package/state/store/root-store.ts +0 -34
- package/state/store/store.ts +0 -25
- package/state.test.ts +0 -235
- package/storage/index.ts +0 -3
- package/storage/indexeddb.test.ts +0 -99
- package/storage/indexeddb.ts +0 -235
- package/storage/memory.test.ts +0 -59
- package/storage/memory.ts +0 -93
- package/transactions/deposit.test.ts +0 -160
- package/transactions/deposit.ts +0 -227
- package/transactions/index.ts +0 -20
- package/transactions/note-sync.test.ts +0 -155
- package/transactions/note-sync.ts +0 -452
- package/transactions/reconcile.ts +0 -73
- package/transactions/transact.test.ts +0 -451
- package/transactions/transact.ts +0 -811
- package/transactions/types.ts +0 -141
- package/tsconfig.json +0 -14
- package/types/global.d.ts +0 -15
- package/types.ts +0 -24
- package/utils/async.ts +0 -15
- package/utils/bigint.ts +0 -34
- package/utils/crypto.test.ts +0 -69
- package/utils/crypto.ts +0 -58
- package/utils/json-codec.ts +0 -38
- package/utils/polling.ts +0 -6
- package/utils/signature.ts +0 -16
- package/utils/validators.test.ts +0 -64
- package/utils/validators.ts +0 -86
- /package/dist/{transactions → history}/types.js +0 -0
- /package/dist/{state/records.js → transactions/types/deposit.js} +0 -0
|
@@ -1,22 +1,16 @@
|
|
|
1
|
+
import { type Chain } from "../keys/address.js";
|
|
2
|
+
import { WithdrawalNoteInput } from "../transactions/types/index.js";
|
|
1
3
|
export declare const SNARK_SCALAR_FIELD = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
2
4
|
export declare function ensurePositiveInt(label: string, value: number): void;
|
|
3
5
|
export declare function ensureChainId(chainId: number): void;
|
|
4
6
|
/** Guards master public key strings against empty values. */
|
|
5
7
|
export declare function ensureMpk(mpk: string): void;
|
|
6
8
|
export declare function ensureBigIntString(label: string, value: string): void;
|
|
7
|
-
export declare function ensureAddress(label: string, value: string):
|
|
9
|
+
export declare function ensureAddress(label: string, value: string): `0x${string}`;
|
|
8
10
|
/** Confirms the bigint resides in the SNARK scalar field range. */
|
|
9
11
|
export declare function ensureFieldElement(label: string, value: bigint): void;
|
|
10
12
|
/** Validates withdrawal note public data before encoding calldata. */
|
|
11
|
-
export declare function ensureWithdrawalInput(label: string, withdrawal:
|
|
12
|
-
npk: bigint;
|
|
13
|
-
amount: bigint;
|
|
14
|
-
token: string;
|
|
15
|
-
}): {
|
|
16
|
-
npk: bigint;
|
|
17
|
-
amount: bigint;
|
|
18
|
-
token: string;
|
|
19
|
-
};
|
|
13
|
+
export declare function ensureWithdrawalInput(label: string, withdrawal: WithdrawalNoteInput | undefined): WithdrawalNoteInput | undefined;
|
|
20
14
|
/** Validates note parameters before producing a Poseidon commitment. */
|
|
21
15
|
export declare function ensureNoteCommitmentInput(label: string, note: {
|
|
22
16
|
npk: bigint;
|
|
@@ -27,4 +21,21 @@ export declare function ensureNoteCommitmentInput(label: string, note: {
|
|
|
27
21
|
amount: bigint;
|
|
28
22
|
token: string;
|
|
29
23
|
};
|
|
24
|
+
/**
|
|
25
|
+
* Parsed ZK address containing the master public key, viewing public key,
|
|
26
|
+
* and optional chain info.
|
|
27
|
+
*/
|
|
28
|
+
export type ParsedZkAddress = {
|
|
29
|
+
masterPublicKey: bigint;
|
|
30
|
+
viewingPublicKey: Uint8Array;
|
|
31
|
+
chain?: Chain;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Parses a Bech32m ZK address (0zk1...) into its components.
|
|
35
|
+
*
|
|
36
|
+
* @param value - The Bech32m address string
|
|
37
|
+
* @returns Parsed address with masterPublicKey, viewingPublicKey, and optional chain
|
|
38
|
+
* @throws ValidationError if the address is invalid
|
|
39
|
+
*/
|
|
40
|
+
export declare function parseZkAddress(value: string): ParsedZkAddress;
|
|
30
41
|
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../utils/validators.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../utils/validators.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,KAAK,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAGrE,eAAO,MAAM,kBAAkB,iFAC0E,CAAC;AAE1G,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAI7D;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,QAI5C;AAED,6DAA6D;AAC7D,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,QAIpC;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAU9D;AAID,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,KAAK,MAAM,EAAE,CAOzE;AAED,mEAAmE;AACnE,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAS9D;AAED,sEAAsE;AACtE,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,mBAAmB,GAAG,SAAS,mCAc5C;AAED,wEAAwE;AACxE,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,MAAM,EACb,IAAI,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE;SAAvC,MAAM;YAAU,MAAM;WAAS,MAAM;EAWnD;AAED;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,eAAe,EAAE,MAAM,CAAC;IACxB,gBAAgB,EAAE,UAAU,CAAC;IAC7B,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAe7D"}
|
package/dist/utils/validators.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ValidationError } from "../errors.js";
|
|
2
|
+
import { decodeAddress } from "../keys/address.js";
|
|
2
3
|
import { assertNonNegative } from "./bigint.js";
|
|
3
4
|
export const SNARK_SCALAR_FIELD = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
4
5
|
export function ensurePositiveInt(label, value) {
|
|
5
6
|
if (!Number.isInteger(value) || value < 0) {
|
|
6
|
-
throw new
|
|
7
|
+
throw new ValidationError(`${label} must be a non-negative integer`);
|
|
7
8
|
}
|
|
8
9
|
}
|
|
9
10
|
export function ensureChainId(chainId) {
|
|
10
11
|
if (!Number.isInteger(chainId) || chainId <= 0) {
|
|
11
|
-
throw new
|
|
12
|
+
throw new ValidationError("chainId must be a positive integer");
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
/** Guards master public key strings against empty values. */
|
|
15
16
|
export function ensureMpk(mpk) {
|
|
16
17
|
if (typeof mpk !== "string" || mpk.length === 0) {
|
|
17
|
-
throw new
|
|
18
|
+
throw new ValidationError("note mpk must be a non-empty string");
|
|
18
19
|
}
|
|
19
20
|
}
|
|
20
21
|
export function ensureBigIntString(label, value) {
|
|
@@ -23,34 +24,38 @@ export function ensureBigIntString(label, value) {
|
|
|
23
24
|
parsed = BigInt(value);
|
|
24
25
|
}
|
|
25
26
|
catch {
|
|
26
|
-
throw new
|
|
27
|
+
throw new ValidationError(`${label} must be a base-10 bigint string`);
|
|
27
28
|
}
|
|
28
29
|
if (parsed < 0n) {
|
|
29
|
-
throw new
|
|
30
|
+
throw new ValidationError(`${label} must be non-negative`);
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
const ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/;
|
|
33
34
|
export function ensureAddress(label, value) {
|
|
34
35
|
if (typeof value !== "string" || !ADDRESS_REGEX.test(value)) {
|
|
35
|
-
throw new
|
|
36
|
+
throw new ValidationError(`${label} must be a 0x-prefixed hex address, received: ${value}`);
|
|
36
37
|
}
|
|
38
|
+
return value.toLowerCase();
|
|
37
39
|
}
|
|
38
40
|
/** Confirms the bigint resides in the SNARK scalar field range. */
|
|
39
41
|
export function ensureFieldElement(label, value) {
|
|
40
42
|
if (value < 0n) {
|
|
41
|
-
throw new
|
|
43
|
+
throw new ValidationError(`${label} must be a non-negative field element`);
|
|
42
44
|
}
|
|
43
45
|
if (value >= SNARK_SCALAR_FIELD) {
|
|
44
|
-
throw new
|
|
46
|
+
throw new ValidationError(`${label} must be less than the SNARK scalar field`);
|
|
45
47
|
}
|
|
46
48
|
}
|
|
47
49
|
/** Validates withdrawal note public data before encoding calldata. */
|
|
48
50
|
export function ensureWithdrawalInput(label, withdrawal) {
|
|
51
|
+
if (!withdrawal) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
49
54
|
assertNonNegative(`${label}.npk`, withdrawal.npk);
|
|
50
55
|
ensureFieldElement(`${label}.npk`, withdrawal.npk);
|
|
51
56
|
assertNonNegative(`${label}.amount`, withdrawal.amount);
|
|
52
57
|
// if (withdrawal.amount === 0n) {
|
|
53
|
-
// throw new
|
|
58
|
+
// throw new ValidationError(`${label}.amount must be greater than zero`);
|
|
54
59
|
// }
|
|
55
60
|
ensureFieldElement(`${label}.amount`, withdrawal.amount);
|
|
56
61
|
ensureAddress(`${label}.token`, withdrawal.token);
|
|
@@ -62,9 +67,30 @@ export function ensureNoteCommitmentInput(label, note) {
|
|
|
62
67
|
ensureFieldElement(`${label}.npk`, note.npk);
|
|
63
68
|
assertNonNegative(`${label}.amount`, note.amount);
|
|
64
69
|
if (note.amount === 0n) {
|
|
65
|
-
throw new
|
|
70
|
+
throw new ValidationError(`${label}.amount must be greater than zero`);
|
|
66
71
|
}
|
|
67
72
|
ensureFieldElement(`${label}.amount`, note.amount);
|
|
68
73
|
ensureAddress(`${label}.token`, note.token);
|
|
69
74
|
return note;
|
|
70
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Parses a Bech32m ZK address (0zk1...) into its components.
|
|
78
|
+
*
|
|
79
|
+
* @param value - The Bech32m address string
|
|
80
|
+
* @returns Parsed address with masterPublicKey, viewingPublicKey, and optional chain
|
|
81
|
+
* @throws ValidationError if the address is invalid
|
|
82
|
+
*/
|
|
83
|
+
export function parseZkAddress(value) {
|
|
84
|
+
const trimmed = value.trim();
|
|
85
|
+
try {
|
|
86
|
+
const decoded = decodeAddress(trimmed);
|
|
87
|
+
return {
|
|
88
|
+
masterPublicKey: decoded.masterPublicKey,
|
|
89
|
+
viewingPublicKey: decoded.viewingPublicKey,
|
|
90
|
+
chain: decoded.chain,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
throw new ValidationError(`Invalid ZK address (expected 0zk1... format): ${err instanceof Error ? err.message : "unknown error"}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vitest.config.d.ts","sourceRoot":"","sources":["../vitest.config.ts"],"names":[],"mappings":";AAEA,wBAWG"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
export default defineConfig({
|
|
3
|
+
test: {
|
|
4
|
+
globals: true,
|
|
5
|
+
setupFiles: ["./vitest.setup.ts"],
|
|
6
|
+
server: {
|
|
7
|
+
deps: {
|
|
8
|
+
// Transform CJS deps to support ESM named imports
|
|
9
|
+
inline: ["circomlibjs"],
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
});
|
package/package.json
CHANGED
|
@@ -1,35 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unlink-xyz/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3-canary.0877bfe",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
|
-
"
|
|
10
|
-
|
|
9
|
+
"browser": {
|
|
10
|
+
"import": "./dist/browser/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"default": {
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"./utils/async.js": {
|
|
19
|
+
"import": "./dist/utils/async.js",
|
|
20
|
+
"types": "./dist/utils/async.d.ts"
|
|
11
21
|
}
|
|
12
22
|
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
13
26
|
"publishConfig": {
|
|
14
|
-
"access": "
|
|
27
|
+
"access": "restricted"
|
|
15
28
|
},
|
|
16
29
|
"scripts": {
|
|
17
|
-
"build": "tsc -p tsconfig.json",
|
|
30
|
+
"build": "tsc -p tsconfig.json && tsup --config tsup.browser.config.ts",
|
|
18
31
|
"lint": "eslint .",
|
|
19
32
|
"lint:fix": "eslint . --fix",
|
|
20
33
|
"format": "prettier . --write",
|
|
21
34
|
"format:check": "prettier . --check",
|
|
22
35
|
"test": "tsc -p tsconfig.json && vitest run --test-timeout 0",
|
|
23
|
-
"setup:artifacts": "./setup-artifacts.sh"
|
|
36
|
+
"setup:artifacts": "bash ./scripts/setup-artifacts.sh",
|
|
37
|
+
"clean": "rm -rf dist *.tsbuildinfo",
|
|
38
|
+
"typecheck": "tsc --noEmit"
|
|
24
39
|
},
|
|
25
40
|
"dependencies": {
|
|
26
41
|
"@noble/ed25519": "3.0.0",
|
|
27
42
|
"@noble/hashes": "2.0.0",
|
|
28
|
-
"@railgun-community/circomlibjs": "0.0.8",
|
|
29
43
|
"@scure/base": "2.0.0",
|
|
30
44
|
"@scure/bip39": "2.0.1",
|
|
31
45
|
"@types/snarkjs": "^0.7.9",
|
|
46
|
+
"@zk-kit/eddsa-poseidon": "^1.1.0",
|
|
32
47
|
"@zk-kit/imt": "2.0.0-beta.8",
|
|
48
|
+
"buffer": "^6.0.3",
|
|
49
|
+
"circomlibjs": "^0.1.7",
|
|
33
50
|
"ethereum-cryptography": "3.2.0",
|
|
34
51
|
"ethers": "6.15.0",
|
|
35
52
|
"ky": "^1.14.0",
|
|
@@ -38,13 +55,13 @@
|
|
|
38
55
|
"uuid": "^13.0.0"
|
|
39
56
|
},
|
|
40
57
|
"devDependencies": {
|
|
41
|
-
"@
|
|
58
|
+
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
|
|
59
|
+
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
|
|
42
60
|
"@types/node": "^22.0.0",
|
|
43
|
-
"
|
|
44
|
-
"@typescript-eslint/parser": "^7.18.0",
|
|
45
|
-
"eslint": "^8.57.1",
|
|
61
|
+
"eslint": "^9.19.0",
|
|
46
62
|
"fake-indexeddb": "6.2.4",
|
|
47
63
|
"prettier": "^3.5.3",
|
|
64
|
+
"tsup": "^8.3.5",
|
|
48
65
|
"typescript": "^5.9.2",
|
|
49
66
|
"vitest": "4.0.1"
|
|
50
67
|
}
|
package/.eslintrc.json
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { Mnemonic } from "../key-derivation/bip39.js";
|
|
4
|
-
import { ByteUtils } from "../key-derivation/bytes.js";
|
|
5
|
-
import {
|
|
6
|
-
deriveNodesFromSeed,
|
|
7
|
-
WalletNode,
|
|
8
|
-
} from "../key-derivation/wallet-node.js";
|
|
9
|
-
import { createMemoryStorage } from "../storage/memory.js";
|
|
10
|
-
import {
|
|
11
|
-
deriveZkAccount,
|
|
12
|
-
deriveZkAccountFromMnemonic,
|
|
13
|
-
generateMasterSeed,
|
|
14
|
-
importMasterMnemonic,
|
|
15
|
-
loadMasterMnemonic,
|
|
16
|
-
loadMasterSeed,
|
|
17
|
-
MASTER_MNEMONIC_KEY,
|
|
18
|
-
MASTER_SEED_KEY,
|
|
19
|
-
MASTER_SEED_LENGTH,
|
|
20
|
-
storeMasterMnemonic,
|
|
21
|
-
type MasterSeedCrypto,
|
|
22
|
-
} from "./zkAccount.js";
|
|
23
|
-
|
|
24
|
-
describe("deriveZkAccount", () => {
|
|
25
|
-
const seed = Uint8Array.from(
|
|
26
|
-
{ length: MASTER_SEED_LENGTH },
|
|
27
|
-
(_, i) => i & 0xff,
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
it("derives wallet key material", async () => {
|
|
31
|
-
const account = await deriveZkAccount(seed, 0);
|
|
32
|
-
|
|
33
|
-
expect(account.spendingKeyPair.privateKey).toHaveLength(32);
|
|
34
|
-
expect(typeof account.spendingKeyPair.pubkey[0]).toBe("bigint");
|
|
35
|
-
expect(typeof account.spendingKeyPair.pubkey[1]).toBe("bigint");
|
|
36
|
-
|
|
37
|
-
expect(account.viewingKeyPair.privateKey).toHaveLength(32);
|
|
38
|
-
expect(account.viewingKeyPair.pubkey).toHaveLength(32);
|
|
39
|
-
|
|
40
|
-
expect(account.nullifyingKey).toBeTypeOf("bigint");
|
|
41
|
-
|
|
42
|
-
const expectedNullifyingKey = await deriveNodesFromSeed(
|
|
43
|
-
seed,
|
|
44
|
-
0,
|
|
45
|
-
).viewing.getNullifyingKey();
|
|
46
|
-
expect(account.nullifyingKey).toBe(expectedNullifyingKey);
|
|
47
|
-
|
|
48
|
-
const expectedMPK = WalletNode.getMasterPublicKey(
|
|
49
|
-
account.spendingKeyPair.pubkey,
|
|
50
|
-
account.nullifyingKey,
|
|
51
|
-
);
|
|
52
|
-
expect(account.masterPublicKey).toBe(expectedMPK);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it("derives distinct accounts per index", async () => {
|
|
56
|
-
const first = await deriveZkAccount(seed, 0);
|
|
57
|
-
const second = await deriveZkAccount(seed, 1);
|
|
58
|
-
|
|
59
|
-
expect(first.spendingKeyPair.privateKey).not.toEqual(
|
|
60
|
-
second.spendingKeyPair.privateKey,
|
|
61
|
-
);
|
|
62
|
-
expect(first.viewingKeyPair.privateKey).not.toEqual(
|
|
63
|
-
second.viewingKeyPair.privateKey,
|
|
64
|
-
);
|
|
65
|
-
expect(first.nullifyingKey).not.toEqual(second.nullifyingKey);
|
|
66
|
-
expect(first.masterPublicKey).not.toEqual(second.masterPublicKey);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("rejects negative indices", async () => {
|
|
70
|
-
await expect(deriveZkAccount(seed, -1)).rejects.toThrow(
|
|
71
|
-
"accountIndex must be a non-negative integer",
|
|
72
|
-
);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it("rejects non-integer indices", async () => {
|
|
76
|
-
await expect(deriveZkAccount(seed, Number.NaN)).rejects.toThrow(
|
|
77
|
-
"accountIndex must be a non-negative integer",
|
|
78
|
-
);
|
|
79
|
-
await expect(deriveZkAccount(seed, 0.25)).rejects.toThrow(
|
|
80
|
-
"accountIndex must be a non-negative integer",
|
|
81
|
-
);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("rejects seeds that are not 64 bytes", async () => {
|
|
85
|
-
const shortSeed = Uint8Array.from({ length: 32 }, () => 1);
|
|
86
|
-
await expect(deriveZkAccount(shortSeed, 0)).rejects.toThrow(
|
|
87
|
-
"master seed must be 64 bytes",
|
|
88
|
-
);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it("derives the same keys when using mnemonic helper", async () => {
|
|
92
|
-
const entropy = Uint8Array.from({ length: 32 }, (_, i) => (i * 19) & 0xff);
|
|
93
|
-
const mnemonic = Mnemonic.fromEntropy(ByteUtils.bytesToHex(entropy));
|
|
94
|
-
const seedFromMnemonic = ByteUtils.hexStringToBytes(
|
|
95
|
-
Mnemonic.toSeed(mnemonic),
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
const fromSeed = await deriveZkAccount(seedFromMnemonic, 2);
|
|
99
|
-
const fromMnemonic = await deriveZkAccountFromMnemonic(mnemonic, 2);
|
|
100
|
-
|
|
101
|
-
expect(fromMnemonic).toEqual(fromSeed);
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
describe("generateMasterSeed", () => {
|
|
106
|
-
const fixtureEntropy = Uint8Array.from(
|
|
107
|
-
{ length: 32 },
|
|
108
|
-
(_, i) => (i * 13) & 0xff,
|
|
109
|
-
);
|
|
110
|
-
const fixtureMnemonic = Mnemonic.fromEntropy(
|
|
111
|
-
ByteUtils.bytesToHex(fixtureEntropy),
|
|
112
|
-
);
|
|
113
|
-
const fixtureSeed = ByteUtils.hexStringToBytes(
|
|
114
|
-
Mnemonic.toSeed(fixtureMnemonic),
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
function rngFixture(): (n: number) => Uint8Array {
|
|
118
|
-
return (n) => {
|
|
119
|
-
expect(n).toBe(32);
|
|
120
|
-
return new Uint8Array(fixtureEntropy);
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
it("creates and persists a new seed when none exists", async () => {
|
|
125
|
-
const storage = createMemoryStorage();
|
|
126
|
-
|
|
127
|
-
const seed = await generateMasterSeed({
|
|
128
|
-
storage,
|
|
129
|
-
rng: rngFixture(),
|
|
130
|
-
mnemonicPassphrase: "",
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
expect(seed).toEqual(fixtureSeed);
|
|
134
|
-
const stored = await storage.get(MASTER_SEED_KEY);
|
|
135
|
-
expect(stored).not.toBeNull();
|
|
136
|
-
expect(stored).toEqual(fixtureSeed);
|
|
137
|
-
|
|
138
|
-
const mnemonic = await loadMasterMnemonic(storage);
|
|
139
|
-
expect(mnemonic).toEqual(fixtureMnemonic);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it("returns existing seed without invoking rng", async () => {
|
|
143
|
-
const storage = createMemoryStorage();
|
|
144
|
-
await storage.put(MASTER_SEED_KEY, fixtureSeed);
|
|
145
|
-
|
|
146
|
-
const seed = await generateMasterSeed({
|
|
147
|
-
storage,
|
|
148
|
-
rng: () => {
|
|
149
|
-
throw new Error("rng should not be called when seed exists");
|
|
150
|
-
},
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
expect(seed).toEqual(fixtureSeed);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
it("overwrites when requested", async () => {
|
|
157
|
-
const storage = createMemoryStorage();
|
|
158
|
-
const existing = Uint8Array.from(
|
|
159
|
-
{ length: MASTER_SEED_LENGTH },
|
|
160
|
-
(_, i) => (i * 3) & 0xff,
|
|
161
|
-
);
|
|
162
|
-
await storage.put(MASTER_SEED_KEY, existing);
|
|
163
|
-
const replacementEntropy = Uint8Array.from(
|
|
164
|
-
{ length: 32 },
|
|
165
|
-
(_, i) => (i * 7) & 0xff,
|
|
166
|
-
);
|
|
167
|
-
const replacementMnemonic = Mnemonic.fromEntropy(
|
|
168
|
-
ByteUtils.bytesToHex(replacementEntropy),
|
|
169
|
-
);
|
|
170
|
-
const replacementSeed = ByteUtils.hexStringToBytes(
|
|
171
|
-
Mnemonic.toSeed(replacementMnemonic),
|
|
172
|
-
);
|
|
173
|
-
|
|
174
|
-
const seed = await generateMasterSeed({
|
|
175
|
-
storage,
|
|
176
|
-
rng: (n) => {
|
|
177
|
-
expect(n).toBe(32);
|
|
178
|
-
return new Uint8Array(replacementEntropy);
|
|
179
|
-
},
|
|
180
|
-
overwrite: true,
|
|
181
|
-
mnemonicPassphrase: "",
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
expect(seed).toEqual(replacementSeed);
|
|
185
|
-
const stored = await storage.get(MASTER_SEED_KEY);
|
|
186
|
-
expect(stored).toEqual(replacementSeed);
|
|
187
|
-
const mnemonic = await loadMasterMnemonic(storage);
|
|
188
|
-
expect(mnemonic).toEqual(replacementMnemonic);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it("applies custom crypto hooks", async () => {
|
|
192
|
-
const storage = createMemoryStorage();
|
|
193
|
-
|
|
194
|
-
const crypto: MasterSeedCrypto = {
|
|
195
|
-
encrypt(seed) {
|
|
196
|
-
return seed.map((byte) => byte ^ 0xff);
|
|
197
|
-
},
|
|
198
|
-
decrypt(payload) {
|
|
199
|
-
return payload.map((byte) => byte ^ 0xff);
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
const seed = await generateMasterSeed({
|
|
204
|
-
storage,
|
|
205
|
-
rng: rngFixture(),
|
|
206
|
-
crypto,
|
|
207
|
-
mnemonicPassphrase: "",
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
expect(seed).toEqual(fixtureSeed);
|
|
211
|
-
const roundTrip = await loadMasterSeed(storage, crypto);
|
|
212
|
-
expect(roundTrip).toEqual(fixtureSeed);
|
|
213
|
-
|
|
214
|
-
const storedMnemonic = await storage.get(MASTER_MNEMONIC_KEY);
|
|
215
|
-
expect(storedMnemonic).toEqual(
|
|
216
|
-
textEncode(fixtureMnemonic).map((byte) => byte ^ 0xff),
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
const mnemonicRoundTrip = await loadMasterMnemonic(storage, crypto);
|
|
220
|
-
expect(mnemonicRoundTrip).toEqual(fixtureMnemonic);
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
it("returns null when no seed is stored", async () => {
|
|
224
|
-
const storage = createMemoryStorage();
|
|
225
|
-
|
|
226
|
-
const result = await loadMasterSeed(storage);
|
|
227
|
-
|
|
228
|
-
expect(result).toBeNull();
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it("throws if rng returns incorrect length", async () => {
|
|
232
|
-
const storage = createMemoryStorage();
|
|
233
|
-
|
|
234
|
-
await expect(
|
|
235
|
-
generateMasterSeed({
|
|
236
|
-
storage,
|
|
237
|
-
rng: () => new Uint8Array(16),
|
|
238
|
-
mnemonicPassphrase: "",
|
|
239
|
-
}),
|
|
240
|
-
).rejects.toThrow(/mnemonic entropy must be 32 bytes/);
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
it("reconstitutes seed from stored mnemonic when seed missing", async () => {
|
|
244
|
-
const storage = createMemoryStorage();
|
|
245
|
-
await storeMnemonicFixture(storage, fixtureMnemonic);
|
|
246
|
-
|
|
247
|
-
const seed = await generateMasterSeed({
|
|
248
|
-
storage,
|
|
249
|
-
rng: rngFixture(),
|
|
250
|
-
mnemonicPassphrase: "",
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
expect(seed).toEqual(fixtureSeed);
|
|
254
|
-
const stored = await storage.get(MASTER_SEED_KEY);
|
|
255
|
-
expect(stored).toEqual(fixtureSeed);
|
|
256
|
-
});
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
describe("importMasterMnemonic", () => {
|
|
260
|
-
const mnemonic =
|
|
261
|
-
"legal winner thank year wave sausage worth useful legal winner thank yellow";
|
|
262
|
-
const seed = ByteUtils.hexStringToBytes(Mnemonic.toSeed(mnemonic));
|
|
263
|
-
|
|
264
|
-
it("imports mnemonic and stores derived seed", async () => {
|
|
265
|
-
const storage = createMemoryStorage();
|
|
266
|
-
|
|
267
|
-
const derived = await importMasterMnemonic({ storage, mnemonic });
|
|
268
|
-
|
|
269
|
-
expect(derived).toEqual(seed);
|
|
270
|
-
const storedSeed = await loadMasterSeed(storage);
|
|
271
|
-
expect(storedSeed).toEqual(seed);
|
|
272
|
-
const storedMnemonic = await loadMasterMnemonic(storage);
|
|
273
|
-
expect(storedMnemonic).toEqual(mnemonic);
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
it("refuses to overwrite without flag", async () => {
|
|
277
|
-
const storage = createMemoryStorage();
|
|
278
|
-
await importMasterMnemonic({ storage, mnemonic });
|
|
279
|
-
|
|
280
|
-
await expect(importMasterMnemonic({ storage, mnemonic })).rejects.toThrow(
|
|
281
|
-
/already exists/,
|
|
282
|
-
);
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it("overwrites when requested", async () => {
|
|
286
|
-
const storage = createMemoryStorage();
|
|
287
|
-
await importMasterMnemonic({ storage, mnemonic });
|
|
288
|
-
|
|
289
|
-
const alternate =
|
|
290
|
-
"letter advice cage absurd amount doctor acoustic avoid letter advice cage above";
|
|
291
|
-
const alternateSeed = ByteUtils.hexStringToBytes(
|
|
292
|
-
Mnemonic.toSeed(alternate),
|
|
293
|
-
);
|
|
294
|
-
|
|
295
|
-
const derived = await importMasterMnemonic({
|
|
296
|
-
storage,
|
|
297
|
-
mnemonic: alternate,
|
|
298
|
-
overwrite: true,
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
expect(derived).toEqual(alternateSeed);
|
|
302
|
-
const storedMnemonic = await loadMasterMnemonic(storage);
|
|
303
|
-
expect(storedMnemonic).toEqual(alternate);
|
|
304
|
-
});
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
function textEncode(value: string): Uint8Array {
|
|
308
|
-
return new TextEncoder().encode(value);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
async function storeMnemonicFixture(
|
|
312
|
-
storage: ReturnType<typeof createMemoryStorage>,
|
|
313
|
-
mnemonic: string,
|
|
314
|
-
) {
|
|
315
|
-
await storeMasterMnemonic(storage, mnemonic);
|
|
316
|
-
}
|