@fairmint/canton-node-sdk 0.0.281 → 0.0.283
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 +128 -154
- package/build/src/utils/external-signing/wallet-ownership-challenger.d.ts.map +1 -1
- package/build/src/utils/external-signing/wallet-ownership-challenger.js +2 -3
- package/build/src/utils/external-signing/wallet-ownership-challenger.js.map +1 -1
- package/build/src/utils/external-signing/wallet-ownership.d.ts.map +1 -1
- package/build/src/utils/external-signing/wallet-ownership.js +8 -18
- package/build/src/utils/external-signing/wallet-ownership.js.map +1 -1
- package/build/src/wallets/acs.d.ts +2 -1
- package/build/src/wallets/acs.d.ts.map +1 -1
- package/build/src/wallets/acs.js +6 -31
- package/build/src/wallets/acs.js.map +1 -1
- package/build/src/wallets/cip103.d.ts +32 -68
- package/build/src/wallets/cip103.d.ts.map +1 -1
- package/build/src/wallets/cip103.js +112 -230
- package/build/src/wallets/cip103.js.map +1 -1
- package/build/src/wallets/commands.d.ts +39 -6
- package/build/src/wallets/commands.d.ts.map +1 -1
- package/build/src/wallets/commands.js +50 -5
- package/build/src/wallets/commands.js.map +1 -1
- package/build/src/wallets/contracts.d.ts +12 -0
- package/build/src/wallets/contracts.d.ts.map +1 -1
- package/build/src/wallets/contracts.js +21 -5
- package/build/src/wallets/contracts.js.map +1 -1
- package/build/src/wallets/da-utilities/client.d.ts +61 -29
- package/build/src/wallets/da-utilities/client.d.ts.map +1 -1
- package/build/src/wallets/da-utilities/client.js +78 -75
- package/build/src/wallets/da-utilities/client.js.map +1 -1
- package/build/src/wallets/errors.d.ts +11 -8
- package/build/src/wallets/errors.d.ts.map +1 -1
- package/build/src/wallets/errors.js +13 -3
- package/build/src/wallets/errors.js.map +1 -1
- package/build/src/wallets/holdings.d.ts +14 -4
- package/build/src/wallets/holdings.d.ts.map +1 -1
- package/build/src/wallets/holdings.js +27 -47
- package/build/src/wallets/holdings.js.map +1 -1
- package/build/src/wallets/index.d.ts +17 -21
- package/build/src/wallets/index.d.ts.map +1 -1
- package/build/src/wallets/index.js +14 -23
- package/build/src/wallets/index.js.map +1 -1
- package/build/src/wallets/instrument.js +1 -1
- package/build/src/wallets/instrument.js.map +1 -1
- package/build/src/wallets/ownership.d.ts +20 -26
- package/build/src/wallets/ownership.d.ts.map +1 -1
- package/build/src/wallets/ownership.js +33 -27
- package/build/src/wallets/ownership.js.map +1 -1
- package/build/src/wallets/session.d.ts +30 -68
- package/build/src/wallets/session.d.ts.map +1 -1
- package/build/src/wallets/session.js +119 -167
- package/build/src/wallets/session.js.map +1 -1
- package/build/src/wallets/store.d.ts +148 -86
- package/build/src/wallets/store.d.ts.map +1 -1
- package/build/src/wallets/store.js +267 -99
- package/build/src/wallets/store.js.map +1 -1
- package/build/src/wallets/templates.d.ts +10 -23
- package/build/src/wallets/templates.d.ts.map +1 -1
- package/build/src/wallets/templates.js +9 -33
- package/build/src/wallets/templates.js.map +1 -1
- package/build/src/wallets/types.d.ts +6 -20
- package/build/src/wallets/types.d.ts.map +1 -1
- package/build/src/wallets/wallet.d.ts +130 -74
- package/build/src/wallets/wallet.d.ts.map +1 -1
- package/build/src/wallets/wallet.js +347 -185
- package/build/src/wallets/wallet.js.map +1 -1
- package/package.json +1 -1
- package/build/src/wallets/cip56-core.d.ts +0 -175
- package/build/src/wallets/cip56-core.d.ts.map +0 -1
- package/build/src/wallets/cip56-core.js +0 -325
- package/build/src/wallets/cip56-core.js.map +0 -1
- package/build/src/wallets/token-registry.d.ts +0 -97
- package/build/src/wallets/token-registry.d.ts.map +0 -1
- package/build/src/wallets/token-registry.js +0 -19
- package/build/src/wallets/token-registry.js.map +0 -1
|
@@ -1,55 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.MINT_OFFER_REJECT_REASON = exports.BURN_REFERENCE_PATTERN = void 0;
|
|
4
4
|
exports.newBurnReference = newBurnReference;
|
|
5
5
|
exports.createWallet = createWallet;
|
|
6
6
|
/**
|
|
7
|
-
* One token
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* its choice context from the registry, refuses anything that does not check out, and submits as the wallet party
|
|
7
|
+
* One wallet party's DA Utility Registry token actions through any CIP-0103 wallet provider: holdings, transfers,
|
|
8
|
+
* transfer offers, mint offers, burn requests and transfer preapprovals. Every write reads its contracts fresh, fetches
|
|
9
|
+
* its choice context from DA's Utilities API, refuses anything that does not check out, and submits as the wallet party
|
|
11
10
|
* only.
|
|
12
11
|
*
|
|
13
|
-
*
|
|
14
|
-
* trusted
|
|
12
|
+
* Trust: a holding counts when its registrar and owner signed it; an offer, a request or a preapproval when it names
|
|
13
|
+
* DA's operator and (offers and requests) a provider trusted for its instrument admin signed it. By default a registrar
|
|
14
|
+
* must be its own provider; `registry.trustedProviders` trusts others (see `isFromRegistry`).
|
|
15
15
|
*/
|
|
16
|
+
const cip103_1 = require("./cip103");
|
|
16
17
|
const commands_1 = require("./commands");
|
|
17
18
|
const contracts_1 = require("./contracts");
|
|
18
19
|
const client_1 = require("./da-utilities/client");
|
|
19
20
|
const errors_1 = require("./errors");
|
|
20
21
|
const holdings_1 = require("./holdings");
|
|
21
22
|
const templates_1 = require("../da-registry/templates");
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const DAY_MS = 24 * 60 * 60 *
|
|
23
|
+
const constants_1 = require("../utils/token-standard/v1/constants");
|
|
24
|
+
const SECOND_MS = 1000;
|
|
25
|
+
const DAY_MS = 24 * 60 * 60 * SECOND_MS;
|
|
26
|
+
/** Transfers: `executeBefore` defaults to now + 24 h, a common wallet default. */
|
|
27
|
+
const TRANSFER_TTL_MS = DAY_MS;
|
|
25
28
|
/** Burn requests: `executeBefore` defaults to now + 7 days, giving the registrar a week to accept or reject. */
|
|
26
|
-
|
|
29
|
+
const BURN_TTL_MS = 7 * DAY_MS;
|
|
30
|
+
/**
|
|
31
|
+
* `requestedAt` = now − 60 s for transfers and burn requests: registries require it in the past by ledger time, and the
|
|
32
|
+
* client's clock can run ahead of the participant's.
|
|
33
|
+
*/
|
|
34
|
+
const REQUESTED_AT_SKEW_MS = 60 * SECOND_MS;
|
|
27
35
|
/** A burn reference: no personal data (DA's operator and the registrar see it). */
|
|
28
36
|
exports.BURN_REFERENCE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,99}$/;
|
|
37
|
+
/** The reason a rejected mint offer gives the registrar: fixed, so nothing personal reaches the ledger. */
|
|
38
|
+
exports.MINT_OFFER_REJECT_REASON = 'Rejected by the holder';
|
|
29
39
|
/** The number of random base-36 digits that end a burn reference. */
|
|
30
40
|
const BURN_REFERENCE_SUFFIX_DIGITS = 6;
|
|
31
41
|
/** 7 × 36: random bytes at or above it are drawn again, so every base-36 digit is equally likely. */
|
|
32
42
|
const UNBIASED_BYTE_LIMIT = 252;
|
|
33
|
-
/**
|
|
34
|
-
* `count` uniformly distributed base-36 digits, from the Web Crypto API when the runtime has it (one byte per digit,
|
|
35
|
-
* bytes in the incomplete last range drawn again). Not a secret: only unique.
|
|
36
|
-
*/
|
|
43
|
+
/** `count` uniformly distributed base-36 digits from the Web Crypto API. Not a secret: only unique. */
|
|
37
44
|
function randomBase36(count) {
|
|
38
|
-
|
|
39
|
-
const { crypto } = globalThis;
|
|
45
|
+
const bytes = new Uint8Array(16);
|
|
40
46
|
let digits = '';
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (byte < UNBIASED_BYTE_LIMIT && digits.length < count)
|
|
46
|
-
digits += (byte % 36).toString(36);
|
|
47
|
-
}
|
|
47
|
+
while (digits.length < count) {
|
|
48
|
+
for (const byte of globalThis.crypto.getRandomValues(bytes)) {
|
|
49
|
+
if (byte < UNBIASED_BYTE_LIMIT && digits.length < count)
|
|
50
|
+
digits += (byte % 36).toString(36);
|
|
48
51
|
}
|
|
49
|
-
return digits;
|
|
50
52
|
}
|
|
51
|
-
while (digits.length < count)
|
|
52
|
-
digits += Math.floor(Math.random() * 36).toString(36);
|
|
53
53
|
return digits;
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
@@ -57,212 +57,374 @@ function randomBase36(count) {
|
|
|
57
57
|
* and the registrar see it). Never `redeem-…`, a prefix a transfer agent may reserve for the redemptions it files
|
|
58
58
|
* itself. `requestBurn` uses one when no `reference` is given.
|
|
59
59
|
*/
|
|
60
|
-
function newBurnReference(now
|
|
61
|
-
const stamp = new Date(
|
|
62
|
-
|
|
63
|
-
return `holder-burn-${stamp}-${suffix}`;
|
|
60
|
+
function newBurnReference(now) {
|
|
61
|
+
const stamp = new Date(requireTime(now, 'now')).toISOString().replace(/[-:]|\.\d+/g, '');
|
|
62
|
+
return `holder-burn-${stamp}-${randomBase36(BURN_REFERENCE_SUFFIX_DIGITS)}`;
|
|
64
63
|
}
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
/** A sentence explaining how the registry runs a transfer of each kind. */
|
|
65
|
+
const TRANSFER_KIND_DESCRIPTIONS = {
|
|
66
|
+
direct: 'Direct: the receiver pre-approved this instrument, so the tokens move at once.',
|
|
67
|
+
offer: 'Offer: the receiver must accept it before it expires. Your tokens stay locked until it is accepted, rejected by the receiver or withdrawn by you; expiry only stops acceptance and does not unlock them.',
|
|
68
|
+
self: 'Self-transfer: the receiver is this wallet; the holdings are merged or split.',
|
|
69
|
+
};
|
|
70
|
+
/** Every template a state snapshot reads, in one cumulative filter. */
|
|
71
|
+
const STATE_TEMPLATES = [
|
|
72
|
+
templates_1.DA_REGISTRY_TEMPLATES.holding,
|
|
73
|
+
templates_1.DA_REGISTRY_TEMPLATES.transferOffer,
|
|
74
|
+
templates_1.DA_REGISTRY_TEMPLATES.mintOffer,
|
|
75
|
+
templates_1.DA_REGISTRY_TEMPLATES.burnRequest,
|
|
76
|
+
templates_1.DA_REGISTRY_TEMPLATES.transferPreapproval,
|
|
77
|
+
];
|
|
78
|
+
function requireNonEmpty(value, field) {
|
|
79
|
+
if (typeof value !== 'string' || value.trim().length === 0) {
|
|
80
|
+
throw new errors_1.WalletError('invalid-input', `${field} is required`);
|
|
74
81
|
}
|
|
75
|
-
return
|
|
82
|
+
return value;
|
|
76
83
|
}
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
/** Milliseconds of a caller's `Date`; `invalid-input` for an invalid one (NaN), which `toISOString` would throw on. */
|
|
85
|
+
function requireTime(date, field) {
|
|
86
|
+
const ms = date instanceof Date ? date.getTime() : Number.NaN;
|
|
87
|
+
if (!Number.isFinite(ms)) {
|
|
88
|
+
throw new errors_1.WalletError('invalid-input', `${field} is not a valid time`);
|
|
89
|
+
}
|
|
90
|
+
return ms;
|
|
91
|
+
}
|
|
92
|
+
/** ISO-8601 of a time in milliseconds; `invalid-input` when it is outside the `Date` range. */
|
|
93
|
+
function isoTime(ms, field) {
|
|
94
|
+
return new Date(requireTime(new Date(ms), field)).toISOString();
|
|
95
|
+
}
|
|
96
|
+
function requireInstrumentId(instrumentId) {
|
|
97
|
+
return {
|
|
98
|
+
admin: requireNonEmpty(instrumentId.admin, 'instrumentId.admin'),
|
|
99
|
+
id: requireNonEmpty(instrumentId.id, 'instrumentId.id'),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** A deadline after `at`: the given one, else `at + defaultMs`; `invalid-input` when it is not in the future. */
|
|
103
|
+
function deadline(executeBefore, at, defaultMs) {
|
|
104
|
+
const ms = executeBefore === undefined ? at + defaultMs : requireTime(executeBefore, 'executeBefore');
|
|
105
|
+
if (ms <= at)
|
|
106
|
+
throw new errors_1.WalletError('invalid-input', 'executeBefore must be in the future');
|
|
107
|
+
return isoTime(ms, 'executeBefore');
|
|
108
|
+
}
|
|
109
|
+
/** The DA Registry token actions of `options.partyId` through `options.provider`. */
|
|
81
110
|
function createWallet(options) {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
111
|
+
const { provider, isActive, now: clock = () => new Date(), readRetry } = options;
|
|
112
|
+
const partyId = requireNonEmpty(options.partyId, 'partyId');
|
|
113
|
+
const { network } = options;
|
|
114
|
+
if (!(0, cip103_1.isDaNetwork)(network))
|
|
115
|
+
throw new TypeError('createWallet: network must be devnet or mainnet');
|
|
116
|
+
const registry = (0, client_1.createDaUtilitiesClient)(network, options.registry);
|
|
117
|
+
const now = () => requireTime(clock(), 'now()');
|
|
118
|
+
/** Refuse a write once the wallet session this wallet belongs to ended (`isActive`). */
|
|
119
|
+
function requireActive() {
|
|
120
|
+
if (isActive !== undefined && !isActive()) {
|
|
121
|
+
throw new errors_1.WalletError('invalid-input', 'The wallet session this action was prepared in has ended (the wallet disconnected or switched accounts); nothing was submitted. Start the action again.');
|
|
91
122
|
}
|
|
92
|
-
return da;
|
|
93
123
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
124
|
+
const ledger = (0, cip103_1.cip103Wallet)(provider, partyId, readRetry !== undefined ? { readRetry } : {});
|
|
125
|
+
async function read(templateId) {
|
|
126
|
+
return (await ledger.readActiveContracts([templateId])).contracts;
|
|
127
|
+
}
|
|
128
|
+
async function submit(submission) {
|
|
129
|
+
// Checked again right before the wallet signs: a write's reads and registry calls await, and the session may end
|
|
130
|
+
// meanwhile.
|
|
131
|
+
requireActive();
|
|
132
|
+
return ledger.submit(submission);
|
|
103
133
|
}
|
|
104
|
-
|
|
134
|
+
// Checked and taken synchronously (before the first await), so a second write started in the same tick is refused
|
|
135
|
+
// and two writes never select the same holdings.
|
|
136
|
+
let busy = false;
|
|
137
|
+
// Bumped when a write settles: a snapshot started before then may predate the write, so later reads do not join it.
|
|
138
|
+
let writesSettled = 0;
|
|
139
|
+
async function exclusive(action) {
|
|
140
|
+
if (busy) {
|
|
141
|
+
throw new errors_1.WalletError('busy', 'Another wallet action is in progress; nothing was submitted');
|
|
142
|
+
}
|
|
143
|
+
requireActive();
|
|
144
|
+
busy = true;
|
|
145
|
+
try {
|
|
146
|
+
return await action();
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
busy = false;
|
|
150
|
+
writesSettled += 1;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const holdingsIn = (contracts) => contracts.flatMap((contract) => {
|
|
154
|
+
const holding = (0, holdings_1.parseHolding)(contract);
|
|
155
|
+
return holding !== null && holding.owner === partyId ? [holding] : [];
|
|
156
|
+
});
|
|
157
|
+
/** The registry contracts of `parse` among `contracts` that DA's registry made (see `isFromRegistry`). */
|
|
158
|
+
function fromRegistry(contracts, parse, operator) {
|
|
105
159
|
return contracts.flatMap((contract) => {
|
|
106
|
-
const
|
|
107
|
-
return
|
|
108
|
-
|
|
109
|
-
(0, contracts_1.isFromRegistry)(request, contract.signatories, operator, client.trustedProvidersOf)
|
|
110
|
-
? [request]
|
|
160
|
+
const parsed = parse(contract);
|
|
161
|
+
return parsed !== null && (0, contracts_1.isFromRegistry)(parsed, contract.signatories, operator, registry.trustedProvidersOf)
|
|
162
|
+
? [parsed]
|
|
111
163
|
: [];
|
|
112
164
|
});
|
|
113
165
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
166
|
+
const ownMintOffers = (contracts, operator) => fromRegistry(contracts, contracts_1.parseMintOffer, operator).filter((offer) => offer.holder === partyId);
|
|
167
|
+
const ownBurnRequests = (contracts, operator) => fromRegistry(contracts, contracts_1.parseBurnRequest, operator).filter((request) => request.holder === partyId);
|
|
168
|
+
function transferOffers(contracts, operator) {
|
|
169
|
+
const offers = fromRegistry(contracts, contracts_1.parseTransferOffer, operator);
|
|
170
|
+
return {
|
|
171
|
+
incoming: offers.filter((offer) => offer.receiver === partyId && offer.sender !== partyId),
|
|
172
|
+
outgoing: offers.filter((offer) => offer.sender === partyId),
|
|
173
|
+
};
|
|
118
174
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
return
|
|
175
|
+
/** The party's own preapprovals under DA's operator: the party is their only signatory. */
|
|
176
|
+
const ownPreapprovals = (contracts, operator) => contracts.flatMap((contract) => {
|
|
177
|
+
const preapproval = (0, contracts_1.parseTransferPreapproval)(contract);
|
|
178
|
+
return preapproval !== null &&
|
|
179
|
+
preapproval.receiver === partyId &&
|
|
180
|
+
preapproval.operator === operator &&
|
|
181
|
+
contract.signatories.includes(partyId)
|
|
182
|
+
? [preapproval]
|
|
183
|
+
: [];
|
|
184
|
+
});
|
|
185
|
+
/** One of `listed`, by contract id; `not-found` otherwise. */
|
|
186
|
+
function find(listed, contractId, what) {
|
|
187
|
+
const found = listed.find((item) => item.contractId === contractId);
|
|
188
|
+
if (found === undefined) {
|
|
189
|
+
throw new errors_1.WalletError('not-found', `${what} ${contractId} is not among the wallet's active ${what.toLowerCase()}s`);
|
|
190
|
+
}
|
|
191
|
+
return found;
|
|
123
192
|
}
|
|
124
|
-
async function
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
193
|
+
async function balancesWithMetadata(holdings) {
|
|
194
|
+
return Promise.all((0, holdings_1.instrumentBalances)(holdings).map(async (balance) => {
|
|
195
|
+
const metadata = await registry.instrumentMetadata(balance.instrumentId);
|
|
196
|
+
return metadata === undefined ? balance : { ...balance, metadata };
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
async function readState() {
|
|
200
|
+
const { offset, contracts } = await ledger.readActiveContracts(STATE_TEMPLATES);
|
|
201
|
+
const holdings = holdingsIn(contracts);
|
|
202
|
+
const [operator, balances] = await Promise.all([registry.operatorParty(), balancesWithMetadata(holdings)]);
|
|
133
203
|
return {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
204
|
+
offset,
|
|
205
|
+
holdings,
|
|
206
|
+
balances,
|
|
207
|
+
transferOffers: transferOffers(contracts, operator),
|
|
208
|
+
mintOffers: ownMintOffers(contracts, operator),
|
|
209
|
+
burnRequests: ownBurnRequests(contracts, operator),
|
|
210
|
+
transferPreapprovals: ownPreapprovals(contracts, operator),
|
|
137
211
|
};
|
|
138
212
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
213
|
+
let inFlight;
|
|
214
|
+
async function loadState() {
|
|
215
|
+
if (inFlight?.writesSettled === writesSettled)
|
|
216
|
+
return inFlight.promise;
|
|
217
|
+
const promise = readState().finally(() => {
|
|
218
|
+
if (inFlight?.promise === promise)
|
|
219
|
+
inFlight = undefined;
|
|
220
|
+
});
|
|
221
|
+
inFlight = { writesSettled, promise };
|
|
222
|
+
return promise;
|
|
223
|
+
}
|
|
224
|
+
/** A validated, frozen copy of a transfer request, so a later change to the caller's object changes nothing. */
|
|
225
|
+
function plannedRequest(request) {
|
|
226
|
+
const { memo, requirePreapproval, executeBefore } = request;
|
|
227
|
+
return Object.freeze({
|
|
228
|
+
receiver: requireNonEmpty(request.receiver, 'receiver'),
|
|
229
|
+
instrumentId: Object.freeze(requireInstrumentId(request.instrumentId)),
|
|
230
|
+
amount: request.amount,
|
|
231
|
+
...(memo !== undefined && memo.length > 0 && { memo }),
|
|
232
|
+
...(requirePreapproval === true && { requirePreapproval }),
|
|
233
|
+
...(executeBefore !== undefined && { executeBefore: new Date(requireTime(executeBefore, 'executeBefore')) }),
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Select inputs and ask the registry for the factory. The factory must be a disclosed contract of a template DA uses
|
|
238
|
+
* for the kind it answered (a direct transfer runs through a `TransferPreapproval`, nothing else does).
|
|
239
|
+
*/
|
|
240
|
+
async function prepareTransfer(request) {
|
|
241
|
+
const at = now();
|
|
242
|
+
const executeBefore = deadline(request.executeBefore, at, TRANSFER_TTL_MS);
|
|
243
|
+
const selection = (0, holdings_1.selectHoldings)(holdingsIn(await read(templates_1.DA_REGISTRY_TEMPLATES.holding)), {
|
|
244
|
+
owner: partyId,
|
|
245
|
+
instrumentId: request.instrumentId,
|
|
246
|
+
amount: request.amount,
|
|
247
|
+
});
|
|
248
|
+
const transfer = {
|
|
249
|
+
sender: partyId,
|
|
250
|
+
receiver: request.receiver,
|
|
251
|
+
amount: selection.amount,
|
|
252
|
+
instrumentId: request.instrumentId,
|
|
253
|
+
requestedAt: isoTime(at - REQUESTED_AT_SKEW_MS, 'requestedAt'),
|
|
254
|
+
executeBefore,
|
|
255
|
+
inputHoldingCids: selection.inputHoldingCids,
|
|
256
|
+
meta: { values: request.memo !== undefined ? { [constants_1.TOKEN_STANDARD_V1_REASON_META_KEY]: request.memo } : {} },
|
|
257
|
+
};
|
|
258
|
+
const factory = await registry.transferFactory(request.instrumentId.admin, transfer);
|
|
259
|
+
const factoryError = (0, client_1.factoryTemplateError)(factory, client_1.DA_TRANSFER_FACTORY_TEMPLATES[factory.transferKind]);
|
|
260
|
+
if (factoryError !== null) {
|
|
261
|
+
throw new errors_1.RegistryResponseError(`${factoryError} It answered a ${factory.transferKind} transfer.`);
|
|
152
262
|
}
|
|
153
|
-
|
|
154
|
-
|
|
263
|
+
return { transfer, factory };
|
|
264
|
+
}
|
|
265
|
+
async function planTransfer(request) {
|
|
266
|
+
const planned = plannedRequest(request);
|
|
267
|
+
const kind = (await prepareTransfer(planned)).factory.transferKind;
|
|
268
|
+
if (planned.requirePreapproval === true && kind !== 'direct') {
|
|
269
|
+
throw new errors_1.WalletError('preapproval-required', `The receiver has no matching transfer preapproval (the registry would make a${kind === 'offer' ? 'n offer' : ` ${kind} transfer`}); nothing was submitted`);
|
|
155
270
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
271
|
+
return Object.freeze({
|
|
272
|
+
kind,
|
|
273
|
+
description: TRANSFER_KIND_DESCRIPTIONS[kind],
|
|
274
|
+
partyId,
|
|
275
|
+
request: planned,
|
|
276
|
+
submit: async () => exclusive(async () => {
|
|
277
|
+
const { transfer, factory } = await prepareTransfer(planned);
|
|
278
|
+
if (factory.transferKind !== kind) {
|
|
279
|
+
throw new errors_1.WalletError('kind-changed', `The registry would now make a${factory.transferKind === 'offer' ? 'n' : ''} ${factory.transferKind} transfer, not the planned ${kind}; nothing was submitted`);
|
|
280
|
+
}
|
|
281
|
+
const result = await submit({
|
|
282
|
+
commands: [
|
|
283
|
+
(0, commands_1.transferCommand)({
|
|
284
|
+
factoryId: factory.factoryId,
|
|
285
|
+
expectedAdmin: transfer.instrumentId.admin,
|
|
286
|
+
transfer,
|
|
287
|
+
context: factory.choiceContextData,
|
|
288
|
+
}),
|
|
289
|
+
],
|
|
290
|
+
disclosedContracts: factory.disclosedContracts,
|
|
291
|
+
});
|
|
292
|
+
return { kind, ...result };
|
|
293
|
+
}),
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
async function answerTransferOffer(transferOfferCid, action) {
|
|
297
|
+
const [contracts, operator] = await Promise.all([
|
|
298
|
+
read(templates_1.DA_REGISTRY_TEMPLATES.transferOffer),
|
|
299
|
+
registry.operatorParty(),
|
|
300
|
+
]);
|
|
301
|
+
const { incoming, outgoing } = transferOffers(contracts, operator);
|
|
302
|
+
const offer = find(action === 'withdraw' ? outgoing : incoming, transferOfferCid, action === 'withdraw' ? 'Outgoing transfer offer' : 'Incoming transfer offer');
|
|
303
|
+
const context = await registry.transferInstructionContext(offer.instrumentId.admin, transferOfferCid, action);
|
|
304
|
+
return submit({
|
|
305
|
+
commands: [(0, commands_1.transferInstructionCommand)(transferOfferCid, action, context.choiceContextData)],
|
|
159
306
|
disclosedContracts: context.disclosedContracts,
|
|
160
307
|
});
|
|
161
308
|
}
|
|
162
|
-
async function
|
|
163
|
-
const
|
|
309
|
+
async function ownMintOffer(mintOfferCid) {
|
|
310
|
+
const [contracts, operator] = await Promise.all([read(templates_1.DA_REGISTRY_TEMPLATES.mintOffer), registry.operatorParty()]);
|
|
311
|
+
return find(ownMintOffers(contracts, operator), mintOfferCid, 'Mint offer');
|
|
312
|
+
}
|
|
313
|
+
async function runRequestBurn(burn) {
|
|
314
|
+
const instrumentId = requireInstrumentId(burn.instrumentId);
|
|
164
315
|
const at = now();
|
|
165
316
|
const reference = burn.reference ?? newBurnReference(new Date(at));
|
|
166
317
|
if (typeof reference !== 'string' || !exports.BURN_REFERENCE_PATTERN.test(reference)) {
|
|
167
318
|
throw new errors_1.WalletError('invalid-input', 'reference must be 1-100 characters of letters, digits and . _ : - (starting with a letter or digit), with no personal data');
|
|
168
319
|
}
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
const requestedAt = (0, cip56_core_1.isoTime)(at - cip56_core_1.REQUESTED_AT_SKEW_MS, 'requestedAt');
|
|
174
|
-
const executeBefore = (0, cip56_core_1.isoTime)(deadline, 'executeBefore');
|
|
175
|
-
const wallet = await connect();
|
|
176
|
-
const selection = (0, holdings_1.selectHoldings)(await core.holdingsOf(wallet), {
|
|
177
|
-
owner: wallet.partyId,
|
|
320
|
+
const executeBefore = deadline(burn.executeBefore, at, BURN_TTL_MS);
|
|
321
|
+
const selection = (0, holdings_1.selectHoldings)(holdingsIn(await read(templates_1.DA_REGISTRY_TEMPLATES.holding)), {
|
|
322
|
+
owner: partyId,
|
|
178
323
|
instrumentId,
|
|
179
324
|
amount: burn.amount,
|
|
180
325
|
});
|
|
181
|
-
const factory = await
|
|
182
|
-
holder:
|
|
326
|
+
const factory = await registry.burnRequestContext({
|
|
327
|
+
holder: partyId,
|
|
183
328
|
holdingContractIds: selection.inputHoldingCids,
|
|
184
329
|
instrumentId,
|
|
185
330
|
});
|
|
186
|
-
const factoryError = (0,
|
|
331
|
+
const factoryError = (0, client_1.factoryTemplateError)(factory, client_1.DA_BURN_FACTORY_TEMPLATES);
|
|
187
332
|
if (factoryError !== null)
|
|
188
333
|
throw new errors_1.RegistryResponseError(factoryError);
|
|
189
|
-
const submitted = wallet.submit({
|
|
190
|
-
commands: [
|
|
191
|
-
(0, commands_1.requestBurnCommand)({
|
|
192
|
-
allocationFactoryCid: factory.factoryId,
|
|
193
|
-
expectedAdmin: instrumentId.admin,
|
|
194
|
-
burn: {
|
|
195
|
-
instrumentId,
|
|
196
|
-
amount: selection.amount,
|
|
197
|
-
holder: wallet.partyId,
|
|
198
|
-
reference,
|
|
199
|
-
requestedAt,
|
|
200
|
-
executeBefore,
|
|
201
|
-
},
|
|
202
|
-
holdingCids: selection.inputHoldingCids,
|
|
203
|
-
context: factory.choiceContextData,
|
|
204
|
-
}),
|
|
205
|
-
],
|
|
206
|
-
disclosedContracts: factory.disclosedContracts,
|
|
207
|
-
});
|
|
208
|
-
let result;
|
|
209
334
|
try {
|
|
210
|
-
result = await
|
|
335
|
+
const result = await submit({
|
|
336
|
+
commands: [
|
|
337
|
+
(0, commands_1.requestBurnCommand)({
|
|
338
|
+
allocationFactoryCid: factory.factoryId,
|
|
339
|
+
expectedAdmin: instrumentId.admin,
|
|
340
|
+
burn: {
|
|
341
|
+
instrumentId,
|
|
342
|
+
amount: selection.amount,
|
|
343
|
+
holder: partyId,
|
|
344
|
+
reference,
|
|
345
|
+
requestedAt: isoTime(at - REQUESTED_AT_SKEW_MS, 'requestedAt'),
|
|
346
|
+
executeBefore,
|
|
347
|
+
},
|
|
348
|
+
holdingCids: selection.inputHoldingCids,
|
|
349
|
+
context: factory.choiceContextData,
|
|
350
|
+
}),
|
|
351
|
+
],
|
|
352
|
+
disclosedContracts: factory.disclosedContracts,
|
|
353
|
+
});
|
|
354
|
+
return { ...result, reference };
|
|
211
355
|
}
|
|
212
356
|
catch (error) {
|
|
213
357
|
// The reference may have been generated here: the caller needs it to look the burn request up before a retry,
|
|
214
358
|
// above all after an unknown outcome, when the request may have been filed.
|
|
215
|
-
if (error instanceof errors_1.WalletSubmitError)
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
burnReference: reference,
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
throw error;
|
|
359
|
+
if (!(error instanceof errors_1.WalletSubmitError))
|
|
360
|
+
throw error;
|
|
361
|
+
throw new errors_1.WalletSubmitError(error.outcome, `${error.message} Burn reference: ${reference}.`, {
|
|
362
|
+
...(error.code !== undefined && { code: error.code }),
|
|
363
|
+
...(error.commandId !== undefined && { commandId: error.commandId }),
|
|
364
|
+
burnReference: reference,
|
|
365
|
+
});
|
|
226
366
|
}
|
|
227
|
-
return { ...result, reference };
|
|
228
367
|
}
|
|
229
|
-
async function runCreateTransferPreapproval(
|
|
230
|
-
const instrumentAdmin =
|
|
368
|
+
async function runCreateTransferPreapproval(preapproval) {
|
|
369
|
+
const instrumentAdmin = requireNonEmpty(preapproval.instrumentAdmin, 'instrumentAdmin');
|
|
231
370
|
const instrumentIds = [...new Set(preapproval.instrumentIds ?? [])];
|
|
232
371
|
if (instrumentIds.length > commands_1.MAX_PREAPPROVAL_INSTRUMENTS) {
|
|
233
372
|
throw new errors_1.WalletError('invalid-input', `A preapproval allows at most ${commands_1.MAX_PREAPPROVAL_INSTRUMENTS} instrument ids`);
|
|
234
373
|
}
|
|
235
|
-
instrumentIds.forEach((id) =>
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
374
|
+
instrumentIds.forEach((id) => requireNonEmpty(id, 'instrumentIds[]'));
|
|
375
|
+
const [contracts, operator] = await Promise.all([
|
|
376
|
+
read(templates_1.DA_REGISTRY_TEMPLATES.transferPreapproval),
|
|
377
|
+
registry.operatorParty(),
|
|
378
|
+
]);
|
|
379
|
+
const covering = ownPreapprovals(contracts, operator).find((existing) => existing.instrumentAdmin === instrumentAdmin &&
|
|
380
|
+
(existing.instrumentIds.length === 0 ||
|
|
381
|
+
(instrumentIds.length > 0 && instrumentIds.every((id) => existing.instrumentIds.includes(id)))));
|
|
382
|
+
if (covering !== undefined) {
|
|
383
|
+
throw new errors_1.WalletError('already-exists', `Transfer preapproval ${covering.contractId} already covers ${instrumentIds.length === 0 ? 'every instrument' : instrumentIds.join(', ')} of ${instrumentAdmin}; nothing was submitted`);
|
|
384
|
+
}
|
|
385
|
+
return submit({
|
|
386
|
+
commands: [(0, commands_1.createDaTransferPreapprovalCommand)({ operator, receiver: partyId, instrumentAdmin, instrumentIds })],
|
|
246
387
|
});
|
|
247
388
|
}
|
|
248
|
-
|
|
389
|
+
async function runRemoveTransferPreapproval(preapprovalCid) {
|
|
390
|
+
const [contracts, operator] = await Promise.all([
|
|
391
|
+
read(templates_1.DA_REGISTRY_TEMPLATES.transferPreapproval),
|
|
392
|
+
registry.operatorParty(),
|
|
393
|
+
]);
|
|
394
|
+
find(ownPreapprovals(contracts, operator), preapprovalCid, 'Transfer preapproval');
|
|
395
|
+
return submit({ commands: [(0, commands_1.withdrawTransferPreapprovalCommand)(preapprovalCid, partyId)] });
|
|
396
|
+
}
|
|
249
397
|
return {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
},
|
|
265
|
-
|
|
398
|
+
partyId,
|
|
399
|
+
network,
|
|
400
|
+
loadState,
|
|
401
|
+
planTransfer,
|
|
402
|
+
acceptTransferOffer: async (cid) => exclusive(async () => answerTransferOffer(cid, 'accept')),
|
|
403
|
+
rejectTransferOffer: async (cid) => exclusive(async () => answerTransferOffer(cid, 'reject')),
|
|
404
|
+
withdrawTransferOffer: async (cid) => exclusive(async () => answerTransferOffer(cid, 'withdraw')),
|
|
405
|
+
acceptMintOffer: async (cid) => exclusive(async () => {
|
|
406
|
+
await ownMintOffer(cid);
|
|
407
|
+
const context = await registry.mintOfferAcceptContext(cid);
|
|
408
|
+
return submit({
|
|
409
|
+
commands: [(0, commands_1.acceptMintOfferCommand)(cid, context.choiceContextData)],
|
|
410
|
+
disclosedContracts: context.disclosedContracts,
|
|
411
|
+
});
|
|
412
|
+
}),
|
|
413
|
+
rejectMintOffer: async (cid) => exclusive(async () => {
|
|
414
|
+
await ownMintOffer(cid);
|
|
415
|
+
return submit({ commands: [(0, commands_1.rejectMintOfferCommand)(cid, exports.MINT_OFFER_REJECT_REASON)] });
|
|
416
|
+
}),
|
|
417
|
+
requestBurn: async (burn) => exclusive(async () => runRequestBurn(burn)),
|
|
418
|
+
cancelBurnRequest: async (cid) => exclusive(async () => {
|
|
419
|
+
const [contracts, operator] = await Promise.all([
|
|
420
|
+
read(templates_1.DA_REGISTRY_TEMPLATES.burnRequest),
|
|
421
|
+
registry.operatorParty(),
|
|
422
|
+
]);
|
|
423
|
+
find(ownBurnRequests(contracts, operator), cid, 'Burn request');
|
|
424
|
+
return submit({ commands: [(0, commands_1.cancelBurnRequestCommand)(cid)] });
|
|
425
|
+
}),
|
|
426
|
+
createTransferPreapproval: async (preapproval) => exclusive(async () => runCreateTransferPreapproval(preapproval)),
|
|
427
|
+
removeTransferPreapproval: async (cid) => exclusive(async () => runRemoveTransferPreapproval(cid)),
|
|
266
428
|
};
|
|
267
429
|
}
|
|
268
430
|
//# sourceMappingURL=wallet.js.map
|