UNPKG

9.65 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Psbt as PsbtBase } from 'bip174';
3import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from 'bip174/src/lib/interfaces';
4import { Network } from './networks';
5import { Transaction } from './transaction';
6export interface TransactionInput {
7 hash: string | Buffer;
8 index: number;
9 sequence?: number;
10}
11export interface PsbtTxInput extends TransactionInput {
12 hash: Buffer;
13}
14export interface TransactionOutput {
15 script: Buffer;
16 value: number;
17}
18export interface PsbtTxOutput extends TransactionOutput {
19 address: string | undefined;
20}
21export type ValidateSigFunction = (pubkey: Buffer, msghash: Buffer, signature: Buffer) => boolean;
22/**
23 * Psbt class can parse and generate a PSBT binary based off of the BIP174.
24 * There are 6 roles that this class fulfills. (Explained in BIP174)
25 *
26 * Creator: This can be done with `new Psbt()`
27 * Updater: This can be done with `psbt.addInput(input)`, `psbt.addInputs(inputs)`,
28 * `psbt.addOutput(output)`, `psbt.addOutputs(outputs)` when you are looking to
29 * add new inputs and outputs to the PSBT, and `psbt.updateGlobal(itemObject)`,
30 * `psbt.updateInput(itemObject)`, `psbt.updateOutput(itemObject)`
31 * addInput requires hash: Buffer | string; and index: number; as attributes
32 * and can also include any attributes that are used in updateInput method.
33 * addOutput requires script: Buffer; and value: number; and likewise can include
34 * data for updateOutput.
35 * For a list of what attributes should be what types. Check the bip174 library.
36 * Also, check the integration tests for some examples of usage.
37 * Signer: There are a few methods. signAllInputs and signAllInputsAsync, which will search all input
38 * information for your pubkey or pubkeyhash, and only sign inputs where it finds
39 * your info. Or you can explicitly sign a specific input with signInput and
40 * signInputAsync. For the async methods you can create a SignerAsync object
41 * and use something like a hardware wallet to sign with. (You must implement this)
42 * Combiner: psbts can be combined easily with `psbt.combine(psbt2, psbt3, psbt4 ...)`
43 * the psbt calling combine will always have precedence when a conflict occurs.
44 * Combine checks if the internal bitcoin transaction is the same, so be sure that
45 * all sequences, version, locktime, etc. are the same before combining.
46 * Input Finalizer: This role is fairly important. Not only does it need to construct
47 * the input scriptSigs and witnesses, but it SHOULD verify the signatures etc.
48 * Before running `psbt.finalizeAllInputs()` please run `psbt.validateSignaturesOfAllInputs()`
49 * Running any finalize method will delete any data in the input(s) that are no longer
50 * needed due to the finalized scripts containing the information.
51 * Transaction Extractor: This role will perform some checks before returning a
52 * Transaction object. Such as fee rate not being larger than maximumFeeRate etc.
53 */
54export declare class Psbt {
55 readonly data: PsbtBase;
56 static fromBase64(data: string, opts?: PsbtOptsOptional): Psbt;
57 static fromHex(data: string, opts?: PsbtOptsOptional): Psbt;
58 static fromBuffer(buffer: Buffer, opts?: PsbtOptsOptional): Psbt;
59 private __CACHE;
60 private opts;
61 constructor(opts?: PsbtOptsOptional, data?: PsbtBase);
62 get inputCount(): number;
63 get version(): number;
64 set version(version: number);
65 get locktime(): number;
66 set locktime(locktime: number);
67 get txInputs(): PsbtTxInput[];
68 get txOutputs(): PsbtTxOutput[];
69 combine(...those: Psbt[]): this;
70 clone(): Psbt;
71 setMaximumFeeRate(satoshiPerByte: number): void;
72 setVersion(version: number): this;
73 setLocktime(locktime: number): this;
74 setInputSequence(inputIndex: number, sequence: number): this;
75 addInputs(inputDatas: PsbtInputExtended[]): this;
76 addInput(inputData: PsbtInputExtended): this;
77 addOutputs(outputDatas: PsbtOutputExtended[]): this;
78 addOutput(outputData: PsbtOutputExtended): this;
79 extractTransaction(disableFeeCheck?: boolean): Transaction;
80 getFeeRate(): number;
81 getFee(): number;
82 finalizeAllInputs(): this;
83 finalizeInput(inputIndex: number, finalScriptsFunc?: FinalScriptsFunc | FinalTaprootScriptsFunc): this;
84 finalizeTaprootInput(inputIndex: number, tapLeafHashToFinalize?: Buffer, finalScriptsFunc?: FinalTaprootScriptsFunc): this;
85 private _finalizeInput;
86 private _finalizeTaprootInput;
87 getInputType(inputIndex: number): AllScriptType;
88 inputHasPubkey(inputIndex: number, pubkey: Buffer): boolean;
89 inputHasHDKey(inputIndex: number, root: HDSigner): boolean;
90 outputHasPubkey(outputIndex: number, pubkey: Buffer): boolean;
91 outputHasHDKey(outputIndex: number, root: HDSigner): boolean;
92 validateSignaturesOfAllInputs(validator: ValidateSigFunction): boolean;
93 validateSignaturesOfInput(inputIndex: number, validator: ValidateSigFunction, pubkey?: Buffer): boolean;
94 private _validateSignaturesOfInput;
95 private validateSignaturesOfTaprootInput;
96 signAllInputsHD(hdKeyPair: HDSigner, sighashTypes?: number[]): this;
97 signAllInputsHDAsync(hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]): Promise<void>;
98 signInputHD(inputIndex: number, hdKeyPair: HDSigner, sighashTypes?: number[]): this;
99 signInputHDAsync(inputIndex: number, hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]): Promise<void>;
100 signAllInputs(keyPair: Signer, sighashTypes?: number[]): this;
101 signAllInputsAsync(keyPair: Signer | SignerAsync, sighashTypes?: number[]): Promise<void>;
102 signInput(inputIndex: number, keyPair: Signer, sighashTypes?: number[]): this;
103 signTaprootInput(inputIndex: number, keyPair: Signer, tapLeafHashToSign?: Buffer, sighashTypes?: number[]): this;
104 private _signInput;
105 private _signTaprootInput;
106 signInputAsync(inputIndex: number, keyPair: Signer | SignerAsync, sighashTypes?: number[]): Promise<void>;
107 signTaprootInputAsync(inputIndex: number, keyPair: Signer | SignerAsync, tapLeafHash?: Buffer, sighashTypes?: number[]): Promise<void>;
108 private _signInputAsync;
109 private _signTaprootInputAsync;
110 private checkTaprootHashesForSig;
111 toBuffer(): Buffer;
112 toHex(): string;
113 toBase64(): string;
114 updateGlobal(updateData: PsbtGlobalUpdate): this;
115 updateInput(inputIndex: number, updateData: PsbtInputUpdate): this;
116 updateOutput(outputIndex: number, updateData: PsbtOutputUpdate): this;
117 addUnknownKeyValToGlobal(keyVal: KeyValue): this;
118 addUnknownKeyValToInput(inputIndex: number, keyVal: KeyValue): this;
119 addUnknownKeyValToOutput(outputIndex: number, keyVal: KeyValue): this;
120 clearFinalizedInput(inputIndex: number): this;
121}
122interface PsbtOptsOptional {
123 network?: Network;
124 maximumFeeRate?: number;
125}
126interface PsbtInputExtended extends PsbtInput, TransactionInput {
127}
128type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;
129interface PsbtOutputExtendedAddress extends PsbtOutput {
130 address: string;
131 value: number;
132}
133interface PsbtOutputExtendedScript extends PsbtOutput {
134 script: Buffer;
135 value: number;
136}
137interface HDSignerBase {
138 /**
139 * DER format compressed publicKey buffer
140 */
141 publicKey: Buffer;
142 /**
143 * The first 4 bytes of the sha256-ripemd160 of the publicKey
144 */
145 fingerprint: Buffer;
146}
147export interface HDSigner extends HDSignerBase {
148 /**
149 * The path string must match /^m(\/\d+'?)+$/
150 * ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
151 */
152 derivePath(path: string): HDSigner;
153 /**
154 * Input hash (the "message digest") for the signature algorithm
155 * Return a 64 byte signature (32 byte r and 32 byte s in that order)
156 */
157 sign(hash: Buffer): Buffer;
158}
159/**
160 * Same as above but with async sign method
161 */
162export interface HDSignerAsync extends HDSignerBase {
163 derivePath(path: string): HDSignerAsync;
164 sign(hash: Buffer): Promise<Buffer>;
165}
166export interface Signer {
167 publicKey: Buffer;
168 network?: any;
169 sign(hash: Buffer, lowR?: boolean): Buffer;
170 signSchnorr?(hash: Buffer): Buffer;
171 getPublicKey?(): Buffer;
172}
173export interface SignerAsync {
174 publicKey: Buffer;
175 network?: any;
176 sign(hash: Buffer, lowR?: boolean): Promise<Buffer>;
177 signSchnorr?(hash: Buffer): Promise<Buffer>;
178 getPublicKey?(): Buffer;
179}
180/**
181 * This function must do two things:
182 * 1. Check if the `input` can be finalized. If it can not be finalized, throw.
183 * ie. `Can not finalize input #${inputIndex}`
184 * 2. Create the finalScriptSig and finalScriptWitness Buffers.
185 */
186type FinalScriptsFunc = (inputIndex: number, // Which input is it?
187input: PsbtInput, // The PSBT input contents
188script: Buffer, // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
189isSegwit: boolean, // Is it segwit?
190isP2SH: boolean, // Is it P2SH?
191isP2WSH: boolean) => {
192 finalScriptSig: Buffer | undefined;
193 finalScriptWitness: Buffer | undefined;
194};
195type FinalTaprootScriptsFunc = (inputIndex: number, // Which input is it?
196input: PsbtInput, // The PSBT input contents
197tapLeafHashToFinalize?: Buffer) => {
198 finalScriptWitness: Buffer | undefined;
199};
200type AllScriptType = 'witnesspubkeyhash' | 'pubkeyhash' | 'multisig' | 'pubkey' | 'nonstandard' | 'p2sh-witnesspubkeyhash' | 'p2sh-pubkeyhash' | 'p2sh-multisig' | 'p2sh-pubkey' | 'p2sh-nonstandard' | 'p2wsh-pubkeyhash' | 'p2wsh-multisig' | 'p2wsh-pubkey' | 'p2wsh-nonstandard' | 'p2sh-p2wsh-pubkeyhash' | 'p2sh-p2wsh-multisig' | 'p2sh-p2wsh-pubkey' | 'p2sh-p2wsh-nonstandard';
201export {};