@caravan/psbt 1.0.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.
@@ -0,0 +1,196 @@
1
+
2
+ 
3
+ > @caravan/psbt@0.0.0 test
4
+ > jest src
5
+
6
+ Determining test suites to run...
7
+
8
+ Test Suites: 0 of 2 total
9
+ Tests: 0 total
10
+ Snapshots: 0 total
11
+ Time: 0 s, estimated 5 s
12
+ ████████████████████████████████████████
13
+  RUNS  src/psbtv2.test.ts
14
+  RUNS  src/psbt.test.ts
15
+
16
+ Test Suites: 0 of 2 total
17
+ Tests: 0 total
18
+ Snapshots: 0 total
19
+ Time: 0 s, estimated 5 s
20
+ ████████████████████████████████████████
21
+  RUNS  src/psbtv2.test.ts
22
+  RUNS  src/psbt.test.ts
23
+
24
+ Test Suites: 0 of 2 total
25
+ Tests: 0 total
26
+ Snapshots: 0 total
27
+ Time: 1 s, estimated 5 s
28
+ ████████████████████████████████████████
29
+  RUNS  src/psbtv2.test.ts
30
+  RUNS  src/psbt.test.ts
31
+
32
+ Test Suites: 0 of 2 total
33
+ Tests: 0 total
34
+ Snapshots: 0 total
35
+ Time: 2 s, estimated 5 s
36
+ ████████████████████████████████████████
37
+  RUNS  src/psbtv2.test.ts
38
+  RUNS  src/psbt.test.ts
39
+
40
+ Test Suites: 0 of 2 total
41
+ Tests: 0 total
42
+ Snapshots: 0 total
43
+ Time: 3 s, estimated 5 s
44
+ ████████████████████████████████████████
45
+  RUNS  src/psbtv2.test.ts
46
+  RUNS  src/psbt.test.ts
47
+
48
+ Test Suites: 0 of 2 total
49
+ Tests: 67 passed, 67 total
50
+ Snapshots: 0 total
51
+ Time: 3 s, estimated 5 s
52
+ ████████████████████████████████████████
53
+
54
+  RUNS  src/psbtv2.test.ts
55
+  RUNS  src/psbt.test.ts
56
+
57
+ Test Suites: 0 of 2 total
58
+ Tests: 67 passed, 67 total
59
+ Snapshots: 0 total
60
+ Time: 3 s, estimated 5 s
61
+ ████████████████████████████████████████
62
+  RUNS  src/psbtv2.test.ts
63
+  RUNS  src/psbt.test.ts
64
+
65
+ Test Suites: 0 of 2 total
66
+ Tests: 67 passed, 67 total
67
+ Snapshots: 0 total
68
+ Time: 3 s, estimated 5 s
69
+ ████████████████████████████████████████
70
+  RUNS  src/psbtv2.test.ts
71
+
72
+ Test Suites: 1 passed, 1 of 2 total
73
+ Tests: 134 passed, 134 total
74
+ Snapshots: 0 total
75
+ Time: 3 s, estimated 5 s
76
+ ████████████████████████████████████████
77
+  RUNS  src/psbtv2.test.ts
78
+
79
+ Test Suites: 1 passed, 1 of 2 total
80
+ Tests: 156 passed, 156 total
81
+ Snapshots: 0 total
82
+ Time: 3 s, estimated 5 s
83
+ ████████████████████████████████████████
84
+
85
+  RUNS  src/psbtv2.test.ts
86
+
87
+ Test Suites: 1 passed, 1 of 2 total
88
+ Tests: 156 passed, 156 total
89
+ Snapshots: 0 total
90
+ Time: 3 s, estimated 5 s
91
+ ████████████████████████████████████████
92
+
93
+ console.warn
94
+  Dangerously setting PsbtV2.PSBT_GLOBAL_TX_VERSION to 1!
95
+ 
96
+    892 | // lifecycle defined for PsbtV2.
97
+    893 | public dangerouslySetGlobalTxVersion1() {
98
+  > 894 | console.warn("Dangerously setting PsbtV2.PSBT_GLOBAL_TX_VERSION to 1!");
99
+    | ^
100
+    895 | const bw = new BufferWriter();
101
+    896 | bw.writeI32(1);
102
+    897 | this.globalMap.set(KeyType.PSBT_GLOBAL_TX_VERSION, bw.render());
103
+ 
104
+  at PsbtV2.dangerouslySetGlobalTxVersion1 (src/psbtv2.ts:894:13)
105
+  at Function.FromV0 (src/psbtv2.ts:1208:14)
106
+  at t (src/psbtv2.test.ts:947:34)
107
+  at Object.<anonymous> (../../node_modules/expect/build/toThrowMatchers.js:74:11)
108
+  at Object.throwingMatcher [as toThrow] (../../node_modules/expect/build/index.js:320:21)
109
+  at src/psbtv2.test.ts:948:25
110
+
111
+ console.warn
112
+  Dangerously setting PsbtV2.PSBT_GLOBAL_TX_VERSION to 1!
113
+ 
114
+    892 | // lifecycle defined for PsbtV2.
115
+    893 | public dangerouslySetGlobalTxVersion1() {
116
+  > 894 | console.warn("Dangerously setting PsbtV2.PSBT_GLOBAL_TX_VERSION to 1!");
117
+    | ^
118
+    895 | const bw = new BufferWriter();
119
+    896 | bw.writeI32(1);
120
+    897 | this.globalMap.set(KeyType.PSBT_GLOBAL_TX_VERSION, bw.render());
121
+ 
122
+  at PsbtV2.dangerouslySetGlobalTxVersion1 (src/psbtv2.ts:894:13)
123
+  at Function.FromV0 (src/psbtv2.ts:1208:14)
124
+  at t (src/psbtv2.test.ts:947:34)
125
+  at Object.<anonymous> (../../node_modules/expect/build/toThrowMatchers.js:74:11)
126
+  at Object.throwingMatcher [as toThrow] (../../node_modules/expect/build/index.js:320:21)
127
+  at src/psbtv2.test.ts:948:25
128
+
129
+ console.warn
130
+  Dangerously setting PsbtV2.PSBT_GLOBAL_TX_VERSION to 1!
131
+ 
132
+    892 | // lifecycle defined for PsbtV2.
133
+    893 | public dangerouslySetGlobalTxVersion1() {
134
+  > 894 | console.warn("Dangerously setting PsbtV2.PSBT_GLOBAL_TX_VERSION to 1!");
135
+    | ^
136
+    895 | const bw = new BufferWriter();
137
+    896 | bw.writeI32(1);
138
+    897 | this.globalMap.set(KeyType.PSBT_GLOBAL_TX_VERSION, bw.render());
139
+ 
140
+  at PsbtV2.dangerouslySetGlobalTxVersion1 (src/psbtv2.ts:894:13)
141
+  at Function.FromV0 (src/psbtv2.ts:1208:14)
142
+  at src/psbtv2.test.ts:959:25
143
+
144
+ console.error
145
+  Error: 
146
+  at PsbtV2.<anonymous> (/Users/atreides/repos/caravan-monorepo/packages/caravan-psbt/src/psbtv2.test.ts:1044:13)
147
+  at /Users/atreides/repos/caravan-monorepo/node_modules/jest-mock/build/index.js:397:39
148
+  at PsbtV2.<anonymous> (/Users/atreides/repos/caravan-monorepo/node_modules/jest-mock/build/index.js:404:13)
149
+  at PsbtV2.mockConstructor [as handleSighashType] (/Users/atreides/repos/caravan-monorepo/node_modules/jest-mock/build/index.js:148:19)
150
+  at PsbtV2.addPartialSig (/Users/atreides/repos/caravan-monorepo/packages/caravan-psbt/src/psbtv2.ts:1120:12)
151
+  at Object.<anonymous> (/Users/atreides/repos/caravan-monorepo/packages/caravan-psbt/src/psbtv2.test.ts:1049:10)
152
+  at Promise.then.completed (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/utils.js:298:28)
153
+  at new Promise (<anonymous>)
154
+  at callAsyncCircusFn (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/utils.js:231:10)
155
+  at _callCircusTest (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/run.js:316:40)
156
+  at processTicksAndRejections (node:internal/process/task_queues:95:5)
157
+  at _runTest (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/run.js:252:3)
158
+  at _runTestsForDescribeBlock (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/run.js:126:9)
159
+  at _runTestsForDescribeBlock (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/run.js:121:9)
160
+  at run (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/run.js:71:3)
161
+  at runAndTransformResultsToJestFormat (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
162
+  at jestAdapter (/Users/atreides/repos/caravan-monorepo/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
163
+  at runTestInternal (/Users/atreides/repos/caravan-monorepo/node_modules/jest-runner/build/runTest.js:367:16)
164
+  at runTest (/Users/atreides/repos/caravan-monorepo/node_modules/jest-runner/build/runTest.js:444:34)
165
+  at Object.worker (/Users/atreides/repos/caravan-monorepo/node_modules/jest-runner/build/testWorker.js:106:12)
166
+ 
167
+    1120 | this.handleSighashType(sig);
168
+    1121 | } catch (err) {
169
+  > 1122 | console.error(err);
170
+    | ^
171
+    1123 | // To remain atomic, attempt to reset everything to the way it was.
172
+    1124 | this.inputMaps[inputIndex].delete(key);
173
+    1125 | this.PSBT_GLOBAL_TX_MODIFIABLE = modBackup;
174
+ 
175
+  at PsbtV2.addPartialSig (src/psbtv2.ts:1122:15)
176
+  at Object.<anonymous> (src/psbtv2.test.ts:1049:10)
177
+
178
+
179
+  RUNS  src/psbtv2.test.ts
180
+
181
+ Test Suites: 1 passed, 1 of 2 total
182
+ Tests: 156 passed, 156 total
183
+ Snapshots: 0 total
184
+ Time: 3 s, estimated 5 s
185
+ ████████████████████████████████████████
186
+
187
+ Test Suites: 2 passed, 2 total
188
+ Tests: 177 passed, 177 total
189
+ Snapshots: 0 total
190
+ Time: 3 s, estimated 5 s
191
+ ████████████████████████████████████████
192
+ Test Suites: 2 passed, 2 total
193
+ Tests: 177 passed, 177 total
194
+ Snapshots: 0 total
195
+ Time: 3.973 s, estimated 5 s
196
+ Ran all test suites matching /src/i.
package/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # @caravan/psbt
2
+
3
+ ## 0.0.0
4
+
5
+ ### Patch Changes
6
+
7
+ - [#15](https://github.com/caravan-bitcoin/caravan/pull/15) [`a39dfde`](https://github.com/caravan-bitcoin/caravan/commit/a39dfde2aab9908370bc5eea032960b1939f1e14) Thanks [@bucko13](https://github.com/bucko13)! - maintenance patch to cleanup dependencies
8
+
9
+ - Updated dependencies [[`a39dfde`](https://github.com/caravan-bitcoin/caravan/commit/a39dfde2aab9908370bc5eea032960b1939f1e14)]:
10
+ - @caravan/bitcoin@0.0.1
package/README.md ADDED
@@ -0,0 +1,197 @@
1
+ # Caravan PSBT
2
+
3
+ A set of utilities for working with PSBTs.
4
+
5
+ ## Table of contents
6
+
7
+ - [PSBTv0](#psbtv0)
8
+ - [Exports](#psbtv0-exports)
9
+ - [`const PSBT_MAGIC_HEX`](#const-psbt_magic_hex)
10
+ - [`const PSBT_MAGIC_B64`](#const-psbt_magic_b64)
11
+ - [`const PSBT_MAGIC_BYTES`](#const-psbt_magic_bytes)
12
+ - [`function autoLoadPSBT`](#function-autoloadpsbt)
13
+ - [`function psbtInputFormatter`](#function-psbtinputformatter)
14
+ - [`function psbtOutputFormatter`](#function-psbtoutputformatter)
15
+ - [`function translatePSBT`](#function-translatepsbt)
16
+ - [`function addSignaturesToPSBT`](#function-addsignaturestopsbt)
17
+ - [`function parseSignaturesFromPSBT`](#function-parsesignaturesfrompsbt)
18
+ - [`function parseSignatureArrayFromPSBT`](#function-parsesignaturearrayfrompsbt)
19
+ - [PSBTv2](#psbtv2)
20
+ - [Exports](#psbtv2-exports)
21
+ - [`class PsbtV2`](#class-psbtv2)
22
+ - [`get nLockTime`](#get-nlocktime)
23
+ - [`public dangerouslySetGlobalTxVersion1`](#public-dangerouslysetglobaltxversion1)
24
+ - [`public addGlobalXpub`](#public-addglobalxpub)
25
+ - [`public addInput`](#public-addinput)
26
+ - [`public addOutput`](#public-addoutput)
27
+ - [`public deleteInput`](#public-deleteinput)
28
+ - [`public deleteOutput`](#public-deleteoutput)
29
+ - [`public addPartialSig`](#public-addpartialsig)
30
+ - [`public removePartialSig`](#public-removepartialsig)
31
+ - [`static PsbtV2.FromV0`](#static-psbtv2fromv0)
32
+ - [`function getPsbtVersionNumber`](#function-getpsbtversionnumber)
33
+ - [`abstract class PsbtV2Maps`](#abstract-class-psbtv2maps)
34
+ - [`public copy`](#public-copy)
35
+ - [Concepts](#concepts)
36
+ - [The operator role saga](#the-operator-role-saga)
37
+ - [TODO](#todo)
38
+
39
+ ## PSBTv0
40
+
41
+ [BIP 174](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki)
42
+
43
+ ### Exports
44
+
45
+ #### `const PSBT_MAGIC_HEX`
46
+
47
+ A utility constant for hex encoded psbt magic bytes equal to `"70736274ff"`.
48
+
49
+ #### `const PSBT_MAGIC_B64`
50
+
51
+ A utility constant for base64 encoded psbt magic bytes equal to `"cHNidP8"`.
52
+
53
+ #### `const PSBT_MAGIC_BYTES`
54
+
55
+ A utility constant for `Buffer` instance of psbt magic bytes.
56
+
57
+ #### `function autoLoadPSBT`
58
+
59
+ Given a string, try to create a Psbt object based on MAGIC (hex or Base64).
60
+
61
+ #### `function psbtInputFormatter`
62
+
63
+ Take a `MultisigTransactionInput` and turn it into a `MultisigTransactionPSBTInput`.
64
+
65
+ #### `function psbtOutputFormatter`
66
+
67
+ Take a `MultisigTransactionOutput` and turn it into a `MultisigTransactionPSBTOutput`.
68
+
69
+ #### `function translatePSBT`
70
+
71
+ Translates a PSBT into inputs/outputs consumable by supported non-PSBT devices in the `@caravan/wallets` library.
72
+
73
+ #### `function addSignaturesToPSBT`
74
+
75
+ Given an unsigned PSBT, an array of signing public key(s) (one per input), an array of signature(s) (one per input) in the same order as the pubkey(s), adds partial signature object(s) to each input and returns the PSBT with partial signature(s) included.
76
+
77
+ #### `function parseSignaturesFromPSBT`
78
+
79
+ Extracts the signature(s) from a PSBT.
80
+
81
+ NOTE: there should be one signature per input, per signer.
82
+
83
+ ADDITIONAL NOTE: because of the restrictions we place on braids to march their multisig addresses (slices) forward at the _same_ index across each chain of the braid, we do not run into a possible collision with this data structure. BUT - to have this method accommodate the _most_ general form of signature parsing, it would be wise to wrap this one level deeper like:
84
+
85
+ ```
86
+ address: [pubkey : [signature(s)]]
87
+ ```
88
+
89
+ that way if your braid only advanced one chain's (member's) index so that a pubkey could be used in more than one address, everything would still function properly.
90
+
91
+ #### `function parseSignatureArrayFromPSBT`
92
+
93
+ Extracts signatures in order of inputs and returns as array (or array of arrays if multiple signature sets).
94
+
95
+ ## PSBTv2
96
+
97
+ The `psbtv2` module provides utilities for working with v2 PSBTs and transformations or conversions between v0 and v2.
98
+
99
+ ### Exports
100
+
101
+ #### `class PsbtV2`
102
+
103
+ An object class representing a PSBTv2 and its current state. While not yet complete, this class aims to also provide sufficient checking to ensure only valid PSBTs are represented and that only valid actions may be taken per operator role.
104
+
105
+ Getters and setters are provided for the keytypes defined in [BIP 174](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki) and [BIP 370](https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki). These getters/setters are named after the keytype in ALL_CAPS_SNAKE like `PsbtV2.PSBT_GLOBAL_VERSION`. Additional getters/setters may be listed in the following API documentation.
106
+
107
+ ##### `get nLockTime`
108
+
109
+ Returns the `nLockTime` field for the psbt as if it were a bitcoin transaction.
110
+
111
+ From BIP 370:
112
+
113
+ > The nLockTime field of a transaction is determined by inspecting the `PSBT_GLOBAL_FALLBACK_LOCKTIME` and each input's `PSBT_IN_REQUIRED_TIME_LOCKTIME` and `PSBT_IN_REQUIRED_HEIGHT_LOCKTIME` fields. If none of the inputs have a `PSBT_IN_REQUIRED_TIME_LOCKTIME` and \*(or) `PSBT_IN_REQUIRED_HEIGHT_LOCKTIME`, then `PSBT_GLOBAL_FALLBACK_LOCKTIME` must be used. If `PSBT_GLOBAL_FALLBACK_LOCKTIME` is not provided, then it is assumed to be 0. If one or more inputs have a `PSBT_IN_REQUIRED_TIME_LOCKTIME` or `PSBT_IN_REQUIRED_HEIGHT_LOCKTIME`, then the field chosen is the one which is supported by all of the inputs. This can be determined by looking at all of the inputs which specify a locktime in either of those fields, and choosing the field which is present in all of those inputs. Inputs not specifying a lock time field can take both types of lock times, as can those that specify both. The lock time chosen is then the maximum value of the chosen type of lock time. If a PSBT has both types of locktimes possible because one or more inputs specify both `PSBT_IN_REQUIRED_TIME_LOCKTIME` and `PSBT_IN_REQUIRED_HEIGHT_LOCKTIME`, then locktime determined by looking at the `PSBT_IN_REQUIRED_HEIGHT_LOCKTIME` fields of the inputs must be chosen.
114
+
115
+ ##### `public dangerouslySetGlobalTxVersion1`
116
+
117
+ A helper method for compatibility, but seems to no-longer be required.
118
+
119
+ ##### `public addGlobalXpub`
120
+
121
+ Adds an entry to the `PSBT_GLOBAL_XPUB` keytype given xpub, fingerprint, and derivation path.
122
+
123
+ ##### `public addInput`
124
+
125
+ Adds an input to the PSBT. This method validates inputs modifiable on `PSBT_GLOBAL_TX_MODIFIABLE`, but other validation is incomplete. Adds details to the input map keytypes if present and increments the global input count. These keytypes include:
126
+
127
+ - `PSBT_GLOBAL_INPUT_COUNT`
128
+ - `PSBT_IN_BIP32_DERIVATION`
129
+ - `PSBT_IN_NON_WITNESS_UTXO`
130
+ - `PSBT_IN_OUTPUT_INDEX`
131
+ - `PSBT_IN_PREVIOUS_TXID`
132
+ - `PSBT_IN_REDEEM_SCRIPT`
133
+ - `PSBT_IN_SEQUENCE`
134
+ - `PSBT_IN_WITNESS_SCRIPT`
135
+ - `PSBT_IN_WITNESS_UTXO`
136
+
137
+ ##### `public addOutput`
138
+
139
+ Adds an output to the PSBT. This method validates outputs modifiable on `PSBT_GLOBAL_TX_MODIFIABLE`, but other validation is incomplete. Adds details to the output map keytypes if present and increments the global output count. These keytypes include:
140
+
141
+ - `PSBT_GLOBAL_OUTPUT_COUNT`
142
+ - `PSBT_OUT_AMOUNT`
143
+ - `PSBT_OUT_BIP32_DERIVATION`
144
+ - `PSBT_OUT_REDEEM_SCRIPT`
145
+ - `PSBT_OUT_SCRIPT`
146
+ - `PSBT_OUT_WITNESS_SCRIPT`
147
+
148
+ ##### `public deleteInput`
149
+
150
+ Removes an input from the PSBT. This method validates inputs modifiable on `PSBT_GLOBAL_TX_MODIFIABLE`, but other validation is incomplete. All keytype references to the input index in the input map are removed. `PSBT_GLOBAL_INPUT_COUNT` is decremented.
151
+
152
+ ##### `public deleteOutput`
153
+
154
+ Removes an output from the PSBT. This method validates outputs modifiable on `PSBT_GLOBAL_TX_MODIFIABLE`, but other validation is incomplete. All keytype references to the output index in the output map are removed. `PSBT_GLOBAL_OUTPUT_COUNT` is decremented.
155
+
156
+ ##### `public addPartialSig`
157
+
158
+ Adds a signature for an input. Validates that the input is mapped and does not already have a signature for the pubkey. Also validates for sighash. Other validation is incomplete.
159
+
160
+ The Signer, when it creates a signature, must add the partial sig keypair to the psbt for the input which it is signing. In the case that a particular signer does not, this method can be used to add a signature to the psbt. This method assumes the Signer did the validation outlined in BIP0174 before creating a signature. See [BIP 174](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#signer).
161
+
162
+ ##### `public removePartialSig`
163
+
164
+ Removes all sigs for an input unless a pubkey is specified. Validates that the input exists. When providing a pubkey, this validates that a sig for the pubkey exists.
165
+
166
+ ##### `static PsbtV2.FromV0`
167
+
168
+ Attempts to return a `PsbtV2` by converting from a PSBTv0 string or Buffer
169
+
170
+ #### `function getPsbtVersionNumber`
171
+
172
+ Attempts to extract the version number as uint32LE from raw psbt regardless of psbt validity.
173
+
174
+ #### `abstract class PsbtV2Maps`
175
+
176
+ This is provided for utility for compatibility and to allow for mapping, map copying, and serialization operations for psbts. This does almost no validation, so do not rely on it for ensuring a valid psbt.
177
+
178
+ ##### `public copy`
179
+
180
+ Copies the maps in this PsbtV2 object to another PsbtV2 object.
181
+
182
+ NOTE: This copy method is made available to achieve parity with the PSBT api required by `ledger-bitcoin` for creating merklized PSBTs. HOWEVER, it is not recommended to use this when avoidable as copying maps bypasses the validation defined in the constructor, so it could create a psbtv2 in an invalid psbt state. PsbtV2.serialize is preferable whenever possible.
183
+
184
+ ## Concepts
185
+
186
+ ### The operator role saga
187
+
188
+ The PSBT is a resource which may be passed between several operators or services. It's best to look at the operator roles as stages of a saga. The next valid operator role(s) can be determined by the state of the PSBT. The actions allowed for a PSBT are determined by which operator role the PSBT can be now and which role it could be next. See the following blog article at Unchained for a more detailed illustration: [Operator roles: Life stages in the saga of a PSBT](https://unchained.com/blog/operator-roles-life-stages-in-the-saga-of-a-psbt/)
189
+
190
+ ### TODO
191
+
192
+ Work remains on PSBTv2 for determining the next valid operator role(s) and restricting actions based on PSBT state. In other words, if the state of the PSBT suggests the PSBT might be in role A and is ready for role B, it should only allow actions within the context of role A or B.
193
+
194
+ The following list is a non-comprehensive list of validation checks which must be performed:
195
+
196
+ - Check ready for Updater role on `addInput`, `addOutput`, `deleteInput`, and `deleteOuput`.
197
+ - Check ready for Signer role on `addPartialSig`.
@@ -0,0 +1,150 @@
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
10
+ */
11
+ type Key = string;
12
+ type Value = Buffer;
13
+ type NonUniqueKeyTypeValue = {
14
+ key: string;
15
+ value: string | null;
16
+ };
17
+ declare enum PsbtGlobalTxModifiableBits {
18
+ INPUTS = "INPUTS",// 0b00000001
19
+ OUTPUTS = "OUTPUTS",// 0b00000010
20
+ SIGHASH_SINGLE = "SIGHASH_SINGLE"
21
+ }
22
+ declare abstract class PsbtV2Maps {
23
+ protected globalMap: Map<Key, Value>;
24
+ protected inputMaps: Map<Key, Value>[];
25
+ protected outputMaps: Map<Key, Value>[];
26
+ constructor(psbt?: Buffer | string);
27
+ serialize(format?: "base64" | "hex"): string;
28
+ copy(to: PsbtV2): void;
29
+ private copyMaps;
30
+ private copyMap;
31
+ }
32
+ declare class PsbtV2 extends PsbtV2Maps {
33
+ constructor(psbt?: Buffer | string);
34
+ /**
35
+ * Globals Getters/Setters
36
+ */
37
+ get PSBT_GLOBAL_XPUB(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
38
+ get PSBT_GLOBAL_TX_VERSION(): number;
39
+ set PSBT_GLOBAL_TX_VERSION(version: number);
40
+ get PSBT_GLOBAL_FALLBACK_LOCKTIME(): number | null;
41
+ set PSBT_GLOBAL_FALLBACK_LOCKTIME(locktime: number | null);
42
+ get PSBT_GLOBAL_INPUT_COUNT(): number;
43
+ set PSBT_GLOBAL_INPUT_COUNT(count: number);
44
+ get PSBT_GLOBAL_OUTPUT_COUNT(): number;
45
+ set PSBT_GLOBAL_OUTPUT_COUNT(count: number);
46
+ get PSBT_GLOBAL_TX_MODIFIABLE(): PsbtGlobalTxModifiableBits[];
47
+ set PSBT_GLOBAL_TX_MODIFIABLE(modifiable: PsbtGlobalTxModifiableBits[]);
48
+ get PSBT_GLOBAL_VERSION(): number;
49
+ set PSBT_GLOBAL_VERSION(version: number);
50
+ get PSBT_GLOBAL_PROPRIETARY(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
51
+ /**
52
+ * Input Getters/Setters
53
+ */
54
+ get PSBT_IN_NON_WITNESS_UTXO(): (string | null)[];
55
+ get PSBT_IN_WITNESS_UTXO(): (string | null)[];
56
+ get PSBT_IN_PARTIAL_SIG(): NonUniqueKeyTypeValue[][];
57
+ get PSBT_IN_SIGHASH_TYPE(): (number | null)[];
58
+ get PSBT_IN_REDEEM_SCRIPT(): (string | null)[];
59
+ get PSBT_IN_WITNESS_SCRIPT(): (string | null)[];
60
+ get PSBT_IN_BIP32_DERIVATION(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
61
+ get PSBT_IN_FINAL_SCRIPTSIG(): (string | null)[];
62
+ get PSBT_IN_FINAL_SCRIPTWITNESS(): (string | null)[];
63
+ 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[][];
68
+ get PSBT_IN_PREVIOUS_TXID(): string[];
69
+ get PSBT_IN_OUTPUT_INDEX(): number[];
70
+ get PSBT_IN_SEQUENCE(): (number | null)[];
71
+ get PSBT_IN_REQUIRED_TIME_LOCKTIME(): (number | null)[];
72
+ get PSBT_IN_REQUIRED_HEIGHT_LOCKTIME(): (number | null)[];
73
+ 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[][];
77
+ get PSBT_IN_TAP_INTERNAL_KEY(): (string | null)[];
78
+ get PSBT_IN_TAP_MERKLE_ROOT(): (string | null)[];
79
+ get PSBT_IN_PROPRIETARY(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
80
+ /**
81
+ * Output Getters/Setters
82
+ */
83
+ get PSBT_OUT_REDEEM_SCRIPT(): (string | null)[];
84
+ get PSBT_OUT_WITNESS_SCRIPT(): (string | null)[];
85
+ get PSBT_OUT_BIP32_DERIVATION(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
86
+ get PSBT_OUT_AMOUNT(): bigint[];
87
+ get PSBT_OUT_SCRIPT(): string[];
88
+ get PSBT_OUT_TAP_INTERNAL_KEY(): (string | null)[];
89
+ get PSBT_OUT_TAP_TREE(): (string | null)[];
90
+ get PSBT_OUT_TAP_BIP32_DERIVATION(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
91
+ get PSBT_OUT_PROPRIETARY(): NonUniqueKeyTypeValue[] | NonUniqueKeyTypeValue[][];
92
+ /**
93
+ * Other Getters/Setters
94
+ */
95
+ get nLockTime(): number | null;
96
+ /**
97
+ * Creator/Constructor Methods
98
+ */
99
+ private create;
100
+ private validate;
101
+ dangerouslySetGlobalTxVersion1(): void;
102
+ addGlobalXpub(xpub: Buffer, fingerprint: Buffer, path: string): void;
103
+ addInput({ previousTxId, outputIndex, sequence, nonWitnessUtxo, witnessUtxo, redeemScript, witnessScript, bip32Derivation, }: {
104
+ previousTxId: Buffer | string;
105
+ outputIndex: number;
106
+ sequence?: number;
107
+ nonWitnessUtxo?: Buffer;
108
+ witnessUtxo?: {
109
+ amount: number;
110
+ script: Buffer;
111
+ };
112
+ redeemScript?: Buffer;
113
+ witnessScript?: Buffer;
114
+ bip32Derivation?: {
115
+ pubkey: Buffer;
116
+ masterFingerprint: Buffer;
117
+ path: string;
118
+ }[];
119
+ }): void;
120
+ addOutput({ amount, script, redeemScript, witnessScript, bip32Derivation, }: {
121
+ amount: number;
122
+ script: Buffer;
123
+ redeemScript?: Buffer;
124
+ witnessScript?: Buffer;
125
+ bip32Derivation?: {
126
+ pubkey: Buffer;
127
+ masterFingerprint: Buffer;
128
+ path: string;
129
+ }[];
130
+ }): void;
131
+ /**
132
+ * Updater/Signer Methods
133
+ */
134
+ deleteInput(index: number): void;
135
+ deleteOutput(index: number): void;
136
+ private isModifiable;
137
+ addPartialSig(inputIndex: number, pubkey: Buffer, sig: Buffer): void;
138
+ removePartialSig(inputIndex: number, pubkey?: Buffer): void;
139
+ private handleSighashType;
140
+ static FromV0(psbt: string | Buffer, allowTxnVersion1?: boolean): PsbtV2;
141
+ }
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
+
150
+ export { PsbtV2, getPsbtVersionNumber };