@veilo/sdk-core 0.4.0 → 0.5.0
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/LICENSE +15 -0
- package/README.md +286 -1298
- package/dist/cjs/accounts/admin.d.ts +84 -0
- package/dist/cjs/accounts/admin.js +208 -0
- package/dist/cjs/accounts/errors.d.ts +6 -0
- package/dist/cjs/accounts/errors.js +98 -0
- package/dist/cjs/accounts/index.d.ts +4 -0
- package/dist/cjs/accounts/index.js +20 -0
- package/dist/cjs/accounts/pdas.d.ts +23 -0
- package/dist/cjs/accounts/pdas.js +44 -0
- package/dist/cjs/accounts/queries.d.ts +41 -0
- package/dist/cjs/accounts/queries.js +95 -0
- package/dist/cjs/client.d.ts +2 -407
- package/dist/cjs/client.js +6 -938
- package/dist/cjs/cloak/client.d.ts +28 -0
- package/dist/cjs/cloak/client.js +68 -0
- package/dist/cjs/cloak/errors.d.ts +13 -0
- package/dist/cjs/cloak/errors.js +25 -0
- package/dist/cjs/cloak/helpers.d.ts +6 -0
- package/dist/cjs/cloak/helpers.js +35 -0
- package/dist/cjs/cloak/index.d.ts +6 -0
- package/dist/cjs/cloak/index.js +24 -0
- package/dist/cjs/cloak/polling.d.ts +7 -0
- package/dist/cjs/cloak/polling.js +66 -0
- package/dist/cjs/cloak/transport.d.ts +15 -0
- package/dist/cjs/cloak/transport.js +119 -0
- package/dist/cjs/cloak/types.d.ts +188 -0
- package/dist/cjs/cloak/types.js +5 -0
- package/dist/cjs/cloak.d.ts +1 -0
- package/dist/cjs/cloak.js +18 -0
- package/dist/cjs/compactNote.d.ts +21 -0
- package/dist/cjs/compactNote.js +31 -7
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/notes/encryption.d.ts +41 -0
- package/dist/cjs/notes/encryption.js +75 -0
- package/dist/cjs/notes/index.d.ts +5 -0
- package/dist/cjs/notes/index.js +21 -0
- package/dist/cjs/notes/mailbox.d.ts +70 -0
- package/dist/cjs/notes/mailbox.js +164 -0
- package/dist/cjs/notes/model.d.ts +91 -0
- package/dist/cjs/notes/model.js +109 -0
- package/dist/cjs/notes/nullifier.d.ts +27 -0
- package/dist/cjs/notes/nullifier.js +45 -0
- package/dist/cjs/notes/recovery.d.ts +10 -0
- package/dist/cjs/notes/recovery.js +31 -0
- package/dist/cjs/proof.d.ts +1 -183
- package/dist/cjs/proof.js +16 -290
- package/dist/cjs/proofs/encoding.d.ts +17 -0
- package/dist/cjs/proofs/encoding.js +72 -0
- package/dist/cjs/proofs/formatting.d.ts +6 -0
- package/dist/cjs/proofs/formatting.js +34 -0
- package/dist/cjs/proofs/index.d.ts +5 -0
- package/dist/cjs/proofs/index.js +21 -0
- package/dist/cjs/proofs/swap.d.ts +87 -0
- package/dist/cjs/proofs/swap.js +144 -0
- package/dist/cjs/proofs/transaction.d.ts +28 -0
- package/dist/cjs/proofs/transaction.js +110 -0
- package/dist/cjs/proofs/types.d.ts +70 -0
- package/dist/cjs/prover.d.ts +4 -1
- package/dist/cjs/prover.js +12 -2
- package/dist/cjs/relayer/client.d.ts +61 -0
- package/dist/cjs/relayer/client.js +151 -0
- package/dist/cjs/relayer/crypto.d.ts +5 -0
- package/dist/cjs/relayer/crypto.js +26 -0
- package/dist/cjs/relayer/encoding.d.ts +2 -0
- package/dist/cjs/relayer/encoding.js +23 -0
- package/dist/cjs/relayer/errors.d.ts +7 -0
- package/dist/cjs/relayer/errors.js +17 -0
- package/dist/cjs/relayer/index.d.ts +3 -0
- package/dist/cjs/relayer/index.js +19 -0
- package/dist/cjs/relayer/transport.d.ts +17 -0
- package/dist/cjs/relayer/transport.js +63 -0
- package/dist/{src/relayer.d.ts → cjs/relayer/types.d.ts} +44 -86
- package/dist/cjs/relayer/types.js +5 -0
- package/dist/cjs/relayer.d.ts +1 -318
- package/dist/cjs/relayer.js +15 -254
- package/dist/cjs/shield/alt.js +8 -8
- package/dist/cjs/shield/errors.js +2 -2
- package/dist/cjs/shield/finalize.js +2 -2
- package/dist/cjs/shield/ix.d.ts +1 -1
- package/dist/cjs/shield/owner.js +9 -8
- package/dist/cjs/shield/ports.js +3 -3
- package/dist/cjs/shield/preflight.js +3 -3
- package/dist/cjs/shield/shield.js +20 -19
- package/dist/cjs/shield/types.d.ts +1 -1
- package/dist/cjs/transactions/deposit.d.ts +94 -0
- package/dist/cjs/transactions/deposit.js +234 -0
- package/dist/cjs/transactions/index.d.ts +7 -0
- package/dist/cjs/transactions/index.js +26 -0
- package/dist/cjs/transactions/swap.d.ts +71 -0
- package/dist/cjs/transactions/swap.js +184 -0
- package/dist/cjs/transactions/transact.d.ts +34 -0
- package/dist/cjs/transactions/transact.js +146 -0
- package/dist/cjs/transactions/transfer.d.ts +51 -0
- package/dist/cjs/transactions/transfer.js +106 -0
- package/dist/cjs/transactions/withdraw.d.ts +52 -0
- package/dist/cjs/transactions/withdraw.js +104 -0
- package/dist/cjs/utxo.d.ts +1 -235
- package/dist/cjs/utxo.js +15 -404
- package/dist/esm/accounts/admin.d.ts +84 -0
- package/dist/esm/accounts/admin.js +165 -0
- package/dist/esm/accounts/errors.d.ts +6 -0
- package/dist/esm/accounts/errors.js +95 -0
- package/dist/esm/accounts/index.d.ts +4 -0
- package/dist/esm/accounts/index.js +4 -0
- package/dist/esm/accounts/pdas.d.ts +23 -0
- package/dist/esm/accounts/pdas.js +38 -0
- package/dist/esm/accounts/queries.d.ts +41 -0
- package/dist/esm/accounts/queries.js +88 -0
- package/dist/esm/client.d.ts +2 -407
- package/dist/esm/client.js +3 -891
- package/dist/esm/cloak/client.d.ts +28 -0
- package/dist/esm/cloak/client.js +64 -0
- package/dist/esm/cloak/errors.d.ts +13 -0
- package/dist/esm/cloak/errors.js +21 -0
- package/dist/esm/cloak/helpers.d.ts +6 -0
- package/dist/esm/cloak/helpers.js +31 -0
- package/dist/esm/cloak/index.d.ts +6 -0
- package/dist/esm/cloak/index.js +6 -0
- package/dist/esm/cloak/polling.d.ts +7 -0
- package/dist/esm/cloak/polling.js +63 -0
- package/dist/esm/cloak/transport.d.ts +15 -0
- package/dist/esm/cloak/transport.js +115 -0
- package/dist/esm/cloak/types.d.ts +188 -0
- package/dist/esm/cloak/types.js +4 -0
- package/dist/esm/cloak.d.ts +1 -0
- package/dist/esm/cloak.js +2 -0
- package/dist/esm/compactNote.d.ts +21 -0
- package/dist/esm/compactNote.js +24 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/notes/encryption.d.ts +41 -0
- package/dist/esm/notes/encryption.js +67 -0
- package/dist/esm/notes/index.d.ts +5 -0
- package/dist/esm/notes/index.js +5 -0
- package/dist/esm/notes/mailbox.d.ts +70 -0
- package/dist/esm/notes/mailbox.js +154 -0
- package/dist/esm/notes/model.d.ts +91 -0
- package/dist/esm/notes/model.js +99 -0
- package/dist/esm/notes/nullifier.d.ts +27 -0
- package/dist/esm/notes/nullifier.js +40 -0
- package/dist/esm/notes/recovery.d.ts +10 -0
- package/dist/esm/notes/recovery.js +28 -0
- package/dist/esm/proof.d.ts +1 -183
- package/dist/esm/proof.js +2 -281
- package/dist/esm/proofs/encoding.d.ts +17 -0
- package/dist/esm/proofs/encoding.js +68 -0
- package/dist/esm/proofs/formatting.d.ts +6 -0
- package/dist/esm/proofs/formatting.js +31 -0
- package/dist/esm/proofs/index.d.ts +5 -0
- package/dist/esm/proofs/index.js +5 -0
- package/dist/esm/proofs/swap.d.ts +87 -0
- package/dist/esm/proofs/swap.js +138 -0
- package/dist/esm/proofs/transaction.d.ts +28 -0
- package/dist/esm/proofs/transaction.js +105 -0
- package/dist/esm/proofs/types.d.ts +70 -0
- package/dist/esm/proofs/types.js +1 -0
- package/dist/esm/prover.d.ts +4 -1
- package/dist/esm/prover.js +10 -1
- package/dist/esm/relayer/client.d.ts +61 -0
- package/dist/esm/relayer/client.js +144 -0
- package/dist/esm/relayer/crypto.d.ts +5 -0
- package/dist/esm/relayer/crypto.js +20 -0
- package/dist/esm/relayer/encoding.d.ts +2 -0
- package/dist/esm/relayer/encoding.js +19 -0
- package/dist/esm/relayer/errors.d.ts +7 -0
- package/dist/esm/relayer/errors.js +13 -0
- package/dist/esm/relayer/index.d.ts +3 -0
- package/dist/esm/relayer/index.js +3 -0
- package/dist/esm/relayer/transport.d.ts +17 -0
- package/dist/esm/relayer/transport.js +59 -0
- package/dist/esm/relayer/types.d.ts +276 -0
- package/dist/esm/relayer/types.js +4 -0
- package/dist/esm/relayer.d.ts +1 -318
- package/dist/esm/relayer.js +2 -249
- package/dist/esm/shield/alt.js +1 -1
- package/dist/esm/shield/errors.js +1 -1
- package/dist/esm/shield/finalize.js +1 -1
- package/dist/esm/shield/ix.d.ts +1 -1
- package/dist/esm/shield/owner.js +2 -1
- package/dist/esm/shield/ports.js +1 -1
- package/dist/esm/shield/preflight.js +1 -1
- package/dist/esm/shield/shield.js +4 -3
- package/dist/esm/shield/types.d.ts +1 -1
- package/dist/esm/transactions/deposit.d.ts +94 -0
- package/dist/esm/transactions/deposit.js +198 -0
- package/dist/esm/transactions/index.d.ts +7 -0
- package/dist/esm/transactions/index.js +7 -0
- package/dist/esm/transactions/swap.d.ts +71 -0
- package/dist/esm/transactions/swap.js +144 -0
- package/dist/esm/transactions/transact.d.ts +34 -0
- package/dist/esm/transactions/transact.js +110 -0
- package/dist/esm/transactions/transfer.d.ts +51 -0
- package/dist/esm/transactions/transfer.js +103 -0
- package/dist/esm/transactions/withdraw.d.ts +52 -0
- package/dist/esm/transactions/withdraw.js +101 -0
- package/dist/esm/utxo.d.ts +1 -235
- package/dist/esm/utxo.js +2 -382
- package/package.json +54 -16
- package/SHIELD_INTEGRATION.md +0 -143
- package/config.d.ts +0 -2
- package/config.js +0 -4
- package/dist/src/client.d.ts +0 -407
- package/dist/src/client.js +0 -951
- package/dist/src/compactNote.d.ts +0 -107
- package/dist/src/compactNote.js +0 -167
- package/dist/src/config.d.ts +0 -82
- package/dist/src/config.js +0 -57
- package/dist/src/events.d.ts +0 -77
- package/dist/src/events.js +0 -167
- package/dist/src/idl/privacy_pool.d.ts +0 -5
- package/dist/src/idl/privacy_pool.js +0 -15218
- package/dist/src/index.d.ts +0 -16
- package/dist/src/index.js +0 -67
- package/dist/src/merkle.d.ts +0 -77
- package/dist/src/merkle.js +0 -156
- package/dist/src/poseidon.d.ts +0 -29
- package/dist/src/poseidon.js +0 -100
- package/dist/src/program.d.ts +0 -37
- package/dist/src/program.js +0 -61
- package/dist/src/proof.d.ts +0 -183
- package/dist/src/proof.js +0 -292
- package/dist/src/prover.d.ts +0 -54
- package/dist/src/prover.js +0 -112
- package/dist/src/random.d.ts +0 -16
- package/dist/src/random.js +0 -28
- package/dist/src/relayer.js +0 -257
- package/dist/src/retry.d.ts +0 -32
- package/dist/src/retry.js +0 -75
- package/dist/src/shield/alt.d.ts +0 -87
- package/dist/src/shield/alt.js +0 -194
- package/dist/src/shield/computeBudget.d.ts +0 -61
- package/dist/src/shield/computeBudget.js +0 -64
- package/dist/src/shield/errors.d.ts +0 -58
- package/dist/src/shield/errors.js +0 -121
- package/dist/src/shield/finalize.d.ts +0 -45
- package/dist/src/shield/finalize.js +0 -119
- package/dist/src/shield/index.d.ts +0 -35
- package/dist/src/shield/index.js +0 -68
- package/dist/src/shield/ix.d.ts +0 -54
- package/dist/src/shield/ix.js +0 -119
- package/dist/src/shield/owner.d.ts +0 -36
- package/dist/src/shield/owner.js +0 -126
- package/dist/src/shield/ports.d.ts +0 -43
- package/dist/src/shield/ports.js +0 -153
- package/dist/src/shield/preflight.d.ts +0 -30
- package/dist/src/shield/preflight.js +0 -154
- package/dist/src/shield/shield.d.ts +0 -68
- package/dist/src/shield/shield.js +0 -499
- package/dist/src/shield/types.d.ts +0 -202
- package/dist/src/utxo.d.ts +0 -235
- package/dist/src/utxo.js +0 -407
- package/dist/tests/compact-note.test.d.ts +0 -1
- package/dist/tests/compact-note.test.js +0 -173
- package/dist/tests/config.test.d.ts +0 -1
- package/dist/tests/config.test.js +0 -102
- package/dist/tests/edge-cases.test.d.ts +0 -1
- package/dist/tests/edge-cases.test.js +0 -220
- package/dist/tests/encryption.test.d.ts +0 -1
- package/dist/tests/encryption.test.js +0 -215
- package/dist/tests/events.test.d.ts +0 -1
- package/dist/tests/events.test.js +0 -78
- package/dist/tests/multi-tree.test.d.ts +0 -1
- package/dist/tests/multi-tree.test.js +0 -405
- package/dist/tests/pda.test.d.ts +0 -1
- package/dist/tests/pda.test.js +0 -229
- package/dist/tests/poseidon-builder-parity.test.d.ts +0 -1
- package/dist/tests/poseidon-builder-parity.test.js +0 -72
- package/dist/tests/poseidon.test.d.ts +0 -1
- package/dist/tests/poseidon.test.js +0 -142
- package/dist/tests/proof.test.d.ts +0 -1
- package/dist/tests/proof.test.js +0 -296
- package/dist/tests/relayer.test.d.ts +0 -1
- package/dist/tests/relayer.test.js +0 -271
- package/dist/tests/sdk.integration.test.d.ts +0 -1
- package/dist/tests/sdk.integration.test.js +0 -330
- package/dist/tests/shield-owner.test.d.ts +0 -1
- package/dist/tests/shield-owner.test.js +0 -89
- package/dist/tests/shield-preflight.test.d.ts +0 -1
- package/dist/tests/shield-preflight.test.js +0 -87
- package/dist/tests/shield-realproof.test.d.ts +0 -1
- package/dist/tests/shield-realproof.test.js +0 -272
- package/dist/tests/shield.test.d.ts +0 -1
- package/dist/tests/shield.test.js +0 -403
- package/dist/tests/utxo.test.d.ts +0 -1
- package/dist/tests/utxo.test.js +0 -140
- package/poseidon.d.ts +0 -2
- package/poseidon.js +0 -4
- package/proof.d.ts +0 -2
- package/proof.js +0 -4
- package/prover.d.ts +0 -2
- package/prover.js +0 -4
- package/shield.d.ts +0 -2
- package/shield.js +0 -4
- /package/dist/{src/shield → cjs/proofs}/types.js +0 -0
package/dist/esm/utxo.js
CHANGED
|
@@ -1,382 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { sha256 } from "@noble/hashes/sha256";
|
|
4
|
-
import { BN254_FR_MODULUS, bigIntToBytesBE, bytesToBigIntBE, poseidon1, poseidon3, poseidon4, pubkeyToField, } from "./poseidon.js";
|
|
5
|
-
// -----------------------------------------------------------------------------
|
|
6
|
-
// Keypair Functions
|
|
7
|
-
// -----------------------------------------------------------------------------
|
|
8
|
-
/**
|
|
9
|
-
* Generate a random keypair.
|
|
10
|
-
* Private key is a random 32-byte scalar.
|
|
11
|
-
* Public key is derived via Poseidon: pubkey = Poseidon(privateKey)
|
|
12
|
-
*/
|
|
13
|
-
export function generateKeypair() {
|
|
14
|
-
const privateKeyBytes = new Uint8Array(randomBytes(32));
|
|
15
|
-
const privateKey = bytesToBigIntBE(privateKeyBytes) % BN254_FR_MODULUS;
|
|
16
|
-
const publicKey = derivePublicKey(privateKey);
|
|
17
|
-
return { privateKey, publicKey };
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Derive public key from private key.
|
|
21
|
-
* Matches circuit's Keypair template: publicKey = Poseidon(privateKey)
|
|
22
|
-
*/
|
|
23
|
-
export function derivePublicKey(privateKey) {
|
|
24
|
-
return poseidon1(privateKey);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Create keypair from a known private key.
|
|
28
|
-
*/
|
|
29
|
-
export function keypairFromPrivateKey(privateKey) {
|
|
30
|
-
return {
|
|
31
|
-
privateKey: privateKey % BN254_FR_MODULUS,
|
|
32
|
-
publicKey: derivePublicKey(privateKey % BN254_FR_MODULUS),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
// -----------------------------------------------------------------------------
|
|
36
|
-
// UTXO Commitment Functions
|
|
37
|
-
// -----------------------------------------------------------------------------
|
|
38
|
-
/**
|
|
39
|
-
* Compute UTXO commitment.
|
|
40
|
-
* Matches circuit's UTXOCommitment template:
|
|
41
|
-
* commitment = Poseidon(amount, pubkey, blinding, mintAddress)
|
|
42
|
-
*/
|
|
43
|
-
export function commitUTXO(utxo) {
|
|
44
|
-
const commitment = poseidon4(utxo.amount, utxo.pubkey, utxo.blinding, utxo.mintAddress);
|
|
45
|
-
return bigIntToBytesBE(commitment);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Create a random UTXO with the given parameters.
|
|
49
|
-
*/
|
|
50
|
-
export function createUTXO(params) {
|
|
51
|
-
const blindingBytes = new Uint8Array(randomBytes(32));
|
|
52
|
-
const blinding = bytesToBigIntBE(blindingBytes) % BN254_FR_MODULUS;
|
|
53
|
-
const utxo = {
|
|
54
|
-
amount: params.amount,
|
|
55
|
-
pubkey: params.pubkey,
|
|
56
|
-
blinding,
|
|
57
|
-
mintAddress: pubkeyToField(params.mintAddress),
|
|
58
|
-
};
|
|
59
|
-
const commitment = commitUTXO(utxo);
|
|
60
|
-
return {
|
|
61
|
-
...utxo,
|
|
62
|
-
commitment,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Create a UTXO with a specified private key (for owned UTXOs).
|
|
67
|
-
*/
|
|
68
|
-
export function createOwnedUTXO(params) {
|
|
69
|
-
const keypair = keypairFromPrivateKey(params.privateKey);
|
|
70
|
-
const utxo = createUTXO({
|
|
71
|
-
amount: params.amount,
|
|
72
|
-
pubkey: keypair.publicKey,
|
|
73
|
-
mintAddress: params.mintAddress,
|
|
74
|
-
});
|
|
75
|
-
return {
|
|
76
|
-
...utxo,
|
|
77
|
-
privateKey: keypair.privateKey,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Create a zero UTXO (for deposits where no input exists).
|
|
82
|
-
* Zero UTXOs have amount=0 and are used as placeholders in 2-in-2-out transactions.
|
|
83
|
-
*/
|
|
84
|
-
export function createZeroUTXO(pubkey, mintAddress) {
|
|
85
|
-
return createUTXO({
|
|
86
|
-
amount: 0n,
|
|
87
|
-
pubkey,
|
|
88
|
-
mintAddress,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Create a zero UTXO with private key (for owned zero UTXOs).
|
|
93
|
-
*/
|
|
94
|
-
export function createOwnedZeroUTXO(privateKey, mintAddress) {
|
|
95
|
-
const keypair = keypairFromPrivateKey(privateKey);
|
|
96
|
-
const utxo = createZeroUTXO(keypair.publicKey, mintAddress);
|
|
97
|
-
return {
|
|
98
|
-
...utxo,
|
|
99
|
-
privateKey: keypair.privateKey,
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
// -----------------------------------------------------------------------------
|
|
103
|
-
// Nullifier Functions
|
|
104
|
-
// -----------------------------------------------------------------------------
|
|
105
|
-
/**
|
|
106
|
-
* Compute signature for nullifier derivation.
|
|
107
|
-
* Matches circuit's Signature template:
|
|
108
|
-
* signature = Poseidon(privateKey, commitment, pathIndex)
|
|
109
|
-
*/
|
|
110
|
-
export function computeSignature(privateKey, commitment, pathIndex) {
|
|
111
|
-
return poseidon3(privateKey, commitment, pathIndex);
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Derive nullifier for a UTXO.
|
|
115
|
-
* Matches circuit's UTXONullifier template:
|
|
116
|
-
* signature = Poseidon(privateKey, commitment, pathIndex)
|
|
117
|
-
* nullifier = Poseidon(commitment, pathIndex, signature)
|
|
118
|
-
*
|
|
119
|
-
* The signature-based nullifier ensures only the private key holder can spend.
|
|
120
|
-
*/
|
|
121
|
-
export function deriveNullifier(utxo, pathIndex, privateKey) {
|
|
122
|
-
const commitment = bytesToBigIntBE(utxo.commitment) % BN254_FR_MODULUS;
|
|
123
|
-
const pathIndexField = BigInt(pathIndex) % BN254_FR_MODULUS;
|
|
124
|
-
const signature = computeSignature(privateKey, commitment, pathIndexField);
|
|
125
|
-
const nullifier = poseidon3(commitment, pathIndexField, signature);
|
|
126
|
-
return bigIntToBytesBE(nullifier);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Convert InputUTXO to the format expected by the circuit.
|
|
130
|
-
*/
|
|
131
|
-
export function inputUTXOToCircuitFormat(input) {
|
|
132
|
-
return {
|
|
133
|
-
amount: input.amount,
|
|
134
|
-
pubkey: input.pubkey,
|
|
135
|
-
blinding: input.blinding,
|
|
136
|
-
pathElements: input.pathElements.map((e) => bytesToBigIntBE(e) % BN254_FR_MODULUS),
|
|
137
|
-
pathIndex: input.pathIndex,
|
|
138
|
-
privateKey: input.privateKey,
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Derive a NaCl X25519 encryption keypair from a UTXO private key.
|
|
143
|
-
* The keypair is deterministic: same privateKey always yields the same pair.
|
|
144
|
-
*/
|
|
145
|
-
export function deriveEncryptionKeypair(privateKey) {
|
|
146
|
-
const seed = sha256(bigIntToBytesBE(privateKey));
|
|
147
|
-
return nacl.box.keyPair.fromSecretKey(seed);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Encrypt a UTXO so the recipient can recover it from on-chain events.
|
|
151
|
-
*
|
|
152
|
-
* Plaintext layout (72 bytes):
|
|
153
|
-
* [0..8] amount — 8-byte little-endian u64
|
|
154
|
-
* [8..40] blinding — 32-byte big-endian field element
|
|
155
|
-
* [40..72] privateKey — 32-byte big-endian field element
|
|
156
|
-
*
|
|
157
|
-
* Uses ephemeral-key NaCl box so the sender key is single-use.
|
|
158
|
-
*/
|
|
159
|
-
export function encryptUTXONote(utxo, recipientEncPubkey) {
|
|
160
|
-
const nonce = nacl.randomBytes(nacl.box.nonceLength); // 24 bytes
|
|
161
|
-
const ephemeral = nacl.box.keyPair();
|
|
162
|
-
const amountBuf = new Uint8Array(8);
|
|
163
|
-
let v = utxo.amount;
|
|
164
|
-
for (let i = 0; i < 8; i++) {
|
|
165
|
-
amountBuf[i] = Number(v & 0xffn);
|
|
166
|
-
v >>= 8n;
|
|
167
|
-
}
|
|
168
|
-
const plaintext = new Uint8Array(72);
|
|
169
|
-
plaintext.set(amountBuf, 0);
|
|
170
|
-
plaintext.set(bigIntToBytesBE(utxo.blinding), 8);
|
|
171
|
-
plaintext.set(bigIntToBytesBE(utxo.privateKey), 40);
|
|
172
|
-
const sharedKey = nacl.box.before(recipientEncPubkey, ephemeral.secretKey);
|
|
173
|
-
const ciphertext = nacl.box.after(plaintext, nonce, sharedKey);
|
|
174
|
-
return { nonce, ciphertext, senderEphemeralPubkey: ephemeral.publicKey };
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Try to decrypt an encrypted UTXO note using the recipient's private key.
|
|
178
|
-
* Returns the SerializedUTXO if decryption succeeds and the commitment
|
|
179
|
-
* matches, or `null` if the note was not intended for this key.
|
|
180
|
-
*/
|
|
181
|
-
export function decryptUTXONote(note, recipientPrivateKey, commitment, mintAddress) {
|
|
182
|
-
const { secretKey } = deriveEncryptionKeypair(recipientPrivateKey);
|
|
183
|
-
const sharedKey = nacl.box.before(note.senderEphemeralPubkey, secretKey);
|
|
184
|
-
const plaintext = nacl.box.open.after(note.ciphertext, note.nonce, sharedKey);
|
|
185
|
-
if (!plaintext || plaintext.length < 72)
|
|
186
|
-
return null;
|
|
187
|
-
let amount = 0n;
|
|
188
|
-
for (let i = 7; i >= 0; i--)
|
|
189
|
-
amount = (amount << 8n) | BigInt(plaintext[i]);
|
|
190
|
-
const blinding = bytesToBigIntBE(plaintext.slice(8, 40)) % BN254_FR_MODULUS;
|
|
191
|
-
const privateKey = bytesToBigIntBE(plaintext.slice(40, 72)) % BN254_FR_MODULUS;
|
|
192
|
-
const pubkey = derivePublicKey(privateKey);
|
|
193
|
-
const utxo = {
|
|
194
|
-
amount,
|
|
195
|
-
pubkey,
|
|
196
|
-
blinding,
|
|
197
|
-
mintAddress: pubkeyToField(mintAddress),
|
|
198
|
-
};
|
|
199
|
-
const expectedCommitment = commitUTXO(utxo);
|
|
200
|
-
if (!expectedCommitment.every((b, i) => b === commitment[i]))
|
|
201
|
-
return null;
|
|
202
|
-
return { ...utxo, commitment, privateKey };
|
|
203
|
-
}
|
|
204
|
-
// -- Internal crypto helpers (no extra dependencies) --------------------------
|
|
205
|
-
/** Derive 32-byte encryption key from a shared secret using SHA-512 truncated.
|
|
206
|
-
* Must match the production "sha256" helper which uses nacl.hash (SHA-512) [0:32].
|
|
207
|
-
*
|
|
208
|
-
* DO NOT "fix" this to real SHA-256. The name in the sibling implementations
|
|
209
|
-
* (`sha256ForNotes`) is a misnomer, but the bytes are load-bearing: changing the
|
|
210
|
-
* KDF makes every historical note undecryptable.
|
|
211
|
-
*
|
|
212
|
-
* @internal Exported for `compactNote.ts`. Not part of the stable public API. */
|
|
213
|
-
export function kdfSHA512_256(secret) {
|
|
214
|
-
return nacl.hash(secret).slice(0, 32);
|
|
215
|
-
}
|
|
216
|
-
/** Convert an Ed25519 private seed (32 bytes) to an X25519 scalar.
|
|
217
|
-
* Equivalent to ed2curve.convertSecretKey(seed).
|
|
218
|
-
*
|
|
219
|
-
* @internal Exported for `compactNote.ts`. Not part of the stable public API. */
|
|
220
|
-
export function ed25519SeedToX25519Private(seed) {
|
|
221
|
-
const h = nacl.hash(seed); // SHA-512
|
|
222
|
-
const scalar = h.slice(0, 32);
|
|
223
|
-
scalar[0] &= 248;
|
|
224
|
-
scalar[31] &= 127;
|
|
225
|
-
scalar[31] |= 64;
|
|
226
|
-
return scalar;
|
|
227
|
-
}
|
|
228
|
-
const P25519 = (1n << 255n) - 19n;
|
|
229
|
-
function modpow25519(b, e) {
|
|
230
|
-
let r = 1n;
|
|
231
|
-
b = b % P25519;
|
|
232
|
-
while (e > 0n) {
|
|
233
|
-
if (e & 1n)
|
|
234
|
-
r = (r * b) % P25519;
|
|
235
|
-
e >>= 1n;
|
|
236
|
-
b = (b * b) % P25519;
|
|
237
|
-
}
|
|
238
|
-
return r;
|
|
239
|
-
}
|
|
240
|
-
/** Convert an Ed25519 compressed public key (32 bytes) to X25519.
|
|
241
|
-
* Equivalent to ed2curve.convertPublicKey(pubkey).
|
|
242
|
-
* Edwards y-coord → Montgomery u via: u = (1 + y) / (1 - y) (mod p).
|
|
243
|
-
*
|
|
244
|
-
* @internal Exported for `compactNote.ts`. Not part of the stable public API. */
|
|
245
|
-
export function ed25519PublicKeyToX25519(pubkey) {
|
|
246
|
-
const pk = new Uint8Array(pubkey);
|
|
247
|
-
pk[31] &= 0x7f; // clear sign bit to get y as LE integer
|
|
248
|
-
// Decode little-endian y
|
|
249
|
-
let y = 0n;
|
|
250
|
-
for (let i = 31; i >= 0; i--)
|
|
251
|
-
y = (y << 8n) | BigInt(pk[i]);
|
|
252
|
-
// u = (1 + y) / (1 - y) mod p
|
|
253
|
-
const num = (1n + y) % P25519;
|
|
254
|
-
const den = (((1n - y) % P25519) + P25519) % P25519;
|
|
255
|
-
const u = (num * modpow25519(den, P25519 - 2n)) % P25519;
|
|
256
|
-
// Encode as 32-byte LE
|
|
257
|
-
const out = new Uint8Array(32);
|
|
258
|
-
let v = u;
|
|
259
|
-
for (let i = 0; i < 32; i++) {
|
|
260
|
-
out[i] = Number(v & 0xffn);
|
|
261
|
-
v >>= 8n;
|
|
262
|
-
}
|
|
263
|
-
return out;
|
|
264
|
-
}
|
|
265
|
-
// -- Public API ---------------------------------------------------------------
|
|
266
|
-
/**
|
|
267
|
-
* Encrypt a UTXO note for a recipient identified by their Solana wallet public key.
|
|
268
|
-
* Produces a blob the relayer will store and serve via POST /notes/save.
|
|
269
|
-
*
|
|
270
|
-
* @param recipientWalletPubkey 32-byte Ed25519 public key (wallet.publicKey.toBytes())
|
|
271
|
-
* @param noteData UTXO details to encrypt
|
|
272
|
-
*/
|
|
273
|
-
export function encryptBlindMailboxNote(recipientWalletPubkey, noteData) {
|
|
274
|
-
// 1. Generate ephemeral Ed25519 keypair seed
|
|
275
|
-
const ephemeralSeed = randomBytes(32);
|
|
276
|
-
// Ed25519 public key stored by relayer; dapp recovers X25519 via ed2curve.convertPublicKey()
|
|
277
|
-
const ephemeralEdPublicKey = nacl.sign.keyPair.fromSeed(new Uint8Array(ephemeralSeed)).publicKey;
|
|
278
|
-
// X25519 scalar (ed2curve.convertSecretKey equivalent)
|
|
279
|
-
const ephemeralX25519Private = ed25519SeedToX25519Private(new Uint8Array(ephemeralSeed));
|
|
280
|
-
// X25519 public key (ed2curve.convertPublicKey equivalent)
|
|
281
|
-
const recipientX25519Public = ed25519PublicKeyToX25519(recipientWalletPubkey);
|
|
282
|
-
const sharedSecret = nacl.scalarMult(ephemeralX25519Private, recipientX25519Public);
|
|
283
|
-
const key = kdfSHA512_256(sharedSecret);
|
|
284
|
-
// JSON format matches production relayer notes schema
|
|
285
|
-
const hexArr = (b) => Array.from(b)
|
|
286
|
-
.map((x) => x.toString(16).padStart(2, "0"))
|
|
287
|
-
.join("");
|
|
288
|
-
const noteJson = JSON.stringify({
|
|
289
|
-
blinding: hexArr(noteData.blinding),
|
|
290
|
-
leafIndex: noteData.leafIndex,
|
|
291
|
-
commitment: hexArr(noteData.commitment),
|
|
292
|
-
amount: noteData.amount.toString(),
|
|
293
|
-
mintAddress: noteData.mintAddress,
|
|
294
|
-
timestamp: Date.now(),
|
|
295
|
-
treeId: noteData.treeId,
|
|
296
|
-
});
|
|
297
|
-
const nonce = nacl.randomBytes(24);
|
|
298
|
-
const plaintext = new TextEncoder().encode(noteJson);
|
|
299
|
-
const encrypted = nacl.secretbox(plaintext, nonce, key);
|
|
300
|
-
const combined = new Uint8Array(nonce.length + encrypted.length);
|
|
301
|
-
combined.set(nonce);
|
|
302
|
-
combined.set(encrypted, nonce.length);
|
|
303
|
-
const encryptedBlob = Buffer.from(combined).toString("base64");
|
|
304
|
-
// Return Ed25519 public key (dapp calls ed2curve.convertPublicKey() to recover X25519)
|
|
305
|
-
return { ephemeralPublicKey: ephemeralEdPublicKey, encryptedBlob };
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Decrypt a Blind Mailbox note using the recipient's Solana wallet private seed.
|
|
309
|
-
*
|
|
310
|
-
* @param walletSecretKey Full 64-byte wallet.secretKey — the first 32 bytes are the seed
|
|
311
|
-
* @param ephemeralPublicKey 32-byte raw public key stored alongside the encrypted blob.
|
|
312
|
-
* This may be either an Ed25519 pubkey (from Keypair.generate())
|
|
313
|
-
* or a raw X25519 pubkey — the decryption path handles both:
|
|
314
|
-
* if it is an Ed25519 key it is converted; otherwise used directly.
|
|
315
|
-
* @param encryptedBlob Base64 string as stored by the relayer
|
|
316
|
-
*/
|
|
317
|
-
export function decryptBlindMailboxNote(walletSecretKey, ephemeralPublicKey, encryptedBlob) {
|
|
318
|
-
// 1. Derive recipient X25519 from Ed25519 seed
|
|
319
|
-
const seed = walletSecretKey.slice(0, 32);
|
|
320
|
-
const myX25519Private = ed25519SeedToX25519Private(seed);
|
|
321
|
-
// ephemeralPublicKey may be an Ed25519 pubkey (dapp's Keypair.generate()) or raw X25519;
|
|
322
|
-
// try Ed25519→X25519 conversion first, fall back to using it directly.
|
|
323
|
-
let theirPublic;
|
|
324
|
-
try {
|
|
325
|
-
theirPublic = ed25519PublicKeyToX25519(ephemeralPublicKey);
|
|
326
|
-
}
|
|
327
|
-
catch {
|
|
328
|
-
theirPublic = ephemeralPublicKey; // already X25519
|
|
329
|
-
}
|
|
330
|
-
const sharedSecret = nacl.scalarMult(myX25519Private, theirPublic);
|
|
331
|
-
const key = kdfSHA512_256(sharedSecret);
|
|
332
|
-
const combined = Buffer.from(encryptedBlob, "base64");
|
|
333
|
-
const nonce = combined.slice(0, 24);
|
|
334
|
-
const ciphertext = combined.slice(24);
|
|
335
|
-
const decrypted = nacl.secretbox.open(new Uint8Array(ciphertext), new Uint8Array(nonce), key);
|
|
336
|
-
if (!decrypted) {
|
|
337
|
-
throw new Error("decryptBlindMailboxNote: decryption failed — wrong key or corrupted blob");
|
|
338
|
-
}
|
|
339
|
-
const parsed = JSON.parse(new TextDecoder().decode(decrypted));
|
|
340
|
-
const hexToBytes = (hex) => {
|
|
341
|
-
const out = new Uint8Array(hex.length / 2);
|
|
342
|
-
for (let i = 0; i < hex.length; i += 2)
|
|
343
|
-
out[i / 2] = parseInt(hex.slice(i, i + 2), 16);
|
|
344
|
-
return out;
|
|
345
|
-
};
|
|
346
|
-
return {
|
|
347
|
-
blinding: hexToBytes(parsed.blinding),
|
|
348
|
-
leafIndex: parsed.leafIndex,
|
|
349
|
-
commitment: hexToBytes(parsed.commitment),
|
|
350
|
-
amount: BigInt(parsed.amount),
|
|
351
|
-
timestamp: parsed.timestamp ?? 0,
|
|
352
|
-
mintAddress: parsed.mintAddress,
|
|
353
|
-
treeId: parsed.treeId,
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Fetch all encrypted notes for a wallet from the relayer's Blind Mailbox endpoint
|
|
358
|
-
* and decrypt them using the wallet's secret key.
|
|
359
|
-
*
|
|
360
|
-
* @param walletPublicKey Base58 wallet address used to query the relayer
|
|
361
|
-
* @param walletSecretKey Full 64-byte keypair.secretKey for decryption
|
|
362
|
-
* @param relayerUrl Relayer base URL (e.g. "https://relay.veilo.app")
|
|
363
|
-
*/
|
|
364
|
-
export async function fetchAndDecryptNotes(walletPublicKey, walletSecretKey, relayerUrl) {
|
|
365
|
-
const resp = await fetch(`${relayerUrl}/api/notes?recipientPublicKey=${walletPublicKey}`);
|
|
366
|
-
if (!resp.ok) {
|
|
367
|
-
throw new Error(`fetchAndDecryptNotes: ${resp.status} ${resp.statusText}`);
|
|
368
|
-
}
|
|
369
|
-
const { notes } = (await resp.json());
|
|
370
|
-
const results = [];
|
|
371
|
-
for (const note of notes) {
|
|
372
|
-
try {
|
|
373
|
-
const epk = Buffer.from(note.ephemeralPublicKey, "base64");
|
|
374
|
-
const decrypted = decryptBlindMailboxNote(walletSecretKey, new Uint8Array(epk), note.encryptedBlob);
|
|
375
|
-
results.push(decrypted);
|
|
376
|
-
}
|
|
377
|
-
catch {
|
|
378
|
-
// Note not for this key or corrupted — skip silently
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
return results;
|
|
382
|
-
}
|
|
1
|
+
// Backward-compatible entry point. Note responsibilities live in src/notes.
|
|
2
|
+
export * from "./notes/index.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veilo/sdk-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "TypeScript SDK for the Veilo Privacy Pool - UTXO-based privacy protocol on Solana with ZK-SNARKs",
|
|
5
5
|
"homepage": "https://github.com/VeiloSolana/veilo-sdk#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -77,6 +77,56 @@
|
|
|
77
77
|
"default": "./dist/cjs/shield/index.js"
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
|
+
"./accounts": {
|
|
81
|
+
"import": {
|
|
82
|
+
"types": "./dist/esm/accounts/index.d.ts",
|
|
83
|
+
"default": "./dist/esm/accounts/index.js"
|
|
84
|
+
},
|
|
85
|
+
"require": {
|
|
86
|
+
"types": "./dist/cjs/accounts/index.d.ts",
|
|
87
|
+
"default": "./dist/cjs/accounts/index.js"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"./transactions": {
|
|
91
|
+
"import": {
|
|
92
|
+
"types": "./dist/esm/transactions/index.d.ts",
|
|
93
|
+
"default": "./dist/esm/transactions/index.js"
|
|
94
|
+
},
|
|
95
|
+
"require": {
|
|
96
|
+
"types": "./dist/cjs/transactions/index.d.ts",
|
|
97
|
+
"default": "./dist/cjs/transactions/index.js"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"./notes": {
|
|
101
|
+
"import": {
|
|
102
|
+
"types": "./dist/esm/notes/index.d.ts",
|
|
103
|
+
"default": "./dist/esm/notes/index.js"
|
|
104
|
+
},
|
|
105
|
+
"require": {
|
|
106
|
+
"types": "./dist/cjs/notes/index.d.ts",
|
|
107
|
+
"default": "./dist/cjs/notes/index.js"
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
"./relayer": {
|
|
111
|
+
"import": {
|
|
112
|
+
"types": "./dist/esm/relayer/index.d.ts",
|
|
113
|
+
"default": "./dist/esm/relayer/index.js"
|
|
114
|
+
},
|
|
115
|
+
"require": {
|
|
116
|
+
"types": "./dist/cjs/relayer/index.d.ts",
|
|
117
|
+
"default": "./dist/cjs/relayer/index.js"
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
"./cloak": {
|
|
121
|
+
"import": {
|
|
122
|
+
"types": "./dist/esm/cloak.d.ts",
|
|
123
|
+
"default": "./dist/esm/cloak.js"
|
|
124
|
+
},
|
|
125
|
+
"require": {
|
|
126
|
+
"types": "./dist/cjs/cloak.d.ts",
|
|
127
|
+
"default": "./dist/cjs/cloak.js"
|
|
128
|
+
}
|
|
129
|
+
},
|
|
80
130
|
"./package.json": "./package.json"
|
|
81
131
|
},
|
|
82
132
|
"publishConfig": {
|
|
@@ -85,28 +135,16 @@
|
|
|
85
135
|
"sideEffects": false,
|
|
86
136
|
"files": [
|
|
87
137
|
"dist/",
|
|
88
|
-
"config.js",
|
|
89
|
-
"config.d.ts",
|
|
90
|
-
"poseidon.js",
|
|
91
|
-
"poseidon.d.ts",
|
|
92
|
-
"proof.js",
|
|
93
|
-
"proof.d.ts",
|
|
94
|
-
"prover.js",
|
|
95
|
-
"prover.d.ts",
|
|
96
|
-
"shield.js",
|
|
97
|
-
"shield.d.ts",
|
|
98
138
|
"README.md",
|
|
99
|
-
"SHIELD_INTEGRATION.md",
|
|
100
139
|
"LICENSE"
|
|
101
140
|
],
|
|
102
|
-
"directories": {
|
|
103
|
-
"test": "tests"
|
|
104
|
-
},
|
|
105
141
|
"scripts": {
|
|
142
|
+
"check:cycles": "node scripts/check-dependency-cycles.mjs",
|
|
106
143
|
"gen:idl": "node scripts/gen-idl.mjs",
|
|
107
144
|
"verify:cipher-parity": "npm run build && node scripts/verify-compact-cipher-parity.mjs",
|
|
108
145
|
"build": "rm -rf dist/ && tsc -p tsconfig.json && tsc -p tsconfig.esm.json && echo '{\"type\":\"module\"}' > dist/esm/package.json",
|
|
109
|
-
"test": "rm -rf dist/ &&
|
|
146
|
+
"test": "rm -rf dist/ && tsc -p test-tsconfig.json && mocha -t 1000000 \"dist/tests/**/*.js\"",
|
|
147
|
+
"test:integration": "export VEILO_RUN_INTEGRATION=1 && export ANCHOR_PROVIDER_URL=https://api.devnet.solana.com && export ANCHOR_WALLET=$HOME/.config/solana/id.json && npm test"
|
|
110
148
|
},
|
|
111
149
|
"dependencies": {
|
|
112
150
|
"@coral-xyz/anchor": "^0.30.0",
|
package/SHIELD_INTEGRATION.md
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
# Wallet-agnostic shielding
|
|
2
|
-
|
|
3
|
-
`shield()` builds an unsigned Solana v0 transaction that moves funds from any
|
|
4
|
-
signing wallet into Veilo's privacy pool. The wallet that pays and the Veilo
|
|
5
|
-
account that owns the resulting private note are independent.
|
|
6
|
-
|
|
7
|
-
This is a build API, like Jupiter's transaction APIs. The SDK proves, encrypts,
|
|
8
|
-
and assembles. Your application asks its wallet to sign, submits the signed
|
|
9
|
-
bytes, confirms them, and handles retry.
|
|
10
|
-
|
|
11
|
-
## Security model
|
|
12
|
-
|
|
13
|
-
- `signer.publicKey` is the transaction fee payer and public funding source.
|
|
14
|
-
- `owner.veiloPublicKey` controls spending the private note.
|
|
15
|
-
- `owner.noteViewingKey` controls detecting and decrypting the note during a
|
|
16
|
-
chain scan.
|
|
17
|
-
- The SDK never receives a signer secret key.
|
|
18
|
-
- The compact note cipher is embedded in the on-chain instruction, so note
|
|
19
|
-
recovery does not depend on a webhook or a later relayer callback.
|
|
20
|
-
|
|
21
|
-
The two owner keys must belong to the same Veilo account. If they do not, the
|
|
22
|
-
note is spendable but invisible to its owner. Always use `resolveShieldOwner()`;
|
|
23
|
-
construct an owner manually only when you can prove the pairing yourself.
|
|
24
|
-
|
|
25
|
-
## Complete flow
|
|
26
|
-
|
|
27
|
-
```ts
|
|
28
|
-
import { Connection, PublicKey } from "@solana/web3.js";
|
|
29
|
-
import {
|
|
30
|
-
createTransactionProver,
|
|
31
|
-
finalizeShield,
|
|
32
|
-
mapShieldError,
|
|
33
|
-
rebuild,
|
|
34
|
-
resolveShieldOwner,
|
|
35
|
-
shield,
|
|
36
|
-
} from "@veilo/sdk-core";
|
|
37
|
-
|
|
38
|
-
const connection = new Connection(RPC_URL, "confirmed");
|
|
39
|
-
const owner = await resolveShieldOwner({ username: "alice" });
|
|
40
|
-
const prover = createTransactionProver({
|
|
41
|
-
wasmPath: TRANSACTION_WASM_URL,
|
|
42
|
-
zkeyPath: TRANSACTION_ZKEY_URL,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
let result = await shield({
|
|
46
|
-
connection,
|
|
47
|
-
amount: 5_000_000n, // base units
|
|
48
|
-
mint: new PublicKey(USDC_MINT),
|
|
49
|
-
owner,
|
|
50
|
-
signer: { publicKey: wallet.publicKey },
|
|
51
|
-
prover,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
async function signAndSubmit() {
|
|
55
|
-
const signed = await wallet.signTransaction(result.transaction);
|
|
56
|
-
const signature = await connection.sendRawTransaction(signed.serialize(), {
|
|
57
|
-
maxRetries: 0,
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const confirmation = await connection.confirmTransaction(
|
|
61
|
-
{
|
|
62
|
-
signature,
|
|
63
|
-
blockhash: result.blockhash,
|
|
64
|
-
lastValidBlockHeight: result.lastValidBlockHeight,
|
|
65
|
-
},
|
|
66
|
-
"confirmed",
|
|
67
|
-
);
|
|
68
|
-
if (confirmation.value.err) throw confirmation.value.err;
|
|
69
|
-
return signature;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let signature: string;
|
|
73
|
-
try {
|
|
74
|
-
signature = await signAndSubmit();
|
|
75
|
-
} catch (cause) {
|
|
76
|
-
const error = mapShieldError(cause);
|
|
77
|
-
if (error.retryable !== "rebuild") throw error;
|
|
78
|
-
|
|
79
|
-
// Reuses the proof and note; only the blockhash and deadline are refreshed.
|
|
80
|
-
result = await rebuild(result, { connection });
|
|
81
|
-
signature = await signAndSubmit(); // the rebuilt transaction needs a new signature
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const finalized = await finalizeShield({
|
|
85
|
-
connection,
|
|
86
|
-
signature,
|
|
87
|
-
note: result.note,
|
|
88
|
-
owner,
|
|
89
|
-
commitment: "finalized",
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
console.log(finalized.leafIndex, finalized.treeId);
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
`predictedLeafIndex` is informational only. Concurrent deposits can move the
|
|
96
|
-
actual index before your transaction lands. Persist `finalized.leafIndex`, which
|
|
97
|
-
comes from this transaction's `CommitmentEvent`.
|
|
98
|
-
|
|
99
|
-
## Preflight and wallet UX
|
|
100
|
-
|
|
101
|
-
Preflight runs before the 10–30 second proof and checks pool limits, token
|
|
102
|
-
ownership and balance, ATA state, and the SOL needed by the transaction. If an
|
|
103
|
-
SPL token account has any delegate—even with a delegated amount of zero—the
|
|
104
|
-
program rejects it. `TOKEN_ACCOUNT_DELEGATED` includes a `remedy` with the
|
|
105
|
-
`Revoke` instruction the wallet can sign before retrying.
|
|
106
|
-
|
|
107
|
-
The wallet may describe the Veilo instruction as an unknown program. Show the
|
|
108
|
-
amount and destination in your own review screen before opening the wallet so
|
|
109
|
-
the signer can verify what it is authorizing.
|
|
110
|
-
|
|
111
|
-
## SOL requirements
|
|
112
|
-
|
|
113
|
-
The signer pays:
|
|
114
|
-
|
|
115
|
-
- the amount itself for native SOL shields;
|
|
116
|
-
- the normal transaction and priority fees; and
|
|
117
|
-
- rent for two nullifier-marker accounts (about 0.00192 SOL at current rent).
|
|
118
|
-
|
|
119
|
-
The program fixes the marker payer to the signer. A zero-SOL wallet therefore
|
|
120
|
-
cannot use this path, even for an SPL token shield and even if another service
|
|
121
|
-
would sponsor the transaction fee. This API does not provide a gasless mode.
|
|
122
|
-
|
|
123
|
-
## Retry decisions
|
|
124
|
-
|
|
125
|
-
Use `mapShieldError()` and branch on `retryable`:
|
|
126
|
-
|
|
127
|
-
| Error | Retry class | Action |
|
|
128
|
-
|---|---|---|
|
|
129
|
-
| `BLOCKHASH_EXPIRED`, `DEADLINE_EXPIRED` | `rebuild` | Call `rebuild()`, sign again, submit again. |
|
|
130
|
-
| `ROOT_STALE` | `reshield` | Call `shield()` again; a fresh proof is required. |
|
|
131
|
-
| `NULLIFIER_COLLISION` | `none` | Treat as already submitted and check the original signature. |
|
|
132
|
-
| Preflight and configuration errors | `none` | Correct the stated condition before retrying. |
|
|
133
|
-
|
|
134
|
-
`ShieldProofCache` is public and JSON-serializable. `ShieldNote` is secret. Do
|
|
135
|
-
not store or transmit the note in the clear, and do not bundle it with the proof
|
|
136
|
-
cache merely because the cache is safe to move between processes.
|
|
137
|
-
|
|
138
|
-
## Optional mailbox delivery
|
|
139
|
-
|
|
140
|
-
`finalizeShield()` returns an encrypted `blob` suitable for `/notes/save`.
|
|
141
|
-
Supplying `recipientWalletPublicKey` gives faster mailbox delivery and push
|
|
142
|
-
notifications, but records a sender-to-recipient association at the relayer.
|
|
143
|
-
Omitting it keeps discovery chain-only and preserves that privacy boundary.
|
package/config.d.ts
DELETED
package/config.js
DELETED