@bsv/sdk 1.0.37 → 1.0.39

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 (46) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/primitives/AESGCM.js +13 -4
  3. package/dist/cjs/src/primitives/AESGCM.js.map +1 -1
  4. package/dist/cjs/src/transaction/Broadcaster.js +15 -0
  5. package/dist/cjs/src/transaction/Broadcaster.js.map +1 -1
  6. package/dist/cjs/src/transaction/broadcasters/ARC.js +32 -5
  7. package/dist/cjs/src/transaction/broadcasters/ARC.js.map +1 -1
  8. package/dist/cjs/src/transaction/http/FetchHttpClient.js +1 -1
  9. package/dist/cjs/src/transaction/http/FetchHttpClient.js.map +1 -1
  10. package/dist/cjs/src/transaction/http/NodejsHttpClient.js +1 -1
  11. package/dist/cjs/src/transaction/http/NodejsHttpClient.js.map +1 -1
  12. package/dist/cjs/src/transaction/index.js +4 -1
  13. package/dist/cjs/src/transaction/index.js.map +1 -1
  14. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  15. package/dist/esm/src/primitives/AESGCM.js +13 -4
  16. package/dist/esm/src/primitives/AESGCM.js.map +1 -1
  17. package/dist/esm/src/transaction/Broadcaster.js +12 -1
  18. package/dist/esm/src/transaction/Broadcaster.js.map +1 -1
  19. package/dist/esm/src/transaction/broadcasters/ARC.js +34 -4
  20. package/dist/esm/src/transaction/broadcasters/ARC.js.map +1 -1
  21. package/dist/esm/src/transaction/http/FetchHttpClient.js +1 -1
  22. package/dist/esm/src/transaction/http/FetchHttpClient.js.map +1 -1
  23. package/dist/esm/src/transaction/http/NodejsHttpClient.js +1 -1
  24. package/dist/esm/src/transaction/http/NodejsHttpClient.js.map +1 -1
  25. package/dist/esm/src/transaction/index.js +1 -0
  26. package/dist/esm/src/transaction/index.js.map +1 -1
  27. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  28. package/dist/types/src/primitives/AESGCM.d.ts.map +1 -1
  29. package/dist/types/src/transaction/Broadcaster.d.ts +8 -0
  30. package/dist/types/src/transaction/Broadcaster.d.ts.map +1 -1
  31. package/dist/types/src/transaction/broadcasters/ARC.d.ts +11 -2
  32. package/dist/types/src/transaction/broadcasters/ARC.d.ts.map +1 -1
  33. package/dist/types/src/transaction/index.d.ts +2 -1
  34. package/dist/types/src/transaction/index.d.ts.map +1 -1
  35. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  36. package/docs/examples/EXAMPLE_UTXOS_TX.md +85 -0
  37. package/docs/transaction.md +55 -1
  38. package/package.json +1 -1
  39. package/src/messages/__tests/EncryptedMessage.test.ts +26 -0
  40. package/src/primitives/AESGCM.ts +10 -4
  41. package/src/transaction/Broadcaster.ts +14 -0
  42. package/src/transaction/broadcasters/ARC.ts +44 -6
  43. package/src/transaction/broadcasters/__tests/ARC.test.ts +4 -4
  44. package/src/transaction/http/FetchHttpClient.ts +1 -1
  45. package/src/transaction/http/NodejsHttpClient.ts +1 -1
  46. package/src/transaction/index.ts +2 -1
@@ -9,10 +9,16 @@ import {toHex} from "../../primitives/utils.js";
9
9
  export interface ArcConfig {
10
10
  /** Authentication token for the ARC API */
11
11
  apiKey?: string
12
- /** Deployment id used annotating api calls in XDeployment-ID header - this value will be randomly generated if not set */
13
- deploymentId?: string
14
12
  /** The HTTP client used to make requests to the ARC API. */
15
13
  httpClient?: HttpClient
14
+ /** Deployment id used annotating api calls in XDeployment-ID header - this value will be randomly generated if not set */
15
+ deploymentId?: string
16
+ /** notification callback endpoint for proofs and double spend notification */
17
+ callbackUrl?: string
18
+ /** default access token for notification callback endpoint. It will be used as a Authorization header for the http callback */
19
+ callbackToken?: string
20
+ /** additional headers to be attached to all tx submissions. */
21
+ headers?: Record<string, string>
16
22
  }
17
23
 
18
24
 
@@ -27,6 +33,9 @@ export default class ARC implements Broadcaster {
27
33
  readonly URL: string
28
34
  readonly apiKey: string | undefined
29
35
  readonly deploymentId: string
36
+ readonly callbackUrl: string | undefined
37
+ readonly callbackToken: string | undefined
38
+ readonly headers: Record<string, string> | undefined
30
39
  private readonly httpClient: HttpClient;
31
40
 
32
41
  /**
@@ -49,11 +58,17 @@ export default class ARC implements Broadcaster {
49
58
  if (typeof config === 'string') {
50
59
  this.apiKey = config
51
60
  this.httpClient = defaultHttpClient()
61
+ this.deploymentId = defaultDeploymentId()
62
+ this.callbackToken = undefined
63
+ this.callbackUrl = undefined
52
64
  } else {
53
- const {apiKey, deploymentId, httpClient} = config ?? {} as ArcConfig
65
+ const {apiKey, deploymentId, httpClient, callbackToken, callbackUrl, headers } = config ?? {} as ArcConfig
66
+ this.apiKey = apiKey
54
67
  this.httpClient = httpClient ?? defaultHttpClient()
55
68
  this.deploymentId = deploymentId ?? defaultDeploymentId()
56
- this.apiKey = apiKey
69
+ this.callbackToken = callbackToken
70
+ this.callbackUrl = callbackUrl
71
+ this.headers = headers
57
72
  }
58
73
  }
59
74
 
@@ -91,11 +106,20 @@ export default class ARC implements Broadcaster {
91
106
  message: `${txStatus} ${extraInfo}`
92
107
  }
93
108
  } else {
94
- return {
109
+ const r: BroadcastFailure = {
95
110
  status: 'error',
96
111
  code: response.status.toString() ?? 'ERR_UNKNOWN',
97
- description: response.data?.detail ?? 'Unknown error'
112
+ description: 'Unknown error'
113
+ }
114
+ if (typeof response.data === 'string') {
115
+ try {
116
+ const data = JSON.parse(response.data)
117
+ if (typeof data.detail === 'string') {
118
+ r.description = data.detail
119
+ }
120
+ } catch {}
98
121
  }
122
+ return r
99
123
  }
100
124
  } catch (error) {
101
125
  return {
@@ -118,6 +142,20 @@ export default class ARC implements Broadcaster {
118
142
  headers['Authorization'] = `Bearer ${this.apiKey}`
119
143
  }
120
144
 
145
+ if (this.callbackUrl) {
146
+ headers['X-CallbackUrl'] = this.callbackUrl
147
+ }
148
+
149
+ if (this.callbackToken) {
150
+ headers['X-CallbackToken'] = this.callbackToken
151
+ }
152
+
153
+ if (!!this.headers) {
154
+ for (const key in this.headers) {
155
+ headers[key] = this.headers[key]
156
+ }
157
+ }
158
+
121
159
  return headers
122
160
  }
123
161
  }
@@ -165,9 +165,9 @@ describe('ARC Broadcaster', () => {
165
165
  it('should handle non-200 responses', async () => {
166
166
  const mockFetch = mockedFetch({
167
167
  status: '400',
168
- data: {
168
+ data: JSON.stringify({
169
169
  detail: 'Bad request'
170
- }
170
+ })
171
171
  })
172
172
 
173
173
  const broadcaster = new ARC(URL, {httpClient: new FetchHttpClient(mockFetch)})
@@ -189,7 +189,7 @@ describe('ARC Broadcaster', () => {
189
189
  headers: {
190
190
  get(key: string) {
191
191
  if (key === 'Content-Type') {
192
- return 'application/json'
192
+ return 'application/json; charset=UTF-8'
193
193
  }
194
194
  }
195
195
  },
@@ -205,7 +205,7 @@ describe('ARC Broadcaster', () => {
205
205
  statusCode: response.status,
206
206
  statusMessage: response.status == 200 ? 'OK' : 'Bad request',
207
207
  headers: {
208
- 'content-type': 'application/json'
208
+ 'content-type': 'application/json; charset=UTF-8'
209
209
  },
210
210
  on: (event, handler) => {
211
211
  if (event === 'data') handler(JSON.stringify(response.data))
@@ -38,7 +38,7 @@ export class FetchHttpClient implements HttpClient {
38
38
 
39
39
  const res = await this.fetch(url, fetchOptions);
40
40
  const mediaType = res.headers.get('Content-Type');
41
- const data = mediaType === 'application/json' ? await res.json() : await res.text();
41
+ const data = mediaType.startsWith('application/json') ? await res.json() : await res.text();
42
42
 
43
43
  return {
44
44
  ok: res.ok,
@@ -32,7 +32,7 @@ export class NodejsHttpClient implements HttpClient {
32
32
  res.on('end', () => {
33
33
  const ok = res.statusCode >= 200 && res.statusCode <= 299;
34
34
  const mediaType = res.headers['content-type'];
35
- const data = body && mediaType === 'application/json' ? JSON.parse(body) : body;
35
+ const data = body && mediaType.startsWith('application/json') ? JSON.parse(body) : body;
36
36
  resolve({
37
37
  status: res.statusCode,
38
38
  statusText: res.statusMessage,
@@ -2,5 +2,6 @@ export { default as Transaction } from './Transaction.js'
2
2
  export { default as MerklePath } from './MerklePath.js'
3
3
  export type { default as TransactionInput } from './TransactionInput.js'
4
4
  export type { default as TransactionOutput } from './TransactionOutput.js'
5
- export type * as Broadcaster from './Broadcaster.js'
5
+ export type { Broadcaster, BroadcastFailure, BroadcastResponse } from './Broadcaster.js'
6
+ export { isBroadcastResponse, isBroadcastFailure } from './Broadcaster.js'
6
7
  export type { default as ChainTracker } from './ChainTracker.js'