clawchain/cmd/clawd/scripts/roundtrip-shield.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

77 lines
2.7 KiB
TypeScript

/**
* Round-trip helper (step 1/2): shield a deterministic amount/blinding so the
* off-chain clawproof prover can reproduce the same commitment + witness.
*
* The on-chain keeper computes commitment = MiMC(amount, bigInt(blindingBytes)).
* We pass blinding as the 32-byte big-endian encoding of a uint64, so it equals
* clawproof's `--blinding <uint>` and both sides derive the identical commitment.
*
* PRIV_HEX=$(cat /tmp/devkey.hex) AMOUNT=5000 BLINDING=12345 \
* npx tsx scripts/roundtrip-shield.ts
*/
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";
import { fromHex, toHex } 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 AMOUNT = BigInt(process.env.AMOUNT ?? "5000");
const BLINDING = BigInt(process.env.BLINDING ?? "12345");
/** Encode a uint64-range bigint as 32 big-endian bytes (matches big.Int.SetBytes). */
function blinding32(v: bigint): Uint8Array {
const out = new Uint8Array(32);
let x = v;
for (let i = 31; i >= 0 && x > 0n; i--) {
out[i] = Number(x & 0xffn);
x >>= 8n;
}
return out;
}
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 client = await connectClawchainSigningClient(RPC, wallet, {
gasPrice: GasPrice.fromString("0.025uclaw"),
});
const blinding = blinding32(BLINDING);
const msg = {
typeUrl: "/clawchain.privacy.v1.MsgShield",
value: {
creator: account.address,
amount: AMOUNT.toString(),
coins: "uclaw",
blinding,
},
};
console.log(`shield amount=${AMOUNT} blinding=${BLINDING} blinding32=0x${toHex(blinding)}`);
const res = await client.signAndBroadcast(account.address, [msg], 2.0);
if (res.code !== 0) {
console.error(`shield FAILED code=${res.code} rawLog=${res.rawLog}`);
client.disconnect();
process.exit(1);
}
let commitment = "";
for (const ev of res.events ?? []) {
if (ev.type === "shield") {
const a = ev.attributes.find((x: { key: string }) => x.key === "commitment");
if (a) commitment = typeof a.value === "string" ? a.value : new TextDecoder().decode(a.value);
}
}
console.log(`code=0 height=${res.height} tx=${res.transactionHash}`);
console.log(`COMMITMENT=${commitment}`);
client.disconnect();
}
main().catch((e) => {
console.error("ERROR:", e instanceof Error ? e.message : String(e));
process.exit(1);
});