UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

248 lines 9.05 kB
import MerklePath from './MerklePath.js'; import Transaction from './Transaction.js'; import ChainTracker from './ChainTracker.js'; import BeefTx from './BeefTx.js'; import { Reader, Writer } from '../primitives/utils.js'; export declare const BEEF_V1 = 4022206465; export declare const BEEF_V2 = 4022206466; export declare const ATOMIC_BEEF = 16843009; export declare enum TX_DATA_FORMAT { RAWTX = 0,// rawtx without BUMP RAWTX_AND_BUMP_INDEX = 1,// rawtx with bump index TXID_ONLY = 2 } export declare class Beef { bumps: MerklePath[]; txs: BeefTx[]; version: number; atomicTxid: string | undefined; private txidIndex; private rawBytesCache?; private hexCache?; private needsSort; constructor(version?: number); private invalidateSerializationCaches; private markMutated; private ensureSerializableState; private ensureSortedForSerialization; private getSerializedBytes; /** * @param txid of `beefTx` to find * @returns `BeefTx` in `txs` with `txid`. */ findTxid(txid: string): BeefTx | undefined; private ensureTxidIndex; private deleteFromIndex; private addToIndex; /** * Replaces `BeefTx` for this txid with txidOnly. * * Replacement is done so that a `clone()` can be * updated by this method without affecting the * original. * * @param txid * @returns undefined if txid is unknown. */ makeTxidOnly(txid: string): BeefTx | undefined; /** * @returns `MerklePath` with level zero hash equal to txid or undefined. */ findBump(txid: string): MerklePath | undefined; /** * Finds a Transaction in this `Beef` * and adds any missing input SourceTransactions from this `Beef`. * * The result is suitable for signing. * * @param txid The id of the target transaction. * @returns Transaction with all available input `SourceTransaction`s from this Beef. */ findTransactionForSigning(txid: string): Transaction | undefined; /** * Builds the proof tree rooted at a specific `Transaction`. * * To succeed, the Beef must contain all the required transaction and merkle path data. * * @param txid The id of the target transaction. * @returns Transaction with input `SourceTransaction` and `MerklePath` populated from this Beef. */ findAtomicTransaction(txid: string): Transaction | undefined; /** * Merge a MerklePath that is assumed to be fully valid. * @param bump * @returns index of merged bump */ mergeBump(bump: MerklePath): number; /** * Merge a serialized transaction. * * Checks that a transaction with the same txid hasn't already been merged. * * Replaces existing transaction with same txid. * * @param rawTx * @param bumpIndex Optional. If a number, must be valid index into bumps array. * @returns txid of rawTx */ mergeRawTx(rawTx: number[], bumpIndex?: number): BeefTx; /** * Merge a `Transaction` and any referenced `merklePath` and `sourceTransaction`, recursifely. * * Replaces existing transaction with same txid. * * Attempts to match an existing bump to the new transaction. * * @param tx * @returns txid of tx */ mergeTransaction(tx: Transaction): BeefTx; /** * Removes an existing transaction from the BEEF, given its TXID * @param txid TXID of the transaction to remove */ removeExistingTxid(txid: string): void; mergeTxidOnly(txid: string): BeefTx; mergeBeefTx(btx: BeefTx): BeefTx; mergeBeef(beef: number[] | Beef): void; /** * Sorts `txs` and checks structural validity of beef. * * Does NOT verify merkle roots. * * Validity requirements: * 1. No 'known' txids, unless `allowTxidOnly` is true. * 2. All transactions have bumps or their inputs chain back to bumps (or are known). * 3. Order of transactions satisfies dependencies before dependents. * 4. No transactions with duplicate txids. * * @param allowTxidOnly optional. If true, transaction txid only is assumed valid */ isValid(allowTxidOnly?: boolean): boolean; /** * Sorts `txs` and confirms validity of transaction data contained in beef * by validating structure of this beef and confirming computed merkle roots * using `chainTracker`. * * Validity requirements: * 1. No 'known' txids, unless `allowTxidOnly` is true. * 2. All transactions have bumps or their inputs chain back to bumps (or are known). * 3. Order of transactions satisfies dependencies before dependents. * 4. No transactions with duplicate txids. * * @param chainTracker Used to verify computed merkle path roots for all bump txids. * @param allowTxidOnly optional. If true, transaction txid is assumed valid */ verify(chainTracker: ChainTracker, allowTxidOnly?: boolean): Promise<boolean>; /** * Sorts `txs` and confirms validity of transaction data contained in beef * by validating structure of this beef. * * Returns block heights and merkle root values to be confirmed by a chaintracker. * * Validity requirements: * 1. No 'known' txids, unless `allowTxidOnly` is true. * 2. All transactions have bumps or their inputs chain back to bumps (or are known). * 3. Order of transactions satisfies dependencies before dependents. * 4. No transactions with duplicate txids. * * @param allowTxidOnly optional. If true, transaction txid is assumed valid * @returns {{valid: boolean, roots: Record<number, string>}} * `valid` is true iff this Beef is structuraly valid. * `roots` is a record where keys are block heights and values are the corresponding merkle roots to be validated. */ verifyValid(allowTxidOnly?: boolean): { valid: boolean; roots: Record<number, string>; }; /** * Serializes this data to `writer` * @param writer */ toWriter(writer: Writer): void; /** * Returns a binary array representing the serialized BEEF * @returns A binary array representing the BEEF */ toBinary(): number[]; toUint8Array(): Uint8Array; /** * Serialize this Beef as AtomicBEEF. * * `txid` must exist * * after sorting, if txid is not last txid, creates a clone and removes newer txs * * @param txid * @returns serialized contents of this Beef with AtomicBEEF prefix. */ toBinaryAtomic(txid: string): number[]; /** * Returns a hex string representing the serialized BEEF * @returns A hex string representing the BEEF */ toHex(): string; static fromReader(br: Reader): Beef; /** * Constructs an instance of the Beef class based on the provided binary array * @param bin The binary array from which to construct BEEF * @returns An instance of the Beef class constructed from the binary data */ static fromBinary(bin: number[]): Beef; /** * Constructs an instance of the Beef class based on the provided string * @param s The string value from which to construct BEEF * @param enc The encoding of the string value from which BEEF should be constructed * @returns An instance of the Beef class constructed from the string */ static fromString(s: string, enc?: 'hex' | 'utf8' | 'base64'): Beef; /** * Try to validate newTx.bumpIndex by looking for an existing bump * that proves newTx.txid * * @param newTx A new `BeefTx` that has been added to this.txs * @returns true if a bump was found, false otherwise */ private tryToValidateBumpIndex; /** * Sort the `txs` by input txid dependency order: * - Oldest Tx Anchored by Path or txid only * - Newer Txs depending on Older parents * - Newest Tx * * with proof (MerklePath) last, longest chain of dependencies first * * @returns `{ missingInputs, notValid, valid, withMissingInputs }` */ sortTxs(): { missingInputs: string[]; notValid: string[]; valid: string[]; withMissingInputs: string[]; txidOnly: string[]; }; /** * @returns a shallow copy of this beef */ clone(): Beef; /** * Ensure that all the txids in `knownTxids` are txidOnly * @param knownTxids */ trimKnownTxids(knownTxids: string[]): void; /** * @returns array of transaction txids that either have a proof or whose inputs chain back to a proven transaction. */ getValidTxids(): string[]; /** * @returns Summary of `Beef` contents as multi-line string. */ toLogString(): string; /** * In some circumstances it may be helpful for the BUMP MerklePaths to include * leaves that can be computed from row zero. */ addComputedLeaves(): void; } export default Beef; //# sourceMappingURL=Beef.d.ts.map