@bsv/sdk 1.3.24 → 1.3.25

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.24",
3
+ "version": "1.3.25",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -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
  /**