@xian-tech/wallet-core 0.4.2 → 0.4.4
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/LICENSE +21 -0
- package/README.md +18 -0
- package/dist/approval-execution.d.ts +5 -0
- package/dist/approval-execution.d.ts.map +1 -0
- package/dist/approval-execution.js +17 -0
- package/dist/approval-execution.js.map +1 -0
- package/dist/approvals.d.ts +26 -2
- package/dist/approvals.d.ts.map +1 -1
- package/dist/approvals.js +358 -280
- package/dist/approvals.js.map +1 -1
- package/dist/controller.d.ts +40 -3
- package/dist/controller.d.ts.map +1 -1
- package/dist/controller.js +426 -169
- package/dist/controller.js.map +1 -1
- package/dist/crypto.d.ts +7 -1
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +39 -13
- package/dist/crypto.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/network.d.ts.map +1 -1
- package/dist/network.js +3 -0
- package/dist/network.js.map +1 -1
- package/dist/signing-context.d.ts +5 -0
- package/dist/signing-context.d.ts.map +1 -0
- package/dist/signing-context.js +22 -0
- package/dist/signing-context.js.map +1 -0
- package/dist/simulation.d.ts +10 -0
- package/dist/simulation.d.ts.map +1 -0
- package/dist/simulation.js +25 -0
- package/dist/simulation.js.map +1 -0
- package/dist/text-safety.d.ts +4 -0
- package/dist/text-safety.d.ts.map +1 -0
- package/dist/text-safety.js +15 -0
- package/dist/text-safety.js.map +1 -0
- package/dist/types.d.ts +22 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/wallet-encryption.d.ts +7 -0
- package/dist/wallet-encryption.d.ts.map +1 -0
- package/dist/wallet-encryption.js +23 -0
- package/dist/wallet-encryption.js.map +1 -0
- package/package.json +7 -5
package/dist/approvals.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { encodeRuntime } from "@xian-tech/client";
|
|
2
|
+
import { HIDDEN_CHARACTERS_WARNING, revealHiddenCharacters } from "./text-safety.js";
|
|
1
3
|
function prettyJson(value) {
|
|
2
|
-
return JSON.stringify(value, null, 2);
|
|
4
|
+
return JSON.stringify(JSON.parse(encodeRuntime(value)), null, 2);
|
|
3
5
|
}
|
|
4
6
|
function payloadForRequest(request) {
|
|
5
7
|
if (Array.isArray(request.params)) {
|
|
@@ -35,11 +37,27 @@ function numericValue(value) {
|
|
|
35
37
|
}
|
|
36
38
|
return null;
|
|
37
39
|
}
|
|
40
|
+
function exactIntegerValue(value) {
|
|
41
|
+
if (typeof value === "bigint") {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
if (typeof value === "string" && /^-?\d+$/.test(value.trim())) {
|
|
45
|
+
return BigInt(value.trim());
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
38
49
|
function formatNumericValue(value) {
|
|
39
50
|
return value.toLocaleString(undefined, {
|
|
40
51
|
maximumFractionDigits: 8
|
|
41
52
|
});
|
|
42
53
|
}
|
|
54
|
+
function formatChiAmount(value, chi) {
|
|
55
|
+
const exactChi = exactIntegerValue(value);
|
|
56
|
+
if (exactChi != null) {
|
|
57
|
+
return exactChi.toLocaleString();
|
|
58
|
+
}
|
|
59
|
+
return chi == null ? stringifyValue(value) : formatNumericValue(chi);
|
|
60
|
+
}
|
|
43
61
|
export function formatChiWithXianCost(value, chiRate) {
|
|
44
62
|
if (value == null ||
|
|
45
63
|
value === "" ||
|
|
@@ -47,18 +65,47 @@ export function formatChiWithXianCost(value, chiRate) {
|
|
|
47
65
|
return null;
|
|
48
66
|
}
|
|
49
67
|
const chi = numericValue(value);
|
|
50
|
-
const formattedChi =
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
68
|
+
const formattedChi = formatChiAmount(value, chi);
|
|
69
|
+
const cost = xianCost(chi, chiRate);
|
|
70
|
+
return cost == null ? formattedChi : `${formattedChi} (~${formatNumericValue(cost)} XIAN)`;
|
|
71
|
+
}
|
|
72
|
+
function isChiCount(value) {
|
|
73
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* What a chi limit costs beyond what the call needs: a paid transaction pays
|
|
77
|
+
* the chi it uses plus half of the chi it supplies but leaves unused.
|
|
78
|
+
*/
|
|
79
|
+
export function unusedChiCost(input) {
|
|
80
|
+
const { supplied, estimated } = input;
|
|
81
|
+
if (!isChiCount(supplied) || !isChiCount(estimated)) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const unused = Math.max(supplied - estimated, 0);
|
|
85
|
+
const extraChi = Math.floor(unused / 2);
|
|
86
|
+
const severity = extraChi <= 1 ? "none" : extraChi >= estimated ? "high" : "caution";
|
|
87
|
+
return { supplied, estimated, unused, extraChi, severity };
|
|
88
|
+
}
|
|
89
|
+
/** The sentence that tells the user what the unused chi costs, or null when nothing does. */
|
|
90
|
+
export function unusedChiWarning(cost, chiRate) {
|
|
91
|
+
if (cost.severity === "none") {
|
|
92
|
+
return null;
|
|
56
93
|
}
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
94
|
+
const extra = formatChiWithXianCost(cost.extraChi, chiRate);
|
|
95
|
+
const needed = `${cost.estimated.toLocaleString()} chi`;
|
|
96
|
+
if (cost.severity === "high") {
|
|
97
|
+
const multiple = Math.round(cost.supplied / cost.estimated).toLocaleString();
|
|
98
|
+
return `This chi limit is ${multiple}x what the call needs (${needed}). Half of unused chi is charged: about ${extra} extra.`;
|
|
60
99
|
}
|
|
61
|
-
return
|
|
100
|
+
return `This chi limit leaves ${cost.unused.toLocaleString()} chi unused. Half of unused chi is charged: about ${extra} more than the call needs (${needed}).`;
|
|
101
|
+
}
|
|
102
|
+
/** The XIAN that `chi` costs at `chiRate` chi per XIAN, or null when it has no finite value. */
|
|
103
|
+
function xianCost(chi, chiRate) {
|
|
104
|
+
if (chi == null || typeof chiRate !== "number" || !Number.isFinite(chiRate) || chiRate <= 0) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const cost = chi / chiRate;
|
|
108
|
+
return Number.isFinite(cost) ? cost : null;
|
|
62
109
|
}
|
|
63
110
|
function compactDetail(label, value, options) {
|
|
64
111
|
if (value == null ||
|
|
@@ -73,6 +120,9 @@ function compactDetail(label, value, options) {
|
|
|
73
120
|
tone: options?.tone
|
|
74
121
|
};
|
|
75
122
|
}
|
|
123
|
+
function siteDetail(approval) {
|
|
124
|
+
return compactDetail("Site", approval.origin, { monospace: true });
|
|
125
|
+
}
|
|
76
126
|
function compactDetails(details) {
|
|
77
127
|
return details.filter((detail) => detail != null);
|
|
78
128
|
}
|
|
@@ -88,37 +138,21 @@ function trustedDappSuggestion(contract, fn, requestLabel) {
|
|
|
88
138
|
broadScope: "any"
|
|
89
139
|
};
|
|
90
140
|
}
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const root = payload;
|
|
96
|
-
const tx = typeof root.tx === "object" && root.tx != null
|
|
97
|
-
? root.tx
|
|
98
|
-
: root;
|
|
99
|
-
const txPayload = typeof tx.payload === "object" && tx.payload != null
|
|
100
|
-
? tx.payload
|
|
101
|
-
: tx;
|
|
102
|
-
return txPayload;
|
|
103
|
-
}
|
|
104
|
-
function intentFromRequestPayload(payload) {
|
|
105
|
-
if (typeof payload !== "object" || payload == null) {
|
|
106
|
-
return {};
|
|
107
|
-
}
|
|
108
|
-
const root = payload;
|
|
109
|
-
return typeof root.intent === "object" && root.intent != null
|
|
110
|
-
? root.intent
|
|
111
|
-
: root;
|
|
141
|
+
function payloadRecord(payload) {
|
|
142
|
+
return typeof payload === "object" && payload != null
|
|
143
|
+
? payload
|
|
144
|
+
: {};
|
|
112
145
|
}
|
|
113
|
-
function
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const root = payload;
|
|
118
|
-
return typeof root.options === "object" && root.options != null
|
|
119
|
-
? root.options
|
|
146
|
+
function nestedOrSelf(root, key) {
|
|
147
|
+
const nested = root[key];
|
|
148
|
+
return typeof nested === "object" && nested != null
|
|
149
|
+
? nested
|
|
120
150
|
: root;
|
|
121
151
|
}
|
|
152
|
+
/** `payload[key]` when it is an object, else the payload itself (flat request shapes). */
|
|
153
|
+
function requestPart(payload, key) {
|
|
154
|
+
return nestedOrSelf(payloadRecord(payload), key);
|
|
155
|
+
}
|
|
122
156
|
function messagePreview(message) {
|
|
123
157
|
if (message.length <= 160) {
|
|
124
158
|
return message;
|
|
@@ -145,247 +179,291 @@ export function approvalKindFromMethod(method) {
|
|
|
145
179
|
throw new Error(`unsupported approval method: ${method}`);
|
|
146
180
|
}
|
|
147
181
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
account: options?.account,
|
|
247
|
-
chainId: options?.chainId,
|
|
248
|
-
createdAt: approval.createdAt
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
case "signTransaction":
|
|
252
|
-
{
|
|
253
|
-
const txPayload = txPayloadFromRequestPayload(payload);
|
|
254
|
-
return {
|
|
255
|
-
id: approval.id,
|
|
256
|
-
origin: approval.origin,
|
|
257
|
-
kind: approval.kind,
|
|
258
|
-
title: "Sign prepared transaction",
|
|
259
|
-
description: "The site already prepared a full transaction payload. Verify the sender, target contract, function, and chi budget before signing.",
|
|
260
|
-
approveLabel: "Sign transaction",
|
|
261
|
-
details: compactDetails([
|
|
262
|
-
compactDetail("Sender", txPayload.sender, { monospace: true }),
|
|
263
|
-
compactDetail("Contract", txPayload.contract, { monospace: true }),
|
|
264
|
-
compactDetail("Function", txPayload.function),
|
|
265
|
-
compactDetail("Network", txPayload.chain_id ?? options?.chainId),
|
|
266
|
-
compactDetail("Nonce", txPayload.nonce),
|
|
267
|
-
compactDetail("Chi", formatChiWithXianCost(txPayload.chi_supplied, options?.chiRate))
|
|
268
|
-
]),
|
|
269
|
-
highlights: [
|
|
270
|
-
`${stringifyValue(txPayload.contract ?? "unknown")}.${stringifyValue(txPayload.function ?? "unknown")}`
|
|
271
|
-
],
|
|
272
|
-
warnings: [
|
|
273
|
-
"Prepared transactions may perform arbitrary contract actions. Inspect the raw payload if anything looks unfamiliar."
|
|
274
|
-
],
|
|
275
|
-
trustSuggestion: typeof txPayload.contract === "string" &&
|
|
276
|
-
typeof txPayload.function === "string"
|
|
277
|
-
? trustedDappSuggestion(txPayload.contract, txPayload.function, "prepared-signature requests")
|
|
278
|
-
: undefined,
|
|
279
|
-
payload: prettyJson(payload),
|
|
280
|
-
payloadLabel: "Raw transaction",
|
|
281
|
-
account: options?.account,
|
|
282
|
-
chainId: options?.chainId,
|
|
283
|
-
createdAt: approval.createdAt
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
case "sendTransaction":
|
|
287
|
-
{
|
|
288
|
-
const txPayload = txPayloadFromRequestPayload(payload);
|
|
289
|
-
return {
|
|
290
|
-
id: approval.id,
|
|
291
|
-
origin: approval.origin,
|
|
292
|
-
kind: approval.kind,
|
|
293
|
-
title: "Send prepared transaction",
|
|
294
|
-
description: "The site supplied a full unsigned transaction. Approving will sign and broadcast it from your wallet.",
|
|
295
|
-
approveLabel: "Send transaction",
|
|
296
|
-
details: compactDetails([
|
|
297
|
-
compactDetail("Sender", txPayload.sender, { monospace: true }),
|
|
298
|
-
compactDetail("Contract", txPayload.contract, { monospace: true }),
|
|
299
|
-
compactDetail("Function", txPayload.function),
|
|
300
|
-
compactDetail("Network", txPayload.chain_id ?? options?.chainId),
|
|
301
|
-
compactDetail("Nonce", txPayload.nonce),
|
|
302
|
-
compactDetail("Chi", formatChiWithXianCost(txPayload.chi_supplied, options?.chiRate), {
|
|
303
|
-
tone: "warning"
|
|
304
|
-
})
|
|
305
|
-
]),
|
|
306
|
-
highlights: [
|
|
307
|
-
"This request can broadcast immediately after approval."
|
|
308
|
-
],
|
|
309
|
-
warnings: [
|
|
310
|
-
"Broadcasting a transaction may move funds or execute irreversible contract logic."
|
|
311
|
-
],
|
|
312
|
-
trustSuggestion: typeof txPayload.contract === "string" &&
|
|
313
|
-
typeof txPayload.function === "string"
|
|
314
|
-
? trustedDappSuggestion(txPayload.contract, txPayload.function, "prepared-transaction broadcasts")
|
|
315
|
-
: undefined,
|
|
316
|
-
payload: prettyJson(payload),
|
|
317
|
-
payloadLabel: "Raw transaction",
|
|
318
|
-
account: options?.account,
|
|
319
|
-
chainId: options?.chainId,
|
|
320
|
-
createdAt: approval.createdAt
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
case "sendCall":
|
|
324
|
-
{
|
|
325
|
-
const intent = intentFromRequestPayload(payload);
|
|
326
|
-
const kwargs = typeof intent.kwargs === "object" && intent.kwargs != null
|
|
327
|
-
? intent.kwargs
|
|
328
|
-
: undefined;
|
|
329
|
-
return {
|
|
330
|
-
id: approval.id,
|
|
331
|
-
origin: approval.origin,
|
|
332
|
-
kind: approval.kind,
|
|
333
|
-
title: "Send contract call",
|
|
334
|
-
description: "The wallet will prepare, sign, and broadcast this intent on your behalf using the active account and latest nonce.",
|
|
335
|
-
approveLabel: "Approve call",
|
|
336
|
-
details: compactDetails([
|
|
337
|
-
compactDetail("Contract", intent.contract),
|
|
338
|
-
compactDetail("Function", intent.function),
|
|
339
|
-
compactDetail("Network", intent.chainId ?? options?.chainId),
|
|
340
|
-
compactDetail("Chi", formatChiWithXianCost(intent.chiSupplied ?? intent.chi, options?.chiRate), { tone: "warning" }),
|
|
341
|
-
compactDetail("Arguments", kwargs ? `${Object.keys(kwargs).length} field(s)` : undefined)
|
|
342
|
-
]),
|
|
343
|
-
highlights: [
|
|
344
|
-
`${stringifyValue(intent.contract ?? "unknown")}.${stringifyValue(intent.function ?? "unknown")}`
|
|
345
|
-
],
|
|
346
|
-
warnings: [
|
|
347
|
-
"The wallet will fill the sender and nonce at approval time. Confirm the contract and arguments match your intent."
|
|
348
|
-
],
|
|
349
|
-
trustSuggestion: typeof intent.contract === "string" &&
|
|
350
|
-
typeof intent.function === "string"
|
|
351
|
-
? trustedDappSuggestion(intent.contract, intent.function, "contract calls")
|
|
352
|
-
: undefined,
|
|
353
|
-
payload: prettyJson(payload),
|
|
354
|
-
payloadLabel: "Raw call intent",
|
|
355
|
-
account: options?.account,
|
|
356
|
-
chainId: options?.chainId,
|
|
357
|
-
createdAt: approval.createdAt
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
case "watchAsset":
|
|
361
|
-
{
|
|
362
|
-
const asset = assetFromRequestPayload(payload);
|
|
363
|
-
return {
|
|
364
|
-
id: approval.id,
|
|
365
|
-
origin: approval.origin,
|
|
366
|
-
kind: approval.kind,
|
|
367
|
-
title: "Add asset to wallet",
|
|
368
|
-
description: "Allow this site to add an asset to your wallet list. This does not verify the asset’s legitimacy.",
|
|
369
|
-
approveLabel: "Add asset",
|
|
370
|
-
details: compactDetails([
|
|
371
|
-
compactDetail("Name", asset.name),
|
|
372
|
-
compactDetail("Symbol", asset.symbol),
|
|
373
|
-
compactDetail("Contract", asset.contract, { monospace: true }),
|
|
374
|
-
compactDetail("Decimals", asset.decimals)
|
|
375
|
-
]),
|
|
376
|
-
highlights: [
|
|
377
|
-
"Always verify the asset contract from an independent trusted source."
|
|
378
|
-
],
|
|
379
|
-
warnings: [
|
|
380
|
-
"Token metadata can be spoofed. Matching names and symbols do not prove authenticity."
|
|
381
|
-
],
|
|
382
|
-
payload: prettyJson(payload),
|
|
383
|
-
payloadLabel: "Asset metadata",
|
|
384
|
-
account: options?.account,
|
|
385
|
-
chainId: options?.chainId,
|
|
386
|
-
createdAt: approval.createdAt
|
|
387
|
-
};
|
|
388
|
-
}
|
|
182
|
+
function connectView(approval, _payload, options) {
|
|
183
|
+
return {
|
|
184
|
+
title: "Connect wallet",
|
|
185
|
+
description: "Allow this site to view your active address and request wallet actions.",
|
|
186
|
+
approveLabel: "Connect",
|
|
187
|
+
details: compactDetails([
|
|
188
|
+
siteDetail(approval),
|
|
189
|
+
compactDetail("Account", options?.account, { monospace: true }),
|
|
190
|
+
compactDetail("Network", options?.chainId),
|
|
191
|
+
compactDetail("Access", "View your address and request approvals")
|
|
192
|
+
]),
|
|
193
|
+
highlights: [
|
|
194
|
+
"Connection does not send a transaction or move funds.",
|
|
195
|
+
"Future signing and transaction requests will still need explicit approval."
|
|
196
|
+
],
|
|
197
|
+
warnings: [
|
|
198
|
+
"Only connect sites you trust. You can revoke site access from the wallet at any time."
|
|
199
|
+
],
|
|
200
|
+
payload: prettyJson({
|
|
201
|
+
origin: approval.origin,
|
|
202
|
+
account: options?.account ?? null,
|
|
203
|
+
chainId: options?.chainId ?? null
|
|
204
|
+
}),
|
|
205
|
+
payloadLabel: "Connection request"
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function requestedChainId(payload, options) {
|
|
209
|
+
const chainId = payloadRecord(payload).chainId;
|
|
210
|
+
return options?.targetChainId ?? (typeof chainId === "string" ? chainId : undefined);
|
|
211
|
+
}
|
|
212
|
+
function switchChainView(approval, payload, options) {
|
|
213
|
+
const targetChainId = requestedChainId(payload, options);
|
|
214
|
+
return {
|
|
215
|
+
title: "Switch network",
|
|
216
|
+
description: "Allow this site to change the wallet network used by every connected site.",
|
|
217
|
+
approveLabel: "Switch network",
|
|
218
|
+
details: compactDetails([
|
|
219
|
+
siteDetail(approval),
|
|
220
|
+
compactDetail("Account", options?.account, { monospace: true }),
|
|
221
|
+
compactDetail("Current network", options?.chainId),
|
|
222
|
+
compactDetail("Requested network", targetChainId)
|
|
223
|
+
]),
|
|
224
|
+
highlights: [
|
|
225
|
+
`Switch from ${stringifyValue(options?.chainId)} to ${stringifyValue(targetChainId)}`
|
|
226
|
+
],
|
|
227
|
+
warnings: [
|
|
228
|
+
"Switching changes the signing and transaction context for all connected sites."
|
|
229
|
+
],
|
|
230
|
+
payload: prettyJson({
|
|
231
|
+
origin: approval.origin,
|
|
232
|
+
account: options?.account ?? null,
|
|
233
|
+
fromChainId: options?.chainId ?? null,
|
|
234
|
+
toChainId: targetChainId ?? null
|
|
235
|
+
}),
|
|
236
|
+
payloadLabel: "Network switch request"
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function signMessageView(approval, payload, options) {
|
|
240
|
+
const message = typeof payload === "object" && payload != null
|
|
241
|
+
? String(payload.message ?? "")
|
|
242
|
+
: String(payload ?? "");
|
|
243
|
+
return {
|
|
244
|
+
title: "Sign message",
|
|
245
|
+
description: "Review the exact message carefully. Message signing should never be used as a substitute for transaction approval.",
|
|
246
|
+
approveLabel: "Sign message",
|
|
247
|
+
details: compactDetails([
|
|
248
|
+
siteDetail(approval),
|
|
249
|
+
compactDetail("Account", options?.account, { monospace: true }),
|
|
250
|
+
compactDetail("Network", options?.chainId),
|
|
251
|
+
compactDetail("Length", `${message.length} characters`),
|
|
252
|
+
compactDetail("Preview", messagePreview(message))
|
|
253
|
+
]),
|
|
254
|
+
highlights: message.length > 0 ? [messagePreview(message)] : [],
|
|
255
|
+
warnings: [
|
|
256
|
+
"Do not sign messages you do not understand.",
|
|
257
|
+
"Never sign a message that looks like a transaction payload or seed phrase request."
|
|
258
|
+
],
|
|
259
|
+
payload: message,
|
|
260
|
+
payloadLabel: "Full message"
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
function exactMarkerText(value) {
|
|
264
|
+
const keys = Object.keys(value);
|
|
265
|
+
if (keys.length !== 1) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
const marker = value.__fixed__ ?? value.__big_int__;
|
|
269
|
+
return typeof marker === "string" ? marker : null;
|
|
270
|
+
}
|
|
271
|
+
function argumentText(value) {
|
|
272
|
+
if (value === null) {
|
|
273
|
+
return "null";
|
|
274
|
+
}
|
|
275
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
276
|
+
const marker = exactMarkerText(value);
|
|
277
|
+
if (marker != null) {
|
|
278
|
+
return marker;
|
|
279
|
+
}
|
|
389
280
|
}
|
|
281
|
+
return stringifyValue(value);
|
|
282
|
+
}
|
|
283
|
+
function argumentDetails(kwargs) {
|
|
284
|
+
if (typeof kwargs !== "object" || kwargs == null || Array.isArray(kwargs)) {
|
|
285
|
+
return [];
|
|
286
|
+
}
|
|
287
|
+
return Object.entries(kwargs).map(([name, value]) => ({
|
|
288
|
+
label: `Argument: ${name}`,
|
|
289
|
+
value: argumentText(value),
|
|
290
|
+
monospace: true
|
|
291
|
+
}));
|
|
292
|
+
}
|
|
293
|
+
function preparedTransactionDetails(approval, txPayload, options, chiTone) {
|
|
294
|
+
return compactDetails([
|
|
295
|
+
siteDetail(approval),
|
|
296
|
+
compactDetail("Sender", txPayload.sender, { monospace: true }),
|
|
297
|
+
compactDetail("Contract", txPayload.contract, { monospace: true }),
|
|
298
|
+
compactDetail("Function", txPayload.function),
|
|
299
|
+
compactDetail("Network", txPayload.chain_id ?? options?.chainId),
|
|
300
|
+
compactDetail("Nonce", txPayload.nonce),
|
|
301
|
+
compactDetail("Chi", formatChiWithXianCost(txPayload.chi_supplied, options?.chiRate), { tone: chiTone }),
|
|
302
|
+
...argumentDetails(txPayload.kwargs)
|
|
303
|
+
]);
|
|
304
|
+
}
|
|
305
|
+
function targetTrustSuggestion(target, requestLabel) {
|
|
306
|
+
return typeof target.contract === "string" &&
|
|
307
|
+
typeof target.function === "string"
|
|
308
|
+
? trustedDappSuggestion(target.contract, target.function, requestLabel)
|
|
309
|
+
: undefined;
|
|
310
|
+
}
|
|
311
|
+
function preparedTransactionView(wording) {
|
|
312
|
+
return (approval, payload, options) => {
|
|
313
|
+
const txPayload = nestedOrSelf(requestPart(payload, "tx"), "payload");
|
|
314
|
+
return {
|
|
315
|
+
title: wording.title,
|
|
316
|
+
description: wording.description,
|
|
317
|
+
approveLabel: wording.approveLabel,
|
|
318
|
+
details: preparedTransactionDetails(approval, txPayload, options, wording.chiTone),
|
|
319
|
+
highlights: wording.highlights(txPayload),
|
|
320
|
+
warnings: [wording.warning],
|
|
321
|
+
trustSuggestion: targetTrustSuggestion(txPayload, wording.trustLabel),
|
|
322
|
+
payload: prettyJson(payload),
|
|
323
|
+
payloadLabel: "Raw transaction"
|
|
324
|
+
};
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
const signTransactionView = preparedTransactionView({
|
|
328
|
+
title: "Sign prepared transaction",
|
|
329
|
+
description: "The site already prepared a full transaction payload. Verify the sender, target contract, function, and chi budget before signing.",
|
|
330
|
+
approveLabel: "Sign transaction",
|
|
331
|
+
chiTone: undefined,
|
|
332
|
+
highlights: (txPayload) => [targetLabel(txPayload)],
|
|
333
|
+
warning: "Prepared transactions may perform arbitrary contract actions. Inspect the raw payload if anything looks unfamiliar.",
|
|
334
|
+
trustLabel: "prepared-signature requests"
|
|
335
|
+
});
|
|
336
|
+
const sendTransactionView = preparedTransactionView({
|
|
337
|
+
title: "Send prepared transaction",
|
|
338
|
+
description: "The site supplied a full unsigned transaction. Approving will sign and broadcast it from your wallet.",
|
|
339
|
+
approveLabel: "Send transaction",
|
|
340
|
+
chiTone: "warning",
|
|
341
|
+
highlights: () => ["This request can broadcast immediately after approval."],
|
|
342
|
+
warning: "Broadcasting a transaction may move funds or execute irreversible contract logic.",
|
|
343
|
+
trustLabel: "prepared-transaction broadcasts"
|
|
344
|
+
});
|
|
345
|
+
const SEND_CALL_INTENT_WARNING = "The wallet will fill the sender and nonce at approval time. Confirm the contract and arguments match your intent.";
|
|
346
|
+
const UNKNOWN_CHI_WARNING = "The wallet could not estimate chi for this call. The chi limit will be chosen when it is sent.";
|
|
347
|
+
function intentKwargs(intent) {
|
|
348
|
+
return typeof intent.kwargs === "object" && intent.kwargs != null
|
|
349
|
+
? intent.kwargs
|
|
350
|
+
: undefined;
|
|
351
|
+
}
|
|
352
|
+
function targetLabel(target) {
|
|
353
|
+
return `${stringifyValue(target.contract ?? "unknown")}.${stringifyValue(target.function ?? "unknown")}`;
|
|
354
|
+
}
|
|
355
|
+
function sendCallChiDetail(chi, options, unusedCost) {
|
|
356
|
+
return chi == null || chi === ""
|
|
357
|
+
? compactDetail("Chi", "Not estimated", { tone: "danger" })
|
|
358
|
+
: compactDetail("Chi", formatChiWithXianCost(chi, options?.chiRate), {
|
|
359
|
+
tone: unusedCost?.severity === "high" ? "danger" : "warning"
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/** What the site's chi limit costs beyond the simulated need, when both are known. */
|
|
363
|
+
function siteChiCost(chi, options) {
|
|
364
|
+
return typeof chi === "number" && typeof options?.estimatedChi === "number"
|
|
365
|
+
? unusedChiCost({ supplied: chi, estimated: options.estimatedChi })
|
|
366
|
+
: null;
|
|
367
|
+
}
|
|
368
|
+
function sendCallView(approval, payload, options) {
|
|
369
|
+
const intent = requestPart(payload, "intent");
|
|
370
|
+
const kwargs = intentKwargs(intent);
|
|
371
|
+
const chi = intent.chiSupplied ?? intent.chi;
|
|
372
|
+
const unusedCost = siteChiCost(chi, options);
|
|
373
|
+
const chiDetail = sendCallChiDetail(chi, options, unusedCost);
|
|
374
|
+
const chiKnown = chi != null && chi !== "";
|
|
375
|
+
const unusedWarning = unusedCost ? unusedChiWarning(unusedCost, options?.chiRate) : null;
|
|
376
|
+
return {
|
|
377
|
+
title: "Send contract call",
|
|
378
|
+
description: "The wallet will prepare, sign, and broadcast this intent on your behalf using the active account and latest nonce.",
|
|
379
|
+
approveLabel: "Approve call",
|
|
380
|
+
details: compactDetails([
|
|
381
|
+
siteDetail(approval),
|
|
382
|
+
compactDetail("Account", options?.account, { monospace: true }),
|
|
383
|
+
compactDetail("Contract", intent.contract),
|
|
384
|
+
compactDetail("Function", intent.function),
|
|
385
|
+
compactDetail("Network", intent.chainId ?? options?.chainId),
|
|
386
|
+
chiDetail,
|
|
387
|
+
compactDetail("Arguments", kwargs ? `${Object.keys(kwargs).length} field(s)` : undefined),
|
|
388
|
+
...argumentDetails(kwargs)
|
|
389
|
+
]),
|
|
390
|
+
highlights: [targetLabel(intent)],
|
|
391
|
+
warnings: [
|
|
392
|
+
SEND_CALL_INTENT_WARNING,
|
|
393
|
+
...(chiKnown ? [] : [UNKNOWN_CHI_WARNING]),
|
|
394
|
+
...(unusedWarning ? [unusedWarning] : [])
|
|
395
|
+
],
|
|
396
|
+
// An auto-approval rule needs a chi cap, so none is offered without one.
|
|
397
|
+
trustSuggestion: chiKnown
|
|
398
|
+
? targetTrustSuggestion(intent, "contract calls")
|
|
399
|
+
: undefined,
|
|
400
|
+
payload: prettyJson(payload),
|
|
401
|
+
payloadLabel: "Raw call intent"
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
function watchAssetView(approval, payload, _options) {
|
|
405
|
+
const asset = requestPart(payload, "options");
|
|
406
|
+
return {
|
|
407
|
+
title: "Add asset to wallet",
|
|
408
|
+
description: "Allow this site to add an asset to your wallet list. This does not verify the asset’s legitimacy.",
|
|
409
|
+
approveLabel: "Add asset",
|
|
410
|
+
details: compactDetails([
|
|
411
|
+
siteDetail(approval),
|
|
412
|
+
compactDetail("Name", asset.name),
|
|
413
|
+
compactDetail("Symbol", asset.symbol),
|
|
414
|
+
compactDetail("Contract", asset.contract, { monospace: true }),
|
|
415
|
+
compactDetail("Decimals", asset.decimals)
|
|
416
|
+
]),
|
|
417
|
+
highlights: [
|
|
418
|
+
"Always verify the asset contract from an independent trusted source."
|
|
419
|
+
],
|
|
420
|
+
warnings: [
|
|
421
|
+
"Token metadata can be spoofed. Matching names and symbols do not prove authenticity."
|
|
422
|
+
],
|
|
423
|
+
payload: prettyJson(payload),
|
|
424
|
+
payloadLabel: "Asset metadata"
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
const APPROVAL_VIEW_BUILDERS = {
|
|
428
|
+
connect: connectView,
|
|
429
|
+
switchChain: switchChainView,
|
|
430
|
+
signMessage: signMessageView,
|
|
431
|
+
signTransaction: signTransactionView,
|
|
432
|
+
sendTransaction: sendTransactionView,
|
|
433
|
+
sendCall: sendCallView,
|
|
434
|
+
watchAsset: watchAssetView
|
|
435
|
+
};
|
|
436
|
+
function revealHiddenText(body) {
|
|
437
|
+
const details = body.details.map((detail) => ({
|
|
438
|
+
...detail,
|
|
439
|
+
label: revealHiddenCharacters(detail.label),
|
|
440
|
+
value: revealHiddenCharacters(detail.value)
|
|
441
|
+
}));
|
|
442
|
+
const highlights = body.highlights.map(revealHiddenCharacters);
|
|
443
|
+
const payload = revealHiddenCharacters(body.payload);
|
|
444
|
+
const revealed = payload !== body.payload ||
|
|
445
|
+
JSON.stringify([details, highlights]) !==
|
|
446
|
+
JSON.stringify([body.details, body.highlights]);
|
|
447
|
+
return {
|
|
448
|
+
...body,
|
|
449
|
+
details,
|
|
450
|
+
highlights,
|
|
451
|
+
payload,
|
|
452
|
+
warnings: revealed
|
|
453
|
+
? [HIDDEN_CHARACTERS_WARNING, ...(body.warnings ?? [])]
|
|
454
|
+
: body.warnings
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
export function buildApprovalView(approval, options) {
|
|
458
|
+
const body = APPROVAL_VIEW_BUILDERS[approval.kind](approval, payloadForRequest(approval.request), options);
|
|
459
|
+
return {
|
|
460
|
+
id: approval.id,
|
|
461
|
+
origin: approval.origin,
|
|
462
|
+
kind: approval.kind,
|
|
463
|
+
...revealHiddenText(body),
|
|
464
|
+
account: options?.account,
|
|
465
|
+
chainId: options?.chainId,
|
|
466
|
+
createdAt: approval.createdAt
|
|
467
|
+
};
|
|
390
468
|
}
|
|
391
469
|
//# sourceMappingURL=approvals.js.map
|