2019-03-07 03:47:00 +01:00
|
|
|
import { reverseBuffer } from './bufferutils';
|
|
|
|
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',
|
|
|
|
);
|
|
|
|
|
2019-03-07 03:47:00 +01:00
|
|
|
function txesHaveWitnessCommit(transactions: Transaction[]): boolean {
|
2019-03-03 15:07:49 +01:00
|
|
|
return (
|
|
|
|
transactions instanceof Array &&
|
2018-12-27 09:49:53 +01:00
|
|
|
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
|
2019-03-03 15:07:49 +01:00
|
|
|
);
|
2014-10-16 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 03:47:00 +01:00
|
|
|
function anyTxHasWitness(transactions: Transaction[]): boolean {
|
2019-03-03 15:07:49 +01:00
|
|
|
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,
|
|
|
|
),
|
2019-01-17 09:10:52 +01:00
|
|
|
)
|
2019-03-03 15:07:49 +01:00
|
|
|
);
|
2019-01-17 09:10:52 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
let offset: number = 0;
|
2018-12-27 09:49:53 +01:00
|
|
|
const readSlice = (n: number): Buffer => {
|
2019-03-03 15:07:49 +01:00
|
|
|
offset += n;
|
|
|
|
return buffer.slice(offset - n, offset);
|
|
|
|
};
|
2018-12-27 09:49:53 +01:00
|
|
|
|
|
|
|
const readUInt32 = (): number => {
|
2019-03-03 15:07:49 +01:00
|
|
|
const i = buffer.readUInt32LE(offset);
|
|
|
|
offset += 4;
|
|
|
|
return i;
|
|
|
|
};
|
2018-12-27 09:49:53 +01:00
|
|
|
|
|
|
|
const readInt32 = (): number => {
|
2019-03-03 15:07:49 +01:00
|
|
|
const i = buffer.readInt32LE(offset);
|
|
|
|
offset += 4;
|
|
|
|
return i;
|
|
|
|
};
|
2018-12-27 09:49:53 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
const block = new Block();
|
|
|
|
block.version = readInt32();
|
|
|
|
block.prevHash = readSlice(32);
|
|
|
|
block.merkleRoot = readSlice(32);
|
|
|
|
block.timestamp = readUInt32();
|
|
|
|
block.bits = readUInt32();
|
|
|
|
block.nonce = 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 readVarInt = (): number => {
|
2019-03-03 15:07:49 +01:00
|
|
|
const vi = varuint.decode(buffer, offset);
|
|
|
|
offset += varuint.decode.bytes;
|
|
|
|
return vi;
|
|
|
|
};
|
2018-12-27 09:49:53 +01:00
|
|
|
|
|
|
|
const readTransaction = (): any => {
|
2019-03-03 15:07:49 +01:00
|
|
|
const tx = Transaction.fromBuffer(buffer.slice(offset), true);
|
|
|
|
offset += tx.byteLength();
|
|
|
|
return tx;
|
|
|
|
};
|
2018-12-27 09:49:53 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
const nTransactions = readVarInt();
|
|
|
|
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;
|
2014-11-28 00:30:07 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
const hashes = transactions.map(transaction =>
|
|
|
|
transaction.getHash(forWitness!),
|
|
|
|
);
|
2014-11-28 00:30:07 +01:00
|
|
|
|
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-07 03:47:00 +01:00
|
|
|
version: number;
|
|
|
|
prevHash?: Buffer;
|
|
|
|
merkleRoot?: Buffer;
|
|
|
|
timestamp: number;
|
|
|
|
witnessCommit?: Buffer;
|
|
|
|
bits: number;
|
|
|
|
nonce: number;
|
|
|
|
transactions?: Transaction[];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.version = 1;
|
|
|
|
this.timestamp = 0;
|
|
|
|
this.bits = 0;
|
|
|
|
this.nonce = 0;
|
|
|
|
this.prevHash = undefined;
|
|
|
|
this.merkleRoot = undefined;
|
|
|
|
this.witnessCommit = undefined;
|
|
|
|
this.transactions = undefined;
|
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
getWitnessCommit(): Buffer | null {
|
|
|
|
if (!txesHaveWitnessCommit(this.transactions!)) return null;
|
2019-01-17 09:10:52 +01:00
|
|
|
|
|
|
|
// 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;
|
2019-01-17 09:10:52 +01:00
|
|
|
// 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-01-17 09:10:52 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
if (!(result instanceof Buffer && result.length === 32)) return null;
|
|
|
|
return result;
|
2019-01-17 09:10:52 +01:00
|
|
|
}
|
|
|
|
|
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-01-17 09:10:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
hasWitness(): boolean {
|
|
|
|
return anyTxHasWitness(this.transactions!);
|
2014-10-16 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
byteLength(headersOnly: boolean): number {
|
|
|
|
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) +
|
2018-12-27 09:49:53 +01:00
|
|
|
this.transactions.reduce((a, x) => a + x.byteLength(), 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
|
2019-03-03 15:07:49 +01:00
|
|
|
toBuffer(headersOnly: boolean): Buffer {
|
|
|
|
const buffer: Buffer = Buffer.allocUnsafe(this.byteLength(headersOnly));
|
2018-12-27 09:49:53 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
let offset: number = 0;
|
2018-12-27 09:49:53 +01:00
|
|
|
const writeSlice = (slice: Buffer): void => {
|
2019-03-03 15:07:49 +01:00
|
|
|
slice.copy(buffer, offset);
|
|
|
|
offset += slice.length;
|
|
|
|
};
|
2018-12-27 09:49:53 +01:00
|
|
|
|
|
|
|
const writeInt32 = (i: number): void => {
|
2019-03-03 15:07:49 +01:00
|
|
|
buffer.writeInt32LE(i, offset);
|
|
|
|
offset += 4;
|
|
|
|
};
|
2018-12-27 09:49:53 +01:00
|
|
|
const writeUInt32 = (i: number): void => {
|
2019-03-03 15:07:49 +01:00
|
|
|
buffer.writeUInt32LE(i, offset);
|
|
|
|
offset += 4;
|
|
|
|
};
|
2018-12-27 09:49:53 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
writeInt32(this.version);
|
|
|
|
writeSlice(this.prevHash!);
|
|
|
|
writeSlice(this.merkleRoot!);
|
|
|
|
writeUInt32(this.timestamp);
|
|
|
|
writeUInt32(this.bits);
|
|
|
|
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
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
varuint.encode(this.transactions.length, buffer, offset);
|
|
|
|
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?
|
|
|
|
tx.toBuffer(buffer, offset);
|
|
|
|
offset += txSize;
|
|
|
|
});
|
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
|
|
|
}
|
2015-12-09 03:35:29 +01:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
toHex(headersOnly: boolean): string {
|
|
|
|
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 {
|
2019-01-17 09:10:52 +01:00
|
|
|
// 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() &&
|
2019-01-17 09:10:52 +01:00
|
|
|
(hasWitnessCommit ? this.__checkWitnessCommit() : true)
|
2019-03-03 15:07:49 +01:00
|
|
|
);
|
2019-01-17 09:01:15 +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
|
|
|
}
|