@fairmint/canton-node-sdk 0.0.308 → 0.0.309
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/README.md +83 -0
- package/build/src/utils/token-standard/v2/constants.d.ts +51 -0
- package/build/src/utils/token-standard/v2/constants.d.ts.map +1 -0
- package/build/src/utils/token-standard/v2/constants.js +54 -0
- package/build/src/utils/token-standard/v2/constants.js.map +1 -0
- package/build/src/utils/token-standard/v2/holdings.d.ts +0 -1
- package/build/src/utils/token-standard/v2/holdings.d.ts.map +1 -1
- package/build/src/utils/token-standard/v2/holdings.js +3 -3
- package/build/src/utils/token-standard/v2/holdings.js.map +1 -1
- package/build/src/utils/token-standard/v2/index.d.ts +1 -0
- package/build/src/utils/token-standard/v2/index.d.ts.map +1 -1
- package/build/src/utils/token-standard/v2/index.js +1 -0
- package/build/src/utils/token-standard/v2/index.js.map +1 -1
- package/build/src/wallets/amulet/registry.d.ts.map +1 -1
- package/build/src/wallets/amulet/registry.js +10 -91
- package/build/src/wallets/amulet/registry.js.map +1 -1
- package/build/src/wallets/cip103.d.ts.map +1 -1
- package/build/src/wallets/cip103.js +5 -1
- package/build/src/wallets/cip103.js.map +1 -1
- package/build/src/wallets/commands.d.ts +6 -0
- package/build/src/wallets/commands.d.ts.map +1 -1
- package/build/src/wallets/commands.js +22 -0
- package/build/src/wallets/commands.js.map +1 -1
- package/build/src/wallets/contracts.d.ts +80 -5
- package/build/src/wallets/contracts.d.ts.map +1 -1
- package/build/src/wallets/contracts.js +174 -2
- package/build/src/wallets/contracts.js.map +1 -1
- package/build/src/wallets/errors.d.ts +8 -1
- package/build/src/wallets/errors.d.ts.map +1 -1
- package/build/src/wallets/errors.js +5 -1
- package/build/src/wallets/errors.js.map +1 -1
- package/build/src/wallets/holding-view.d.ts +33 -1
- package/build/src/wallets/holding-view.d.ts.map +1 -1
- package/build/src/wallets/holding-view.js +49 -1
- package/build/src/wallets/holding-view.js.map +1 -1
- package/build/src/wallets/holdings.d.ts +38 -8
- package/build/src/wallets/holdings.d.ts.map +1 -1
- package/build/src/wallets/holdings.js +49 -9
- package/build/src/wallets/holdings.js.map +1 -1
- package/build/src/wallets/index.d.ts +11 -2
- package/build/src/wallets/index.d.ts.map +1 -1
- package/build/src/wallets/index.js +25 -2
- package/build/src/wallets/index.js.map +1 -1
- package/build/src/wallets/registry-http.d.ts +32 -0
- package/build/src/wallets/registry-http.d.ts.map +1 -0
- package/build/src/wallets/registry-http.js +105 -0
- package/build/src/wallets/registry-http.js.map +1 -0
- package/build/src/wallets/store.d.ts +3 -0
- package/build/src/wallets/store.d.ts.map +1 -1
- package/build/src/wallets/store.js +2 -1
- package/build/src/wallets/store.js.map +1 -1
- package/build/src/wallets/token-registry.d.ts +126 -0
- package/build/src/wallets/token-registry.d.ts.map +1 -0
- package/build/src/wallets/token-registry.js +249 -0
- package/build/src/wallets/token-registry.js.map +1 -0
- package/build/src/wallets/tokens.d.ts +16 -4
- package/build/src/wallets/tokens.d.ts.map +1 -1
- package/build/src/wallets/tokens.js +21 -15
- package/build/src/wallets/tokens.js.map +1 -1
- package/build/src/wallets/wallet.d.ts +30 -0
- package/build/src/wallets/wallet.d.ts.map +1 -1
- package/build/src/wallets/wallet.js +377 -18
- package/build/src/wallets/wallet.js.map +1 -1
- package/package.json +1 -1
|
@@ -20,6 +20,13 @@ exports.createWallet = createWallet;
|
|
|
20
20
|
* others (see `isFromRegistry`). A Canton Coin offer counts when it is of Canton Coin and the DSO and its sender signed
|
|
21
21
|
* it. The DSO party is resolved once for both (`createAmuletRegistry`): pinned, else the registry's `adminId`; while it
|
|
22
22
|
* is unconfirmed, no Canton Coin is listed (`WalletState.cantonCoinUnavailable`).
|
|
23
|
+
*
|
|
24
|
+
* Token-standard registries (`WalletOptions.registries`, each by URL and pinned admin party): their holdings and
|
|
25
|
+
* transfer instructions, V2 and V1, mint offers included, are listed when that admin signed them, and the offers are
|
|
26
|
+
* answered through the V1 interface (V2 for a contract that implements only V2, and for the admin's withdrawal of a
|
|
27
|
+
* mint offer) with the registry's own choice contexts, once its `adminId` is confirmed. Their tokens are not
|
|
28
|
+
* transferred or burnt here. The raw views of any further interfaces a registry names (`extraInterfaceIds`) are passed
|
|
29
|
+
* on undecoded, for a library outside this SDK to read.
|
|
23
30
|
*/
|
|
24
31
|
const cip103_1 = require("./cip103");
|
|
25
32
|
const commands_1 = require("./commands");
|
|
@@ -30,6 +37,7 @@ const constants_1 = require("./amulet/constants");
|
|
|
30
37
|
const registry_1 = require("./amulet/registry");
|
|
31
38
|
const errors_1 = require("./errors");
|
|
32
39
|
const holding_view_1 = require("./holding-view");
|
|
40
|
+
const token_registry_1 = require("./token-registry");
|
|
33
41
|
const holdings_1 = require("./holdings");
|
|
34
42
|
const decimal_1 = require("./decimal");
|
|
35
43
|
const instrument_1 = require("./instrument");
|
|
@@ -38,6 +46,7 @@ const templates_1 = require("./templates");
|
|
|
38
46
|
const tokens_1 = require("./tokens");
|
|
39
47
|
const templates_2 = require("../da-registry/templates");
|
|
40
48
|
const constants_2 = require("../utils/token-standard/v1/constants");
|
|
49
|
+
const constants_3 = require("../utils/token-standard/v2/constants");
|
|
41
50
|
const SECOND_MS = 1000;
|
|
42
51
|
const DAY_MS = 24 * 60 * 60 * SECOND_MS;
|
|
43
52
|
/** Transfers: `executeBefore` defaults to now + 24 h, a common wallet default. */
|
|
@@ -106,6 +115,114 @@ const CANTON_COIN_STATE_FILTERS = [
|
|
|
106
115
|
...DA_STATE_TEMPLATES,
|
|
107
116
|
constants_1.AMULET_TEMPLATES.transferInstruction,
|
|
108
117
|
];
|
|
118
|
+
/**
|
|
119
|
+
* The interface (with its view) each token-standard interface of a configured registry is read through: holdings
|
|
120
|
+
* (`HoldingV2`, and `HoldingV1` for a registry that implements only CIP-56) and transfer instructions (V2 and V1).
|
|
121
|
+
* Which contracts count is decided per contract (`parseHolding`, `tokenStandardOffers`).
|
|
122
|
+
*/
|
|
123
|
+
const TOKEN_STANDARD_INTERFACE_FILTERS = {
|
|
124
|
+
'holding-v2': { interfaceId: holding_view_1.HOLDING_V2_INTERFACE_ID, includeInterfaceView: true },
|
|
125
|
+
'holding-v1': { interfaceId: holding_view_1.HOLDING_V1_INTERFACE_ID, includeInterfaceView: true },
|
|
126
|
+
'transfer-instruction-v2': {
|
|
127
|
+
interfaceId: constants_3.TOKEN_STANDARD_V2_TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
128
|
+
includeInterfaceView: true,
|
|
129
|
+
},
|
|
130
|
+
'transfer-instruction-v1': {
|
|
131
|
+
interfaceId: constants_2.TOKEN_STANDARD_V1_TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
132
|
+
includeInterfaceView: true,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* How `WalletState.registryInterfacesUnavailable` names the interface a filter reads: a token-standard interface of
|
|
137
|
+
* {@link TOKEN_STANDARD_INTERFACE_FILTERS} by its name, an extra interface by its id.
|
|
138
|
+
*/
|
|
139
|
+
function filterInterfaceName(filter) {
|
|
140
|
+
const standard = token_registry_1.TOKEN_STANDARD_INTERFACES.find((entry) => TOKEN_STANDARD_INTERFACE_FILTERS[entry] === filter);
|
|
141
|
+
return standard ?? (typeof filter === 'string' ? filter : filter.interfaceId);
|
|
142
|
+
}
|
|
143
|
+
/** The transfer instruction interfaces, for a read of offers only. */
|
|
144
|
+
const TOKEN_STANDARD_OFFER_INTERFACES = [
|
|
145
|
+
'transfer-instruction-v2',
|
|
146
|
+
'transfer-instruction-v1',
|
|
147
|
+
];
|
|
148
|
+
/** The holding interfaces, to tell a configured registry's token apart before refusing its transfer or burn. */
|
|
149
|
+
const TOKEN_STANDARD_HOLDING_INTERFACES = ['holding-v2', 'holding-v1'];
|
|
150
|
+
/**
|
|
151
|
+
* The Canton errors a participant answers a read with when a filter names a package or interface it does not have. The
|
|
152
|
+
* wallet then reads again without the configured registries' filters the error names (all of them when it names none),
|
|
153
|
+
* so a participant without one registry interface still loads the rest of the state; the state names the interfaces
|
|
154
|
+
* left out (`WalletState.registryInterfacesUnavailable`).
|
|
155
|
+
*/
|
|
156
|
+
const UNRESOLVED_FILTER_ERRORS = new Set([
|
|
157
|
+
'PACKAGE_NAMES_NOT_FOUND',
|
|
158
|
+
'TEMPLATES_OR_INTERFACES_NOT_FOUND',
|
|
159
|
+
]);
|
|
160
|
+
/** How much of a refusal's text is searched for the filters it names. */
|
|
161
|
+
const UNRESOLVED_FILTER_TEXT_CHARS = 16384;
|
|
162
|
+
/** A refusal's message and data as text, bounded; never throws. */
|
|
163
|
+
function refusalText(error) {
|
|
164
|
+
const parts = [];
|
|
165
|
+
const add = (value) => {
|
|
166
|
+
if (typeof value === 'string') {
|
|
167
|
+
parts.push(value);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (value === undefined)
|
|
171
|
+
return;
|
|
172
|
+
try {
|
|
173
|
+
parts.push(JSON.stringify(value));
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
// Unserializable data names nothing.
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
if (error instanceof Error)
|
|
180
|
+
add(error.message);
|
|
181
|
+
const cause = (0, json_1.isRecord)(error) ? error['cause'] : undefined;
|
|
182
|
+
if ((0, json_1.isRecord)(cause)) {
|
|
183
|
+
if (typeof cause['message'] === 'string')
|
|
184
|
+
add(cause['message']);
|
|
185
|
+
add(cause['data']);
|
|
186
|
+
}
|
|
187
|
+
return parts.join('\n').slice(0, UNRESOLVED_FILTER_TEXT_CHARS);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The text of a read refused because a filter names a package or interface the participant does not have, or undefined
|
|
191
|
+
* for any other failure.
|
|
192
|
+
*/
|
|
193
|
+
function unresolvedFilterRefusal(error) {
|
|
194
|
+
if (!(0, errors_1.isWalletReadError)(error))
|
|
195
|
+
return undefined;
|
|
196
|
+
const id = error.cantonErrorId;
|
|
197
|
+
return id !== undefined && UNRESOLVED_FILTER_ERRORS.has(id) ? refusalText(error) : undefined;
|
|
198
|
+
}
|
|
199
|
+
const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
200
|
+
/**
|
|
201
|
+
* Whether a refusal's `text` names `filter`'s interface: its id, its package (name or id) as a whole word, or its
|
|
202
|
+
* `Module:Entity` part as a whole name (a refusal that names the interface by the other id form).
|
|
203
|
+
*/
|
|
204
|
+
function refusalNames(text, filter) {
|
|
205
|
+
const id = typeof filter === 'string' ? filter : filter.interfaceId;
|
|
206
|
+
if (text.includes(id) || text.includes(id.replace(/^#/, '')))
|
|
207
|
+
return true;
|
|
208
|
+
const parts = /^#?([^:]+):(.+)$/.exec(id);
|
|
209
|
+
if (parts === null)
|
|
210
|
+
return false;
|
|
211
|
+
const [, packageName = '', qualifiedName = ''] = parts;
|
|
212
|
+
return (new RegExp(`(^|[^\\w-])${escapeRegExp(packageName)}($|[^\\w-])`).test(text) ||
|
|
213
|
+
new RegExp(`(^|[^\\w.-])${escapeRegExp(qualifiedName)}($|[^\\w.-])`).test(text));
|
|
214
|
+
}
|
|
215
|
+
/** `filters` without repeats (an interface filter asked twice, by the same id and view flag). */
|
|
216
|
+
function uniqueFilters(filters) {
|
|
217
|
+
const seen = new Set();
|
|
218
|
+
return filters.filter((filter) => {
|
|
219
|
+
const key = typeof filter === 'string' ? filter : `${filter.interfaceId}+${filter.includeInterfaceView === true}`;
|
|
220
|
+
if (seen.has(key))
|
|
221
|
+
return false;
|
|
222
|
+
seen.add(key);
|
|
223
|
+
return true;
|
|
224
|
+
});
|
|
225
|
+
}
|
|
109
226
|
/** Canton Coin's holding templates. */
|
|
110
227
|
const CANTON_COIN_HOLDING_TEMPLATES = [constants_1.AMULET_TEMPLATES.amulet, constants_1.AMULET_TEMPLATES.lockedAmulet];
|
|
111
228
|
/** Canton Coin contracts whose DSO must be confirmed before they are listed. */
|
|
@@ -146,6 +263,21 @@ function deadline(executeBefore, at, defaultMs) {
|
|
|
146
263
|
throw new errors_1.WalletError('invalid-input', 'executeBefore must be in the future');
|
|
147
264
|
return isoTime(ms, 'executeBefore');
|
|
148
265
|
}
|
|
266
|
+
/** The `TransferInstruction` versions a token-standard offer implements (`standards`, else its one `standard`). */
|
|
267
|
+
function implementedStandards(offer) {
|
|
268
|
+
return offer.standards ?? [offer.standard ?? 'v1'];
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The `TransferInstruction` version the wallet exercises `action` through on a token-standard offer: V1 whenever the
|
|
272
|
+
* contract implements it, so a holder only ever needs the CIP-56 V1 choices its signing policy already allows, and V2
|
|
273
|
+
* for a contract that implements V2 alone. A mint offer is withdrawn through V2: its V1 view names the stub party as
|
|
274
|
+
* sender, so a V1 withdrawal would be refused on the ledger (one without V2 is refused before this).
|
|
275
|
+
*/
|
|
276
|
+
function answerStandard(offer, action) {
|
|
277
|
+
if (offer.mint === true && action === 'withdraw')
|
|
278
|
+
return 'v2';
|
|
279
|
+
return implementedStandards(offer).includes('v1') ? 'v1' : 'v2';
|
|
280
|
+
}
|
|
149
281
|
/** The DA Registry token actions of `options.partyId` through `options.provider`. */
|
|
150
282
|
function createWallet(options) {
|
|
151
283
|
const { provider, isActive, now: clock = () => new Date(), readRetry } = options;
|
|
@@ -157,10 +289,29 @@ function createWallet(options) {
|
|
|
157
289
|
const now = () => requireTime(clock(), 'now()');
|
|
158
290
|
/** Canton Coin's registry: the one DSO resolution holdings and offers share; null with `cantonCoin: false`. */
|
|
159
291
|
const cantonCoin = options.cantonCoin === false ? null : (0, registry_1.createAmuletRegistry)(network, options.cantonCoin, { now });
|
|
160
|
-
|
|
292
|
+
/** The configured token-standard registries, by admin party; empty without `registries`. */
|
|
293
|
+
const tokenRegistries = (0, token_registry_1.createTokenRegistries)(options.registries, { now });
|
|
294
|
+
const configured = {
|
|
295
|
+
trusts: (admin, contract) => tokenRegistries.get(admin)?.trusts(contract) === true,
|
|
296
|
+
};
|
|
297
|
+
const hasTokenRegistries = tokenRegistries.size > 0;
|
|
298
|
+
/** The interfaces any configured registry is read through; none without `registries`. */
|
|
299
|
+
const readInterfaces = new Set([...tokenRegistries.values()].flatMap((entry) => entry.interfaces));
|
|
300
|
+
/** The filters of `interfaces` that a configured registry is read through. */
|
|
301
|
+
const tokenStandardFilters = (interfaces) => interfaces.filter((entry) => readInterfaces.has(entry)).map((entry) => TOKEN_STANDARD_INTERFACE_FILTERS[entry]);
|
|
302
|
+
const stateTemplates = cantonCoin === null ? DA_STATE_TEMPLATES : CANTON_COIN_STATE_FILTERS;
|
|
303
|
+
/** The configured registries' filters of a state snapshot; empty without `registries`. */
|
|
304
|
+
const tokenStandardStateFilters = tokenStandardFilters(token_registry_1.TOKEN_STANDARD_INTERFACES);
|
|
305
|
+
/**
|
|
306
|
+
* The registries' extra interfaces (with their views), read in a state snapshot beside the standard ones, each once;
|
|
307
|
+
* empty when no registry names any, so the snapshot reads exactly the standard filters.
|
|
308
|
+
*/
|
|
309
|
+
const extraInterfaceFilters = uniqueFilters([...tokenRegistries.values()].flatMap((entry) => entry.extraInterfaceIds.map((interfaceId) => ({ interfaceId, includeInterfaceView: true }))));
|
|
161
310
|
const offerTemplates = cantonCoin === null
|
|
162
311
|
? [templates_2.DA_REGISTRY_TEMPLATES.transferOffer]
|
|
163
312
|
: [templates_2.DA_REGISTRY_TEMPLATES.transferOffer, constants_1.AMULET_TEMPLATES.transferInstruction];
|
|
313
|
+
/** The configured registries' transfer instruction filters, for an answer to an offer; empty without `registries`. */
|
|
314
|
+
const tokenStandardOfferFilters = tokenStandardFilters(TOKEN_STANDARD_OFFER_INTERFACES);
|
|
164
315
|
/** Refuse a write once the wallet session this wallet belongs to ended (`isActive`). */
|
|
165
316
|
function requireActive() {
|
|
166
317
|
if (isActive !== undefined && !isActive()) {
|
|
@@ -169,9 +320,44 @@ function createWallet(options) {
|
|
|
169
320
|
}
|
|
170
321
|
const ledger = (0, cip103_1.cip103Wallet)(provider, partyId, readRetry !== undefined ? { readRetry } : {});
|
|
171
322
|
/** A ledger read; an aborted `signal` stops waiting for it (the wallet's read itself cannot be cancelled). */
|
|
172
|
-
async function read(
|
|
173
|
-
const
|
|
174
|
-
return (await (0, abort_1.awaitWithAbort)(async () => ledger.readActiveContracts(
|
|
323
|
+
async function read(filter, signal) {
|
|
324
|
+
const filters = Array.isArray(filter) ? filter : [filter];
|
|
325
|
+
return (await (0, abort_1.awaitWithAbort)(async () => ledger.readActiveContracts(filters), signal)).contracts;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* One snapshot of `required` and the configured registries' `optional` filters. When the participant refuses the read
|
|
329
|
+
* because a filter names a package or interface it does not have, it is read again without the optional filters the
|
|
330
|
+
* refusal names (every optional filter when it names none), until it answers or no optional filter is left; any other
|
|
331
|
+
* failure, or a refusal of the required filters alone, is thrown. Without optional filters this is one read, exactly
|
|
332
|
+
* as before. `snapshot` is undefined when nothing is left to read (no required filter, and every optional one left
|
|
333
|
+
* out); `omitted` are the optional filters left out, in the order given.
|
|
334
|
+
*/
|
|
335
|
+
async function snapshotWithOptional(required, optional) {
|
|
336
|
+
const base = uniqueFilters(required);
|
|
337
|
+
const keyOf = (entry) => typeof entry === 'string' ? entry : `${entry.interfaceId}+${entry.includeInterfaceView === true}`;
|
|
338
|
+
const baseKeys = new Set(base.map(keyOf));
|
|
339
|
+
const wanted = uniqueFilters(optional).filter((entry) => !baseKeys.has(keyOf(entry)));
|
|
340
|
+
let remaining = wanted;
|
|
341
|
+
const omitted = () => wanted.filter((entry) => !remaining.includes(entry));
|
|
342
|
+
for (;;) {
|
|
343
|
+
const filters = [...base, ...remaining];
|
|
344
|
+
if (filters.length === 0)
|
|
345
|
+
return { snapshot: undefined, omitted: omitted() };
|
|
346
|
+
try {
|
|
347
|
+
return { snapshot: await ledger.readActiveContracts(filters), omitted: omitted() };
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
const refusal = remaining.length > 0 ? unresolvedFilterRefusal(error) : undefined;
|
|
351
|
+
if (refusal === undefined)
|
|
352
|
+
throw error;
|
|
353
|
+
const named = remaining.filter((entry) => refusalNames(refusal, entry));
|
|
354
|
+
remaining = named.length > 0 ? remaining.filter((entry) => !named.includes(entry)) : [];
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/** The contracts of {@link snapshotWithOptional}; none when nothing is left to read. */
|
|
359
|
+
async function readWithOptional(required, optional, signal) {
|
|
360
|
+
return ((await (0, abort_1.awaitWithAbort)(async () => snapshotWithOptional(required, optional), signal)).snapshot?.contracts ?? []);
|
|
175
361
|
}
|
|
176
362
|
async function submit(submission, signal) {
|
|
177
363
|
// Checked again right before the wallet signs: a write's reads and registry calls await, and the session may end
|
|
@@ -231,7 +417,7 @@ function createWallet(options) {
|
|
|
231
417
|
function holdingsIn(contracts, dso) {
|
|
232
418
|
const seen = new Set();
|
|
233
419
|
return contracts.flatMap((contract) => {
|
|
234
|
-
const holding = (0, holdings_1.parseHolding)(contract);
|
|
420
|
+
const holding = (0, holdings_1.parseHolding)(contract, hasTokenRegistries ? configured : undefined);
|
|
235
421
|
if (holding?.owner !== partyId || seen.has(holding.contractId))
|
|
236
422
|
return [];
|
|
237
423
|
if ((0, holdings_1.isCantonCoin)(holding) && (dso === undefined || holding.instrumentId.admin !== dso))
|
|
@@ -240,8 +426,49 @@ function createWallet(options) {
|
|
|
240
426
|
return [holding];
|
|
241
427
|
});
|
|
242
428
|
}
|
|
243
|
-
/**
|
|
244
|
-
|
|
429
|
+
/**
|
|
430
|
+
* The party's DA Registry holdings, for a transfer or burn (Canton Coin and the configured registries' holdings are
|
|
431
|
+
* read-only here).
|
|
432
|
+
*/
|
|
433
|
+
const daHoldingsIn = (contracts) => holdingsIn(contracts).filter((holding) => (holding.source ?? 'da-registry') === 'da-registry');
|
|
434
|
+
/**
|
|
435
|
+
* The party's token-standard holding of this instrument, when its admin is a configured registry; else undefined.
|
|
436
|
+
* Read only for a configured registry's admin. One party may administer DA Registry instruments too, so a refusal
|
|
437
|
+
* goes by the instrument the party holds, never by its admin alone.
|
|
438
|
+
*/
|
|
439
|
+
async function tokenStandardHoldingOf(instrumentId, signal) {
|
|
440
|
+
if (!tokenRegistries.has(instrumentId.admin))
|
|
441
|
+
return undefined;
|
|
442
|
+
return holdingsIn(await readWithOptional([], tokenStandardFilters(TOKEN_STANDARD_HOLDING_INTERFACES), signal)).find((holding) => holding.source === 'token-standard' && (0, instrument_1.sameInstrument)(holding.instrumentId, instrumentId));
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Refuse a transfer of an instrument the party holds through a configured registry and not through DA: this wallet
|
|
446
|
+
* does not transfer them. A DA Registry instrument of the same admin stays transferable.
|
|
447
|
+
*/
|
|
448
|
+
async function refuseTokenStandardTransfer(instrumentId, signal) {
|
|
449
|
+
if (!tokenRegistries.has(instrumentId.admin))
|
|
450
|
+
return;
|
|
451
|
+
// A DA instrument of the same admin goes to the DA path, decided from the DA holdings the transfer reads anyway,
|
|
452
|
+
// so no interface read (which can also match Canton Coin's holdings and reach the list limit) is made for it.
|
|
453
|
+
const daHoldings = daHoldingsIn(await read(templates_2.DA_REGISTRY_TEMPLATES.holding, signal));
|
|
454
|
+
if (daHoldings.some((holding) => (0, instrument_1.sameInstrument)(holding.instrumentId, instrumentId)))
|
|
455
|
+
return;
|
|
456
|
+
if ((await tokenStandardHoldingOf(instrumentId, signal)) === undefined)
|
|
457
|
+
return;
|
|
458
|
+
throw new errors_1.WalletError('invalid-input', `Tokens of ${tokenRegistries.get(instrumentId.admin)?.name ?? instrumentId.admin} cannot be transferred through this wallet yet; nothing was submitted`);
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Refuse a burn of an instrument the party holds through a configured registry and not through DA: this wallet burns
|
|
462
|
+
* DA Registry instruments only. Checked once the party holds no DA holding of it, so a DA instrument of the same
|
|
463
|
+
* admin stays burnable.
|
|
464
|
+
*/
|
|
465
|
+
async function refuseTokenStandardBurn(instrumentId, daHoldings, signal) {
|
|
466
|
+
if (daHoldings.some((holding) => (0, instrument_1.sameInstrument)(holding.instrumentId, instrumentId)))
|
|
467
|
+
return;
|
|
468
|
+
if ((await tokenStandardHoldingOf(instrumentId, signal)) === undefined)
|
|
469
|
+
return;
|
|
470
|
+
throw new errors_1.WalletError('invalid-input', `Tokens of ${tokenRegistries.get(instrumentId.admin)?.name ?? instrumentId.admin} cannot be burned through this wallet; nothing was submitted`);
|
|
471
|
+
}
|
|
245
472
|
/**
|
|
246
473
|
* Refuse a Canton Coin transfer or burn: this wallet only reads Canton Coin. Before any read when the instrument is
|
|
247
474
|
* the known DSO's `Amulet` (pinned, or already confirmed by the registry); otherwise (`holdings` given) an `Amulet`
|
|
@@ -291,8 +518,32 @@ function createWallet(options) {
|
|
|
291
518
|
return byDirection([
|
|
292
519
|
...fromRegistry(contracts, contracts_1.parseTransferOffer, operator),
|
|
293
520
|
...amuletTransferOffers(contracts, dso),
|
|
521
|
+
...tokenStandardOffers(contracts),
|
|
294
522
|
]);
|
|
295
523
|
}
|
|
524
|
+
/**
|
|
525
|
+
* The configured registries' transfer instructions among `contracts` (V2 view, else V1; DA's and Canton Coin's own
|
|
526
|
+
* templates are read by their own rules): the instrument admin is a configured registry that `trusts` the contract
|
|
527
|
+
* (it and the registry's required signatories signed it). Each contract once.
|
|
528
|
+
*/
|
|
529
|
+
function tokenStandardOffers(contracts) {
|
|
530
|
+
if (!hasTokenRegistries)
|
|
531
|
+
return [];
|
|
532
|
+
const seen = new Set();
|
|
533
|
+
return contracts.flatMap((contract) => {
|
|
534
|
+
if (seen.has(contract.contractId))
|
|
535
|
+
return [];
|
|
536
|
+
if ((0, templates_1.isTemplate)(contract, templates_2.DA_REGISTRY_TEMPLATES.transferOffer))
|
|
537
|
+
return [];
|
|
538
|
+
if ((0, templates_1.isTemplate)(contract, constants_1.AMULET_TEMPLATES.transferInstruction))
|
|
539
|
+
return [];
|
|
540
|
+
const offer = (0, contracts_1.parseTokenStandardTransferOffer)(contract);
|
|
541
|
+
if (offer === null || !configured.trusts(offer.instrumentId.admin, contract))
|
|
542
|
+
return [];
|
|
543
|
+
seen.add(contract.contractId);
|
|
544
|
+
return [offer];
|
|
545
|
+
});
|
|
546
|
+
}
|
|
296
547
|
/** Canton Coin's transfer offers among `contracts` of the confirmed `dso`, signed by it and their sender. */
|
|
297
548
|
function amuletTransferOffers(contracts, dso) {
|
|
298
549
|
if (dso === undefined)
|
|
@@ -302,8 +553,12 @@ function createWallet(options) {
|
|
|
302
553
|
return offer === null ? [] : [offer];
|
|
303
554
|
});
|
|
304
555
|
}
|
|
556
|
+
/**
|
|
557
|
+
* The party's offers by direction. A self-transfer is outgoing only; a mint offer to the registry admin itself (its
|
|
558
|
+
* `sender` is the admin, standing in for the mint account) is both: the admin receives it and may also withdraw it.
|
|
559
|
+
*/
|
|
305
560
|
const byDirection = (offers) => ({
|
|
306
|
-
incoming: offers.filter((offer) => offer.receiver === partyId && offer.sender !== partyId),
|
|
561
|
+
incoming: offers.filter((offer) => offer.receiver === partyId && (offer.sender !== partyId || offer.mint === true)),
|
|
307
562
|
outgoing: offers.filter((offer) => offer.sender === partyId),
|
|
308
563
|
});
|
|
309
564
|
/** The party's own preapprovals under DA's operator: the party is their only signatory. */
|
|
@@ -324,17 +579,79 @@ function createWallet(options) {
|
|
|
324
579
|
}
|
|
325
580
|
return found;
|
|
326
581
|
}
|
|
327
|
-
/**
|
|
328
|
-
|
|
582
|
+
/**
|
|
583
|
+
* Each configured registry's entry for each of its instruments among `holdings`, by `instrumentKey` (looked up by id
|
|
584
|
+
* at the instrument's own registry; an instrument it did not answer is left out).
|
|
585
|
+
*/
|
|
586
|
+
async function registryInstruments(holdings) {
|
|
587
|
+
const ids = new Map();
|
|
588
|
+
for (const holding of holdings) {
|
|
589
|
+
if (holding.source === 'token-standard')
|
|
590
|
+
ids.set((0, holdings_1.instrumentKey)(holding.instrumentId), holding.instrumentId);
|
|
591
|
+
}
|
|
592
|
+
const answers = await Promise.all([...ids.entries()].map(async ([key, instrumentId]) => {
|
|
593
|
+
const entry = await tokenRegistries.get(instrumentId.admin)?.instrument(instrumentId);
|
|
594
|
+
return [key, entry];
|
|
595
|
+
}));
|
|
596
|
+
return new Map(answers.flatMap(([key, entry]) => (entry === undefined ? [] : [[key, entry]])));
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* The balances of `holdings`, each with its registry's metadata (Canton Coin's is given: it is looked up apart; the
|
|
600
|
+
* configured registries' come from `instruments`).
|
|
601
|
+
*/
|
|
602
|
+
async function balancesWithMetadata(holdings, cantonCoinMetadata, instruments) {
|
|
329
603
|
return Promise.all((0, holdings_1.instrumentBalances)(holdings).map(async (balance) => {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
604
|
+
let metadata;
|
|
605
|
+
if ((0, holdings_1.isCantonCoin)(balance)) {
|
|
606
|
+
metadata = cantonCoinMetadata;
|
|
607
|
+
}
|
|
608
|
+
else if (balance.source === 'token-standard') {
|
|
609
|
+
const entry = instruments.get(balance.key);
|
|
610
|
+
metadata =
|
|
611
|
+
entry === undefined ? undefined : { name: entry.name, symbol: entry.symbol, decimals: entry.decimals };
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
metadata = await registry.instrumentMetadata(balance.instrumentId);
|
|
615
|
+
}
|
|
333
616
|
return metadata === undefined ? balance : { ...balance, metadata };
|
|
334
617
|
}));
|
|
335
618
|
}
|
|
619
|
+
/**
|
|
620
|
+
* The raw views of the extra interfaces (`TokenRegistryOptions.extraInterfaceIds`) that the registry of `admin`
|
|
621
|
+
* names, among `contract`'s views, keyed by the id as configured: each one's single, successfully computed view
|
|
622
|
+
* (matched by `Module:Entity`, as the ledger reports a view by package id). Undefined when the registry names none or
|
|
623
|
+
* the contract has none of their views.
|
|
624
|
+
*/
|
|
625
|
+
function extraInterfaceViews(admin, contract) {
|
|
626
|
+
const ids = tokenRegistries.get(admin)?.extraInterfaceIds ?? [];
|
|
627
|
+
if (contract === undefined || ids.length === 0)
|
|
628
|
+
return undefined;
|
|
629
|
+
const views = Object.create(null);
|
|
630
|
+
let found = false;
|
|
631
|
+
for (const interfaceId of ids) {
|
|
632
|
+
const value = (0, holding_view_1.interfaceViewValue)(contract, interfaceId);
|
|
633
|
+
if (value === undefined || value === null)
|
|
634
|
+
continue;
|
|
635
|
+
views[interfaceId] = value;
|
|
636
|
+
found = true;
|
|
637
|
+
}
|
|
638
|
+
return found ? Object.freeze(views) : undefined;
|
|
639
|
+
}
|
|
640
|
+
/** `entry` with its extra interface views (see {@link extraInterfaceViews}), for a configured registry's entry. */
|
|
641
|
+
function withInterfaceViews(entry, contractsById) {
|
|
642
|
+
if (extraInterfaceFilters.length === 0)
|
|
643
|
+
return entry;
|
|
644
|
+
const interfaceViews = extraInterfaceViews(entry.instrumentId.admin, contractsById.get(entry.contractId));
|
|
645
|
+
return interfaceViews === undefined ? entry : { ...entry, interfaceViews };
|
|
646
|
+
}
|
|
336
647
|
async function readState() {
|
|
337
|
-
|
|
648
|
+
// Always at least the DA templates, so there is always a snapshot.
|
|
649
|
+
const stateRead = await snapshotWithOptional(stateTemplates, [
|
|
650
|
+
...tokenStandardStateFilters,
|
|
651
|
+
...extraInterfaceFilters,
|
|
652
|
+
]);
|
|
653
|
+
const { offset, contracts } = stateRead.snapshot;
|
|
654
|
+
const registryInterfacesUnavailable = stateRead.omitted.map(filterInterfaceName);
|
|
338
655
|
// Asked only when the party has Canton Coin. The DSO and Canton Coin's metadata are looked up at once, so a
|
|
339
656
|
// snapshot waits for the registry once (see `createAmuletRegistry` for how long).
|
|
340
657
|
const [{ dso, unavailable }, cantonCoinMetadata] = await Promise.all([
|
|
@@ -343,14 +660,18 @@ function createWallet(options) {
|
|
|
343
660
|
? cantonCoin.instrumentMetadata()
|
|
344
661
|
: undefined,
|
|
345
662
|
]);
|
|
346
|
-
const
|
|
663
|
+
const contractsById = new Map(extraInterfaceFilters.length === 0 ? [] : contracts.map((contract) => [contract.contractId, contract]));
|
|
664
|
+
const holdings = holdingsIn(contracts, dso).map((holding) => holding.source === 'token-standard' ? withInterfaceViews(holding, contractsById) : holding);
|
|
665
|
+
const instruments = await registryInstruments(holdings);
|
|
347
666
|
const [operator, balances] = await Promise.all([
|
|
348
667
|
registry.operatorParty(),
|
|
349
|
-
balancesWithMetadata(holdings, cantonCoinMetadata),
|
|
668
|
+
balancesWithMetadata(holdings, cantonCoinMetadata, instruments),
|
|
350
669
|
]);
|
|
351
670
|
const at = now();
|
|
352
671
|
const withExpiry = (offers) => offers.map((offer) => ({ ...offer, expired: (0, tokens_1.isTransferOfferExpired)(offer, at) }));
|
|
353
|
-
const
|
|
672
|
+
const withViews = (list) => list.map((offer) => (offer.registry === 'token-standard' ? withInterfaceViews(offer, contractsById) : offer));
|
|
673
|
+
const listed = transferOffers(contracts, operator, dso);
|
|
674
|
+
const offers = { incoming: withViews(listed.incoming), outgoing: withViews(listed.outgoing) };
|
|
354
675
|
const outgoing = withExpiry(offers.outgoing);
|
|
355
676
|
const reclaimable = (0, tokens_1.lockedInExpiredOffers)(holdings, outgoing.filter((offer) => offer.expired === true));
|
|
356
677
|
const inExpiredLocks = (0, tokens_1.unitsByInstrument)((0, tokens_1.expiredUnclaimedLocks)(holdings, [...offers.incoming, ...offers.outgoing], at));
|
|
@@ -367,6 +688,7 @@ function createWallet(options) {
|
|
|
367
688
|
burnRequests: ownBurnRequests(contracts, operator),
|
|
368
689
|
transferPreapprovals: ownPreapprovals(contracts, operator),
|
|
369
690
|
cantonCoinUnavailable: unavailable,
|
|
691
|
+
registryInterfacesUnavailable,
|
|
370
692
|
};
|
|
371
693
|
}
|
|
372
694
|
let inFlight;
|
|
@@ -462,6 +784,7 @@ function createWallet(options) {
|
|
|
462
784
|
const planSignal = call.signal;
|
|
463
785
|
(0, abort_1.throwIfAborted)(planSignal);
|
|
464
786
|
const planned = plannedRequest(request);
|
|
787
|
+
await refuseTokenStandardTransfer(planned.instrumentId, planSignal);
|
|
465
788
|
const kind = (await prepareTransfer(planned, planSignal)).factory.transferKind;
|
|
466
789
|
requirePreapprovalIfAsked(planned, kind);
|
|
467
790
|
return Object.freeze({
|
|
@@ -479,7 +802,7 @@ function createWallet(options) {
|
|
|
479
802
|
async function answerTransferOfferSubmission(transferOfferCid, action, signal) {
|
|
480
803
|
// Read the target first, then resolve only the registry that validates it: the DSO for a Canton Coin offer, DA's
|
|
481
804
|
// operator for a DA offer. Neither offer kind depends on the other registry being up.
|
|
482
|
-
const target = (await
|
|
805
|
+
const target = (await readWithOptional(offerTemplates, tokenStandardOfferFilters, signal)).filter((contract) => contract.contractId === transferOfferCid);
|
|
483
806
|
let offers;
|
|
484
807
|
if (cantonCoin !== null && anyOf(target, [constants_1.AMULET_TEMPLATES.transferInstruction])) {
|
|
485
808
|
const { dso, unavailable } = await cantonCoinDso(target, signal, registry_1.AMULET_REGISTRY_REQUEST_TIMEOUT_MS);
|
|
@@ -491,16 +814,50 @@ function createWallet(options) {
|
|
|
491
814
|
else if (anyOf(target, [templates_2.DA_REGISTRY_TEMPLATES.transferOffer])) {
|
|
492
815
|
offers = byDirection(fromRegistry(target, contracts_1.parseTransferOffer, await registry.operatorParty(signal)));
|
|
493
816
|
}
|
|
817
|
+
else if (hasTokenRegistries) {
|
|
818
|
+
offers = byDirection(tokenStandardOffers(target));
|
|
819
|
+
}
|
|
494
820
|
else {
|
|
495
821
|
offers = { incoming: [], outgoing: [] };
|
|
496
822
|
}
|
|
497
823
|
const { incoming, outgoing } = offers;
|
|
498
824
|
const offer = find(action === 'withdraw' ? outgoing : incoming, transferOfferCid, action === 'withdraw' ? 'Outgoing transfer offer' : 'Incoming transfer offer');
|
|
825
|
+
if (offer.registry === 'token-standard')
|
|
826
|
+
refuseTokenStandardAnswer(offer, action);
|
|
499
827
|
return offerAnswerSubmission(offer, action, signal);
|
|
500
828
|
}
|
|
829
|
+
/**
|
|
830
|
+
* Refuse, before any registry call, an action a configured registry's instruction does not let the party take alone
|
|
831
|
+
* (its V2 `availableActions`), and a withdrawal of a mint offer that does not implement V2 (its V1 sender is the mint
|
|
832
|
+
* account's stub party, which nothing can act as).
|
|
833
|
+
*/
|
|
834
|
+
function refuseTokenStandardAnswer(offer, action) {
|
|
835
|
+
if (offer.mint === true && action === 'withdraw' && !implementedStandards(offer).includes('v2')) {
|
|
836
|
+
throw new errors_1.WalletError('invalid-input', `Mint offer ${offer.contractId} implements only the V1 TransferInstruction, whose sender is the mint account: it cannot be withdrawn through this wallet; nothing was submitted`);
|
|
837
|
+
}
|
|
838
|
+
if (!(0, contracts_1.partyMayAct)(offer, action, partyId)) {
|
|
839
|
+
throw new errors_1.WalletError('invalid-input', `Transfer instruction ${offer.contractId} does not let ${partyId} ${action} it alone; nothing was submitted`);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
501
842
|
/** Exercise `action` on a transfer offer already read and validated, with its own registry's choice context. */
|
|
502
843
|
async function offerAnswerSubmission(offer, action, signal) {
|
|
503
844
|
const transferOfferCid = offer.contractId;
|
|
845
|
+
if (offer.registry === 'token-standard') {
|
|
846
|
+
const owner = tokenRegistries.get(offer.instrumentId.admin);
|
|
847
|
+
if (owner === undefined) {
|
|
848
|
+
throw new errors_1.WalletError('not-found', `No registry is configured for ${offer.instrumentId.admin}, so transfer offer ${transferOfferCid} cannot be answered; nothing was submitted`);
|
|
849
|
+
}
|
|
850
|
+
const standard = answerStandard(offer, action);
|
|
851
|
+
const tokenContext = await owner.transferInstructionContext(transferOfferCid, action, standard, signal);
|
|
852
|
+
return {
|
|
853
|
+
commands: [
|
|
854
|
+
standard === 'v2'
|
|
855
|
+
? (0, commands_1.transferInstructionV2Command)(transferOfferCid, action, partyId, tokenContext.choiceContextData)
|
|
856
|
+
: (0, commands_1.transferInstructionCommand)(transferOfferCid, action, tokenContext.choiceContextData),
|
|
857
|
+
],
|
|
858
|
+
disclosedContracts: tokenContext.disclosedContracts,
|
|
859
|
+
};
|
|
860
|
+
}
|
|
504
861
|
const context = offer.registry === 'amulet' && cantonCoin !== null
|
|
505
862
|
? await cantonCoin.transferInstructionContext(transferOfferCid, action, signal)
|
|
506
863
|
: await registry.transferInstructionContext(offer.instrumentId.admin, transferOfferCid, action, signal);
|
|
@@ -630,6 +987,7 @@ function createWallet(options) {
|
|
|
630
987
|
const executeBefore = deadline(burn.executeBefore, at, BURN_TTL_MS);
|
|
631
988
|
const daHoldings = daHoldingsIn(await read(templates_2.DA_REGISTRY_TEMPLATES.holding, signal));
|
|
632
989
|
refuseCantonCoin(instrumentId, 'burned', daHoldings);
|
|
990
|
+
await refuseTokenStandardBurn(instrumentId, daHoldings, signal);
|
|
633
991
|
const selection = (0, holdings_1.selectHoldings)(daHoldings, {
|
|
634
992
|
owner: partyId,
|
|
635
993
|
instrumentId,
|
|
@@ -750,6 +1108,7 @@ function createWallet(options) {
|
|
|
750
1108
|
/** A transfer planned and prepared in one go, refused as `planTransfer` refuses it. */
|
|
751
1109
|
async function transferRequestSubmission(request, signal) {
|
|
752
1110
|
const planned = plannedRequest(request);
|
|
1111
|
+
await refuseTokenStandardTransfer(planned.instrumentId, signal);
|
|
753
1112
|
const { transfer, factory } = await prepareTransfer(planned, signal);
|
|
754
1113
|
requirePreapprovalIfAsked(planned, factory.transferKind);
|
|
755
1114
|
return transferSubmission(transfer, factory);
|