@caravan/psbt 1.0.0 → 1.1.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.
package/dist/index.d.ts CHANGED
@@ -1,40 +1,80 @@
1
1
  /**
2
- * The PsbtV2 class is intended to represent an easily modifiable and
3
- * serializable psbt of version 2 conforming to BIP0174. Getters exist for all
4
- * BIP-defined keytypes. Very few setters and modifier methods exist. As they
5
- * are added, they should enforce implied and documented rules and limitations.
6
- *
7
- * Defining BIPs:
8
- * https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
9
- * https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki
2
+ * Hex encoded string containing `<keytype><keydata>`. A string is needed for
3
+ * Map.get() since it matches by identity. Most commonly, a `Key` only contains a
4
+ * keytype byte, however, some with keydata can allow for multiple unique keys
5
+ * of the same type.
10
6
  */
11
7
  type Key = string;
8
+ /**
9
+ * Values can be of various different types or formats. Here we leave them as
10
+ * Buffers so that getters can decide how they should be formatted.
11
+ */
12
12
  type Value = Buffer;
13
13
  type NonUniqueKeyTypeValue = {
14
14
  key: string;
15
15
  value: string | null;
16
16
  };
17
+ /**
18
+ * Provided to friendly-format the `PSBT_GLOBAL_TX_MODIFIABLE` bitmask from
19
+ * `PsbtV2.PSBT_GLOBAL_TX_MODIFIABLE` which returns
20
+ * `PsbtGlobalTxModifiableBits[]`.
21
+ */
17
22
  declare enum PsbtGlobalTxModifiableBits {
18
23
  INPUTS = "INPUTS",// 0b00000001
19
24
  OUTPUTS = "OUTPUTS",// 0b00000010
20
25
  SIGHASH_SINGLE = "SIGHASH_SINGLE"
21
26
  }
27
+
28
+ /**
29
+ * Attempts to extract the version number as uint32LE from raw psbt regardless
30
+ * of psbt validity.
31
+ */
32
+ declare function getPsbtVersionNumber(psbt: string | Buffer): number;
33
+
34
+ /**
35
+ * This abstract class is provided for utility to allow for mapping, map
36
+ * copying, and serialization operations for psbts. This does almost no
37
+ * validation, so do not rely on it for ensuring a valid psbt.
38
+ */
22
39
  declare abstract class PsbtV2Maps {
23
40
  protected globalMap: Map<Key, Value>;
24
41
  protected inputMaps: Map<Key, Value>[];
25
42
  protected outputMaps: Map<Key, Value>[];
26
43
  constructor(psbt?: Buffer | string);
44
+ /**
45
+ * Return the current state of the psbt as a string in the specified format.
46
+ */
27
47
  serialize(format?: "base64" | "hex"): string;
48
+ /**
49
+ * Copies the maps in this PsbtV2 object to another PsbtV2 object.
50
+ *
51
+ * NOTE: This copy method is made available to achieve parity with the PSBT
52
+ * api required by `ledger-bitcoin` for creating merklized PSBTs. HOWEVER, it
53
+ * is not recommended to use this when avoidable as copying maps bypasses the
54
+ * validation defined in the constructor, so it could create a psbtv2 in an
55
+ * invalid psbt state. PsbtV2.serialize is preferable whenever possible.
56
+ */
28
57
  copy(to: PsbtV2): void;
29
58
  private copyMaps;
30
59
  private copyMap;
31
60
  }
61
+
62
+ /**
63
+ * The PsbtV2 class is intended to represent an easily modifiable and
64
+ * serializable psbt of version 2 conforming to BIP0174. Getters exist for all
65
+ * BIP-defined keytypes. Very few setters and modifier methods exist. As they
66
+ * are added, they should enforce implied and documented rules and limitations.
67
+ *
68
+ * Defining BIPs:
69
+ * https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
70
+ * https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki
71
+ */
32
72
  declare class PsbtV2 extends PsbtV2Maps {
33
73
  constructor(psbt?: Buffer | string);
34
74
  /**
35
75
  * Globals Getters/Setters
36
76
  */
37
- get PSBT_GLOBAL_XPUB(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
77
+ get PSBT_GLOBAL_XPUB(): NonUniqueKeyTypeValue[];
38
78
  get PSBT_GLOBAL_TX_VERSION(): number;
39
79
  set PSBT_GLOBAL_TX_VERSION(version: number);
40
80
  get PSBT_GLOBAL_FALLBACK_LOCKTIME(): number | null;
@@ -47,7 +87,7 @@ declare class PsbtV2 extends PsbtV2Maps {
47
87
  set PSBT_GLOBAL_TX_MODIFIABLE(modifiable: PsbtGlobalTxModifiableBits[]);
48
88
  get PSBT_GLOBAL_VERSION(): number;
49
89
  set PSBT_GLOBAL_VERSION(version: number);
50
- get PSBT_GLOBAL_PROPRIETARY(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
90
+ get PSBT_GLOBAL_PROPRIETARY(): NonUniqueKeyTypeValue[];
51
91
  /**
52
92
  * Input Getters/Setters
53
93
  */
@@ -57,23 +97,23 @@ declare class PsbtV2 extends PsbtV2Maps {
57
97
  get PSBT_IN_SIGHASH_TYPE(): (number | null)[];
58
98
  get PSBT_IN_REDEEM_SCRIPT(): (string | null)[];
59
99
  get PSBT_IN_WITNESS_SCRIPT(): (string | null)[];
60
- get PSBT_IN_BIP32_DERIVATION(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
100
+ get PSBT_IN_BIP32_DERIVATION(): NonUniqueKeyTypeValue[][];
61
101
  get PSBT_IN_FINAL_SCRIPTSIG(): (string | null)[];
62
102
  get PSBT_IN_FINAL_SCRIPTWITNESS(): (string | null)[];
63
103
  get PSBT_IN_POR_COMMITMENT(): (string | null)[];
64
- get PSBT_IN_RIPEMD160(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
65
- get PSBT_IN_SHA256(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
66
- get PSBT_IN_HASH160(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
67
- get PSBT_IN_HASH256(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
104
+ get PSBT_IN_RIPEMD160(): NonUniqueKeyTypeValue[][];
105
+ get PSBT_IN_SHA256(): NonUniqueKeyTypeValue[][];
106
+ get PSBT_IN_HASH160(): NonUniqueKeyTypeValue[][];
107
+ get PSBT_IN_HASH256(): NonUniqueKeyTypeValue[][];
68
108
  get PSBT_IN_PREVIOUS_TXID(): string[];
69
109
  get PSBT_IN_OUTPUT_INDEX(): number[];
70
110
  get PSBT_IN_SEQUENCE(): (number | null)[];
71
111
  get PSBT_IN_REQUIRED_TIME_LOCKTIME(): (number | null)[];
72
112
  get PSBT_IN_REQUIRED_HEIGHT_LOCKTIME(): (number | null)[];
73
113
  get PSBT_IN_TAP_KEY_SIG(): (string | null)[];
74
- get PSBT_IN_TAP_SCRIPT_SIG(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
75
- get PSBT_IN_TAP_LEAF_SCRIPT(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
76
- get PSBT_IN_TAP_BIP32_DERIVATION(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
114
+ get PSBT_IN_TAP_SCRIPT_SIG(): NonUniqueKeyTypeValue[][];
115
+ get PSBT_IN_TAP_LEAF_SCRIPT(): NonUniqueKeyTypeValue[][];
116
+ get PSBT_IN_TAP_BIP32_DERIVATION(): NonUniqueKeyTypeValue[][];
77
117
  get PSBT_IN_TAP_INTERNAL_KEY(): (string | null)[];
78
118
  get PSBT_IN_TAP_MERKLE_ROOT(): (string | null)[];
79
119
  get PSBT_IN_PROPRIETARY(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
@@ -89,15 +129,91 @@ declare class PsbtV2 extends PsbtV2Maps {
89
129
  get PSBT_OUT_TAP_TREE(): (string | null)[];
90
130
  get PSBT_OUT_TAP_BIP32_DERIVATION(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
91
131
  get PSBT_OUT_PROPRIETARY(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
132
+ /**
133
+ * Operator Role Validation Getters
134
+ */
135
+ /**
136
+ * Returns true if the PsbtV2 is ready for an operator taking the Constructor
137
+ * role.
138
+ *
139
+ * This check assumes that the Creator used this class's constructor method to
140
+ * initialize the PsbtV2 without passing a psbt (constructor defaults were
141
+ * set).
142
+ */
143
+ get isReadyForConstructor(): boolean;
144
+ /**
145
+ * Returns true if the PsbtV2 is ready for an operator taking the Updater
146
+ * role.
147
+ *
148
+ * Before signatures are added, but after an input is added, a PsbtV2 is
149
+ * likely to be ready for Constructor, ready for Updater, and ready for Signer
150
+ * simultaneously.
151
+ *
152
+ * According to BIP370, the Updater can modify the sequence number, but it is
153
+ * unclear if the Updater retains permissions provided in psbtv0 (BIP174). It
154
+ * is likely not the case that the Updater has the same permissions as
155
+ * previously because it seems to now be the realm of the Constructor to add
156
+ * inputs and outputs.
157
+ */
158
+ get isReadyForUpdater(): boolean;
159
+ /**
160
+ * Returns true if the PsbtV2 is ready for an operator taking the Signer role.
161
+ */
162
+ get isReadyForSigner(): boolean;
163
+ /**
164
+ * Returns true if the PsbtV2 is ready for an operator taking the Combiner
165
+ * role.
166
+ */
167
+ get isReadyForCombiner(): boolean;
168
+ /**
169
+ * Unimplemented. Returns false.
170
+ */
171
+ get isReadyForInputFinalizer(): boolean;
172
+ /**
173
+ * Returns true if the PsbtV2 is ready for an operator taking the Transaction
174
+ * Extractor role.
175
+ *
176
+ * If all the inputs have been finalized, then the psbt is ready for the
177
+ * Transaction Extractor. According to BIP 174, it's the responsibility of the
178
+ * Input Finalizer to add scriptSigs or scriptWitnesses and then remove other
179
+ * details besides the UTXO. This getter checks that the Input Finalizer has
180
+ * finished its job.
181
+ */
182
+ get isReadyForTransactionExtractor(): boolean;
92
183
  /**
93
184
  * Other Getters/Setters
94
185
  */
186
+ /**
187
+ * Returns the `nLockTime` field for the psbt as if it were a bitcoin
188
+ * transaction.
189
+ */
95
190
  get nLockTime(): number | null;
96
191
  /**
97
192
  * Creator/Constructor Methods
98
193
  */
194
+ /**
195
+ * Ensures that global fields have initial values required by a PsbtV2
196
+ * Creator. It is called by the constructor if constructed without a psbt.
197
+ */
99
198
  private create;
199
+ /**
200
+ * Checks initial construction of any valid PsbtV2. It is called when a psbt
201
+ * is passed to the constructor or when a new psbt is being created. If
202
+ * constructed with a psbt, this method acts outside of the Creator role to
203
+ * validate the current state of the psbt.
204
+ */
100
205
  private validate;
206
+ /**
207
+ * This method is provided for compatibility issues and probably shouldn't be
208
+ * used since a PsbtV2 with PSBT_GLOBAL_TX_VERSION = 1 is BIP0370
209
+ * non-compliant. No guarantees can be made here that a serialized PsbtV2
210
+ * which used this method will be compatible with outside consumers.
211
+ *
212
+ * One may wish to instance this class from a partially signed PSBTv0 with a
213
+ * txn version 1 by using the static PsbtV2.FromV0. This method provides a way
214
+ * to override validation logic for the txn version and roles lifecycle
215
+ * defined for PsbtV2.
216
+ */
101
217
  dangerouslySetGlobalTxVersion1(): void;
102
218
  addGlobalXpub(xpub: Buffer, fingerprint: Buffer, path: string): void;
103
219
  addInput({ previousTxId, outputIndex, sequence, nonWitnessUtxo, witnessUtxo, redeemScript, witnessScript, bip32Derivation, }: {
@@ -131,20 +247,51 @@ declare class PsbtV2 extends PsbtV2Maps {
131
247
  /**
132
248
  * Updater/Signer Methods
133
249
  */
250
+ /**
251
+ * Removes an input-map from inputMaps.
252
+ */
134
253
  deleteInput(index: number): void;
254
+ /**
255
+ * Removes an output-map from outputMaps.
256
+ */
135
257
  deleteOutput(index: number): void;
258
+ /**
259
+ * Checks that all provided flags are present in PSBT_GLOBAL_TX_MODIFIABLE.
260
+ */
136
261
  private isModifiable;
262
+ /**
263
+ * Adds a signature for an input. Validates that the input is mapped and does
264
+ * not already have a signature for the pubkey. Also validates for sighash.
265
+ * Other validation is incomplete. Also validates for required args in case
266
+ * typescript is not being used to call the method.
267
+ *
268
+ * The Signer, when it creates a signature, must add the partial sig keypair
269
+ * to the psbt for the input which it is signing. In the case that a
270
+ * particular signer does not, this method can be used to add a signature to
271
+ * the psbt. This method assumes the Signer did the validation outlined in
272
+ * BIP0174 before creating a signature.
273
+ * https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#signer
274
+ */
137
275
  addPartialSig(inputIndex: number, pubkey: Buffer, sig: Buffer): void;
276
+ /**
277
+ * Removes all sigs for an input unless a pubkey is specified. Validates that
278
+ * the input exists. When providing a pubkey, this validates that a sig for
279
+ * the pubkey exists.
280
+ */
138
281
  removePartialSig(inputIndex: number, pubkey?: Buffer): void;
282
+ /**
283
+ * Ensures the PSBT is in the proper state when adding a partial sig keypair.
284
+ * https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki#signer
285
+ */
139
286
  private handleSighashType;
287
+ /**
288
+ * Attempts to return a PsbtV2 by converting from a PsbtV0 string or Buffer.
289
+ *
290
+ * This method first starts with a fresh PsbtV2 having just been created. It
291
+ * then takes the PsbtV2 through its operator saga through the Signer role. In
292
+ * this sense validation for each operator role will be performed.
293
+ */
140
294
  static FromV0(psbt: string | Buffer, allowTxnVersion1?: boolean): PsbtV2;
141
295
  }
142
- /**
143
- * Attempts to extract the version number as uint32LE from raw psbt regardless
144
- * of psbt validity.
145
- * @param {string | Buffer} psbt - hex, base64 or buffer of psbt
146
- * @returns {number} version number
147
- */
148
- declare function getPsbtVersionNumber(psbt: string | Buffer): number;
149
296
 
150
297
  export { PsbtV2, getPsbtVersionNumber };