UNPKG

libnexa-ts

Version:

A pure and powerful Nexa SDK library.

1,573 lines 91.8 kB
import { curve, BNInput } from "elliptic"; import BN from "bn.js"; export class BufferUtils { /** * Tests for both node's Buffer and Uint8Array * * @param arg * @return Returns true if the given argument is an instance of a buffer. */ static isBuffer(arg: unknown): arg is Buffer; /** * Tests for both node's Buffer and Uint8Array * * @param arg * @return Returns true if the given argument is an instance of a hash160 or hash256 buffer. */ static isHashBuffer(arg: unknown): boolean; /** * Reverse a buffer * @param param * @return new reversed buffer */ static reverse(param: Buffer): Buffer; /** * Transforms a buffer into a string with a number in hexa representation * * Shorthand for <tt>buffer.toString('hex')</tt> * * @param buffer * @return string */ static bufferToHex(buffer: Buffer): string; /** * Transforms a number from 0 to 255 into a Buffer of size 1 with that value * * @param integer * @return Buffer */ static integerAsSingleByteBuffer(integer: number): Buffer; /** * Transforms the first byte of an array into a number ranging from -128 to 127 * * @param buffer * @return number */ static integerFromSingleByteBuffer(buffer: Buffer): number; /** * Transform a 4-byte integer into a Buffer of length 4. * * @param integer * @return Buffer */ static integerAsBuffer(integer: number): Buffer; /** * Transform the first 4 values of a Buffer into a number, in little endian encoding * * @param buffer * @return integer */ static integerFromBuffer(buffer: Buffer): number; static getRandomBuffer(size: number): Buffer; } export class Hash { static sha1(buf: Buffer): Buffer; static sha256(buf: Buffer): Buffer; static sha512(buf: Buffer): Buffer; static ripemd160(buf: Buffer): Buffer; static sha256sha256(buf: Buffer): Buffer; static sha256ripemd160(buf: Buffer): Buffer; static sha256hmac(data: Buffer, key: Buffer): Buffer; static sha512hmac(data: Buffer, key: Buffer): Buffer; } export class CommonUtils { /** * Determines whether a string contains only hexadecimal values * * @param value * @returns true if the string is the hexa representation of a number */ static isHexa(value: unknown): boolean; /** * Test if an argument is a valid JSON object. If it is, returns a truthy * value (the json object decoded), so no double JSON.parse call is necessary * * @param arg * @return false if the argument is not a JSON string. */ static isValidJSON(arg: string): object | boolean; static cloneArray<T>(array: T[]): T[]; /** * Checks that a value is a natural number. * * @param value * @return true if a positive integer or zero. */ static isNaturalNumber(value: number): boolean; /** * Checks that a value is a natural number. * * @param value * @return true if a positive integer or zero. */ static isNaturalBigInt(value: bigint): boolean; } export class Point { ecPoint: curve.short.ShortPoint; constructor(point: curve.short.ShortPoint, skipValidation?: boolean); /** * Will return the X coordinate of the Point * * @returns A BN instance of the X coordinate */ getX(): BN; /** * Will return the Y coordinate of the Point * * @returns A BN instance of the Y coordinate */ getY(): BN; add(p: Point): Point; mul(k: BN): Point; mulAdd(k1: BN, p2: Point, k2: BN): Point; eq(p: Point): boolean; /** * Will determine if the point is valid * * @see {@link https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf} * @throws A validation error if exists * @returns An instance of the same Point */ validate(): this; hasSquare(): boolean; /** * Instantiate a valid secp256k1 Point from the X and Y coordinates. * * @param x - The X coordinate * @param y - The Y coordinate * @see {@link https://github.com/indutny/elliptic} * @throws A validation error if exists */ static ecPoint(x: BNInput, y: BNInput, isRed?: boolean): Point; /** * * Instantiate a valid secp256k1 Point from only the X coordinate * * @param odd - If the Y coordinate is odd * @param x - The X coordinate * @throws A validation error if exists */ static ecPointFromX(odd: boolean, x: BNInput): Point; /** * * Will return a secp256k1 ECDSA base point. * * @see {@link https://en.bitcoin.it/wiki/Secp256k1} * @returns An instance of the base point. */ static getG(): Point; /** * * Will return the max of range of valid private keys as governed by the secp256k1 ECDSA standard. * * @see {@link https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys} * @returns A BN instance of the number of points on the curve */ static getN(): BN; static pointToCompressed(point: Point): Buffer; } export class Network { name: string; alias: string; prefix: string; pubkeyhash: number; privatekey: number; scripthash: number; xpubkey: number; xprivkey: number; networkMagic: Buffer; port: number; dnsSeeds: string[]; constructor(params: NetworkParams); toString(): string; } export type EndianType = "big" | "little"; /** * Any type that can be used where a numeric value is needed. */ export type Numeric = number | bigint; /** * Any type that can be used where a big number is needed. */ export type BigNumberish = string | Numeric; /** * Any type that can be used where a network is needed. */ export type Networkish = string | Network; /** * Any type that can be used where a Buffer is needed. */ export type Bufferish = Buffer | Uint8Array; /** * NetworkManager is a singleton service, containing map values that correspond to version * numbers for each nexa network. Currently only supporting "mainnet" * (a.k.a. "livenet") and "testnet", with option to add custom networks. * * @remarks should be used as singletone. * * @see {@linkcode NetworkManager.getInstance} */ export class NetworkManager { get mainnet(): Network; get testnet(): Network; get defaultNetwork(): Network; set defaultNetwork(network: Network); /** * @returns the singleton instance of NetworkManager */ static getInstance(): NetworkManager; get(arg?: Networkish | number, key?: keyof Network): Network | undefined; create(network: NetworkParams): Network; add(network: Network | NetworkParams): void; remove(network: Networkish | NetworkParams): void; } export type PublicKeyVariants = PublicKey | Point | Partial<IPrivateKey> | Partial<IPublicKey> | PublicKeyDto | Buffer | string; /** * Instantiate new PublicKey. * * There are two internal properties, `network` and `compressed`, that deal with importing * a PublicKey from a PrivateKey in WIF format. * * @remarks Better to use {@linkcode PublicKey.from} method to init public key from various formats if the formart unknown. * * @example * ```ts * // export to as a DER hex encoded string * let exported = key.toString(); * * // import the public key * let imported = PublicKey.fromString(exported); * //or * let imported = PublicKey.from(exported); * ``` */ export class PublicKey implements IPublicKey { point: Point; compressed: boolean; network: Network; /** * @param data - The pubkey data */ constructor(data: Partial<IPublicKey>); toObject: () => PublicKeyDto; toDER: () => Buffer; /** * @returns A plain object of the PublicKey */ toJSON(): PublicKeyDto; /** * Will output the PublicKey to a DER Buffer * * @returns A DER hex encoded buffer */ toBuffer(): Buffer; /** * Will output the PublicKey to a DER encoded hex string * * @returns A DER hex encoded string */ toString(): string; /** * Will return a string formatted for the console * * @returns Public key string inspection */ inspect(): string; /** * Instantiate a PublicKey from various formats * * @param data The encoded data in various formats * @param compressed If the public key is compressed * @param network The key network * @returns New PublicKey instance */ static from(data: PublicKeyVariants, compressed?: boolean, network?: Network): PublicKey; static fromDER: typeof PublicKey.fromBuffer; static fromObject: typeof PublicKey.fromJSON; /** * Instantiate a PublicKey from a Buffer * * @param buf - A DER hex buffer * @param strict - if set to false, will loosen some conditions * @param network - the network of the key * @returns A new valid instance of PublicKey */ static fromBuffer(buf: Buffer, strict?: boolean, network?: Network): PublicKey; /** * Instantiate a PublicKey from a Point * * @param point - A Point instance * @param compressed - whether to store this public key as compressed format * @param network - the network of the key * @returns A new valid instance of PublicKey */ static fromPoint(point: Point, compressed?: boolean, network?: Network): PublicKey; /** * Instantiate a PublicKey from a DER hex encoded string * * @param str - A DER hex string * @param encoding - The type of string encoding * @param network - the network of the key * @returns A new valid instance of PublicKey */ static fromString(str: string, encoding?: BufferEncoding, network?: Network): PublicKey; /** * Instantiate a PublicKey from PrivateKey data * * @param data - Object contains data of PrivateKey * @returns A new valid instance of PublicKey */ static fromPrivateKey(data: IPrivateKey): PublicKey; static fromJSON(data: PublicKeyDto): PublicKey; /** * Check if there would be any errors when initializing a PublicKey * * @param data - The encoded data in various formats * @returns An error if exists */ static getValidationError(data: PublicKeyVariants): Error | undefined; /** * Check if the parameters are valid * * @param data - The encoded data in various formats * @returns true If the public key would be valid */ static isValid(data: PublicKeyVariants): boolean; } export enum AddressType { PayToPublicKeyHash = "P2PKH", PayToScriptTemplate = "P2ST", GroupIdAddress = "GROUP" } export class BufferReader { buf: Buffer; pos: number; constructor(buf?: Buffer | string | BufferParams); set(obj: BufferParams): this; eof(): boolean; finished: () => boolean; read(len: number): Buffer; readAll(): Buffer; readUInt8(): number; readUInt16BE(): number; readUInt16LE(): number; readUInt32BE(): number; readUInt32LE(): number; readInt32LE(): number; readUInt64BEBN(): BN; readUInt64LEBN(): BN; readVarintNum(): number; /** * reads a length prepended buffer */ readVarLengthBuffer(): Buffer; readVarintBuf(): Buffer; readVarintBN(): BN; reverse(): this; readReverse(len?: number): Buffer; readCoreVarintNum(): number; } export class BufferWriter { constructor(obj?: BufferWriterOptions); set(obj: BufferWriterOptions): this; toBuffer(): Buffer; concat(): Buffer; write(buf: Buffer): this; writeReverse(buf: Buffer): this; writeUInt8(n: number): this; writeUInt16BE(n: number): this; writeUInt16LE(n: number): this; writeUInt32BE(n: number): this; writeInt32LE(n: number): this; writeUInt32LE(n: number): this; writeUInt64BEBN(bn: BN): this; writeUInt64LEBN(bn: BN): this; writeVarintNum(n: number): this; writeVarintBN(bn: BN): this; writeVarLengthBuf(buf: Buffer): this; writeCoreVarintNum(n: number): this; static varintBufNum(n: number): Buffer; static varintBufBN(bn: BN): Buffer; } export enum Opcode { OP_FALSE = 0, OP_0 = 0, OP_PUSHDATA1 = 76, OP_PUSHDATA2 = 77, OP_PUSHDATA4 = 78, OP_1NEGATE = 79, OP_RESERVED = 80, OP_TRUE = 81, OP_1 = 81, OP_2 = 82, OP_3 = 83, OP_4 = 84, OP_5 = 85, OP_6 = 86, OP_7 = 87, OP_8 = 88, OP_9 = 89, OP_10 = 90, OP_11 = 91, OP_12 = 92, OP_13 = 93, OP_14 = 94, OP_15 = 95, OP_16 = 96, OP_NOP = 97, OP_INVALID_CONTROL1 = 98, OP_IF = 99, OP_NOTIF = 100, OP_JUMP = 101, OP_INVALID_CONTROL2 = 102, OP_ELSE = 103, OP_ENDIF = 104, OP_VERIFY = 105, OP_RETURN = 106, OP_TOALTSTACK = 107, OP_FROMALTSTACK = 108, OP_2DROP = 109, OP_2DUP = 110, OP_3DUP = 111, OP_2OVER = 112, OP_2ROT = 113, OP_2SWAP = 114, OP_IFDUP = 115, OP_DEPTH = 116, OP_DROP = 117, OP_DUP = 118, OP_NIP = 119, OP_OVER = 120, OP_PICK = 121, OP_ROLL = 122, OP_ROT = 123, OP_SWAP = 124, OP_TUCK = 125, OP_CAT = 126, OP_SPLIT = 127, OP_NUM2BIN = 128, OP_BIN2NUM = 129, OP_SIZE = 130, OP_INVERT = 131, OP_AND = 132, OP_OR = 133, OP_XOR = 134, OP_EQUAL = 135, OP_EQUALVERIFY = 136, OP_RESERVED1 = 137, OP_RESERVED2 = 138, OP_1ADD = 139, OP_1SUB = 140, OP_2MUL = 141, OP_2DIV = 142, OP_NEGATE = 143, OP_ABS = 144, OP_NOT = 145, OP_0NOTEQUAL = 146, OP_ADD = 147, OP_SUB = 148, OP_MUL = 149, OP_DIV = 150, OP_MOD = 151, OP_LSHIFT = 152, OP_RSHIFT = 153, OP_BOOLAND = 154, OP_BOOLOR = 155, OP_NUMEQUAL = 156, OP_NUMEQUALVERIFY = 157, OP_NUMNOTEQUAL = 158, OP_LESSTHAN = 159, OP_GREATERTHAN = 160, OP_LESSTHANOREQUAL = 161, OP_GREATERTHANOREQUAL = 162, OP_MIN = 163, OP_MAX = 164, OP_WITHIN = 165, OP_RIPEMD160 = 166, OP_SHA1 = 167, OP_SHA256 = 168, OP_HASH160 = 169, OP_HASH256 = 170, OP_CODESEPARATOR = 171, OP_CHECKSIG = 172, OP_CHECKSIGVERIFY = 173, OP_CHECKMULTISIG = 174, OP_CHECKMULTISIGVERIFY = 175, OP_NOP2 = 177, OP_CHECKLOCKTIMEVERIFY = 177, OP_NOP3 = 178, OP_CHECKSEQUENCEVERIFY = 178, OP_NOP1 = 176, OP_NOP4 = 179, OP_NOP5 = 180, OP_NOP6 = 181, OP_NOP7 = 182, OP_NOP8 = 183, OP_NOP9 = 184, OP_NOP10 = 185, OP_CHECKDATASIG = 186, OP_CHECKDATASIGVERIFY = 187, OP_REVERSEBYTES = 188, OP_INPUTINDEX = 192, OP_ACTIVEBYTECODE = 193, OP_TXVERSION = 194, OP_TXINPUTCOUNT = 195, OP_TXOUTPUTCOUNT = 196, OP_TXLOCKTIME = 197, OP_UTXOVALUE = 198, OP_UTXOBYTECODE = 199, OP_OUTPOINTHASH = 200, OP_INPUTBYTECODE = 202, OP_INPUTSEQUENCENUMBER = 203, OP_OUTPUTVALUE = 204, OP_OUTPUTBYTECODE = 205, OP_INPUTTYPE = 206, OP_OUTPUTTYPE = 207, OP_INPUTVALUE = 208, OP_PARSE = 230, OP_STORE = 231, OP_LOAD = 232, OP_PLACE = 233, OP_PUSH_TX_STATE = 234, OP_SETBMD = 235, OP_BIN2BIGNUM = 236, OP_EXEC = 237, OP_MERKLEROOT = 238, FIRST_UNDEFINED_OP_VALUE = 239, OP_INVALIDOPCODE = 255 } export class ScriptOpcode { num: number; constructor(val: number | string); static fromBuffer(buf: Buffer): ScriptOpcode; static fromNumber(num: number): ScriptOpcode; static fromString(str: string): ScriptOpcode; static smallInt(n: number): ScriptOpcode; /** * @returns true if opcode is one of OP_0, OP_1, ..., OP_16 */ static isSmallIntOp(opcode: ScriptOpcode | number): boolean; toHex(): string; toBuffer(): Buffer; toNumber(): number; toString(): string; /** * Will return a string formatted for the console * * @returns Script opcode */ inspect(): string; /** * Comes from nexad's script DecodeOP_N function * @param opcode * @returns numeric value in range of 0 to 16 */ static decodeOP_N(opcode: number): number; } export type ScriptElement = string | number | bigint | boolean | Buffer | ScriptOpcode | ScriptChunk | Script; /** * A nexa transaction script. Each transaction's inputs and outputs * has a script that is evaluated to validate it's spending. * * @see {@link https://spec.nexa.org/script/1script/} */ export class Script implements IScript { chunks: ScriptChunk[]; constructor(from?: Buffer | string | IScript); set(obj: IScript): this; /** * @returns a new empty script */ static empty(): Script; static fromBuffer(buffer: Buffer): Script; toBuffer(): Buffer; static fromHex(str: string): Script; static fromString(str: string): Script; static fromASM(str: string): Script; toASM(): string; toString(): string; toHex(): string; inspect(): string; /** * Adds a script element to the end of the script. * * @param param a script element to add * @returns this script instance * */ add(param: ScriptElement): this; append: (param: ScriptElement) => this; /** * Adds a script element at the start of the script. * @param param a script element to add * @returns this script instance */ prepend(param: ScriptElement): this; /** * Compares a script with another script */ equals(script: Script): boolean; /** * Analogous to nexad's FindAndDelete. Find and delete equivalent chunks, * typically used with push data chunks. Note that this will find and delete * not just the same data, but the same data with the same push data op as * produced by default. i.e., if a pushdata in a tx does not use the minimal * pushdata op, then when you try to remove the data it is pushing, it will not * be removed, because they do not use the same pushdata op. */ findAndDelete(script: Script): this; /** * Replace the chunk at the given index with the chunks of `replacement`. * * @param index Zero-based index of the chunk to replace. * @param replacement Script whose chunks will be spliced in at `index`. * * @returns `true` if a replacement was made, otherwise `false`. */ replaceChunk(index: number, replacement: Script): boolean; /** * Comes from nexad's script interpreter CheckMinimalPush function * @returns true if the chunk {i} is the smallest way to push that particular data. */ checkMinimalPush(i: number): boolean; /** * Comes from bitcoind's script GetSigOpCount(boolean) function * @param accurate default true * @returns number of signature operations required by this script */ getSignatureOperationsCount(accurate?: boolean): number; /** * @returns true if the script is only composed of data pushing * opcodes or small int opcodes (OP_0, OP_1, ..., OP_16) * * @remarks In the case of an OP_RETURN, it will return true if previous chunks are push only. * Anything beyond the op return is not executed. */ isPushOnly(): boolean; /** * Find the index of the first placeholder chunk. * * A placeholder is defined as: * - pushdata present (buf exists) * - length between 64 and 68 bytes * - first 64 bytes are all zero * - The optional trailing 0–4 bytes (positions 65–68) may be any value, ususally the sighash type bytes. * * @returns The zero-based index of the first placeholder, or -1 if not found. */ findPlaceholder(): number; /** * @returns `true` if the script is empty (has no chunks). */ isEmpty(): boolean; /** * @returns true if this is a pay to script template output script * @remarks for well-known-1 template use {@link isPublicKeyTemplateOut} */ isScriptTemplateOut(): boolean; /** * Checks if this script is a valid pay to script template input script * * @returns true if this is a pay to script template form input script * @remarks for well-known-1 template use {@link isPublicKeyTemplateIn} */ isScriptTemplateIn(): boolean; /** * @returns true if this is a pay to pubkey template output script (well-known-1, p2pkt) */ isPublicKeyTemplateOut(): boolean; /** * @returns true if this is a pay to public key template input script */ isPublicKeyTemplateIn(): boolean; /** * @returns true if this is a pay to pubkey hash output script */ isPublicKeyHashOut(): boolean; /** * @returns {boolean} if this is a pay to public key hash input script */ isPublicKeyHashIn(): boolean; /** * @returns true if this is a valid standard OP_RETURN output */ isDataOut(): boolean; /** * @returns true if this is a valid Token Description OP_RETURN output */ isTokenDescriptionOut(): boolean; /** * Will retrieve the Public Key buffer from p2pkt/p2pkh input scriptSig */ getPublicKey(): Buffer; /** * Will retrieve the Public Key Hash buffer from p2pkh output scriptPubKey */ getPublicKeyHash(): Buffer; /** * Will retrieve the Template Hash from p2pkt/p2st output scriptPubKey * * @returns OP_1 if its p2pkt, otherwise the template hash buffer */ getTemplateHash(): Buffer | Opcode.OP_1; /** * Will retrieve the Constraint Hash from p2pkt/p2st output scriptPubKey * * @returns The constraint hash buffer, or OP_FALSE if not included */ getConstraintHash(): Buffer | Opcode.OP_FALSE; /** * Will retrieve the visible Args from p2st output scriptPubKey * * @returns A Script object containing the visible args */ getVisibleArgs(): Script; /** * Will retrieve the Group Identifier number from Token Description OP_RETURN output * * @remarks This method doesn't check if the group id number is fit to NRC1/NRC2 etc. */ getGroupIdType(): number; /** * Will retrieve the Group Data from p2pkt/p2st output scriptPubKey * * @returns An object with groupId and groupAmount buffers, or undefined if no group data is present */ getGroupData(): { groupId: Buffer; groupAmount: Buffer; } | undefined; } export class Address { readonly data: Buffer; readonly network: Network; readonly type: AddressType; /** * Instantiate an address from an address String or Buffer, a public key or script hash Buffer, * or an instance of {@link PublicKey} or {@link Script}. * * This is an immutable class, and if the first parameter provided to this constructor is an * `Address` instance, the same argument will be returned. * * An address has two key properties: `network` and `type`. The type is either * `AddressType.PayToPublicKeyHash` (value is the `'P2PKH'` string) * or `AddressType.PayToScriptTemplate` (the string `'P2ST'`). The network is an instance of {@link Network} or network name. * You can quickly check whether an address is of a given kind by using the methods * `isPayToPublicKeyHash` and `isPayToScriptTemplate` * * @example * ```javascript * // validate that an input field is valid * let error = Address.getValidationError(input, 'testnet'); * if (!error) { * let address = new Address(input, 'testnet'); * } else { * // invalid network or checksum (typo?) * let message = error.messsage; * } * * // get an address from a public key * let address = Address.fromPublicKey(publicKey, 'testnet').toString(); * ``` * * @param data The encoded data in various formats * @param network The network: 'mainnet' (default) or 'testnet' * @param type The type of address: 'P2ST' (default) or 'P2PKH' or 'GROUP' * @returns A new valid and frozen instance of an Address */ constructor(data: Address | string | Buffer, network?: Networkish, type?: AddressType); static validateParams(network?: Networkish, type?: AddressType): void; /** * @param address string * * @returns A new valid and frozen instance of an Address */ static fromString(address: string): Address; /** * Will return a cashaddr representation of the address. Always return lower case * Can be converted by the caller to uppercase is needed (still valid). * * @returns Nexa address */ toString(): string; /** * Will return a string formatted for the console * * @returns {string} Bitcoin address */ inspect(): string; /** * Instantiate an address from an Object * * @param obj - A JSON object with keys: data, network and type * @returns A new valid instance of an Address */ static fromObject(obj: AddressDto): Address; /** * @returns A plain object with the address information */ toJSON(): AddressDto; toObject: () => AddressDto; /** * @return true if an address is of pay to public key hash type */ isPayToPublicKeyHash(): boolean; /** * @return true if an address is of pay to script template type */ isPayToScriptTemplate(): boolean; /** * @return true if an address is a group token address */ isGroupIdentifierAddress(): boolean; /** * Will return a validation error if exists * * @example * ```javascript * // a network mismatch error * let error = Address.getValidationError('nexatest:nqtsq5g567x44x5g54t2wsxz60zwqmyks63rkrddl4stwnzu', 'testnet'); * ``` * * @param data The encoded data * @param network either a Network instance, 'mainnet', or 'testnet' * @param type The type of address: 'P2ST' or 'GROUP' or 'P2PKH' * @returns The corresponding error message */ static getValidationError(data: string | Buffer, network?: Networkish, type?: AddressType): Error | undefined; /** * Will return a boolean if an address is valid * * @example * ```javascript * assert(Address.isValid('nexa:nqtsq5g567x44x5g54t2wsxz60zwqmyks63rkrddsfq94pd2', 'mainned')); * ``` * * @param data The encoded data * @param network either a Network instance, 'mainnet', or 'testnet' * @param type The type of address: 'P2ST' or 'GROUP' or 'P2PKH' * @returns true if valid */ static isValid(data: string | Buffer, network?: Networkish, type?: AddressType): boolean; /** * Instantiate an address from a PublicKey instance * * @param pubkey the public key instance * @param network either a Network instance, 'mainnet' or 'testnet' * @param type address encoding type * @returns A new valid and frozen instance of an Address */ static fromPublicKey(pubkey: PublicKey, network?: Networkish, type?: AddressType): Address; /** * Instantiate an address from a non grouped script template * * @param templateHash An instance of a template hash Buffer * @param constraintHash An instance of a constraint hash Buffer * @param visibleArgs An array of push-only args, or hex string represent script buffer, or Script with push args * @param network either a Network instance, 'mainnet' or 'testnet' * @returns A new valid and frozen instance of an Address */ static fromScriptTemplate(templateHash: Buffer | Opcode, constraintHash: Buffer | Opcode, visibleArgs?: string | Script | ScriptElement[], network?: Networkish): Address; /** * Will return the transaction output type by the address type * * @param address as string * @returns 1 - Template, 0 - otherwise */ static getOutputType(address: string): number; /** * Will return the transaction output type by the address type * * @returns 1 - Template, 0 - otherwise */ getOutputType(): number; } export interface UTXO { outpoint: string; satoshis?: bigint | string | number; amount?: string | number; scriptPubKey?: string | Script; address?: string | Address; groupId?: string | Address; groupAmount?: bigint; templateData?: ScriptTemplateObject; } export interface IOutput { type?: number; scriptPubKey: Script | string; value: bigint | number | string; address?: string; groupId?: string; groupAmount?: string; } export interface IInput { type?: number; outpoint: string | Buffer; scriptSig: string | Script; amount: string | bigint; sequenceNumber?: number; output?: IOutput; templateData?: ScriptTemplateObject; } export interface ScriptTemplateObject { templateScript: Script | string; constraintScript?: Script | string; publicKey?: PublicKey | string; } export interface ITransaction { id: string; idem: string; version: number; inputs: IInput[]; outputs: IOutput[]; nLockTime: number; fee?: number; feePerByte?: number; changeIndex?: number; changeScript?: string; } /** * Contain a set of flags to skip certain tests: * * * `disableAll`: disable all checks * * `disableIsFullySigned`: disable checking if all inputs are fully signed * * `disableDustOutputs`: disable checking if there are no outputs that are dust amounts * * `disableMoreOutputThanInput`: disable checking if the transaction spends more nexas than the sum of the input amounts */ export interface TxVerifyOptions { disableAll?: boolean; disableDustOutputs?: boolean; disableIsFullySigned?: boolean; disableMoreOutputThanInput?: boolean; } export interface BufferParams { buf?: Buffer; pos?: number; } export interface BufferWriterOptions { bufs?: Buffer[]; } export interface NetworkParams { name: string; alias: string; prefix: string; pubkeyhash: number; privatekey: number; scripthash: number; xpubkey: number; xprivkey: number; networkMagic: number; port: number; dnsSeeds: string[]; } export interface BNOptions { endian?: "little" | "big"; size?: number; bignum?: boolean; } export interface GroupIdData { hashBuffer: Buffer; nonce: bigint; } export interface ISignature { r: BN; s: BN; i?: number; compressed?: boolean; toBuffer(isSchnorr: boolean): Buffer; } export interface IDigitalSignature { hashbuf: Buffer; endian?: "big" | "little"; privkey: IPrivateKey; pubkey: IPublicKey; sig?: ISignature; verified?: boolean; sign(): this; verify(): this; } export interface IPrivateKey { compressed: boolean; network: Network; bn: BN; get publicKey(): IPublicKey; toString(): string; toWIF(): string; toBigNumber(): BN; toBuffer(): Buffer; toBufferNoPadding(): Buffer; toPublicKey(): IPublicKey; toJSON(): PrivateKeyDto; toObject(): PrivateKeyDto; inspect(): string; } export interface IPublicKey { point: Point; compressed?: boolean; network?: Network; toString(): string; toJSON(): PublicKeyDto; toObject(): PublicKeyDto; toDER(): Buffer; toBuffer(): Buffer; inspect(): string; } export interface PrivateKeyDto { compressed: boolean; network: string; bn: string; } export interface PublicKeyDto { x: string; y: string; compressed: boolean; network: string; } interface IHDKey { network: Network; depth: number; parentFingerPrint: Buffer; fingerPrint: Buffer; chainCode: Buffer; childIndex: number; checksum: Buffer; } export interface IHDPrivateKey extends IHDKey { privateKey: IPrivateKey; publicKey?: IPublicKey; xprivkey: string; } export interface IHDPublicKey extends IHDKey { publicKey: IPublicKey; xpubkey: string; } interface HDKeyBuffers { version: Buffer; depth: Buffer; parentFingerPrint: Buffer; childIndex: Buffer; chainCode: Buffer; checksum?: Buffer; } export interface HDPublicKeyBuffers extends HDKeyBuffers { publicKey: Buffer; } export interface HDPrivateKeyBuffers extends HDKeyBuffers { privateKey: Buffer; } interface HDKeyDto { network: string; depth: number; fingerPrint: number; parentFingerPrint: number; childIndex: number; chainCode: string; checksum: number; } export interface HDPrivateKeyDto extends HDKeyDto { privateKey: string; xprivkey: string; } export interface HDPublicKeyDto extends HDKeyDto { publicKey: string; xpubkey: string; } export type HDPrivateKeyMinimalDto = Omit<HDPrivateKeyDto, 'xprivkey' | 'checksum' | 'fingerPrint'>; export type HDPublicKeyMinimalDto = Omit<HDPublicKeyDto, 'xpubkey' | 'checksum' | 'fingerPrint'>; export interface ScriptChunk { buf?: Buffer; len?: number; opcodenum: number; } export interface IScript { chunks: ScriptChunk[]; } export interface AddressDto { data: string; network: string; type: string; } export interface IMessage { message: string; } export interface IBlockHeader { hash?: string; prevHash: string | Buffer; bits: number; ancestorHash: string | Buffer; merkleRoot: string | Buffer; txFilter: string | Buffer; time: number; height: number; chainWork: string | Buffer; size: number; txCount: number; poolFee: number; utxoCommitment: string | Buffer; minerData: string | Buffer; nonce: string | Buffer; } export interface IBlock { header: IBlockHeader; transactions: ITransaction[]; } export class BNExtended extends BN { static Zero: BNExtended; static One: BNExtended; static Minus1: BNExtended; static fromNumber(num: number): BNExtended; static fromBigInt(num: bigint): BNExtended; static fromString(str: string, base?: number | "hex"): BNExtended; static fromBuffer(buf: Buffer, opts?: BNOptions): BNExtended; /** * Create a BN from a "ScriptNum": * This is analogous to the constructor for CScriptNum in nexad. Many ops in * nexad's script interpreter use CScriptNum, which is not really a proper * bignum. Instead, an error is thrown if trying to input a number bigger than * 4 bytes. We copy that behavior here. A third argument, `size`, is provided to * extend the hard limit of 4 bytes, as some usages require more than 4 bytes. */ static fromScriptNumBuffer(buf: Buffer, fRequireMinimal?: boolean, size?: number): BNExtended; add(b: BN): BNExtended; sub(b: BN): BNExtended; mul(b: BN): BNExtended; mod(b: BN): BNExtended; umod(b: BN): BNExtended; toNumber(): number; toBigInt(): bigint; toBuffer(opts?: BN.Endianness | BNOptions, length?: number): Buffer; /** * The corollary to the above, with the notable exception that we do not throw * an error if the output is larger than four bytes. (Which can happen if * performing a numerical operation that results in an overflow to more than 4 * bytes). */ toScriptNumBuffer(): Buffer; toScriptBigNumBuffer(): Buffer; getSize(): number; safeAdd(bigNumToAdd: BNExtended, maxSize: number): BNExtended; safeSub(bigNumToSubtract: BNExtended, maxSize: number): BNExtended; safeMul(bigNumToMultiply: BNExtended, maxSize: number): BNExtended; } export enum UnitType { MEX = 8, KEX = 5, NEXA = 2 } /** * Utility for handling and converting nexa units. * You can consult for different representation of a unit using it's * {format} method or the fixed unit methods like {parse}. * * @example * ```ts * let nex = UnitUtils.formatNEXA(546); // "5.46" * let sats = UnitUtils.parseNEXA("5.46"); // 546n * let mex = UnitUtils.formatUnits(100000000, UnitType.MEX) // "1.00000000"; * let units = UnitUtils.parseUnits('1.0', 5); // 100000n * ``` */ export class UnitUtils { /** * Converts `value` into a decimal string, assuming `unit` decimal * places. The `unit` may be the number of decimal places or the enum of * a unit (e.g. ``UnitType.MEX`` for 8 decimal places). * */ static formatUnits(value: BigNumberish, unit?: UnitType | number): string; /** * Converts the decimal string `value` to a BigInt, assuming * `unit` decimal places. The `unit` may the number of decimal places * or the name of a unit (e.g. ``UnitType.KEX`` for 5 decimal places). */ static parseUnits(value: string, unit?: UnitType | number): bigint; /** * Converts `value` into a decimal string using 2 decimal places. */ static formatNEXA(sats: BigNumberish): string; /** * Converts the decimal string `NEXA` to a BigInt, using 2 decimal places. */ static parseNEXA(nexa: string): bigint; } export class Signature implements ISignature { r: BN; s: BN; i?: number; compressed?: boolean; constructor(params: Partial<ISignature>); toBuffer(isSchnorr?: boolean): Buffer; toTxFormat(sighashBuf?: Buffer): Buffer; toString(): string; /** * Schnorr signatures are 64 bytes: r [len] 32 || s [len] 32. * * There can be a few more bytes that is the sighashtype. It needs to be trimmed before calling this. */ static fromBuffer(buf: Buffer, strict?: boolean): Signature; /** * The format used in a tx. * schnorr is 64 bytes, the rest are sighashtype bytes * * @param buf */ static fromTxFormat(buf: Buffer): Signature; /** * This assumes the str is a raw signature and does not have sighashtype. * Use {@link Signature.fromTxString} when decoding a tx * * @param str the signature hex string * @see fromTxString */ static fromString(str: string): Signature; /** * This assumes the str might have sighashtype bytes and will trim it if needed. * Use this when decoding a tx signature string * * @param str the tx signature hex string */ static fromTxString(str: string, encoding?: BufferEncoding): Signature; /** * For ECDSA. In order to mimic the non-strict DER encoding of OpenSSL, set strict = false. */ static parseDER(buf: Buffer, strict?: boolean): Record<string, unknown>; /** * ECDSA format. used for sign messages */ toCompact(i?: number, compressed?: boolean): Buffer; static fromCompact(buf: Buffer): Signature; } export abstract class DigitalSignature implements IDigitalSignature { hashbuf: Buffer; endian?: EndianType; privkey: IPrivateKey; pubkey: IPublicKey; sig?: ISignature; verified?: boolean; constructor(obj?: Partial<IDigitalSignature>); protected set(obj: Partial<IDigitalSignature>): this; protected abstract _findSignature(d: BN, e: BN): Partial<ISignature>; abstract sigError(): boolean | string; sign(): this; verify(): this; toPublicKey(): IPublicKey; privkey2pubkey(): void; } /** * IMPORTANT: ECDSA only used for compact message signing. * for transactions signing, use Schnorr. */ export class ECDSA extends DigitalSignature { k?: BN; set(obj: Partial<ECDSA>): this; sigError(): boolean | string; protected _findSignature(d: BN, e: BN): Partial<ISignature>; calcI(): this; randomK(): this; deterministicK(badrs?: number): this; signRandomK(): this; toString(): string; toPublicKey(): PublicKey; static fromString(str: string): ECDSA; static sign(hashbuf: Buffer, privkey: IPrivateKey, endian?: EndianType): ISignature; static verify(hashbuf: Buffer, sig: ISignature, pubkey: IPublicKey, endian?: EndianType): boolean; } export class Schnorr extends DigitalSignature { sigError(): boolean | string; /** * RFC6979 deterministic nonce generation used from https://reviews.bitcoinabc.org/D2501 * * @param privkeybuf * @param msgbuf * @return BN nonce */ nonceFunctionRFC6979(privkeybuf: Buffer, msgbuf: Buffer): BN; /** * @remarks * Important references for schnorr implementation {@link https://spec.nexa.org/forks/2019-05-15-schnorr/} * * @param d the private key * @param e the message to be signed */ protected _findSignature(d: BN, e: BN): Partial<ISignature>; static sign(hashbuf: Buffer, privkey: IPrivateKey, endian: EndianType): ISignature; static verify(hashbuf: Buffer, sig: ISignature, pubkey: IPublicKey, endian: EndianType): boolean; } export type PrivateKeyVariants = BN | string | Bufferish | PrivateKey | IPrivateKey | PrivateKeyDto | null; export class PrivateKey implements IPrivateKey { bn: BN; compressed: boolean; network: Network; /** * Instantiate a PrivateKey. * * @param data The private key data * * @remarks Better to use {@linkcode PrivateKey.from} method to init private key from various formats if the formart unknown. * * @example * ```ts * // generate a new random key * let key = new PrivateKey(); * * // encode into wallet import format * let exported = key.toWIF(); * * // instantiate from the exported (and saved) private key * let imported = PrivateKey.fromWIF(exported); * ``` */ constructor(data?: Partial<IPrivateKey>); get publicKey(): PublicKey; /** * Will return an address for the private key with its defined network * * @param type - optional parameter specifying the desired type of the address. * default {@link AddressType.PayToScriptTemplate} * * @returns An address generated from the private key */ toAddress(type?: AddressType): Address; /** * Will output the PrivateKey encoded as hex string */ toString(): string; /** * Will encode the PrivateKey to a WIF string * * @returns A WIF representation of the private key */ toWIF(): string; /** * Will return the private key as a BN instance * * @returns A BN instance of the private key */ toBigNumber(): BN; /** * Will return the private key as a BN buffer * * @returns A buffer of the private key */ toBuffer(): Buffer; /** * Will return the private key as a BN buffer without leading zero padding * * @returns A buffer of the private key */ toBufferNoPadding(): Buffer; /** * Will return the corresponding public key * * @returns A public key generated from the private key */ toPublicKey(): PublicKey; toObject: () => PrivateKeyDto; /** * @returns A plain object representation */ toJSON(): PrivateKeyDto; /** * Will return a string formatted for the console * * @returns Private key details */ inspect(): string; /** * Instantiate a PrivateKey from a Buffer with the DER or WIF representation */ static fromBuffer(buf: Buffer, network?: Networkish): PrivateKey; static fromString: typeof PrivateKey.fromWIF; /** * Instantiate a PrivateKey from a WIF string * * @param str - The WIF encoded private key string * @returns A new valid instance of PrivateKey */ static fromWIF(str: string, network?: Networkish): PrivateKey; static fromObject: typeof PrivateKey.fromJSON; /** * Instantiate a PrivateKey from a plain JavaScript object * * @param obj - The output from privateKey.toObject() */ static fromJSON(obj: PrivateKeyDto): PrivateKey; /** * Instantiate a PrivateKey from random bytes * * @param network - Either "mainnet" or "testnet" * @returns A new valid instance of PrivateKey */ static fromRandom(network?: Networkish): PrivateKey; /** * Check if there would be any errors when initializing a PrivateKey * * @param data - The encoded data in various formats * @param network - Either "mainnet" or "testnet" * @returns An error if exists */ static getValidationError(data: PrivateKeyVariants, network?: Networkish): Error | undefined; /** * Check if the parameters are valid * * @param data - The encoded data in various formats * @param network - Either "mainnet" or "testnet" * @returns true If the private key would be valid */ static isValid(data?: PrivateKeyVariants, network?: Networkish): boolean; /** * Helper to instantiate PrivateKey from different kinds of arguments. */ static from(data?: PrivateKeyVariants, network?: Networkish): PrivateKey; } export class HDPublicKey implements IHDPublicKey { readonly publicKey: PublicKey; readonly network: Network; readonly depth: number; readonly parentFingerPrint: Buffer; readonly fingerPrint: Buffer; readonly chainCode: Buffer; readonly childIndex: number; readonly checksum: Buffer; readonly xpubkey: string; /** * The representation of an hierarchically derived public key. * * See https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki * * @param arg */ constructor(arg: string | Buffer | HDPublicKeyDto | HDPublicKeyMinimalDto | IHDPublicKey | IHDPrivateKey); /** * Verifies that a given path is valid. * * @param arg * @return {boolean} */ static isValidPath(arg: string | number): boolean; /** * Create a HDPublicKey from a buffer argument * * @param buf */ static fromBuffer(buf: Buffer): HDPublicKey; /** * Return a buffer representation of the xpubkey */ toBuffer(): Buffer; static fromString(xpubkey: string): HDPublicKey; /** * Returns the base58 checked representation of the public key * @return a string starting with "xpub..." in livenet */ toString(): string; /** * Returns the console representation of this extended public key. */ inspect(): string; static fromObject(arg: HDPublicKeyDto): HDPublicKey; static fromMinimalObject(arg: HDPublicKeyMinimalDto): HDPublicKey; toJSON: () => HDPublicKeyDto; /** * Returns a plain JavaScript object with information to reconstruct a key. */ toObject(): HDPublicKeyDto; /** * Will return an address for the hdpubkey with its defined network * * @param type - optional parameter specifying the desired type of the address. * default {@link AddressType.PayToScriptTemplate} * * @returns An address generated from the hd public key */ toAddress(type?: AddressType): Address; /** * Get a derivated child based on a string or number. * * If the first argument is a string, it's parsed as the full path of * derivation. Valid values for this argument include "m" (which returns the * same public key), "m/0/1/40/2/1000". * * Note that hardened keys can't be derived from a public extended key. * * If the first argument is a number, the child with that index will be * derived. See the example usage for clarification. * * @example * ```javascript * let parent = new HDPublicKey('xpub...'); * let child_0_1_2 = parent.deriveChild(0).deriveChild(1).deriveChild(2); * let copy_of_child_0_1_2 = parent.deriveChild("m/0/1/2"); * assert(child_0_1_2.xpubkey === copy_of_child_0_1_2.xpubkey); * ``` * * @param {string|number} arg */ deriveChild(arg: string | number, hardened?: boolean): HDPublicKey; } export class HDPrivateKey implements IHDPrivateKey { readonly privateKey: PrivateKey; readonly publicKey: PublicKey; readonly network: Network; readonly depth: number; readonly parentFingerPrint: Buffer; readonly fingerPrint: Buffer; readonly chainCode: Buffer; readonly childIndex: number; readonly checksum: Buffer; readonly xprivkey: string; /** * Represents an instance of an hierarchically derived private key. * * More info on https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki */ constructor(arg?: string | Buffer | IHDPrivateKey | HDPrivateKeyDto | HDPrivateKeyMinimalDto | Networkish); get hdPublicKey(): HDPublicKey; get xpubkey(): string; /** * Verifies that a given path is valid. * * @param arg * @param hardened */ static isValidPath(arg: string | number, hardened?: boolean): boolean; static fromString(xprivkey: string): HDPrivateKey; /** * Returns the string representation of this private key (ext privkey). */ toString(): string; /** * Build a HDPrivateKey from a buffer * * @param {Buffer} buf */ static fromBuffer(buf: Buffer): HDPrivateKey; /** * Returns a buffer representation of the HDPrivateKey */ toBuffer(): Buffer; toJSON: () => HDPrivateKeyDto; /** * Returns a plain object with a representation of this private key. */ toObject(): HDPrivateKeyDto; static fromObject(arg: HDPrivateKeyDto): HDPrivateKey; static fromMinimalObject(arg: HDPrivateKeyMinimalDto): HDPrivateKey; /** * Generate a private key from a seed, as described in BIP32 * * @param seed * @param network * @return HDPrivateKey */ static fromSeed(seed: string | Buffer, network?: Networkish): HDPrivateKey; /** * Get a derived child based on a string or number. * * If the first argument is a string, it's parsed as the full path of * derivation. Valid values for this argument include "m" (which returns the * same private key), "m/0/1/40/2'/1000", where the ' quote means a hardened * derivation. * * If the first argument is a number, the child with that index will be * derived. If the second a