Details
export function fastMerkleRoot(
values: Uint8Array[],
digestFn: (b: Uint8Array) => Uint8Array,
mutated?: { value: boolean },
): Uint8Array {
...
while (length > 1) {
let j = 0;
for (let i = 0; i < length; i += 2, ++j) {
const left = results[i];
const right = i + 1 === length ? left : results[i + 1];
if (i + 1 < length && tools.compare(left, right) === 0 && mutated)
mutated.value = true;
results[j] = digestFn(tools.concat([left, right]));
}
length = j;
}
return results[0];
}
Thread the flag through Block.calculateMerkleRoot and have __checkMerkleRoot return false when it is set, so a block containing an adjacent duplicate pair at any level is rejected the way CheckBlock rejects it with bad-txns-duplicate. Rejecting any block that contains a repeated txid gives the same result and is simpler to audit.
Affected versions
- Versions 2.3.0 through 5.2.0.
- Versions 6.0.0 through 6.1.7, including 6.1.0-rc.0.
- Versions 7.0.0-rc.0 through 7.0.1.
The affected block-root verifier was introduced in 2.3.0 as Block.checkMerkleRoot(), alongside Block.calculateMerkleRoot(). Later versions expose block-root validation through Block.checkTxRoots().
Remediation
Upgrade to bitcoinjs-lib 5.2.1, 6.1.8 or 7.0.2, or a later patched release on the corresponding line. Versions below v5 have no in-line patch and require upgrading to a fixed line.
The fix detects adjacent equal hashes at each level before odd-node padding and makes block-root validation reject mutated transaction trees.