@bsv/sdk 1.2.20 → 1.2.21

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 (154) hide show
  1. package/dist/cjs/package.json +3 -3
  2. package/dist/cjs/src/auth/Peer.js +536 -0
  3. package/dist/cjs/src/auth/Peer.js.map +1 -0
  4. package/dist/cjs/src/auth/SessionManager.js +66 -0
  5. package/dist/cjs/src/auth/SessionManager.js.map +1 -0
  6. package/dist/cjs/src/auth/{Certificate.js → certificates/Certificate.js} +22 -26
  7. package/dist/cjs/src/auth/certificates/Certificate.js.map +1 -0
  8. package/dist/cjs/src/auth/certificates/MasterCertificate.js +79 -0
  9. package/dist/cjs/src/auth/certificates/MasterCertificate.js.map +1 -0
  10. package/dist/cjs/src/auth/certificates/VerifiableCertificate.js +49 -0
  11. package/dist/cjs/src/auth/certificates/VerifiableCertificate.js.map +1 -0
  12. package/dist/cjs/src/auth/certificates/index.js +25 -0
  13. package/dist/cjs/src/auth/certificates/index.js.map +1 -0
  14. package/dist/cjs/src/auth/clients/AuthFetch.js +411 -0
  15. package/dist/cjs/src/auth/clients/AuthFetch.js.map +1 -0
  16. package/dist/cjs/src/auth/clients/index.js +18 -0
  17. package/dist/cjs/src/auth/clients/index.js.map +1 -0
  18. package/dist/cjs/src/auth/index.js +20 -5
  19. package/dist/cjs/src/auth/index.js.map +1 -1
  20. package/dist/cjs/src/auth/transports/SimplifiedFetchTransport.js +259 -0
  21. package/dist/cjs/src/auth/transports/SimplifiedFetchTransport.js.map +1 -0
  22. package/dist/cjs/src/auth/transports/index.js +18 -0
  23. package/dist/cjs/src/auth/transports/index.js.map +1 -0
  24. package/dist/cjs/src/auth/types.js +3 -0
  25. package/dist/cjs/src/auth/types.js.map +1 -0
  26. package/dist/cjs/src/auth/utils/certificateHelpers.js +51 -0
  27. package/dist/cjs/src/auth/utils/certificateHelpers.js.map +1 -0
  28. package/dist/cjs/src/auth/utils/createNonce.js +19 -0
  29. package/dist/cjs/src/auth/utils/createNonce.js.map +1 -0
  30. package/dist/cjs/src/auth/utils/getVerifiableCertificates.js +31 -0
  31. package/dist/cjs/src/auth/utils/getVerifiableCertificates.js.map +1 -0
  32. package/dist/cjs/src/auth/utils/index.js +21 -0
  33. package/dist/cjs/src/auth/utils/index.js.map +1 -0
  34. package/dist/cjs/src/auth/utils/validateCertificates.js +42 -0
  35. package/dist/cjs/src/auth/utils/validateCertificates.js.map +1 -0
  36. package/dist/cjs/src/auth/utils/verifyNonce.js +27 -0
  37. package/dist/cjs/src/auth/utils/verifyNonce.js.map +1 -0
  38. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js +1 -1
  39. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js.map +1 -1
  40. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js +148 -148
  41. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
  42. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  43. package/dist/esm/src/auth/Peer.js +533 -0
  44. package/dist/esm/src/auth/Peer.js.map +1 -0
  45. package/dist/esm/src/auth/SessionManager.js +63 -0
  46. package/dist/esm/src/auth/SessionManager.js.map +1 -0
  47. package/dist/esm/src/auth/{Certificate.js → certificates/Certificate.js} +1 -2
  48. package/dist/esm/src/auth/certificates/Certificate.js.map +1 -0
  49. package/dist/esm/src/auth/certificates/MasterCertificate.js +73 -0
  50. package/dist/esm/src/auth/certificates/MasterCertificate.js.map +1 -0
  51. package/dist/esm/src/auth/certificates/VerifiableCertificate.js +44 -0
  52. package/dist/esm/src/auth/certificates/VerifiableCertificate.js.map +1 -0
  53. package/dist/esm/src/auth/certificates/index.js +4 -0
  54. package/dist/esm/src/auth/certificates/index.js.map +1 -0
  55. package/dist/esm/src/auth/clients/AuthFetch.js +409 -0
  56. package/dist/esm/src/auth/clients/AuthFetch.js.map +1 -0
  57. package/dist/esm/src/auth/clients/index.js +2 -0
  58. package/dist/esm/src/auth/clients/index.js.map +1 -0
  59. package/dist/esm/src/auth/index.js +7 -1
  60. package/dist/esm/src/auth/index.js.map +1 -1
  61. package/dist/esm/src/auth/transports/SimplifiedFetchTransport.js +258 -0
  62. package/dist/esm/src/auth/transports/SimplifiedFetchTransport.js.map +1 -0
  63. package/dist/esm/src/auth/transports/index.js +2 -0
  64. package/dist/esm/src/auth/transports/index.js.map +1 -0
  65. package/dist/esm/src/auth/types.js +2 -0
  66. package/dist/esm/src/auth/types.js.map +1 -0
  67. package/dist/esm/src/auth/utils/certificateHelpers.js +47 -0
  68. package/dist/esm/src/auth/utils/certificateHelpers.js.map +1 -0
  69. package/dist/esm/src/auth/utils/createNonce.js +16 -0
  70. package/dist/esm/src/auth/utils/createNonce.js.map +1 -0
  71. package/dist/esm/src/auth/utils/getVerifiableCertificates.js +27 -0
  72. package/dist/esm/src/auth/utils/getVerifiableCertificates.js.map +1 -0
  73. package/dist/esm/src/auth/utils/index.js +5 -0
  74. package/dist/esm/src/auth/utils/index.js.map +1 -0
  75. package/dist/esm/src/auth/utils/validateCertificates.js +38 -0
  76. package/dist/esm/src/auth/utils/validateCertificates.js.map +1 -0
  77. package/dist/esm/src/auth/utils/verifyNonce.js +24 -0
  78. package/dist/esm/src/auth/utils/verifyNonce.js.map +1 -0
  79. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js +1 -1
  80. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js.map +1 -1
  81. package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js +1 -1
  82. package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
  83. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  84. package/dist/types/src/auth/Peer.d.ts +193 -0
  85. package/dist/types/src/auth/Peer.d.ts.map +1 -0
  86. package/dist/types/src/auth/SessionManager.d.ts +42 -0
  87. package/dist/types/src/auth/SessionManager.d.ts.map +1 -0
  88. package/dist/types/src/auth/{Certificate.d.ts → certificates/Certificate.d.ts} +1 -1
  89. package/dist/types/src/auth/certificates/Certificate.d.ts.map +1 -0
  90. package/dist/types/src/auth/certificates/MasterCertificate.d.ts +38 -0
  91. package/dist/types/src/auth/certificates/MasterCertificate.d.ts.map +1 -0
  92. package/dist/types/src/auth/certificates/VerifiableCertificate.d.ts +26 -0
  93. package/dist/types/src/auth/certificates/VerifiableCertificate.d.ts.map +1 -0
  94. package/dist/types/src/auth/certificates/index.d.ts +4 -0
  95. package/dist/types/src/auth/certificates/index.d.ts.map +1 -0
  96. package/dist/types/src/auth/clients/AuthFetch.d.ts +87 -0
  97. package/dist/types/src/auth/clients/AuthFetch.d.ts.map +1 -0
  98. package/dist/types/src/auth/clients/index.d.ts +2 -0
  99. package/dist/types/src/auth/clients/index.d.ts.map +1 -0
  100. package/dist/types/src/auth/index.d.ts +7 -1
  101. package/dist/types/src/auth/index.d.ts.map +1 -1
  102. package/dist/types/src/auth/transports/SimplifiedFetchTransport.d.ts +51 -0
  103. package/dist/types/src/auth/transports/SimplifiedFetchTransport.d.ts.map +1 -0
  104. package/dist/types/src/auth/transports/index.d.ts +2 -0
  105. package/dist/types/src/auth/transports/index.d.ts.map +1 -0
  106. package/dist/types/src/auth/types.d.ts +31 -0
  107. package/dist/types/src/auth/types.d.ts.map +1 -0
  108. package/dist/types/src/auth/utils/certificateHelpers.d.ts +26 -0
  109. package/dist/types/src/auth/utils/certificateHelpers.d.ts.map +1 -0
  110. package/dist/types/src/auth/utils/createNonce.d.ts +8 -0
  111. package/dist/types/src/auth/utils/createNonce.d.ts.map +1 -0
  112. package/dist/types/src/auth/utils/getVerifiableCertificates.d.ts +13 -0
  113. package/dist/types/src/auth/utils/getVerifiableCertificates.d.ts.map +1 -0
  114. package/dist/types/src/auth/utils/index.d.ts +5 -0
  115. package/dist/types/src/auth/utils/index.d.ts.map +1 -0
  116. package/dist/types/src/auth/utils/validateCertificates.d.ts +12 -0
  117. package/dist/types/src/auth/utils/validateCertificates.d.ts.map +1 -0
  118. package/dist/types/src/auth/utils/verifyNonce.d.ts +9 -0
  119. package/dist/types/src/auth/utils/verifyNonce.d.ts.map +1 -0
  120. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  121. package/dist/umd/bundle.js +1 -1
  122. package/docs/README.md +1 -0
  123. package/docs/auth.md +1119 -0
  124. package/package.json +13 -3
  125. package/src/auth/Peer.ts +600 -0
  126. package/src/auth/SessionManager.ts +71 -0
  127. package/src/auth/__tests/Peer.test.ts +599 -0
  128. package/src/auth/__tests/SessionManager.test.ts +87 -0
  129. package/src/auth/{Certificate.ts → certificates/Certificate.ts} +15 -8
  130. package/src/auth/certificates/MasterCertificate.ts +106 -0
  131. package/src/auth/certificates/VerifiableCertificate.ts +73 -0
  132. package/src/auth/certificates/__tests/Certificate.test.ts +282 -0
  133. package/src/auth/certificates/index.ts +3 -0
  134. package/src/auth/clients/AuthFetch.ts +482 -0
  135. package/src/auth/clients/index.ts +1 -0
  136. package/src/auth/index.ts +7 -1
  137. package/src/auth/transports/SimplifiedFetchTransport.ts +288 -0
  138. package/src/auth/transports/index.ts +1 -0
  139. package/src/auth/types.ts +41 -0
  140. package/src/auth/utils/__tests/cryptononce.test.ts +84 -0
  141. package/src/auth/utils/__tests/getVerifiableCertificates.test.ts +126 -0
  142. package/src/auth/utils/__tests/validateCertificates.test.ts +142 -0
  143. package/src/auth/utils/certificateHelpers.ts +86 -0
  144. package/src/auth/utils/createNonce.ts +16 -0
  145. package/src/auth/utils/getVerifiableCertificates.ts +40 -0
  146. package/src/auth/utils/index.ts +4 -0
  147. package/src/auth/utils/validateCertificates.ts +54 -0
  148. package/src/auth/utils/verifyNonce.ts +27 -0
  149. package/src/wallet/substrates/WalletWireProcessor.ts +1 -1
  150. package/src/wallet/substrates/WalletWireTransceiver.ts +1 -1
  151. package/dist/cjs/src/auth/Certificate.js.map +0 -1
  152. package/dist/esm/src/auth/Certificate.js.map +0 -1
  153. package/dist/types/src/auth/Certificate.d.ts.map +0 -1
  154. package/src/auth/__tests/Certificate.test.ts +0 -282
@@ -0,0 +1,258 @@
1
+ import { Utils } from '../../../mod.js';
2
+ const SUCCESS_STATUS_CODES = [200, 402];
3
+ /**
4
+ * Implements an HTTP-specific transport for handling Peer mutual authentication messages.
5
+ * This class integrates with fetch to send and receive authenticated messages between peers.
6
+ */
7
+ export class SimplifiedFetchTransport {
8
+ onDataCallback;
9
+ fetchClient;
10
+ baseUrl;
11
+ /**
12
+ * Constructs a new instance of SimplifiedFetchTransport.
13
+ * @param baseUrl - The base URL for all HTTP requests made by this transport.
14
+ * @param fetchClient - A fetch implementation to use for HTTP requests (default: global fetch).
15
+ */
16
+ constructor(baseUrl, fetchClient = fetch) {
17
+ this.fetchClient = fetchClient;
18
+ this.baseUrl = baseUrl;
19
+ }
20
+ /**
21
+ * Sends a message to an HTTP server using the transport mechanism.
22
+ * Handles both general and authenticated message types. For general messages,
23
+ * the payload is deserialized and sent as an HTTP request. For other message types,
24
+ * the message is sent as a POST request to the `/auth` endpoint.
25
+ *
26
+ * @param message - The AuthMessage to send.
27
+ * @returns A promise that resolves when the message is successfully sent.
28
+ *
29
+ * @throws Will throw an error if no listener has been registered via `onData`.
30
+ */
31
+ async send(message) {
32
+ if (!this.onDataCallback) {
33
+ throw new Error('Listen before you start speaking. God gave you two ears and one mouth for a reason.');
34
+ }
35
+ if (message.messageType !== 'general') {
36
+ const response = await this.fetchClient(`${this.baseUrl}/.well-known/auth`, {
37
+ method: 'POST',
38
+ headers: {
39
+ 'Content-Type': 'application/json'
40
+ },
41
+ body: JSON.stringify(message)
42
+ });
43
+ // Handle the response if data is received and callback is set
44
+ if (response.ok && this.onDataCallback) {
45
+ const responseMessage = await response.json();
46
+ if (responseMessage?.status !== 'certificate received') {
47
+ this.onDataCallback(responseMessage);
48
+ }
49
+ }
50
+ else {
51
+ // Server may be a non authenticated server
52
+ throw new Error('HTTP server failed to authenticate');
53
+ }
54
+ }
55
+ else {
56
+ // Parse message payload
57
+ const httpRequest = this.deserializeRequestPayload(message.payload);
58
+ // Send the byte array as the HTTP payload
59
+ const url = `${this.baseUrl}${httpRequest.urlPostfix}`;
60
+ let httpRequestWithAuthHeaders = httpRequest;
61
+ if (typeof httpRequest.headers !== 'object') {
62
+ httpRequestWithAuthHeaders.headers = {};
63
+ }
64
+ // Append auth headers in request to server
65
+ httpRequestWithAuthHeaders.headers['x-bsv-auth-version'] = message.version;
66
+ httpRequestWithAuthHeaders.headers['x-bsv-auth-identity-key'] = message.identityKey;
67
+ httpRequestWithAuthHeaders.headers['x-bsv-auth-nonce'] = message.nonce;
68
+ httpRequestWithAuthHeaders.headers['x-bsv-auth-your-nonce'] = message.yourNonce;
69
+ httpRequestWithAuthHeaders.headers['x-bsv-auth-signature'] = Utils.toHex(message.signature);
70
+ httpRequestWithAuthHeaders.headers['x-bsv-auth-request-id'] = httpRequest.requestId;
71
+ // Ensure Content-Type is set for requests with a body
72
+ if (httpRequestWithAuthHeaders.body) {
73
+ const headers = httpRequestWithAuthHeaders.headers;
74
+ if (!headers['content-type']) {
75
+ throw new Error('Content-Type header is required for requests with a body.');
76
+ }
77
+ const contentType = headers['content-type'];
78
+ // Transform body based on Content-Type
79
+ if (contentType.includes('application/json')) {
80
+ // Convert byte array to JSON string
81
+ httpRequestWithAuthHeaders.body = Utils.toUTF8(httpRequestWithAuthHeaders.body);
82
+ }
83
+ else if (contentType.includes('application/x-www-form-urlencoded')) {
84
+ // Convert byte array to URL-encoded string
85
+ httpRequestWithAuthHeaders.body = Utils.toUTF8(httpRequestWithAuthHeaders.body);
86
+ }
87
+ else if (contentType.includes('text/plain')) {
88
+ // Convert byte array to plain UTF-8 string
89
+ httpRequestWithAuthHeaders.body = Utils.toUTF8(httpRequestWithAuthHeaders.body);
90
+ }
91
+ else {
92
+ // For all other content types, treat as binary data
93
+ httpRequestWithAuthHeaders.body = new Uint8Array(httpRequestWithAuthHeaders.body);
94
+ }
95
+ }
96
+ // Send the actual fetch request to the server
97
+ const response = await this.fetchClient(url, {
98
+ method: httpRequestWithAuthHeaders.method,
99
+ headers: httpRequestWithAuthHeaders.headers,
100
+ body: httpRequestWithAuthHeaders.body
101
+ });
102
+ // Check for an acceptable status
103
+ if (!SUCCESS_STATUS_CODES.includes(response.status)) {
104
+ // Try parsing JSON error
105
+ let errorInfo;
106
+ try {
107
+ errorInfo = await response.json();
108
+ }
109
+ catch {
110
+ // Fallback to text if JSON parse fails
111
+ const text = await response.text().catch(() => '');
112
+ throw new Error(`HTTP ${response.status} - ${text || 'Unknown error'}`);
113
+ }
114
+ // If we find a known { status: 'error', code, description } structure
115
+ if (errorInfo?.status === 'error' && typeof errorInfo.description === 'string') {
116
+ const msg = `HTTP ${response.status} - ${errorInfo.description}`;
117
+ throw new Error(errorInfo.code ? `${msg} (code: ${errorInfo.code})` : msg);
118
+ }
119
+ // Otherwise just throw whatever we got
120
+ throw new Error(`HTTP ${response.status} - ${JSON.stringify(errorInfo)}`);
121
+ }
122
+ const parsedBody = await response.arrayBuffer();
123
+ const payloadWriter = new Utils.Writer();
124
+ payloadWriter.write(Utils.toArray(response.headers.get('x-bsv-auth-request-id'), 'base64'));
125
+ payloadWriter.writeVarIntNum(response.status);
126
+ // Filter out headers the server signed:
127
+ // - Custom headers prefixed with x-bsv are included, except auth
128
+ // - x-bsv-auth headers are not allowed
129
+ // - authorization header is signed by the server
130
+ const includedHeaders = [];
131
+ // Collect headers into a raw array for sorting
132
+ const headersArray = [];
133
+ response.headers.forEach((value, key) => {
134
+ const lowerKey = key.toLowerCase();
135
+ if (lowerKey.startsWith('x-bsv-') || lowerKey === 'authorization') {
136
+ if (!lowerKey.startsWith('x-bsv-auth')) {
137
+ headersArray.push([lowerKey, value]);
138
+ }
139
+ }
140
+ });
141
+ // Sort headers explicitly to match server-side order
142
+ headersArray.sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
143
+ includedHeaders.push(...headersArray);
144
+ // nHeaders
145
+ payloadWriter.writeVarIntNum(includedHeaders.length);
146
+ for (let i = 0; i < includedHeaders.length; i++) {
147
+ // headerKeyLength
148
+ const headerKeyAsArray = Utils.toArray(includedHeaders[i][0], 'utf8');
149
+ payloadWriter.writeVarIntNum(headerKeyAsArray.length);
150
+ // headerKey
151
+ payloadWriter.write(headerKeyAsArray);
152
+ // headerValueLength
153
+ const headerValueAsArray = Utils.toArray(includedHeaders[i][1], 'utf8');
154
+ payloadWriter.writeVarIntNum(headerValueAsArray.length);
155
+ // headerValue
156
+ payloadWriter.write(headerValueAsArray);
157
+ }
158
+ // Handle body
159
+ if (parsedBody) {
160
+ const bodyAsArray = Array.from(new Uint8Array(parsedBody));
161
+ payloadWriter.writeVarIntNum(bodyAsArray.length);
162
+ payloadWriter.write(bodyAsArray);
163
+ }
164
+ else {
165
+ payloadWriter.writeVarIntNum(-1);
166
+ }
167
+ // Build the correct AuthMessage for the response
168
+ const responseMessage = {
169
+ version: response.headers.get('x-bsv-auth-version'),
170
+ messageType: response.headers.get('x-bsv-auth-message-type') === 'certificateRequest' ? 'certificateRequest' : 'general',
171
+ identityKey: response.headers.get('x-bsv-auth-identity-key'),
172
+ nonce: response.headers.get('x-bsv-auth-nonce'),
173
+ yourNonce: response.headers.get('x-bsv-auth-your-nonce'),
174
+ requestedCertificates: JSON.parse(response.headers.get('x-bsv-auth-requested-certificates')),
175
+ payload: payloadWriter.toArray(),
176
+ signature: Utils.toArray(response.headers.get('x-bsv-auth-signature'), 'hex'),
177
+ };
178
+ // If the server didn't provide the correct authentication headers, throw an error
179
+ if (!responseMessage.version) {
180
+ throw new Error('HTTP server failed to authenticate');
181
+ }
182
+ // Handle the response if data is received and callback is set
183
+ this.onDataCallback(responseMessage);
184
+ }
185
+ }
186
+ /**
187
+ * Registers a callback to handle incoming messages.
188
+ * This must be called before sending any messages to ensure responses can be processed.
189
+ *
190
+ * @param callback - A function to invoke when an incoming AuthMessage is received.
191
+ * @returns A promise that resolves once the callback is set.
192
+ */
193
+ async onData(callback) {
194
+ this.onDataCallback = (m) => {
195
+ callback(m);
196
+ };
197
+ }
198
+ /**
199
+ * Deserializes a request payload from a byte array into an HTTP request-like structure.
200
+ *
201
+ * @param payload - The serialized payload to deserialize.
202
+ * @returns An object representing the deserialized request, including the method,
203
+ * URL postfix (path and query string), headers, body, and request ID.
204
+ */
205
+ deserializeRequestPayload(payload) {
206
+ // Create a reader
207
+ const requestReader = new Utils.Reader(payload);
208
+ // The first 32 bytes is the requestId
209
+ const requestId = Utils.toBase64(requestReader.read(32));
210
+ // Method
211
+ const methodLength = requestReader.readVarIntNum();
212
+ let method = 'GET';
213
+ if (methodLength > 0) {
214
+ method = Utils.toUTF8(requestReader.read(methodLength));
215
+ }
216
+ // Path
217
+ const pathLength = requestReader.readVarIntNum();
218
+ let path = '';
219
+ if (pathLength > 0) {
220
+ path = Utils.toUTF8(requestReader.read(pathLength));
221
+ }
222
+ // Search
223
+ const searchLength = requestReader.readVarIntNum();
224
+ let search = '';
225
+ if (searchLength > 0) {
226
+ search = Utils.toUTF8(requestReader.read(searchLength));
227
+ }
228
+ // Read headers
229
+ const requestHeaders = {};
230
+ const nHeaders = requestReader.readVarIntNum();
231
+ if (nHeaders > 0) {
232
+ for (let i = 0; i < nHeaders; i++) {
233
+ const nHeaderKeyBytes = requestReader.readVarIntNum();
234
+ const headerKeyBytes = requestReader.read(nHeaderKeyBytes);
235
+ const headerKey = Utils.toUTF8(headerKeyBytes);
236
+ const nHeaderValueBytes = requestReader.readVarIntNum();
237
+ const headerValueBytes = requestReader.read(nHeaderValueBytes);
238
+ const headerValue = Utils.toUTF8(headerValueBytes);
239
+ requestHeaders[headerKey] = headerValue;
240
+ }
241
+ }
242
+ // Read body
243
+ let requestBody;
244
+ const requestBodyBytes = requestReader.readVarIntNum();
245
+ if (requestBodyBytes > 0) {
246
+ requestBody = requestReader.read(requestBodyBytes);
247
+ }
248
+ // Return the deserialized RequestInit
249
+ return {
250
+ urlPostfix: path + search,
251
+ method,
252
+ headers: requestHeaders,
253
+ body: requestBody,
254
+ requestId
255
+ };
256
+ }
257
+ }
258
+ //# sourceMappingURL=SimplifiedFetchTransport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SimplifiedFetchTransport.js","sourceRoot":"","sources":["../../../../../src/auth/transports/SimplifiedFetchTransport.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAEvC,MAAM,oBAAoB,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;AAEvC;;;GAGG;AACH,MAAM,OAAO,wBAAwB;IAC3B,cAAc,CAAiC;IACvD,WAAW,CAAc;IACzB,OAAO,CAAQ;IAEf;;;;OAIG;IACH,YAAY,OAAe,EAAE,WAAW,GAAG,KAAK;QAC9C,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,IAAI,CAAC,OAAoB;QAC7B,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,qFAAqF,CAAC,CAAA;QACxG,CAAC;QAED,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,EAAE,CAAC;YACtC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC,OAAO,mBAAmB,EAAE;gBAC1E,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE;oBACP,cAAc,EAAE,kBAAkB;iBACnC;gBACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;aAC9B,CAAC,CAAA;YACF,8DAA8D;YAC9D,IAAI,QAAQ,CAAC,EAAE,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;gBACvC,MAAM,eAAe,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;gBAC7C,IAAI,eAAe,EAAE,MAAM,KAAK,sBAAsB,EAAE,CAAC;oBACvD,IAAI,CAAC,cAAc,CAAC,eAA8B,CAAC,CAAA;gBACrD,CAAC;YACH,CAAC;iBAAM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":["../../../../../src/auth/transports/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/auth/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,47 @@
1
+ import { SymmetricKey, Utils, ProtoWallet } from "../../../mod.js";
2
+ import { MasterCertificate } from "../certificates/MasterCertificate.js";
3
+ import { VerifiableCertificate } from "../certificates/VerifiableCertificate.js";
4
+ /**
5
+ * Creates a Master Certificate by encrypting provided fields and generating a master keyring.
6
+ *
7
+ * @param {Wallet} wallet - The wallet instance used for encryption and public key retrieval.
8
+ * @param {Record<string, string>} fields - The certificate fields to encrypt.
9
+ * @param {string} certificateType - The type of the certificate being created.
10
+ * @param {string} certificateSerialNumber - The serial number of the certificate.
11
+ * @param {string} certifierPublicKey - The public key of the certifier.
12
+ * @returns {Promise<MasterCertificate>} A promise resolving to the created Master Certificate.
13
+ */
14
+ export async function createMasterCertificate(wallet, fields, certificateType, certificateSerialNumber, certifierPublicKey) {
15
+ const certificateFields = {};
16
+ const masterKeyring = {};
17
+ for (const fieldName in fields) {
18
+ const fieldSymmetricKey = SymmetricKey.fromRandom();
19
+ const encryptedFieldValue = fieldSymmetricKey.encrypt(Utils.toArray(fields[fieldName], 'utf8'));
20
+ certificateFields[fieldName] = Utils.toBase64(encryptedFieldValue);
21
+ const encryptedFieldKey = await wallet.encrypt({
22
+ plaintext: fieldSymmetricKey.toArray(),
23
+ protocolID: [2, 'certificate field encryption'],
24
+ keyID: `${certificateSerialNumber} ${fieldName}`,
25
+ counterparty: 'self'
26
+ });
27
+ masterKeyring[fieldName] = Utils.toBase64(encryptedFieldKey.ciphertext);
28
+ }
29
+ return new MasterCertificate(certificateType, certificateSerialNumber, (await wallet.getPublicKey({ identityKey: true })).publicKey, certifierPublicKey, 'revocationOutpoint', certificateFields, masterKeyring);
30
+ }
31
+ /**
32
+ * Creates a Verifiable Certificate by signing a Master Certificate and generating a keyring for a verifier.
33
+ *
34
+ * @param {MasterCertificate} masterCertificate - The master certificate to convert into a verifiable certificate.
35
+ * @param {Wallet} wallet - The wallet instance used for generating a keyring for the verifier.
36
+ * @param {string} verifierIdentityKey - The identity key of the verifier.
37
+ * @param {string[]} fieldsToReveal - The list of fields to reveal to the verifier.
38
+ * @param {PrivateKey} certifierPrivateKey - The private key of the certifier for signing the certificate.
39
+ * @returns {Promise<VerifiableCertificate>} A promise resolving to the created Verifiable Certificate.
40
+ */
41
+ export async function createVerifiableCertificate(masterCertificate, wallet, verifierIdentityKey, fieldsToReveal, certifierPrivateKey) {
42
+ const certifierWallet = new ProtoWallet(certifierPrivateKey);
43
+ await masterCertificate.sign(certifierWallet);
44
+ const keyringForVerifier = await masterCertificate.createKeyringForVerifier(wallet, verifierIdentityKey, fieldsToReveal);
45
+ return new VerifiableCertificate(masterCertificate.type, masterCertificate.serialNumber, masterCertificate.subject, masterCertificate.certifier, masterCertificate.revocationOutpoint, masterCertificate.fields, masterCertificate.signature, keyringForVerifier);
46
+ }
47
+ //# sourceMappingURL=certificateHelpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"certificateHelpers.js","sourceRoot":"","sources":["../../../../../src/auth/utils/certificateHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,YAAY,EAAE,KAAK,EAAU,WAAW,EAAE,MAAM,iBAAiB,CAAA;AACtF,OAAO,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAA;AACxE,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAA;AAEhF;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,MAAc,EACd,MAA8B,EAC9B,eAAuB,EACvB,uBAA+B,EAC/B,kBAA0B;IAE1B,MAAM,iBAAiB,GAA2B,EAAE,CAAA;IACpD,MAAM,aAAa,GAA2B,EAAE,CAAA;IAEh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,cAAc,CACf,CAAA;IAED,OAAO,IAAI,qBAAqB,CAC9B,iBAAiB,CAAC,IAAI,EACtB,iBAAiB,CAAC,YAAY,EAC9B,iBAAiB,CAAC,OAAO,EACzB,iBAAiB,CAAC,SAAS,EAC3B,iBAAiB,CAAC,kBAAkB,EACpC,iBAAiB,CAAC,MAAM,EACxB,iBAAiB,CAAC,SAAS,EAC3B,kBAAkB,CACnB,CAAA;AACH,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { Utils, Random } from '../../../mod.js';
2
+ /**
3
+ * Creates a nonce derived from a privateKey
4
+ * @param wallet
5
+ * @returns A random nonce derived with a wallet
6
+ */
7
+ export async function createNonce(wallet) {
8
+ // Generate 16 random bytes for the first half of the data
9
+ const firstHalf = Random(16);
10
+ // Create an sha256 HMAC
11
+ const { hmac } = await wallet.createHmac({ protocolID: [2, 'server hmac'], keyID: Utils.toUTF8(firstHalf), data: firstHalf, counterparty: 'self' });
12
+ // Concatenate firstHalf and secondHalf as the nonce bytes
13
+ const nonceBytes = [...firstHalf, ...hmac];
14
+ return Utils.toBase64(nonceBytes);
15
+ }
16
+ //# sourceMappingURL=createNonce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createNonce.js","sourceRoot":"","sources":["../../../../../src/auth/utils/createNonce.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAU,MAAM,iBAAiB,CAAA;AAEvD;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,MAAc;IAC9C,0DAA0D;IAC1D,MAAM,SAAS,GAAG,MAAM,CAAC,EAAE,CAAC,CAAA;IAC5B,wBAAwB;IACxB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,EAAE,aAAa,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC,CAAA;IACnJ,0DAA0D;IAC1D,MAAM,UAAU,GAAG,CAAC,GAAG,SAAS,EAAE,GAAG,IAAI,CAAC,CAAA;IAC1C,OAAO,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAA;AACnC,CAAC"}
@@ -0,0 +1,27 @@
1
+ import { VerifiableCertificate } from "../certificates/VerifiableCertificate.js";
2
+ /**
3
+ * Retrieves an array of verifiable certificates based on the request.
4
+ *
5
+ * @private
6
+ * @param {RequestedCertificateSet} requestedCertificates - The set of certificates requested by the peer.
7
+ * @param {string} verifierIdentityKey - The public key of the verifier requesting the certificates.
8
+ * @returns {Promise<VerifiableCertificate[]>} An array of verifiable certificates.
9
+ */
10
+ export const getVerifiableCertificates = async (wallet, requestedCertificates, verifierIdentityKey) => {
11
+ // Find matching certificates we have
12
+ // Note: This may return multiple certificates that match the correct type.
13
+ const matchingCertificates = await wallet.listCertificates({
14
+ certifiers: requestedCertificates.certifiers,
15
+ types: Object.keys(requestedCertificates.types)
16
+ });
17
+ // For each certificate requested, create a verifiable cert with selectively revealed fields
18
+ return await Promise.all(matchingCertificates.certificates.map(async (certificate) => {
19
+ const { keyringForVerifier } = await wallet.proveCertificate({
20
+ certificate,
21
+ fieldsToReveal: requestedCertificates.types[certificate.type],
22
+ verifier: verifierIdentityKey
23
+ });
24
+ return new VerifiableCertificate(certificate.type, certificate.serialNumber, certificate.subject, certificate.certifier, certificate.revocationOutpoint, certificate.fields, certificate.signature, keyringForVerifier);
25
+ }));
26
+ };
27
+ //# sourceMappingURL=getVerifiableCertificates.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getVerifiableCertificates.js","sourceRoot":"","sources":["../../../../../src/auth/utils/getVerifiableCertificates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAA;AAIhF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,KAAK,EAAE,MAAc,EAAE,qBAA8C,EAAE,mBAA2B,EAAoC,EAAE;IAC/K,qCAAqC;IACrC,2EAA2E;IAC3E,MAAM,oBAAoB,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC;QACzD,UAAU,EAAE,qBAAqB,CAAC,UAAU;QAC5C,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC,KAAK,CAAC;KAChD,CAAC,CAAA;IAEF,4FAA4F;IAC5F,OAAO,MAAM,OAAO,CAAC,GAAG,CACtB,oBAAoB,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,EAAC,WAAW,EAAC,EAAE;QACxD,MAAM,EAAE,kBAAkB,EAAE,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC;YAC3D,WAAW;YACX,cAAc,EAAE,qBAAqB,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC;YAC7D,QAAQ,EAAE,mBAAmB;SAC9B,CAAC,CAAA;QACF,OAAO,IAAI,qBAAqB,CAC9B,WAAW,CAAC,IAAI,EAChB,WAAW,CAAC,YAAY,EACxB,WAAW,CAAC,OAAO,EACnB,WAAW,CAAC,SAAS,EACrB,WAAW,CAAC,kBAAkB,EAC9B,WAAW,CAAC,MAAM,EAClB,WAAW,CAAC,SAAS,EACrB,kBAAkB,CACnB,CAAA;IACH,CAAC,CAAC,CAAC,CAAA;AACP,CAAC,CAAA"}
@@ -0,0 +1,5 @@
1
+ export * from './verifyNonce.js';
2
+ export * from './createNonce.js';
3
+ export * from './getVerifiableCertificates.js';
4
+ export * from './validateCertificates.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/auth/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,gCAAgC,CAAA;AAC9C,cAAc,2BAA2B,CAAA"}
@@ -0,0 +1,38 @@
1
+ import { VerifiableCertificate } from "../certificates/VerifiableCertificate.js";
2
+ /**
3
+ * Validates and processes the certificates received from a peer.
4
+ *
5
+ * @private
6
+ * @param {AuthMessage} message - The message containing the certificates to validate.
7
+ * @returns {Promise<void>}
8
+ * @throws Will throw an error if certificate validation or field decryption fails.
9
+ */
10
+ export const validateCertificates = async (verifierWallet, message, certificatesRequested) => {
11
+ await Promise.all(message.certificates.map(async (incomingCert) => {
12
+ if (incomingCert.subject !== message.identityKey) {
13
+ throw new Error(`The subject of one of your certificates ("${incomingCert.subject}") is not the same as the request sender ("${message.identityKey}").`);
14
+ }
15
+ // Verify Certificate structure and signature
16
+ const certToVerify = new VerifiableCertificate(incomingCert.type, incomingCert.serialNumber, incomingCert.subject, incomingCert.certifier, incomingCert.revocationOutpoint, incomingCert.fields, incomingCert.signature, incomingCert.keyring);
17
+ const isValidCert = await certToVerify.verify();
18
+ if (!isValidCert) {
19
+ throw new Error(`The signature for the certificate with serial number ${certToVerify.serialNumber} is invalid!`);
20
+ }
21
+ // Check if the certificate matches requested certifiers, types, and fields
22
+ if (certificatesRequested) {
23
+ const { certifiers, types } = certificatesRequested;
24
+ // Check certifier matches
25
+ if (!certifiers.includes(certToVerify.certifier)) {
26
+ throw new Error(`Certificate with serial number ${certToVerify.serialNumber} has an unrequested certifier: ${certToVerify.certifier}`);
27
+ }
28
+ // Check type and fields match requested
29
+ const requestedFields = types[certToVerify.type];
30
+ if (!requestedFields) {
31
+ throw new Error(`Certificate with type ${certToVerify.type} was not requested`);
32
+ }
33
+ }
34
+ // Attempt to decrypt fields
35
+ await certToVerify.decryptFields(verifierWallet);
36
+ }));
37
+ };
38
+ //# sourceMappingURL=validateCertificates.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateCertificates.js","sourceRoot":"","sources":["../../../../../src/auth/utils/validateCertificates.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAA;AAEhF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,o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E,CAAC;gBACrB,MAAM,IAAI,KAAK,CAAC,yBAAyB,YAAY,CAAC,IAAI,oBAAoB,CAAC,CAAA;YACjF,CAAC;QACH,CAAC;QAED,4BAA4B;QAC5B,MAAM,YAAY,CAAC,aAAa,CAAC,cAAc,CAAC,CAAA;IAClD,CAAC,CAAC,CAAC,CAAA;AACL,CAAC,CAAA"}
@@ -0,0 +1,24 @@
1
+ import { Utils } from '../../../mod.js';
2
+ /**
3
+ * Verifies a nonce derived from a wallet
4
+ * @param nonce - A nonce to verify as a base64 string.
5
+ * @param wallet
6
+ * @returns The status of the validation
7
+ */
8
+ export async function verifyNonce(nonce, wallet) {
9
+ // Convert nonce from base64 string to Uint8Array
10
+ const buffer = Utils.toArray(nonce, 'base64');
11
+ // Split the nonce buffer
12
+ const data = buffer.slice(0, 16);
13
+ const hmac = buffer.slice(16);
14
+ // Calculate the HMAC
15
+ const { valid } = await wallet.verifyHmac({
16
+ data,
17
+ hmac,
18
+ protocolID: [2, 'server hmac'],
19
+ keyID: Utils.toUTF8(data),
20
+ counterparty: 'self'
21
+ });
22
+ return valid;
23
+ }
24
+ //# sourceMappingURL=verifyNonce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verifyNonce.js","sourceRoot":"","sources":["../../../../../src/auth/utils/verifyNonce.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAU,MAAM,iBAAiB,CAAA;AAE/C;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,KAAa,EAAE,MAAc;IAC7D,iDAAiD;IACjD,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;IAE7C,yBAAyB;IACzB,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IAChC,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IAE7B,qBAAqB;IACrB,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC;QACxC,IAAI;QACJ,IAAI;QACJ,UAAU,EAAE,CAAC,CAAC,EAAE,aAAa,CAAC;QAC9B,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC;QACzB,YAAY,EAAE,MAAM;KACrB,CAAC,CAAA;IAEF,OAAO,KAAK,CAAA;AACd,CAAC"}
@@ -1,6 +1,6 @@
1
1
  import { Utils } from '../../primitives/index.js';
2
2
  import calls from './WalletWireCalls.js';
3
- import Certificate from '../../auth/Certificate.js';
3
+ import Certificate from '../../auth/certificates/Certificate.js';
4
4
  /**
5
5
  * Processes incoming wallet calls received over a wallet wire, with a given wallet.
6
6
  */