clawchain/cmd/clawd/scripts/roundtrip-unshield.ts
caelum0x e83f24a993 feat(clawd): live-verify privacy shield->unshield round-trip with real ZK proof
Prove the full privacy lifecycle end-to-end through the registry, the hardest
part of the clawd client layer:

- roundtrip-shield.ts: shields a deterministic (amount, uint64 blinding) so the
  off-chain clawproof prover can reproduce the identical commitment. Encodes
  blinding as 32 big-endian bytes = bigInt the keeper derives → same MiMC commit.
- roundtrip-unshield.ts: submits MsgUnshield with a clawproof-generated Groth16
  proof + nullifier + root, withdrawing back to a public address.

Verified live on clawchain-local:
  shield 5000 (blinding 12345) -> code=0, commitment 1ea3acbb… (matches clawproof)
  clawproof unshield-proof -> merkle_root 21d851… MATCHES on-chain root
  MsgUnshield -> code=0 height 1174, proof verified against on-chain VK, funds released
  nullifier 2e1c8dd0… now exists=true; replay rejected 'double-spend' (state_machine.go:54)

Prereq finding (extends Gap A): the unshield circuit's proving key is NOT
distributed with the on-chain VK — only *_vk.bin exist in .local-node/keys.
A verifiable proof required 'clawproof setup' to regenerate a pk+vk pair and
swapping the fresh dev VK onto the node (Groth16 setup is randomized, so the
fresh VK differs from the original). The dev key-gen path must emit pk+vk
together for a reproducible round-trip. Also: clawproof's --blinding is uint64,
while the on-chain msg accepts a 256-bit blinding — keep dev blindings in
uint64 range to reproduce witnesses.
2026-05-31 20:13:14 +03:00

69 lines
2.4 KiB
TypeScript

/**
* Round-trip helper (step 2/2): submit MsgUnshield with a clawproof-generated
* Groth16 proof, withdrawing a previously-shielded commitment back to a public
* address. Proves the privacy unshield path works end-to-end through the registry.
*
* PRIV_HEX=$(cat /tmp/devkey.hex) COMMITMENT=.. NULLIFIER=.. PROOF=.. \
* AMOUNT=5000 ROOT=.. npx tsx scripts/roundtrip-unshield.ts
*/
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";
import { fromHex } from "@cosmjs/encoding";
import { connectClawchainSigningClient } from "../src/lib/signing.js";
const RPC = process.env.RPC_URL ?? "http://localhost:26657";
const PRIV_HEX = (process.env.PRIV_HEX ?? "").trim();
const env = (k: string): string => {
const v = process.env[k];
if (!v) throw new Error(`missing env ${k}`);
return v;
};
async function main(): Promise<void> {
if (PRIV_HEX.length !== 64) throw new Error("PRIV_HEX must be 64 hex chars");
const wallet = await DirectSecp256k1Wallet.fromKey(fromHex(PRIV_HEX), "claw");
const [account] = await wallet.getAccounts();
if (!account) throw new Error("no account");
const recipient = process.env.RECIPIENT ?? account.address;
const client = await connectClawchainSigningClient(RPC, wallet, {
gasPrice: GasPrice.fromString("0.025uclaw"),
});
const before = await client.getBalance(recipient, "uclaw");
const msg = {
typeUrl: "/clawchain.privacy.v1.MsgUnshield",
value: {
creator: account.address,
commitment: env("COMMITMENT"),
nullifier: env("NULLIFIER"),
proof: env("PROOF"),
amount: env("AMOUNT"),
recipient,
root: env("ROOT"),
},
};
console.log(`unshield ${env("AMOUNT")}uclaw -> ${recipient}`);
const res = await client.signAndBroadcast(account.address, [msg], 2.0);
if (res.code !== 0) {
console.error(`unshield FAILED code=${res.code} rawLog=${res.rawLog}`);
client.disconnect();
process.exit(1);
}
const after = await client.getBalance(recipient, "uclaw");
const delta = BigInt(after.amount) - BigInt(before.amount);
console.log(`code=0 height=${res.height} tx=${res.transactionHash}`);
console.log(`recipient uclaw: ${before.amount} -> ${after.amount} (delta ${delta})`);
console.log(`SUCCESS — unshield accepted; proof verified against on-chain VK.`);
client.disconnect();
}
main().catch((e) => {
console.error("ERROR:", e instanceof Error ? e.message : String(e));
process.exit(1);
});