@veilo/sdk-core 0.3.3 → 0.4.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/README.md +42 -3
- package/SHIELD_INTEGRATION.md +143 -0
- package/config.d.ts +2 -0
- package/config.js +4 -0
- package/dist/cjs/client.d.ts +5 -5
- package/dist/cjs/client.js +79 -42
- package/dist/cjs/compactNote.d.ts +107 -0
- package/dist/cjs/compactNote.js +167 -0
- package/dist/cjs/events.d.ts +1 -1
- package/dist/cjs/events.js +2 -2
- package/dist/cjs/idl/privacy_pool.d.ts +5 -0
- package/dist/cjs/idl/privacy_pool.js +15218 -0
- package/dist/cjs/index.d.ts +16 -13
- package/dist/cjs/index.js +40 -30
- package/dist/cjs/merkle.d.ts +13 -0
- package/dist/cjs/merkle.js +31 -8
- package/dist/cjs/program.d.ts +11 -0
- package/dist/cjs/program.js +26 -3
- package/dist/cjs/proof.d.ts +1 -1
- package/dist/cjs/proof.js +37 -37
- package/dist/cjs/prover.d.ts +1 -1
- package/dist/cjs/prover.js +2 -2
- package/dist/cjs/random.d.ts +16 -0
- package/dist/cjs/random.js +28 -0
- package/dist/cjs/relayer.d.ts +27 -4
- package/dist/cjs/relayer.js +16 -5
- package/dist/cjs/shield/alt.d.ts +87 -0
- package/dist/cjs/shield/alt.js +194 -0
- package/dist/cjs/shield/computeBudget.d.ts +61 -0
- package/dist/cjs/shield/computeBudget.js +64 -0
- package/dist/cjs/shield/errors.d.ts +58 -0
- package/dist/cjs/shield/errors.js +121 -0
- package/dist/cjs/shield/finalize.d.ts +45 -0
- package/dist/cjs/shield/finalize.js +119 -0
- package/dist/cjs/shield/index.d.ts +35 -0
- package/dist/cjs/shield/index.js +68 -0
- package/dist/cjs/shield/ix.d.ts +54 -0
- package/dist/cjs/shield/ix.js +119 -0
- package/dist/cjs/shield/owner.d.ts +36 -0
- package/dist/cjs/shield/owner.js +126 -0
- package/dist/cjs/shield/ports.d.ts +43 -0
- package/dist/cjs/shield/ports.js +153 -0
- package/dist/cjs/shield/preflight.d.ts +30 -0
- package/dist/cjs/shield/preflight.js +154 -0
- package/dist/cjs/shield/shield.d.ts +68 -0
- package/dist/cjs/shield/shield.js +499 -0
- package/dist/cjs/shield/types.d.ts +202 -0
- package/dist/cjs/shield/types.js +2 -0
- package/dist/cjs/utxo.d.ts +20 -0
- package/dist/cjs/utxo.js +41 -28
- package/dist/esm/client.d.ts +407 -0
- package/dist/esm/client.js +9 -5
- package/dist/esm/compactNote.d.ts +107 -0
- package/dist/esm/compactNote.js +156 -0
- package/dist/esm/config.d.ts +82 -0
- package/dist/esm/events.d.ts +77 -0
- package/dist/esm/events.js +1 -1
- package/dist/esm/idl/privacy_pool.d.ts +5 -0
- package/dist/esm/idl/privacy_pool.js +15216 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.js +18 -11
- package/dist/esm/merkle.d.ts +77 -0
- package/dist/esm/merkle.js +23 -1
- package/dist/esm/package.json +1 -0
- package/dist/esm/poseidon.d.ts +29 -0
- package/dist/esm/program.d.ts +37 -0
- package/dist/esm/program.js +23 -1
- package/dist/esm/proof.d.ts +183 -0
- package/dist/esm/proof.js +2 -2
- package/dist/esm/prover.d.ts +54 -0
- package/dist/esm/prover.js +1 -1
- package/dist/esm/random.d.ts +16 -0
- package/dist/esm/random.js +21 -0
- package/dist/esm/relayer.d.ts +318 -0
- package/dist/esm/relayer.js +16 -5
- package/dist/esm/retry.d.ts +32 -0
- package/dist/esm/shield/alt.d.ts +87 -0
- package/dist/esm/shield/alt.js +186 -0
- package/dist/esm/shield/computeBudget.d.ts +61 -0
- package/dist/esm/shield/computeBudget.js +61 -0
- package/dist/esm/shield/errors.d.ts +58 -0
- package/dist/esm/shield/errors.js +115 -0
- package/dist/esm/shield/finalize.d.ts +45 -0
- package/dist/esm/shield/finalize.js +83 -0
- package/dist/esm/shield/index.d.ts +35 -0
- package/dist/esm/shield/index.js +32 -0
- package/dist/esm/shield/ix.d.ts +54 -0
- package/dist/esm/shield/ix.js +82 -0
- package/dist/esm/shield/owner.d.ts +36 -0
- package/dist/esm/shield/owner.js +122 -0
- package/dist/esm/shield/ports.d.ts +43 -0
- package/dist/esm/shield/ports.js +147 -0
- package/dist/esm/shield/preflight.d.ts +30 -0
- package/dist/esm/shield/preflight.js +151 -0
- package/dist/esm/shield/shield.d.ts +68 -0
- package/dist/esm/shield/shield.js +492 -0
- package/dist/esm/shield/types.d.ts +202 -0
- package/dist/esm/shield/types.js +1 -0
- package/dist/esm/utxo.d.ts +235 -0
- package/dist/esm/utxo.js +18 -8
- package/dist/src/client.d.ts +407 -0
- package/dist/src/client.js +951 -0
- package/dist/src/compactNote.d.ts +107 -0
- package/dist/src/compactNote.js +167 -0
- package/dist/src/config.d.ts +82 -0
- package/dist/src/config.js +57 -0
- package/dist/src/events.d.ts +77 -0
- package/dist/src/events.js +167 -0
- package/dist/src/idl/privacy_pool.d.ts +5 -0
- package/dist/src/idl/privacy_pool.js +15218 -0
- package/dist/src/index.d.ts +16 -0
- package/dist/src/index.js +67 -0
- package/dist/src/merkle.d.ts +77 -0
- package/dist/src/merkle.js +156 -0
- package/dist/src/poseidon.d.ts +29 -0
- package/dist/src/poseidon.js +100 -0
- package/dist/src/program.d.ts +37 -0
- package/dist/src/program.js +61 -0
- package/dist/src/proof.d.ts +183 -0
- package/dist/src/proof.js +292 -0
- package/dist/src/prover.d.ts +54 -0
- package/dist/src/prover.js +112 -0
- package/dist/src/random.d.ts +16 -0
- package/dist/src/random.js +28 -0
- package/dist/src/relayer.d.ts +318 -0
- package/dist/src/relayer.js +257 -0
- package/dist/src/retry.d.ts +32 -0
- package/dist/src/retry.js +75 -0
- package/dist/src/shield/alt.d.ts +87 -0
- package/dist/src/shield/alt.js +194 -0
- package/dist/src/shield/computeBudget.d.ts +61 -0
- package/dist/src/shield/computeBudget.js +64 -0
- package/dist/src/shield/errors.d.ts +58 -0
- package/dist/src/shield/errors.js +121 -0
- package/dist/src/shield/finalize.d.ts +45 -0
- package/dist/src/shield/finalize.js +119 -0
- package/dist/src/shield/index.d.ts +35 -0
- package/dist/src/shield/index.js +68 -0
- package/dist/src/shield/ix.d.ts +54 -0
- package/dist/src/shield/ix.js +119 -0
- package/dist/src/shield/owner.d.ts +36 -0
- package/dist/src/shield/owner.js +126 -0
- package/dist/src/shield/ports.d.ts +43 -0
- package/dist/src/shield/ports.js +153 -0
- package/dist/src/shield/preflight.d.ts +30 -0
- package/dist/src/shield/preflight.js +154 -0
- package/dist/src/shield/shield.d.ts +68 -0
- package/dist/src/shield/shield.js +499 -0
- package/dist/src/shield/types.d.ts +202 -0
- package/dist/src/shield/types.js +2 -0
- package/dist/src/utxo.d.ts +235 -0
- package/dist/src/utxo.js +407 -0
- package/dist/tests/compact-note.test.d.ts +1 -0
- package/dist/tests/compact-note.test.js +173 -0
- package/dist/tests/config.test.d.ts +1 -0
- package/dist/tests/config.test.js +102 -0
- package/dist/tests/edge-cases.test.d.ts +1 -0
- package/dist/tests/edge-cases.test.js +220 -0
- package/dist/tests/encryption.test.d.ts +1 -0
- package/dist/tests/encryption.test.js +215 -0
- package/dist/tests/events.test.d.ts +1 -0
- package/dist/tests/events.test.js +78 -0
- package/dist/tests/multi-tree.test.d.ts +1 -0
- package/dist/tests/multi-tree.test.js +405 -0
- package/dist/tests/pda.test.d.ts +1 -0
- package/dist/tests/pda.test.js +229 -0
- package/dist/tests/poseidon-builder-parity.test.d.ts +1 -0
- package/dist/tests/poseidon-builder-parity.test.js +72 -0
- package/dist/tests/poseidon.test.d.ts +1 -0
- package/dist/tests/poseidon.test.js +142 -0
- package/dist/tests/proof.test.d.ts +1 -0
- package/dist/tests/proof.test.js +296 -0
- package/dist/tests/relayer.test.d.ts +1 -0
- package/dist/tests/relayer.test.js +271 -0
- package/dist/tests/sdk.integration.test.d.ts +1 -0
- package/dist/tests/sdk.integration.test.js +330 -0
- package/dist/tests/shield-owner.test.d.ts +1 -0
- package/dist/tests/shield-owner.test.js +89 -0
- package/dist/tests/shield-preflight.test.d.ts +1 -0
- package/dist/tests/shield-preflight.test.js +87 -0
- package/dist/tests/shield-realproof.test.d.ts +1 -0
- package/dist/tests/shield-realproof.test.js +272 -0
- package/dist/tests/shield.test.d.ts +1 -0
- package/dist/tests/shield.test.js +403 -0
- package/dist/tests/utxo.test.d.ts +1 -0
- package/dist/tests/utxo.test.js +140 -0
- package/package.json +69 -11
- package/poseidon.d.ts +2 -0
- package/poseidon.js +4 -0
- package/proof.d.ts +2 -0
- package/proof.js +4 -0
- package/prover.d.ts +2 -0
- package/prover.js +4 -0
- package/shield.d.ts +2 -0
- package/shield.js +4 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.finalizeShield = finalizeShield;
|
|
37
|
+
const anchor = __importStar(require("@coral-xyz/anchor"));
|
|
38
|
+
const program_js_1 = require("../program.js");
|
|
39
|
+
const utxo_js_1 = require("../utxo.js");
|
|
40
|
+
const poseidon_js_1 = require("../poseidon.js");
|
|
41
|
+
const errors_js_1 = require("./errors.js");
|
|
42
|
+
/**
|
|
43
|
+
* Read the authoritative leaf index for a landed shield, and produce the
|
|
44
|
+
* mailbox blob for optional relayer delivery.
|
|
45
|
+
*
|
|
46
|
+
* ## Why this is a separate step
|
|
47
|
+
*
|
|
48
|
+
* Every existing deposit implementation records `tree.nextIndex` read *before*
|
|
49
|
+
* submission as the note's leaf index. Under concurrent deposits into the same
|
|
50
|
+
* tree that value is wrong, and it is wrong permanently — the stored index is
|
|
51
|
+
* what a later withdrawal builds its Merkle path from, so the note becomes
|
|
52
|
+
* unspendable through the normal path.
|
|
53
|
+
*
|
|
54
|
+
* That race is rare for a single wallet and constant for an SDK: a platform
|
|
55
|
+
* batching shields for many users will hit it routinely. So `shield()` reports
|
|
56
|
+
* only a `predictedLeafIndex`, and the real one is read here from the
|
|
57
|
+
* transaction's own `CommitmentEvent` after it confirms.
|
|
58
|
+
*
|
|
59
|
+
* ## What is optional
|
|
60
|
+
*
|
|
61
|
+
* Everything, strictly. The note is recoverable from chain data alone via the
|
|
62
|
+
* on-chain compact cipher, so a caller that skips finalization has not lost the
|
|
63
|
+
* funds — only the fast path to finding them. Persist nothing that depends on a
|
|
64
|
+
* leaf index without calling this first.
|
|
65
|
+
*/
|
|
66
|
+
async function finalizeShield(params) {
|
|
67
|
+
const { connection, signature, note, owner, programId = program_js_1.PRIVACY_POOL_PROGRAM_ID, commitment = "confirmed", } = params;
|
|
68
|
+
const tx = await connection.getTransaction(signature, {
|
|
69
|
+
commitment: commitment === "finalized" ? "finalized" : "confirmed",
|
|
70
|
+
maxSupportedTransactionVersion: 0,
|
|
71
|
+
});
|
|
72
|
+
if (!tx) {
|
|
73
|
+
throw new errors_js_1.ShieldError("UNKNOWN", `Transaction ${signature} not found at ${commitment} commitment. ` +
|
|
74
|
+
`If it was just submitted, wait for confirmation and retry.`, { context: { signature } });
|
|
75
|
+
}
|
|
76
|
+
if (tx.meta?.err) {
|
|
77
|
+
throw new errors_js_1.ShieldError("UNKNOWN", `Transaction ${signature} failed on-chain; there is no note to finalize.`, { context: { signature, err: tx.meta.err } });
|
|
78
|
+
}
|
|
79
|
+
const parser = new anchor.EventParser(programId, new anchor.BorshCoder(program_js_1.PRIVACY_POOL_IDL));
|
|
80
|
+
const wantCommitment = Buffer.from(note.commitment).toString("hex");
|
|
81
|
+
let match = null;
|
|
82
|
+
for (const event of parser.parseLogs(tx.meta?.logMessages ?? [])) {
|
|
83
|
+
if (event.name !== "CommitmentEvent" && event.name !== "commitmentEvent")
|
|
84
|
+
continue;
|
|
85
|
+
const data = event.data;
|
|
86
|
+
const emitted = Buffer.from(data.commitment).toString("hex");
|
|
87
|
+
if (emitted === wantCommitment) {
|
|
88
|
+
match = data;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!match) {
|
|
93
|
+
throw new errors_js_1.ShieldError("UNKNOWN", `Transaction ${signature} landed but emitted no CommitmentEvent for this note's ` +
|
|
94
|
+
`commitment. The note may belong to a different transaction.`, { context: { signature, commitment: wantCommitment } });
|
|
95
|
+
}
|
|
96
|
+
const leafIndex = Number(match.leafIndex ?? match.leaf_index);
|
|
97
|
+
const treeId = Number(match.treeId ?? match.tree_id ?? note.treeId);
|
|
98
|
+
const newRoot = new Uint8Array(match.newRoot ?? match.new_root);
|
|
99
|
+
// Mailbox copy. Sending this to the relayer with a `recipientWalletPublicKey`
|
|
100
|
+
// buys instant delivery and a push notification, at the cost of writing the
|
|
101
|
+
// sender→recipient association into the relayer's database AND removing the
|
|
102
|
+
// note from the chain-recovery feed. For shield-to-someone-else, chain-only
|
|
103
|
+
// discovery is usually the better default — see the plan's Phase 3 note.
|
|
104
|
+
const blob = (0, utxo_js_1.encryptBlindMailboxNote)(owner.noteViewingKey.toBytes(), {
|
|
105
|
+
blinding: (0, poseidon_js_1.bigIntToBytesBE)(note.blinding),
|
|
106
|
+
leafIndex,
|
|
107
|
+
commitment: note.commitment,
|
|
108
|
+
amount: note.amount,
|
|
109
|
+
mintAddress: note.mint.toBase58(),
|
|
110
|
+
treeId,
|
|
111
|
+
});
|
|
112
|
+
return {
|
|
113
|
+
leafIndex,
|
|
114
|
+
newRoot,
|
|
115
|
+
treeId,
|
|
116
|
+
slot: tx.slot,
|
|
117
|
+
blob,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wallet-agnostic shielding.
|
|
3
|
+
*
|
|
4
|
+
* `shield()` builds an unsigned transaction that deposits public funds into the
|
|
5
|
+
* privacy pool, creating a note owned by an arbitrary recipient. Any external
|
|
6
|
+
* wallet — a browser extension, or a third-party platform's managed wallet —
|
|
7
|
+
* signs and submits it. The SDK never touches a secret key.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const owner = await resolveShieldOwner({ username: "alice" });
|
|
11
|
+
* const { transaction, note, proofCache } = await shield({
|
|
12
|
+
* connection, amount, mint, owner,
|
|
13
|
+
* signer: { publicKey: wallet.publicKey },
|
|
14
|
+
* prover: createTransactionProver(artifacts),
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* const signed = await wallet.signTransaction(transaction);
|
|
18
|
+
* const sig = await connection.sendRawTransaction(signed.serialize());
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* On blockhash expiry, `rebuild(result, { connection })` re-assembles with a
|
|
22
|
+
* fresh blockhash and deadline without re-proving.
|
|
23
|
+
*/
|
|
24
|
+
export { shield, rebuild, serializeProofCache, deserializeProofCache } from "./shield.js";
|
|
25
|
+
export { resolveShieldOwner, unsafeShieldOwner } from "./owner.js";
|
|
26
|
+
export { finalizeShield } from "./finalize.js";
|
|
27
|
+
export { preflightShield } from "./preflight.js";
|
|
28
|
+
export { buildTransactInstruction, TRANSACT_ACCOUNT_NAMES } from "./ix.js";
|
|
29
|
+
export type { TransactAccounts, TransactArgs } from "./ix.js";
|
|
30
|
+
export { sharedDepositAlt, explicitAlt, auditDepositAlt, expectedDepositAltKeys, requiredAltKeys, SHARED_DEPOSIT_ALT, SHARED_ALT_MINTS, } from "./alt.js";
|
|
31
|
+
export { onChainTreeSource, staticTreeSource, memoryCache, noCache } from "./ports.js";
|
|
32
|
+
export { ShieldError, mapShieldError, extractProgramCode, } from "./errors.js";
|
|
33
|
+
export type { ShieldErrorCode, ShieldRetryClass, ShieldErrorRemedy } from "./errors.js";
|
|
34
|
+
export { SHIELD_COMPUTE_UNIT_LIMIT, SHIELD_COMPUTE_UNIT_PRICE_MICRO_LAMPORTS, SHIELD_COMPUTE_UNITS_MEASURED_TYPICAL, MAX_TRANSACTION_BYTES, SIGNATURE_BYTES, TRANSACT_INSTRUCTION_DATA_BYTES, NULLIFIER_MARKER_ACCOUNT_BYTES, NULLIFIER_MARKERS_PER_TRANSACT, DEFAULT_DEADLINE_SECONDS, } from "./computeBudget.js";
|
|
35
|
+
export type { ShieldOwner, ShieldSigner, ShieldParams, ShieldResult, ShieldNote, ShieldProofCache, ShieldQuote, TreeSource, AltSource, ShieldCachePort, DepositTarget, PreflightOptions, } from "./types.js";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Wallet-agnostic shielding.
|
|
4
|
+
*
|
|
5
|
+
* `shield()` builds an unsigned transaction that deposits public funds into the
|
|
6
|
+
* privacy pool, creating a note owned by an arbitrary recipient. Any external
|
|
7
|
+
* wallet — a browser extension, or a third-party platform's managed wallet —
|
|
8
|
+
* signs and submits it. The SDK never touches a secret key.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const owner = await resolveShieldOwner({ username: "alice" });
|
|
12
|
+
* const { transaction, note, proofCache } = await shield({
|
|
13
|
+
* connection, amount, mint, owner,
|
|
14
|
+
* signer: { publicKey: wallet.publicKey },
|
|
15
|
+
* prover: createTransactionProver(artifacts),
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* const signed = await wallet.signTransaction(transaction);
|
|
19
|
+
* const sig = await connection.sendRawTransaction(signed.serialize());
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* On blockhash expiry, `rebuild(result, { connection })` re-assembles with a
|
|
23
|
+
* fresh blockhash and deadline without re-proving.
|
|
24
|
+
*/
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.DEFAULT_DEADLINE_SECONDS = exports.NULLIFIER_MARKERS_PER_TRANSACT = exports.NULLIFIER_MARKER_ACCOUNT_BYTES = exports.TRANSACT_INSTRUCTION_DATA_BYTES = exports.SIGNATURE_BYTES = exports.MAX_TRANSACTION_BYTES = exports.SHIELD_COMPUTE_UNITS_MEASURED_TYPICAL = exports.SHIELD_COMPUTE_UNIT_PRICE_MICRO_LAMPORTS = exports.SHIELD_COMPUTE_UNIT_LIMIT = exports.extractProgramCode = exports.mapShieldError = exports.ShieldError = exports.noCache = exports.memoryCache = exports.staticTreeSource = exports.onChainTreeSource = exports.SHARED_ALT_MINTS = exports.SHARED_DEPOSIT_ALT = exports.requiredAltKeys = exports.expectedDepositAltKeys = exports.auditDepositAlt = exports.explicitAlt = exports.sharedDepositAlt = exports.TRANSACT_ACCOUNT_NAMES = exports.buildTransactInstruction = exports.preflightShield = exports.finalizeShield = exports.unsafeShieldOwner = exports.resolveShieldOwner = exports.deserializeProofCache = exports.serializeProofCache = exports.rebuild = exports.shield = void 0;
|
|
27
|
+
var shield_js_1 = require("./shield.js");
|
|
28
|
+
Object.defineProperty(exports, "shield", { enumerable: true, get: function () { return shield_js_1.shield; } });
|
|
29
|
+
Object.defineProperty(exports, "rebuild", { enumerable: true, get: function () { return shield_js_1.rebuild; } });
|
|
30
|
+
Object.defineProperty(exports, "serializeProofCache", { enumerable: true, get: function () { return shield_js_1.serializeProofCache; } });
|
|
31
|
+
Object.defineProperty(exports, "deserializeProofCache", { enumerable: true, get: function () { return shield_js_1.deserializeProofCache; } });
|
|
32
|
+
var owner_js_1 = require("./owner.js");
|
|
33
|
+
Object.defineProperty(exports, "resolveShieldOwner", { enumerable: true, get: function () { return owner_js_1.resolveShieldOwner; } });
|
|
34
|
+
Object.defineProperty(exports, "unsafeShieldOwner", { enumerable: true, get: function () { return owner_js_1.unsafeShieldOwner; } });
|
|
35
|
+
var finalize_js_1 = require("./finalize.js");
|
|
36
|
+
Object.defineProperty(exports, "finalizeShield", { enumerable: true, get: function () { return finalize_js_1.finalizeShield; } });
|
|
37
|
+
var preflight_js_1 = require("./preflight.js");
|
|
38
|
+
Object.defineProperty(exports, "preflightShield", { enumerable: true, get: function () { return preflight_js_1.preflightShield; } });
|
|
39
|
+
var ix_js_1 = require("./ix.js");
|
|
40
|
+
Object.defineProperty(exports, "buildTransactInstruction", { enumerable: true, get: function () { return ix_js_1.buildTransactInstruction; } });
|
|
41
|
+
Object.defineProperty(exports, "TRANSACT_ACCOUNT_NAMES", { enumerable: true, get: function () { return ix_js_1.TRANSACT_ACCOUNT_NAMES; } });
|
|
42
|
+
var alt_js_1 = require("./alt.js");
|
|
43
|
+
Object.defineProperty(exports, "sharedDepositAlt", { enumerable: true, get: function () { return alt_js_1.sharedDepositAlt; } });
|
|
44
|
+
Object.defineProperty(exports, "explicitAlt", { enumerable: true, get: function () { return alt_js_1.explicitAlt; } });
|
|
45
|
+
Object.defineProperty(exports, "auditDepositAlt", { enumerable: true, get: function () { return alt_js_1.auditDepositAlt; } });
|
|
46
|
+
Object.defineProperty(exports, "expectedDepositAltKeys", { enumerable: true, get: function () { return alt_js_1.expectedDepositAltKeys; } });
|
|
47
|
+
Object.defineProperty(exports, "requiredAltKeys", { enumerable: true, get: function () { return alt_js_1.requiredAltKeys; } });
|
|
48
|
+
Object.defineProperty(exports, "SHARED_DEPOSIT_ALT", { enumerable: true, get: function () { return alt_js_1.SHARED_DEPOSIT_ALT; } });
|
|
49
|
+
Object.defineProperty(exports, "SHARED_ALT_MINTS", { enumerable: true, get: function () { return alt_js_1.SHARED_ALT_MINTS; } });
|
|
50
|
+
var ports_js_1 = require("./ports.js");
|
|
51
|
+
Object.defineProperty(exports, "onChainTreeSource", { enumerable: true, get: function () { return ports_js_1.onChainTreeSource; } });
|
|
52
|
+
Object.defineProperty(exports, "staticTreeSource", { enumerable: true, get: function () { return ports_js_1.staticTreeSource; } });
|
|
53
|
+
Object.defineProperty(exports, "memoryCache", { enumerable: true, get: function () { return ports_js_1.memoryCache; } });
|
|
54
|
+
Object.defineProperty(exports, "noCache", { enumerable: true, get: function () { return ports_js_1.noCache; } });
|
|
55
|
+
var errors_js_1 = require("./errors.js");
|
|
56
|
+
Object.defineProperty(exports, "ShieldError", { enumerable: true, get: function () { return errors_js_1.ShieldError; } });
|
|
57
|
+
Object.defineProperty(exports, "mapShieldError", { enumerable: true, get: function () { return errors_js_1.mapShieldError; } });
|
|
58
|
+
Object.defineProperty(exports, "extractProgramCode", { enumerable: true, get: function () { return errors_js_1.extractProgramCode; } });
|
|
59
|
+
var computeBudget_js_1 = require("./computeBudget.js");
|
|
60
|
+
Object.defineProperty(exports, "SHIELD_COMPUTE_UNIT_LIMIT", { enumerable: true, get: function () { return computeBudget_js_1.SHIELD_COMPUTE_UNIT_LIMIT; } });
|
|
61
|
+
Object.defineProperty(exports, "SHIELD_COMPUTE_UNIT_PRICE_MICRO_LAMPORTS", { enumerable: true, get: function () { return computeBudget_js_1.SHIELD_COMPUTE_UNIT_PRICE_MICRO_LAMPORTS; } });
|
|
62
|
+
Object.defineProperty(exports, "SHIELD_COMPUTE_UNITS_MEASURED_TYPICAL", { enumerable: true, get: function () { return computeBudget_js_1.SHIELD_COMPUTE_UNITS_MEASURED_TYPICAL; } });
|
|
63
|
+
Object.defineProperty(exports, "MAX_TRANSACTION_BYTES", { enumerable: true, get: function () { return computeBudget_js_1.MAX_TRANSACTION_BYTES; } });
|
|
64
|
+
Object.defineProperty(exports, "SIGNATURE_BYTES", { enumerable: true, get: function () { return computeBudget_js_1.SIGNATURE_BYTES; } });
|
|
65
|
+
Object.defineProperty(exports, "TRANSACT_INSTRUCTION_DATA_BYTES", { enumerable: true, get: function () { return computeBudget_js_1.TRANSACT_INSTRUCTION_DATA_BYTES; } });
|
|
66
|
+
Object.defineProperty(exports, "NULLIFIER_MARKER_ACCOUNT_BYTES", { enumerable: true, get: function () { return computeBudget_js_1.NULLIFIER_MARKER_ACCOUNT_BYTES; } });
|
|
67
|
+
Object.defineProperty(exports, "NULLIFIER_MARKERS_PER_TRANSACT", { enumerable: true, get: function () { return computeBudget_js_1.NULLIFIER_MARKERS_PER_TRANSACT; } });
|
|
68
|
+
Object.defineProperty(exports, "DEFAULT_DEADLINE_SECONDS", { enumerable: true, get: function () { return computeBudget_js_1.DEFAULT_DEADLINE_SECONDS; } });
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { PublicKey, TransactionInstruction } from "@solana/web3.js";
|
|
2
|
+
import type { TransactionProofStruct } from "../proof.js";
|
|
3
|
+
/**
|
|
4
|
+
* `transact` instruction assembly, driven by the IDL rather than by a
|
|
5
|
+
* hand-written account list.
|
|
6
|
+
*
|
|
7
|
+
* Every existing deposit implementation spells out all sixteen accounts by hand
|
|
8
|
+
* in an `accountsStrict({...})` literal. That works until the IDL changes, at
|
|
9
|
+
* which point a silently misordered or missing account produces a runtime
|
|
10
|
+
* failure that is very hard to trace. Here the account list and the ordering
|
|
11
|
+
* come from the IDL itself, and an unresolved name throws at build time.
|
|
12
|
+
*
|
|
13
|
+
* Building the instruction directly also means no Anchor `Provider` and no
|
|
14
|
+
* `Wallet` — which is the whole point of a wallet-agnostic shield.
|
|
15
|
+
*/
|
|
16
|
+
/** Account names the IDL declares for `transact`, in order. */
|
|
17
|
+
export type TransactAccounts = Record<string, PublicKey>;
|
|
18
|
+
export type TransactArgs = {
|
|
19
|
+
root: Uint8Array;
|
|
20
|
+
inputTreeId: number;
|
|
21
|
+
outputTreeId: number;
|
|
22
|
+
publicAmount: bigint;
|
|
23
|
+
extDataHash: Uint8Array;
|
|
24
|
+
mintAddress: PublicKey;
|
|
25
|
+
inputNullifiers: [Uint8Array, Uint8Array];
|
|
26
|
+
outputCommitments: [Uint8Array, Uint8Array];
|
|
27
|
+
deadline: bigint;
|
|
28
|
+
extData: {
|
|
29
|
+
recipient: PublicKey;
|
|
30
|
+
relayer: PublicKey;
|
|
31
|
+
fee: bigint;
|
|
32
|
+
refund: bigint;
|
|
33
|
+
claimant: PublicKey;
|
|
34
|
+
};
|
|
35
|
+
proof: TransactionProofStruct;
|
|
36
|
+
noteCiphers: {
|
|
37
|
+
note0EphemeralKey: Uint8Array;
|
|
38
|
+
note0Encrypted: Uint8Array;
|
|
39
|
+
note0ViewTag: number;
|
|
40
|
+
note1EphemeralKey: Uint8Array;
|
|
41
|
+
note1Encrypted: Uint8Array;
|
|
42
|
+
note1ViewTag: number;
|
|
43
|
+
} | null;
|
|
44
|
+
};
|
|
45
|
+
/** Account names in IDL order. Exported so callers can see what must be resolved. */
|
|
46
|
+
export declare const TRANSACT_ACCOUNT_NAMES: string[];
|
|
47
|
+
/**
|
|
48
|
+
* Build the `transact` instruction.
|
|
49
|
+
*
|
|
50
|
+
* @throws ShieldError('UNRESOLVED_ACCOUNT') if the IDL declares an account the
|
|
51
|
+
* caller did not supply — a loud failure is the point, because the
|
|
52
|
+
* alternative is a misordered account list that fails opaquely on-chain.
|
|
53
|
+
*/
|
|
54
|
+
export declare function buildTransactInstruction(accounts: TransactAccounts, args: TransactArgs, programId?: PublicKey): TransactionInstruction;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.TRANSACT_ACCOUNT_NAMES = void 0;
|
|
37
|
+
exports.buildTransactInstruction = buildTransactInstruction;
|
|
38
|
+
const anchor = __importStar(require("@coral-xyz/anchor"));
|
|
39
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
40
|
+
const program_js_1 = require("../program.js");
|
|
41
|
+
const errors_js_1 = require("./errors.js");
|
|
42
|
+
const computeBudget_js_1 = require("./computeBudget.js");
|
|
43
|
+
// Anchor is CommonJS and does not expose BN as a detectable named export to
|
|
44
|
+
// Node ESM. Pull both runtime values from the namespace so CJS and ESM agree.
|
|
45
|
+
const { BN, BorshInstructionCoder } = anchor;
|
|
46
|
+
const IDL_TRANSACT = program_js_1.PRIVACY_POOL_IDL.instructions.find((i) => i.name === "transact");
|
|
47
|
+
if (!IDL_TRANSACT) {
|
|
48
|
+
throw new Error("privacy_pool IDL has no `transact` instruction");
|
|
49
|
+
}
|
|
50
|
+
/** Account names in IDL order. Exported so callers can see what must be resolved. */
|
|
51
|
+
exports.TRANSACT_ACCOUNT_NAMES = IDL_TRANSACT.accounts.map((a) => a.name);
|
|
52
|
+
/**
|
|
53
|
+
* Build the `transact` instruction.
|
|
54
|
+
*
|
|
55
|
+
* @throws ShieldError('UNRESOLVED_ACCOUNT') if the IDL declares an account the
|
|
56
|
+
* caller did not supply — a loud failure is the point, because the
|
|
57
|
+
* alternative is a misordered account list that fails opaquely on-chain.
|
|
58
|
+
*/
|
|
59
|
+
function buildTransactInstruction(accounts, args, programId = program_js_1.PRIVACY_POOL_PROGRAM_ID) {
|
|
60
|
+
const keys = IDL_TRANSACT.accounts.map((a) => {
|
|
61
|
+
const pubkey = accounts[a.name];
|
|
62
|
+
if (!pubkey) {
|
|
63
|
+
throw new errors_js_1.ShieldError("UNRESOLVED_ACCOUNT", `The IDL declares account "${a.name}" for transact, but it was not resolved. ` +
|
|
64
|
+
`Expected accounts: ${exports.TRANSACT_ACCOUNT_NAMES.join(", ")}`, { context: { missing: a.name, supplied: Object.keys(accounts) } });
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
pubkey,
|
|
68
|
+
isWritable: !!a.writable,
|
|
69
|
+
isSigner: !!a.signer,
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
const coder = new BorshInstructionCoder(program_js_1.PRIVACY_POOL_IDL);
|
|
73
|
+
const data = coder.encode("transact", toBorshArgs(args));
|
|
74
|
+
if (data.length !== computeBudget_js_1.TRANSACT_INSTRUCTION_DATA_BYTES) {
|
|
75
|
+
// Not fatal — the size budget assertion downstream is authoritative — but a
|
|
76
|
+
// change here means the size arithmetic in computeBudget.ts is stale.
|
|
77
|
+
console.warn(`[veilo] transact instruction data is ${data.length} bytes, expected ` +
|
|
78
|
+
`${computeBudget_js_1.TRANSACT_INSTRUCTION_DATA_BYTES}. The shield size budget may no longer hold.`);
|
|
79
|
+
}
|
|
80
|
+
return new web3_js_1.TransactionInstruction({ programId, keys, data });
|
|
81
|
+
}
|
|
82
|
+
/** Anchor's Borsh coder wants camelCase field names, BNs, and plain arrays. */
|
|
83
|
+
function toBorshArgs(args) {
|
|
84
|
+
return {
|
|
85
|
+
root: Array.from(args.root),
|
|
86
|
+
input_tree_id: args.inputTreeId,
|
|
87
|
+
output_tree_id: args.outputTreeId,
|
|
88
|
+
public_amount: new BN(args.publicAmount.toString()),
|
|
89
|
+
ext_data_hash: Array.from(args.extDataHash),
|
|
90
|
+
mint_address: args.mintAddress,
|
|
91
|
+
input_nullifier_0: Array.from(args.inputNullifiers[0]),
|
|
92
|
+
input_nullifier_1: Array.from(args.inputNullifiers[1]),
|
|
93
|
+
output_commitment_0: Array.from(args.outputCommitments[0]),
|
|
94
|
+
output_commitment_1: Array.from(args.outputCommitments[1]),
|
|
95
|
+
deadline: new BN(args.deadline.toString()),
|
|
96
|
+
ext_data: {
|
|
97
|
+
recipient: args.extData.recipient,
|
|
98
|
+
relayer: args.extData.relayer,
|
|
99
|
+
fee: new BN(args.extData.fee.toString()),
|
|
100
|
+
refund: new BN(args.extData.refund.toString()),
|
|
101
|
+
claimant: args.extData.claimant,
|
|
102
|
+
},
|
|
103
|
+
proof: {
|
|
104
|
+
proof_a: args.proof.proofA,
|
|
105
|
+
proof_b: args.proof.proofB,
|
|
106
|
+
proof_c: args.proof.proofC,
|
|
107
|
+
},
|
|
108
|
+
note_ciphers: args.noteCiphers
|
|
109
|
+
? {
|
|
110
|
+
note0_ephemeral_key: Array.from(args.noteCiphers.note0EphemeralKey),
|
|
111
|
+
note0_encrypted: Array.from(args.noteCiphers.note0Encrypted),
|
|
112
|
+
note0_view_tag: args.noteCiphers.note0ViewTag,
|
|
113
|
+
note1_ephemeral_key: Array.from(args.noteCiphers.note1EphemeralKey),
|
|
114
|
+
note1_encrypted: Array.from(args.noteCiphers.note1Encrypted),
|
|
115
|
+
note1_view_tag: args.noteCiphers.note1ViewTag,
|
|
116
|
+
}
|
|
117
|
+
: null,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import type { ShieldOwner } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Resolve a shield recipient into the pair of keys that must belong to the same
|
|
5
|
+
* account.
|
|
6
|
+
*
|
|
7
|
+
* This is the only mechanism that makes the owner invariant safe. A note's
|
|
8
|
+
* *spend* authority is a Poseidon field element and its *detection* authority is
|
|
9
|
+
* an Ed25519 wallet key; nothing on-chain relates them, and if an integrator
|
|
10
|
+
* sources them separately and gets them wrong, the note is spendable but
|
|
11
|
+
* permanently invisible — the funds are gone from the owner's view with no error
|
|
12
|
+
* raised anywhere in the stack.
|
|
13
|
+
*
|
|
14
|
+
* `GET /auth/veiloPublicKey` returns both halves in a single response, so
|
|
15
|
+
* routing through this function makes the pairing structural rather than a
|
|
16
|
+
* convention integrators are asked to remember.
|
|
17
|
+
*
|
|
18
|
+
* Prefer this over hand-building a {@link ShieldOwner} in all cases.
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolveShieldOwner(target: {
|
|
21
|
+
username: string;
|
|
22
|
+
} | {
|
|
23
|
+
publicKey: string | PublicKey;
|
|
24
|
+
}): Promise<ShieldOwner>;
|
|
25
|
+
/**
|
|
26
|
+
* Build a {@link ShieldOwner} from keys the caller already holds.
|
|
27
|
+
*
|
|
28
|
+
* Only correct when both keys provably belong to the same account. The explicit
|
|
29
|
+
* flag is not ceremony — it is the last checkpoint before a class of bug whose
|
|
30
|
+
* only symptom is a user's money quietly disappearing from their balance.
|
|
31
|
+
*/
|
|
32
|
+
export declare function unsafeShieldOwner(params: {
|
|
33
|
+
veiloPublicKey: bigint;
|
|
34
|
+
noteViewingKey: PublicKey;
|
|
35
|
+
acknowledgeOwnerInvariant: true;
|
|
36
|
+
}): ShieldOwner;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveShieldOwner = resolveShieldOwner;
|
|
4
|
+
exports.unsafeShieldOwner = unsafeShieldOwner;
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const relayer_js_1 = require("../relayer.js");
|
|
7
|
+
const errors_js_1 = require("./errors.js");
|
|
8
|
+
/**
|
|
9
|
+
* Owner resolution is a Veilo protocol service, not an integrator-selected
|
|
10
|
+
* transport. Keep the production endpoint and public encryption key inside the
|
|
11
|
+
* SDK so callers cannot accidentally resolve the two owner keys from different
|
|
12
|
+
* relayers.
|
|
13
|
+
*
|
|
14
|
+
* These values are public network coordinates, not credentials. In particular,
|
|
15
|
+
* never put a secret API key here: browser/mobile package contents are always
|
|
16
|
+
* inspectable. The two owner endpoints used below are intentionally public and
|
|
17
|
+
* return public key material only.
|
|
18
|
+
*/
|
|
19
|
+
const OWNER_RELAYER = new relayer_js_1.VeiloRelayerClient({
|
|
20
|
+
apiKey: "",
|
|
21
|
+
relayerPublicKey: "utVxnA7zax09qJCZ7UJsa8PAOoWLRcCwOkdxg/ZGmD4=",
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Resolve a shield recipient into the pair of keys that must belong to the same
|
|
25
|
+
* account.
|
|
26
|
+
*
|
|
27
|
+
* This is the only mechanism that makes the owner invariant safe. A note's
|
|
28
|
+
* *spend* authority is a Poseidon field element and its *detection* authority is
|
|
29
|
+
* an Ed25519 wallet key; nothing on-chain relates them, and if an integrator
|
|
30
|
+
* sources them separately and gets them wrong, the note is spendable but
|
|
31
|
+
* permanently invisible — the funds are gone from the owner's view with no error
|
|
32
|
+
* raised anywhere in the stack.
|
|
33
|
+
*
|
|
34
|
+
* `GET /auth/veiloPublicKey` returns both halves in a single response, so
|
|
35
|
+
* routing through this function makes the pairing structural rather than a
|
|
36
|
+
* convention integrators are asked to remember.
|
|
37
|
+
*
|
|
38
|
+
* Prefer this over hand-building a {@link ShieldOwner} in all cases.
|
|
39
|
+
*/
|
|
40
|
+
async function resolveShieldOwner(target) {
|
|
41
|
+
const query = "username" in target
|
|
42
|
+
? { username: target.username }
|
|
43
|
+
: {
|
|
44
|
+
publicKey: typeof target.publicKey === "string"
|
|
45
|
+
? target.publicKey
|
|
46
|
+
: target.publicKey.toBase58(),
|
|
47
|
+
};
|
|
48
|
+
let response;
|
|
49
|
+
try {
|
|
50
|
+
response = await OWNER_RELAYER.getVeiloPublicKey(query);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
// A raw Solana address may not have a Veilo account yet. Provisioning is
|
|
54
|
+
// public and idempotent, and returns only the Poseidon spend key; the
|
|
55
|
+
// address we were given is the matching note-viewing key.
|
|
56
|
+
if ("publicKey" in target &&
|
|
57
|
+
(e?.status === 404 || (e instanceof relayer_js_1.VeiloApiError && e.status === 404))) {
|
|
58
|
+
const wallet = typeof target.publicKey === "string"
|
|
59
|
+
? new web3_js_1.PublicKey(target.publicKey)
|
|
60
|
+
: target.publicKey;
|
|
61
|
+
const provisioned = await OWNER_RELAYER.preRegisterKey(wallet.toBase58());
|
|
62
|
+
if (!provisioned?.veiloPublicKey) {
|
|
63
|
+
throw new errors_js_1.ShieldError("UNKNOWN", "The relayer pre-registered the recipient but returned no Veilo public key.", { context: { publicKey: wallet.toBase58(), response: provisioned }, cause: e });
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
veiloPublicKey: BigInt(provisioned.veiloPublicKey),
|
|
67
|
+
noteViewingKey: wallet,
|
|
68
|
+
acknowledgeOwnerInvariant: true,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
// A pre-registered account is one someone else created on the recipient's
|
|
72
|
+
// behalf; the recipient has never proved ownership, so the relayer refuses
|
|
73
|
+
// to resolve it by username. Shielding to them still works, but the caller
|
|
74
|
+
// must address them by Solana address instead.
|
|
75
|
+
if (e?.body?.preRegistered ||
|
|
76
|
+
e?.details?.preRegistered ||
|
|
77
|
+
/preregistered/i.test(String(e?.message ?? ""))) {
|
|
78
|
+
throw new errors_js_1.ShieldError("UNKNOWN", `"${"username" in target ? target.username : "that account"}" is pre-registered ` +
|
|
79
|
+
`and cannot be resolved by username. Resolve it by Solana address instead — ` +
|
|
80
|
+
`resolveShieldOwner({ publicKey }).`, { context: { preRegistered: true }, cause: e });
|
|
81
|
+
}
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
const veiloPublicKeyRaw = response?.veiloPublicKey;
|
|
85
|
+
const walletRaw = response?.pubK;
|
|
86
|
+
if (!veiloPublicKeyRaw || !walletRaw) {
|
|
87
|
+
throw new errors_js_1.ShieldError("UNKNOWN", `The relayer did not return both keys for this recipient ` +
|
|
88
|
+
`(veiloPublicKey=${!!veiloPublicKeyRaw}, pubK=${!!walletRaw}). ` +
|
|
89
|
+
`Shielding without both would create a note the owner cannot find.`, { context: { response } });
|
|
90
|
+
}
|
|
91
|
+
const noteViewingKey = new web3_js_1.PublicKey(walletRaw);
|
|
92
|
+
if ("publicKey" in target &&
|
|
93
|
+
!noteViewingKey.equals(typeof target.publicKey === "string"
|
|
94
|
+
? new web3_js_1.PublicKey(target.publicKey)
|
|
95
|
+
: target.publicKey)) {
|
|
96
|
+
throw new errors_js_1.ShieldError("UNKNOWN", "The Veilo owner service returned a different wallet than the one requested. " +
|
|
97
|
+
"Shielding was stopped to prevent creating a note for the wrong recipient.", {
|
|
98
|
+
context: {
|
|
99
|
+
requested: typeof target.publicKey === "string"
|
|
100
|
+
? target.publicKey
|
|
101
|
+
: target.publicKey.toBase58(),
|
|
102
|
+
returned: noteViewingKey.toBase58(),
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
veiloPublicKey: BigInt(veiloPublicKeyRaw),
|
|
108
|
+
noteViewingKey,
|
|
109
|
+
// Safe to assert: both halves came from one response about one account.
|
|
110
|
+
acknowledgeOwnerInvariant: true,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Build a {@link ShieldOwner} from keys the caller already holds.
|
|
115
|
+
*
|
|
116
|
+
* Only correct when both keys provably belong to the same account. The explicit
|
|
117
|
+
* flag is not ceremony — it is the last checkpoint before a class of bug whose
|
|
118
|
+
* only symptom is a user's money quietly disappearing from their balance.
|
|
119
|
+
*/
|
|
120
|
+
function unsafeShieldOwner(params) {
|
|
121
|
+
return {
|
|
122
|
+
veiloPublicKey: params.veiloPublicKey,
|
|
123
|
+
noteViewingKey: params.noteViewingKey,
|
|
124
|
+
acknowledgeOwnerInvariant: true,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
|
2
|
+
import type { DepositTarget, ShieldCachePort, TreeSource } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Injection points for the shield path.
|
|
5
|
+
*
|
|
6
|
+
* The three production deposit implementations (extension, wallet-app,
|
|
7
|
+
* veilo-dapp) are ~85% identical and diverge on exactly these axes: where
|
|
8
|
+
* proofs come from, where the tree comes from, which lookup table to use, and
|
|
9
|
+
* what to cache into. Making each an explicit port is what lets one
|
|
10
|
+
* implementation serve all of them — and lets a third-party integrator supply
|
|
11
|
+
* their own without forking anything.
|
|
12
|
+
*
|
|
13
|
+
* Proof generation is deliberately NOT redefined here: `TransactionProofBuilder`
|
|
14
|
+
* from `proof.ts` already has the right shape, and a second prover abstraction
|
|
15
|
+
* would be exactly the kind of duplication this module exists to avoid.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Read the target tree and root directly from chain.
|
|
19
|
+
*
|
|
20
|
+
* Note the account decoding: `MerkleTreeAccount::EXPECTED_SIZE` (9107) does not
|
|
21
|
+
* match the size its declared fields sum to (8987), a 120-byte discrepancy that
|
|
22
|
+
* is unexplained. Until it is, hand-slicing this account at fixed byte offsets
|
|
23
|
+
* is unsafe. Anchor's coder reads declared fields from the front and ignores the
|
|
24
|
+
* tail, so `.fetch()` stays correct regardless.
|
|
25
|
+
*/
|
|
26
|
+
export declare function onChainTreeSource(connection: Connection, programId?: PublicKey): TreeSource;
|
|
27
|
+
/**
|
|
28
|
+
* Fixed values, for tests and offline builds.
|
|
29
|
+
*
|
|
30
|
+
* Also the escape hatch for an integrator who already tracks the tree state and
|
|
31
|
+
* does not want the SDK making RPC calls on their behalf.
|
|
32
|
+
*/
|
|
33
|
+
export declare function staticTreeSource(target: DepositTarget): TreeSource;
|
|
34
|
+
/**
|
|
35
|
+
* In-memory cache with TTL. The default.
|
|
36
|
+
*
|
|
37
|
+
* Persistent storage is deliberately the caller's problem — `localStorage`,
|
|
38
|
+
* `chrome.storage`, and `AsyncStorage` are three different answers in three
|
|
39
|
+
* different apps, and none of them belongs in a published SDK.
|
|
40
|
+
*/
|
|
41
|
+
export declare function memoryCache(): ShieldCachePort;
|
|
42
|
+
/** A cache that never stores anything. Useful for tests that assert fetch counts. */
|
|
43
|
+
export declare function noCache(): ShieldCachePort;
|