UNPKG

6.14 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Psbt as PsbtBase } from 'bip174';
3import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, TransactionInput, TransactionOutput } from 'bip174/src/lib/interfaces';
4import { Signer, SignerAsync } from './ecpair';
5import { Network } from './networks';
6import { Transaction } from './transaction';
7/**
8 * Psbt class can parse and generate a PSBT binary based off of the BIP174.
9 * There are 6 roles that this class fulfills. (Explained in BIP174)
10 *
11 * Creator: This can be done with `new Psbt()`
12 * Updater: This can be done with `psbt.addInput(input)`, `psbt.addInputs(inputs)`,
13 * `psbt.addOutput(output)`, `psbt.addOutputs(outputs)` when you are looking to
14 * add new inputs and outputs to the PSBT, and `psbt.updateGlobal(itemObject)`,
15 * `psbt.updateInput(itemObject)`, `psbt.updateOutput(itemObject)`
16 * addInput requires hash: Buffer | string; and index: number; as attributes
17 * and can also include any attributes that are used in updateInput method.
18 * addOutput requires script: Buffer; and value: number; and likewise can include
19 * data for updateOutput.
20 * For a list of what attributes should be what types. Check the bip174 library.
21 * Also, check the integration tests for some examples of usage.
22 * Signer: There are a few methods. signAllInputs and signAllInputsAsync, which will search all input
23 * information for your pubkey or pubkeyhash, and only sign inputs where it finds
24 * your info. Or you can explicitly sign a specific input with signInput and
25 * signInputAsync. For the async methods you can create a SignerAsync object
26 * and use something like a hardware wallet to sign with. (You must implement this)
27 * Combiner: psbts can be combined easily with `psbt.combine(psbt2, psbt3, psbt4 ...)`
28 * the psbt calling combine will always have precedence when a conflict occurs.
29 * Combine checks if the internal bitcoin transaction is the same, so be sure that
30 * all sequences, version, locktime, etc. are the same before combining.
31 * Input Finalizer: This role is fairly important. Not only does it need to construct
32 * the input scriptSigs and witnesses, but it SHOULD verify the signatures etc.
33 * Before running `psbt.finalizeAllInputs()` please run `psbt.validateSignaturesOfAllInputs()`
34 * Running any finalize method will delete any data in the input(s) that are no longer
35 * needed due to the finalized scripts containing the information.
36 * Transaction Extractor: This role will perform some checks before returning a
37 * Transaction object. Such as fee rate not being larger than maximumFeeRate etc.
38 */
39export declare class Psbt {
40 readonly data: PsbtBase;
41 static fromBase64(data: string, opts?: PsbtOptsOptional): Psbt;
42 static fromHex(data: string, opts?: PsbtOptsOptional): Psbt;
43 static fromBuffer(buffer: Buffer, opts?: PsbtOptsOptional): Psbt;
44 private __CACHE;
45 private opts;
46 constructor(opts?: PsbtOptsOptional, data?: PsbtBase);
47 readonly inputCount: number;
48 combine(...those: Psbt[]): this;
49 clone(): Psbt;
50 setMaximumFeeRate(satoshiPerByte: number): void;
51 setVersion(version: number): this;
52 setLocktime(locktime: number): this;
53 setInputSequence(inputIndex: number, sequence: number): this;
54 addInputs(inputDatas: PsbtInputExtended[]): this;
55 addInput(inputData: PsbtInputExtended): this;
56 addOutputs(outputDatas: PsbtOutputExtended[]): this;
57 addOutput(outputData: PsbtOutputExtended): this;
58 extractTransaction(disableFeeCheck?: boolean): Transaction;
59 getFeeRate(): number;
60 getFee(): number;
61 finalizeAllInputs(): this;
62 finalizeInput(inputIndex: number): this;
63 validateSignaturesOfAllInputs(): boolean;
64 validateSignaturesOfInput(inputIndex: number, pubkey?: Buffer): boolean;
65 signAllInputsHD(hdKeyPair: HDSigner, sighashTypes?: number[]): this;
66 signAllInputsHDAsync(hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]): Promise<void>;
67 signInputHD(inputIndex: number, hdKeyPair: HDSigner, sighashTypes?: number[]): this;
68 signInputHDAsync(inputIndex: number, hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]): Promise<void>;
69 signAllInputs(keyPair: Signer, sighashTypes?: number[]): this;
70 signAllInputsAsync(keyPair: Signer | SignerAsync, sighashTypes?: number[]): Promise<void>;
71 signInput(inputIndex: number, keyPair: Signer, sighashTypes?: number[]): this;
72 signInputAsync(inputIndex: number, keyPair: Signer | SignerAsync, sighashTypes?: number[]): Promise<void>;
73 toBuffer(): Buffer;
74 toHex(): string;
75 toBase64(): string;
76 updateGlobal(updateData: PsbtGlobalUpdate): this;
77 updateInput(inputIndex: number, updateData: PsbtInputUpdate): this;
78 updateOutput(outputIndex: number, updateData: PsbtOutputUpdate): this;
79 addUnknownKeyValToGlobal(keyVal: KeyValue): this;
80 addUnknownKeyValToInput(inputIndex: number, keyVal: KeyValue): this;
81 addUnknownKeyValToOutput(outputIndex: number, keyVal: KeyValue): this;
82 clearFinalizedInput(inputIndex: number): this;
83}
84interface PsbtOptsOptional {
85 network?: Network;
86 maximumFeeRate?: number;
87}
88interface PsbtInputExtended extends PsbtInput, TransactionInput {
89}
90interface PsbtOutputExtended extends PsbtOutput, TransactionOutput {
91}
92interface HDSignerBase {
93 /**
94 * DER format compressed publicKey buffer
95 */
96 publicKey: Buffer;
97 /**
98 * The first 4 bytes of the sha256-ripemd160 of the publicKey
99 */
100 fingerprint: Buffer;
101}
102interface HDSigner extends HDSignerBase {
103 /**
104 * The path string must match /^m(\/\d+'?)+$/
105 * ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
106 */
107 derivePath(path: string): HDSigner;
108 /**
109 * Input hash (the "message digest") for the signature algorithm
110 * Return a 64 byte signature (32 byte r and 32 byte s in that order)
111 */
112 sign(hash: Buffer): Buffer;
113}
114/**
115 * Same as above but with async sign method
116 */
117interface HDSignerAsync extends HDSignerBase {
118 derivePath(path: string): HDSignerAsync;
119 sign(hash: Buffer): Promise<Buffer>;
120}
121export {};