@bsv/sdk 1.0.19 → 1.0.21

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 (38) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/compat/HD.js +37 -19
  3. package/dist/cjs/src/compat/HD.js.map +1 -1
  4. package/dist/cjs/src/primitives/Signature.js +2 -2
  5. package/dist/cjs/src/primitives/Signature.js.map +1 -1
  6. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
  7. package/dist/cjs/src/script/Spend.js +1 -0
  8. package/dist/cjs/src/script/Spend.js.map +1 -1
  9. package/dist/cjs/src/transaction/Transaction.js.map +1 -1
  10. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  11. package/dist/esm/src/compat/HD.js +37 -19
  12. package/dist/esm/src/compat/HD.js.map +1 -1
  13. package/dist/esm/src/primitives/Signature.js +2 -2
  14. package/dist/esm/src/primitives/Signature.js.map +1 -1
  15. package/dist/esm/src/primitives/TransactionSignature.js.map +1 -1
  16. package/dist/esm/src/script/Spend.js +1 -0
  17. package/dist/esm/src/script/Spend.js.map +1 -1
  18. package/dist/esm/src/transaction/Transaction.js.map +1 -1
  19. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  20. package/dist/types/src/compat/HD.d.ts +30 -16
  21. package/dist/types/src/compat/HD.d.ts.map +1 -1
  22. package/dist/types/src/script/ScriptTemplate.d.ts.map +1 -1
  23. package/dist/types/src/script/Spend.d.ts.map +1 -1
  24. package/dist/types/src/transaction/Transaction.d.ts +10 -4
  25. package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
  26. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  27. package/docs/compat.md +42 -4
  28. package/docs/concepts/HOW_TX.md +4 -4
  29. package/docs/examples/EXAMPLE_HD_WALLETS.md +4 -4
  30. package/docs/transaction.md +31 -0
  31. package/package.json +1 -1
  32. package/src/compat/HD.ts +39 -19
  33. package/src/compat/__tests/HD.test.ts +51 -52
  34. package/src/primitives/TransactionSignature.ts +1 -1
  35. package/src/script/ScriptTemplate.ts +5 -5
  36. package/src/script/Spend.ts +1 -0
  37. package/src/script/__tests/spend.valid.vectors.ts +5 -0
  38. package/src/transaction/Transaction.ts +18 -5
@@ -442,6 +442,8 @@ export default class Transaction {
442
442
  toHex(): string
443
443
  toHexBEEF(): string
444
444
  hash(enc?: "hex"): number[] | string
445
+ id(): number[];
446
+ id(enc: "hex"): string;
445
447
  id(enc?: "hex"): number[] | string
446
448
  async verify(chainTracker: ChainTracker | "scripts only"): Promise<boolean>
447
449
  toBEEF(): number[]
@@ -605,6 +607,35 @@ Argument Details
605
607
 
606
608
  #### Method id
607
609
 
610
+ Calculates the transaction's ID in binary array.
611
+
612
+ ```ts
613
+ id(): number[]
614
+ ```
615
+
616
+ Returns
617
+
618
+ - The ID of the transaction in the binary array format.
619
+
620
+ #### Method id
621
+
622
+ Calculates the transaction's ID in hexadecimal format.
623
+
624
+ ```ts
625
+ id(enc: "hex"): string
626
+ ```
627
+
628
+ Returns
629
+
630
+ - The ID of the transaction in the hex format.
631
+
632
+ Argument Details
633
+
634
+ + **enc**
635
+ + The encoding to use for the ID. If 'hex', returns a hexadecimal string.
636
+
637
+ #### Method id
638
+
608
639
  Calculates the transaction's ID.
609
640
 
610
641
  ```ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
package/src/compat/HD.ts CHANGED
@@ -84,6 +84,16 @@ export default class HD {
84
84
  return new this().fromRandom()
85
85
  }
86
86
 
87
+ /**
88
+ * Initializes the HD wallet from a given base58 encoded string.
89
+ * This method decodes a provided string to set up the HD wallet's properties.
90
+ * @param str - A base58 encoded string representing the wallet.
91
+ * @returns {HD} The new instance with properties set from the string.
92
+ */
93
+ public static fromString (str: string): HD {
94
+ return new this().fromString(str)
95
+ }
96
+
87
97
  /**
88
98
  * Initializes the HD wallet from a given base58 encoded string.
89
99
  * This method decodes a provided string to set up the HD wallet's properties.
@@ -96,13 +106,13 @@ export default class HD {
96
106
  }
97
107
 
98
108
  /**
99
- * Converts the HD wallet to a base58 encoded string.
100
- * This method provides a string representation of the HD wallet's current state.
101
- * @returns {string} A base58 encoded string of the HD wallet.
102
- */
103
- public toString (): string {
104
- const bin = this.toBinary()
105
- return toBase58Check(bin, [])
109
+ * Initializes the HD wallet from a seed.
110
+ * This method generates keys and other properties from a given seed, conforming to the BIP32 specification.
111
+ * @param bytes - An array of bytes representing the seed.
112
+ * @returns {HD} The current instance with properties set from the seed.
113
+ */
114
+ public static fromSeed (bytes: number[]): HD {
115
+ return new this().fromSeed(bytes)
106
116
  }
107
117
 
108
118
  /**
@@ -132,21 +142,21 @@ export default class HD {
132
142
  }
133
143
 
134
144
  /**
135
- * Initializes the HD wallet from a seed.
136
- * This method generates keys and other properties from a given seed, conforming to the BIP32 specification.
137
- * @param bytes - An array of bytes representing the seed.
138
- * @returns {HD} The current instance with properties set from the seed.
139
- */
140
- public static fromSeed (bytes: number[]): HD {
141
- return new this().fromSeed(bytes)
145
+ * Initializes the HD wallet from a binary buffer.
146
+ * Parses a binary buffer to set up the wallet's properties.
147
+ * @param buf - A buffer containing the wallet data.
148
+ * @returns {HD} The new instance with properties set from the buffer.
149
+ */
150
+ public static fromBinary (buf: number[]): HD {
151
+ return new this().fromBinary(buf)
142
152
  }
143
153
 
144
154
  /**
145
- * Initializes the HD wallet from a binary buffer.
146
- * Parses a binary buffer to set up the wallet's properties.
147
- * @param buf - A buffer containing the wallet data.
148
- * @returns {HD} The current instance with properties set from the buffer.
149
- */
155
+ * Initializes the HD wallet from a binary buffer.
156
+ * Parses a binary buffer to set up the wallet's properties.
157
+ * @param buf - A buffer containing the wallet data.
158
+ * @returns {HD} The current instance with properties set from the buffer.
159
+ */
150
160
  public fromBinary (buf: number[]): this {
151
161
  // Both pub and private extended keys are 78 buf
152
162
  if (buf.length !== 78) {
@@ -176,6 +186,16 @@ export default class HD {
176
186
  return this
177
187
  }
178
188
 
189
+ /**
190
+ * Converts the HD wallet to a base58 encoded string.
191
+ * This method provides a string representation of the HD wallet's current state.
192
+ * @returns {string} A base58 encoded string of the HD wallet.
193
+ */
194
+ public toString (): string {
195
+ const bin = this.toBinary()
196
+ return toBase58Check(bin, [])
197
+ }
198
+
179
199
  /**
180
200
  * Derives a child HD wallet based on a given path.
181
201
  * The path specifies the hierarchy of the child key to be derived.
@@ -1,5 +1,5 @@
1
1
  import HD from '../../../dist/cjs/src/compat/HD'
2
- import { fromBase58Check, toBase58Check, toArray, toHex } from '../../../dist/cjs/src/primitives/utils'
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(new HD().fromString(vector1mPrivate).toString()).toEqual(vector1mPrivate)
68
- expect(new HD()
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 = new HD().fromString(vector1mPublic)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
117
+ const bip32 = HD.fromString(vector1mPrivate)
119
118
  const child = bip32.derive("m/0'")
120
- const childPub = new HD().fromString(child.toPublic().toString())
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
147
+ const bip32 = HD.fromString(vector1mPrivate)
149
148
  const child = bip32.derive("m/0'/1/2'")
150
- const childPub = new HD().fromString(child.toPublic().toString())
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
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 = new HD().fromString(vector1mPrivate)
177
+ const bip32 = HD.fromString(vector1mPrivate)
179
178
  const child = bip32.derive("m/0'/1/2'/2")
180
- const childPub = new HD().fromString(child.toPublic().toString())
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 = new HD().fromString(vector2mPublic)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
215
+ const bip32 = HD.fromString(vector2mPrivate)
217
216
  const child = bip32.derive('m')
218
- const childPub = new HD().fromString(child.toPublic().toString())
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
252
+ const bip32 = HD.fromString(vector2mPrivate)
254
253
  const child = bip32.derive("m/0/2147483647'")
255
- const childPub = new HD().fromString(child.toPublic().toString())
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
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 = new HD().fromString(vector2mPrivate)
289
+ const bip32 = HD.fromString(vector2mPrivate)
291
290
  const child = bip32.derive("m/0/2147483647'/1/2147483646'")
292
- const childPub = new HD().fromString(child.toPublic().toString())
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 = new HD().fromRandom()
301
- const bip32b = new HD().fromRandom()
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 = new HD().fromSeed(toArray(hex, 'hex'))
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 = new HD().fromSeed(toArray(hex, 'hex'))
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 = new HD().fromBinary([...buf.prefix, ...buf.data])
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 = new HD().fromBinary(bip32.toBinary())
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 = new HD().fromString(str)
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 = new HD().fromString(str)
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 = new HD().fromRandom()
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() as number[])
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
- sign: (tx: Transaction, inputIndex: number) =>
33
- Promise<UnlockingScript>
34
- estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>
35
- }
31
+ {
32
+ sign: (tx: Transaction, inputIndex: number) =>
33
+ Promise<UnlockingScript>
34
+ estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>
35
+ }
36
36
  }
@@ -583,6 +583,7 @@ export default class Spend {
583
583
  } else {
584
584
  this.programCounter = this.lockingScript.chunks.length
585
585
  }
586
+ this.ifStack = []
586
587
  break
587
588
 
588
589
  case OP.OP_TOALTSTACK:
@@ -2286,5 +2286,10 @@ export default [
2286
2286
  "51",
2287
2287
  "000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af000000af",
2288
2288
  "test"
2289
+ ],
2290
+ [
2291
+ "51",
2292
+ "63516a68",
2293
+ "Returning within an if statement should succeed"
2289
2294
  ]
2290
2295
  ]
@@ -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') as string
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') as string,
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') as string} 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.`)
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') as string} 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.`)
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') as string,
532
+ sourceTXID: input.sourceTransaction.id('hex'),
520
533
  sourceOutputIndex: input.sourceOutputIndex,
521
534
  lockingScript: sourceOutput.lockingScript,
522
535
  sourceSatoshis: sourceOutput.satoshis,