@bsv/sdk 1.3.25 → 1.3.27

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.3.25",
3
+ "version": "1.3.27",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -1,5 +1,5 @@
1
1
  // @ts-nocheck
2
- import { Utils, Random, P2PKH, PublicKey, WalletInterface } from '../../../mod.js'
2
+ import { Utils, Random, P2PKH, PublicKey, WalletInterface, createNonce } from '../../../mod.js'
3
3
  import { Peer } from '../Peer.js'
4
4
  import { SimplifiedFetchTransport } from '../transports/SimplifiedFetchTransport.js'
5
5
  import { SessionManager } from '../SessionManager.js'
@@ -329,6 +329,19 @@ export class AuthFetch {
329
329
  writer.write(headerValueAsArray)
330
330
  }
331
331
 
332
+ // If method typically carries a body and body is undefined, default it
333
+ // This prevents signature verification errors due to mismatch default body types with express
334
+ const methodsThatTypicallyHaveBody = ['POST', 'PUT', 'PATCH', 'DELETE']
335
+ if (methodsThatTypicallyHaveBody.includes(method.toUpperCase()) && body === undefined) {
336
+ // Check if content-type is application/json
337
+ const contentTypeHeader = includedHeaders.find(([k]) => k === 'content-type')
338
+ if (contentTypeHeader && contentTypeHeader[1].includes('application/json')) {
339
+ body = '{}'
340
+ } else {
341
+ body = ''
342
+ }
343
+ }
344
+
332
345
  // Handle body
333
346
  if (body) {
334
347
  const reqBody = await this.normalizeBodyToNumberArray(body) // Use the utility function
@@ -397,15 +410,15 @@ export class AuthFetch {
397
410
  }
398
411
 
399
412
  // Create a random suffix for the derivation path
400
- const derivationSuffix = Utils.toBase64(Random(10))
413
+ const derivationSuffix = await createNonce(this.wallet)
401
414
 
402
415
  // Derive the script hex from the server identity key
403
416
  const { publicKey: derivedPublicKey } = await this.wallet.getPublicKey({
404
- protocolID: [2, 'wallet payment'],
417
+ protocolID: [2, '3241645161d8'], // wallet payment protocol
405
418
  keyID: `${derivationPrefix} ${derivationSuffix}`,
406
419
  counterparty: serverIdentityKey
407
420
  })
408
- const lockingScript = new P2PKH().lock(PublicKey.fromString(derivedPublicKey).toHash()).toHex()
421
+ const lockingScript = new P2PKH().lock(PublicKey.fromString(derivedPublicKey).toAddress()).toHex()
409
422
 
410
423
  // Create the payment transaction using createAction
411
424
  const { tx } = await this.wallet.createAction({
@@ -421,6 +434,7 @@ export class AuthFetch {
421
434
  config.headers = config.headers || {}
422
435
  config.headers['x-bsv-payment'] = JSON.stringify({
423
436
  derivationPrefix,
437
+ derivationSuffix,
424
438
  transaction: Utils.toBase64(tx)
425
439
  })
426
440
  config.retryCounter ??= 3
@@ -430,11 +444,16 @@ export class AuthFetch {
430
444
  }
431
445
 
432
446
  private async normalizeBodyToNumberArray(body: BodyInit | null | undefined): Promise<number[]> {
433
- // 1. Null / undefined
447
+ // 0. Null / undefined
434
448
  if (body == null) {
435
449
  return []
436
450
  }
437
451
 
452
+ // 1. object
453
+ if (typeof body === 'object') {
454
+ return Utils.toArray(JSON.stringify(body, 'utf8'))
455
+ }
456
+
438
457
  // 2. number[]
439
458
  if (Array.isArray(body) && body.every((item) => typeof item === 'number')) {
440
459
  return body // Return the array as is
@@ -2,7 +2,8 @@ import {
2
2
  Utils,
3
3
  Random,
4
4
  WalletInterface,
5
- WalletCounterparty
5
+ WalletCounterparty,
6
+ Base64String
6
7
  } from '../../../mod.js'
7
8
 
8
9
  /**
@@ -14,7 +15,7 @@ import {
14
15
  export async function createNonce(
15
16
  wallet: WalletInterface,
16
17
  counterparty: WalletCounterparty = 'self'
17
- ): Promise<string> {
18
+ ): Promise<Base64String> {
18
19
  // Generate 16 random bytes for the first half of the data
19
20
  const firstHalf = Random(16)
20
21
  // Create an sha256 HMAC
@@ -1,4 +1,4 @@
1
- import { Utils, WalletInterface, WalletCounterparty } from '../../../mod.js'
1
+ import { Utils, WalletInterface, WalletCounterparty, Base64String } from '../../../mod.js'
2
2
 
3
3
  /**
4
4
  * Verifies a nonce derived from a wallet
@@ -8,7 +8,7 @@ import { Utils, WalletInterface, WalletCounterparty } from '../../../mod.js'
8
8
  * @returns The status of the validation
9
9
  */
10
10
  export async function verifyNonce(
11
- nonce: string,
11
+ nonce: Base64String,
12
12
  wallet: WalletInterface,
13
13
  counterparty: WalletCounterparty = 'self'
14
14
  ): Promise<boolean> {