@unlink-xyz/core 0.1.3 → 0.1.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/README.md +9 -0
- package/dist/account/{zkAccount.d.ts → account.d.ts} +36 -5
- package/dist/account/account.d.ts.map +1 -0
- package/dist/account/accounts.d.ts +42 -0
- package/dist/account/accounts.d.ts.map +1 -0
- package/dist/account/seed.d.ts +45 -0
- package/dist/account/seed.d.ts.map +1 -0
- package/dist/account/serialization.d.ts +6 -0
- package/dist/account/serialization.d.ts.map +1 -0
- package/dist/browser/index.js +56221 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/wallet/index.js +55942 -0
- package/dist/browser/wallet/index.js.map +1 -0
- package/dist/clients/broadcaster.d.ts +8 -2
- package/dist/clients/broadcaster.d.ts.map +1 -1
- package/dist/clients/http.d.ts +6 -0
- package/dist/clients/http.d.ts.map +1 -1
- package/dist/clients/indexer.d.ts +16 -0
- package/dist/clients/indexer.d.ts.map +1 -1
- package/dist/config.d.ts +30 -9
- package/dist/config.d.ts.map +1 -1
- package/dist/constants.d.ts +6 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/core.d.ts.map +1 -1
- package/dist/crypto/adapters/index.d.ts +17 -0
- package/dist/crypto/adapters/index.d.ts.map +1 -0
- package/dist/crypto/adapters/polyfills.d.ts +5 -0
- package/dist/crypto/adapters/polyfills.d.ts.map +1 -0
- package/dist/crypto/encrypt.d.ts +33 -0
- package/dist/crypto/encrypt.d.ts.map +1 -0
- package/dist/crypto/secure-memory.d.ts +25 -0
- package/dist/crypto/secure-memory.d.ts.map +1 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/history/index.d.ts +3 -0
- package/dist/history/index.d.ts.map +1 -0
- package/dist/history/service.d.ts +46 -0
- package/dist/history/service.d.ts.map +1 -0
- package/dist/history/types.d.ts +21 -0
- package/dist/history/types.d.ts.map +1 -0
- package/dist/index.d.ts +16 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6721 -19
- package/dist/index.js.map +1 -0
- package/dist/keys/address.d.ts +13 -0
- package/dist/keys/address.d.ts.map +1 -0
- package/dist/keys/derive.d.ts +37 -0
- package/dist/keys/derive.d.ts.map +1 -0
- package/dist/keys/hex.d.ts +14 -0
- package/dist/keys/hex.d.ts.map +1 -0
- package/dist/keys/index.d.ts +5 -0
- package/dist/keys/index.d.ts.map +1 -0
- package/dist/keys/mnemonic.d.ts +6 -0
- package/dist/keys/mnemonic.d.ts.map +1 -0
- package/dist/keys.d.ts +5 -1
- package/dist/keys.d.ts.map +1 -1
- package/dist/prover/config.d.ts +53 -22
- package/dist/prover/config.d.ts.map +1 -1
- package/dist/prover/integrity.d.ts +20 -0
- package/dist/prover/integrity.d.ts.map +1 -0
- package/dist/prover/prover.d.ts +16 -20
- package/dist/prover/prover.d.ts.map +1 -1
- package/dist/prover/registry.d.ts +3 -30
- package/dist/prover/registry.d.ts.map +1 -1
- package/dist/state/merkle/hydrator.d.ts +21 -19
- package/dist/state/merkle/hydrator.d.ts.map +1 -1
- package/dist/state/merkle/index.d.ts +2 -2
- package/dist/state/merkle/index.d.ts.map +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/store/ciphertext-store.d.ts +11 -0
- package/dist/state/store/ciphertext-store.d.ts.map +1 -1
- 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/index.d.ts +3 -2
- package/dist/state/store/index.d.ts.map +1 -1
- 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/jobs.d.ts +70 -25
- package/dist/state/store/jobs.d.ts.map +1 -1
- package/dist/state/store/leaf-store.d.ts +4 -0
- package/dist/state/store/leaf-store.d.ts.map +1 -1
- 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/nullifier-store.d.ts +9 -0
- package/dist/state/store/nullifier-store.d.ts.map +1 -1
- package/dist/state/store/records.d.ts +39 -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/store.d.ts +79 -27
- package/dist/state/store/store.d.ts.map +1 -1
- package/dist/storage/indexeddb.d.ts.map +1 -1
- package/dist/storage/memory.d.ts.map +1 -1
- package/dist/transactions/adapter.d.ts +31 -0
- package/dist/transactions/adapter.d.ts.map +1 -0
- package/dist/transactions/deposit.d.ts +12 -15
- package/dist/transactions/deposit.d.ts.map +1 -1
- package/dist/transactions/index.d.ts +9 -4
- package/dist/transactions/index.d.ts.map +1 -1
- package/dist/transactions/note-selection.d.ts +17 -0
- package/dist/transactions/note-selection.d.ts.map +1 -0
- package/dist/transactions/note-sync.d.ts +5 -33
- package/dist/transactions/note-sync.d.ts.map +1 -1
- package/dist/transactions/reconcile.d.ts +9 -11
- package/dist/transactions/reconcile.d.ts.map +1 -1
- package/dist/transactions/transact.d.ts +30 -22
- package/dist/transactions/transact.d.ts.map +1 -1
- package/dist/transactions/transaction-planner.d.ts +34 -0
- package/dist/transactions/transaction-planner.d.ts.map +1 -0
- package/dist/transactions/transfer-planner.d.ts +37 -0
- package/dist/transactions/transfer-planner.d.ts.map +1 -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 +70 -0
- package/dist/transactions/types/domain.d.ts.map +1 -0
- package/dist/transactions/types/index.d.ts +18 -0
- package/dist/transactions/types/index.d.ts.map +1 -0
- package/dist/transactions/types/options.d.ts +54 -0
- package/dist/transactions/types/options.d.ts.map +1 -0
- package/dist/transactions/types/planning.d.ts +82 -0
- package/dist/transactions/types/planning.d.ts.map +1 -0
- package/dist/transactions/types/state-stores.d.ts +151 -0
- package/dist/transactions/types/state-stores.d.ts.map +1 -0
- package/dist/transactions/types/transact.d.ts +83 -0
- package/dist/transactions/types/transact.d.ts.map +1 -0
- package/dist/transactions/withdrawal-planner.d.ts +58 -0
- package/dist/transactions/withdrawal-planner.d.ts.map +1 -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.config.d.ts +8 -0
- package/dist/tsup.config.d.ts.map +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/amounts.d.ts +26 -0
- package/dist/utils/amounts.d.ts.map +1 -0
- package/dist/utils/async.d.ts +9 -0
- package/dist/utils/async.d.ts.map +1 -1
- package/dist/utils/async.js +38 -11
- package/dist/utils/async.js.map +1 -0
- package/dist/utils/bigint.d.ts +0 -2
- package/dist/utils/bigint.d.ts.map +1 -1
- package/dist/utils/format.d.ts +25 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/notes.d.ts +15 -0
- package/dist/utils/notes.d.ts.map +1 -0
- package/dist/utils/polling.d.ts +5 -0
- package/dist/utils/polling.d.ts.map +1 -1
- package/dist/utils/random.d.ts +18 -0
- package/dist/utils/random.d.ts.map +1 -0
- package/dist/utils/signature.d.ts +6 -0
- package/dist/utils/signature.d.ts.map +1 -1
- package/dist/utils/validators.d.ts +21 -10
- package/dist/utils/validators.d.ts.map +1 -1
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/wallet/adapter.d.ts +21 -0
- package/dist/wallet/adapter.d.ts.map +1 -0
- package/dist/wallet/burner/service.d.ts +32 -0
- package/dist/wallet/burner/service.d.ts.map +1 -0
- package/dist/wallet/burner/types.d.ts +47 -0
- package/dist/wallet/burner/types.d.ts.map +1 -0
- package/dist/wallet/index.d.ts +20 -0
- package/dist/wallet/index.d.ts.map +1 -0
- package/dist/wallet/index.js +6462 -0
- package/dist/wallet/index.js.map +1 -0
- package/dist/wallet/sdk.d.ts +48 -0
- package/dist/wallet/sdk.d.ts.map +1 -0
- package/dist/wallet/types.d.ts +457 -0
- package/dist/wallet/types.d.ts.map +1 -0
- package/dist/wallet/unlink-wallet.d.ts +187 -0
- package/dist/wallet/unlink-wallet.d.ts.map +1 -0
- package/package.json +38 -15
- package/.eslintrc.json +0 -4
- package/account/zkAccount.test.ts +0 -316
- package/account/zkAccount.ts +0 -222
- package/circuits.json +0 -26
- 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/account/zkAccount.js +0 -128
- package/dist/circuits.json +0 -26
- package/dist/clients/broadcaster.js +0 -23
- package/dist/clients/http.js +0 -57
- package/dist/clients/indexer.js +0 -67
- package/dist/config.js +0 -29
- package/dist/core.js +0 -12
- package/dist/errors.js +0 -18
- 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/keys.js +0 -41
- package/dist/prover/config.js +0 -80
- package/dist/prover/index.js +0 -1
- package/dist/prover/prover.js +0 -274
- package/dist/prover/registry.js +0 -57
- package/dist/schema.js +0 -14
- 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/index.js +0 -2
- 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/hydrator.js +0 -36
- package/dist/state/merkle/index.js +0 -2
- package/dist/state/merkle/merkle-tree.js +0 -104
- 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/records.js +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/ciphertext-store.js +0 -25
- package/dist/state/store/index.js +0 -8
- package/dist/state/store/job-store.js +0 -118
- package/dist/state/store/jobs.js +0 -1
- package/dist/state/store/leaf-store.js +0 -35
- package/dist/state/store/note-store.js +0 -142
- package/dist/state/store/nullifier-store.js +0 -30
- package/dist/state/store/records.js +0 -1
- package/dist/state/store/root-store.js +0 -30
- package/dist/state/store/store.js +0 -22
- 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/storage/index.js +0 -2
- package/dist/storage/indexeddb.js +0 -205
- package/dist/storage/memory.js +0 -87
- package/dist/transactions/deposit.js +0 -169
- package/dist/transactions/index.js +0 -4
- package/dist/transactions/note-sync.js +0 -320
- package/dist/transactions/reconcile.js +0 -39
- 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/transact.js +0 -561
- package/dist/transactions/types.d.ts +0 -114
- package/dist/transactions/types.d.ts.map +0 -1
- package/dist/transactions/types.js +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/types.js +0 -1
- package/dist/utils/bigint.js +0 -29
- package/dist/utils/crypto.d.ts +0 -12
- package/dist/utils/crypto.d.ts.map +0 -1
- package/dist/utils/crypto.js +0 -39
- package/dist/utils/json-codec.js +0 -25
- package/dist/utils/polling.js +0 -6
- package/dist/utils/signature.js +0 -12
- 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/validators.js +0 -70
- 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 -21
- 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 -15
- 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
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Computes the bound parameters hash from chain ID, pool address, and withdrawal parameters.
|
|
3
|
-
* This hash binds the transaction to specific chain and withdrawal context.
|
|
4
|
-
*/
|
|
5
|
-
export declare function computeBoundParamsHash(chainId: number, poolAddress: string, withdrawal: {
|
|
6
|
-
npk: bigint;
|
|
7
|
-
amount: bigint;
|
|
8
|
-
token: string;
|
|
9
|
-
}): bigint;
|
|
10
|
-
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../transactions/utils.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACzD,MAAM,CAYR"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { poseidon } from "@railgun-community/circomlibjs"; // TODO: replace with zk-kit
|
|
2
|
-
/**
|
|
3
|
-
* Computes the bound parameters hash from chain ID, pool address, and withdrawal parameters.
|
|
4
|
-
* This hash binds the transaction to specific chain and withdrawal context.
|
|
5
|
-
*/
|
|
6
|
-
export function computeBoundParamsHash(chainId, poolAddress, withdrawal) {
|
|
7
|
-
const chainIdBigInt = BigInt(chainId);
|
|
8
|
-
const poolAddressBigInt = BigInt(poolAddress);
|
|
9
|
-
const tokenBigInt = BigInt(withdrawal.token);
|
|
10
|
-
return poseidon([
|
|
11
|
-
chainIdBigInt,
|
|
12
|
-
poolAddressBigInt,
|
|
13
|
-
withdrawal.npk,
|
|
14
|
-
withdrawal.amount,
|
|
15
|
-
tokenBigInt,
|
|
16
|
-
]);
|
|
17
|
-
}
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/utils/bigint.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { CoreError } from "../errors.js";
|
|
2
|
-
import { ByteLength, ByteUtils } from "../key-derivation/bytes.js";
|
|
3
|
-
/**
|
|
4
|
-
* BigInt helpers shared across transaction builders to keep formatting consistent.
|
|
5
|
-
*/
|
|
6
|
-
export function formatUint256(value) {
|
|
7
|
-
assertNonNegative("uint256", value);
|
|
8
|
-
return ByteUtils.nToHex(value, ByteLength.UINT_256, true);
|
|
9
|
-
}
|
|
10
|
-
export function parseHexToBigInt(value) {
|
|
11
|
-
return ByteUtils.hexToBigInt(value);
|
|
12
|
-
}
|
|
13
|
-
export function parseNumToBigInt(value) {
|
|
14
|
-
if (value === undefined) {
|
|
15
|
-
throw new CoreError("Value is undefined");
|
|
16
|
-
}
|
|
17
|
-
return BigInt(value);
|
|
18
|
-
}
|
|
19
|
-
export function assertNonNegative(label, value) {
|
|
20
|
-
if (value < 0n) {
|
|
21
|
-
throw new CoreError(`${label} must be non-negative`);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
export function ensureBigint(label, value) {
|
|
25
|
-
if (typeof value !== "bigint") {
|
|
26
|
-
throw new CoreError(`${label} must be provided as a bigint`);
|
|
27
|
-
}
|
|
28
|
-
return value;
|
|
29
|
-
}
|
package/dist/utils/crypto.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Ciphertext, DepositNoteInput, DepositRequest, OutputNoteInput } from "../transactions/types.js";
|
|
2
|
-
export declare function encryptNote(note: OutputNoteInput | DepositNoteInput): Ciphertext;
|
|
3
|
-
export declare function encrypt(data: bigint, key: bigint): bigint;
|
|
4
|
-
export declare function decryptNote(c: Ciphertext, key: bigint): OutputNoteInput;
|
|
5
|
-
export declare function deriveCommitment(note: {
|
|
6
|
-
npk: bigint;
|
|
7
|
-
amount: bigint;
|
|
8
|
-
token: string;
|
|
9
|
-
}): bigint;
|
|
10
|
-
export declare function deriveNpk(request: DepositRequest): bigint;
|
|
11
|
-
export declare function computeCommitment(request: DepositRequest, npk: bigint): bigint;
|
|
12
|
-
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../utils/crypto.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,eAAe,EAChB,MAAM,0BAA0B,CAAC;AAGlC,wBAAgB,WAAW,CACzB,IAAI,EAAE,eAAe,GAAG,gBAAgB,GACvC,UAAU,CAQZ;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,eAAe,CAYvE;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE;IACrC,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACf,UAGA;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,cAAc,UAEhD;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,UAGrE"}
|
package/dist/utils/crypto.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { poseidon } from "@railgun-community/circomlibjs";
|
|
2
|
-
import { CoreError } from "../errors.js";
|
|
3
|
-
import { parseHexToBigInt } from "./bigint.js";
|
|
4
|
-
export function encryptNote(note) {
|
|
5
|
-
return {
|
|
6
|
-
data: [
|
|
7
|
-
encrypt(note.random, note.mpk),
|
|
8
|
-
encrypt(parseHexToBigInt(note.token), note.mpk),
|
|
9
|
-
encrypt(note.amount, note.mpk),
|
|
10
|
-
],
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
export function encrypt(data, key) {
|
|
14
|
-
return poseidon([0x556e6c696e6b, key]) ^ data;
|
|
15
|
-
}
|
|
16
|
-
export function decryptNote(c, key) {
|
|
17
|
-
const token_bigint = encrypt(c.data[1], key);
|
|
18
|
-
if (token_bigint > 0xffffffffffffffffffffffffffffffffffffffffn) {
|
|
19
|
-
throw new CoreError("Invalid Decrypt");
|
|
20
|
-
}
|
|
21
|
-
const result = {
|
|
22
|
-
mpk: key,
|
|
23
|
-
random: encrypt(c.data[0], key),
|
|
24
|
-
token: "0x" + token_bigint.toString(16).padStart(40, "0"),
|
|
25
|
-
amount: encrypt(c.data[2], key),
|
|
26
|
-
};
|
|
27
|
-
return result;
|
|
28
|
-
}
|
|
29
|
-
export function deriveCommitment(note) {
|
|
30
|
-
const tokenScalar = BigInt(note.token);
|
|
31
|
-
return poseidon([note.npk, tokenScalar, note.amount]);
|
|
32
|
-
}
|
|
33
|
-
export function deriveNpk(request) {
|
|
34
|
-
return poseidon([request.note.mpk, request.note.random]);
|
|
35
|
-
}
|
|
36
|
-
export function computeCommitment(request, npk) {
|
|
37
|
-
const tokenScalar = BigInt(request.note.token);
|
|
38
|
-
return poseidon([npk, tokenScalar, request.note.amount]);
|
|
39
|
-
}
|
package/dist/utils/json-codec.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { CoreError } from "../errors.js";
|
|
2
|
-
import { validateKey } from "../keys.js";
|
|
3
|
-
const encoder = new TextEncoder();
|
|
4
|
-
const decoder = new TextDecoder();
|
|
5
|
-
export function encodeJson(value) {
|
|
6
|
-
return encoder.encode(JSON.stringify(value));
|
|
7
|
-
}
|
|
8
|
-
export function decodeJson(payload) {
|
|
9
|
-
try {
|
|
10
|
-
return JSON.parse(decoder.decode(payload));
|
|
11
|
-
}
|
|
12
|
-
catch (err) {
|
|
13
|
-
throw new CoreError("failed to decode stored state payload");
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export async function putJson(storage, key, record) {
|
|
17
|
-
validateKey(key);
|
|
18
|
-
await storage.put(key, encodeJson(record));
|
|
19
|
-
}
|
|
20
|
-
export async function getJson(storage, key) {
|
|
21
|
-
const payload = await storage.get(key);
|
|
22
|
-
if (!payload)
|
|
23
|
-
return null;
|
|
24
|
-
return decodeJson(payload);
|
|
25
|
-
}
|
package/dist/utils/polling.js
DELETED
package/dist/utils/signature.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Buffer } from "buffer";
|
|
2
|
-
import { eddsa } from "@railgun-community/circomlibjs";
|
|
3
|
-
/**
|
|
4
|
-
* Generates an EdDSA signature over a poseidon-hashed message using the spending private key.
|
|
5
|
-
* Used for transact public signal signing.
|
|
6
|
-
*/
|
|
7
|
-
export function signTransactMessage(spendingPrivateKey, message) {
|
|
8
|
-
const key = Buffer.isBuffer(spendingPrivateKey)
|
|
9
|
-
? spendingPrivateKey
|
|
10
|
-
: Buffer.from(spendingPrivateKey);
|
|
11
|
-
return eddsa.signPoseidon(key, message);
|
|
12
|
-
}
|
package/dist/utils/time.d.ts
DELETED
package/dist/utils/time.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../utils/time.ts"],"names":[],"mappings":"AAAA,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,oBAE/B"}
|
package/dist/utils/time.js
DELETED
package/dist/utils/validators.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { CoreError } from "../errors.js";
|
|
2
|
-
import { assertNonNegative } from "./bigint.js";
|
|
3
|
-
export const SNARK_SCALAR_FIELD = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
4
|
-
export function ensurePositiveInt(label, value) {
|
|
5
|
-
if (!Number.isInteger(value) || value < 0) {
|
|
6
|
-
throw new CoreError(`${label} must be a non-negative integer`);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
export function ensureChainId(chainId) {
|
|
10
|
-
if (!Number.isInteger(chainId) || chainId <= 0) {
|
|
11
|
-
throw new CoreError("chainId must be a positive integer");
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
/** Guards master public key strings against empty values. */
|
|
15
|
-
export function ensureMpk(mpk) {
|
|
16
|
-
if (typeof mpk !== "string" || mpk.length === 0) {
|
|
17
|
-
throw new CoreError("note mpk must be a non-empty string");
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
export function ensureBigIntString(label, value) {
|
|
21
|
-
let parsed;
|
|
22
|
-
try {
|
|
23
|
-
parsed = BigInt(value);
|
|
24
|
-
}
|
|
25
|
-
catch {
|
|
26
|
-
throw new CoreError(`${label} must be a base-10 bigint string`);
|
|
27
|
-
}
|
|
28
|
-
if (parsed < 0n) {
|
|
29
|
-
throw new CoreError(`${label} must be non-negative`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
const ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/;
|
|
33
|
-
export function ensureAddress(label, value) {
|
|
34
|
-
if (typeof value !== "string" || !ADDRESS_REGEX.test(value)) {
|
|
35
|
-
throw new CoreError(`${label} must be a 0x-prefixed hex address`);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/** Confirms the bigint resides in the SNARK scalar field range. */
|
|
39
|
-
export function ensureFieldElement(label, value) {
|
|
40
|
-
if (value < 0n) {
|
|
41
|
-
throw new CoreError(`${label} must be a non-negative field element`);
|
|
42
|
-
}
|
|
43
|
-
if (value >= SNARK_SCALAR_FIELD) {
|
|
44
|
-
throw new CoreError(`${label} must be less than the SNARK scalar field`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
/** Validates withdrawal note public data before encoding calldata. */
|
|
48
|
-
export function ensureWithdrawalInput(label, withdrawal) {
|
|
49
|
-
assertNonNegative(`${label}.npk`, withdrawal.npk);
|
|
50
|
-
ensureFieldElement(`${label}.npk`, withdrawal.npk);
|
|
51
|
-
assertNonNegative(`${label}.amount`, withdrawal.amount);
|
|
52
|
-
// if (withdrawal.amount === 0n) {
|
|
53
|
-
// throw new CoreError(`${label}.amount must be greater than zero`);
|
|
54
|
-
// }
|
|
55
|
-
ensureFieldElement(`${label}.amount`, withdrawal.amount);
|
|
56
|
-
ensureAddress(`${label}.token`, withdrawal.token);
|
|
57
|
-
return withdrawal;
|
|
58
|
-
}
|
|
59
|
-
/** Validates note parameters before producing a Poseidon commitment. */
|
|
60
|
-
export function ensureNoteCommitmentInput(label, note) {
|
|
61
|
-
assertNonNegative(`${label}.npk`, note.npk);
|
|
62
|
-
ensureFieldElement(`${label}.npk`, note.npk);
|
|
63
|
-
assertNonNegative(`${label}.amount`, note.amount);
|
|
64
|
-
if (note.amount === 0n) {
|
|
65
|
-
throw new CoreError(`${label}.amount must be greater than zero`);
|
|
66
|
-
}
|
|
67
|
-
ensureFieldElement(`${label}.amount`, note.amount);
|
|
68
|
-
ensureAddress(`${label}.token`, note.token);
|
|
69
|
-
return note;
|
|
70
|
-
}
|
package/dist/utils/witness.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { IMTMerkleProof } from "@zk-kit/imt";
|
|
2
|
-
export type SerializedWitness = {
|
|
3
|
-
root: string;
|
|
4
|
-
leaf: string;
|
|
5
|
-
pathElements: string[][];
|
|
6
|
-
pathIndices: number[];
|
|
7
|
-
leafIndex: number;
|
|
8
|
-
};
|
|
9
|
-
export declare function serializeWitness(proof: IMTMerkleProof, index: number): SerializedWitness;
|
|
10
|
-
export declare function deserializeWitness(serialized: SerializedWitness): IMTMerkleProof;
|
|
11
|
-
//# sourceMappingURL=witness.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"witness.d.ts","sourceRoot":"","sources":["../../utils/witness.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG7C,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,EAAE,EAAE,CAAC;IACzB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,cAAc,EACrB,KAAK,EAAE,MAAM,GACZ,iBAAiB,CAUnB;AAED,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,iBAAiB,GAAG,cAAc,CAUhF"}
|
package/dist/utils/witness.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { formatUint256, parseHexToBigInt } from "./bigint.js";
|
|
2
|
-
export function serializeWitness(proof, index) {
|
|
3
|
-
return {
|
|
4
|
-
root: formatUint256(BigInt(proof.root)),
|
|
5
|
-
leaf: formatUint256(BigInt(proof.leaf)),
|
|
6
|
-
pathElements: proof.siblings.map((level) => level.map((node) => formatUint256(BigInt(node)))),
|
|
7
|
-
pathIndices: proof.pathIndices ?? [],
|
|
8
|
-
leafIndex: index,
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
export function deserializeWitness(serialized) {
|
|
12
|
-
return {
|
|
13
|
-
root: parseHexToBigInt(serialized.root),
|
|
14
|
-
leaf: parseHexToBigInt(serialized.leaf),
|
|
15
|
-
siblings: serialized.pathElements.map((level) => level.map((node) => parseHexToBigInt(node))),
|
|
16
|
-
pathIndices: serialized.pathIndices,
|
|
17
|
-
leafIndex: serialized.leafIndex,
|
|
18
|
-
};
|
|
19
|
-
}
|
package/errors.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export class CoreError extends Error {
|
|
2
|
-
constructor(message: string) {
|
|
3
|
-
super(message);
|
|
4
|
-
this.name = "CoreError";
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export class KeyValidationError extends CoreError {
|
|
9
|
-
constructor(message: string) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.name = "KeyValidationError";
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class SchemaMismatchError extends CoreError {
|
|
16
|
-
constructor(current: number, expected: number) {
|
|
17
|
-
super(`schema mismatch (current ${current}, expected ${expected})`);
|
|
18
|
-
this.name = "SchemaMismatchError";
|
|
19
|
-
}
|
|
20
|
-
}
|
package/index.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export * from "./types.js";
|
|
2
|
-
export * from "./errors.js";
|
|
3
|
-
export * from "./keys.js";
|
|
4
|
-
export * from "./storage/index.js";
|
|
5
|
-
export * from "./schema.js";
|
|
6
|
-
export * from "./core.js";
|
|
7
|
-
export * from "./state/index.js";
|
|
8
|
-
export * from "./utils/validators.js";
|
|
9
|
-
export * from "./utils/bigint.js";
|
|
10
|
-
export * from "./account/zkAccount.js";
|
|
11
|
-
export * from "./key-derivation/index.js";
|
|
12
|
-
export * from "./transactions/index.js";
|
|
13
|
-
export { createJobReconciler } from "./transactions/reconcile.js";
|
|
14
|
-
export { serviceConfig } from "./config.js";
|
|
15
|
-
export type { ServiceConfig } from "./config.js";
|
|
16
|
-
export * from "./clients/broadcaster.js";
|
|
17
|
-
export * from "./clients/indexer.js";
|
|
18
|
-
|
|
19
|
-
// Re-export circuits configuration
|
|
20
|
-
import circuitsConfig from "./circuits.json" with { type: "json" };
|
|
21
|
-
export { circuitsConfig };
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { babyjub } from "@railgun-community/circomlibjs";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Lightweight wrapper exposing the BabyJubJub point packing helpers we rely on for
|
|
5
|
-
* derivation and address encoding. Re-exported for consumers that need raw curve ops.
|
|
6
|
-
*/
|
|
7
|
-
export class Babyjubjub {
|
|
8
|
-
static packPoint = babyjub.packPoint;
|
|
9
|
-
|
|
10
|
-
static unpackPoint = babyjub.unpackPoint;
|
|
11
|
-
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { bech32m } from "@scure/base";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
ADDRESS_LENGTH_LIMIT,
|
|
6
|
-
ADDRESS_VERSION,
|
|
7
|
-
decodeAddress,
|
|
8
|
-
encodeAddress,
|
|
9
|
-
type Chain,
|
|
10
|
-
} from "./bech32.js";
|
|
11
|
-
|
|
12
|
-
describe("bech32 address encoding", () => {
|
|
13
|
-
const masterPublicKey = 1234567890123456789012345678901234567890n;
|
|
14
|
-
const viewingPublicKey = Uint8Array.from(
|
|
15
|
-
{ length: 32 },
|
|
16
|
-
(_, i) => (i * 3) & 0xff,
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
it("encodes and decodes addresses without chain metadata", () => {
|
|
20
|
-
const address = encodeAddress({
|
|
21
|
-
masterPublicKey,
|
|
22
|
-
viewingPublicKey,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const decoded = decodeAddress(address);
|
|
26
|
-
|
|
27
|
-
expect(decoded.masterPublicKey).toEqual(masterPublicKey);
|
|
28
|
-
expect(decoded.viewingPublicKey).toEqual(viewingPublicKey);
|
|
29
|
-
expect(decoded.chain).toBeUndefined();
|
|
30
|
-
expect(decoded.version).toBe(ADDRESS_VERSION);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("preserves chain metadata when provided", () => {
|
|
34
|
-
const chain: Chain = { type: 1, id: 42161 };
|
|
35
|
-
const address = encodeAddress({
|
|
36
|
-
masterPublicKey,
|
|
37
|
-
viewingPublicKey,
|
|
38
|
-
chain,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const decoded = decodeAddress(address);
|
|
42
|
-
|
|
43
|
-
expect(decoded.chain).toEqual(chain);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("rejects malformed payloads", () => {
|
|
47
|
-
const address = encodeAddress({
|
|
48
|
-
masterPublicKey,
|
|
49
|
-
viewingPublicKey,
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
expect(() => decodeAddress("")).toThrow("No address to decode");
|
|
53
|
-
|
|
54
|
-
const decoded = bech32m.decode(address, ADDRESS_LENGTH_LIMIT);
|
|
55
|
-
const reencoded = bech32m.encode(
|
|
56
|
-
"abc",
|
|
57
|
-
decoded.words,
|
|
58
|
-
ADDRESS_LENGTH_LIMIT,
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
expect(() => decodeAddress(reencoded)).toThrow("Invalid address prefix");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("throws on incorrect version", () => {
|
|
65
|
-
const address = encodeAddress({
|
|
66
|
-
masterPublicKey,
|
|
67
|
-
viewingPublicKey,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
const mutated = mutatePayload(address, (payload) => {
|
|
71
|
-
payload[0] ^= 0xff;
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
expect(() => decodeAddress(mutated)).toThrow("Incorrect address version");
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
function mutatePayload(
|
|
79
|
-
address: string,
|
|
80
|
-
mutator: (payload: Uint8Array) => void,
|
|
81
|
-
): string {
|
|
82
|
-
const decoded = bech32m.decode(address, ADDRESS_LENGTH_LIMIT);
|
|
83
|
-
const payload = Uint8Array.from(bech32m.fromWords(decoded.words));
|
|
84
|
-
mutator(payload);
|
|
85
|
-
return bech32m.encode(
|
|
86
|
-
decoded.prefix,
|
|
87
|
-
bech32m.toWords(payload),
|
|
88
|
-
ADDRESS_LENGTH_LIMIT,
|
|
89
|
-
);
|
|
90
|
-
}
|
package/key-derivation/bech32.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { bech32m } from "@scure/base";
|
|
2
|
-
|
|
3
|
-
import { ByteLength, ByteUtils } from "./bytes.js";
|
|
4
|
-
|
|
5
|
-
export type Chain = {
|
|
6
|
-
type: number;
|
|
7
|
-
id: number;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export type AddressData = {
|
|
11
|
-
masterPublicKey: bigint;
|
|
12
|
-
viewingPublicKey: Uint8Array;
|
|
13
|
-
chain?: Chain;
|
|
14
|
-
version?: number;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export const ADDRESS_VERSION = 1;
|
|
18
|
-
export const ADDRESS_LENGTH_LIMIT = 127;
|
|
19
|
-
export const ALL_CHAINS_NETWORK_ID = "ffffffffffffffff";
|
|
20
|
-
|
|
21
|
-
const PREFIX = "0zk";
|
|
22
|
-
const XOR_SALT = new TextEncoder().encode("unlink");
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* XOR the network identifier with a static salt to keep addresses compact and avoid
|
|
26
|
-
* exposing raw chain IDs directly (mirrors the reference implementation).
|
|
27
|
-
*/
|
|
28
|
-
const xorNetworkID = (networkID: string): string => {
|
|
29
|
-
const bytes = ByteUtils.hexStringToBytes(networkID);
|
|
30
|
-
const result = new Uint8Array(bytes.length);
|
|
31
|
-
const saltLength = XOR_SALT.length;
|
|
32
|
-
for (let i = 0; i < bytes.length; i += 1) {
|
|
33
|
-
const byte = bytes[i] ?? 0;
|
|
34
|
-
let saltByte = 0;
|
|
35
|
-
if (saltLength > 0) {
|
|
36
|
-
const saltIndex = i % saltLength;
|
|
37
|
-
saltByte = XOR_SALT[saltIndex] ?? 0;
|
|
38
|
-
}
|
|
39
|
-
result[i] = byte ^ saltByte;
|
|
40
|
-
}
|
|
41
|
-
return ByteUtils.bytesToHex(result);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const chainToNetworkID = (chain?: Chain): string => {
|
|
45
|
-
if (!chain) return ALL_CHAINS_NETWORK_ID;
|
|
46
|
-
const { type, id } = chain;
|
|
47
|
-
const typeHex = (type & 0xff).toString(16).padStart(2, "0");
|
|
48
|
-
const idHex = BigInt(id).toString(16).padStart(14, "0");
|
|
49
|
-
return `${typeHex}${idHex}`;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const networkIDToChain = (networkID: string): Chain | undefined => {
|
|
53
|
-
if (networkID === ALL_CHAINS_NETWORK_ID) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
const type = parseInt(networkID.slice(0, 2), 16);
|
|
57
|
-
const id = parseInt(networkID.slice(2), 16);
|
|
58
|
-
return { type, id };
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Encode address metadata into a Bech32m string with the 0zk prefix.
|
|
63
|
-
*/
|
|
64
|
-
export const encodeAddress = (addressData: AddressData): string => {
|
|
65
|
-
const versionHex = (addressData.version ?? ADDRESS_VERSION)
|
|
66
|
-
.toString(16)
|
|
67
|
-
.padStart(2, "0");
|
|
68
|
-
const masterPublicKey = ByteUtils.nToHex(
|
|
69
|
-
addressData.masterPublicKey,
|
|
70
|
-
ByteLength.UINT_256,
|
|
71
|
-
false,
|
|
72
|
-
);
|
|
73
|
-
const viewingPublicKey = ByteUtils.formatToByteLength(
|
|
74
|
-
addressData.viewingPublicKey,
|
|
75
|
-
ByteLength.UINT_256,
|
|
76
|
-
);
|
|
77
|
-
const networkID = xorNetworkID(chainToNetworkID(addressData.chain));
|
|
78
|
-
|
|
79
|
-
const payloadHex = `${versionHex}${masterPublicKey}${networkID}${viewingPublicKey}`;
|
|
80
|
-
const payload = ByteUtils.hexStringToBytes(payloadHex);
|
|
81
|
-
|
|
82
|
-
return bech32m.encode(PREFIX, bech32m.toWords(payload), ADDRESS_LENGTH_LIMIT);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Decode and validate a Bech32m address, returning the structured payload.
|
|
87
|
-
*/
|
|
88
|
-
export const decodeAddress = (address: string): AddressData => {
|
|
89
|
-
if (!address) {
|
|
90
|
-
throw new Error("No address to decode");
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const decoded = bech32m.decode(
|
|
94
|
-
address as `${string}1${string}`,
|
|
95
|
-
ADDRESS_LENGTH_LIMIT,
|
|
96
|
-
);
|
|
97
|
-
if (decoded.prefix !== PREFIX) {
|
|
98
|
-
throw new Error("Invalid address prefix");
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const payloadHex = ByteUtils.hexlify(bech32m.fromWords(decoded.words));
|
|
102
|
-
if (payloadHex.length !== 2 + 64 + 16 + 64) {
|
|
103
|
-
throw new Error("Incorrect address payload length");
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const version = parseInt(payloadHex.slice(0, 2), 16);
|
|
107
|
-
if (version !== ADDRESS_VERSION) {
|
|
108
|
-
throw new Error("Incorrect address version");
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const masterPublicKey = ByteUtils.hexToBigInt(payloadHex.slice(2, 66));
|
|
112
|
-
const networkID = xorNetworkID(payloadHex.slice(66, 82));
|
|
113
|
-
const viewingPublicKey = ByteUtils.hexStringToBytes(
|
|
114
|
-
payloadHex.slice(82, 146),
|
|
115
|
-
);
|
|
116
|
-
const chain = networkIDToChain(networkID);
|
|
117
|
-
|
|
118
|
-
return {
|
|
119
|
-
version,
|
|
120
|
-
masterPublicKey,
|
|
121
|
-
viewingPublicKey,
|
|
122
|
-
chain,
|
|
123
|
-
};
|
|
124
|
-
};
|
package/key-derivation/bip32.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { ByteUtils, fromUTF8String } from "./bytes.js";
|
|
2
|
-
import { sha512HMAC } from "./hash.js";
|
|
3
|
-
|
|
4
|
-
const CURVE_SEED = fromUTF8String("babyjubjub seed");
|
|
5
|
-
|
|
6
|
-
export type KeyNode = {
|
|
7
|
-
chainKey: string;
|
|
8
|
-
chainCode: string;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
// Validates derivation paths of the form m/... with hardened segments.
|
|
12
|
-
const PATH_REGEX = /^m(\/[0-9]+')+$/;
|
|
13
|
-
|
|
14
|
-
const isValidPath = (path: string): boolean => {
|
|
15
|
-
return PATH_REGEX.test(path);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Parse a derivation path into hardened segment indexes.
|
|
20
|
-
*/
|
|
21
|
-
export const getPathSegments = (path: string): number[] => {
|
|
22
|
-
if (!isValidPath(path)) {
|
|
23
|
-
throw new Error("Invalid derivation path");
|
|
24
|
-
}
|
|
25
|
-
return path
|
|
26
|
-
.split("/")
|
|
27
|
-
.slice(1)
|
|
28
|
-
.map((segment) => segment.replace("'", ""))
|
|
29
|
-
.map((segment) => parseInt(segment, 10));
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Perform hardened child derivation using the BabyJubJub curve seed.
|
|
34
|
-
*/
|
|
35
|
-
export const childKeyDerivationHardened = (
|
|
36
|
-
node: KeyNode,
|
|
37
|
-
index: number,
|
|
38
|
-
offset: number = 0x80000000,
|
|
39
|
-
): KeyNode => {
|
|
40
|
-
const indexFormatted = ByteUtils.padToLength(index + offset, 4);
|
|
41
|
-
const preImage = `00${node.chainKey}${indexFormatted}`;
|
|
42
|
-
const I = sha512HMAC(node.chainCode, preImage);
|
|
43
|
-
const chainKey = I.slice(0, 64);
|
|
44
|
-
const chainCode = I.slice(64);
|
|
45
|
-
return { chainKey, chainCode };
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Create the root BIP-32 node from a BIP-39 seed.
|
|
50
|
-
*/
|
|
51
|
-
export const getMasterKeyFromSeed = (seed: string): KeyNode => {
|
|
52
|
-
const I = sha512HMAC(CURVE_SEED, seed);
|
|
53
|
-
const chainKey = I.slice(0, 64);
|
|
54
|
-
const chainCode = I.slice(64);
|
|
55
|
-
return { chainKey, chainCode };
|
|
56
|
-
};
|