@bsv/sdk 1.0.19 → 1.0.20
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/cjs/package.json +1 -1
- package/dist/cjs/src/compat/HD.js +37 -19
- package/dist/cjs/src/compat/HD.js.map +1 -1
- package/dist/cjs/src/primitives/Signature.js +2 -2
- package/dist/cjs/src/primitives/Signature.js.map +1 -1
- package/dist/cjs/src/transaction/Transaction.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/compat/HD.js +37 -19
- package/dist/esm/src/compat/HD.js.map +1 -1
- package/dist/esm/src/primitives/Signature.js +2 -2
- package/dist/esm/src/primitives/Signature.js.map +1 -1
- package/dist/esm/src/transaction/Transaction.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/compat/HD.d.ts +30 -16
- package/dist/types/src/compat/HD.d.ts.map +1 -1
- package/dist/types/src/transaction/Transaction.d.ts +10 -4
- package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/docs/compat.md +42 -4
- package/docs/examples/EXAMPLE_HD_WALLETS.md +4 -4
- package/docs/transaction.md +31 -0
- package/package.json +1 -1
- package/src/compat/HD.ts +39 -19
- package/src/compat/__tests/HD.test.ts +51 -52
- package/src/primitives/TransactionSignature.ts +1 -1
- package/src/script/ScriptTemplate.ts +5 -5
- package/src/transaction/Transaction.ts +18 -5
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import HD from '../../../dist/cjs/src/compat/HD'
|
|
2
|
-
import { fromBase58Check,
|
|
2
|
+
import { fromBase58Check, toArray, toHex } from '../../../dist/cjs/src/primitives/utils'
|
|
3
3
|
|
|
4
4
|
describe('HD', () => {
|
|
5
5
|
it('should satisfy these basic API features', () => {
|
|
@@ -64,232 +64,231 @@ describe('HD', () => {
|
|
|
64
64
|
let bip32
|
|
65
65
|
bip32 = new HD()
|
|
66
66
|
expect(bip32).toBeDefined()
|
|
67
|
-
expect(
|
|
68
|
-
expect(
|
|
69
|
-
.fromString(new HD().fromString(vector1mPrivate).toString())
|
|
67
|
+
expect(HD.fromString(vector1mPrivate).toString()).toEqual(vector1mPrivate)
|
|
68
|
+
expect(HD.fromString(HD.fromString(vector1mPrivate).toString())
|
|
70
69
|
.toString())
|
|
71
70
|
.toEqual(vector1mPrivate)
|
|
72
71
|
})
|
|
73
72
|
|
|
74
73
|
it('should initialize test vector 1 from the extended public key', () => {
|
|
75
|
-
const bip32 =
|
|
74
|
+
const bip32 = HD.fromString(vector1mPublic)
|
|
76
75
|
expect(bip32).toBeDefined()
|
|
77
76
|
})
|
|
78
77
|
|
|
79
78
|
it('should initialize test vector 1 from the extended private key', () => {
|
|
80
|
-
const bip32 =
|
|
79
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
81
80
|
expect(bip32).toBeDefined()
|
|
82
81
|
})
|
|
83
82
|
|
|
84
83
|
it('should get the extended public key from the extended private key for test vector 1', () => {
|
|
85
|
-
const bip32 =
|
|
84
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
86
85
|
expect(bip32.toPublic().toString()).toEqual(vector1mPublic)
|
|
87
86
|
})
|
|
88
87
|
|
|
89
88
|
it("should get m/0' ext. private key from test vector 1", () => {
|
|
90
|
-
const bip32 =
|
|
89
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
91
90
|
const child = bip32.derive("m/0'")
|
|
92
91
|
expect(child).toBeDefined()
|
|
93
92
|
expect(child.toString()).toEqual(vector1m0hPrivate)
|
|
94
93
|
})
|
|
95
94
|
|
|
96
95
|
it("should get m/0' ext. public key from test vector 1", () => {
|
|
97
|
-
const bip32 =
|
|
96
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
98
97
|
const child = bip32.derive("m/0'")
|
|
99
98
|
expect(child).toBeDefined()
|
|
100
99
|
expect(child.toPublic().toString()).toEqual(vector1m0hPublic)
|
|
101
100
|
})
|
|
102
101
|
|
|
103
102
|
it("should get m/0'/1 ext. private key from test vector 1", () => {
|
|
104
|
-
const bip32 =
|
|
103
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
105
104
|
const child = bip32.derive("m/0'/1")
|
|
106
105
|
expect(child).toBeDefined()
|
|
107
106
|
expect(child.toString()).toEqual(vector1m0h1Private)
|
|
108
107
|
})
|
|
109
108
|
|
|
110
109
|
it("should get m/0'/1 ext. public key from test vector 1", () => {
|
|
111
|
-
const bip32 =
|
|
110
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
112
111
|
const child = bip32.derive("m/0'/1")
|
|
113
112
|
expect(child).toBeDefined()
|
|
114
113
|
expect(child.toPublic().toString()).toEqual(vector1m0h1Public)
|
|
115
114
|
})
|
|
116
115
|
|
|
117
116
|
it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", () => {
|
|
118
|
-
const bip32 =
|
|
117
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
119
118
|
const child = bip32.derive("m/0'")
|
|
120
|
-
const childPub =
|
|
119
|
+
const childPub = HD.fromString(child.toPublic().toString())
|
|
121
120
|
const child2 = childPub.derive('m/1')
|
|
122
121
|
expect(child2).toBeDefined()
|
|
123
122
|
expect(child2.toPublic().toString()).toEqual(vector1m0h1Public)
|
|
124
123
|
})
|
|
125
124
|
|
|
126
125
|
it("should get m/0'/1/2h ext. private key from test vector 1", () => {
|
|
127
|
-
const bip32 =
|
|
126
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
128
127
|
const child = bip32.derive("m/0'/1/2'")
|
|
129
128
|
expect(child).toBeDefined()
|
|
130
129
|
expect(child.toString()).toEqual(vector1m0h12hPrivate)
|
|
131
130
|
})
|
|
132
131
|
|
|
133
132
|
it("should get m/0'/1/2h ext. public key from test vector 1", () => {
|
|
134
|
-
const bip32 =
|
|
133
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
135
134
|
const child = bip32.derive("m/0'/1/2'")
|
|
136
135
|
expect(child).toBeDefined()
|
|
137
136
|
expect(child.toPublic().toString()).toEqual(vector1m0h12hPublic)
|
|
138
137
|
})
|
|
139
138
|
|
|
140
139
|
it("should get m/0'/1/2h/2 ext. private key from test vector 1", () => {
|
|
141
|
-
const bip32 =
|
|
140
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
142
141
|
const child = bip32.derive("m/0'/1/2'/2")
|
|
143
142
|
expect(child).toBeDefined()
|
|
144
143
|
expect(child.toString()).toEqual(vector1m0h12h2Private)
|
|
145
144
|
})
|
|
146
145
|
|
|
147
146
|
it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", () => {
|
|
148
|
-
const bip32 =
|
|
147
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
149
148
|
const child = bip32.derive("m/0'/1/2'")
|
|
150
|
-
const childPub =
|
|
149
|
+
const childPub = HD.fromString(child.toPublic().toString())
|
|
151
150
|
const child2 = childPub.derive('m/2')
|
|
152
151
|
expect(child2).toBeDefined()
|
|
153
152
|
expect(child2.toPublic().toString()).toEqual(vector1m0h12h2Public)
|
|
154
153
|
})
|
|
155
154
|
|
|
156
155
|
it("should get m/0'/1/2h/2 ext. public key from test vector 1", () => {
|
|
157
|
-
const bip32 =
|
|
156
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
158
157
|
const child = bip32.derive("m/0'/1/2'/2")
|
|
159
158
|
expect(child).toBeDefined()
|
|
160
159
|
expect(child.toPublic().toString()).toEqual(vector1m0h12h2Public)
|
|
161
160
|
})
|
|
162
161
|
|
|
163
162
|
it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", () => {
|
|
164
|
-
const bip32 =
|
|
163
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
165
164
|
const child = bip32.derive("m/0'/1/2'/2/1000000000")
|
|
166
165
|
expect(child).toBeDefined()
|
|
167
166
|
expect(child.toString()).toEqual(vector1m0h12h21000000000Private)
|
|
168
167
|
})
|
|
169
168
|
|
|
170
169
|
it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", () => {
|
|
171
|
-
const bip32 =
|
|
170
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
172
171
|
const child = bip32.derive("m/0'/1/2'/2/1000000000")
|
|
173
172
|
expect(child).toBeDefined()
|
|
174
173
|
expect(child.toPublic().toString()).toEqual(vector1m0h12h21000000000Public)
|
|
175
174
|
})
|
|
176
175
|
|
|
177
176
|
it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", () => {
|
|
178
|
-
const bip32 =
|
|
177
|
+
const bip32 = HD.fromString(vector1mPrivate)
|
|
179
178
|
const child = bip32.derive("m/0'/1/2'/2")
|
|
180
|
-
const childPub =
|
|
179
|
+
const childPub = HD.fromString(child.toPublic().toString())
|
|
181
180
|
const child2 = childPub.derive('m/1000000000')
|
|
182
181
|
expect(child2).toBeDefined()
|
|
183
182
|
expect(child2.toPublic().toString()).toEqual(vector1m0h12h21000000000Public)
|
|
184
183
|
})
|
|
185
184
|
|
|
186
185
|
it('should initialize test vector 2 from the extended public key', () => {
|
|
187
|
-
const bip32 =
|
|
186
|
+
const bip32 = HD.fromString(vector2mPublic)
|
|
188
187
|
expect(bip32).toBeDefined()
|
|
189
188
|
})
|
|
190
189
|
|
|
191
190
|
it('should initialize test vector 2 from the extended private key', () => {
|
|
192
|
-
const bip32 =
|
|
191
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
193
192
|
expect(bip32).toBeDefined()
|
|
194
193
|
})
|
|
195
194
|
|
|
196
195
|
it('should get the extended public key from the extended private key for test vector 2', () => {
|
|
197
|
-
const bip32 =
|
|
196
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
198
197
|
expect(bip32.toPublic().toString()).toEqual(vector2mPublic)
|
|
199
198
|
})
|
|
200
199
|
|
|
201
200
|
it('should get m/0 ext. private key from test vector 2', () => {
|
|
202
|
-
const bip32 =
|
|
201
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
203
202
|
const child = bip32.derive('m/0')
|
|
204
203
|
expect(child).toBeDefined()
|
|
205
204
|
expect(child.toString()).toEqual(vector2m0Private)
|
|
206
205
|
})
|
|
207
206
|
|
|
208
207
|
it('should get m/0 ext. public key from test vector 2', () => {
|
|
209
|
-
const bip32 =
|
|
208
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
210
209
|
const child = bip32.derive('m/0')
|
|
211
210
|
expect(child).toBeDefined()
|
|
212
211
|
expect(child.toPublic().toString()).toEqual(vector2m0Public)
|
|
213
212
|
})
|
|
214
213
|
|
|
215
214
|
it('should get m/0 ext. public key from m public key from test vector 2', () => {
|
|
216
|
-
const bip32 =
|
|
215
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
217
216
|
const child = bip32.derive('m')
|
|
218
|
-
const childPub =
|
|
217
|
+
const childPub = HD.fromString(child.toPublic().toString())
|
|
219
218
|
const child2 = childPub.derive('m/0')
|
|
220
219
|
expect(child2).toBeDefined()
|
|
221
220
|
expect(child2.toPublic().toString()).toEqual(vector2m0Public)
|
|
222
221
|
})
|
|
223
222
|
|
|
224
223
|
it('should get m/0/2147483647h ext. private key from test vector 2', () => {
|
|
225
|
-
const bip32 =
|
|
224
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
226
225
|
const child = bip32.derive("m/0/2147483647'")
|
|
227
226
|
expect(child).toBeDefined()
|
|
228
227
|
expect(child.toString()).toEqual(vector2m02147483647hPrivate)
|
|
229
228
|
})
|
|
230
229
|
|
|
231
230
|
it('should get m/0/2147483647h ext. public key from test vector 2', () => {
|
|
232
|
-
const bip32 =
|
|
231
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
233
232
|
const child = bip32.derive("m/0/2147483647'")
|
|
234
233
|
expect(child).toBeDefined()
|
|
235
234
|
expect(child.toPublic().toString()).toEqual(vector2m02147483647hPublic)
|
|
236
235
|
})
|
|
237
236
|
|
|
238
237
|
it('should get m/0/2147483647h/1 ext. private key from test vector 2', () => {
|
|
239
|
-
const bip32 =
|
|
238
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
240
239
|
const child = bip32.derive("m/0/2147483647'/1")
|
|
241
240
|
expect(child).toBeDefined()
|
|
242
241
|
expect(child.toString()).toEqual(vector2m02147483647h1Private)
|
|
243
242
|
})
|
|
244
243
|
|
|
245
244
|
it('should get m/0/2147483647h/1 ext. public key from test vector 2', () => {
|
|
246
|
-
const bip32 =
|
|
245
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
247
246
|
const child = bip32.derive("m/0/2147483647'/1")
|
|
248
247
|
expect(child).toBeDefined()
|
|
249
248
|
expect(child.toPublic().toString()).toEqual(vector2m02147483647h1Public)
|
|
250
249
|
})
|
|
251
250
|
|
|
252
251
|
it('should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2', () => {
|
|
253
|
-
const bip32 =
|
|
252
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
254
253
|
const child = bip32.derive("m/0/2147483647'")
|
|
255
|
-
const childPub =
|
|
254
|
+
const childPub = HD.fromString(child.toPublic().toString())
|
|
256
255
|
const child2 = childPub.derive('m/1')
|
|
257
256
|
expect(child2).toBeDefined()
|
|
258
257
|
expect(child2.toPublic().toString()).toEqual(vector2m02147483647h1Public)
|
|
259
258
|
})
|
|
260
259
|
|
|
261
260
|
it('should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2', () => {
|
|
262
|
-
const bip32 =
|
|
261
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
263
262
|
const child = bip32.derive("m/0/2147483647'/1/2147483646'")
|
|
264
263
|
expect(child).toBeDefined()
|
|
265
264
|
expect(child.toString()).toEqual(vector2m02147483647h12147483646hPrivate)
|
|
266
265
|
})
|
|
267
266
|
|
|
268
267
|
it('should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2', () => {
|
|
269
|
-
const bip32 =
|
|
268
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
270
269
|
const child = bip32.derive("m/0/2147483647'/1/2147483646'")
|
|
271
270
|
expect(child).toBeDefined()
|
|
272
271
|
expect(child.toPublic().toString()).toEqual(vector2m02147483647h12147483646hPublic)
|
|
273
272
|
})
|
|
274
273
|
|
|
275
274
|
it('should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2', () => {
|
|
276
|
-
const bip32 =
|
|
275
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
277
276
|
const child = bip32.derive("m/0/2147483647'/1/2147483646'/2")
|
|
278
277
|
expect(child).toBeDefined()
|
|
279
278
|
expect(child.toString()).toEqual(vector2m02147483647h12147483646h2Private)
|
|
280
279
|
})
|
|
281
280
|
|
|
282
281
|
it('should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2', () => {
|
|
283
|
-
const bip32 =
|
|
282
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
284
283
|
const child = bip32.derive("m/0/2147483647'/1/2147483646'/2")
|
|
285
284
|
expect(child).toBeDefined()
|
|
286
285
|
expect(child.toPublic().toString()).toEqual(vector2m02147483647h12147483646h2Public)
|
|
287
286
|
})
|
|
288
287
|
|
|
289
288
|
it('should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2', () => {
|
|
290
|
-
const bip32 =
|
|
289
|
+
const bip32 = HD.fromString(vector2mPrivate)
|
|
291
290
|
const child = bip32.derive("m/0/2147483647'/1/2147483646'")
|
|
292
|
-
const childPub =
|
|
291
|
+
const childPub = HD.fromString(child.toPublic().toString())
|
|
293
292
|
const child2 = childPub.derive('m/2')
|
|
294
293
|
expect(child2).toBeDefined()
|
|
295
294
|
expect(child2.toPublic().toString()).toEqual(vector2m02147483647h12147483646h2Public)
|
|
@@ -297,8 +296,8 @@ describe('HD', () => {
|
|
|
297
296
|
|
|
298
297
|
describe('#fromRandom', () => {
|
|
299
298
|
it('should not return the same one twice', () => {
|
|
300
|
-
const bip32a =
|
|
301
|
-
const bip32b =
|
|
299
|
+
const bip32a = HD.fromRandom()
|
|
300
|
+
const bip32b = HD.fromRandom()
|
|
302
301
|
expect(bip32a.toString()).not.toEqual(bip32b.toString())
|
|
303
302
|
})
|
|
304
303
|
})
|
|
@@ -314,7 +313,7 @@ describe('HD', () => {
|
|
|
314
313
|
describe('#fromSeed', () => {
|
|
315
314
|
it('should initialize a new Bip32 correctly from test vector 1 seed', () => {
|
|
316
315
|
const hex = vector1master
|
|
317
|
-
const bip32 =
|
|
316
|
+
const bip32 = HD.fromSeed(toArray(hex, 'hex'))
|
|
318
317
|
expect(bip32).toBeDefined()
|
|
319
318
|
expect(bip32.toString()).toEqual(vector1mPrivate)
|
|
320
319
|
expect(bip32.toPublic().toString()).toEqual(vector1mPublic)
|
|
@@ -322,7 +321,7 @@ describe('HD', () => {
|
|
|
322
321
|
|
|
323
322
|
it('should initialize a new Bip32 correctly from test vector 2 seed', () => {
|
|
324
323
|
const hex = vector2master
|
|
325
|
-
const bip32 =
|
|
324
|
+
const bip32 = HD.fromSeed(toArray(hex, 'hex'))
|
|
326
325
|
expect(bip32).toBeDefined()
|
|
327
326
|
expect(bip32.toString()).toEqual(vector2mPrivate)
|
|
328
327
|
expect(bip32.toPublic().toString()).toEqual(vector2mPublic)
|
|
@@ -352,12 +351,12 @@ describe('HD', () => {
|
|
|
352
351
|
const str =
|
|
353
352
|
'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
|
|
354
353
|
const buf = fromBase58Check(str)
|
|
355
|
-
let bip32 =
|
|
354
|
+
let bip32 = HD.fromBinary([...buf.prefix, ...buf.data])
|
|
356
355
|
expect(bip32).toBeDefined()
|
|
357
356
|
expect(bip32.toString()).toEqual(str)
|
|
358
357
|
bip32 = bip32.toPublic()
|
|
359
358
|
const xpub = bip32.toString()
|
|
360
|
-
bip32 =
|
|
359
|
+
bip32 = HD.fromBinary(bip32.toBinary())
|
|
361
360
|
expect(bip32.toString()).toEqual(xpub)
|
|
362
361
|
})
|
|
363
362
|
})
|
|
@@ -367,7 +366,7 @@ describe('HD', () => {
|
|
|
367
366
|
const str =
|
|
368
367
|
'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
|
|
369
368
|
const buf = fromBase58Check(str)
|
|
370
|
-
const bip32 =
|
|
369
|
+
const bip32 = HD.fromString(str)
|
|
371
370
|
expect(toHex(bip32.toBinary())).toEqual(toHex([...buf.prefix, ...buf.data]))
|
|
372
371
|
})
|
|
373
372
|
})
|
|
@@ -376,7 +375,7 @@ describe('HD', () => {
|
|
|
376
375
|
it('should make a bip32 from a string', () => {
|
|
377
376
|
const str =
|
|
378
377
|
'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
|
|
379
|
-
const bip32 =
|
|
378
|
+
const bip32 = HD.fromString(str)
|
|
380
379
|
expect(bip32).toBeDefined()
|
|
381
380
|
expect(bip32.toString()).toEqual(str)
|
|
382
381
|
})
|
|
@@ -396,10 +395,10 @@ describe('HD', () => {
|
|
|
396
395
|
|
|
397
396
|
describe('#isPrivate', () => {
|
|
398
397
|
it('should know if this bip32 is private', () => {
|
|
399
|
-
const bip32priv =
|
|
398
|
+
const bip32priv = HD.fromRandom()
|
|
400
399
|
const bip32pub = bip32priv.toPublic()
|
|
401
400
|
expect(bip32priv.isPrivate()).toEqual(true)
|
|
402
401
|
expect(bip32pub.isPrivate()).toEqual(false)
|
|
403
402
|
})
|
|
404
403
|
})
|
|
405
|
-
})
|
|
404
|
+
})
|
|
@@ -42,7 +42,7 @@ export default class TransactionSignature extends Signature {
|
|
|
42
42
|
if (typeof input.sourceTransaction === 'undefined') {
|
|
43
43
|
writer.writeReverse(toArray(input.sourceTXID, 'hex'))
|
|
44
44
|
} else {
|
|
45
|
-
writer.writeReverse(input.sourceTransaction.id()
|
|
45
|
+
writer.writeReverse(input.sourceTransaction.id())
|
|
46
46
|
}
|
|
47
47
|
writer.writeUInt32LE(input.sourceOutputIndex)
|
|
48
48
|
}
|
|
@@ -28,9 +28,9 @@ export default interface ScriptTemplate {
|
|
|
28
28
|
* @returns {Object} - An object containing the `sign` and `estimateLength` functions.
|
|
29
29
|
*/
|
|
30
30
|
unlock: (...params: any) =>
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
{
|
|
32
|
+
sign: (tx: Transaction, inputIndex: number) =>
|
|
33
|
+
Promise<UnlockingScript>
|
|
34
|
+
estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>
|
|
35
|
+
}
|
|
36
36
|
}
|
|
@@ -83,7 +83,7 @@ export default class Transaction {
|
|
|
83
83
|
for (let i = 0; i < numberOfTransactions; i++) {
|
|
84
84
|
const tx = Transaction.fromReader(reader)
|
|
85
85
|
const obj: { pathIndex?: number, tx: Transaction } = { tx }
|
|
86
|
-
const txid = tx.id('hex')
|
|
86
|
+
const txid = tx.id('hex')
|
|
87
87
|
if (i + 1 === numberOfTransactions) { // The last tXID is stored for later
|
|
88
88
|
lastTXID = txid
|
|
89
89
|
}
|
|
@@ -461,6 +461,19 @@ export default class Transaction {
|
|
|
461
461
|
}
|
|
462
462
|
}
|
|
463
463
|
|
|
464
|
+
/**
|
|
465
|
+
* Calculates the transaction's ID in binary array.
|
|
466
|
+
*
|
|
467
|
+
* @returns {number[]} - The ID of the transaction in the binary array format.
|
|
468
|
+
*/
|
|
469
|
+
id (): number[]
|
|
470
|
+
/**
|
|
471
|
+
* Calculates the transaction's ID in hexadecimal format.
|
|
472
|
+
*
|
|
473
|
+
* @param {'hex'} enc - The encoding to use for the ID. If 'hex', returns a hexadecimal string.
|
|
474
|
+
* @returns {string} - The ID of the transaction in the hex format.
|
|
475
|
+
*/
|
|
476
|
+
id (enc: 'hex'): string
|
|
464
477
|
/**
|
|
465
478
|
* Calculates the transaction's ID.
|
|
466
479
|
*
|
|
@@ -487,7 +500,7 @@ export default class Transaction {
|
|
|
487
500
|
// If the transaction has a valid merkle path, verification is complete.
|
|
488
501
|
if (typeof this.merklePath === 'object' && chainTracker !== 'scripts only') {
|
|
489
502
|
const proofValid = await this.merklePath.verify(
|
|
490
|
-
this.id('hex')
|
|
503
|
+
this.id('hex'),
|
|
491
504
|
chainTracker
|
|
492
505
|
)
|
|
493
506
|
// Note that if the proof is provided but not valid, the transaction could still be verified by proving all inputs (if they are available) and checking the associated spends.
|
|
@@ -502,10 +515,10 @@ export default class Transaction {
|
|
|
502
515
|
for (let i = 0; i < this.inputs.length; i++) {
|
|
503
516
|
const input = this.inputs[i]
|
|
504
517
|
if (typeof input.sourceTransaction !== 'object') {
|
|
505
|
-
throw new Error(`Verification failed because the input at index ${i} of transaction ${this.id('hex')
|
|
518
|
+
throw new Error(`Verification failed because the input at index ${i} of transaction ${this.id('hex')} is missing an associated source transaction. This source transaction is required for transaction verification because there is no merkle proof for the transaction spending a UTXO it contains.`)
|
|
506
519
|
}
|
|
507
520
|
if (typeof input.unlockingScript !== 'object') {
|
|
508
|
-
throw new Error(`Verification failed because the input at index ${i} of transaction ${this.id('hex')
|
|
521
|
+
throw new Error(`Verification failed because the input at index ${i} of transaction ${this.id('hex')} is missing an associated unlocking script. This script is required for transaction verification because there is no merkle proof for the transaction spending the UTXO.`)
|
|
509
522
|
}
|
|
510
523
|
const sourceOutput = input.sourceTransaction.outputs[input.sourceOutputIndex]
|
|
511
524
|
inputTotal += sourceOutput.satoshis
|
|
@@ -516,7 +529,7 @@ export default class Transaction {
|
|
|
516
529
|
const otherInputs = [...this.inputs]
|
|
517
530
|
otherInputs.splice(i, 1)
|
|
518
531
|
const spend = new Spend({
|
|
519
|
-
sourceTXID: input.sourceTransaction.id('hex')
|
|
532
|
+
sourceTXID: input.sourceTransaction.id('hex'),
|
|
520
533
|
sourceOutputIndex: input.sourceOutputIndex,
|
|
521
534
|
lockingScript: sourceOutput.lockingScript,
|
|
522
535
|
sourceSatoshis: sourceOutput.satoshis,
|