@xian-tech/wallet-core 0.3.5 → 0.3.8-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/approvals.d.ts +2 -0
- package/dist/approvals.d.ts.map +1 -1
- package/dist/approvals.js +43 -4
- package/dist/approvals.js.map +1 -1
- package/dist/controller.d.ts +17 -1
- package/dist/controller.d.ts.map +1 -1
- package/dist/controller.js +505 -44
- package/dist/controller.js.map +1 -1
- package/dist/crypto.d.ts +3 -1
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +78 -15
- package/dist/crypto.js.map +1 -1
- package/dist/types.d.ts +51 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/controller.js
CHANGED
|
@@ -2,11 +2,20 @@ import { Ed25519Signer, shieldedSyncHintFromViewingPrivateKey, XianClient } from
|
|
|
2
2
|
import { createXianDappPolicyForRequest, findMatchingXianDappPolicy, ProviderChainMismatchError, ProviderUnauthorizedError, ProviderUnsupportedMethodError } from "@xian-tech/provider";
|
|
3
3
|
import { approvalKindFromMethod, buildApprovalView } from "./approvals.js";
|
|
4
4
|
import { DEFAULT_NETWORK_PRESETS, DEFAULT_DASHBOARD_URL, DEFAULT_RPC_URL, LOCAL_NETWORK_PRESET_NAME, DEFAULT_WALLET_CAPABILITIES, LOCAL_NETWORK_PRESET_ID, UNLOCKED_SESSION_TIMEOUT_MS } from "./constants.js";
|
|
5
|
-
import { createWalletSessionKey, decryptSecretTextWithSessionKey, createWalletSecret, decryptMnemonicWithSessionKey, decryptPrivateKeyWithSessionKey, deriveWalletSessionKey, derivePrivateKeyFromMnemonic, encryptSecretTextWithSessionKey, encryptMnemonicWithSessionKey, encryptPrivateKeyWithSessionKey, isUnsafeMessageToSign } from "./crypto.js";
|
|
5
|
+
import { createWalletSessionKey, decryptSecretTextWithSessionKey, createWalletSecret, decryptMnemonicWithSessionKey, decryptPrivateKeyWithSessionKey, decryptWalletBackup, deriveWalletSessionKey, derivePrivateKeyFromMnemonic, encryptSecretTextWithSessionKey, encryptMnemonicWithSessionKey, encryptPrivateKeyWithSessionKey, encryptWalletBackupPayload, isUnsafeMessageToSign } from "./crypto.js";
|
|
6
6
|
const SAFE_CHAIN_ID_LOOKUP_TIMEOUT_MS = 2_000;
|
|
7
7
|
const TRUSTED_DAPP_POLICY_TTL_MS = 30 * 24 * 60 * 60 * 1000;
|
|
8
8
|
const MAX_DAPP_METADATA_TEXT_LENGTH = 120;
|
|
9
9
|
const MAX_DAPP_ICON_URL_LENGTH = 2048;
|
|
10
|
+
const DEX_ROUTER_CONTRACT = "con_dex";
|
|
11
|
+
const DEX_PAIRS_CONTRACT = "con_pairs";
|
|
12
|
+
const DEFAULT_DEX_FEE_BPS = 30;
|
|
13
|
+
const ZERO_DEX_FEE_BPS = 0;
|
|
14
|
+
const DEX_MAX_HOPS = 3;
|
|
15
|
+
const DEX_REQUIRED_SWAP_EXPORTS = new Set([
|
|
16
|
+
"swapExactTokensForTokens",
|
|
17
|
+
"swapExactTokensForTokensSupportingFeeOnTransferTokens"
|
|
18
|
+
]);
|
|
10
19
|
function firstParamObject(params) {
|
|
11
20
|
if (Array.isArray(params)) {
|
|
12
21
|
return (params[0] ?? {});
|
|
@@ -39,6 +48,32 @@ function trimOptionalString(value) {
|
|
|
39
48
|
const trimmed = value?.trim();
|
|
40
49
|
return trimmed ? trimmed : undefined;
|
|
41
50
|
}
|
|
51
|
+
function isLoopbackHttpUrl(value) {
|
|
52
|
+
try {
|
|
53
|
+
const url = new URL(value);
|
|
54
|
+
return (url.protocol === "http:" &&
|
|
55
|
+
(url.hostname === "localhost" ||
|
|
56
|
+
url.hostname === "::1" ||
|
|
57
|
+
/^127(?:\.\d{1,3}){3}$/.test(url.hostname)));
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function assertRpcTransportAllowed(rpcUrl, allowInsecureHttp) {
|
|
64
|
+
let parsed;
|
|
65
|
+
try {
|
|
66
|
+
parsed = new URL(rpcUrl);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
throw new TypeError("network preset rpcUrl must be a valid URL");
|
|
70
|
+
}
|
|
71
|
+
if (parsed.protocol === "http:" &&
|
|
72
|
+
!allowInsecureHttp &&
|
|
73
|
+
!isLoopbackHttpUrl(rpcUrl)) {
|
|
74
|
+
throw new Error("HTTP RPC URLs are disabled for this network. Enable HTTP data transfers only for endpoints you trust.");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
42
77
|
function trimOptionalIndexedString(value) {
|
|
43
78
|
const trimmed = value?.trim();
|
|
44
79
|
if (!trimmed) {
|
|
@@ -82,6 +117,23 @@ function messageFromUnknown(value) {
|
|
|
82
117
|
return String(value);
|
|
83
118
|
}
|
|
84
119
|
}
|
|
120
|
+
function numberFromUnknown(value, fallback = 0) {
|
|
121
|
+
if (typeof value === "number") {
|
|
122
|
+
return Number.isFinite(value) ? value : fallback;
|
|
123
|
+
}
|
|
124
|
+
if (typeof value === "bigint") {
|
|
125
|
+
return Number(value);
|
|
126
|
+
}
|
|
127
|
+
if (typeof value === "string") {
|
|
128
|
+
const trimmed = value.trim();
|
|
129
|
+
if (!trimmed) {
|
|
130
|
+
return fallback;
|
|
131
|
+
}
|
|
132
|
+
const parsed = Number(trimmed);
|
|
133
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
134
|
+
}
|
|
135
|
+
return fallback;
|
|
136
|
+
}
|
|
85
137
|
function isMissingContractResult(value) {
|
|
86
138
|
const message = messageFromUnknown(value);
|
|
87
139
|
return /ImportError\(['"]Module\s+[^'"]+\s+not found['"]\)/i.test(message) ||
|
|
@@ -90,6 +142,30 @@ function isMissingContractResult(value) {
|
|
|
90
142
|
function isRecord(value) {
|
|
91
143
|
return typeof value === "object" && value != null;
|
|
92
144
|
}
|
|
145
|
+
function readExportDecoratorFlag(value) {
|
|
146
|
+
if (typeof value === "string") {
|
|
147
|
+
return value === "export";
|
|
148
|
+
}
|
|
149
|
+
if (Array.isArray(value)) {
|
|
150
|
+
let sawDecorator = false;
|
|
151
|
+
for (const entry of value) {
|
|
152
|
+
const flag = readExportDecoratorFlag(entry);
|
|
153
|
+
if (flag == null) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
sawDecorator = true;
|
|
157
|
+
if (flag) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return sawDecorator ? false : null;
|
|
162
|
+
}
|
|
163
|
+
if (isRecord(value)) {
|
|
164
|
+
const name = value.name ?? value.id;
|
|
165
|
+
return typeof name === "string" ? name === "export" : null;
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
93
169
|
function normalizeAssetNetworkStates(value) {
|
|
94
170
|
if (!isRecord(value)) {
|
|
95
171
|
return {};
|
|
@@ -243,6 +319,80 @@ function normalizeConnectedDappMetadata(value, connectedOrigins) {
|
|
|
243
319
|
}
|
|
244
320
|
return metadata;
|
|
245
321
|
}
|
|
322
|
+
function readExportedFunctionNamesFromIr(contractIr) {
|
|
323
|
+
if (!contractIr) {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
let parsed;
|
|
327
|
+
try {
|
|
328
|
+
parsed = JSON.parse(contractIr);
|
|
329
|
+
}
|
|
330
|
+
catch {
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
if (!isRecord(parsed) || !Array.isArray(parsed.functions)) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
const exported = new Set();
|
|
337
|
+
let sawExportMetadata = false;
|
|
338
|
+
for (const entry of parsed.functions) {
|
|
339
|
+
if (!isRecord(entry) || typeof entry.name !== "string") {
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (entry.name.startsWith("__")) {
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
let isExported = false;
|
|
346
|
+
if (typeof entry.visibility === "string") {
|
|
347
|
+
sawExportMetadata = true;
|
|
348
|
+
isExported = entry.visibility === "export";
|
|
349
|
+
}
|
|
350
|
+
for (const decoratorValue of [entry.decorator, entry.decorators]) {
|
|
351
|
+
const decoratorFlag = readExportDecoratorFlag(decoratorValue);
|
|
352
|
+
if (decoratorFlag == null) {
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
sawExportMetadata = true;
|
|
356
|
+
isExported = isExported || decoratorFlag;
|
|
357
|
+
}
|
|
358
|
+
if (isExported) {
|
|
359
|
+
exported.add(entry.name);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return sawExportMetadata ? exported : null;
|
|
363
|
+
}
|
|
364
|
+
function readExportedFunctionNamesFromSource(source) {
|
|
365
|
+
if (!source) {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
const exported = new Set();
|
|
369
|
+
let pendingExport = false;
|
|
370
|
+
for (const line of source.split(/\r?\n/)) {
|
|
371
|
+
const trimmed = line.trim();
|
|
372
|
+
if (!trimmed || trimmed.startsWith("#")) {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (/^@export(?:\s*(?:\(|$))/.test(trimmed)) {
|
|
376
|
+
pendingExport = true;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (trimmed.startsWith("@")) {
|
|
380
|
+
pendingExport = false;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
const match = /^(?:async\s+)?def\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(/.exec(trimmed);
|
|
384
|
+
if (match) {
|
|
385
|
+
const name = match[1];
|
|
386
|
+
if (pendingExport && !name.startsWith("__")) {
|
|
387
|
+
exported.add(name);
|
|
388
|
+
}
|
|
389
|
+
pendingExport = false;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
pendingExport = false;
|
|
393
|
+
}
|
|
394
|
+
return exported;
|
|
395
|
+
}
|
|
246
396
|
function normalizeTrackedAsset(asset) {
|
|
247
397
|
return {
|
|
248
398
|
contract: asset.contract.trim(),
|
|
@@ -268,13 +418,16 @@ function createLocalNetworkPreset() {
|
|
|
268
418
|
};
|
|
269
419
|
}
|
|
270
420
|
function normalizePresetInputValue(preset, fallback) {
|
|
421
|
+
const rpcUrl = trimOptionalString(preset.rpcUrl) ?? fallback.rpcUrl;
|
|
422
|
+
const allowInsecureHttp = preset.allowInsecureHttp === true || fallback.allowInsecureHttp === true;
|
|
271
423
|
return {
|
|
272
424
|
id: trimOptionalString(preset.id) ?? fallback.id,
|
|
273
425
|
name: trimOptionalString(preset.name) ?? fallback.name,
|
|
274
426
|
chainId: trimOptionalString(preset.chainId),
|
|
275
|
-
rpcUrl
|
|
427
|
+
rpcUrl,
|
|
276
428
|
dashboardUrl: trimOptionalString(preset.dashboardUrl) ??
|
|
277
429
|
trimOptionalString(fallback.dashboardUrl),
|
|
430
|
+
allowInsecureHttp,
|
|
278
431
|
builtin: preset.builtin ?? fallback.builtin
|
|
279
432
|
};
|
|
280
433
|
}
|
|
@@ -522,10 +675,45 @@ export class WalletController {
|
|
|
522
675
|
await this.expireUnlockedSession();
|
|
523
676
|
return false;
|
|
524
677
|
}
|
|
525
|
-
this.unlockedPrivateKey
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
678
|
+
if ((!this.unlockedPrivateKey || !this.unlockedSessionKey) && session.sessionKey) {
|
|
679
|
+
const state = await this.loadWalletState();
|
|
680
|
+
if (!state || state.publicKey !== session.publicKey) {
|
|
681
|
+
await this.clearUnlockedSession();
|
|
682
|
+
return false;
|
|
683
|
+
}
|
|
684
|
+
try {
|
|
685
|
+
const privateKey = await decryptPrivateKeyWithSessionKey(state.encryptedPrivateKey, session.sessionKey);
|
|
686
|
+
const signer = new Ed25519Signer(privateKey);
|
|
687
|
+
if (signer.address !== session.publicKey) {
|
|
688
|
+
await this.clearUnlockedSession();
|
|
689
|
+
return false;
|
|
690
|
+
}
|
|
691
|
+
this.unlockedPrivateKey = privateKey;
|
|
692
|
+
this.unlockedSigner = signer;
|
|
693
|
+
this.unlockedSessionKey = session.sessionKey;
|
|
694
|
+
if (state.encryptedMnemonic) {
|
|
695
|
+
this.unlockedMnemonic = await decryptMnemonicWithSessionKey(state.encryptedMnemonic, session.sessionKey).catch(() => null);
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
this.unlockedMnemonic = null;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
catch {
|
|
702
|
+
await this.clearUnlockedSession();
|
|
703
|
+
return false;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
if (!this.unlockedPrivateKey || !this.unlockedSessionKey) {
|
|
707
|
+
await this.clearUnlockedSession();
|
|
708
|
+
return false;
|
|
709
|
+
}
|
|
710
|
+
if (!this.unlockedSigner) {
|
|
711
|
+
this.unlockedSigner = new Ed25519Signer(this.unlockedPrivateKey);
|
|
712
|
+
}
|
|
713
|
+
if (this.unlockedSigner.address !== session.publicKey) {
|
|
714
|
+
await this.clearUnlockedSession();
|
|
715
|
+
return false;
|
|
716
|
+
}
|
|
529
717
|
this.unlockedSessionExpiresAt = session.expiresAt;
|
|
530
718
|
return true;
|
|
531
719
|
}
|
|
@@ -536,19 +724,20 @@ export class WalletController {
|
|
|
536
724
|
? expiresAt
|
|
537
725
|
: now + UNLOCKED_SESSION_TIMEOUT_MS;
|
|
538
726
|
}
|
|
539
|
-
async persistUnlockedSession(
|
|
727
|
+
async persistUnlockedSession(expiresAt) {
|
|
728
|
+
if (!this.unlockedSigner || !this.unlockedSessionKey) {
|
|
729
|
+
throw new ProviderUnauthorizedError("wallet is locked");
|
|
730
|
+
}
|
|
540
731
|
const resolvedExpiresAt = expiresAt ?? (await this.resolveUnlockedSessionExpiry());
|
|
541
732
|
const currentSession = await this.store.loadUnlockedSession();
|
|
542
|
-
const nextExpiresAt = currentSession?.
|
|
543
|
-
currentSession.sessionKey === this.unlockedSessionKey &&
|
|
733
|
+
const nextExpiresAt = currentSession?.publicKey === this.unlockedSigner.address &&
|
|
544
734
|
currentSession.expiresAt > resolvedExpiresAt
|
|
545
735
|
? currentSession.expiresAt
|
|
546
736
|
: resolvedExpiresAt;
|
|
547
737
|
const session = {
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
sessionKey: this.unlockedSessionKey
|
|
551
|
-
expiresAt: nextExpiresAt
|
|
738
|
+
publicKey: this.unlockedSigner.address,
|
|
739
|
+
expiresAt: nextExpiresAt,
|
|
740
|
+
sessionKey: this.unlockedSessionKey
|
|
552
741
|
};
|
|
553
742
|
await this.store.saveUnlockedSession(session);
|
|
554
743
|
this.unlockedSessionExpiresAt = nextExpiresAt;
|
|
@@ -577,10 +766,12 @@ export class WalletController {
|
|
|
577
766
|
if (!this.unlockedSigner) {
|
|
578
767
|
this.unlockedSigner = new Ed25519Signer(this.unlockedPrivateKey);
|
|
579
768
|
}
|
|
580
|
-
await this.persistUnlockedSession(
|
|
769
|
+
await this.persistUnlockedSession();
|
|
581
770
|
return this.unlockedSigner;
|
|
582
771
|
}
|
|
583
772
|
currentClient(state) {
|
|
773
|
+
const activePreset = this.activeNetworkPreset(state);
|
|
774
|
+
assertRpcTransportAllowed(state.rpcUrl, activePreset.allowInsecureHttp);
|
|
584
775
|
if (this.options.createClient) {
|
|
585
776
|
return this.options.createClient(state);
|
|
586
777
|
}
|
|
@@ -978,6 +1169,8 @@ export class WalletController {
|
|
|
978
1169
|
if (!rpcUrl) {
|
|
979
1170
|
throw new TypeError("network preset rpcUrl is required");
|
|
980
1171
|
}
|
|
1172
|
+
const allowInsecureHttp = input.allowInsecureHttp === true;
|
|
1173
|
+
assertRpcTransportAllowed(rpcUrl, allowInsecureHttp);
|
|
981
1174
|
return {
|
|
982
1175
|
...input,
|
|
983
1176
|
id: trimOptionalString(input.id),
|
|
@@ -985,6 +1178,7 @@ export class WalletController {
|
|
|
985
1178
|
chainId: trimOptionalString(input.chainId),
|
|
986
1179
|
rpcUrl,
|
|
987
1180
|
dashboardUrl: trimOptionalString(input.dashboardUrl),
|
|
1181
|
+
allowInsecureHttp,
|
|
988
1182
|
makeActive: input.makeActive ?? false
|
|
989
1183
|
};
|
|
990
1184
|
}
|
|
@@ -1002,12 +1196,14 @@ export class WalletController {
|
|
|
1002
1196
|
chainId: sanitized.chainId,
|
|
1003
1197
|
rpcUrl: sanitized.rpcUrl,
|
|
1004
1198
|
dashboardUrl: sanitized.dashboardUrl,
|
|
1199
|
+
allowInsecureHttp: sanitized.allowInsecureHttp,
|
|
1005
1200
|
builtin: false
|
|
1006
1201
|
}, {
|
|
1007
1202
|
id: presetId,
|
|
1008
1203
|
name: sanitized.name,
|
|
1009
1204
|
rpcUrl: sanitized.rpcUrl,
|
|
1010
1205
|
dashboardUrl: sanitized.dashboardUrl,
|
|
1206
|
+
allowInsecureHttp: sanitized.allowInsecureHttp,
|
|
1011
1207
|
builtin: false
|
|
1012
1208
|
});
|
|
1013
1209
|
const nextPresets = normalized.networkPresets.filter((preset) => preset.id !== presetId);
|
|
@@ -1207,15 +1403,65 @@ export class WalletController {
|
|
|
1207
1403
|
error: serialized
|
|
1208
1404
|
};
|
|
1209
1405
|
}
|
|
1406
|
+
async requestWithEstimatedSendCallChi(request, account) {
|
|
1407
|
+
if (request.method !== "xian_sendCall" || !account) {
|
|
1408
|
+
return request;
|
|
1409
|
+
}
|
|
1410
|
+
const firstParam = firstParamObject(request.params);
|
|
1411
|
+
const intent = isRecord(firstParam.intent) ? firstParam.intent : null;
|
|
1412
|
+
if (!intent ||
|
|
1413
|
+
intent.chi != null ||
|
|
1414
|
+
intent.chiSupplied != null ||
|
|
1415
|
+
typeof intent.contract !== "string" ||
|
|
1416
|
+
typeof intent.function !== "string") {
|
|
1417
|
+
return request;
|
|
1418
|
+
}
|
|
1419
|
+
const kwargs = isRecord(intent.kwargs) ? intent.kwargs : {};
|
|
1420
|
+
try {
|
|
1421
|
+
const state = this.requireStoredWallet(await this.loadWalletState());
|
|
1422
|
+
const estimated = await this.currentClient(state).estimateChi({
|
|
1423
|
+
sender: account,
|
|
1424
|
+
contract: intent.contract,
|
|
1425
|
+
function: intent.function,
|
|
1426
|
+
kwargs
|
|
1427
|
+
});
|
|
1428
|
+
if (!Number.isFinite(estimated.estimated) || estimated.estimated <= 0) {
|
|
1429
|
+
return request;
|
|
1430
|
+
}
|
|
1431
|
+
const nextFirstParam = {
|
|
1432
|
+
...firstParam,
|
|
1433
|
+
intent: {
|
|
1434
|
+
...intent,
|
|
1435
|
+
kwargs,
|
|
1436
|
+
chi: estimated.estimated
|
|
1437
|
+
}
|
|
1438
|
+
};
|
|
1439
|
+
return {
|
|
1440
|
+
...request,
|
|
1441
|
+
params: Array.isArray(request.params)
|
|
1442
|
+
? [nextFirstParam, ...request.params.slice(1)]
|
|
1443
|
+
: nextFirstParam
|
|
1444
|
+
};
|
|
1445
|
+
}
|
|
1446
|
+
catch {
|
|
1447
|
+
return request;
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1210
1450
|
async createApprovalRequest(requestState, account, chainId) {
|
|
1451
|
+
const request = await this.requestWithEstimatedSendCallChi(requestState.request, account);
|
|
1211
1452
|
const record = {
|
|
1212
1453
|
id: this.createId(),
|
|
1213
1454
|
origin: requestState.origin,
|
|
1214
1455
|
kind: approvalKindFromMethod(requestState.request.method),
|
|
1215
|
-
request
|
|
1456
|
+
request,
|
|
1216
1457
|
createdAt: this.now()
|
|
1217
1458
|
};
|
|
1218
|
-
const
|
|
1459
|
+
const chiRate = record.kind === "signTransaction" ||
|
|
1460
|
+
record.kind === "sendTransaction" ||
|
|
1461
|
+
record.kind === "sendCall"
|
|
1462
|
+
? await this.getChiRate()
|
|
1463
|
+
: null;
|
|
1464
|
+
const view = buildApprovalView(record, { account, chainId, chiRate });
|
|
1219
1465
|
const approval = {
|
|
1220
1466
|
id: record.id,
|
|
1221
1467
|
requestId: requestState.requestId,
|
|
@@ -1225,6 +1471,7 @@ export class WalletController {
|
|
|
1225
1471
|
await this.store.saveApprovalState(approval);
|
|
1226
1472
|
await this.store.saveRequestState({
|
|
1227
1473
|
...requestState,
|
|
1474
|
+
request,
|
|
1228
1475
|
updatedAt: this.now(),
|
|
1229
1476
|
status: "pending",
|
|
1230
1477
|
approvalId: record.id
|
|
@@ -1575,9 +1822,184 @@ export class WalletController {
|
|
|
1575
1822
|
});
|
|
1576
1823
|
return this.sendPreparedTransaction(state, tx, { mode: "commit" });
|
|
1577
1824
|
}
|
|
1825
|
+
async getDexSnapshot() {
|
|
1826
|
+
const state = this.requireStoredWallet(await this.loadWalletState());
|
|
1827
|
+
const client = this.currentClient(state);
|
|
1828
|
+
if (typeof client.getState !== "function") {
|
|
1829
|
+
return {
|
|
1830
|
+
available: false,
|
|
1831
|
+
contract: DEX_ROUTER_CONTRACT,
|
|
1832
|
+
pairsContract: DEX_PAIRS_CONTRACT,
|
|
1833
|
+
reason: "Current network client does not support DEX state reads.",
|
|
1834
|
+
tradeFeeBps: DEFAULT_DEX_FEE_BPS,
|
|
1835
|
+
maxHops: DEX_MAX_HOPS,
|
|
1836
|
+
pairs: [],
|
|
1837
|
+
tokens: []
|
|
1838
|
+
};
|
|
1839
|
+
}
|
|
1840
|
+
const methods = await this.getContractMethods(DEX_ROUTER_CONTRACT);
|
|
1841
|
+
const methodNames = new Set(methods.map((method) => method.name));
|
|
1842
|
+
const hasSwapExport = [...DEX_REQUIRED_SWAP_EXPORTS].some((name) => methodNames.has(name));
|
|
1843
|
+
if (!hasSwapExport) {
|
|
1844
|
+
return {
|
|
1845
|
+
available: false,
|
|
1846
|
+
contract: DEX_ROUTER_CONTRACT,
|
|
1847
|
+
pairsContract: DEX_PAIRS_CONTRACT,
|
|
1848
|
+
reason: `${DEX_ROUTER_CONTRACT} is not deployed on this network.`,
|
|
1849
|
+
tradeFeeBps: DEFAULT_DEX_FEE_BPS,
|
|
1850
|
+
maxHops: DEX_MAX_HOPS,
|
|
1851
|
+
pairs: [],
|
|
1852
|
+
tokens: []
|
|
1853
|
+
};
|
|
1854
|
+
}
|
|
1855
|
+
const pairs = await this.readDexPairs(client);
|
|
1856
|
+
const tokenContracts = new Set();
|
|
1857
|
+
for (const asset of state.watchedAssets) {
|
|
1858
|
+
tokenContracts.add(asset.contract);
|
|
1859
|
+
}
|
|
1860
|
+
for (const pair of pairs) {
|
|
1861
|
+
tokenContracts.add(pair.token0);
|
|
1862
|
+
tokenContracts.add(pair.token1);
|
|
1863
|
+
}
|
|
1864
|
+
const [tradeFeeBps, tokens] = await Promise.all([
|
|
1865
|
+
this.readDexTradeFeeBps(client, state.publicKey),
|
|
1866
|
+
Promise.all([...tokenContracts].map((contract) => this.readDexTokenInfo(state, client, contract)))
|
|
1867
|
+
]);
|
|
1868
|
+
return {
|
|
1869
|
+
available: true,
|
|
1870
|
+
contract: DEX_ROUTER_CONTRACT,
|
|
1871
|
+
pairsContract: DEX_PAIRS_CONTRACT,
|
|
1872
|
+
tradeFeeBps,
|
|
1873
|
+
maxHops: DEX_MAX_HOPS,
|
|
1874
|
+
pairs,
|
|
1875
|
+
tokens
|
|
1876
|
+
};
|
|
1877
|
+
}
|
|
1878
|
+
async readDexPairs(client) {
|
|
1879
|
+
if (typeof client.getState !== "function") {
|
|
1880
|
+
return [];
|
|
1881
|
+
}
|
|
1882
|
+
const countRaw = await client
|
|
1883
|
+
.getState(DEX_PAIRS_CONTRACT, "pairs_num")
|
|
1884
|
+
.catch(() => 0);
|
|
1885
|
+
const count = Math.max(0, Math.floor(numberFromUnknown(countRaw)));
|
|
1886
|
+
if (count <= 0) {
|
|
1887
|
+
return [];
|
|
1888
|
+
}
|
|
1889
|
+
const pairs = await Promise.all(Array.from({ length: count }, (_, index) => this.readDexPair(client, index + 1).catch(() => null)));
|
|
1890
|
+
return pairs.filter((pair) => pair != null);
|
|
1891
|
+
}
|
|
1892
|
+
async readDexPair(client, id) {
|
|
1893
|
+
if (typeof client.getState !== "function") {
|
|
1894
|
+
return null;
|
|
1895
|
+
}
|
|
1896
|
+
const key = String(id);
|
|
1897
|
+
const [token0, token1, reserve0, reserve1, totalSupply, blockTimestampLast, creationTime] = await Promise.all([
|
|
1898
|
+
client.getState(DEX_PAIRS_CONTRACT, "pairs", [key, "token0"]),
|
|
1899
|
+
client.getState(DEX_PAIRS_CONTRACT, "pairs", [key, "token1"]),
|
|
1900
|
+
client.getState(DEX_PAIRS_CONTRACT, "pairs", [key, "reserve0"]),
|
|
1901
|
+
client.getState(DEX_PAIRS_CONTRACT, "pairs", [key, "reserve1"]),
|
|
1902
|
+
client.getState(DEX_PAIRS_CONTRACT, "pairs", [key, "totalSupply"]),
|
|
1903
|
+
client.getState(DEX_PAIRS_CONTRACT, "pairs", [key, "blockTimestampLast"]),
|
|
1904
|
+
client.getState(DEX_PAIRS_CONTRACT, "pairs", [key, "creationTime"])
|
|
1905
|
+
]);
|
|
1906
|
+
if (typeof token0 !== "string" || typeof token1 !== "string") {
|
|
1907
|
+
return null;
|
|
1908
|
+
}
|
|
1909
|
+
return {
|
|
1910
|
+
id,
|
|
1911
|
+
token0,
|
|
1912
|
+
token1,
|
|
1913
|
+
reserve0: numberFromUnknown(reserve0),
|
|
1914
|
+
reserve1: numberFromUnknown(reserve1),
|
|
1915
|
+
totalSupply: numberFromUnknown(totalSupply),
|
|
1916
|
+
blockTimestampLast: blockTimestampLast == null ? null : String(blockTimestampLast),
|
|
1917
|
+
creationTime: creationTime == null ? null : String(creationTime)
|
|
1918
|
+
};
|
|
1919
|
+
}
|
|
1920
|
+
async readDexTradeFeeBps(client, account) {
|
|
1921
|
+
if (typeof client.call !== "function") {
|
|
1922
|
+
return DEFAULT_DEX_FEE_BPS;
|
|
1923
|
+
}
|
|
1924
|
+
try {
|
|
1925
|
+
const result = await client.call({
|
|
1926
|
+
sender: account,
|
|
1927
|
+
contract: DEX_ROUTER_CONTRACT,
|
|
1928
|
+
function: "getTradeFeeBps",
|
|
1929
|
+
kwargs: { account }
|
|
1930
|
+
});
|
|
1931
|
+
const bps = numberFromUnknown(result, DEFAULT_DEX_FEE_BPS);
|
|
1932
|
+
return bps === ZERO_DEX_FEE_BPS ? ZERO_DEX_FEE_BPS : DEFAULT_DEX_FEE_BPS;
|
|
1933
|
+
}
|
|
1934
|
+
catch {
|
|
1935
|
+
return DEFAULT_DEX_FEE_BPS;
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
async readDexTokenInfo(state, client, contract) {
|
|
1939
|
+
const [metadata, precisionRaw, balanceRaw, allowanceRaw, feeOnTransferRaw] = await Promise.all([
|
|
1940
|
+
this.resolveTokenMetadataForState(state, contract).catch(() => ({
|
|
1941
|
+
contract,
|
|
1942
|
+
name: null,
|
|
1943
|
+
symbol: null,
|
|
1944
|
+
logoUrl: null,
|
|
1945
|
+
logoSvg: null
|
|
1946
|
+
})),
|
|
1947
|
+
client.getState
|
|
1948
|
+
? client.getState(contract, "metadata", ["precision"]).catch(() => null)
|
|
1949
|
+
: Promise.resolve(null),
|
|
1950
|
+
client.getBalance(state.publicKey, { contract }).catch(() => 0),
|
|
1951
|
+
client.getState
|
|
1952
|
+
? client
|
|
1953
|
+
.getState(contract, "approvals", [
|
|
1954
|
+
state.publicKey,
|
|
1955
|
+
DEX_ROUTER_CONTRACT
|
|
1956
|
+
])
|
|
1957
|
+
.catch(() => 0)
|
|
1958
|
+
: Promise.resolve(0),
|
|
1959
|
+
client.getState
|
|
1960
|
+
? client
|
|
1961
|
+
.getState(DEX_ROUTER_CONTRACT, "fee_on_transfer_tokens", [
|
|
1962
|
+
contract
|
|
1963
|
+
])
|
|
1964
|
+
.catch(() => false)
|
|
1965
|
+
: Promise.resolve(false)
|
|
1966
|
+
]);
|
|
1967
|
+
const watched = state.watchedAssets.find((asset) => asset.contract === contract);
|
|
1968
|
+
const precision = Number.isInteger(numberFromUnknown(precisionRaw, NaN))
|
|
1969
|
+
? numberFromUnknown(precisionRaw)
|
|
1970
|
+
: typeof watched?.decimals === "number"
|
|
1971
|
+
? watched.decimals
|
|
1972
|
+
: null;
|
|
1973
|
+
return {
|
|
1974
|
+
contract,
|
|
1975
|
+
name: metadata.name,
|
|
1976
|
+
symbol: metadata.symbol,
|
|
1977
|
+
logoUrl: metadata.logoUrl,
|
|
1978
|
+
logoSvg: metadata.logoSvg,
|
|
1979
|
+
precision,
|
|
1980
|
+
balance: numberFromUnknown(balanceRaw),
|
|
1981
|
+
allowance: numberFromUnknown(allowanceRaw),
|
|
1982
|
+
feeOnTransfer: feeOnTransferRaw === true
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1578
1985
|
async getContractMethods(contract) {
|
|
1579
1986
|
const state = this.requireStoredWallet(await this.loadWalletState());
|
|
1580
|
-
|
|
1987
|
+
const client = this.currentClient(state);
|
|
1988
|
+
const methods = await client.getContractMethods(contract);
|
|
1989
|
+
let exportedNames = null;
|
|
1990
|
+
if (client.getContractIr) {
|
|
1991
|
+
const contractIr = await client.getContractIr(contract).catch(() => null);
|
|
1992
|
+
exportedNames = readExportedFunctionNamesFromIr(contractIr);
|
|
1993
|
+
}
|
|
1994
|
+
if (!exportedNames && client.getContractSource) {
|
|
1995
|
+
const contractSource = await client
|
|
1996
|
+
.getContractSource(contract)
|
|
1997
|
+
.catch(() => null);
|
|
1998
|
+
exportedNames = readExportedFunctionNamesFromSource(contractSource);
|
|
1999
|
+
}
|
|
2000
|
+
return exportedNames
|
|
2001
|
+
? methods.filter((method) => exportedNames.has(method.name))
|
|
2002
|
+
: methods;
|
|
1581
2003
|
}
|
|
1582
2004
|
async fetchAssetBalanceSnapshot(state, assets) {
|
|
1583
2005
|
const balances = {};
|
|
@@ -1675,10 +2097,12 @@ export class WalletController {
|
|
|
1675
2097
|
this.unlockedSigner = signer;
|
|
1676
2098
|
this.unlockedMnemonic = secret.mnemonic ?? null;
|
|
1677
2099
|
this.unlockedSessionKey = sessionKey;
|
|
1678
|
-
await this.persistUnlockedSession(
|
|
2100
|
+
await this.persistUnlockedSession();
|
|
1679
2101
|
await this.invalidatePendingRequests(new ProviderUnauthorizedError("wallet was replaced"));
|
|
1680
2102
|
const setupRpcUrl = trimOptionalString(input.rpcUrl) ?? DEFAULT_RPC_URL;
|
|
1681
2103
|
const setupDashboardUrl = trimOptionalString(input.dashboardUrl) ?? DEFAULT_DASHBOARD_URL;
|
|
2104
|
+
const setupAllowInsecureHttp = input.allowInsecureHttp === true;
|
|
2105
|
+
assertRpcTransportAllowed(setupRpcUrl, setupAllowInsecureHttp);
|
|
1682
2106
|
const localPreset = createLocalNetworkPreset();
|
|
1683
2107
|
const useLocalPreset = setupRpcUrl === localPreset.rpcUrl &&
|
|
1684
2108
|
(setupDashboardUrl ?? "") === (localPreset.dashboardUrl ?? "");
|
|
@@ -1691,12 +2115,14 @@ export class WalletController {
|
|
|
1691
2115
|
chainId: trimOptionalString(input.expectedChainId),
|
|
1692
2116
|
rpcUrl: setupRpcUrl,
|
|
1693
2117
|
dashboardUrl: setupDashboardUrl,
|
|
2118
|
+
allowInsecureHttp: setupAllowInsecureHttp,
|
|
1694
2119
|
builtin: false
|
|
1695
2120
|
}, {
|
|
1696
2121
|
id: customPresetId ?? "custom-network",
|
|
1697
2122
|
name: trimOptionalString(input.networkName) ?? "Custom network",
|
|
1698
2123
|
rpcUrl: setupRpcUrl,
|
|
1699
2124
|
dashboardUrl: setupDashboardUrl,
|
|
2125
|
+
allowInsecureHttp: setupAllowInsecureHttp,
|
|
1700
2126
|
builtin: false
|
|
1701
2127
|
});
|
|
1702
2128
|
const networkPresets = useLocalPreset
|
|
@@ -1766,7 +2192,7 @@ export class WalletController {
|
|
|
1766
2192
|
this.unlockedMnemonic = null;
|
|
1767
2193
|
}
|
|
1768
2194
|
}
|
|
1769
|
-
await this.persistUnlockedSession(
|
|
2195
|
+
await this.persistUnlockedSession();
|
|
1770
2196
|
void this.notifyUnlockedOrigins(state);
|
|
1771
2197
|
return this.getPopupState();
|
|
1772
2198
|
}
|
|
@@ -1781,7 +2207,11 @@ export class WalletController {
|
|
|
1781
2207
|
async exportWallet(password) {
|
|
1782
2208
|
const state = this.requireStoredWallet(await this.loadWalletState());
|
|
1783
2209
|
const accounts = this.requireAccounts(state);
|
|
1784
|
-
|
|
2210
|
+
await this.restoreUnlockedSession();
|
|
2211
|
+
const sessionKey = this.unlockedSessionKey;
|
|
2212
|
+
if (!sessionKey) {
|
|
2213
|
+
throw new ProviderUnauthorizedError("wallet is locked");
|
|
2214
|
+
}
|
|
1785
2215
|
const backup = {
|
|
1786
2216
|
version: 1,
|
|
1787
2217
|
type: state.seedSource,
|
|
@@ -1802,18 +2232,19 @@ export class WalletController {
|
|
|
1802
2232
|
if (shieldedStateSnapshots.length > 0) {
|
|
1803
2233
|
backup.shieldedStateSnapshots = shieldedStateSnapshots;
|
|
1804
2234
|
}
|
|
1805
|
-
return backup;
|
|
2235
|
+
return encryptWalletBackupPayload(backup, password);
|
|
1806
2236
|
}
|
|
1807
2237
|
async importWalletBackup(backup, password) {
|
|
2238
|
+
const decryptedBackup = await decryptWalletBackup(backup, password);
|
|
1808
2239
|
// Derive or use the provided private key
|
|
1809
2240
|
let primaryKey;
|
|
1810
2241
|
let mnemonic;
|
|
1811
|
-
if (
|
|
1812
|
-
mnemonic =
|
|
2242
|
+
if (decryptedBackup.type === "mnemonic" && decryptedBackup.mnemonic) {
|
|
2243
|
+
mnemonic = decryptedBackup.mnemonic;
|
|
1813
2244
|
primaryKey = await derivePrivateKeyFromMnemonic(mnemonic, 0);
|
|
1814
2245
|
}
|
|
1815
|
-
else if (
|
|
1816
|
-
primaryKey =
|
|
2246
|
+
else if (decryptedBackup.privateKey) {
|
|
2247
|
+
primaryKey = decryptedBackup.privateKey;
|
|
1817
2248
|
}
|
|
1818
2249
|
else {
|
|
1819
2250
|
throw new Error("backup must contain a mnemonic or private key");
|
|
@@ -1824,7 +2255,7 @@ export class WalletController {
|
|
|
1824
2255
|
? await encryptMnemonicWithSessionKey(mnemonic, sessionKey)
|
|
1825
2256
|
: undefined;
|
|
1826
2257
|
// Build accounts list
|
|
1827
|
-
const accountEntries =
|
|
2258
|
+
const accountEntries = decryptedBackup.accounts;
|
|
1828
2259
|
if (!accountEntries || accountEntries.length === 0) {
|
|
1829
2260
|
throw new Error("backup must contain at least one account");
|
|
1830
2261
|
}
|
|
@@ -1843,7 +2274,7 @@ export class WalletController {
|
|
|
1843
2274
|
name: entry.name
|
|
1844
2275
|
});
|
|
1845
2276
|
}
|
|
1846
|
-
const activeAccount = accounts.find((account) => account.index ===
|
|
2277
|
+
const activeAccount = accounts.find((account) => account.index === decryptedBackup.activeAccountIndex) ??
|
|
1847
2278
|
accounts[0];
|
|
1848
2279
|
if (!activeAccount) {
|
|
1849
2280
|
throw new Error("backup must contain at least one account");
|
|
@@ -1854,30 +2285,38 @@ export class WalletController {
|
|
|
1854
2285
|
this.unlockedSigner = signer;
|
|
1855
2286
|
this.unlockedMnemonic = mnemonic ?? null;
|
|
1856
2287
|
this.unlockedSessionKey = sessionKey;
|
|
1857
|
-
await this.persistUnlockedSession(
|
|
2288
|
+
await this.persistUnlockedSession();
|
|
1858
2289
|
await this.invalidatePendingRequests(new ProviderUnauthorizedError("wallet was replaced"));
|
|
1859
2290
|
// Merge network presets
|
|
1860
2291
|
const presets = [...DEFAULT_NETWORK_PRESETS];
|
|
1861
|
-
for (const p of
|
|
2292
|
+
for (const p of decryptedBackup.networkPresets ?? []) {
|
|
1862
2293
|
if (!presets.some((existing) => existing.id === p.id)) {
|
|
1863
|
-
|
|
2294
|
+
assertRpcTransportAllowed(p.rpcUrl, p.allowInsecureHttp);
|
|
2295
|
+
presets.push(normalizePresetInputValue(p, {
|
|
2296
|
+
id: p.id,
|
|
2297
|
+
name: p.name,
|
|
2298
|
+
rpcUrl: p.rpcUrl,
|
|
2299
|
+
dashboardUrl: p.dashboardUrl,
|
|
2300
|
+
allowInsecureHttp: p.allowInsecureHttp,
|
|
2301
|
+
builtin: p.builtin
|
|
2302
|
+
}));
|
|
1864
2303
|
}
|
|
1865
2304
|
}
|
|
1866
|
-
const activePreset = presets.find((preset) => preset.id ===
|
|
2305
|
+
const activePreset = presets.find((preset) => preset.id === decryptedBackup.activeNetworkId) ??
|
|
1867
2306
|
presets[0];
|
|
1868
2307
|
if (!activePreset) {
|
|
1869
2308
|
throw new Error("backup must contain at least one network preset");
|
|
1870
2309
|
}
|
|
1871
|
-
const watchedAssets =
|
|
1872
|
-
?
|
|
2310
|
+
const watchedAssets = decryptedBackup.watchedAssets?.length
|
|
2311
|
+
? decryptedBackup.watchedAssets
|
|
1873
2312
|
: [{ contract: "currency", name: "Xian", symbol: "XIAN" }];
|
|
1874
|
-
const shieldedWalletSnapshots = await this.importShieldedWalletSnapshots(
|
|
2313
|
+
const shieldedWalletSnapshots = await this.importShieldedWalletSnapshots(decryptedBackup.shieldedStateSnapshots, sessionKey, nowIso);
|
|
1875
2314
|
await this.persistWalletState({
|
|
1876
2315
|
publicKey: activeAccount.publicKey,
|
|
1877
2316
|
encryptedPrivateKey: activeAccount.encryptedPrivateKey,
|
|
1878
2317
|
encryptedMnemonic,
|
|
1879
2318
|
walletEncryptionSalt,
|
|
1880
|
-
seedSource:
|
|
2319
|
+
seedSource: decryptedBackup.type,
|
|
1881
2320
|
mnemonicWordCount: mnemonic ? mnemonic.split(" ").length : undefined,
|
|
1882
2321
|
accounts,
|
|
1883
2322
|
activeAccountIndex: activeAccount.index,
|
|
@@ -1886,7 +2325,7 @@ export class WalletController {
|
|
|
1886
2325
|
activeNetworkId: activePreset.id,
|
|
1887
2326
|
networkPresets: presets,
|
|
1888
2327
|
watchedAssets,
|
|
1889
|
-
assetNetworkStates:
|
|
2328
|
+
assetNetworkStates: decryptedBackup.assetNetworkStates,
|
|
1890
2329
|
shieldedWalletSnapshots,
|
|
1891
2330
|
trustedDappPolicies: [],
|
|
1892
2331
|
connectedDappMetadata: {},
|
|
@@ -1921,12 +2360,17 @@ export class WalletController {
|
|
|
1921
2360
|
return this.getPopupState();
|
|
1922
2361
|
}
|
|
1923
2362
|
async exportShieldedWalletSnapshot(snapshotId, password) {
|
|
2363
|
+
void password;
|
|
1924
2364
|
const state = this.requireStoredWallet(await this.loadWalletState());
|
|
1925
2365
|
const record = this.storedShieldedWalletSnapshots(state).find((item) => item.id === snapshotId);
|
|
1926
2366
|
if (!record) {
|
|
1927
2367
|
throw new Error("shielded wallet snapshot not found");
|
|
1928
2368
|
}
|
|
1929
|
-
|
|
2369
|
+
await this.restoreUnlockedSession();
|
|
2370
|
+
const sessionKey = this.unlockedSessionKey;
|
|
2371
|
+
if (!sessionKey) {
|
|
2372
|
+
throw new ProviderUnauthorizedError("wallet is locked");
|
|
2373
|
+
}
|
|
1930
2374
|
return {
|
|
1931
2375
|
label: record.label,
|
|
1932
2376
|
stateSnapshot: await decryptSecretTextWithSessionKey(record.encryptedStateSnapshot, sessionKey),
|
|
@@ -2006,11 +2450,12 @@ export class WalletController {
|
|
|
2006
2450
|
await this.store.saveState(state);
|
|
2007
2451
|
this.unlockedPrivateKey = privateKey;
|
|
2008
2452
|
this.unlockedSigner = signer;
|
|
2009
|
-
await this.persistUnlockedSession(
|
|
2453
|
+
await this.persistUnlockedSession();
|
|
2010
2454
|
await this.emitSelectedAccountChangedForConnectedOrigins(state);
|
|
2011
2455
|
return this.getPopupState();
|
|
2012
2456
|
}
|
|
2013
2457
|
async switchAccount(index) {
|
|
2458
|
+
const wasUnlocked = await this.restoreUnlockedSession();
|
|
2014
2459
|
const state = this.requireStoredWallet(await this.loadWalletState());
|
|
2015
2460
|
const accounts = this.requireAccounts(state);
|
|
2016
2461
|
const target = accounts.find((a) => a.index === index);
|
|
@@ -2023,13 +2468,13 @@ export class WalletController {
|
|
|
2023
2468
|
state.activeAccountIndex = index;
|
|
2024
2469
|
await this.store.saveState(state);
|
|
2025
2470
|
// If unlocked, switch the in-memory signer
|
|
2026
|
-
if (this.unlockedMnemonic) {
|
|
2471
|
+
if (wasUnlocked && this.unlockedMnemonic) {
|
|
2027
2472
|
const privateKey = await derivePrivateKeyFromMnemonic(this.unlockedMnemonic, index);
|
|
2028
2473
|
this.unlockedPrivateKey = privateKey;
|
|
2029
2474
|
this.unlockedSigner = new Ed25519Signer(privateKey);
|
|
2030
|
-
await this.persistUnlockedSession(
|
|
2475
|
+
await this.persistUnlockedSession();
|
|
2031
2476
|
}
|
|
2032
|
-
else if (this.unlockedPrivateKey) {
|
|
2477
|
+
else if (wasUnlocked && this.unlockedPrivateKey) {
|
|
2033
2478
|
// No mnemonic in session — clear unlock (requires re-auth)
|
|
2034
2479
|
await this.clearUnlockedSession();
|
|
2035
2480
|
}
|
|
@@ -2053,6 +2498,7 @@ export class WalletController {
|
|
|
2053
2498
|
return this.getPopupState();
|
|
2054
2499
|
}
|
|
2055
2500
|
async removeAccount(index) {
|
|
2501
|
+
const wasUnlocked = await this.restoreUnlockedSession();
|
|
2056
2502
|
const state = this.requireStoredWallet(await this.loadWalletState());
|
|
2057
2503
|
if (index === 0) {
|
|
2058
2504
|
throw new Error("cannot remove the primary account");
|
|
@@ -2069,13 +2515,13 @@ export class WalletController {
|
|
|
2069
2515
|
state.publicKey = nextActiveAccount.publicKey;
|
|
2070
2516
|
state.encryptedPrivateKey = nextActiveAccount.encryptedPrivateKey;
|
|
2071
2517
|
state.activeAccountIndex = nextActiveAccount.index;
|
|
2072
|
-
if (this.unlockedMnemonic) {
|
|
2518
|
+
if (wasUnlocked && this.unlockedMnemonic) {
|
|
2073
2519
|
const privateKey = await derivePrivateKeyFromMnemonic(this.unlockedMnemonic, nextActiveAccount.index);
|
|
2074
2520
|
this.unlockedPrivateKey = privateKey;
|
|
2075
2521
|
this.unlockedSigner = new Ed25519Signer(privateKey);
|
|
2076
|
-
await this.persistUnlockedSession(
|
|
2522
|
+
await this.persistUnlockedSession();
|
|
2077
2523
|
}
|
|
2078
|
-
else if (this.unlockedPrivateKey) {
|
|
2524
|
+
else if (wasUnlocked && this.unlockedPrivateKey) {
|
|
2079
2525
|
await this.clearUnlockedSession();
|
|
2080
2526
|
}
|
|
2081
2527
|
}
|
|
@@ -2114,6 +2560,7 @@ export class WalletController {
|
|
|
2114
2560
|
chainId: trimOptionalString(input.expectedChainId) ?? activePreset.chainId,
|
|
2115
2561
|
rpcUrl: input.rpcUrl.trim() || DEFAULT_RPC_URL,
|
|
2116
2562
|
dashboardUrl: input.dashboardUrl?.trim() || DEFAULT_DASHBOARD_URL,
|
|
2563
|
+
allowInsecureHttp: input.allowInsecureHttp === true,
|
|
2117
2564
|
makeActive: true
|
|
2118
2565
|
});
|
|
2119
2566
|
await this.store.saveState(nextState);
|
|
@@ -2225,6 +2672,15 @@ export class WalletController {
|
|
|
2225
2672
|
async startProviderRequest(requestId, origin, request, options) {
|
|
2226
2673
|
const existing = await this.store.loadRequestState(requestId);
|
|
2227
2674
|
if (existing) {
|
|
2675
|
+
if (existing.origin !== origin) {
|
|
2676
|
+
return {
|
|
2677
|
+
status: "rejected",
|
|
2678
|
+
error: {
|
|
2679
|
+
name: "Error",
|
|
2680
|
+
message: "request id is already in use by a different origin"
|
|
2681
|
+
}
|
|
2682
|
+
};
|
|
2683
|
+
}
|
|
2228
2684
|
if (existing.status === "pending") {
|
|
2229
2685
|
return {
|
|
2230
2686
|
status: "pending",
|
|
@@ -2281,6 +2737,11 @@ export class WalletController {
|
|
|
2281
2737
|
status: "not_found"
|
|
2282
2738
|
};
|
|
2283
2739
|
}
|
|
2740
|
+
if (options?.origin && state.origin !== options.origin) {
|
|
2741
|
+
return {
|
|
2742
|
+
status: "not_found"
|
|
2743
|
+
};
|
|
2744
|
+
}
|
|
2284
2745
|
if (state.status === "pending") {
|
|
2285
2746
|
return {
|
|
2286
2747
|
status: "pending",
|