bitcoinjs-lib/ts_src/block.ts

260 lines
7.6 KiB
TypeScript
Raw Permalink Normal View History

2020-01-15 03:25:53 +01:00
import { BufferReader, BufferWriter, reverseBuffer } from './bufferutils';
2019-03-07 03:47:00 +01:00
import * as bcrypto from './crypto';
2019-03-03 15:07:49 +01:00
import { Transaction } from './transaction';
import * as types from './types';
const fastMerkleRoot = require('merkle-lib/fastRoot');
const typeforce = require('typeforce');
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',
);
2018-12-27 09:49:53 +01:00
export class Block {
2019-03-03 15:07:49 +01:00
static fromBuffer(buffer: Buffer): Block {
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)');
2018-12-27 09:49:53 +01:00
2020-01-15 03:25:53 +01:00
const bufferReader = new BufferReader(buffer);
2018-12-27 09:49:53 +01:00
2019-03-03 15:07:49 +01:00
const block = new Block();
2020-01-15 03:25:53 +01:00
block.version = bufferReader.readInt32();
block.prevHash = bufferReader.readSlice(32);
block.merkleRoot = bufferReader.readSlice(32);
block.timestamp = bufferReader.readUInt32();
block.bits = bufferReader.readUInt32();
block.nonce = bufferReader.readUInt32();
2018-12-27 09:49:53 +01:00
2019-03-03 15:07:49 +01:00
if (buffer.length === 80) return block;
2018-12-27 09:49:53 +01:00
const readTransaction = (): any => {
2020-01-15 03:25:53 +01:00
const tx = Transaction.fromBuffer(
bufferReader.buffer.slice(bufferReader.offset),
true,
);
bufferReader.offset += tx.byteLength();
2019-03-03 15:07:49 +01:00
return tx;
};
2018-12-27 09:49:53 +01:00
2020-01-15 03:25:53 +01:00
const nTransactions = bufferReader.readVarInt();
2019-03-03 15:07:49 +01:00
block.transactions = [];
2018-12-27 09:49:53 +01:00
2019-03-07 03:47:00 +01:00
for (let i = 0; i < nTransactions; ++i) {
2019-03-03 15:07:49 +01:00
const tx = readTransaction();
block.transactions.push(tx);
2018-12-27 09:49:53 +01:00
}
2019-03-07 03:47:00 +01:00
const witnessCommit = block.getWitnessCommit();
2018-12-27 09:49:53 +01:00
// This Block contains a witness commit
2019-03-03 15:07:49 +01:00
if (witnessCommit) block.witnessCommit = witnessCommit;
2018-12-27 09:49:53 +01:00
2019-03-03 15:07:49 +01:00
return block;
2014-10-16 06:30:57 +02:00
}
2019-03-03 15:07:49 +01:00
static fromHex(hex: string): Block {
return Block.fromBuffer(Buffer.from(hex, 'hex'));
2014-10-16 06:30:57 +02:00
}
2019-03-03 15:07:49 +01:00
static calculateTarget(bits: number): Buffer {
const exponent = ((bits & 0xff000000) >> 24) - 3;
const mantissa = bits & 0x007fffff;
const target = Buffer.alloc(32, 0);
target.writeUIntBE(mantissa, 29 - exponent, 3);
return target;
2014-10-16 06:30:57 +02:00
}
2019-03-03 15:07:49 +01:00
static calculateMerkleRoot(
2019-03-07 03:47:00 +01:00
transactions: Transaction[],
2019-03-03 15:07:49 +01:00
forWitness?: boolean,
): Buffer {
typeforce([{ getHash: types.Function }], transactions);
if (transactions.length === 0) throw errorMerkleNoTxes;
if (forWitness && !txesHaveWitnessCommit(transactions))
throw errorWitnessNotSegwit;
2019-03-03 15:07:49 +01:00
const hashes = transactions.map(transaction =>
transaction.getHash(forWitness!),
);
2019-03-03 15:07:49 +01:00
const rootHash = fastMerkleRoot(hashes, bcrypto.hash256);
2014-10-16 06:30:57 +02:00
2018-12-27 09:49:53 +01:00
return forWitness
2019-03-03 15:07:49 +01:00
? bcrypto.hash256(
Buffer.concat([rootHash, transactions[0].ins[0].witness[0]]),
)
: rootHash;
2014-10-16 06:30:57 +02:00
}
2019-03-20 07:25:48 +01:00
version: number = 1;
prevHash?: Buffer = undefined;
merkleRoot?: Buffer = undefined;
timestamp: number = 0;
witnessCommit?: Buffer = undefined;
bits: number = 0;
nonce: number = 0;
transactions?: Transaction[] = undefined;
2019-03-07 03:47:00 +01:00
2019-03-03 15:07:49 +01:00
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.
2019-03-07 03:47:00 +01:00
const witnessCommits = this.transactions![0].outs.filter(out =>
2019-03-03 15:07:49 +01:00
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)
2019-03-07 03:47:00 +01:00
const result = witnessCommits[witnessCommits.length - 1];
2019-03-03 15:07:49 +01:00
if (!(result instanceof Buffer && result.length === 32)) return null;
return result;
}
2019-03-03 15:07:49 +01:00
hasWitnessCommit(): boolean {
if (
this.witnessCommit instanceof Buffer &&
this.witnessCommit.length === 32
)
return true;
if (this.getWitnessCommit() !== null) return true;
return false;
}
2019-03-03 15:07:49 +01:00
hasWitness(): boolean {
return anyTxHasWitness(this.transactions!);
2014-10-16 06:30:57 +02:00
}
weight(): number {
const base = this.byteLength(false, false);
const total = this.byteLength(false, true);
return base * 3 + total;
}
byteLength(headersOnly?: boolean, allowWitness: boolean = true): number {
2019-03-03 15:07:49 +01:00
if (headersOnly || !this.transactions) return 80;
2014-10-16 06:30:57 +02:00
2019-03-03 15:07:49 +01:00
return (
80 +
varuint.encodingLength(this.transactions.length) +
this.transactions.reduce((a, x) => a + x.byteLength(allowWitness), 0)
2019-03-03 15:07:49 +01:00
);
2018-12-27 09:49:53 +01:00
}
2014-10-16 06:30:57 +02:00
2019-03-03 15:07:49 +01:00
getHash(): Buffer {
return bcrypto.hash256(this.toBuffer(true));
2018-12-27 09:49:53 +01:00
}
2017-04-18 03:56:35 +02:00
2019-03-03 15:07:49 +01:00
getId(): string {
return reverseBuffer(this.getHash()).toString('hex');
2018-12-27 09:49:53 +01:00
}
2014-10-16 06:30:57 +02:00
2019-03-03 15:07:49 +01:00
getUTCDate(): Date {
const date = new Date(0); // epoch
date.setUTCSeconds(this.timestamp);
2014-10-16 06:30:57 +02:00
2019-03-03 15:07:49 +01:00
return date;
2018-12-27 09:49:53 +01:00
}
2014-10-16 06:30:57 +02:00
2018-12-27 09:49:53 +01:00
// TODO: buffer, offset compatibility
toBuffer(headersOnly?: boolean): Buffer {
2019-03-03 15:07:49 +01:00
const buffer: Buffer = Buffer.allocUnsafe(this.byteLength(headersOnly));
2018-12-27 09:49:53 +01:00
2020-01-15 03:25:53 +01:00
const bufferWriter = new BufferWriter(buffer);
2018-12-27 09:49:53 +01:00
2020-01-15 03:25:53 +01:00
bufferWriter.writeInt32(this.version);
bufferWriter.writeSlice(this.prevHash!);
bufferWriter.writeSlice(this.merkleRoot!);
bufferWriter.writeUInt32(this.timestamp);
bufferWriter.writeUInt32(this.bits);
bufferWriter.writeUInt32(this.nonce);
2018-12-27 09:49:53 +01:00
2019-03-03 15:07:49 +01:00
if (headersOnly || !this.transactions) return buffer;
2018-12-27 09:49:53 +01:00
2020-01-15 03:25:53 +01:00
varuint.encode(this.transactions.length, buffer, bufferWriter.offset);
bufferWriter.offset += varuint.encode.bytes;
2018-12-27 09:49:53 +01:00
this.transactions.forEach(tx => {
2019-03-03 15:07:49 +01:00
const txSize = tx.byteLength(); // TODO: extract from toBuffer?
2020-01-15 03:25:53 +01:00
tx.toBuffer(buffer, bufferWriter.offset);
bufferWriter.offset += txSize;
2019-03-03 15:07:49 +01:00
});
2018-12-27 09:49:53 +01:00
2019-03-03 15:07:49 +01:00
return buffer;
2018-12-27 09:49:53 +01:00
}
toHex(headersOnly?: boolean): string {
2019-03-03 15:07:49 +01:00
return this.toBuffer(headersOnly).toString('hex');
2018-12-27 09:49:53 +01:00
}
2016-05-04 16:42:05 +02:00
2019-03-03 15:07:49 +01:00
checkTxRoots(): boolean {
// If the Block has segwit transactions but no witness commit,
// there's no way it can be valid, so fail the check.
2019-03-07 03:47:00 +01:00
const hasWitnessCommit = this.hasWitnessCommit();
2019-03-03 15:07:49 +01:00
if (!hasWitnessCommit && this.hasWitness()) return false;
return (
this.__checkMerkleRoot() &&
(hasWitnessCommit ? this.__checkWitnessCommit() : true)
2019-03-03 15:07:49 +01:00
);
}
2019-03-07 03:47:00 +01:00
checkProofOfWork(): boolean {
const hash: Buffer = reverseBuffer(this.getHash());
const target = Block.calculateTarget(this.bits);
return hash.compare(target) <= 0;
}
private __checkMerkleRoot(): boolean {
2019-03-03 15:07:49 +01:00
if (!this.transactions) throw errorMerkleNoTxes;
2016-05-04 16:42:05 +02:00
2019-03-03 15:07:49 +01:00
const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions);
return this.merkleRoot!.compare(actualMerkleRoot) === 0;
2018-12-27 09:49:53 +01:00
}
2016-05-04 16:42:05 +02:00
2019-03-07 03:47:00 +01:00
private __checkWitnessCommit(): boolean {
2019-03-03 15:07:49 +01:00
if (!this.transactions) throw errorMerkleNoTxes;
if (!this.hasWitnessCommit()) throw errorWitnessNotSegwit;
2016-05-04 16:42:05 +02:00
2019-03-03 15:07:49 +01:00
const actualWitnessCommit = Block.calculateMerkleRoot(
this.transactions,
true,
);
return this.witnessCommit!.compare(actualWitnessCommit) === 0;
2018-12-27 09:49:53 +01:00
}
2015-12-08 08:51:35 +01:00
}
2019-03-20 07:25:48 +01:00
function txesHaveWitnessCommit(transactions: Transaction[]): boolean {
return (
transactions instanceof Array &&
transactions[0] &&
transactions[0].ins &&
transactions[0].ins instanceof Array &&
transactions[0].ins[0] &&
transactions[0].ins[0].witness &&
transactions[0].ins[0].witness instanceof Array &&
transactions[0].ins[0].witness.length > 0
);
}
function anyTxHasWitness(transactions: 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,
),
)
);
}