Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
handle transactions with byte length of 64
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed May 18, 2020
1 parent b61cc92 commit 0b3ee9f
Show file tree
Hide file tree
Showing 5 changed files with 36,903 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/bitcoin-tx.js
Expand Up @@ -33,16 +33,6 @@ async function * _encodeAll (multiformats, deserialized, arg) {
throw new TypeError('deserialized argument must be a Bitcoin block representation')
}

/*
if (deserialized.tx.length === 1 && arg !== BitcoinTransaction.HASH_NO_WITNESS) {
// witness merkle with only a coinbase!
const hash = dblSha2256(NULL_HASH)
const mh = await multiformats.multihash.encode(hash, HASH_ALG)
const cid = new multiformats.CID(1, CODEC_TX_CODE, mh)
yield { cid, binary: Buffer.alloc(64) }
}
*/

const hashes = []
for (let ii = 0; ii < deserialized.tx.length; ii++) {
if (ii === 0 && arg !== BitcoinTransaction.HASH_NO_WITNESS) {
Expand Down Expand Up @@ -84,7 +74,27 @@ function decodeInit (multiformats) {
}
buf = Buffer.from(buf)

if (buf.length === 64) {
// we don't know whether we're dealing with a real transaciton or a binary merkle node,
// even if length==64. So we should _try_ to decode the tx to see if it might be one.
// But, in the witness merkle, the lowest, left-most, non-leaf node contains 32-bytes
// of leading zeros and this makes the bytes decodeable into transaction form
let tx
if (buf.length !== 64 || NULL_HASH.compare(buf, 0, 32) !== 0) {
try {
tx = BitcoinTransaction.decode(buf)
if (buf.length === 64 && tx.version === 0 && tx.vin.length === 0 && tx.vout.length === 0) {
// this is almost certainly not a transaction but a binary merkle node with enough leading
// zeros to fake it
tx = null
}
} catch (err) {
if (buf.length !== 64) {
throw err
}
}
}

if (!tx && buf.length === 64) {
// is some kind of merkle node
let left = buf.slice(0, 32)
const right = buf.slice(32)
Expand All @@ -98,7 +108,6 @@ function decodeInit (multiformats) {
return [leftCid, rightCid]
}

const tx = BitcoinTransaction.decode(buf)
const deserialized = tx.toPorcelain()
if (tx.isCoinbase()) {
const witnessCommitment = tx.getWitnessCommitment()
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures.js
Expand Up @@ -32,6 +32,14 @@ const meta = {
txCid: 'bagyqcvrahor637l2pmjle6whfq7go5upmf74qg6drcffcmr2t64kusy6lzfa',
tx: require('./fixtures/genesis.tx')
},
384931: {
segwit: false,
hash: '00000000000000000a4bffdfd5ff5a60051116c07d30f9a590edb55b5ad03e1f',
cid: 'bagyacvrad47naws3wxwzbjpzgb64afqravqfv76v377uwcqaaaaaaaaaaaaa',
parentCid: 'bagyacvra25jot5wucq7ijjrktm5nhpntj7ra7mu42zmbgdaaaaaaaaaaaaaa',
txCid: 'bagyqcvrarbiwgiwb53gvfvab3h5lxtow5cd2rydzwkotwrg653uhxlycoziq',
tx: require('./fixtures/384931.tx')
},
450002: {
segwit: false,
hash: '0000000000000000017fd226fff84c38c5eccac41910c020692751ffd5b3361d',
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/384931.hex

Large diffs are not rendered by default.

0 comments on commit 0b3ee9f

Please sign in to comment.