@bsv/sdk 1.3.29 → 1.3.30

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.29",
3
+ "version": "1.3.30",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -64,7 +64,7 @@ export class AuthFetch {
64
64
  }
65
65
  config.retryCounter--
66
66
  }
67
- const response = await new Promise<Response>(async (resolve, reject) => {
67
+ const response = await new Promise<Response>((async (resolve, reject) => {
68
68
  try {
69
69
  // Apply defaults
70
70
  const { method = 'GET', headers = {}, body } = config
@@ -75,7 +75,7 @@ export class AuthFetch {
75
75
 
76
76
  // Create a new transport for this base url if needed
77
77
  let peerToUse: AuthPeer
78
- if (!this.peers[baseURL]) {
78
+ if (typeof this.peers[baseURL] === 'undefined') {
79
79
  // Create a peer for the request
80
80
  const newTransport = new SimplifiedFetchTransport(baseURL)
81
81
  peerToUse = {
@@ -96,6 +96,7 @@ export class AuthFetch {
96
96
  } catch (error) {
97
97
  reject(error)
98
98
  }
99
+ return
99
100
  }
100
101
  peerToUse = this.peers[baseURL]
101
102
  }
@@ -119,59 +120,68 @@ export class AuthFetch {
119
120
  const responseReader = new Utils.Reader(payload)
120
121
  // Deserialize first 32 bytes of payload
121
122
  const responseNonceAsBase64 = Utils.toBase64(responseReader.read(32))
122
- if (responseNonceAsBase64 === requestNonceAsBase64) {
123
- peerToUse.peer.stopListeningForGeneralMessages(listenerId)
124
-
125
- // Save the identity key for the peer for future requests, since we have it here.
126
- this.peers[baseURL].identityKey = senderPublicKey
127
- this.peers[baseURL].supportsMutualAuth = true
128
-
129
- // Status code
130
- const statusCode = responseReader.readVarIntNum()
131
-
132
- // Headers
133
- const responseHeaders = {}
134
- const nHeaders = responseReader.readVarIntNum()
135
- if (nHeaders > 0) {
136
- for (let i = 0; i < nHeaders; i++) {
137
- const nHeaderKeyBytes = responseReader.readVarIntNum()
138
- const headerKeyBytes = responseReader.read(nHeaderKeyBytes)
139
- const headerKey = Utils.toUTF8(headerKeyBytes)
140
- const nHeaderValueBytes = responseReader.readVarIntNum()
141
- const headerValueBytes = responseReader.read(nHeaderValueBytes)
142
- const headerValue = Utils.toUTF8(headerValueBytes)
143
- responseHeaders[headerKey] = headerValue
144
- }
123
+ if (responseNonceAsBase64 !== requestNonceAsBase64) {
124
+ return
125
+ }
126
+ peerToUse.peer.stopListeningForGeneralMessages(listenerId)
127
+
128
+ // Save the identity key for the peer for future requests, since we have it here.
129
+ this.peers[baseURL].identityKey = senderPublicKey
130
+ this.peers[baseURL].supportsMutualAuth = true
131
+
132
+ // Status code
133
+ const statusCode = responseReader.readVarIntNum()
134
+
135
+ // Headers
136
+ const responseHeaders = {}
137
+ const nHeaders = responseReader.readVarIntNum()
138
+ if (nHeaders > 0) {
139
+ for (let i = 0; i < nHeaders; i++) {
140
+ const nHeaderKeyBytes = responseReader.readVarIntNum()
141
+ const headerKeyBytes = responseReader.read(nHeaderKeyBytes)
142
+ const headerKey = Utils.toUTF8(headerKeyBytes)
143
+ const nHeaderValueBytes = responseReader.readVarIntNum()
144
+ const headerValueBytes = responseReader.read(nHeaderValueBytes)
145
+ const headerValue = Utils.toUTF8(headerValueBytes)
146
+ responseHeaders[headerKey] = headerValue
145
147
  }
148
+ }
146
149
 
147
- // Add back the server identity key header
148
- responseHeaders['x-bsv-auth-identity-key'] = senderPublicKey
150
+ // Add back the server identity key header
151
+ responseHeaders['x-bsv-auth-identity-key'] = senderPublicKey
149
152
 
150
- // Body
151
- let responseBody
152
- const responseBodyBytes = responseReader.readVarIntNum()
153
- if (responseBodyBytes > 0) {
154
- responseBody = responseReader.read(responseBodyBytes)
155
- }
153
+ // Body
154
+ let responseBody
155
+ const responseBodyBytes = responseReader.readVarIntNum()
156
+ if (responseBodyBytes > 0) {
157
+ responseBody = responseReader.read(responseBodyBytes)
158
+ }
156
159
 
157
- // Create the Response object
158
- const responseValue = new Response(
159
- responseBody ? new Uint8Array(responseBody) : null, {
160
+ // Create the Response object
161
+ const responseValue = new Response(
162
+ responseBody ? new Uint8Array(responseBody) : null,
163
+ {
160
164
  status: statusCode,
161
165
  statusText: `${statusCode}`,
162
166
  headers: new Headers(responseHeaders)
163
- })
167
+ }
168
+ )
164
169
 
165
- // Resolve or reject the correct request with the response data
166
- this.callbacks[requestNonceAsBase64].resolve(responseValue)
170
+ // Resolve or reject the correct request with the response data
171
+ this.callbacks[requestNonceAsBase64].resolve(responseValue)
167
172
 
168
- // Clean up
169
- delete this.callbacks[requestNonceAsBase64]
170
- }
173
+ // Clean up
174
+ delete this.callbacks[requestNonceAsBase64]
171
175
  })
172
176
 
173
177
  // Send the request, now that all listeners are set up
174
178
  await peerToUse.peer.toPeer(writer.toArray(), peerToUse.identityKey).catch(async error => {
179
+ if (error.message.includes('Session not found for nonce')) {
180
+ delete this.peers[baseURL]
181
+ config.retryCounter ??= 3
182
+ const response = await this.fetch(url, config)
183
+ resolve(response)
184
+ }
175
185
  if (error.message.includes('HTTP server failed to authenticate')) {
176
186
  try {
177
187
  const response = await this.handleFetchAndValidate(url, config, peerToUse)
@@ -186,7 +196,7 @@ export class AuthFetch {
186
196
  } catch (e) {
187
197
  reject(e)
188
198
  }
189
- })
199
+ }) as Function)
190
200
 
191
201
  // Check if server requires payment to access the requested route
192
202
  if (response.status === 402) {