@bsv/sdk 1.3.24 → 1.3.26

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 (35) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/auth/clients/AuthFetch.js +4 -3
  3. package/dist/cjs/src/auth/clients/AuthFetch.js.map +1 -1
  4. package/dist/cjs/src/auth/utils/createNonce.js.map +1 -1
  5. package/dist/cjs/src/auth/utils/verifyNonce.js.map +1 -1
  6. package/dist/cjs/src/primitives/utils.js +40 -11
  7. package/dist/cjs/src/primitives/utils.js.map +1 -1
  8. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  9. package/dist/esm/src/auth/clients/AuthFetch.js +5 -4
  10. package/dist/esm/src/auth/clients/AuthFetch.js.map +1 -1
  11. package/dist/esm/src/auth/utils/createNonce.js.map +1 -1
  12. package/dist/esm/src/auth/utils/verifyNonce.js.map +1 -1
  13. package/dist/esm/src/primitives/utils.js +40 -11
  14. package/dist/esm/src/primitives/utils.js.map +1 -1
  15. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  16. package/dist/types/src/auth/clients/AuthFetch.d.ts.map +1 -1
  17. package/dist/types/src/auth/utils/createNonce.d.ts +2 -2
  18. package/dist/types/src/auth/utils/createNonce.d.ts.map +1 -1
  19. package/dist/types/src/auth/utils/verifyNonce.d.ts +2 -2
  20. package/dist/types/src/auth/utils/verifyNonce.d.ts.map +1 -1
  21. package/dist/types/src/primitives/utils.d.ts.map +1 -1
  22. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  23. package/dist/umd/bundle.js +1 -1
  24. package/docs/swagger/dist/swagger-initializer.js +7 -7
  25. package/docs/swagger/dist/swagger-ui-bundle.js +1 -1
  26. package/docs/swagger/dist/swagger-ui-es-bundle-core.js +2 -2
  27. package/docs/swagger/dist/swagger-ui-es-bundle.js +1 -1
  28. package/docs/swagger/dist/swagger-ui-standalone-preset.js +1 -1
  29. package/docs/swagger/dist/swagger-ui.js +2 -2
  30. package/package.json +1 -1
  31. package/src/auth/clients/AuthFetch.ts +5 -4
  32. package/src/auth/utils/createNonce.ts +3 -2
  33. package/src/auth/utils/verifyNonce.ts +2 -2
  34. package/src/primitives/__tests/utils.test.ts +51 -1
  35. package/src/primitives/utils.ts +52 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.3.24",
3
+ "version": "1.3.26",
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'
@@ -397,15 +397,15 @@ export class AuthFetch {
397
397
  }
398
398
 
399
399
  // Create a random suffix for the derivation path
400
- const derivationSuffix = Utils.toBase64(Random(10))
400
+ const derivationSuffix = await createNonce(this.wallet)
401
401
 
402
402
  // Derive the script hex from the server identity key
403
403
  const { publicKey: derivedPublicKey } = await this.wallet.getPublicKey({
404
- protocolID: [2, 'wallet payment'],
404
+ protocolID: [2, '3241645161d8'], // wallet payment protocol
405
405
  keyID: `${derivationPrefix} ${derivationSuffix}`,
406
406
  counterparty: serverIdentityKey
407
407
  })
408
- const lockingScript = new P2PKH().lock(PublicKey.fromString(derivedPublicKey).toHash()).toHex()
408
+ const lockingScript = new P2PKH().lock(PublicKey.fromString(derivedPublicKey).toAddress()).toHex()
409
409
 
410
410
  // Create the payment transaction using createAction
411
411
  const { tx } = await this.wallet.createAction({
@@ -421,6 +421,7 @@ export class AuthFetch {
421
421
  config.headers = config.headers || {}
422
422
  config.headers['x-bsv-payment'] = JSON.stringify({
423
423
  derivationPrefix,
424
+ derivationSuffix,
424
425
  transaction: Utils.toBase64(tx)
425
426
  })
426
427
  config.retryCounter ??= 3
@@ -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> {
@@ -15,7 +15,8 @@ describe('utils', () => {
15
15
  expect(toArray('1234', 'hex')).toEqual([0x12, 0x34])
16
16
  expect(toArray('1234')).toEqual([49, 50, 51, 52])
17
17
  expect(toArray('1234', 'utf8')).toEqual([49, 50, 51, 52])
18
- expect(toArray('\u1234234')).toEqual([18, 52, 50, 51, 52])
18
+ expect(toArray('\u1234', 'utf8')).toEqual([225, 136, 180])
19
+ expect(toArray('\u1234' + '234', 'utf8')).toEqual([225, 136, 180, 50, 51, 52])
19
20
  expect(toArray([1, 2, 3, 4])).toEqual([1, 2, 3, 4])
20
21
  })
21
22
 
@@ -156,4 +157,53 @@ describe('utils', () => {
156
157
  })
157
158
  })
158
159
  })
160
+
161
+ test('should return an empty array for an empty string', () => {
162
+ expect(toArray("")).toEqual([])
163
+ })
164
+
165
+ test('should encode ASCII characters correctly', () => {
166
+ const input = "Hello, World!"
167
+ const expected = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
168
+ expect(toArray(input)).toEqual(expected)
169
+ })
170
+
171
+ test('should encode 2-byte characters correctly', () => {
172
+ // "é" (U+00E9) should encode to [0xC3, 0xA9]
173
+ expect(toArray("é")).toEqual([0xC3, 0xA9])
174
+ })
175
+
176
+ test('should encode 3-byte characters correctly', () => {
177
+ // "€" (U+20AC) should encode to [0xE2, 0x82, 0xAC]
178
+ expect(toArray("€")).toEqual([0xE2, 0x82, 0xAC])
179
+ })
180
+
181
+ test('should encode 4-byte characters correctly', () => {
182
+ // "😃" (U+1F603) should encode to [0xF0, 0x9F, 0x98, 0x83]
183
+ expect(toArray("😃")).toEqual([0xF0, 0x9F, 0x98, 0x83])
184
+ })
185
+
186
+ test('should encode mixed content correctly', () => {
187
+ // "Hello, 😃! €" contains ASCII, an emoji, and a 3-byte character.
188
+ const input = "Hello, 😃! €"
189
+ const expected = [
190
+ // "Hello, " => ASCII bytes:
191
+ 72, 101, 108, 108, 111, 44, 32,
192
+ // "😃" => 4-byte sequence:
193
+ 0xF0, 0x9F, 0x98, 0x83,
194
+ // "!" => ASCII, then space:
195
+ 33, 32,
196
+ // "€" => 3-byte sequence:
197
+ 0xE2, 0x82, 0xAC
198
+ ]
199
+ expect(toArray(input)).toEqual(expected)
200
+ })
201
+
202
+ test('should replace lone surrogates with the replacement character', () => {
203
+ // An unpaired high surrogate "\uD800" should be replaced with U+FFFD,
204
+ // which is encoded in UTF-8 as [0xEF, 0xBF, 0xBD]
205
+ const input = "\uD800"
206
+ const expected = [0xEF, 0xBF, 0xBD]
207
+ expect(toArray(input)).toEqual(expected)
208
+ })
159
209
  })
@@ -84,19 +84,61 @@ const base64ToArray = (msg: string): number[] => {
84
84
  return result
85
85
  }
86
86
 
87
- const utf8ToArray = (msg: string): number[] => {
88
- const res: number[] = []
89
- for (let i = 0; i < msg.length; i++) {
90
- const c = msg.charCodeAt(i)
91
- const hi = c >> 8
92
- const lo = c & 0xff
93
- if (hi !== 0) {
94
- res.push(hi, lo)
87
+ /**
88
+ * Encodes a string into an array of bytes representing its UTF-8 encoding.
89
+ * Any lone surrogates are replaced with the Unicode replacement character (U+FFFD).
90
+ *
91
+ * @param str - The string to encode.
92
+ * @returns An array of numbers, each representing a byte in the UTF-8 encoded string.
93
+ */
94
+ function utf8ToArray (str: string): number[] {
95
+ const result: number[] = []
96
+
97
+ for (let i = 0; i < str.length; i++) {
98
+ const cp = str.codePointAt(i)
99
+ if (cp === undefined) {
100
+ // Should never be out of range.
101
+ throw new Error(`Index out of range: ${i}`)
102
+ }
103
+ let codePoint = cp
104
+
105
+ if (codePoint > 0xFFFF) {
106
+ // Valid surrogate pair => skip the next code unit because codePointAt
107
+ // has already combined them into a single code point.
108
+ i++
109
+ } else {
110
+ // Check if codePoint is a lone (unpaired) high surrogate or low surrogate.
111
+ if (codePoint >= 0xD800 && codePoint <= 0xDFFF) {
112
+ // Replace with the replacement character (U+FFFD).
113
+ codePoint = 0xFFFD
114
+ }
115
+ }
116
+
117
+ // Encode according to the UTF-8 standard
118
+ if (codePoint <= 0x7F) {
119
+ result.push(codePoint)
120
+ } else if (codePoint <= 0x7FF) {
121
+ result.push(
122
+ 0xC0 | (codePoint >> 6),
123
+ 0x80 | (codePoint & 0x3F)
124
+ )
125
+ } else if (codePoint <= 0xFFFF) {
126
+ result.push(
127
+ 0xE0 | (codePoint >> 12),
128
+ 0x80 | ((codePoint >> 6) & 0x3F),
129
+ 0x80 | (codePoint & 0x3F)
130
+ )
95
131
  } else {
96
- res.push(lo)
132
+ result.push(
133
+ 0xF0 | (codePoint >> 18),
134
+ 0x80 | ((codePoint >> 12) & 0x3F),
135
+ 0x80 | ((codePoint >> 6) & 0x3F),
136
+ 0x80 | (codePoint & 0x3F)
137
+ )
97
138
  }
98
139
  }
99
- return res
140
+
141
+ return result
100
142
  }
101
143
 
102
144
  /**