From ea496de4d3d52686427d4b77267fae7ee2867a92 Mon Sep 17 00:00:00 2001 From: wangjiling Date: Wed, 14 Nov 2018 15:45:00 +0800 Subject: [PATCH] correct consensusBranchId for sapling --- src/bufferutils.js | 8 ++++++++ src/networks.js | 18 ++++++++++++++++-- src/transaction.js | 2 +- src/transaction_builder.js | 4 ++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/bufferutils.js b/src/bufferutils.js index 179e84e20..9fc25cb80 100644 --- a/src/bufferutils.js +++ b/src/bufferutils.js @@ -19,6 +19,13 @@ function readUInt64LE (buffer, offset) { return b + a } +function readInt64LE (buffer, offset) { + var a = buffer.readUInt32LE(offset) + var b = buffer.readInt32LE(offset + 4) + b *= 0x100000000 + return b + a +} + function writeUInt64LE (buffer, value, offset) { verifuint(value, 0x001fffffffffffff) @@ -48,6 +55,7 @@ module.exports = { readPushDataInt: pushdata.decode, readUInt64LE: readUInt64LE, readVarInt: readVarInt, + readInt64LE: readInt64LE, varIntBuffer: varuint.encode, varIntSize: varuint.encodingLength, writePushDataInt: pushdata.encode, diff --git a/src/networks.js b/src/networks.js index 2c5134eff..e2f5391db 100644 --- a/src/networks.js +++ b/src/networks.js @@ -11,7 +11,15 @@ module.exports = { }, pubKeyHash: 0x1cb8, scriptHash: 0x1cbd, - wif: 0x80 + wif: 0x80, + // This parameter was introduced in version 3 to allow soft forks, for version 1 and 2 transactions we add a + // dummy value. + consensusBranchId: { + 1: 0x00, + 2: 0x00, + 3: 0x5ba81b19, + 4: 0x76b809bb + } }, testnet: { messagePrefix: '\x18Bitcoin Signed Message:\n', @@ -22,7 +30,13 @@ module.exports = { }, pubKeyHash: 0x6f, scriptHash: 0xc4, - wif: 0xef + wif: 0xef, + consensusBranchId: { + 1: 0x00, + 2: 0x00, + 3: 0x5ba81b19, + 4: 0x76b809bb + } }, litecoin: { messagePrefix: '\x19Litecoin Signed Message:\n', diff --git a/src/transaction.js b/src/transaction.js index de456c895..8f208ba0a 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -65,7 +65,7 @@ Transaction.PREVOUTS_HASH_PERSON = new Buffer('ZcashPrevoutHash') Transaction.SEQUENCE_HASH_PERSON = new Buffer('ZcashSequencHash') Transaction.OUTPUTS_HASH_PERSON = new Buffer('ZcashOutputsHash') Transaction.JOINSPLITS_HASH_PERSON = new Buffer('ZcashJSplitsHash') -Transaction.OVERWINTER_HASH_PERSON = Buffer.concat([new Buffer('ZcashSigHash'), Buffer.from('191ba85b', 'hex')]) +Transaction.OVERWINTER_HASH_PERSON = Buffer.concat([new Buffer('ZcashSigHash'), Buffer.from('bb09b876', 'hex')]) // Sapling note magic values, copied from src/zcash/Zcash.h var NOTEENCRYPTION_AUTH_BYTES = 16; diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 3b6e3e954..2613d2649 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -512,6 +512,10 @@ TransactionBuilder.prototype.setLockTime = function (locktime) { TransactionBuilder.prototype.setVersion = function (version) { typeforce(types.UInt32, version) + if (!this.network.consensusBranchId.hasOwnProperty(this.tx.version)) { + throw new Error('Unsupported Zcash transaction') + } + // XXX: this might eventually become more complex depending on what the versions represent this.tx.version = version }