@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
package/dist/state.js
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { CoreError } from './errors.js';
|
|
2
|
-
import { keys, validateKey } from './keys.js';
|
|
3
|
-
import { decodeJson, encodeJson, getJson, putJson, } from './utils/json-codec.js';
|
|
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
|
-
function ensureMpk(mpk) {
|
|
15
|
-
if (typeof mpk !== 'string' || mpk.length === 0) {
|
|
16
|
-
throw new CoreError('note mpk must be a non-empty string');
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
function ensureBigIntString(label, value) {
|
|
20
|
-
try {
|
|
21
|
-
BigInt(value);
|
|
22
|
-
}
|
|
23
|
-
catch {
|
|
24
|
-
throw new CoreError(`${label} must be a base-10 bigint string`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
const emptyBytes = () => new Uint8Array(0);
|
|
28
|
-
export function createStateStore(storage) {
|
|
29
|
-
const store = {
|
|
30
|
-
async putNote(note) {
|
|
31
|
-
ensureChainId(note.chainId);
|
|
32
|
-
ensurePositiveInt('note index', note.index);
|
|
33
|
-
ensureMpk(note.mpk);
|
|
34
|
-
ensureBigIntString('note value', note.value);
|
|
35
|
-
if (note.spentAt !== undefined) {
|
|
36
|
-
ensurePositiveInt('note spentAt', note.spentAt);
|
|
37
|
-
}
|
|
38
|
-
const noteKey = keys.note(note.chainId, note.index);
|
|
39
|
-
const previous = await getJson(storage, noteKey);
|
|
40
|
-
const ops = [
|
|
41
|
-
{ put: [noteKey, encodeJson(note)] },
|
|
42
|
-
];
|
|
43
|
-
if (previous && previous.spentAt === undefined) {
|
|
44
|
-
ops.push({
|
|
45
|
-
del: keys.unspent(previous.chainId, previous.mpk, previous.index),
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
if (note.spentAt === undefined) {
|
|
49
|
-
ops.push({
|
|
50
|
-
put: [
|
|
51
|
-
keys.unspent(note.chainId, note.mpk, note.index),
|
|
52
|
-
emptyBytes(),
|
|
53
|
-
],
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
ops.push({
|
|
58
|
-
del: keys.unspent(note.chainId, note.mpk, note.index),
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
await storage.batch(ops);
|
|
62
|
-
},
|
|
63
|
-
async getNote(chainId, index) {
|
|
64
|
-
ensureChainId(chainId);
|
|
65
|
-
ensurePositiveInt('note index', index);
|
|
66
|
-
return getJson(storage, keys.note(chainId, index));
|
|
67
|
-
},
|
|
68
|
-
async listNotes(options = {}) {
|
|
69
|
-
const { chainId, mpk, includeSpent = true } = options;
|
|
70
|
-
if (chainId !== undefined) {
|
|
71
|
-
ensureChainId(chainId);
|
|
72
|
-
}
|
|
73
|
-
if (mpk !== undefined) {
|
|
74
|
-
ensureMpk(mpk);
|
|
75
|
-
}
|
|
76
|
-
const prefix = chainId !== undefined ? `notes:${chainId}:` : 'notes:';
|
|
77
|
-
const entries = await storage.iter({ prefix });
|
|
78
|
-
return entries
|
|
79
|
-
.map(({ value }) => decodeJson(value))
|
|
80
|
-
.filter((note) => (chainId === undefined || note.chainId === chainId) &&
|
|
81
|
-
(mpk === undefined || note.mpk === mpk) &&
|
|
82
|
-
(includeSpent || note.spentAt === undefined));
|
|
83
|
-
},
|
|
84
|
-
async markNoteSpent(chainId, index, spentAt = Date.now()) {
|
|
85
|
-
ensureChainId(chainId);
|
|
86
|
-
ensurePositiveInt('note index', index);
|
|
87
|
-
ensurePositiveInt('note spentAt', spentAt);
|
|
88
|
-
const existing = await store.getNote(chainId, index);
|
|
89
|
-
if (!existing) {
|
|
90
|
-
throw new CoreError('note not found');
|
|
91
|
-
}
|
|
92
|
-
if (existing.spentAt === spentAt) {
|
|
93
|
-
return existing;
|
|
94
|
-
}
|
|
95
|
-
const updated = { ...existing, spentAt };
|
|
96
|
-
await store.putNote(updated);
|
|
97
|
-
return updated;
|
|
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 store.putNote(updated);
|
|
112
|
-
return updated;
|
|
113
|
-
},
|
|
114
|
-
async getShieldedBalance(mpk, options = {}) {
|
|
115
|
-
ensureMpk(mpk);
|
|
116
|
-
const notes = await store.listNotes({
|
|
117
|
-
chainId: options.chainId,
|
|
118
|
-
mpk,
|
|
119
|
-
includeSpent: false,
|
|
120
|
-
});
|
|
121
|
-
const totals = {};
|
|
122
|
-
for (const note of notes) {
|
|
123
|
-
const amount = BigInt(note.value);
|
|
124
|
-
totals[note.token] = (totals[note.token] ?? 0n) + amount;
|
|
125
|
-
}
|
|
126
|
-
return totals;
|
|
127
|
-
},
|
|
128
|
-
async putNullifier(nullifier) {
|
|
129
|
-
ensureChainId(nullifier.chainId);
|
|
130
|
-
await putJson(storage, keys.nullifier(nullifier.chainId, nullifier.nullifier), nullifier);
|
|
131
|
-
},
|
|
132
|
-
async getNullifier(chainId, value) {
|
|
133
|
-
ensureChainId(chainId);
|
|
134
|
-
return getJson(storage, keys.nullifier(chainId, value));
|
|
135
|
-
},
|
|
136
|
-
async putLeaf(leaf) {
|
|
137
|
-
ensureChainId(leaf.chainId);
|
|
138
|
-
ensurePositiveInt('leaf index', leaf.index);
|
|
139
|
-
await putJson(storage, keys.leaf(leaf.chainId, leaf.index), leaf);
|
|
140
|
-
},
|
|
141
|
-
async getLeaf(chainId, index) {
|
|
142
|
-
ensureChainId(chainId);
|
|
143
|
-
ensurePositiveInt('leaf index', index);
|
|
144
|
-
return getJson(storage, keys.leaf(chainId, index));
|
|
145
|
-
},
|
|
146
|
-
async putRoot(root) {
|
|
147
|
-
ensureChainId(root.chainId);
|
|
148
|
-
ensurePositiveInt('root sequence', root.sequence);
|
|
149
|
-
await putJson(storage, keys.root(root.chainId, root.sequence), root);
|
|
150
|
-
},
|
|
151
|
-
async getRoot(chainId, sequence) {
|
|
152
|
-
ensureChainId(chainId);
|
|
153
|
-
ensurePositiveInt('root sequence', sequence);
|
|
154
|
-
return getJson(storage, keys.root(chainId, sequence));
|
|
155
|
-
},
|
|
156
|
-
async putCiphertext(chainId, index, payload) {
|
|
157
|
-
ensureChainId(chainId);
|
|
158
|
-
ensurePositiveInt('ciphertext index', index);
|
|
159
|
-
const key = keys.ciphertext(chainId, index);
|
|
160
|
-
validateKey(key);
|
|
161
|
-
await storage.put(key, new Uint8Array(payload));
|
|
162
|
-
},
|
|
163
|
-
async getCiphertext(chainId, index) {
|
|
164
|
-
ensureChainId(chainId);
|
|
165
|
-
ensurePositiveInt('ciphertext index', index);
|
|
166
|
-
const data = await storage.get(keys.ciphertext(chainId, index));
|
|
167
|
-
return data ? new Uint8Array(data) : null;
|
|
168
|
-
},
|
|
169
|
-
};
|
|
170
|
-
return store;
|
|
171
|
-
}
|
package/dist/storage/index.js
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* IndexedDB-backed key/value storage for Unlink Core.
|
|
3
|
-
*
|
|
4
|
-
* What this is
|
|
5
|
-
* - A durable, transactional KV store implemented on top of the browser's IndexedDB.
|
|
6
|
-
* - Two object stores: "kv" for app data, "meta" for schema/versioning.
|
|
7
|
-
*
|
|
8
|
-
* Why we implement it
|
|
9
|
-
* - We need persistent client-side state (notes, leaves, nullifiers, cfg, etc.) that survives reloads
|
|
10
|
-
* and works in both web apps and browser extensions without introducing a new backend.
|
|
11
|
-
*
|
|
12
|
-
* When to use
|
|
13
|
-
* - Browser UIs and extensions (MV2/MV3) where persistence is required.
|
|
14
|
-
* - Any environment with a real IndexedDB (tests use `fake-indexeddb`).
|
|
15
|
-
*
|
|
16
|
-
* Trade-offs
|
|
17
|
-
* - 👍 Durable, quota-managed, non-blocking, transactional writes (atomic batch).
|
|
18
|
-
* - 👍 Works offline; no server dependency.
|
|
19
|
-
* - ⚠️ Not natively available in Node/SSR; needs a polyfill for tests only.
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
import { CoreError } from "../errors.js";
|
|
23
|
-
import { validateKey } from "../keys.js";
|
|
24
|
-
const DEFAULT_DB_NAME = "unlink-core";
|
|
25
|
-
const DB_VERSION = 1;
|
|
26
|
-
const KV_STORE = "kv";
|
|
27
|
-
const META_STORE = "meta";
|
|
28
|
-
const SCHEMA_KEY = "schema_version";
|
|
29
|
-
const copy = (value) => new Uint8Array(value);
|
|
30
|
-
function assertIndexedDb() {
|
|
31
|
-
if (typeof indexedDB === "undefined") {
|
|
32
|
-
throw new CoreError("indexedDB is not available in this environment");
|
|
33
|
-
}
|
|
34
|
-
return indexedDB;
|
|
35
|
-
}
|
|
36
|
-
// Translate IndexedDB's event-based transaction API into a promise that resolves
|
|
37
|
-
// when the transaction completes or rejects on failure/abort.
|
|
38
|
-
function txDone(tx) {
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
tx.oncomplete = () => resolve();
|
|
41
|
-
tx.onerror = () => reject(tx.error ?? new CoreError("indexedDB transaction failed"));
|
|
42
|
-
tx.onabort = () => reject(tx.error ?? new CoreError("indexedDB transaction aborted"));
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
function wrapRequest(request) {
|
|
46
|
-
return new Promise((resolve, reject) => {
|
|
47
|
-
request.onsuccess = () => resolve(request.result);
|
|
48
|
-
request.onerror = () => reject(request.error ?? new CoreError("indexedDB request failed"));
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
// Coerce whatever IndexedDB gives us back into our Bytes type.
|
|
52
|
-
function asBytes(raw) {
|
|
53
|
-
if (raw instanceof Uint8Array)
|
|
54
|
-
return copy(raw);
|
|
55
|
-
if (raw instanceof ArrayBuffer)
|
|
56
|
-
return copy(new Uint8Array(raw));
|
|
57
|
-
if (ArrayBuffer.isView(raw)) {
|
|
58
|
-
const view = raw;
|
|
59
|
-
const buf = view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
|
60
|
-
return copy(new Uint8Array(buf));
|
|
61
|
-
}
|
|
62
|
-
throw new CoreError("indexedDB value is not binary data");
|
|
63
|
-
}
|
|
64
|
-
// IndexedDB-backed implementation implementing the storage API.
|
|
65
|
-
export function createIndexedDbStorage(opts = {}) {
|
|
66
|
-
const name = opts.name ?? DEFAULT_DB_NAME;
|
|
67
|
-
let db = null;
|
|
68
|
-
// Lazy-open the database so we only touch IndexedDB when needed.
|
|
69
|
-
async function ensureOpen() {
|
|
70
|
-
if (db)
|
|
71
|
-
return db;
|
|
72
|
-
// Guard for environments without IndexedDB (unit tests or SSR).
|
|
73
|
-
const idb = assertIndexedDb();
|
|
74
|
-
const request = idb.open(name, DB_VERSION);
|
|
75
|
-
// Versioned schema: create one store for kv entries and one for metadata.
|
|
76
|
-
request.onupgradeneeded = () => {
|
|
77
|
-
const upgradeDb = request.result;
|
|
78
|
-
if (!upgradeDb.objectStoreNames.contains(KV_STORE)) {
|
|
79
|
-
upgradeDb.createObjectStore(KV_STORE);
|
|
80
|
-
}
|
|
81
|
-
if (!upgradeDb.objectStoreNames.contains(META_STORE)) {
|
|
82
|
-
upgradeDb.createObjectStore(META_STORE);
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
db = await new Promise((resolve, reject) => {
|
|
86
|
-
request.onsuccess = () => resolve(request.result);
|
|
87
|
-
request.onerror = () => reject(request.error ?? new CoreError("indexedDB open failed"));
|
|
88
|
-
request.onblocked = () => reject(new CoreError("indexedDB open request was blocked"));
|
|
89
|
-
});
|
|
90
|
-
return db;
|
|
91
|
-
}
|
|
92
|
-
// Start a transaction and give callers both the store and a completion promise.
|
|
93
|
-
const getStore = async (storeName, mode) => {
|
|
94
|
-
const database = await ensureOpen();
|
|
95
|
-
const tx = database.transaction(storeName, mode);
|
|
96
|
-
const done = txDone(tx);
|
|
97
|
-
return { store: tx.objectStore(storeName), done };
|
|
98
|
-
};
|
|
99
|
-
return {
|
|
100
|
-
async open() {
|
|
101
|
-
await ensureOpen();
|
|
102
|
-
},
|
|
103
|
-
async get(key) {
|
|
104
|
-
validateKey(key);
|
|
105
|
-
const { store, done } = await getStore(KV_STORE, "readonly");
|
|
106
|
-
const result = await wrapRequest(store.get(key));
|
|
107
|
-
await done;
|
|
108
|
-
if (result === undefined)
|
|
109
|
-
return null;
|
|
110
|
-
return asBytes(result);
|
|
111
|
-
},
|
|
112
|
-
async put(key, value) {
|
|
113
|
-
validateKey(key);
|
|
114
|
-
const { store, done } = await getStore(KV_STORE, "readwrite");
|
|
115
|
-
await wrapRequest(store.put(copy(value), key)); // copy avoids shared mutation; revisit if this becomes costly
|
|
116
|
-
await done;
|
|
117
|
-
},
|
|
118
|
-
async delete(key) {
|
|
119
|
-
validateKey(key);
|
|
120
|
-
const { store, done } = await getStore(KV_STORE, "readwrite");
|
|
121
|
-
await wrapRequest(store.delete(key));
|
|
122
|
-
await done;
|
|
123
|
-
},
|
|
124
|
-
async batch(ops) {
|
|
125
|
-
// Execute the batch within a single transaction to preserve atomicity.
|
|
126
|
-
const database = await ensureOpen();
|
|
127
|
-
const tx = database.transaction(KV_STORE, "readwrite");
|
|
128
|
-
const store = tx.objectStore(KV_STORE);
|
|
129
|
-
const done = txDone(tx);
|
|
130
|
-
try {
|
|
131
|
-
// Replay the batch within a single transaction; validation failure aborts everything.
|
|
132
|
-
for (const op of ops) {
|
|
133
|
-
if (op.put) {
|
|
134
|
-
const [key, value] = op.put;
|
|
135
|
-
validateKey(key);
|
|
136
|
-
store.put(copy(value), key); // copy avoids shared mutation; revisit if this becomes costly
|
|
137
|
-
}
|
|
138
|
-
if (op.del) {
|
|
139
|
-
validateKey(op.del);
|
|
140
|
-
store.delete(op.del);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
await done;
|
|
144
|
-
}
|
|
145
|
-
catch (err) {
|
|
146
|
-
try {
|
|
147
|
-
tx.abort();
|
|
148
|
-
}
|
|
149
|
-
catch (_) {
|
|
150
|
-
/* noop */
|
|
151
|
-
}
|
|
152
|
-
await done.catch(() => { });
|
|
153
|
-
throw err;
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
async iter(options = {}) {
|
|
157
|
-
if (options.start && options.end && options.start > options.end) {
|
|
158
|
-
throw new CoreError("iter start bound must not exceed end bound");
|
|
159
|
-
}
|
|
160
|
-
// Pull everything into memory for now. TODO: stream w/ cursor when needed for perf
|
|
161
|
-
const database = await ensureOpen();
|
|
162
|
-
const tx = database.transaction(KV_STORE, "readonly");
|
|
163
|
-
const store = tx.objectStore(KV_STORE);
|
|
164
|
-
const done = txDone(tx);
|
|
165
|
-
const [keys, values] = await Promise.all([
|
|
166
|
-
wrapRequest(store.getAllKeys()),
|
|
167
|
-
wrapRequest(store.getAll()),
|
|
168
|
-
]);
|
|
169
|
-
await done;
|
|
170
|
-
const pairs = keys.map((key, idx) => {
|
|
171
|
-
if (typeof key !== "string") {
|
|
172
|
-
throw new CoreError("encountered non-string key in indexedDB");
|
|
173
|
-
}
|
|
174
|
-
const raw = values[idx];
|
|
175
|
-
return { key, value: asBytes(raw) };
|
|
176
|
-
});
|
|
177
|
-
let filtered = pairs.filter(({ key }) => {
|
|
178
|
-
if (options.prefix && !key.startsWith(options.prefix))
|
|
179
|
-
return false;
|
|
180
|
-
if (options.start && key < options.start)
|
|
181
|
-
return false;
|
|
182
|
-
if (options.end && key > options.end)
|
|
183
|
-
return false;
|
|
184
|
-
return true;
|
|
185
|
-
});
|
|
186
|
-
filtered.sort((a, b) => a.key.localeCompare(b.key));
|
|
187
|
-
if (options.reverse)
|
|
188
|
-
filtered = filtered.reverse();
|
|
189
|
-
if (options.limit)
|
|
190
|
-
filtered = filtered.slice(0, options.limit);
|
|
191
|
-
return filtered;
|
|
192
|
-
},
|
|
193
|
-
async getSchemaVersion() {
|
|
194
|
-
const { store, done } = await getStore(META_STORE, "readonly");
|
|
195
|
-
const value = await wrapRequest(store.get(SCHEMA_KEY));
|
|
196
|
-
await done;
|
|
197
|
-
return value ?? 0;
|
|
198
|
-
},
|
|
199
|
-
async setSchemaVersion(version) {
|
|
200
|
-
const { store, done } = await getStore(META_STORE, "readwrite");
|
|
201
|
-
await wrapRequest(store.put(version, SCHEMA_KEY));
|
|
202
|
-
await done;
|
|
203
|
-
},
|
|
204
|
-
};
|
|
205
|
-
}
|
package/dist/storage/memory.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* In-memory key/value storage for Unlink Core.
|
|
3
|
-
*
|
|
4
|
-
* What this is
|
|
5
|
-
* - A minimal, process-local KV store backed by a `Map<string, Uint8Array>`.
|
|
6
|
-
*
|
|
7
|
-
* Why we implement it
|
|
8
|
-
* - Fast, dependency-free storage for unit tests, demos, and ephemeral runs.
|
|
9
|
-
* - Useful as a reference driver for the Storage interface.
|
|
10
|
-
*
|
|
11
|
-
* When to use
|
|
12
|
-
* - Unit tests (deterministic, zero I/O).
|
|
13
|
-
* - CLI/Node scripts that do not need persistence beyond process lifetime.
|
|
14
|
-
* - Prototyping or sandboxing Core logic.
|
|
15
|
-
*
|
|
16
|
-
* Trade-offs
|
|
17
|
-
* - 👍 Extremely fast; zero external APIs; simple to reason about.
|
|
18
|
-
* - 👍 Great for isolation in tests; easy to snapshot/clone.
|
|
19
|
-
* - ⚠️ No durability (data is lost on process exit).
|
|
20
|
-
* - ⚠️ Per-process only; no cross-tab or reload survival.
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
import { CoreError } from "../errors.js";
|
|
24
|
-
import { validateKey } from "../keys.js";
|
|
25
|
-
const copy = (value) => new Uint8Array(value);
|
|
26
|
-
export function createMemoryStorage() {
|
|
27
|
-
const data = new Map();
|
|
28
|
-
let schema = 0;
|
|
29
|
-
return {
|
|
30
|
-
async open() { },
|
|
31
|
-
async get(k) {
|
|
32
|
-
validateKey(k);
|
|
33
|
-
return data.has(k) ? copy(data.get(k)) : null;
|
|
34
|
-
},
|
|
35
|
-
async put(k, v) {
|
|
36
|
-
validateKey(k);
|
|
37
|
-
data.set(k, copy(v));
|
|
38
|
-
},
|
|
39
|
-
async delete(k) {
|
|
40
|
-
validateKey(k);
|
|
41
|
-
data.delete(k);
|
|
42
|
-
},
|
|
43
|
-
async batch(ops) {
|
|
44
|
-
const draft = new Map(data);
|
|
45
|
-
for (const op of ops) {
|
|
46
|
-
if (op.put) {
|
|
47
|
-
const [k, v] = op.put;
|
|
48
|
-
validateKey(k);
|
|
49
|
-
draft.set(k, copy(v));
|
|
50
|
-
}
|
|
51
|
-
if (op.del) {
|
|
52
|
-
validateKey(op.del);
|
|
53
|
-
draft.delete(op.del);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
data.clear();
|
|
57
|
-
for (const [k, v] of draft.entries()) {
|
|
58
|
-
data.set(k, copy(v));
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
async iter(o = {}) {
|
|
62
|
-
if (o.start && o.end && o.start > o.end) {
|
|
63
|
-
throw new CoreError("iter start bound must not exceed end bound");
|
|
64
|
-
}
|
|
65
|
-
const all = [...data.entries()].map(([key, value]) => ({
|
|
66
|
-
key,
|
|
67
|
-
value: copy(value),
|
|
68
|
-
}));
|
|
69
|
-
let ks = all
|
|
70
|
-
.filter(({ key }) => (!o.prefix || key.startsWith(o.prefix)) &&
|
|
71
|
-
(!o.start || key >= o.start) &&
|
|
72
|
-
(!o.end || key <= o.end))
|
|
73
|
-
.sort((a, b) => a.key.localeCompare(b.key));
|
|
74
|
-
if (o.reverse)
|
|
75
|
-
ks.reverse();
|
|
76
|
-
if (o.limit)
|
|
77
|
-
ks = ks.slice(0, o.limit);
|
|
78
|
-
return ks;
|
|
79
|
-
},
|
|
80
|
-
async getSchemaVersion() {
|
|
81
|
-
return schema;
|
|
82
|
-
},
|
|
83
|
-
async setSchemaVersion(n) {
|
|
84
|
-
schema = n;
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { Interface } from "ethers";
|
|
2
|
-
import { v4 as uuidv4 } from "uuid";
|
|
3
|
-
import { createIndexerClient } from "../clients/indexer.js";
|
|
4
|
-
import { serviceConfig } from "../config.js";
|
|
5
|
-
import { ByteLength, ByteUtils } from "../key-derivation/bytes.js";
|
|
6
|
-
import { createMerkleTrees, DEFAULT_JOB_TIMEOUT_MS, rebuildTreeFromStore, } from "../state/index.js";
|
|
7
|
-
import { isNotFoundError, sleep } from "../utils/async.js";
|
|
8
|
-
import { parseHexToBigInt } from "../utils/bigint.js";
|
|
9
|
-
import { computeCommitment, deriveNpk, encryptNote } from "../utils/crypto.js";
|
|
10
|
-
import { DEFAULT_POLL_INTERVAL_MS, DEFAULT_POLL_TIMEOUT_MS, MAX_POLL_INTERVAL_MS, } from "../utils/polling.js";
|
|
11
|
-
import { ensureAddress, ensureChainId } from "../utils/validators.js";
|
|
12
|
-
export const DEPOSIT_ABI = [
|
|
13
|
-
"function deposit(address _depositor, (uint256 npk, uint256 amount, address token)[] notes, (uint256[3] data)[] ciphertexts)",
|
|
14
|
-
];
|
|
15
|
-
const depositInterface = new Interface(DEPOSIT_ABI);
|
|
16
|
-
function validateDepositRequest(request) {
|
|
17
|
-
ensureChainId(request.chainId);
|
|
18
|
-
ensureAddress("pool address", request.poolAddress);
|
|
19
|
-
ensureAddress("depositor", request.depositor);
|
|
20
|
-
ensureAddress("token", request.note.token);
|
|
21
|
-
if (request.note.mpk < 0n) {
|
|
22
|
-
throw new Error("mpk must be non-negative");
|
|
23
|
-
}
|
|
24
|
-
if (request.note.random < 0n) {
|
|
25
|
-
throw new Error("random must be non-negative");
|
|
26
|
-
}
|
|
27
|
-
if (request.note.amount < 0n) {
|
|
28
|
-
throw new Error("amount must be non-negative");
|
|
29
|
-
}
|
|
30
|
-
const tokenScalar = BigInt(request.note.token);
|
|
31
|
-
if (tokenScalar < 0n) {
|
|
32
|
-
throw new Error("token must map to non-negative scalar");
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
export function createDepositClient(stateStore, options) {
|
|
36
|
-
if (!stateStore) {
|
|
37
|
-
throw new Error("stateStore dependency is required");
|
|
38
|
-
}
|
|
39
|
-
const merkleTrees = options.merkleTrees ?? createMerkleTrees();
|
|
40
|
-
const indexerClient = createIndexerClient(serviceConfig.indexerBaseUrl, {
|
|
41
|
-
fetch: options.fetch,
|
|
42
|
-
});
|
|
43
|
-
function buildCalldata(request, npk) {
|
|
44
|
-
return depositInterface.encodeFunctionData("deposit", [
|
|
45
|
-
request.depositor,
|
|
46
|
-
[
|
|
47
|
-
// TODO(julienbrs) handle multiple notes
|
|
48
|
-
{
|
|
49
|
-
npk: npk,
|
|
50
|
-
amount: request.note.amount,
|
|
51
|
-
token: request.note.token,
|
|
52
|
-
},
|
|
53
|
-
],
|
|
54
|
-
[
|
|
55
|
-
{
|
|
56
|
-
data: encryptNote(request.note).data,
|
|
57
|
-
},
|
|
58
|
-
],
|
|
59
|
-
]);
|
|
60
|
-
}
|
|
61
|
-
async function persistDepositSuccess(job, record, indexedRoot) {
|
|
62
|
-
await Promise.all([
|
|
63
|
-
stateStore.putLeaf({
|
|
64
|
-
chainId: job.chainId,
|
|
65
|
-
index: record.index,
|
|
66
|
-
commitment: record.commitment,
|
|
67
|
-
}),
|
|
68
|
-
stateStore.putRoot({
|
|
69
|
-
chainId: job.chainId,
|
|
70
|
-
root: indexedRoot,
|
|
71
|
-
}),
|
|
72
|
-
stateStore.putPendingJob({
|
|
73
|
-
...job,
|
|
74
|
-
status: "succeeded",
|
|
75
|
-
lastCheckedAt: Date.now(),
|
|
76
|
-
txHash: record.txHash ?? job.txHash ?? null,
|
|
77
|
-
predictedCommitment: {
|
|
78
|
-
...job.predictedCommitment,
|
|
79
|
-
index: record.index,
|
|
80
|
-
root: indexedRoot,
|
|
81
|
-
},
|
|
82
|
-
}),
|
|
83
|
-
]);
|
|
84
|
-
}
|
|
85
|
-
return {
|
|
86
|
-
async request(request) {
|
|
87
|
-
validateDepositRequest(request);
|
|
88
|
-
const npk = deriveNpk(request);
|
|
89
|
-
const calldata = buildCalldata(request, npk);
|
|
90
|
-
const commitment = computeCommitment(request, npk);
|
|
91
|
-
const commitmentHex = ByteUtils.nToHex(commitment, ByteLength.UINT_256, true);
|
|
92
|
-
const relayId = uuidv4();
|
|
93
|
-
const job = {
|
|
94
|
-
relayId,
|
|
95
|
-
kind: "deposit",
|
|
96
|
-
chainId: request.chainId,
|
|
97
|
-
status: "pending",
|
|
98
|
-
broadcasterRelayId: null,
|
|
99
|
-
txHash: null,
|
|
100
|
-
createdAt: Date.now(),
|
|
101
|
-
timeoutMs: DEFAULT_JOB_TIMEOUT_MS,
|
|
102
|
-
predictedCommitment: {
|
|
103
|
-
hex: commitmentHex,
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
await stateStore.putPendingJob(job);
|
|
107
|
-
return { relayId, calldata, commitment: commitmentHex };
|
|
108
|
-
},
|
|
109
|
-
async syncPendingDeposit(relayId) {
|
|
110
|
-
const job = await stateStore.getPendingJob(relayId);
|
|
111
|
-
if (!job || job.kind !== "deposit") {
|
|
112
|
-
throw new Error(`unknown deposit relay ${relayId}`);
|
|
113
|
-
}
|
|
114
|
-
await rebuildTreeFromStore({
|
|
115
|
-
chainId: job.chainId,
|
|
116
|
-
trees: merkleTrees,
|
|
117
|
-
loadLeaf: stateStore.getLeaf.bind(stateStore),
|
|
118
|
-
});
|
|
119
|
-
const commitmentHex = job.predictedCommitment.hex;
|
|
120
|
-
const record = await waitForCommitment(job.chainId, commitmentHex);
|
|
121
|
-
const { index, root: indexedRoot } = record;
|
|
122
|
-
const commitmentValue = parseHexToBigInt(commitmentHex);
|
|
123
|
-
const { index: localIndex, root: localRoot } = merkleTrees.addLeaf(job.chainId, commitmentValue);
|
|
124
|
-
// TODO: if local state drift detected, need to re-sync since last correct state
|
|
125
|
-
if (localIndex !== index) {
|
|
126
|
-
throw new Error("local merkle tree desynchronized (index mismatch), local index: " +
|
|
127
|
-
localIndex +
|
|
128
|
-
", indexed index: " +
|
|
129
|
-
index);
|
|
130
|
-
}
|
|
131
|
-
if (localRoot.toLowerCase() !== indexedRoot.toLowerCase()) {
|
|
132
|
-
throw new Error("local merkle tree desynchronized (root mismatch), local root: " +
|
|
133
|
-
localRoot +
|
|
134
|
-
", indexed root: " +
|
|
135
|
-
indexedRoot);
|
|
136
|
-
}
|
|
137
|
-
await persistDepositSuccess(job, record, indexedRoot);
|
|
138
|
-
return {
|
|
139
|
-
chainId: job.chainId,
|
|
140
|
-
index,
|
|
141
|
-
commitment: record.commitment,
|
|
142
|
-
root: indexedRoot,
|
|
143
|
-
};
|
|
144
|
-
},
|
|
145
|
-
};
|
|
146
|
-
async function waitForCommitment(chainId, commitmentHex) {
|
|
147
|
-
const timeout = options.pollTimeoutMs ?? DEFAULT_POLL_TIMEOUT_MS;
|
|
148
|
-
const startedAt = Date.now();
|
|
149
|
-
let delay = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
|
|
150
|
-
while (Date.now() - startedAt <= timeout) {
|
|
151
|
-
try {
|
|
152
|
-
const record = await indexerClient.getCommitment({
|
|
153
|
-
chainId,
|
|
154
|
-
commitment: commitmentHex,
|
|
155
|
-
});
|
|
156
|
-
if (record)
|
|
157
|
-
return record;
|
|
158
|
-
}
|
|
159
|
-
catch (err) {
|
|
160
|
-
if (!isNotFoundError(err)) {
|
|
161
|
-
throw err;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
await sleep(delay);
|
|
165
|
-
delay = Math.min(delay * 2, MAX_POLL_INTERVAL_MS);
|
|
166
|
-
}
|
|
167
|
-
throw new Error("commitment not found in indexer before timeout");
|
|
168
|
-
}
|
|
169
|
-
}
|