@btc-vision/bitcoin 6.3.0

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