@canton-network/core-ledger-client 1.8.0 → 1.10.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.
- package/dist/index.cjs +399 -519
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +374 -514
- package/dist/index.js.map +1 -1
- package/dist/ledger-client.d.ts +2 -0
- package/dist/ledger-client.d.ts.map +1 -1
- package/package.json +15 -15
package/dist/index.js
CHANGED
|
@@ -1,527 +1,387 @@
|
|
|
1
|
-
import { supportedLedgerApiVersions } from
|
|
2
|
-
import createClient from
|
|
3
|
-
import { ErrorInfo, RetryInfo } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { LedgerGetRoutes, LedgerPostRoutes, supportedLedgerApiVersions } from "@canton-network/core-ledger-client-types";
|
|
2
|
+
import createClient from "openapi-fetch";
|
|
3
|
+
import { ErrorInfo, RetryInfo } from "@canton-network/core-ledger-proto";
|
|
4
|
+
//#region src/ledger-api-utils.ts
|
|
5
|
+
const COMPLETIONS_LIMIT = "100";
|
|
6
|
+
const COMPLETIONS_STREAM_IDLE_TIMEOUT_MS = "1000";
|
|
7
|
+
/**
|
|
8
|
+
* Polls the completions endpoint until
|
|
9
|
+
* the completion with the given (userId, commandId, submissionId) is returned.
|
|
10
|
+
* Then returns the updateId, synchronizerId and recordTime of that completion.
|
|
11
|
+
*/
|
|
10
12
|
async function awaitCompletion(ledgerClient, ledgerEnd, partyId, userId, commandIdOrSubmissionId) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return false;
|
|
38
|
-
});
|
|
39
|
-
if (wantedCompletion?.completionResponse && "Completion" in wantedCompletion.completionResponse) {
|
|
40
|
-
const completion = wantedCompletion.completionResponse.Completion.value;
|
|
41
|
-
const status = completion.status;
|
|
42
|
-
if (status && status.code !== 0) {
|
|
43
|
-
throw asGrpcError(status);
|
|
44
|
-
}
|
|
45
|
-
return completion;
|
|
46
|
-
} else {
|
|
47
|
-
const lastCompletion = completions[completions.length - 1];
|
|
48
|
-
const newLedgerEnd = lastCompletion?.completionResponse && "Completion" in lastCompletion.completionResponse ? lastCompletion.completionResponse.Completion.value.offset : void 0;
|
|
49
|
-
return awaitCompletion(
|
|
50
|
-
ledgerClient,
|
|
51
|
-
newLedgerEnd || ledgerEnd,
|
|
52
|
-
// !newLedgerEnd implies response was empty
|
|
53
|
-
partyId,
|
|
54
|
-
userId,
|
|
55
|
-
commandIdOrSubmissionId
|
|
56
|
-
);
|
|
57
|
-
}
|
|
13
|
+
const responses = await ledgerClient.postWithRetry("/v2/commands/completions", {
|
|
14
|
+
userId,
|
|
15
|
+
parties: [partyId],
|
|
16
|
+
beginExclusive: ledgerEnd
|
|
17
|
+
}, defaultRetryableOptions, { query: {
|
|
18
|
+
limit: COMPLETIONS_LIMIT,
|
|
19
|
+
stream_idle_timeout_ms: COMPLETIONS_STREAM_IDLE_TIMEOUT_MS
|
|
20
|
+
} });
|
|
21
|
+
const completions = responses.filter((r) => r.completionResponse && "Completion" in r.completionResponse);
|
|
22
|
+
const wantedCompletion = responses.find((r) => {
|
|
23
|
+
if (r.completionResponse && "Completion" in r.completionResponse) {
|
|
24
|
+
const completion = r.completionResponse.Completion?.value;
|
|
25
|
+
if (!completion) return false;
|
|
26
|
+
return completion.userId === userId && (completion.commandId === commandIdOrSubmissionId || completion.submissionId === commandIdOrSubmissionId);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
});
|
|
30
|
+
if (wantedCompletion?.completionResponse && "Completion" in wantedCompletion.completionResponse) {
|
|
31
|
+
const completion = wantedCompletion.completionResponse.Completion.value;
|
|
32
|
+
const status = completion.status;
|
|
33
|
+
if (status && status.code !== 0) throw asGrpcError(status);
|
|
34
|
+
return completion;
|
|
35
|
+
} else {
|
|
36
|
+
const lastCompletion = completions[completions.length - 1];
|
|
37
|
+
return awaitCompletion(ledgerClient, (lastCompletion?.completionResponse && "Completion" in lastCompletion.completionResponse ? lastCompletion.completionResponse.Completion.value.offset : void 0) || ledgerEnd, partyId, userId, commandIdOrSubmissionId);
|
|
38
|
+
}
|
|
58
39
|
}
|
|
59
40
|
async function promiseWithTimeout(promise, timeoutMs, errorMessage) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
}
|
|
41
|
+
let timeoutPid = null;
|
|
42
|
+
const timeoutPromise = new Promise((_resolve, reject) => {
|
|
43
|
+
timeoutPid = setTimeout(() => reject(errorMessage), timeoutMs);
|
|
44
|
+
});
|
|
45
|
+
try {
|
|
46
|
+
return await Promise.race([promise, timeoutPromise]);
|
|
47
|
+
} finally {
|
|
48
|
+
if (timeoutPid) clearTimeout(timeoutPid);
|
|
49
|
+
}
|
|
71
50
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
51
|
+
const defaultRetryableOptions = {
|
|
52
|
+
retries: 5,
|
|
53
|
+
delayMs: 3e3,
|
|
54
|
+
cantonErrorKeys: [
|
|
55
|
+
"SEQUENCER_REQUEST_FAILED",
|
|
56
|
+
"SEQUENCER_BACKPRESSURE",
|
|
57
|
+
"SUBMISSION_ALREADY_IN_FLIGHT",
|
|
58
|
+
"LOCAL_VERDICT_TIMEOUT",
|
|
59
|
+
"NOT_SEQUENCED_TIMEOUT",
|
|
60
|
+
"NO_VIEW_WITH_VALID_RECIPIENTS"
|
|
61
|
+
]
|
|
83
62
|
};
|
|
84
63
|
async function retryable(fn, retryableOptions, logger) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
);
|
|
98
|
-
await new Promise(
|
|
99
|
-
(res) => setTimeout(res, retryableOptions.delayMs)
|
|
100
|
-
);
|
|
101
|
-
} else {
|
|
102
|
-
throw grpcError;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
throw new Error("retryable: Exceeded maximum retries without throwing.");
|
|
64
|
+
for (let attempts = 1; attempts <= retryableOptions.retries; attempts++) try {
|
|
65
|
+
return await fn();
|
|
66
|
+
} catch (err) {
|
|
67
|
+
const grpcError = asGrpcError(err);
|
|
68
|
+
const message = grpcError.message;
|
|
69
|
+
const shouldRetry = retryableOptions.cantonErrorKeys.length === 0 || retryableOptions.cantonErrorKeys.some((key) => message.includes(key));
|
|
70
|
+
if (attempts < retryableOptions.retries && shouldRetry) {
|
|
71
|
+
logger?.warn(`Caught retryiable error: ${message}. Retrying attempt ${attempts} of ${retryableOptions.retries}...`);
|
|
72
|
+
await new Promise((res) => setTimeout(res, retryableOptions.delayMs));
|
|
73
|
+
} else throw grpcError;
|
|
74
|
+
}
|
|
75
|
+
throw new Error("retryable: Exceeded maximum retries without throwing.");
|
|
107
76
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
} else {
|
|
113
|
-
throw e;
|
|
114
|
-
}
|
|
77
|
+
const isJsCantonError = (e) => typeof e === "object" && e !== null && "code" in e && "cause" in e && "errorCategory" in e;
|
|
78
|
+
const asJsCantonError = (e) => {
|
|
79
|
+
if (isJsCantonError(e)) return e;
|
|
80
|
+
else throw e;
|
|
115
81
|
};
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return { code, message, errorInfo, retryInfo };
|
|
140
|
-
}
|
|
141
|
-
throw e;
|
|
82
|
+
const asGrpcError = (e) => {
|
|
83
|
+
let errorInfo;
|
|
84
|
+
let retryInfo;
|
|
85
|
+
if (typeof e === "object" && e !== null && "code" in e && "message" in e && "details" in e && Array.isArray(e.details)) {
|
|
86
|
+
const code = e.code;
|
|
87
|
+
const message = e.message;
|
|
88
|
+
for (const detail of e.details) if (detail.typeUrl === "type.googleapis.com/google.rpc.ErrorInfo" && typeof detail.value === "string") try {
|
|
89
|
+
errorInfo = ErrorInfo.fromBinary(Buffer.from(detail.value, "base64"));
|
|
90
|
+
} catch {}
|
|
91
|
+
else if (detail.typeUrl === "type.googleapis.com/google.rpc.RetryInfo" && typeof detail.value === "string") try {
|
|
92
|
+
retryInfo = RetryInfo.fromBinary(Buffer.from(detail.value, "base64"));
|
|
93
|
+
} catch {}
|
|
94
|
+
return {
|
|
95
|
+
code,
|
|
96
|
+
message,
|
|
97
|
+
errorInfo,
|
|
98
|
+
retryInfo
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
throw e;
|
|
142
102
|
};
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region \0@oxc-project+runtime@0.142.0/helpers/esm/typeof.js
|
|
105
|
+
function _typeof(o) {
|
|
106
|
+
"@babel/helpers - typeof";
|
|
107
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
108
|
+
return typeof o;
|
|
109
|
+
} : function(o) {
|
|
110
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
111
|
+
}, _typeof(o);
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPrimitive.js
|
|
115
|
+
function toPrimitive(t, r) {
|
|
116
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
117
|
+
var e = t[Symbol.toPrimitive];
|
|
118
|
+
if (void 0 !== e) {
|
|
119
|
+
var i = e.call(t, r || "default");
|
|
120
|
+
if ("object" != _typeof(i)) return i;
|
|
121
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
122
|
+
}
|
|
123
|
+
return ("string" === r ? String : Number)(t);
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPropertyKey.js
|
|
127
|
+
function toPropertyKey(t) {
|
|
128
|
+
var i = toPrimitive(t, "string");
|
|
129
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
130
|
+
}
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region \0@oxc-project+runtime@0.142.0/helpers/esm/defineProperty.js
|
|
133
|
+
function _defineProperty(e, r, t) {
|
|
134
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
135
|
+
value: t,
|
|
136
|
+
enumerable: !0,
|
|
137
|
+
configurable: !0,
|
|
138
|
+
writable: !0
|
|
139
|
+
}) : e[r] = t, e;
|
|
140
|
+
}
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/ledger-client.ts
|
|
143
|
+
const supportedVersions = supportedLedgerApiVersions;
|
|
144
|
+
function isValidGetEndpoint(resource) {
|
|
145
|
+
return LedgerGetRoutes.has(resource);
|
|
146
|
+
}
|
|
147
|
+
function isValidPostEndpoint(resource) {
|
|
148
|
+
return LedgerPostRoutes.has(resource);
|
|
149
|
+
}
|
|
146
150
|
var LedgerClient = class {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
380
|
-
if (userRightsOptions.canExecuteAsAnyParty) {
|
|
381
|
-
rights.push({
|
|
382
|
-
kind: {
|
|
383
|
-
CanExecuteAsAnyParty: {
|
|
384
|
-
value: {}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
const result = await this.post(
|
|
390
|
-
"/v2/users/{user-id}/rights",
|
|
391
|
-
{
|
|
392
|
-
identityProviderId: "",
|
|
393
|
-
userId,
|
|
394
|
-
rights
|
|
395
|
-
},
|
|
396
|
-
{
|
|
397
|
-
path: {
|
|
398
|
-
"user-id": userId
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
);
|
|
402
|
-
if (!result.newlyGrantedRights) {
|
|
403
|
-
throw new Error("Failed to grant user rights");
|
|
404
|
-
}
|
|
405
|
-
return result;
|
|
406
|
-
}
|
|
407
|
-
async allocateExternalParty(synchronizerId, onboardingTransactions, multiHashSignatures) {
|
|
408
|
-
await this.init();
|
|
409
|
-
const resp = await this.currentClient.POST(
|
|
410
|
-
"/v2/parties/external/allocate",
|
|
411
|
-
{
|
|
412
|
-
body: {
|
|
413
|
-
synchronizer: synchronizerId,
|
|
414
|
-
identityProviderId: "",
|
|
415
|
-
onboardingTransactions,
|
|
416
|
-
multiHashSignatures
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
);
|
|
420
|
-
return this.valueOrError(resp);
|
|
421
|
-
}
|
|
422
|
-
async generateTopology(synchronizerId, publicKey, partyHint, localParticipantObservationOnly = false, confirmationThreshold = 1, otherConfirmingParticipantUids = [], observingParticipantUids = []) {
|
|
423
|
-
await this.init();
|
|
424
|
-
const body = {
|
|
425
|
-
synchronizer: synchronizerId,
|
|
426
|
-
partyHint,
|
|
427
|
-
publicKey: {
|
|
428
|
-
format: "CRYPTO_KEY_FORMAT_RAW",
|
|
429
|
-
keyData: publicKey,
|
|
430
|
-
keySpec: "SIGNING_KEY_SPEC_EC_CURVE25519"
|
|
431
|
-
},
|
|
432
|
-
localParticipantObservationOnly,
|
|
433
|
-
confirmationThreshold,
|
|
434
|
-
otherConfirmingParticipantUids,
|
|
435
|
-
observingParticipantUids
|
|
436
|
-
};
|
|
437
|
-
this.logger.debug(body, "generateTopology request body");
|
|
438
|
-
const resp = await this.currentClient.POST(
|
|
439
|
-
"/v2/parties/external/generate-topology",
|
|
440
|
-
{ body }
|
|
441
|
-
);
|
|
442
|
-
return this.valueOrError(resp);
|
|
443
|
-
}
|
|
444
|
-
// Retrieve an (arbitrary) synchronizer id from the validator.
|
|
445
|
-
// This synchronizer id is cached for the remainder of this object's life.
|
|
446
|
-
async getSynchronizerId() {
|
|
447
|
-
if (this.synchronizerId) return this.synchronizerId;
|
|
448
|
-
const response = await this.getWithRetry(
|
|
449
|
-
"/v2/state/connected-synchronizers"
|
|
450
|
-
);
|
|
451
|
-
if (!response.connectedSynchronizers?.[0]) {
|
|
452
|
-
throw new Error("No connected synchronizers found");
|
|
453
|
-
}
|
|
454
|
-
const synchronizerId = response.connectedSynchronizers[0].synchronizerId;
|
|
455
|
-
if (response.connectedSynchronizers.length > 1) {
|
|
456
|
-
this.logger.warn(
|
|
457
|
-
`Found ${response.connectedSynchronizers.length} synchronizers, defaulting to ${synchronizerId}`
|
|
458
|
-
);
|
|
459
|
-
}
|
|
460
|
-
this.synchronizerId = synchronizerId;
|
|
461
|
-
return synchronizerId;
|
|
462
|
-
}
|
|
463
|
-
async postWithRetry(path, body, retryOptions = defaultRetryableOptions, params, additionalOptions) {
|
|
464
|
-
return await retryable(
|
|
465
|
-
() => this.post(path, body, params, additionalOptions),
|
|
466
|
-
retryOptions,
|
|
467
|
-
this.logger
|
|
468
|
-
).catch((e) => {
|
|
469
|
-
this.logger.warn(
|
|
470
|
-
`Error in postWithRetry for path ${path} with body retry options ${JSON.stringify(retryOptions)}`
|
|
471
|
-
);
|
|
472
|
-
this.logger.debug(JSON.stringify(body));
|
|
473
|
-
throw asJsCantonError(e);
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
async getWithRetry(path, retryOptions = defaultRetryableOptions, params) {
|
|
477
|
-
return await retryable(
|
|
478
|
-
() => this.get(path, params),
|
|
479
|
-
retryOptions,
|
|
480
|
-
this.logger
|
|
481
|
-
).catch((e) => {
|
|
482
|
-
this.logger.warn(
|
|
483
|
-
`Error in getWithRetry for path ${path} with retry options ${JSON.stringify(retryOptions)}`
|
|
484
|
-
);
|
|
485
|
-
throw asJsCantonError(e);
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
async patchWithRetry(path, body, retryOptions = defaultRetryableOptions, params, additionalOptions) {
|
|
489
|
-
return await retryable(
|
|
490
|
-
() => this.patch(path, body, params, additionalOptions),
|
|
491
|
-
retryOptions,
|
|
492
|
-
this.logger
|
|
493
|
-
).catch((e) => {
|
|
494
|
-
this.logger.warn(
|
|
495
|
-
`Error in patchWithRetry for path ${path} with body retry options ${JSON.stringify(retryOptions)}`
|
|
496
|
-
);
|
|
497
|
-
throw asJsCantonError(e);
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
async patch(path, body, params, additionalOptions) {
|
|
501
|
-
const options = { body, params, ...additionalOptions };
|
|
502
|
-
const resp = await this.currentClient.PATCH(path, options);
|
|
503
|
-
return this.valueOrError(resp);
|
|
504
|
-
}
|
|
505
|
-
async post(path, body, params, additionalOptions) {
|
|
506
|
-
const options = { body, params, ...additionalOptions };
|
|
507
|
-
const resp = await this.currentClient.POST(path, options);
|
|
508
|
-
return this.valueOrError(resp);
|
|
509
|
-
}
|
|
510
|
-
async get(path, params) {
|
|
511
|
-
await this.init();
|
|
512
|
-
const options = { params };
|
|
513
|
-
const resp = await this.currentClient.GET(path, options);
|
|
514
|
-
return this.valueOrError(resp);
|
|
515
|
-
}
|
|
516
|
-
async valueOrError(response) {
|
|
517
|
-
if (response.data === void 0) {
|
|
518
|
-
return Promise.reject(response.error);
|
|
519
|
-
} else {
|
|
520
|
-
return Promise.resolve(response.data);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
151
|
+
constructor({ baseUrl, logger, accessTokenProvider, version }) {
|
|
152
|
+
_defineProperty(this, "clients", void 0);
|
|
153
|
+
_defineProperty(this, "clientVersion", "3.5");
|
|
154
|
+
_defineProperty(this, "initialized", false);
|
|
155
|
+
_defineProperty(this, "accessTokenProvider", void 0);
|
|
156
|
+
_defineProperty(this, "logger", void 0);
|
|
157
|
+
_defineProperty(this, "synchronizerId", void 0);
|
|
158
|
+
_defineProperty(this, "baseUrl", void 0);
|
|
159
|
+
this.logger = logger.child({ component: "LedgerClient" });
|
|
160
|
+
this.accessTokenProvider = accessTokenProvider;
|
|
161
|
+
const authenticatedFetch = async (url, options = {}) => {
|
|
162
|
+
const token = await this.accessTokenProvider.getAccessToken();
|
|
163
|
+
return fetch(url, {
|
|
164
|
+
...options,
|
|
165
|
+
headers: {
|
|
166
|
+
...options.headers || {},
|
|
167
|
+
...token && { Authorization: `Bearer ${token}` }
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
this.clients = { ...supportedVersions.reduce((acc, version) => {
|
|
172
|
+
acc[version] = createClient({
|
|
173
|
+
baseUrl: baseUrl.href,
|
|
174
|
+
fetch: authenticatedFetch
|
|
175
|
+
});
|
|
176
|
+
return acc;
|
|
177
|
+
}, {}) };
|
|
178
|
+
this.clientVersion = version ?? this.clientVersion;
|
|
179
|
+
this.baseUrl = baseUrl;
|
|
180
|
+
}
|
|
181
|
+
get currentClient() {
|
|
182
|
+
return this.clients[this.clientVersion];
|
|
183
|
+
}
|
|
184
|
+
async init() {
|
|
185
|
+
if (!this.initialized) {
|
|
186
|
+
this.logger.debug(`Initializing LedgerClient with version ${this.clientVersion} for url ${this.baseUrl.href}`);
|
|
187
|
+
const versionFromClient = await this.currentClient.GET("/v2/version");
|
|
188
|
+
this.logger.debug({
|
|
189
|
+
data: versionFromClient.data,
|
|
190
|
+
status: versionFromClient.response.status
|
|
191
|
+
}, "getV2Version response");
|
|
192
|
+
this.clientVersion = this.parseSupportedVersions(versionFromClient.data?.version);
|
|
193
|
+
this.initialized = true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
getCurrentClientVersion() {
|
|
197
|
+
return this.clientVersion;
|
|
198
|
+
}
|
|
199
|
+
parseSupportedVersions(version) {
|
|
200
|
+
if (!version) throw new Error("Client version missing from response");
|
|
201
|
+
const match = supportedVersions.find((v) => version.startsWith(v));
|
|
202
|
+
if (!match) {
|
|
203
|
+
this.logger.warn(`Unknown support of version ${version} (defaulting to ${this.clientVersion}) please check if a newer version is available that support this version of canton.`);
|
|
204
|
+
return this.clientVersion;
|
|
205
|
+
}
|
|
206
|
+
return match;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Check if a party exists
|
|
210
|
+
*
|
|
211
|
+
* @param partyId The ID of the party to look for
|
|
212
|
+
* @returns A promise to resolves to a boolean.
|
|
213
|
+
*/
|
|
214
|
+
async checkIfPartyExists(partyId) {
|
|
215
|
+
try {
|
|
216
|
+
const party = await this.get("/v2/parties/{party}", { path: { party: partyId } });
|
|
217
|
+
return party.partyDetails !== void 0 && party.partyDetails[0].party === partyId;
|
|
218
|
+
} catch {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* grant "Master User" rights to a user.
|
|
224
|
+
*
|
|
225
|
+
* this require running with an admin token.
|
|
226
|
+
*
|
|
227
|
+
* @param userId The ID of the user to grant rights to.
|
|
228
|
+
* @param canReadAsAnyParty define if the user can read as any party.
|
|
229
|
+
* @param canExecuteAsAnyParty define if the user can execute as any party.
|
|
230
|
+
*/
|
|
231
|
+
async grantMasterUserRights(userId, canReadAsAnyParty, canExecuteAsAnyParty) {
|
|
232
|
+
const rights = [];
|
|
233
|
+
if (canReadAsAnyParty) rights.push({ kind: { CanReadAsAnyParty: { value: {} } } });
|
|
234
|
+
if (canExecuteAsAnyParty) rights.push({ kind: { CanExecuteAsAnyParty: { value: {} } } });
|
|
235
|
+
if (!(await this.post("/v2/users/{user-id}/rights", {
|
|
236
|
+
identityProviderId: "",
|
|
237
|
+
userId,
|
|
238
|
+
rights
|
|
239
|
+
}, { path: { "user-id": userId } })).newlyGrantedRights) throw new Error("Failed to grant user rights");
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Create a new user.
|
|
243
|
+
*
|
|
244
|
+
* @param userId The ID of the user to create.
|
|
245
|
+
* @param primaryParty The primary party of the user.
|
|
246
|
+
*/
|
|
247
|
+
async createUser(userId, primaryParty) {
|
|
248
|
+
try {
|
|
249
|
+
const existing = await this.get("/v2/users/{user-id}", { path: { "user-id": userId } });
|
|
250
|
+
if (existing && existing.user) return existing.user;
|
|
251
|
+
} catch {}
|
|
252
|
+
return (await this.post("/v2/users", {
|
|
253
|
+
user: {
|
|
254
|
+
identityProviderId: "",
|
|
255
|
+
id: userId,
|
|
256
|
+
isDeactivated: false,
|
|
257
|
+
primaryParty
|
|
258
|
+
},
|
|
259
|
+
rights: [{ kind: { ParticipantAdmin: { value: {} } } }]
|
|
260
|
+
})).user;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Grants a user the right to act as a party, while ensuring the party exists.
|
|
264
|
+
*
|
|
265
|
+
* @param userId The ID of the user to grant rights to.
|
|
266
|
+
* @param partyId The ID of the party to grant rights for.
|
|
267
|
+
* @param maxTries Optional max number of retries with default 30. May be increased if expecting heavy load.
|
|
268
|
+
* @param retryIntervalMs Optional interval between retries to verify that party exists with default 2000ms. May be increased if expecting heavy load.
|
|
269
|
+
* @returns A promise that resolves when the rights have been granted.
|
|
270
|
+
*/
|
|
271
|
+
async waitForPartyAndGrantUserRights(userId, partyId, maxTries = 30, retryIntervalMs = 2e3) {
|
|
272
|
+
await this.init();
|
|
273
|
+
let partyFound = false;
|
|
274
|
+
let tries = 0;
|
|
275
|
+
while (!partyFound && tries < maxTries) {
|
|
276
|
+
partyFound = await this.checkIfPartyExists(partyId);
|
|
277
|
+
await new Promise((resolve) => setTimeout(resolve, retryIntervalMs));
|
|
278
|
+
tries++;
|
|
279
|
+
}
|
|
280
|
+
if (tries >= maxTries) throw new Error(`timed out waiting for new party to appear after ${maxTries} tries`);
|
|
281
|
+
if (!(await this.grantRights(userId, { actAs: [partyId] })).newlyGrantedRights) throw new Error("Failed to grant user rights");
|
|
282
|
+
}
|
|
283
|
+
async grantRights(userId, userRightsOptions) {
|
|
284
|
+
await this.init();
|
|
285
|
+
const rights = [];
|
|
286
|
+
for (const partyId of userRightsOptions.readAs ?? []) rights.push({ kind: { CanReadAs: { value: { party: partyId } } } });
|
|
287
|
+
for (const partyId of userRightsOptions.actAs ?? []) rights.push({ kind: { CanActAs: { value: { party: partyId } } } });
|
|
288
|
+
if (userRightsOptions.canReadAsAnyParty) rights.push({ kind: { CanReadAsAnyParty: { value: {} } } });
|
|
289
|
+
if (userRightsOptions.canExecuteAsAnyParty) rights.push({ kind: { CanExecuteAsAnyParty: { value: {} } } });
|
|
290
|
+
const result = await this.post("/v2/users/{user-id}/rights", {
|
|
291
|
+
identityProviderId: "",
|
|
292
|
+
userId,
|
|
293
|
+
rights
|
|
294
|
+
}, { path: { "user-id": userId } });
|
|
295
|
+
if (!result.newlyGrantedRights) throw new Error("Failed to grant user rights");
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
298
|
+
async allocateExternalParty(synchronizerId, onboardingTransactions, multiHashSignatures) {
|
|
299
|
+
await this.init();
|
|
300
|
+
const resp = await this.currentClient.POST("/v2/parties/external/allocate", { body: {
|
|
301
|
+
synchronizer: synchronizerId,
|
|
302
|
+
identityProviderId: "",
|
|
303
|
+
onboardingTransactions,
|
|
304
|
+
multiHashSignatures
|
|
305
|
+
} });
|
|
306
|
+
return this.valueOrError(resp);
|
|
307
|
+
}
|
|
308
|
+
async generateTopology(synchronizerId, publicKey, partyHint, localParticipantObservationOnly = false, confirmationThreshold = 1, otherConfirmingParticipantUids = [], observingParticipantUids = []) {
|
|
309
|
+
await this.init();
|
|
310
|
+
const body = {
|
|
311
|
+
synchronizer: synchronizerId,
|
|
312
|
+
partyHint,
|
|
313
|
+
publicKey: {
|
|
314
|
+
format: "CRYPTO_KEY_FORMAT_RAW",
|
|
315
|
+
keyData: publicKey,
|
|
316
|
+
keySpec: "SIGNING_KEY_SPEC_EC_CURVE25519"
|
|
317
|
+
},
|
|
318
|
+
localParticipantObservationOnly,
|
|
319
|
+
confirmationThreshold,
|
|
320
|
+
otherConfirmingParticipantUids,
|
|
321
|
+
observingParticipantUids
|
|
322
|
+
};
|
|
323
|
+
this.logger.debug(body, "generateTopology request body");
|
|
324
|
+
const resp = await this.currentClient.POST("/v2/parties/external/generate-topology", { body });
|
|
325
|
+
return this.valueOrError(resp);
|
|
326
|
+
}
|
|
327
|
+
async getSynchronizerId() {
|
|
328
|
+
if (this.synchronizerId) return this.synchronizerId;
|
|
329
|
+
const response = await this.getWithRetry("/v2/state/connected-synchronizers");
|
|
330
|
+
if (!response.connectedSynchronizers?.[0]) throw new Error("No connected synchronizers found");
|
|
331
|
+
const synchronizerId = response.connectedSynchronizers[0].synchronizerId;
|
|
332
|
+
if (response.connectedSynchronizers.length > 1) this.logger.warn(`Found ${response.connectedSynchronizers.length} synchronizers, defaulting to ${synchronizerId}`);
|
|
333
|
+
this.synchronizerId = synchronizerId;
|
|
334
|
+
return synchronizerId;
|
|
335
|
+
}
|
|
336
|
+
async postWithRetry(path, body, retryOptions = defaultRetryableOptions, params, additionalOptions) {
|
|
337
|
+
return await retryable(() => this.post(path, body, params, additionalOptions), retryOptions, this.logger).catch((e) => {
|
|
338
|
+
this.logger.warn(`Error in postWithRetry for path ${path} with body retry options ${JSON.stringify(retryOptions)}`);
|
|
339
|
+
this.logger.debug(JSON.stringify(body));
|
|
340
|
+
throw asJsCantonError(e);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
async getWithRetry(path, retryOptions = defaultRetryableOptions, params) {
|
|
344
|
+
return await retryable(() => this.get(path, params), retryOptions, this.logger).catch((e) => {
|
|
345
|
+
this.logger.warn(`Error in getWithRetry for path ${path} with retry options ${JSON.stringify(retryOptions)}`);
|
|
346
|
+
throw asJsCantonError(e);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
async patchWithRetry(path, body, retryOptions = defaultRetryableOptions, params, additionalOptions) {
|
|
350
|
+
return await retryable(() => this.patch(path, body, params, additionalOptions), retryOptions, this.logger).catch((e) => {
|
|
351
|
+
this.logger.warn(`Error in patchWithRetry for path ${path} with body retry options ${JSON.stringify(retryOptions)}`);
|
|
352
|
+
throw asJsCantonError(e);
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
async patch(path, body, params, additionalOptions) {
|
|
356
|
+
const options = {
|
|
357
|
+
body,
|
|
358
|
+
params,
|
|
359
|
+
...additionalOptions
|
|
360
|
+
};
|
|
361
|
+
const resp = await this.currentClient.PATCH(path, options);
|
|
362
|
+
return this.valueOrError(resp);
|
|
363
|
+
}
|
|
364
|
+
async post(path, body, params, additionalOptions) {
|
|
365
|
+
const options = {
|
|
366
|
+
body,
|
|
367
|
+
params,
|
|
368
|
+
...additionalOptions
|
|
369
|
+
};
|
|
370
|
+
const resp = await this.currentClient.POST(path, options);
|
|
371
|
+
return this.valueOrError(resp);
|
|
372
|
+
}
|
|
373
|
+
async get(path, params) {
|
|
374
|
+
await this.init();
|
|
375
|
+
const options = { params };
|
|
376
|
+
const resp = await this.currentClient.GET(path, options);
|
|
377
|
+
return this.valueOrError(resp);
|
|
378
|
+
}
|
|
379
|
+
async valueOrError(response) {
|
|
380
|
+
if (response.data === void 0) return Promise.reject(response.error);
|
|
381
|
+
else return Promise.resolve(response.data);
|
|
382
|
+
}
|
|
523
383
|
};
|
|
384
|
+
//#endregion
|
|
385
|
+
export { LedgerClient, asJsCantonError, awaitCompletion, defaultRetryableOptions, isJsCantonError, isValidGetEndpoint, isValidPostEndpoint, promiseWithTimeout, supportedVersions };
|
|
524
386
|
|
|
525
|
-
export { LedgerClient, asJsCantonError, awaitCompletion, defaultRetryableOptions, isJsCantonError, promiseWithTimeout, supportedVersions };
|
|
526
|
-
//# sourceMappingURL=index.js.map
|
|
527
387
|
//# sourceMappingURL=index.js.map
|