Skip to content

Instantly share code, notes, and snippets.

@argjv
Last active October 17, 2020 15:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save argjv/289c80dbe89c43179f6f543ed94283ea to your computer and use it in GitHub Desktop.
Save argjv/289c80dbe89c43179f6f543ed94283ea to your computer and use it in GitHub Desktop.
Using bitgo-utxo-lib to create a multisig Zcash Sapling compatible transaction
const bitGoUTXO = require('bitgo-utxo-lib');
// Choose the configuration for the transaction builder
const zecTestNetwork = bitGoUTXO.networks.zcashTest;
const builder = new bitGoUTXO.TransactionBuilder(zecTestNetwork);
// Required Zcash parameters
builder.setVersion(bitGoUTXO.Transaction.ZCASH_SAPLING_VERSION); // 4
builder.setVersionGroupId(parseInt('0x892F2085', 16));
// Optional parameters
builder.setLockTime(0);
builder.setExpiryHeight(289507);
// Build inputs
const transactionId = '1d7d9686ed5eeb7154c2fe659fd3eeea169057cb7bc996962945225139669b86';
const outputIndex = 1;
builder.addInput(transactionId, outputIndex);
// Build outputs
const destinationAddress = 'tmKBPqa8qqKA7vrGq1AaXHSAr9vqa3GczzK';
const transferValue = 199999000;
builder.addOutput(destinationAddress, transferValue);
// Sign
const redeemScriptHex = '5221021dbb31392fa4857601d5ce2225429923688fede8c2d69e547542cbd88240903a2103c8249e0c474d95e09bb04254d342ef1177f8ca92d2a57356a16df25a4635a5382102fae89068c5c63426f83f0bd5492c4fd757e1f4b575b5d6d05592e8ba519bfd6e53ae';
const redeemScript = Buffer.from(redeemScriptHex, 'hex');
// Print redeemScript just to see how it looks
console.log(bitGoUTXO.script.toASM(redeemScript));
const hashType = bitGoUTXO.Transaction.SIGHASH_ALL;
const inputValueToSign = 200000000;
const outputIndexToSign = 0;
// 1st signature
const testPrivateKey1 = 'cVmRcxsNdhiCigzrBfpv51JtExBPehtMNzUs9CBvymu1B3ch7LLa';
const keyPair1 = bitGoUTXO.ECPair.fromWIF(testPrivateKey1, zecTestNetwork);
builder.sign(outputIndexToSign, keyPair1, redeemScript, hashType, inputValueToSign);
// 2nd signature
const testPrivateKey2 = 'cV2mApzXqoGcGzyoDy5aaiZqQtV5G1HeEuoM1cgpEoiGAeagPeV2';
const keyPair2 = bitGoUTXO.ECPair.fromWIF(testPrivateKey2, zecTestNetwork);
builder.sign(outputIndexToSign, keyPair2, redeemScript, hashType, inputValueToSign);
// Build final transaction
const signedTransaction = builder.build();
// Resulting hex:
// 0400008085202f8901869b6639512245299696c97bcb579016eaeed39f65fec25471eb5eed86967d1d01000000fdfe0000483045022100838ce3e
// c670286ece02b5b93538fed1021dbabc3f98dfd951295e290b79e067b0220405654c743ac3fd38a2e99669608825a9074abf941ff44333a82ef80
// 92e4341f01483045022100c6a6940bc13b2e1fc229a48cb066a894a7a80768fee3a5f93829bad0ba7912fe02200dd541f06b1cde0d251af704d71
// 78afb3e5ae35f26833a1674ff1c3b01d11a74014c695221021dbb31392fa4857601d5ce2225429923688fede8c2d69e547542cbd88240903a2103
// c8249e0c474d95e09bb04254d342ef1177f8ca92d2a57356a16df25a4635a5382102fae89068c5c63426f83f0bd5492c4fd757e1f4b575b5d6d05
// 592e8ba519bfd6e53aeffffffff0118beeb0b000000001976a91467d674a78a010c82c168718ba42a6bbb1e124af088ac00000000000000000000
// 000000000000000000
// You can see the spent transaction here:
// https://explorer.testnet.z.cash/tx/20ec1ae4eb2082499c0014a520c13266b18dd134d65274de4f3adca701c9042f
console.log(signedTransaction.toHex());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment