Check for segwit block with no witness commit
This commit is contained in:
parent
e52abecee2
commit
4c6ea80459
3 changed files with 100 additions and 39 deletions
ts_src
|
@ -9,11 +9,9 @@ const varuint = require('varuint-bitcoin')
|
|||
|
||||
const errorMerkleNoTxes = new TypeError('Cannot compute merkle root for zero transactions')
|
||||
const errorWitnessNotSegwit = new TypeError('Cannot compute witness commit for non-segwit block')
|
||||
const errorBufferTooSmall = new Error('Buffer too small (< 80 bytes)')
|
||||
|
||||
function txesHaveWitness (transactions: Array<Transaction>): boolean {
|
||||
return transactions !== undefined &&
|
||||
transactions instanceof Array &&
|
||||
function txesHaveWitnessCommit (transactions: Array<Transaction>): boolean {
|
||||
return transactions instanceof Array &&
|
||||
transactions[0] &&
|
||||
transactions[0].ins &&
|
||||
transactions[0].ins instanceof Array &&
|
||||
|
@ -23,6 +21,19 @@ function txesHaveWitness (transactions: Array<Transaction>): boolean {
|
|||
transactions[0].ins[0].witness.length > 0
|
||||
}
|
||||
|
||||
function anyTxHasWitness (transactions: Array<Transaction>): boolean {
|
||||
return transactions instanceof Array &&
|
||||
transactions.some(tx =>
|
||||
typeof tx === 'object' &&
|
||||
tx.ins instanceof Array &&
|
||||
tx.ins.some(input =>
|
||||
typeof input === 'object' &&
|
||||
input.witness instanceof Array &&
|
||||
input.witness.length > 0
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export class Block {
|
||||
version: number
|
||||
prevHash?: Buffer
|
||||
|
@ -45,7 +56,7 @@ export class Block {
|
|||
}
|
||||
|
||||
static fromBuffer (buffer: Buffer): Block {
|
||||
if (buffer.length < 80) throw errorBufferTooSmall
|
||||
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)')
|
||||
|
||||
let offset: number = 0
|
||||
const readSlice = (n: number): Buffer => {
|
||||
|
@ -95,19 +106,9 @@ export class Block {
|
|||
block.transactions.push(tx)
|
||||
}
|
||||
|
||||
let witnessCommit = block.getWitnessCommit()
|
||||
// This Block contains a witness commit
|
||||
if (block.hasWitnessCommit()) {
|
||||
// The merkle root for the witness data is in an OP_RETURN output.
|
||||
// There is no rule for the index of the output, so use filter to find it.
|
||||
// The root is prepended with 0xaa21a9ed so check for 0x6a24aa21a9ed
|
||||
// If multiple commits are found, the output with highest index is assumed.
|
||||
let witnessCommits = block.transactions[0].outs
|
||||
.filter(out => out.script.slice(0, 6).equals(Buffer.from('6a24aa21a9ed', 'hex')))
|
||||
.map(out => out.script.slice(6, 38))
|
||||
|
||||
// Use the commit with the highest output (should only be one though)
|
||||
block.witnessCommit = witnessCommits[witnessCommits.length - 1]
|
||||
}
|
||||
if (witnessCommit) block.witnessCommit = witnessCommit
|
||||
|
||||
return block
|
||||
}
|
||||
|
@ -127,7 +128,7 @@ export class Block {
|
|||
static calculateMerkleRoot (transactions: Array<Transaction>, forWitness?: boolean): Buffer {
|
||||
typeforce([{ getHash: types.Function }], transactions)
|
||||
if (transactions.length === 0) throw errorMerkleNoTxes
|
||||
if (forWitness && !txesHaveWitness(transactions)) throw errorWitnessNotSegwit
|
||||
if (forWitness && !txesHaveWitnessCommit(transactions)) throw errorWitnessNotSegwit
|
||||
|
||||
const hashes = transactions.map(transaction => transaction.getHash(forWitness!))
|
||||
|
||||
|
@ -138,8 +139,33 @@ export class Block {
|
|||
: rootHash
|
||||
}
|
||||
|
||||
getWitnessCommit (): Buffer | null {
|
||||
if (!txesHaveWitnessCommit(this.transactions!)) return null
|
||||
|
||||
// The merkle root for the witness data is in an OP_RETURN output.
|
||||
// There is no rule for the index of the output, so use filter to find it.
|
||||
// The root is prepended with 0xaa21a9ed so check for 0x6a24aa21a9ed
|
||||
// If multiple commits are found, the output with highest index is assumed.
|
||||
let witnessCommits = this.transactions![0].outs
|
||||
.filter(out => out.script.slice(0, 6).equals(Buffer.from('6a24aa21a9ed', 'hex')))
|
||||
.map(out => out.script.slice(6, 38))
|
||||
if (witnessCommits.length === 0) return null
|
||||
// Use the commit with the highest output (should only be one though)
|
||||
let result = witnessCommits[witnessCommits.length - 1]
|
||||
|
||||
if (!(result instanceof Buffer && result.length === 32)) return null
|
||||
return result
|
||||
}
|
||||
|
||||
hasWitnessCommit (): boolean {
|
||||
return txesHaveWitness(this.transactions!)
|
||||
if (this.witnessCommit instanceof Buffer &&
|
||||
this.witnessCommit.length === 32) return true
|
||||
if (this.getWitnessCommit() !== null) return true
|
||||
return false
|
||||
}
|
||||
|
||||
hasWitness (): boolean {
|
||||
return anyTxHasWitness(this.transactions!)
|
||||
}
|
||||
|
||||
byteLength (headersOnly: boolean): number {
|
||||
|
@ -209,8 +235,12 @@ export class Block {
|
|||
}
|
||||
|
||||
checkTxRoots (): boolean {
|
||||
// If the Block has segwit transactions but no witness commit,
|
||||
// there's no way it can be valid, so fail the check.
|
||||
let hasWitnessCommit = this.hasWitnessCommit()
|
||||
if (!hasWitnessCommit && this.hasWitness()) return false
|
||||
return this.__checkMerkleRoot() &&
|
||||
(this.hasWitnessCommit() ? this.__checkWitnessCommit() : true)
|
||||
(hasWitnessCommit ? this.__checkWitnessCommit() : true)
|
||||
}
|
||||
|
||||
checkMerkleRoot (): boolean {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue