@bsv/sdk 1.4.23 → 1.5.0

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 (59) hide show
  1. package/README.md +14 -0
  2. package/dist/cjs/package.json +1 -1
  3. package/dist/cjs/src/transaction/broadcasters/Teranode.js +64 -0
  4. package/dist/cjs/src/transaction/broadcasters/Teranode.js.map +1 -0
  5. package/dist/cjs/src/transaction/broadcasters/index.js +3 -1
  6. package/dist/cjs/src/transaction/broadcasters/index.js.map +1 -1
  7. package/dist/cjs/src/transaction/http/BinaryFetchClient.js +94 -0
  8. package/dist/cjs/src/transaction/http/BinaryFetchClient.js.map +1 -0
  9. package/dist/cjs/src/transaction/http/NodejsHttpClient.js.map +1 -1
  10. package/dist/cjs/src/transaction/http/index.js +3 -1
  11. package/dist/cjs/src/transaction/http/index.js.map +1 -1
  12. package/dist/cjs/src/wallet/WalletClient.js +12 -2
  13. package/dist/cjs/src/wallet/WalletClient.js.map +1 -1
  14. package/dist/cjs/src/wallet/substrates/ReactNativeWebView.js +165 -0
  15. package/dist/cjs/src/wallet/substrates/ReactNativeWebView.js.map +1 -0
  16. package/dist/cjs/src/wallet/substrates/index.js +3 -1
  17. package/dist/cjs/src/wallet/substrates/index.js.map +1 -1
  18. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  19. package/dist/esm/src/transaction/broadcasters/Teranode.js +62 -0
  20. package/dist/esm/src/transaction/broadcasters/Teranode.js.map +1 -0
  21. package/dist/esm/src/transaction/broadcasters/index.js +1 -0
  22. package/dist/esm/src/transaction/broadcasters/index.js.map +1 -1
  23. package/dist/esm/src/transaction/http/BinaryFetchClient.js +90 -0
  24. package/dist/esm/src/transaction/http/BinaryFetchClient.js.map +1 -0
  25. package/dist/esm/src/transaction/http/NodejsHttpClient.js.map +1 -1
  26. package/dist/esm/src/transaction/http/index.js +1 -0
  27. package/dist/esm/src/transaction/http/index.js.map +1 -1
  28. package/dist/esm/src/wallet/WalletClient.js +12 -2
  29. package/dist/esm/src/wallet/WalletClient.js.map +1 -1
  30. package/dist/esm/src/wallet/substrates/ReactNativeWebView.js +137 -0
  31. package/dist/esm/src/wallet/substrates/ReactNativeWebView.js.map +1 -0
  32. package/dist/esm/src/wallet/substrates/index.js +1 -0
  33. package/dist/esm/src/wallet/substrates/index.js.map +1 -1
  34. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  35. package/dist/types/src/transaction/broadcasters/Teranode.d.ts +25 -0
  36. package/dist/types/src/transaction/broadcasters/Teranode.d.ts.map +1 -0
  37. package/dist/types/src/transaction/broadcasters/index.d.ts +1 -0
  38. package/dist/types/src/transaction/broadcasters/index.d.ts.map +1 -1
  39. package/dist/types/src/transaction/http/BinaryFetchClient.d.ts +50 -0
  40. package/dist/types/src/transaction/http/BinaryFetchClient.d.ts.map +1 -0
  41. package/dist/types/src/transaction/http/index.d.ts +1 -0
  42. package/dist/types/src/transaction/http/index.d.ts.map +1 -1
  43. package/dist/types/src/wallet/WalletClient.d.ts.map +1 -1
  44. package/dist/types/src/wallet/substrates/ReactNativeWebView.d.ts +412 -0
  45. package/dist/types/src/wallet/substrates/ReactNativeWebView.d.ts.map +1 -0
  46. package/dist/types/src/wallet/substrates/index.d.ts +1 -0
  47. package/dist/types/src/wallet/substrates/index.d.ts.map +1 -1
  48. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  49. package/dist/umd/bundle.js +1 -1
  50. package/docs/wallet.md +453 -14
  51. package/package.json +1 -1
  52. package/src/transaction/broadcasters/Teranode.ts +77 -0
  53. package/src/transaction/broadcasters/index.ts +1 -0
  54. package/src/transaction/http/BinaryFetchClient.ts +141 -0
  55. package/src/transaction/http/NodejsHttpClient.ts +1 -1
  56. package/src/transaction/http/index.ts +1 -0
  57. package/src/wallet/WalletClient.ts +13 -4
  58. package/src/wallet/substrates/ReactNativeWebView.ts +560 -0
  59. package/src/wallet/substrates/index.ts +1 -0
@@ -0,0 +1,141 @@
1
+ import {
2
+ HttpClient,
3
+ HttpClientRequestOptions,
4
+ HttpClientResponse
5
+ } from './HttpClient.js'
6
+
7
+ /** Node Https module interface limited to options needed by ts-sdk */
8
+ export interface BinaryHttpsNodejs {
9
+ request: (
10
+ url: string,
11
+ options: HttpClientRequestOptions,
12
+ callback: (res: any) => void
13
+ ) => BinaryNodejsHttpClientRequest
14
+ }
15
+
16
+ /** Nodejs result of the Node https.request call limited to options needed by ts-sdk */
17
+ export interface BinaryNodejsHttpClientRequest {
18
+ write: (chunk: Buffer) => void
19
+
20
+ on: (event: string, callback: (data: any) => void) => void
21
+
22
+ end: (() => void) & (() => void)
23
+ }
24
+
25
+ /**
26
+ * Adapter for Node Https module to be used as HttpClient
27
+ */
28
+ export class BinaryNodejsHttpClient implements HttpClient {
29
+ constructor(private readonly https: BinaryHttpsNodejs) { }
30
+
31
+ async request(
32
+ url: string,
33
+ requestOptions: HttpClientRequestOptions
34
+ ): Promise<HttpClientResponse> {
35
+ return await new Promise((resolve, reject) => {
36
+ const req = this.https.request(url, requestOptions, (res) => {
37
+ let body = ''
38
+ res.on('data', (chunk: string) => {
39
+ body += chunk
40
+ })
41
+ res.on('end', () => {
42
+ const ok = res.statusCode >= 200 && res.statusCode <= 299
43
+ const mediaType = res.headers['content-type']
44
+ const data =
45
+ body !== '' && typeof mediaType === 'string' && mediaType.startsWith('application/json')
46
+ ? JSON.parse(body)
47
+ : body
48
+ resolve({
49
+ status: res.statusCode,
50
+ statusText: res.statusMessage,
51
+ ok,
52
+ data
53
+ })
54
+ })
55
+ })
56
+
57
+ req.on('error', (error: Error) => {
58
+ reject(error)
59
+ })
60
+
61
+ if (requestOptions.data !== null && requestOptions.data !== undefined) {
62
+ req.write(Buffer.from(requestOptions.data))
63
+ }
64
+ req.end()
65
+ })
66
+ }
67
+ }
68
+
69
+ /** fetch function interface limited to options needed by ts-sdk */
70
+ /**
71
+ * Makes a request to the server.
72
+ * @param url The URL to make the request to.
73
+ * @param options The request configuration.
74
+ */
75
+ export type Fetch = (url: string, options: FetchOptions) => Promise<Response>
76
+
77
+ /**
78
+ * An interface for configuration of the request to be passed to the fetch method
79
+ * limited to options needed by ts-sdk.
80
+ */
81
+ export interface FetchOptions {
82
+ /** A string to set request's method. */
83
+ method?: string
84
+ /** An object literal set request's headers. */
85
+ headers?: Record<string, string>
86
+ /** An object or null to set request's body. */
87
+ body?: Buffer | Uint8Array | Blob | null
88
+ }
89
+
90
+ /**
91
+ * Adapter for Node Https module to be used as HttpClient
92
+ */
93
+ export class BinaryFetchClient implements HttpClient {
94
+ constructor(private readonly fetch: Fetch) { }
95
+
96
+ async request<D>(
97
+ url: string,
98
+ options: HttpClientRequestOptions
99
+ ): Promise<HttpClientResponse<D>> {
100
+ const fetchOptions: FetchOptions = {
101
+ method: options.method,
102
+ headers: options.headers,
103
+ body: options.data
104
+ }
105
+
106
+ const res = await this.fetch(url, fetchOptions)
107
+ const data = await res.text()
108
+
109
+ return {
110
+ ok: res.ok,
111
+ status: res.status,
112
+ statusText: res.statusText,
113
+ data: data as D
114
+ }
115
+ }
116
+ }
117
+
118
+ export function binaryHttpClient(): HttpClient {
119
+ const noHttpClient: HttpClient = {
120
+ async request(..._): Promise<HttpClientResponse> {
121
+ throw new Error('No method available to perform HTTP request')
122
+ }
123
+ }
124
+
125
+ if (typeof window !== 'undefined' && typeof window.fetch === 'function') {
126
+ // Use fetch in a browser environment
127
+ return new BinaryFetchClient(window.fetch.bind(window))
128
+ } else if (typeof require !== 'undefined') {
129
+ // Use Node https module
130
+ // eslint-disable-next-line
131
+ try {
132
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
133
+ const https = require('https')
134
+ return new BinaryNodejsHttpClient(https)
135
+ } catch (e) {
136
+ return noHttpClient
137
+ }
138
+ } else {
139
+ return noHttpClient
140
+ }
141
+ }
@@ -54,7 +54,7 @@ export class NodejsHttpClient implements HttpClient {
54
54
  })
55
55
  })
56
56
 
57
- req.on('error', (error) => {
57
+ req.on('error', (error: Error) => {
58
58
  reject(error)
59
59
  })
60
60
 
@@ -4,6 +4,7 @@ export type {
4
4
  HttpClientRequestOptions
5
5
  } from './HttpClient.js'
6
6
  export { defaultHttpClient } from './DefaultHttpClient.js'
7
+ export { binaryHttpClient } from './BinaryFetchClient.js'
7
8
  export { NodejsHttpClient } from './NodejsHttpClient.js'
8
9
  export { FetchHttpClient } from './FetchHttpClient.js'
9
10
  export type { Fetch, FetchOptions } from './FetchHttpClient.js'
@@ -40,6 +40,7 @@ import XDMSubstrate from './substrates/XDM.js'
40
40
  import WalletWireTransceiver from './substrates/WalletWireTransceiver.js'
41
41
  import HTTPWalletWire from './substrates/HTTPWalletWire.js'
42
42
  import HTTPWalletJSON from './substrates/HTTPWalletJSON.js'
43
+ import ReactNativeWebView from './substrates/ReactNativeWebView.js'
43
44
 
44
45
  const MAX_XDM_RESPONSE_WAIT = 200
45
46
 
@@ -91,6 +92,7 @@ export default class WalletClient implements WalletInterface {
91
92
  }
92
93
  }
93
94
  try {
95
+ console.log('Connecting to substrate...')
94
96
  sub = new WindowCWISubstrate()
95
97
  await checkSub()
96
98
  this.substrate = sub
@@ -113,10 +115,17 @@ export default class WalletClient implements WalletInterface {
113
115
  await checkSub()
114
116
  this.substrate = sub
115
117
  } catch (e) {
116
- // No comms. Tell the user to install a BSV wallet.
117
- throw new Error(
118
- 'No wallet available over any communication substrate. Install a BSV wallet today!'
119
- )
118
+ // HTTP JSON failed, attempt the next...
119
+ try {
120
+ sub = new ReactNativeWebView(this.originator)
121
+ await checkSub()
122
+ this.substrate = sub
123
+ } catch (e) {
124
+ // No comms. Tell the user to install a BSV wallet.
125
+ throw new Error(
126
+ 'No wallet available over any communication substrate. Install a BSV wallet today!'
127
+ )
128
+ }
120
129
  }
121
130
  }
122
131
  }