Build
SDK quickstart
batin-sdk is how another project joins the one Batin pool. It is not a second pool, and it is not a hosted wallet. The seed stays in the caller's process. The package is Apache-2.0 and ships compiled JavaScript with type declarations.
npm install batin-sdk
Testnet only (Sei Atlantic-2). The SDK, circuit and pool are not audited.
The live pool is DEPLOYMENT (Atlantic-2, chain 1328, 0xc0a0d2E64cB30aFD4128748A34C767fd5CF1598D, from block 286080094). BatinWallet refuses any other pool unless a local test passes allowOtherPool. A second pool would be a separate anonymity set. Assets are 1n (SEI) and 2n (USDC).
The SDK does not bundle a prover.
snarkjsis GPL-3.0; you pass in a function with the same shape assnarkjs.groth16.fullProve. See Licences. The circuit does not prove note ciphertexts.buildTxruns that check, so build payments throughBatinWalletrather than a witness of your own.
Set up
import * as snarkjs from "snarkjs";
import {
BatinWallet, deriveKeys, generateSeed, publicClient, batinSubmitter,
callArgs, encodeAddress, BATIN_POOL_ABI, DEPLOYMENT, RELAYER_URL,
} from "batin-sdk";
// Download once: https://batin.vercel.app/circuit/joinsplit-e6b020ae70bc.wasm
// https://batin.vercel.app/circuit/joinsplit-a1a6e0bc0bea.zkey
const WASM = "joinsplit.wasm", ZKEY = "joinsplit.zkey";
const prover = (input: Record<string, unknown>) => snarkjs.groth16.fullProve(input, WASM, ZKEY);
const seed = generateSeed(); // 32 random bytes: keep it secret
const wallet = new BatinWallet(deriveKeys(seed), { client: publicClient(), prover });
console.log(encodeAddress(wallet.address)); // batin1…
console.log(RELAYER_URL); // the relayer of this pool
Deposit
A deposit is proved locally and sent by the depositor, who owns the refund. The relayer does not send deposits. The deposit itself is public.
const tx = await wallet.shield(1n, 10n ** 17n); // 0.1 SEI, tokenId 1
const hash = await walletClient.writeContract({
address: DEPLOYMENT.pool, abi: BATIN_POOL_ABI, functionName: "shield",
args: callArgs(tx), value: tx.extAmount, // value only for SEI; approve USDC first
});
The deposit is now pending. On testnet, ask the approval service to admit it:
await fetch("https://batin.vercel.app/api/asp/v1/approve", {
method: "POST", headers: { "content-type": "application/json" },
body: JSON.stringify({ id: depositId.toString(), commitments: [c0, c1] }), // from DepositPending
});
Pay by link and claim
const info = await (await fetch(`${RELAYER_URL}/v1/info`)).json();
const submit = batinSubmitter();
await wallet.sync();
const { url, tx: pay } = await wallet.createPaymentLink(1n, 5n * 10n ** 16n, info.relayer, "https://batin.vercel.app");
await submit(pay); // share `url` through the share sheet
const bob = new BatinWallet(deriveKeys(generateSeed()), { client: publicClient(), prover });
const result = await bob.claim(url, submit, info.relayer);
// { status: "claimed" | "already-claimed" | "not-found" | "wrong-network" }
Withdraw
await bob.sync();
const out = await bob.unshield(1n, 4n * 10n ** 16n, "0xRecipient…", 10n ** 16n, info.relayer);
await submit(out);
Scan from scratch
const restored = new BatinWallet(deriveKeys(seed), { client: publicClient(), prover });
const state = await restored.sync(); // full view-key scan of pool events
state.balances; // { "1": bigint, "2": bigint }
The public RPC caps eth_getLogs at 2,000 blocks, so a full scan walks the chain from the deploy block in chunks. The app keeps an incremental cache; a hosted indexer is an open item.
When a payment arrives
A server that should credit an account does not get a webhook. It scans with the view key, and it credits only after the new note is in the scan.
const before = new Set((await wallet.sync()).notes.map((n) => n.commitment.toString()));
// later, on a timer of your own
const after = await wallet.sync();
const fresh = after.notes.filter((n) => n.kind === "note" && !n.spent && !before.has(n.commitment.toString()));
The sender still knows the commitment they created. Before you treat that money as yours to withdraw, spend it to your own address. That is an ordinary private payment, so it has no fee. The withdrawal fee applies only when the money leaves the pool.
const me = wallet.address;
for (const n of fresh) {
const tx = await wallet.payAddress(n.note.tokenId, n.note.amount, me, info.relayer);
await submit(tx);
}
await wallet.sync();
Credit the account only after that second sync shows the new note. A payment request link is how the payer knows where to send it. See Payment requests.
Source: apps/web/content/docs/sdk-quickstart.md