@canton-network/core-signing-dfns 0.9.0 → 0.11.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.js CHANGED
@@ -1,453 +1,435 @@
1
- import { PartyMode, SigningProvider, buildController } from '@canton-network/core-signing-lib';
2
- import { DfnsApiClient } from '@dfns/sdk';
3
- import { AsymmetricKeySigner } from '@dfns/sdk-keysigner';
4
- import { pino } from 'pino';
5
- import _ from 'lodash';
6
- import { z } from 'zod';
7
-
8
- var __defProp = Object.defineProperty;
9
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
11
- var logger = pino({ name: "dfns-handler", level: "debug" });
12
- var DFNS_LIST_PAGE_SIZE = 50;
1
+ import { PartyMode, SigningProvider, buildController } from "@canton-network/core-signing-lib";
2
+ import { DfnsApiClient } from "@dfns/sdk";
3
+ import { AsymmetricKeySigner } from "@dfns/sdk-keysigner";
4
+ import { pino } from "pino";
5
+ import _ from "lodash";
6
+ import { z } from "zod";
7
+ //#region \0@oxc-project+runtime@0.142.0/helpers/esm/typeof.js
8
+ function _typeof(o) {
9
+ "@babel/helpers - typeof";
10
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
11
+ return typeof o;
12
+ } : function(o) {
13
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
14
+ }, _typeof(o);
15
+ }
16
+ //#endregion
17
+ //#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPrimitive.js
18
+ function toPrimitive(t, r) {
19
+ if ("object" != _typeof(t) || !t) return t;
20
+ var e = t[Symbol.toPrimitive];
21
+ if (void 0 !== e) {
22
+ var i = e.call(t, r || "default");
23
+ if ("object" != _typeof(i)) return i;
24
+ throw new TypeError("@@toPrimitive must return a primitive value.");
25
+ }
26
+ return ("string" === r ? String : Number)(t);
27
+ }
28
+ //#endregion
29
+ //#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPropertyKey.js
30
+ function toPropertyKey(t) {
31
+ var i = toPrimitive(t, "string");
32
+ return "symbol" == _typeof(i) ? i : i + "";
33
+ }
34
+ //#endregion
35
+ //#region \0@oxc-project+runtime@0.142.0/helpers/esm/defineProperty.js
36
+ function _defineProperty(e, r, t) {
37
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
38
+ value: t,
39
+ enumerable: !0,
40
+ configurable: !0,
41
+ writable: !0
42
+ }) : e[r] = t, e;
43
+ }
44
+ //#endregion
45
+ //#region src/dfns.ts
46
+ const logger = pino({
47
+ name: "dfns-handler",
48
+ level: "debug"
49
+ });
50
+ const DFNS_LIST_PAGE_SIZE = 50;
13
51
  function stripHexPrefix(hex) {
14
- return hex.startsWith("0x") ? hex.slice(2) : hex;
52
+ return hex.startsWith("0x") ? hex.slice(2) : hex;
15
53
  }
16
54
  function hexToBase64(hex) {
17
- return Buffer.from(stripHexPrefix(hex), "hex").toString("base64");
55
+ return Buffer.from(stripHexPrefix(hex), "hex").toString("base64");
18
56
  }
19
57
  function base64ToHex(base64) {
20
- return Buffer.from(base64, "base64").toString("hex");
58
+ return Buffer.from(base64, "base64").toString("hex");
21
59
  }
22
60
  function isCantonKey(scheme, curve) {
23
- return scheme === "EdDSA" && curve === "ed25519";
61
+ return scheme === "EdDSA" && curve === "ed25519";
24
62
  }
25
63
  var DfnsHandler = class {
26
- constructor(orgId, baseUrl, credentials) {
27
- __publicField(this, "client");
28
- __publicField(this, "orgId");
29
- __publicField(this, "baseUrl");
30
- // Tracks the key that produced each signature so getSignature can look it
31
- // up directly. `listKeys` (used by `iterateKeys`) is eventually-consistent
32
- // — a key created moments ago may not yet appear in the list, so iterating
33
- // would 404 even though the signature exists under the just-used key.
34
- __publicField(this, "signatureKey", /* @__PURE__ */ new Map());
35
- this.orgId = orgId;
36
- this.baseUrl = baseUrl;
37
- this.client = this.createClient(credentials);
38
- }
39
- createClient(credentials) {
40
- const signer = new AsymmetricKeySigner({
41
- credId: credentials.credId,
42
- privateKey: credentials.privateKey
43
- });
44
- return new DfnsApiClient({
45
- orgId: this.orgId,
46
- authToken: credentials.authToken,
47
- baseUrl: this.baseUrl,
48
- signer
49
- });
50
- }
51
- /**
52
- * Create a raw ed25519 key in Dfns suitable for signing Canton topology
53
- * and ledger transactions. The key is not bound to a network — chain
54
- * activation happens on the gateway against its configured validator.
55
- */
56
- async createKey(name) {
57
- try {
58
- const key = await this.client.keys.createKey({
59
- body: {
60
- scheme: "EdDSA",
61
- curve: "ed25519",
62
- name
63
- }
64
- });
65
- return {
66
- id: key.id,
67
- name: key.name || key.id,
68
- publicKey: hexToBase64(key.publicKey)
69
- };
70
- } catch (error) {
71
- logger.error(error, "Error creating Dfns key");
72
- throw error;
73
- }
74
- }
75
- async *iterateKeys() {
76
- let paginationToken;
77
- do {
78
- const response = await this.client.keys.listKeys({
79
- query: {
80
- limit: DFNS_LIST_PAGE_SIZE,
81
- ...paginationToken ? { paginationToken } : {}
82
- }
83
- });
84
- for (const key of response.items) {
85
- if (key.status !== "Active" || !isCantonKey(key.scheme, key.curve)) {
86
- continue;
87
- }
88
- yield {
89
- id: key.id,
90
- name: key.name || key.id,
91
- publicKey: hexToBase64(key.publicKey)
92
- };
93
- }
94
- paginationToken = response.nextPageToken;
95
- } while (paginationToken);
96
- }
97
- async listKeys() {
98
- const keys = [];
99
- try {
100
- for await (const key of this.iterateKeys()) {
101
- keys.push(key);
102
- }
103
- } catch (error) {
104
- logger.error(error, "Error listing Dfns keys");
105
- throw error;
106
- }
107
- return keys;
108
- }
109
- async getKey(keyId) {
110
- try {
111
- const key = await this.client.keys.getKey({ keyId });
112
- if (key.status !== "Active" || !isCantonKey(key.scheme, key.curve)) {
113
- return void 0;
114
- }
115
- return {
116
- id: key.id,
117
- name: key.name || key.id,
118
- publicKey: hexToBase64(key.publicKey)
119
- };
120
- } catch (error) {
121
- logger.debug(error, `Key ${keyId} not found`);
122
- return void 0;
123
- }
124
- }
125
- /**
126
- * Resolve a key by base64 ed25519 public key. Iterates Dfns keys —
127
- * acceptable for first iteration since most deployments hold a single
128
- * Canton key per credential.
129
- */
130
- async findKeyByPublicKey(publicKeyBase64) {
131
- for await (const key of this.iterateKeys()) {
132
- if (key.publicKey === publicKeyBase64) {
133
- return key;
134
- }
135
- }
136
- return void 0;
137
- }
138
- /**
139
- * Sign a Canton hash (raw or multihash-framed) by handing the bytes to
140
- * Dfns's `Message` kind, which signs the supplied hex with raw ed25519 —
141
- * the same shape Fireblocks's `RAW` operation produces.
142
- *
143
- * `kind: 'Hash'` would be the natural fit but its regex caps at 32 bytes,
144
- * forcing us to strip the multihash prefix; Canton then rejects the
145
- * resulting signature because it verifies over the full 34-byte
146
- * multihash, not the digest. Every Canton-tagged kind (Hash/Message/
147
- * Transaction + `blockchainKind: 'Canton'`) is server-rejected, so we
148
- * use plain `Message` (no `blockchainKind`) and pass the raw bytes.
149
- */
150
- async signHash(keyId, hashBase64, externalId) {
151
- const messageHex = base64ToHex(hashBase64);
152
- const body = {
153
- kind: "Message",
154
- message: messageHex,
155
- ...externalId ? { externalId } : {}
156
- };
157
- return this.submitSignatureRequest(keyId, body);
158
- }
159
- async submitSignatureRequest(keyId, body) {
160
- try {
161
- const result = await this.client.keys.generateSignature({
162
- keyId,
163
- body
164
- });
165
- const mapped = this.mapStatus(result.status);
166
- if (mapped === "failed" || mapped === "rejected") {
167
- logger.error(
168
- { keyId, result },
169
- `Dfns signing returned ${result.status}`
170
- );
171
- }
172
- this.signatureKey.set(result.id, keyId);
173
- return {
174
- id: result.id,
175
- keyId,
176
- status: mapped,
177
- signature: this.extractSignature(result.signature)
178
- };
179
- } catch (error) {
180
- logger.error(
181
- { keyId, err: error },
182
- "Error generating Dfns signature"
183
- );
184
- throw error;
185
- }
186
- }
187
- async getSignature(keyId, signatureId) {
188
- try {
189
- const result = await this.client.keys.getSignature({
190
- keyId,
191
- signatureId
192
- });
193
- return {
194
- id: result.id,
195
- keyId,
196
- status: this.mapStatus(result.status),
197
- signature: this.extractSignature(result.signature)
198
- };
199
- } catch (error) {
200
- logger.debug(error, `Signature ${signatureId} not found`);
201
- return void 0;
202
- }
203
- }
204
- /**
205
- * Locate a signature by id. Prefers the cached key from the originating
206
- * sign call — `listKeys` is eventually-consistent, so iterating right
207
- * after creating a key would miss the signature. Falls back to iteration
208
- * when the cache misses (e.g. across a gateway restart).
209
- */
210
- async findSignature(signatureId) {
211
- const cachedKeyId = this.signatureKey.get(signatureId);
212
- if (cachedKeyId) {
213
- const sig = await this.getSignature(cachedKeyId, signatureId);
214
- if (sig) return sig;
215
- }
216
- for await (const key of this.iterateKeys()) {
217
- if (key.id === cachedKeyId) continue;
218
- const sig = await this.getSignature(key.id, signatureId);
219
- if (sig) return sig;
220
- }
221
- return void 0;
222
- }
223
- async *listSignatures(keyId) {
224
- let paginationToken;
225
- do {
226
- const response = await this.client.keys.listSignatures({
227
- keyId,
228
- query: {
229
- limit: DFNS_LIST_PAGE_SIZE,
230
- ...paginationToken ? { paginationToken } : {}
231
- }
232
- });
233
- for (const sig of response.items) {
234
- yield {
235
- id: sig.id,
236
- keyId,
237
- status: this.mapStatus(sig.status),
238
- signature: this.extractSignature(sig.signature)
239
- };
240
- }
241
- paginationToken = response.nextPageToken;
242
- } while (paginationToken);
243
- }
244
- extractSignature(signature) {
245
- if (!signature) return void 0;
246
- const hex = signature.encoded ? stripHexPrefix(signature.encoded) : `${stripHexPrefix(signature.r)}${stripHexPrefix(signature.s)}`;
247
- return hexToBase64(hex);
248
- }
249
- mapStatus(dfnsStatus) {
250
- switch (dfnsStatus) {
251
- case "Signed":
252
- case "Confirmed":
253
- return "signed";
254
- case "Pending":
255
- case "Executing":
256
- return "pending";
257
- case "Rejected":
258
- return "rejected";
259
- case "Failed":
260
- return "failed";
261
- default:
262
- logger.warn(`Unknown Dfns signature status: ${dfnsStatus}`);
263
- return "pending";
264
- }
265
- }
64
+ constructor(orgId, baseUrl, credentials) {
65
+ _defineProperty(this, "client", void 0);
66
+ _defineProperty(this, "orgId", void 0);
67
+ _defineProperty(this, "baseUrl", void 0);
68
+ _defineProperty(this, "signatureKey", /* @__PURE__ */ new Map());
69
+ this.orgId = orgId;
70
+ this.baseUrl = baseUrl;
71
+ this.client = this.createClient(credentials);
72
+ }
73
+ createClient(credentials) {
74
+ const signer = new AsymmetricKeySigner({
75
+ credId: credentials.credId,
76
+ privateKey: credentials.privateKey
77
+ });
78
+ return new DfnsApiClient({
79
+ orgId: this.orgId,
80
+ authToken: credentials.authToken,
81
+ baseUrl: this.baseUrl,
82
+ signer
83
+ });
84
+ }
85
+ /**
86
+ * Create a raw ed25519 key in Dfns suitable for signing Canton topology
87
+ * and ledger transactions. The key is not bound to a network — chain
88
+ * activation happens on the gateway against its configured validator.
89
+ */
90
+ async createKey(name) {
91
+ try {
92
+ const key = await this.client.keys.createKey({ body: {
93
+ scheme: "EdDSA",
94
+ curve: "ed25519",
95
+ name
96
+ } });
97
+ return {
98
+ id: key.id,
99
+ name: key.name || key.id,
100
+ publicKey: hexToBase64(key.publicKey)
101
+ };
102
+ } catch (error) {
103
+ logger.error(error, "Error creating Dfns key");
104
+ throw error;
105
+ }
106
+ }
107
+ async *iterateKeys() {
108
+ let paginationToken;
109
+ do {
110
+ const response = await this.client.keys.listKeys({ query: {
111
+ limit: DFNS_LIST_PAGE_SIZE,
112
+ ...paginationToken ? { paginationToken } : {}
113
+ } });
114
+ for (const key of response.items) {
115
+ if (key.status !== "Active" || !isCantonKey(key.scheme, key.curve)) continue;
116
+ yield {
117
+ id: key.id,
118
+ name: key.name || key.id,
119
+ publicKey: hexToBase64(key.publicKey)
120
+ };
121
+ }
122
+ paginationToken = response.nextPageToken;
123
+ } while (paginationToken);
124
+ }
125
+ async listKeys() {
126
+ const keys = [];
127
+ try {
128
+ for await (const key of this.iterateKeys()) keys.push(key);
129
+ } catch (error) {
130
+ logger.error(error, "Error listing Dfns keys");
131
+ throw error;
132
+ }
133
+ return keys;
134
+ }
135
+ async getKey(keyId) {
136
+ try {
137
+ const key = await this.client.keys.getKey({ keyId });
138
+ if (key.status !== "Active" || !isCantonKey(key.scheme, key.curve)) return;
139
+ return {
140
+ id: key.id,
141
+ name: key.name || key.id,
142
+ publicKey: hexToBase64(key.publicKey)
143
+ };
144
+ } catch (error) {
145
+ logger.debug(error, `Key ${keyId} not found`);
146
+ return;
147
+ }
148
+ }
149
+ /**
150
+ * Resolve a key by base64 ed25519 public key. Iterates Dfns keys —
151
+ * acceptable for first iteration since most deployments hold a single
152
+ * Canton key per credential.
153
+ */
154
+ async findKeyByPublicKey(publicKeyBase64) {
155
+ for await (const key of this.iterateKeys()) if (key.publicKey === publicKeyBase64) return key;
156
+ }
157
+ /**
158
+ * Sign a Canton hash (raw or multihash-framed) by handing the bytes to
159
+ * Dfns's `Message` kind, which signs the supplied hex with raw ed25519 —
160
+ * the same shape Fireblocks's `RAW` operation produces.
161
+ *
162
+ * `kind: 'Hash'` would be the natural fit but its regex caps at 32 bytes,
163
+ * forcing us to strip the multihash prefix; Canton then rejects the
164
+ * resulting signature because it verifies over the full 34-byte
165
+ * multihash, not the digest. Every Canton-tagged kind (Hash/Message/
166
+ * Transaction + `blockchainKind: 'Canton'`) is server-rejected, so we
167
+ * use plain `Message` (no `blockchainKind`) and pass the raw bytes.
168
+ */
169
+ async signHash(keyId, hashBase64, externalId) {
170
+ const body = {
171
+ kind: "Message",
172
+ message: base64ToHex(hashBase64),
173
+ ...externalId ? { externalId } : {}
174
+ };
175
+ return this.submitSignatureRequest(keyId, body);
176
+ }
177
+ async submitSignatureRequest(keyId, body) {
178
+ try {
179
+ const result = await this.client.keys.generateSignature({
180
+ keyId,
181
+ body
182
+ });
183
+ const mapped = this.mapStatus(result.status);
184
+ if (mapped === "failed" || mapped === "rejected") logger.error({
185
+ keyId,
186
+ result
187
+ }, `Dfns signing returned ${result.status}`);
188
+ this.signatureKey.set(result.id, keyId);
189
+ return {
190
+ id: result.id,
191
+ keyId,
192
+ status: mapped,
193
+ signature: this.extractSignature(result.signature)
194
+ };
195
+ } catch (error) {
196
+ logger.error({
197
+ keyId,
198
+ err: error
199
+ }, "Error generating Dfns signature");
200
+ throw error;
201
+ }
202
+ }
203
+ async getSignature(keyId, signatureId) {
204
+ try {
205
+ const result = await this.client.keys.getSignature({
206
+ keyId,
207
+ signatureId
208
+ });
209
+ return {
210
+ id: result.id,
211
+ keyId,
212
+ status: this.mapStatus(result.status),
213
+ signature: this.extractSignature(result.signature)
214
+ };
215
+ } catch (error) {
216
+ logger.debug(error, `Signature ${signatureId} not found`);
217
+ return;
218
+ }
219
+ }
220
+ /**
221
+ * Locate a signature by id. Prefers the cached key from the originating
222
+ * sign call — `listKeys` is eventually-consistent, so iterating right
223
+ * after creating a key would miss the signature. Falls back to iteration
224
+ * when the cache misses (e.g. across a gateway restart).
225
+ */
226
+ async findSignature(signatureId) {
227
+ const cachedKeyId = this.signatureKey.get(signatureId);
228
+ if (cachedKeyId) {
229
+ const sig = await this.getSignature(cachedKeyId, signatureId);
230
+ if (sig) return sig;
231
+ }
232
+ for await (const key of this.iterateKeys()) {
233
+ if (key.id === cachedKeyId) continue;
234
+ const sig = await this.getSignature(key.id, signatureId);
235
+ if (sig) return sig;
236
+ }
237
+ }
238
+ async *listSignatures(keyId) {
239
+ let paginationToken;
240
+ do {
241
+ const response = await this.client.keys.listSignatures({
242
+ keyId,
243
+ query: {
244
+ limit: DFNS_LIST_PAGE_SIZE,
245
+ ...paginationToken ? { paginationToken } : {}
246
+ }
247
+ });
248
+ for (const sig of response.items) yield {
249
+ id: sig.id,
250
+ keyId,
251
+ status: this.mapStatus(sig.status),
252
+ signature: this.extractSignature(sig.signature)
253
+ };
254
+ paginationToken = response.nextPageToken;
255
+ } while (paginationToken);
256
+ }
257
+ extractSignature(signature) {
258
+ if (!signature) return void 0;
259
+ return hexToBase64(signature.encoded ? stripHexPrefix(signature.encoded) : `${stripHexPrefix(signature.r)}${stripHexPrefix(signature.s)}`);
260
+ }
261
+ mapStatus(dfnsStatus) {
262
+ switch (dfnsStatus) {
263
+ case "Signed":
264
+ case "Confirmed": return "signed";
265
+ case "Pending":
266
+ case "Executing": return "pending";
267
+ case "Rejected": return "rejected";
268
+ case "Failed": return "failed";
269
+ default:
270
+ logger.warn(`Unknown Dfns signature status: ${dfnsStatus}`);
271
+ return "pending";
272
+ }
273
+ }
266
274
  };
267
- var DfnsCredentialsSchema = z.object({
268
- credId: z.string().min(1),
269
- privateKey: z.string().min(1),
270
- authToken: z.string().min(1)
275
+ //#endregion
276
+ //#region src/index.ts
277
+ const DfnsCredentialsSchema = z.object({
278
+ credId: z.string().min(1),
279
+ privateKey: z.string().min(1),
280
+ authToken: z.string().min(1)
271
281
  });
272
- var DfnsConfigSchema = z.object({
273
- orgId: z.string().min(1),
274
- baseUrl: z.string().url(),
275
- credentials: DfnsCredentialsSchema
282
+ const DfnsConfigSchema = z.object({
283
+ orgId: z.string().min(1),
284
+ baseUrl: z.string().url(),
285
+ credentials: DfnsCredentialsSchema
276
286
  });
277
- var createDfnsHandler = (config) => new DfnsHandler(config.orgId, config.baseUrl, config.credentials);
287
+ const createDfnsHandler = (config) => new DfnsHandler(config.orgId, config.baseUrl, config.credentials);
278
288
  function toTransaction(sig, publicKey) {
279
- const tx = {
280
- txId: sig.id,
281
- status: sig.status
282
- };
283
- if (sig.signature) tx.signature = sig.signature;
284
- if (publicKey) tx.publicKey = publicKey;
285
- return tx;
289
+ const tx = {
290
+ txId: sig.id,
291
+ status: sig.status
292
+ };
293
+ if (sig.signature) tx.signature = sig.signature;
294
+ if (publicKey) tx.publicKey = publicKey;
295
+ return tx;
286
296
  }
287
297
  var DfnsSigningDriver = class {
288
- constructor(config) {
289
- __publicField(this, "dfns");
290
- __publicField(this, "config");
291
- __publicField(this, "partyMode", PartyMode.EXTERNAL);
292
- __publicField(this, "signingProvider", SigningProvider.DFNS);
293
- __publicField(this, "controller", (_userId) => buildController({
294
- signTransaction: async (params) => {
295
- try {
296
- const keyId = await this.resolveKeyId(params.keyIdentifier);
297
- if (!keyId) {
298
- return {
299
- error: "key_not_found",
300
- error_description: "No Dfns key found for the provided key identifier."
301
- };
302
- }
303
- if (!params.txHash) {
304
- return {
305
- error: "bad_arguments",
306
- error_description: "txHash is required to sign with Dfns."
307
- };
308
- }
309
- const sig = await this.dfns.signHash(
310
- keyId,
311
- params.txHash,
312
- params.internalTxId
313
- );
314
- return toTransaction(sig, params.keyIdentifier.publicKey);
315
- } catch (error) {
316
- return {
317
- error: "signing_error",
318
- error_description: error.message
319
- };
320
- }
321
- },
322
- getTransaction: async (params) => {
323
- try {
324
- const sig = await this.dfns.findSignature(params.txId);
325
- if (!sig) {
326
- return {
327
- error: "transaction_not_found",
328
- error_description: "The requested signature does not exist."
329
- };
330
- }
331
- const key = await this.dfns.getKey(sig.keyId);
332
- return toTransaction(sig, key?.publicKey);
333
- } catch (error) {
334
- return {
335
- error: "fetch_error",
336
- error_description: error.message
337
- };
338
- }
339
- },
340
- getTransactions: async (params) => {
341
- if (!params.publicKeys && !params.txIds) {
342
- return {
343
- error: "bad_arguments",
344
- error_description: "Either publicKeys or txIds must be provided."
345
- };
346
- }
347
- try {
348
- const txIds = new Set(params.txIds || []);
349
- const publicKeys = new Set(params.publicKeys || []);
350
- const transactions = [];
351
- for await (const key of this.dfns.iterateKeys()) {
352
- if (params.publicKeys && !publicKeys.has(key.publicKey)) {
353
- continue;
354
- }
355
- for await (const sig of this.dfns.listSignatures(
356
- key.id
357
- )) {
358
- if (params.txIds && !txIds.has(sig.id)) continue;
359
- transactions.push(toTransaction(sig, key.publicKey));
360
- if (params.txIds && !params.publicKeys && transactions.length === txIds.size) {
361
- return { transactions };
362
- }
363
- }
364
- }
365
- return { transactions };
366
- } catch (error) {
367
- return {
368
- error: "fetch_error",
369
- error_description: error.message
370
- };
371
- }
372
- },
373
- getKeys: async () => {
374
- try {
375
- const keys = await this.dfns.listKeys();
376
- return {
377
- keys: keys.map((k) => ({
378
- id: k.id,
379
- name: k.name,
380
- publicKey: k.publicKey
381
- }))
382
- };
383
- } catch (error) {
384
- return {
385
- error: "fetch_error",
386
- error_description: error.message
387
- };
388
- }
389
- },
390
- createKey: async (params) => {
391
- try {
392
- const key = await this.dfns.createKey(params.name);
393
- return {
394
- id: key.id,
395
- name: key.name,
396
- publicKey: key.publicKey
397
- };
398
- } catch (error) {
399
- return {
400
- error: "creation_error",
401
- error_description: error.message
402
- };
403
- }
404
- },
405
- getConfiguration: async () => ({
406
- orgId: this.config.orgId,
407
- baseUrl: this.config.baseUrl,
408
- credentials: this.config.credentials
409
- }),
410
- setConfiguration: async (params) => {
411
- const validated = DfnsConfigSchema.safeParse(params);
412
- if (!validated.success) {
413
- return {
414
- error: "bad_arguments",
415
- error_description: validated.error.message
416
- };
417
- }
418
- const newConfig = {
419
- orgId: validated.data.orgId,
420
- baseUrl: validated.data.baseUrl,
421
- credentials: validated.data.credentials
422
- };
423
- if (!_.isEqual(newConfig, this.config)) {
424
- this.config = newConfig;
425
- this.dfns = createDfnsHandler(this.config);
426
- }
427
- return params;
428
- },
429
- // TODO remove comment below and write a test once / if ever implemented
430
- // v8 ignore next -- @preserve
431
- subscribeTransactions: async (_params) => {
432
- return Promise.resolve({});
433
- },
434
- // TODO remove comment below and write a test once implemented
435
- // v8 ignore next -- @preserve
436
- signMessage: function() {
437
- throw new Error("Function not implemented.");
438
- }
439
- }));
440
- this.config = config;
441
- this.dfns = createDfnsHandler(config);
442
- }
443
- async resolveKeyId(keyIdentifier) {
444
- if (keyIdentifier.id) return keyIdentifier.id;
445
- if (!keyIdentifier.publicKey) return void 0;
446
- const key = await this.dfns.findKeyByPublicKey(keyIdentifier.publicKey);
447
- return key?.id;
448
- }
298
+ constructor(config) {
299
+ _defineProperty(this, "dfns", void 0);
300
+ _defineProperty(this, "config", void 0);
301
+ _defineProperty(this, "partyMode", PartyMode.EXTERNAL);
302
+ _defineProperty(this, "signingProvider", SigningProvider.DFNS);
303
+ _defineProperty(this, "controller", (_userId) => buildController({
304
+ signTransaction: async (params) => {
305
+ try {
306
+ const keyId = await this.resolveKeyId(params.keyIdentifier);
307
+ if (!keyId) return {
308
+ error: "key_not_found",
309
+ error_description: "No Dfns key found for the provided key identifier."
310
+ };
311
+ if (!params.txHash) return {
312
+ error: "bad_arguments",
313
+ error_description: "txHash is required to sign with Dfns."
314
+ };
315
+ return toTransaction(await this.dfns.signHash(keyId, params.txHash, params.internalTxId), params.keyIdentifier.publicKey);
316
+ } catch (error) {
317
+ return {
318
+ error: "signing_error",
319
+ error_description: error.message
320
+ };
321
+ }
322
+ },
323
+ getTransaction: async (params) => {
324
+ try {
325
+ const sig = await this.dfns.findSignature(params.txId);
326
+ if (!sig) return {
327
+ error: "transaction_not_found",
328
+ error_description: "The requested signature does not exist."
329
+ };
330
+ return toTransaction(sig, (await this.dfns.getKey(sig.keyId))?.publicKey);
331
+ } catch (error) {
332
+ return {
333
+ error: "fetch_error",
334
+ error_description: error.message
335
+ };
336
+ }
337
+ },
338
+ getTransactions: async (params) => {
339
+ if (!params.publicKeys && !params.txIds) return {
340
+ error: "bad_arguments",
341
+ error_description: "Either publicKeys or txIds must be provided."
342
+ };
343
+ try {
344
+ const txIds = new Set(params.txIds || []);
345
+ const publicKeys = new Set(params.publicKeys || []);
346
+ const transactions = [];
347
+ for await (const key of this.dfns.iterateKeys()) {
348
+ if (params.publicKeys && !publicKeys.has(key.publicKey)) continue;
349
+ for await (const sig of this.dfns.listSignatures(key.id)) {
350
+ if (params.txIds && !txIds.has(sig.id)) continue;
351
+ transactions.push(toTransaction(sig, key.publicKey));
352
+ if (params.txIds && !params.publicKeys && transactions.length === txIds.size) return { transactions };
353
+ }
354
+ }
355
+ return { transactions };
356
+ } catch (error) {
357
+ return {
358
+ error: "fetch_error",
359
+ error_description: error.message
360
+ };
361
+ }
362
+ },
363
+ getKeys: async () => {
364
+ try {
365
+ return { keys: (await this.dfns.listKeys()).map((k) => ({
366
+ id: k.id,
367
+ name: k.name,
368
+ publicKey: k.publicKey
369
+ })) };
370
+ } catch (error) {
371
+ return {
372
+ error: "fetch_error",
373
+ error_description: error.message
374
+ };
375
+ }
376
+ },
377
+ createKey: async (params) => {
378
+ try {
379
+ const key = await this.dfns.createKey(params.name);
380
+ return {
381
+ id: key.id,
382
+ name: key.name,
383
+ publicKey: key.publicKey
384
+ };
385
+ } catch (error) {
386
+ return {
387
+ error: "creation_error",
388
+ error_description: error.message
389
+ };
390
+ }
391
+ },
392
+ getConfiguration: async () => ({
393
+ orgId: this.config.orgId,
394
+ baseUrl: this.config.baseUrl,
395
+ credentials: this.config.credentials
396
+ }),
397
+ setConfiguration: async (params) => {
398
+ const validated = DfnsConfigSchema.safeParse(params);
399
+ if (!validated.success) return {
400
+ error: "bad_arguments",
401
+ error_description: validated.error.message
402
+ };
403
+ const newConfig = {
404
+ orgId: validated.data.orgId,
405
+ baseUrl: validated.data.baseUrl,
406
+ credentials: validated.data.credentials
407
+ };
408
+ if (!_.isEqual(newConfig, this.config)) {
409
+ this.config = newConfig;
410
+ this.dfns = createDfnsHandler(this.config);
411
+ }
412
+ return params;
413
+ },
414
+ // v8 ignore next -- @preserve
415
+ subscribeTransactions: async (_params) => {
416
+ return Promise.resolve({});
417
+ },
418
+ // v8 ignore next -- @preserve
419
+ signMessage: function() {
420
+ throw new Error("Function not implemented.");
421
+ }
422
+ }));
423
+ this.config = config;
424
+ this.dfns = createDfnsHandler(config);
425
+ }
426
+ async resolveKeyId(keyIdentifier) {
427
+ if (keyIdentifier.id) return keyIdentifier.id;
428
+ if (!keyIdentifier.publicKey) return void 0;
429
+ return (await this.dfns.findKeyByPublicKey(keyIdentifier.publicKey))?.id;
430
+ }
449
431
  };
450
-
432
+ //#endregion
451
433
  export { DfnsHandler, DfnsSigningDriver as default };
452
- //# sourceMappingURL=index.js.map
434
+
453
435
  //# sourceMappingURL=index.js.map