import * as t2z from '@d4mr/t2z-wasm';
import { secp256k1 } from '@noble/curves/secp256k1';
async function sendToShielded() {
// Initialize
t2z.init();
// Pre-build proving key (optional, for faster proving later)
if (!t2z.is_proving_key_ready()) {
console.log('Building proving key...');
t2z.prebuild_proving_key();
}
// Prepare input
const input = new t2z.WasmTransparentInput(
publicKeyHex,
prevTxIdLittleEndian,
0,
1_000_000n,
scriptPubkeyHex,
null
);
// Prepare payment
const payment = new t2z.WasmPayment(
'u1recipient...',
800_000n,
null,
null
);
// Create PCZT
let pczt = t2z.propose_transaction(
[input],
[payment],
'u1change...',
'testnet',
3720100
);
// Inspect the PCZT
const info = t2z.inspect_pczt(pczt.to_hex());
console.log('Fee:', info.implied_fee);
// Sign
const sighash = t2z.get_sighash(pczt, 0);
const sig = secp256k1.sign(hexToBytes(sighash), privateKeyBytes);
const derSig = new Uint8Array([...sig.toDERRawBytes(), 0x01]);
pczt = t2z.append_signature(pczt, 0, publicKeyHex, bytesToHex(derSig));
// Prove
pczt = t2z.prove_transaction(pczt);
// Finalize
const txHex = t2z.finalize_and_extract_hex(pczt);
console.log('Transaction:', txHex);
return txHex;
}