@unlink-xyz/core 0.1.0 → 0.1.3-canary.01ac52d
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 +56253 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/wallet/index.js +55974 -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 +35 -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 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9969 -16
- 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 -17
- 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 -27
- 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 +9712 -0
- package/dist/wallet/index.js.map +1 -0
- package/dist/wallet/sdk.d.ts +47 -0
- package/dist/wallet/sdk.d.ts.map +1 -0
- package/dist/wallet/types.d.ts +455 -0
- package/dist/wallet/types.d.ts.map +1 -0
- package/dist/wallet/unlink-wallet.d.ts +186 -0
- package/dist/wallet/unlink-wallet.d.ts.map +1 -0
- package/package.json +40 -14
- package/dist/account/zkAccount.d.ts.map +0 -1
- package/dist/account/zkAccount.js +0 -128
- 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/index.js +0 -2
- 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/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 -173
- 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/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/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/validators.js +0 -70
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { IMTMerkleProof } from "@zk-kit/imt";
|
|
2
|
-
export type AddLeafResult = {
|
|
3
|
-
index: number;
|
|
4
|
-
root: string;
|
|
5
|
-
};
|
|
6
|
-
export type MerkleProof = {
|
|
7
|
-
root: string;
|
|
8
|
-
leaf: string;
|
|
9
|
-
pathElements: string[][];
|
|
10
|
-
pathIndices: number[];
|
|
11
|
-
leafIndex: number;
|
|
12
|
-
};
|
|
13
|
-
export declare function createLocalMerkleTree(): {
|
|
14
|
-
addLeaf: (value: bigint) => AddLeafResult;
|
|
15
|
-
getRoot: () => string;
|
|
16
|
-
getLeafCount: () => number;
|
|
17
|
-
getLeaf: (index: number) => string;
|
|
18
|
-
createMerkleProof: (index: number) => IMTMerkleProof;
|
|
19
|
-
getZero(level: number): string;
|
|
20
|
-
readonly depth: number;
|
|
21
|
-
readonly capacity: number;
|
|
22
|
-
};
|
|
23
|
-
export type LocalMerkleTree = ReturnType<typeof createLocalMerkleTree>;
|
|
24
|
-
export type LocalMerkleTrees = {
|
|
25
|
-
get(chainId: number): LocalMerkleTree | undefined;
|
|
26
|
-
getOrCreate(chainId: number): LocalMerkleTree;
|
|
27
|
-
addLeaf(chainId: number, value: bigint): AddLeafResult;
|
|
28
|
-
createMerkleProof(chainId: number, index: number): IMTMerkleProof;
|
|
29
|
-
getRoot(chainId: number): string;
|
|
30
|
-
getLeafCount(chainId: number): number;
|
|
31
|
-
reset(chainId: number): void;
|
|
32
|
-
};
|
|
33
|
-
export declare function createMerkleTrees(): LocalMerkleTrees;
|
|
34
|
-
//# sourceMappingURL=merkle-tree.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"merkle-tree.d.ts","sourceRoot":"","sources":["../../state/merkle-tree.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,cAAc,EAGf,MAAM,aAAa,CAAC;AAWrB,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,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;AAaF,wBAAgB,qBAAqB;qBAUX,MAAM,KAAG,aAAa;;;qBAsBtB,MAAM;+BAQI,MAAM,KAAG,cAAc;mBAUxC,MAAM;;;EAiBxB;AAED,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEvE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAAC;IAClD,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe,CAAC;IAC9C,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,aAAa,CAAC;IACvD,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,cAAc,CAAC;IAClE,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACjC,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B,CAAC;AAEF,wBAAgB,iBAAiB,IAAI,gBAAgB,CAwCpD"}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { poseidon } from "@railgun-community/circomlibjs";
|
|
2
|
-
import { IMT, } from "@zk-kit/imt";
|
|
3
|
-
import { ByteLength, ByteUtils } from "../key-derivation/bytes.js";
|
|
4
|
-
import { ensureChainId } from "../utils/validators.js";
|
|
5
|
-
const DEFAULT_DEPTH = 16;
|
|
6
|
-
const DEFAULT_ARITY = 2;
|
|
7
|
-
const ZERO_BYTE_LENGTH = ByteLength.UINT_256;
|
|
8
|
-
// Simple adapter: IMTNode[] -> poseidon(bigint[])
|
|
9
|
-
const hash = (values) => poseidon(values.map((v) => BigInt(v)));
|
|
10
|
-
const formatNode = (value) => ByteUtils.nToHex(value, ZERO_BYTE_LENGTH, true);
|
|
11
|
-
export function createLocalMerkleTree() {
|
|
12
|
-
const zeroLeaf = 0n;
|
|
13
|
-
const capacity = 2 ** DEFAULT_DEPTH;
|
|
14
|
-
const tree = new IMT(hash, DEFAULT_DEPTH, zeroLeaf, DEFAULT_ARITY);
|
|
15
|
-
function addLeaf(value) {
|
|
16
|
-
if (tree.leaves.length >= capacity) {
|
|
17
|
-
throw new Error("merkle tree is full");
|
|
18
|
-
}
|
|
19
|
-
const insertedIndex = tree.leaves.length;
|
|
20
|
-
tree.insert(value);
|
|
21
|
-
return {
|
|
22
|
-
index: insertedIndex,
|
|
23
|
-
root: formatNode(tree.root),
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
function getRoot() {
|
|
27
|
-
return formatNode(tree.root);
|
|
28
|
-
}
|
|
29
|
-
function getLeafCount() {
|
|
30
|
-
return tree.leaves.length;
|
|
31
|
-
}
|
|
32
|
-
function getLeaf(index) {
|
|
33
|
-
const leaf = tree.leaves[index];
|
|
34
|
-
if (leaf === undefined) {
|
|
35
|
-
throw new Error("leaf does not exist in this tree");
|
|
36
|
-
}
|
|
37
|
-
return formatNode(BigInt(leaf));
|
|
38
|
-
}
|
|
39
|
-
function createMerkleProof(index) {
|
|
40
|
-
return tree.createProof(index);
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
addLeaf,
|
|
44
|
-
getRoot,
|
|
45
|
-
getLeafCount,
|
|
46
|
-
getLeaf,
|
|
47
|
-
createMerkleProof,
|
|
48
|
-
getZero(level) {
|
|
49
|
-
if (level === tree.depth) {
|
|
50
|
-
return getRoot();
|
|
51
|
-
}
|
|
52
|
-
const zero = tree.zeroes[level];
|
|
53
|
-
if (zero === undefined) {
|
|
54
|
-
throw new Error("invalid level");
|
|
55
|
-
}
|
|
56
|
-
return formatNode(zero);
|
|
57
|
-
},
|
|
58
|
-
get depth() {
|
|
59
|
-
return tree.depth;
|
|
60
|
-
},
|
|
61
|
-
get capacity() {
|
|
62
|
-
return capacity;
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
export function createMerkleTrees() {
|
|
67
|
-
const trees = new Map();
|
|
68
|
-
const resetTree = (chainId) => {
|
|
69
|
-
ensureChainId(chainId);
|
|
70
|
-
const tree = createLocalMerkleTree();
|
|
71
|
-
trees.set(chainId, tree);
|
|
72
|
-
return tree;
|
|
73
|
-
};
|
|
74
|
-
const getOrCreate = (chainId) => {
|
|
75
|
-
ensureChainId(chainId);
|
|
76
|
-
let tree = trees.get(chainId);
|
|
77
|
-
if (!tree) {
|
|
78
|
-
tree = resetTree(chainId);
|
|
79
|
-
}
|
|
80
|
-
return tree;
|
|
81
|
-
};
|
|
82
|
-
return {
|
|
83
|
-
get(chainId) {
|
|
84
|
-
ensureChainId(chainId);
|
|
85
|
-
return trees.get(chainId);
|
|
86
|
-
},
|
|
87
|
-
getOrCreate,
|
|
88
|
-
addLeaf(chainId, value) {
|
|
89
|
-
return getOrCreate(chainId).addLeaf(value);
|
|
90
|
-
},
|
|
91
|
-
createMerkleProof(chainId, index) {
|
|
92
|
-
return getOrCreate(chainId).createMerkleProof(index);
|
|
93
|
-
},
|
|
94
|
-
getRoot(chainId) {
|
|
95
|
-
return getOrCreate(chainId).getRoot();
|
|
96
|
-
},
|
|
97
|
-
getLeafCount(chainId) {
|
|
98
|
-
return getOrCreate(chainId).getLeafCount();
|
|
99
|
-
},
|
|
100
|
-
reset(chainId) {
|
|
101
|
-
resetTree(chainId);
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { Storage } from "../types.js";
|
|
2
|
-
import type { NoteInsert, NoteRecord } from "./records.js";
|
|
3
|
-
export declare const emptyBytes: () => Uint8Array<ArrayBuffer>;
|
|
4
|
-
export declare function createNoteStore(storage: Storage): {
|
|
5
|
-
/**
|
|
6
|
-
* Upsert a note record and maintain the unspent-note index for fast balance queries.
|
|
7
|
-
*/
|
|
8
|
-
putNote(note: NoteInsert): Promise<void>;
|
|
9
|
-
/**
|
|
10
|
-
* Fetch a note by (chainId, index) if it exists in local storage.
|
|
11
|
-
*/
|
|
12
|
-
getNote(chainId: number, index: number): Promise<NoteRecord | null>;
|
|
13
|
-
/**
|
|
14
|
-
* List notes matching optional filters (by chainId, mpk) and spent state.
|
|
15
|
-
*/
|
|
16
|
-
listNotes(options?: {
|
|
17
|
-
chainId?: number;
|
|
18
|
-
mpk?: string;
|
|
19
|
-
token?: string;
|
|
20
|
-
includeSpent?: boolean;
|
|
21
|
-
}): Promise<NoteRecord[]>;
|
|
22
|
-
/**
|
|
23
|
-
* Mark a cached note as spent by setting its timestamp and re-indexing metadata.
|
|
24
|
-
*/
|
|
25
|
-
markNoteSpent(chainId: number, index: number, spentAt?: number): Promise<NoteRecord>;
|
|
26
|
-
/**
|
|
27
|
-
* Drop the spent marker from a cached note, returning it to the unspent index.
|
|
28
|
-
*/
|
|
29
|
-
markNoteUnspent(chainId: number, index: number): Promise<NoteRecord>;
|
|
30
|
-
/**
|
|
31
|
-
* Aggregate unspent note balances per token for a given master public key.
|
|
32
|
-
*/
|
|
33
|
-
getZkBalances(mpk: string, options?: {
|
|
34
|
-
chainId?: number;
|
|
35
|
-
}): Promise<Record<string, bigint>>;
|
|
36
|
-
};
|
|
37
|
-
//# sourceMappingURL=note-store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"note-store.d.ts","sourceRoot":"","sources":["../../state/note-store.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAW,OAAO,EAAE,MAAM,aAAa,CAAC;AAapD,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE3D,eAAO,MAAM,UAAU,+BAA0B,CAAC;AAElD,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO;IAyC5C;;OAEG;kBACiB,UAAU;IAI9B;;OAEG;qBACoB,MAAM,SAAS,MAAM;IAM5C;;OAEG;wBAEQ;QACP,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,YAAY,CAAC,EAAE,OAAO,CAAC;KACxB;IAyBH;;OAEG;2BAC0B,MAAM,SAAS,MAAM;IAgBlD;;OAEG;6BAC4B,MAAM,SAAS,MAAM;IAgBpD;;OAEG;uBACsB,MAAM,YAAW;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE;EAiBjE"}
|
package/dist/state/note-store.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { CoreError } from "../errors.js";
|
|
2
|
-
import { keys } from "../keys.js";
|
|
3
|
-
import { decodeJson, encodeJson, getJson, } from "../utils/json-codec.js";
|
|
4
|
-
import { ensureBigIntString, ensureChainId, ensureMpk, ensurePositiveInt, } from "../utils/validators.js";
|
|
5
|
-
export const emptyBytes = () => new Uint8Array(0);
|
|
6
|
-
export function createNoteStore(storage) {
|
|
7
|
-
const persistNote = async (record) => {
|
|
8
|
-
ensureChainId(record.chainId);
|
|
9
|
-
ensurePositiveInt("note index", record.index);
|
|
10
|
-
ensureMpk(record.mpk);
|
|
11
|
-
ensureBigIntString("note value", record.value);
|
|
12
|
-
if (record.spentAt !== undefined) {
|
|
13
|
-
ensurePositiveInt("note spentAt", record.spentAt);
|
|
14
|
-
}
|
|
15
|
-
const noteKey = keys.note(record.chainId, record.index);
|
|
16
|
-
const previous = await getJson(storage, noteKey);
|
|
17
|
-
const ops = [
|
|
18
|
-
{ put: [noteKey, encodeJson(record)] },
|
|
19
|
-
];
|
|
20
|
-
if (previous && previous.spentAt === undefined) {
|
|
21
|
-
ops.push({
|
|
22
|
-
del: keys.unspent(previous.chainId, previous.mpk, previous.index),
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
if (record.spentAt === undefined) {
|
|
26
|
-
// idx:notes:unspent functions as a set, so we only persist the sentinel bytes.
|
|
27
|
-
ops.push({
|
|
28
|
-
put: [
|
|
29
|
-
keys.unspent(record.chainId, record.mpk, record.index),
|
|
30
|
-
emptyBytes(),
|
|
31
|
-
],
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
ops.push({
|
|
36
|
-
del: keys.unspent(record.chainId, record.mpk, record.index),
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
await storage.batch(ops);
|
|
40
|
-
};
|
|
41
|
-
const store = {
|
|
42
|
-
/**
|
|
43
|
-
* Upsert a note record and maintain the unspent-note index for fast balance queries.
|
|
44
|
-
*/
|
|
45
|
-
async putNote(note) {
|
|
46
|
-
await persistNote({ ...note });
|
|
47
|
-
},
|
|
48
|
-
/**
|
|
49
|
-
* Fetch a note by (chainId, index) if it exists in local storage.
|
|
50
|
-
*/
|
|
51
|
-
async getNote(chainId, index) {
|
|
52
|
-
ensureChainId(chainId);
|
|
53
|
-
ensurePositiveInt("note index", index);
|
|
54
|
-
return getJson(storage, keys.note(chainId, index));
|
|
55
|
-
},
|
|
56
|
-
/**
|
|
57
|
-
* List notes matching optional filters (by chainId, mpk) and spent state.
|
|
58
|
-
*/
|
|
59
|
-
async listNotes(options = {}) {
|
|
60
|
-
const { chainId, mpk, token, includeSpent = true } = options;
|
|
61
|
-
if (chainId !== undefined) {
|
|
62
|
-
ensureChainId(chainId);
|
|
63
|
-
}
|
|
64
|
-
if (mpk !== undefined) {
|
|
65
|
-
ensureMpk(mpk);
|
|
66
|
-
}
|
|
67
|
-
const prefix = chainId !== undefined ? `notes:${chainId}:` : "notes:";
|
|
68
|
-
const entries = await storage.iter({ prefix });
|
|
69
|
-
const filtered = entries
|
|
70
|
-
.map(({ value }) => decodeJson(value))
|
|
71
|
-
.filter((note) => (chainId === undefined || note.chainId === chainId) &&
|
|
72
|
-
(mpk === undefined || note.mpk === mpk) &&
|
|
73
|
-
(token === undefined || note.token === token) &&
|
|
74
|
-
(includeSpent || note.spentAt === undefined));
|
|
75
|
-
filtered.sort((a, b) => a.index - b.index);
|
|
76
|
-
return filtered;
|
|
77
|
-
},
|
|
78
|
-
/**
|
|
79
|
-
* Mark a cached note as spent by setting its timestamp and re-indexing metadata.
|
|
80
|
-
*/
|
|
81
|
-
async markNoteSpent(chainId, index, spentAt = Date.now()) {
|
|
82
|
-
ensureChainId(chainId);
|
|
83
|
-
ensurePositiveInt("note index", index);
|
|
84
|
-
ensurePositiveInt("note spentAt", spentAt);
|
|
85
|
-
const existing = await store.getNote(chainId, index);
|
|
86
|
-
if (!existing) {
|
|
87
|
-
throw new CoreError("note not found");
|
|
88
|
-
}
|
|
89
|
-
if (existing.spentAt === spentAt) {
|
|
90
|
-
return existing;
|
|
91
|
-
}
|
|
92
|
-
const updated = { ...existing, spentAt };
|
|
93
|
-
await persistNote(updated);
|
|
94
|
-
return updated;
|
|
95
|
-
},
|
|
96
|
-
/**
|
|
97
|
-
* Drop the spent marker from a cached note, returning it to the unspent index.
|
|
98
|
-
*/
|
|
99
|
-
async markNoteUnspent(chainId, index) {
|
|
100
|
-
ensureChainId(chainId);
|
|
101
|
-
ensurePositiveInt("note index", index);
|
|
102
|
-
const existing = await store.getNote(chainId, index);
|
|
103
|
-
if (!existing) {
|
|
104
|
-
throw new CoreError("note not found");
|
|
105
|
-
}
|
|
106
|
-
if (existing.spentAt === undefined) {
|
|
107
|
-
return existing;
|
|
108
|
-
}
|
|
109
|
-
const updated = { ...existing };
|
|
110
|
-
delete updated.spentAt;
|
|
111
|
-
await persistNote(updated);
|
|
112
|
-
return updated;
|
|
113
|
-
},
|
|
114
|
-
/**
|
|
115
|
-
* Aggregate unspent note balances per token for a given master public key.
|
|
116
|
-
*/
|
|
117
|
-
async getZkBalances(mpk, options = {}) {
|
|
118
|
-
ensureMpk(mpk);
|
|
119
|
-
const notes = await store.listNotes({
|
|
120
|
-
chainId: options.chainId,
|
|
121
|
-
mpk,
|
|
122
|
-
includeSpent: false,
|
|
123
|
-
});
|
|
124
|
-
const totals = {};
|
|
125
|
-
for (const note of notes) {
|
|
126
|
-
const amount = BigInt(note.value);
|
|
127
|
-
totals[note.token] = (totals[note.token] ?? 0n) + amount;
|
|
128
|
-
}
|
|
129
|
-
return totals;
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
return store;
|
|
133
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Storage } from "../types.js";
|
|
2
|
-
import type { NullifierRecord } from "./records.js";
|
|
3
|
-
export declare function createNullifierStore(storage: Storage): {
|
|
4
|
-
/**
|
|
5
|
-
* Record a nullifier observation so later syncs can de-duplicate work.
|
|
6
|
-
*/
|
|
7
|
-
putNullifier(nullifier: NullifierRecord): Promise<void>;
|
|
8
|
-
/**
|
|
9
|
-
* Lookup a previously observed nullifier by value.
|
|
10
|
-
*/
|
|
11
|
-
getNullifier(chainId: number, value: string): Promise<NullifierRecord | null>;
|
|
12
|
-
};
|
|
13
|
-
//# sourceMappingURL=nullifier-store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nullifier-store.d.ts","sourceRoot":"","sources":["../../state/nullifier-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,OAAO;IAEjD;;OAEG;4BAC2B,eAAe;IAS7C;;OAEG;0BACyB,MAAM,SAAS,MAAM;EAKpD"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { keys } from "../keys.js";
|
|
2
|
-
import { getJson, putJson } from "../utils/json-codec.js";
|
|
3
|
-
import { ensureChainId } from "../utils/validators.js";
|
|
4
|
-
export function createNullifierStore(storage) {
|
|
5
|
-
return {
|
|
6
|
-
/**
|
|
7
|
-
* Record a nullifier observation so later syncs can de-duplicate work.
|
|
8
|
-
*/
|
|
9
|
-
async putNullifier(nullifier) {
|
|
10
|
-
ensureChainId(nullifier.chainId);
|
|
11
|
-
await putJson(storage, keys.nullifier(nullifier.chainId, nullifier.nullifier), nullifier);
|
|
12
|
-
},
|
|
13
|
-
/**
|
|
14
|
-
* Lookup a previously observed nullifier by value.
|
|
15
|
-
*/
|
|
16
|
-
async getNullifier(chainId, value) {
|
|
17
|
-
ensureChainId(chainId);
|
|
18
|
-
return getJson(storage, keys.nullifier(chainId, value));
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
}
|
package/dist/state/records.d.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Locally cached metadata for a deposit note we consider owned.
|
|
3
|
-
*/
|
|
4
|
-
export type NoteRecord = {
|
|
5
|
-
chainId: number;
|
|
6
|
-
/** Leaf index within the on-chain tree (zero-based, monotonic). */
|
|
7
|
-
index: number;
|
|
8
|
-
/** Canonical asset identifier, typically the ERC-20 address lowercased. */
|
|
9
|
-
token: string;
|
|
10
|
-
/** Amount expressed as an unsigned BigInt serialized to a base-10 string. */
|
|
11
|
-
value: string;
|
|
12
|
-
/** Commitment stored on-chain (Poseidon(note)). */
|
|
13
|
-
commitment: string;
|
|
14
|
-
/** Note public key derived from the master public key. */
|
|
15
|
-
npk: string;
|
|
16
|
-
/**
|
|
17
|
-
* Master public key for the account (Poseidon(spendingPk.public, nullifyingKey)).
|
|
18
|
-
* Acts as the stable account identifier when grouping notes locally.
|
|
19
|
-
*/
|
|
20
|
-
mpk: string;
|
|
21
|
-
/** Randomizer used when committing the note. */
|
|
22
|
-
random: string;
|
|
23
|
-
/** Nullifier that will be revealed when the note is spent. */
|
|
24
|
-
nullifier: string;
|
|
25
|
-
/** Optional timestamp when the note was marked as spent. */
|
|
26
|
-
spentAt?: number;
|
|
27
|
-
};
|
|
28
|
-
export type NoteInsert = Omit<NoteRecord, 'spentAt'>;
|
|
29
|
-
/**
|
|
30
|
-
* Details about a nullifier observed on-chain, whether or not we own the note.
|
|
31
|
-
*/
|
|
32
|
-
export type NullifierRecord = {
|
|
33
|
-
chainId: number;
|
|
34
|
-
/** Nullifier value (expected to match NoteRecord.nullifier). */
|
|
35
|
-
nullifier: string;
|
|
36
|
-
/** Leaf index of the corresponding note when known. */
|
|
37
|
-
noteIndex?: number;
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* Public information about a leaf commitment inserted into the pool's tree.
|
|
41
|
-
*/
|
|
42
|
-
export type LeafRecord = {
|
|
43
|
-
chainId: number;
|
|
44
|
-
/** Leaf index inside the tree (zero-based). */
|
|
45
|
-
index: number;
|
|
46
|
-
/** Commitment value stored in the tree. */
|
|
47
|
-
commitment: string;
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* Historical merkle root snapshot used for proof construction.
|
|
51
|
-
*/
|
|
52
|
-
export type RootRecord = {
|
|
53
|
-
chainId: number;
|
|
54
|
-
/** Merkle root value committed on-chain. */
|
|
55
|
-
root: string;
|
|
56
|
-
};
|
|
57
|
-
//# sourceMappingURL=records.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"records.d.ts","sourceRoot":"","sources":["../../state/records.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,mEAAmE;IACnE,KAAK,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,KAAK,EAAE,MAAM,CAAC;IACd,6EAA6E;IAC7E,KAAK,EAAE,MAAM,CAAC;IACd,mDAAmD;IACnD,UAAU,EAAE,MAAM,CAAC;IACnB,0DAA0D;IAC1D,GAAG,EAAE,MAAM,CAAC;IACZ;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAC;IACf,8DAA8D;IAC9D,SAAS,EAAE,MAAM,CAAC;IAClB,4DAA4D;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAGF,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;AAErD;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAClB,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,4CAA4C;IAC5C,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
|
package/dist/state/records.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Storage } from "../types.js";
|
|
2
|
-
import type { RootRecord } from "./records.js";
|
|
3
|
-
export declare function createRootStore(storage: Storage): {
|
|
4
|
-
/**
|
|
5
|
-
* Persist a historical root snapshot for proof generation.
|
|
6
|
-
*/
|
|
7
|
-
putRoot(root: RootRecord): Promise<void>;
|
|
8
|
-
/**
|
|
9
|
-
* Retrieve a root snapshot by Merkle root value.
|
|
10
|
-
*/
|
|
11
|
-
getRoot(chainId: number, value: string): Promise<RootRecord | null>;
|
|
12
|
-
};
|
|
13
|
-
//# sourceMappingURL=root-store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"root-store.d.ts","sourceRoot":"","sources":["../../state/root-store.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO;IAE5C;;OAEG;kBACiB,UAAU;IAU9B;;OAEG;qBACoB,MAAM,SAAS,MAAM;EAQ/C"}
|
package/dist/state/root-store.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { CoreError } from "../errors.js";
|
|
2
|
-
import { keys, validateKey } from "../keys.js";
|
|
3
|
-
import { getJson, putJson } from "../utils/json-codec.js";
|
|
4
|
-
import { ensureChainId } from "../utils/validators.js";
|
|
5
|
-
export function createRootStore(storage) {
|
|
6
|
-
return {
|
|
7
|
-
/**
|
|
8
|
-
* Persist a historical root snapshot for proof generation.
|
|
9
|
-
*/
|
|
10
|
-
async putRoot(root) {
|
|
11
|
-
ensureChainId(root.chainId);
|
|
12
|
-
if (typeof root.root !== "string" || root.root.length === 0) {
|
|
13
|
-
throw new CoreError("root value must be a non-empty string");
|
|
14
|
-
}
|
|
15
|
-
const key = keys.root(root.chainId, root.root);
|
|
16
|
-
validateKey(key);
|
|
17
|
-
await putJson(storage, key, root);
|
|
18
|
-
},
|
|
19
|
-
/**
|
|
20
|
-
* Retrieve a root snapshot by Merkle root value.
|
|
21
|
-
*/
|
|
22
|
-
async getRoot(chainId, value) {
|
|
23
|
-
ensureChainId(chainId);
|
|
24
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
25
|
-
throw new CoreError("root value must be a non-empty string");
|
|
26
|
-
}
|
|
27
|
-
return getJson(storage, keys.root(chainId, value));
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { keys, validateKey } from "../../keys.js";
|
|
2
|
-
import { ensureChainId, ensurePositiveInt } from "../../utils/validators.js";
|
|
3
|
-
export function createCiphertextStore(storage) {
|
|
4
|
-
return {
|
|
5
|
-
/**
|
|
6
|
-
* Store encrypted note payloads alongside their on-chain commitments.
|
|
7
|
-
*/
|
|
8
|
-
async putCiphertext(chainId, index, payload) {
|
|
9
|
-
ensureChainId(chainId);
|
|
10
|
-
ensurePositiveInt("ciphertext index", index);
|
|
11
|
-
const key = keys.ciphertext(chainId, index);
|
|
12
|
-
validateKey(key);
|
|
13
|
-
await storage.put(key, new Uint8Array(payload));
|
|
14
|
-
},
|
|
15
|
-
/**
|
|
16
|
-
* Retrieve encrypted note payloads; returns null when the ciphertext is missing.
|
|
17
|
-
*/
|
|
18
|
-
async getCiphertext(chainId, index) {
|
|
19
|
-
ensureChainId(chainId);
|
|
20
|
-
ensurePositiveInt("ciphertext index", index);
|
|
21
|
-
const data = await storage.get(keys.ciphertext(chainId, index));
|
|
22
|
-
return data ? new Uint8Array(data) : null;
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export { createStateStore } from "./store.js";
|
|
2
|
-
export { createLeafStore } from "./leaf-store.js";
|
|
3
|
-
export { createNoteStore } from "./note-store.js";
|
|
4
|
-
export { createNullifierStore } from "./nullifier-store.js";
|
|
5
|
-
export { createRootStore } from "./root-store.js";
|
|
6
|
-
export { createCiphertextStore } from "./ciphertext-store.js";
|
|
7
|
-
export { createJobStore } from "./job-store.js";
|
|
8
|
-
export { DEFAULT_JOB_TIMEOUT_MS, } from "./jobs.js";
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { keys } from "../../keys.js";
|
|
2
|
-
import { decodeJson, encodeJson, getJson } from "../../utils/json-codec.js";
|
|
3
|
-
import { ensureAddress, ensureChainId, ensurePositiveInt, } from "../../utils/validators.js";
|
|
4
|
-
import { DEFAULT_JOB_TIMEOUT_MS } from "../store/jobs.js";
|
|
5
|
-
const VALID_STATUSES = [
|
|
6
|
-
"pending",
|
|
7
|
-
"submitted",
|
|
8
|
-
"broadcasting",
|
|
9
|
-
"succeeded",
|
|
10
|
-
"failed",
|
|
11
|
-
"dead",
|
|
12
|
-
];
|
|
13
|
-
function assertStatus(status) {
|
|
14
|
-
if (!VALID_STATUSES.includes(status)) {
|
|
15
|
-
throw new Error(`invalid job status: ${status}`);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
function assertKind(kind) {
|
|
19
|
-
if (kind !== "deposit" && kind !== "transact") {
|
|
20
|
-
throw new Error(`invalid job kind: ${kind}`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function normalizeTimestamps(job) {
|
|
24
|
-
const createdAt = typeof job.createdAt === "number" && Number.isFinite(job.createdAt)
|
|
25
|
-
? job.createdAt
|
|
26
|
-
: Date.now();
|
|
27
|
-
const timeoutMs = typeof job.timeoutMs === "number" && job.timeoutMs > 0
|
|
28
|
-
? job.timeoutMs
|
|
29
|
-
: DEFAULT_JOB_TIMEOUT_MS;
|
|
30
|
-
return {
|
|
31
|
-
...job,
|
|
32
|
-
createdAt,
|
|
33
|
-
timeoutMs,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
function validateTransactContext(context) {
|
|
37
|
-
ensurePositiveInt("context index", context.index);
|
|
38
|
-
if (!context.nullifier) {
|
|
39
|
-
throw new Error("context nullifier is required");
|
|
40
|
-
}
|
|
41
|
-
const { witness } = context;
|
|
42
|
-
if (!witness) {
|
|
43
|
-
throw new Error("context witness is required");
|
|
44
|
-
}
|
|
45
|
-
ensurePositiveInt("witness leafIndex", witness.leafIndex);
|
|
46
|
-
if (!Array.isArray(witness.pathElements) ||
|
|
47
|
-
!Array.isArray(witness.pathIndices)) {
|
|
48
|
-
throw new Error("witness pathElements and pathIndices are required");
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function validateTransactJob(job) {
|
|
52
|
-
ensureAddress("pool address", job.poolAddress);
|
|
53
|
-
if (!job.calldata) {
|
|
54
|
-
throw new Error("calldata is required for transact job");
|
|
55
|
-
}
|
|
56
|
-
job.contexts.forEach(validateTransactContext);
|
|
57
|
-
job.predictedOutputs.forEach((output) => {
|
|
58
|
-
if (!output.hex) {
|
|
59
|
-
throw new Error("predicted output hex is required");
|
|
60
|
-
}
|
|
61
|
-
ensurePositiveInt("predicted output index", output.index);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
function validateDepositJob(job) {
|
|
65
|
-
if (!job.predictedCommitment?.hex) {
|
|
66
|
-
throw new Error("predicted commitment hex is required");
|
|
67
|
-
}
|
|
68
|
-
if (job.predictedCommitment.index !== undefined) {
|
|
69
|
-
ensurePositiveInt("predicted commitment index", job.predictedCommitment.index);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
function validateJob(job) {
|
|
73
|
-
if (!job.relayId) {
|
|
74
|
-
throw new Error("relayId is required");
|
|
75
|
-
}
|
|
76
|
-
ensureChainId(job.chainId);
|
|
77
|
-
assertStatus(job.status);
|
|
78
|
-
assertKind(job.kind);
|
|
79
|
-
ensurePositiveInt("job createdAt", job.createdAt);
|
|
80
|
-
ensurePositiveInt("job timeoutMs", job.timeoutMs);
|
|
81
|
-
if (job.lastCheckedAt !== undefined) {
|
|
82
|
-
ensurePositiveInt("job lastCheckedAt", job.lastCheckedAt);
|
|
83
|
-
}
|
|
84
|
-
if (job.kind === "deposit") {
|
|
85
|
-
validateDepositJob(job);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
validateTransactJob(job);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
function buildJobKey(relayId) {
|
|
92
|
-
return keys.job(relayId);
|
|
93
|
-
}
|
|
94
|
-
export function createJobStore(storage) {
|
|
95
|
-
return {
|
|
96
|
-
async putPendingJob(job) {
|
|
97
|
-
const normalized = normalizeTimestamps(job);
|
|
98
|
-
validateJob(normalized);
|
|
99
|
-
await storage.put(buildJobKey(normalized.relayId), encodeJson(normalized));
|
|
100
|
-
},
|
|
101
|
-
async getPendingJob(relayId) {
|
|
102
|
-
return getJson(storage, buildJobKey(relayId));
|
|
103
|
-
},
|
|
104
|
-
async listPendingJobs(filter = {}) {
|
|
105
|
-
const entries = await storage.iter({ prefix: "jobs:" });
|
|
106
|
-
const statuses = filter.statuses ?? VALID_STATUSES;
|
|
107
|
-
const filtered = entries
|
|
108
|
-
.map(({ value }) => decodeJson(value))
|
|
109
|
-
.filter((job) => (filter.kind === undefined || job.kind === filter.kind) &&
|
|
110
|
-
statuses.includes(job.status));
|
|
111
|
-
filtered.sort((a, b) => a.createdAt - b.createdAt);
|
|
112
|
-
return filtered;
|
|
113
|
-
},
|
|
114
|
-
async deletePendingJob(relayId) {
|
|
115
|
-
await storage.delete(buildJobKey(relayId));
|
|
116
|
-
},
|
|
117
|
-
};
|
|
118
|
-
}
|
package/dist/state/store/jobs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const DEFAULT_JOB_TIMEOUT_MS = 5 * 60 * 1000;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { keys } from "../../keys.js";
|
|
2
|
-
import { getJson, putJson } from "../../utils/json-codec.js";
|
|
3
|
-
import { ensureChainId, ensurePositiveInt } from "../../utils/validators.js";
|
|
4
|
-
export function createLeafStore(storage) {
|
|
5
|
-
return {
|
|
6
|
-
/**
|
|
7
|
-
* Cache public leaf data pulled from the on-chain Merkle tree.
|
|
8
|
-
*/
|
|
9
|
-
async putLeaf(leaf) {
|
|
10
|
-
ensureChainId(leaf.chainId);
|
|
11
|
-
ensurePositiveInt("leaf index", leaf.index);
|
|
12
|
-
await putJson(storage, keys.leaf(leaf.chainId, leaf.index), leaf);
|
|
13
|
-
},
|
|
14
|
-
/**
|
|
15
|
-
* Load a leaf commitment by tree index.
|
|
16
|
-
*/
|
|
17
|
-
async getLeaf(chainId, index) {
|
|
18
|
-
ensureChainId(chainId);
|
|
19
|
-
ensurePositiveInt("leaf index", index);
|
|
20
|
-
return getJson(storage, keys.leaf(chainId, index));
|
|
21
|
-
},
|
|
22
|
-
/**
|
|
23
|
-
* Remove all cached leaves for a chain.
|
|
24
|
-
*/
|
|
25
|
-
async clearLeaves(chainId) {
|
|
26
|
-
ensureChainId(chainId);
|
|
27
|
-
const prefix = `leaves:${chainId}:`;
|
|
28
|
-
const entries = await storage.iter({ prefix });
|
|
29
|
-
if (entries.length === 0)
|
|
30
|
-
return;
|
|
31
|
-
const deletions = entries.map(({ key }) => ({ del: key }));
|
|
32
|
-
await storage.batch(deletions);
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
}
|