@caravan/psbt 1.0.0 → 1.0.1

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.mts CHANGED
@@ -1,34 +1,74 @@
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
  /**
@@ -92,12 +132,37 @@ declare class PsbtV2 extends PsbtV2Maps {
92
132
  /**
93
133
  * Other Getters/Setters
94
134
  */
135
+ /**
136
+ * Returns the `nLockTime` field for the psbt as if it were a bitcoin
137
+ * transaction.
138
+ */
95
139
  get nLockTime(): number | null;
96
140
  /**
97
141
  * Creator/Constructor Methods
98
142
  */
143
+ /**
144
+ * Ensures that global fields have initial values required by a PsbtV2
145
+ * Creator. It is called by the constructor if constructed without a psbt.
146
+ */
99
147
  private create;
148
+ /**
149
+ * Checks initial construction of any valid PsbtV2. It is called when a psbt
150
+ * is passed to the constructor or when a new psbt is being created. If
151
+ * constructed with a psbt, this method acts outside of the Creator role to
152
+ * validate the current state of the psbt.
153
+ */
100
154
  private validate;
155
+ /**
156
+ * This method is provided for compatibility issues and probably shouldn't be
157
+ * used since a PsbtV2 with PSBT_GLOBAL_TX_VERSION = 1 is BIP0370
158
+ * non-compliant. No guarantees can be made here that a serialized PsbtV2
159
+ * which used this method will be compatible with outside consumers.
160
+ *
161
+ * One may wish to instance this class from a partially signed PSBTv0 with a
162
+ * txn version 1 by using the static PsbtV2.FromV0. This method provides a way
163
+ * to override validation logic for the txn version and roles lifecycle
164
+ * defined for PsbtV2.
165
+ */
101
166
  dangerouslySetGlobalTxVersion1(): void;
102
167
  addGlobalXpub(xpub: Buffer, fingerprint: Buffer, path: string): void;
103
168
  addInput({ previousTxId, outputIndex, sequence, nonWitnessUtxo, witnessUtxo, redeemScript, witnessScript, bip32Derivation, }: {
@@ -131,20 +196,47 @@ declare class PsbtV2 extends PsbtV2Maps {
131
196
  /**
132
197
  * Updater/Signer Methods
133
198
  */
199
+ /**
200
+ * Removes an input-map from inputMaps.
201
+ */
134
202
  deleteInput(index: number): void;
203
+ /**
204
+ * Removes an output-map from outputMaps.
205
+ */
135
206
  deleteOutput(index: number): void;
207
+ /**
208
+ * Checks that provided flags are present in PSBT_GLOBAL_TX_MODIFIABLE.
209
+ */
136
210
  private isModifiable;
211
+ /**
212
+ * Adds a signature for an input. Validates that the input is mapped and does
213
+ * not already have a signature for the pubkey. Also validates for sighash.
214
+ * Other validation is incomplete. Also validates for required args in case
215
+ * typescript is not being used to call the method.
216
+ *
217
+ * The Signer, when it creates a signature, must add the partial sig keypair
218
+ * to the psbt for the input which it is signing. In the case that a
219
+ * particular signer does not, this method can be used to add a signature to
220
+ * the psbt. This method assumes the Signer did the validation outlined in
221
+ * BIP0174 before creating a signature.
222
+ * https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#signer
223
+ */
137
224
  addPartialSig(inputIndex: number, pubkey: Buffer, sig: Buffer): void;
225
+ /**
226
+ * Removes all sigs for an input unless a pubkey is specified. Validates that
227
+ * the input exists. When providing a pubkey, this validates that a sig for
228
+ * the pubkey exists.
229
+ */
138
230
  removePartialSig(inputIndex: number, pubkey?: Buffer): void;
231
+ /**
232
+ * Ensures the PSBT is in the proper state when adding a partial sig keypair.
233
+ * https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki#signer
234
+ */
139
235
  private handleSighashType;
236
+ /**
237
+ * Attempts to return a PsbtV2 by converting from a PsbtV0 string or Buffer
238
+ */
140
239
  static FromV0(psbt: string | Buffer, allowTxnVersion1?: boolean): PsbtV2;
141
240
  }
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
241
 
150
242
  export { PsbtV2, getPsbtVersionNumber };
package/dist/index.d.ts CHANGED
@@ -1,34 +1,74 @@
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
  /**
@@ -92,12 +132,37 @@ declare class PsbtV2 extends PsbtV2Maps {
92
132
  /**
93
133
  * Other Getters/Setters
94
134
  */
135
+ /**
136
+ * Returns the `nLockTime` field for the psbt as if it were a bitcoin
137
+ * transaction.
138
+ */
95
139
  get nLockTime(): number | null;
96
140
  /**
97
141
  * Creator/Constructor Methods
98
142
  */
143
+ /**
144
+ * Ensures that global fields have initial values required by a PsbtV2
145
+ * Creator. It is called by the constructor if constructed without a psbt.
146
+ */
99
147
  private create;
148
+ /**
149
+ * Checks initial construction of any valid PsbtV2. It is called when a psbt
150
+ * is passed to the constructor or when a new psbt is being created. If
151
+ * constructed with a psbt, this method acts outside of the Creator role to
152
+ * validate the current state of the psbt.
153
+ */
100
154
  private validate;
155
+ /**
156
+ * This method is provided for compatibility issues and probably shouldn't be
157
+ * used since a PsbtV2 with PSBT_GLOBAL_TX_VERSION = 1 is BIP0370
158
+ * non-compliant. No guarantees can be made here that a serialized PsbtV2
159
+ * which used this method will be compatible with outside consumers.
160
+ *
161
+ * One may wish to instance this class from a partially signed PSBTv0 with a
162
+ * txn version 1 by using the static PsbtV2.FromV0. This method provides a way
163
+ * to override validation logic for the txn version and roles lifecycle
164
+ * defined for PsbtV2.
165
+ */
101
166
  dangerouslySetGlobalTxVersion1(): void;
102
167
  addGlobalXpub(xpub: Buffer, fingerprint: Buffer, path: string): void;
103
168
  addInput({ previousTxId, outputIndex, sequence, nonWitnessUtxo, witnessUtxo, redeemScript, witnessScript, bip32Derivation, }: {
@@ -131,20 +196,47 @@ declare class PsbtV2 extends PsbtV2Maps {
131
196
  /**
132
197
  * Updater/Signer Methods
133
198
  */
199
+ /**
200
+ * Removes an input-map from inputMaps.
201
+ */
134
202
  deleteInput(index: number): void;
203
+ /**
204
+ * Removes an output-map from outputMaps.
205
+ */
135
206
  deleteOutput(index: number): void;
207
+ /**
208
+ * Checks that provided flags are present in PSBT_GLOBAL_TX_MODIFIABLE.
209
+ */
136
210
  private isModifiable;
211
+ /**
212
+ * Adds a signature for an input. Validates that the input is mapped and does
213
+ * not already have a signature for the pubkey. Also validates for sighash.
214
+ * Other validation is incomplete. Also validates for required args in case
215
+ * typescript is not being used to call the method.
216
+ *
217
+ * The Signer, when it creates a signature, must add the partial sig keypair
218
+ * to the psbt for the input which it is signing. In the case that a
219
+ * particular signer does not, this method can be used to add a signature to
220
+ * the psbt. This method assumes the Signer did the validation outlined in
221
+ * BIP0174 before creating a signature.
222
+ * https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#signer
223
+ */
137
224
  addPartialSig(inputIndex: number, pubkey: Buffer, sig: Buffer): void;
225
+ /**
226
+ * Removes all sigs for an input unless a pubkey is specified. Validates that
227
+ * the input exists. When providing a pubkey, this validates that a sig for
228
+ * the pubkey exists.
229
+ */
138
230
  removePartialSig(inputIndex: number, pubkey?: Buffer): void;
231
+ /**
232
+ * Ensures the PSBT is in the proper state when adding a partial sig keypair.
233
+ * https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki#signer
234
+ */
139
235
  private handleSighashType;
236
+ /**
237
+ * Attempts to return a PsbtV2 by converting from a PsbtV0 string or Buffer
238
+ */
140
239
  static FromV0(psbt: string | Buffer, allowTxnVersion1?: boolean): PsbtV2;
141
240
  }
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
241
 
150
242
  export { PsbtV2, getPsbtVersionNumber };