UNPKG

@node-dlc/bitcoin

Version:
151 lines (150 loc) 4.9 kB
/// <reference types="node" /> import { StreamReader } from '@node-dlc/bufio'; import { HashValue } from './HashValue'; import { LockTime } from './LockTime'; import { SizeResult } from './SizeResult'; import { TxIn } from './TxIn'; import { TxOut } from './TxOut'; /** * This class is an immutable Bitcoin transaction. This class is used * as a data container from parsed blocks, RPC, or other sources. To use * a mutable transaction, you should use `TxBuilder` class. */ export declare class Tx { /** * Decodes a `Tx` stream similar to Bitcoin Core's DecodeTx method * in that it will first try to parse with SegWit markers enabled. * If there is an error (such as with a base transaction with no * inputs), then it will try parsing using the legacy method. * @param reader */ static decode(reader: StreamReader): Tx; /** * Parses a transaction from its byte format in a stream. Capable of * parsing both legacy and segwit transactions. This method is * similar to Bitcoin Core's `UnserializeTransaction` on the * `Transaction` type. This method is expected to throw if witness * is enabled and we have an ambiguous base transaction (zero inputs). * @param reader */ private static parse; /** * Parses the inputs for a transaction * @param reader * @returns */ private static parseInputs; /** * Parses the outputs for a transaction * @param reader * @returns */ private static parseOutputs; /** * Parses a transaction from a buffer that contains the fully * serialization transaction bytes. * @param buf */ static fromBuffer(buf: Buffer): Tx; /** * Parses a transaction from a hex string containing the fully * serialized transaction bytes. * @param hex */ static fromHex(hex: string): Tx; /** * Get the transaction version. The transaction version corresponds * to features that are enabled for the transaction such as time * locks. */ get version(): number; /** * Gets the transaction identifier. The `txId` for both legacy and * segwit transaction is the hash256 of * `hash256(version||inputs||ouputs||locktime)`. */ get txId(): HashValue; /** * Gets the transaction segwit transaction identifier. For legacy * transaction this is the same as the `txId` property. For segwit * transaction this is the hash256 of * `hash256(version||0x0001||inputs||outputs||witness||locktime)`. * * This is the same value as the `hash` property in bitcoind RPC * results. */ get witnessTxId(): HashValue; /** * Gets the transaction inputs. */ get inputs(): TxIn[]; /** * Gets the transaction outputs */ get outputs(): TxOut[]; /** * Gets the transaction `nLocktime` value that is used to control * absolute timelocks. */ get locktime(): LockTime; get isSegWit(): boolean; get size(): number; get vsize(): number; get weight(): number; private _version; private _txId; private _wtxid; private _inputs; private _outputs; private _locktime; private _sizes; constructor(version?: number, inputs?: TxIn[], outputs?: TxOut[], locktime?: LockTime, sizes?: SizeResult); /** * Serializes legacy or segwit transactions into a Buffer */ serialize(): Buffer; toJSON(): { version: number; inputs: { outpoint: { txid: string; index: number; }; scriptSig: string; sequence: string; }[]; outputs: { value: string; scriptPubKey: string; }[]; locktime: number; }; toHex(pretty?: boolean): string; private _prettyHex; private _serializeLegacy; private _serializeSegWit; /** * Decodes the txId and hash from the Buffer. * * For non-segwit transitions, the hash value is the double-sha256 of * version|vins|vouts|locktime. The txid is the reverse of the hash. * * For segwit transactions, the hash value is returned as the wtxid as * calculated by the double-sha256 of * version|0x00|0x01|inputs|outputs|witness|locktime. The txId is * calculate the same as legacy transactions by performing a double * sha256 hash of the data minus segwit data and markers. */ private _calcTxId; /** * Calculates the size, virtual size, and weight properties from the * based on the current inputs and outputs. * * `size` is the number of raw bytes. * `weight` is the number of witness bytes + the number of non-witness * bytes multiplied by four. * `vsize` is the weight divided by four. */ private _calcSize; private _lazyCalc; }