@0xsequence/provider 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/0xsequence-provider.cjs.dev.js +1742 -1678
- package/dist/0xsequence-provider.cjs.prod.js +1742 -1678
- package/dist/0xsequence-provider.esm.js +1735 -1676
- package/dist/declarations/src/client.d.ts +74 -0
- package/dist/declarations/src/index.d.ts +3 -1
- package/dist/declarations/src/init.d.ts +25 -0
- package/dist/declarations/src/provider.d.ts +127 -63
- package/dist/declarations/src/signer.d.ts +87 -0
- package/dist/declarations/src/transports/mux-transport/mux-message-provider.d.ts +21 -0
- package/dist/declarations/src/transports/wallet-request-handler.d.ts +0 -1
- package/dist/declarations/src/types.d.ts +25 -9
- package/dist/declarations/src/utils/index.d.ts +10 -7
- package/dist/declarations/src/utils.d.ts +21 -15
- package/package.json +19 -12
- package/src/client.ts +423 -0
- package/src/extended.ts +4 -0
- package/src/index.ts +3 -1
- package/src/init.ts +134 -0
- package/src/provider.ts +447 -424
- package/src/signer.ts +316 -0
- package/src/transports/base-wallet-transport.ts +3 -8
- package/src/transports/mux-transport/mux-message-provider.ts +86 -2
- package/src/transports/proxy-transport/proxy-message-provider.ts +1 -1
- package/src/transports/wallet-request-handler.ts +3 -33
- package/src/transports/window-transport/window-message-provider.ts +4 -2
- package/src/types.ts +41 -10
- package/src/utils/index.ts +16 -76
- package/src/utils.ts +81 -51
- package/dist/declarations/src/extended.d.ts +0 -6
- package/dist/declarations/src/wallet.d.ts +0 -105
- package/src/wallet.ts +0 -770
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { EventEmitter2 } from 'eventemitter2';
|
|
2
|
-
import { logger, encodeMessageDigest, encodeTypedDataDigest, sanitizeNumberString, sanitizeHost, sanitizeAlphanumeric, base64EncodeObject, base64DecodeObject
|
|
3
|
-
import { ethers, BigNumber
|
|
4
|
-
import { findSupportedNetwork,
|
|
5
|
-
import { commons
|
|
2
|
+
import { logger, encodeMessageDigest, encodeTypedDataDigest, sanitizeNumberString, sanitizeHost, sanitizeAlphanumeric, base64EncodeObject, base64DecodeObject } from '@0xsequence/utils';
|
|
3
|
+
import { ethers, BigNumber } from 'ethers';
|
|
4
|
+
import { findSupportedNetwork, allNetworks, findNetworkConfig, JsonRpcRouter, loggingProviderMiddleware, exceptionProviderMiddleware, CachedProvider, JsonRpcSender } from '@0xsequence/network';
|
|
5
|
+
import { commons } from '@0xsequence/core';
|
|
6
6
|
import { signAuthorization } from '@0xsequence/auth';
|
|
7
|
-
import { Signer, resolveArrayProperties } from '@0xsequence/wallet';
|
|
8
7
|
|
|
9
8
|
function _extends() {
|
|
10
9
|
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
@@ -21,6 +20,10 @@ function _extends() {
|
|
|
21
20
|
return _extends.apply(this, arguments);
|
|
22
21
|
}
|
|
23
22
|
|
|
23
|
+
function isProviderTransport(transport) {
|
|
24
|
+
return transport && typeof transport === 'object' && typeof transport.register === 'function' && typeof transport.unregister === 'function' && typeof transport.openWallet === 'function' && typeof transport.closeWallet === 'function' && typeof transport.isOpened === 'function' && typeof transport.isConnected === 'function' && typeof transport.on === 'function';
|
|
25
|
+
}
|
|
26
|
+
|
|
24
27
|
// ProviderMessageCallback is used to respond to ProviderMessage requests. The error
|
|
25
28
|
// argument is for exceptions during the execution, and response is the response payload
|
|
26
29
|
// which may contain the result or an error payload from the wallet.
|
|
@@ -72,6 +75,8 @@ const ErrSignedInRequired = new ProviderError('Wallet is not signed in. Connect
|
|
|
72
75
|
|
|
73
76
|
// TODO: lets build some nice error handling tools, prob in /utils ...
|
|
74
77
|
|
|
78
|
+
// This is required by viem, it expects a provider to have an EIP-1193 compliant `request` attribute.
|
|
79
|
+
|
|
75
80
|
const PROVIDER_OPEN_TIMEOUT = 30000; // in ms
|
|
76
81
|
|
|
77
82
|
let _messageIdx = 0;
|
|
@@ -609,59 +614,87 @@ const isUnityPlugin = () => !!navigator.userAgent.match(/UnitySequence/i);
|
|
|
609
614
|
const isWalletUpToDate = status => {
|
|
610
615
|
return status.onChain.deployed && status.fullyMigrated;
|
|
611
616
|
};
|
|
617
|
+
class MemoryItemStore {
|
|
618
|
+
constructor() {
|
|
619
|
+
this.callbacks = [];
|
|
620
|
+
this.store = {};
|
|
621
|
+
}
|
|
622
|
+
getItem(key) {
|
|
623
|
+
return this.store[key] || null;
|
|
624
|
+
}
|
|
625
|
+
setItem(key, value) {
|
|
626
|
+
this.store[key] = value;
|
|
627
|
+
this.callbacks.filter(c => c.key === key).forEach(c => c.cb(value));
|
|
628
|
+
}
|
|
629
|
+
removeItem(key) {
|
|
630
|
+
delete this.store[key];
|
|
631
|
+
}
|
|
632
|
+
onItemChange(key, cb) {
|
|
633
|
+
this.callbacks.push({
|
|
634
|
+
key,
|
|
635
|
+
cb
|
|
636
|
+
});
|
|
637
|
+
return () => {
|
|
638
|
+
this.callbacks = this.callbacks.filter(c => c.cb !== cb);
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
}
|
|
612
642
|
class LocalStorage {
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
removeItem: key => Promise.resolve(window.localStorage.removeItem(key))
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
} else {
|
|
624
|
-
// noop local storage if window is not defined
|
|
625
|
-
// TODO: perhaps add an in-memory local storage if we need?
|
|
626
|
-
if (!LocalStorage._instance) {
|
|
627
|
-
LocalStorage._instance = {
|
|
628
|
-
getItem: key => Promise.resolve(null),
|
|
629
|
-
setItem: (key, value) => Promise.resolve(),
|
|
630
|
-
removeItem: key => Promise.resolve()
|
|
631
|
-
};
|
|
632
|
-
}
|
|
643
|
+
static isAvailable() {
|
|
644
|
+
return typeof window === 'object' && typeof window.localStorage === 'object';
|
|
645
|
+
}
|
|
646
|
+
constructor() {
|
|
647
|
+
this.callbacks = [];
|
|
648
|
+
if (!LocalStorage.isAvailable()) {
|
|
649
|
+
throw new Error('LocalStorage is not available');
|
|
633
650
|
}
|
|
634
|
-
|
|
651
|
+
window.addEventListener('storage', e => {
|
|
652
|
+
const {
|
|
653
|
+
key
|
|
654
|
+
} = e;
|
|
655
|
+
const cb = this.callbacks.filter(c => c.key === key);
|
|
656
|
+
cb.forEach(c => c.cb(this.getItem(key)));
|
|
657
|
+
});
|
|
635
658
|
}
|
|
636
|
-
|
|
637
|
-
|
|
659
|
+
getItem(key) {
|
|
660
|
+
return window.localStorage.getItem(key);
|
|
638
661
|
}
|
|
639
|
-
|
|
662
|
+
setItem(key, value) {
|
|
663
|
+
window.localStorage.setItem(key, value);
|
|
640
664
|
|
|
641
|
-
//
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
this.def = def;
|
|
645
|
-
this.key = key;
|
|
665
|
+
// Trigger callbacks
|
|
666
|
+
// NOTICE: the event is not triggered on the same window
|
|
667
|
+
this.callbacks.filter(c => c.key === key).forEach(c => c.cb(value));
|
|
646
668
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
669
|
+
removeItem(key) {
|
|
670
|
+
window.localStorage.removeItem(key);
|
|
671
|
+
|
|
672
|
+
// Trigger callbacks
|
|
673
|
+
// NOTICE: the event is not triggered on the same window
|
|
674
|
+
this.callbacks.filter(c => c.key === key).forEach(c => c.cb(null));
|
|
675
|
+
}
|
|
676
|
+
onItemChange(key, cb) {
|
|
677
|
+
this.callbacks.push({
|
|
678
|
+
key,
|
|
679
|
+
cb
|
|
680
|
+
});
|
|
681
|
+
return () => {
|
|
682
|
+
this.callbacks = this.callbacks.filter(c => c.cb !== cb);
|
|
683
|
+
};
|
|
658
684
|
}
|
|
659
|
-
|
|
660
|
-
|
|
685
|
+
}
|
|
686
|
+
function useBestStore() {
|
|
687
|
+
if (LocalStorage.isAvailable()) {
|
|
688
|
+
return new LocalStorage();
|
|
661
689
|
}
|
|
662
|
-
|
|
663
|
-
|
|
690
|
+
return new MemoryItemStore();
|
|
691
|
+
}
|
|
692
|
+
async function resolveArrayProperties(object) {
|
|
693
|
+
if (Array.isArray(object)) {
|
|
694
|
+
// T must include array type
|
|
695
|
+
return Promise.all(object.map(o => ethers.utils.resolveProperties(o)));
|
|
664
696
|
}
|
|
697
|
+
return ethers.utils.resolveProperties(object);
|
|
665
698
|
}
|
|
666
699
|
|
|
667
700
|
const TRANSPORT_SESSION_LS_KEY = '@sequence.transportSession';
|
|
@@ -802,11 +835,6 @@ class BaseWalletTransport {
|
|
|
802
835
|
// Set connect options on the walletRequestHandler as our primary
|
|
803
836
|
// wallet controller, and fall back to networkId if necessary
|
|
804
837
|
_this.walletRequestHandler.setConnectOptions(connectOptions);
|
|
805
|
-
if (connectOptions.networkId) {
|
|
806
|
-
networkId = connectOptions.networkId;
|
|
807
|
-
} else if (networkId) {
|
|
808
|
-
connectOptions.networkId = networkId;
|
|
809
|
-
}
|
|
810
838
|
} else {
|
|
811
839
|
_this.walletRequestHandler.setConnectOptions(undefined);
|
|
812
840
|
}
|
|
@@ -876,10 +904,10 @@ class BaseWalletTransport {
|
|
|
876
904
|
return true;
|
|
877
905
|
};
|
|
878
906
|
this.saveTransportSession = session => {
|
|
879
|
-
|
|
907
|
+
useBestStore().setItem(TRANSPORT_SESSION_LS_KEY, JSON.stringify(session));
|
|
880
908
|
};
|
|
881
909
|
this.getCachedTransportSession = async function () {
|
|
882
|
-
const session =
|
|
910
|
+
const session = useBestStore().getItem(TRANSPORT_SESSION_LS_KEY);
|
|
883
911
|
try {
|
|
884
912
|
return session ? JSON.parse(session) : null;
|
|
885
913
|
} catch (err) {
|
|
@@ -1233,203 +1261,84 @@ class ProxyMessageHandler extends BaseWalletTransport {
|
|
|
1233
1261
|
}
|
|
1234
1262
|
}
|
|
1235
1263
|
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
// REVIEW/NOTE: ........ this method does not work for the chrome-extension. The issue becomes
|
|
1247
|
-
// when the browser quits or restarts, the "open" event is never triggered. Perhaps the code here is fine,
|
|
1248
|
-
// or maybe its not. What should happen is when a dapp makes a request, it will call openWallet
|
|
1249
|
-
// below, in which case one of the events will register. So perhaps this is fine.
|
|
1250
|
-
this.messageProviders.forEach(m => {
|
|
1251
|
-
m.register();
|
|
1252
|
-
m.once('open', () => {
|
|
1253
|
-
// the first one to open is the winner, and others will be unregistered
|
|
1254
|
-
if (!this.provider) {
|
|
1255
|
-
this.provider = m;
|
|
1256
|
-
|
|
1257
|
-
// unregister other providers
|
|
1258
|
-
this.messageProviders.forEach(m => {
|
|
1259
|
-
if (this.provider !== m) {
|
|
1260
|
-
m.unregister();
|
|
1261
|
-
}
|
|
1262
|
-
});
|
|
1263
|
-
}
|
|
1264
|
-
});
|
|
1265
|
-
});
|
|
1266
|
-
};
|
|
1267
|
-
this.unregister = () => {
|
|
1268
|
-
this.messageProviders.forEach(m => m.unregister());
|
|
1269
|
-
this.provider = undefined;
|
|
1270
|
-
};
|
|
1271
|
-
this.openWallet = (path, intent, networkId) => {
|
|
1272
|
-
if (this.provider) {
|
|
1273
|
-
this.provider.openWallet(path, intent, networkId);
|
|
1274
|
-
return;
|
|
1275
|
-
}
|
|
1276
|
-
this.messageProviders.forEach(m => m.openWallet(path, intent, networkId));
|
|
1277
|
-
};
|
|
1278
|
-
this.sendAsync = async function (request, callback, chainId) {
|
|
1279
|
-
if (_this.provider) {
|
|
1280
|
-
_this.provider.sendAsync(request, callback, chainId);
|
|
1281
|
-
return;
|
|
1282
|
-
}
|
|
1283
|
-
throw new Error('impossible state, wallet must be opened first');
|
|
1284
|
-
};
|
|
1285
|
-
this.sendMessageRequest = async function (message) {
|
|
1286
|
-
if (_this.provider) {
|
|
1287
|
-
return _this.provider.sendMessageRequest(message);
|
|
1288
|
-
}
|
|
1289
|
-
throw new Error('impossible state, wallet must be opened first');
|
|
1264
|
+
/**
|
|
1265
|
+
* This will redirect console logs from Sequence.js & the wallet to the Unreal console, for debugging purposes.
|
|
1266
|
+
*/
|
|
1267
|
+
function overrideLogs(side) {
|
|
1268
|
+
var _window$ue;
|
|
1269
|
+
if ((_window$ue = window.ue) != null && _window$ue.sequencewallettransport && !window.logsOverriddenForUnreal) {
|
|
1270
|
+
var _window$ue2;
|
|
1271
|
+
const t = (_window$ue2 = window.ue) == null ? void 0 : _window$ue2.sequencewallettransport;
|
|
1272
|
+
console.log = (...args) => {
|
|
1273
|
+
t.logfromjs(`${side}: ${stringify(args)}`);
|
|
1290
1274
|
};
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
return _this.provider.waitUntilOpened();
|
|
1294
|
-
}
|
|
1295
|
-
return Promise.race(_this.messageProviders.map(p => p.waitUntilOpened()));
|
|
1275
|
+
console.warn = (...args) => {
|
|
1276
|
+
t.warnfromjs(`${side}: ${stringify(args)}`);
|
|
1296
1277
|
};
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
return _this.provider.waitUntilConnected();
|
|
1300
|
-
}
|
|
1301
|
-
throw new Error('impossible state, wallet must be opened first');
|
|
1278
|
+
console.error = (...args) => {
|
|
1279
|
+
t.errorfromjs(`${side}: ${stringify(args)}`);
|
|
1302
1280
|
};
|
|
1303
|
-
|
|
1304
|
-
this.provider = undefined;
|
|
1305
|
-
}
|
|
1306
|
-
add(...messageProviders) {
|
|
1307
|
-
this.messageProviders.push(...messageProviders);
|
|
1308
|
-
}
|
|
1309
|
-
closeWallet() {
|
|
1310
|
-
if (this.provider) {
|
|
1311
|
-
this.provider.closeWallet();
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
isOpened() {
|
|
1315
|
-
if (this.provider) {
|
|
1316
|
-
return this.provider.isOpened();
|
|
1317
|
-
}
|
|
1318
|
-
return false;
|
|
1319
|
-
}
|
|
1320
|
-
isConnected() {
|
|
1321
|
-
if (this.provider) {
|
|
1322
|
-
return this.provider.isConnected();
|
|
1323
|
-
}
|
|
1324
|
-
return false;
|
|
1325
|
-
}
|
|
1326
|
-
on(event, fn) {
|
|
1327
|
-
if (this.provider) {
|
|
1328
|
-
this.provider.on(event, fn);
|
|
1329
|
-
return;
|
|
1330
|
-
}
|
|
1331
|
-
this.messageProviders.forEach(m => {
|
|
1332
|
-
m.on(event, fn);
|
|
1333
|
-
});
|
|
1334
|
-
}
|
|
1335
|
-
once(event, fn) {
|
|
1336
|
-
if (this.provider) {
|
|
1337
|
-
this.provider.once(event, fn);
|
|
1338
|
-
return;
|
|
1339
|
-
}
|
|
1340
|
-
this.messageProviders.forEach(m => {
|
|
1341
|
-
m.once(event, fn);
|
|
1342
|
-
});
|
|
1343
|
-
}
|
|
1344
|
-
emit(event, ...args) {
|
|
1345
|
-
if (this.provider) {
|
|
1346
|
-
return this.provider.emit(event, ...args);
|
|
1347
|
-
}
|
|
1348
|
-
for (let i = 0; i < this.messageProviders.length; i++) {
|
|
1349
|
-
this.messageProviders[i].emit(event, ...args);
|
|
1350
|
-
}
|
|
1351
|
-
return true;
|
|
1352
|
-
}
|
|
1353
|
-
sendMessage(message) {
|
|
1354
|
-
if (!message.idx || message.idx <= 0) {
|
|
1355
|
-
throw new Error('message idx is empty');
|
|
1356
|
-
}
|
|
1357
|
-
if (this.provider) {
|
|
1358
|
-
this.provider.sendMessage(message);
|
|
1359
|
-
} else {
|
|
1360
|
-
throw new Error('impossible state, wallet must be opened first');
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1363
|
-
handleMessage(message) {
|
|
1364
|
-
if (this.provider) {
|
|
1365
|
-
this.provider.handleMessage(message);
|
|
1366
|
-
return;
|
|
1367
|
-
}
|
|
1368
|
-
throw new Error('impossible state, wallet must be opened first');
|
|
1281
|
+
window.logsOverriddenForUnreal = true;
|
|
1369
1282
|
}
|
|
1370
1283
|
}
|
|
1284
|
+
function stringify(things) {
|
|
1285
|
+
return things.map(a => typeof a === 'object' ? a instanceof Error ? a.message : JSON.stringify(a) : String(a)).join(' ');
|
|
1286
|
+
}
|
|
1371
1287
|
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1288
|
+
let registeredUnrealMessageProvider;
|
|
1289
|
+
|
|
1290
|
+
// all lowercase is an annoying limitation of Unreal CEF BindUObject
|
|
1291
|
+
|
|
1292
|
+
/**
|
|
1293
|
+
* Initialized on dApp side
|
|
1294
|
+
*/
|
|
1295
|
+
class UnrealMessageProvider extends BaseProviderTransport {
|
|
1375
1296
|
constructor(walletAppURL) {
|
|
1376
1297
|
super();
|
|
1377
1298
|
this.register = () => {
|
|
1378
|
-
|
|
1299
|
+
var _window$ue;
|
|
1300
|
+
overrideLogs('dapp');
|
|
1301
|
+
if (registeredUnrealMessageProvider) {
|
|
1379
1302
|
// overriding the registered message provider
|
|
1380
|
-
|
|
1381
|
-
|
|
1303
|
+
registeredUnrealMessageProvider.unregister();
|
|
1304
|
+
registeredUnrealMessageProvider = this;
|
|
1382
1305
|
}
|
|
1383
1306
|
|
|
1384
1307
|
// listen for incoming messages from wallet
|
|
1385
|
-
window.
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
this.on('open', () => {
|
|
1390
|
-
// Heartbeat to track if window closed
|
|
1391
|
-
const popup = this.walletWindow;
|
|
1392
|
-
const interval = setInterval(() => {
|
|
1393
|
-
if (popup && popup.closed) {
|
|
1394
|
-
clearInterval(interval);
|
|
1395
|
-
this.close();
|
|
1396
|
-
}
|
|
1397
|
-
}, 500);
|
|
1398
|
-
});
|
|
1399
|
-
|
|
1400
|
-
// close clean up
|
|
1401
|
-
this.on('close', () => {
|
|
1402
|
-
if (this.walletWindow) {
|
|
1403
|
-
this.walletWindow.close();
|
|
1404
|
-
this.walletWindow = null;
|
|
1405
|
-
}
|
|
1406
|
-
});
|
|
1308
|
+
if ((_window$ue = window.ue) != null && _window$ue.sequencewallettransport) {
|
|
1309
|
+
window.ue.sequencewallettransport.onmessagefromwallet = this.onUnrealCallback;
|
|
1310
|
+
}
|
|
1311
|
+
registeredUnrealMessageProvider = this;
|
|
1407
1312
|
this._registered = true;
|
|
1313
|
+
console.log('registering transport!');
|
|
1408
1314
|
};
|
|
1409
1315
|
this.unregister = () => {
|
|
1316
|
+
var _window$ue2, _window$ue2$sequencew;
|
|
1410
1317
|
this._registered = false;
|
|
1411
1318
|
this.closeWallet();
|
|
1412
1319
|
|
|
1413
1320
|
// disable message listener
|
|
1414
|
-
if (
|
|
1415
|
-
|
|
1321
|
+
if (registeredUnrealMessageProvider === this) {
|
|
1322
|
+
registeredUnrealMessageProvider = undefined;
|
|
1323
|
+
}
|
|
1324
|
+
if (((_window$ue2 = window.ue) == null ? void 0 : (_window$ue2$sequencew = _window$ue2.sequencewallettransport) == null ? void 0 : _window$ue2$sequencew.onmessagefromwallet) === this.onUnrealCallback) {
|
|
1325
|
+
delete window.ue.sequencewallettransport.onmessagefromwallet;
|
|
1416
1326
|
}
|
|
1417
|
-
window.removeEventListener('message', this.onWindowEvent);
|
|
1418
1327
|
|
|
1419
1328
|
// clear event listeners
|
|
1420
1329
|
this.events.removeAllListeners();
|
|
1421
1330
|
};
|
|
1422
1331
|
this.openWallet = (path, intent, networkId) => {
|
|
1423
|
-
if (this.
|
|
1424
|
-
// TODO
|
|
1425
|
-
|
|
1332
|
+
if (this.isOpened()) {
|
|
1333
|
+
// TODO focus wallet
|
|
1334
|
+
console.log('wallet already open!');
|
|
1426
1335
|
return;
|
|
1427
1336
|
}
|
|
1428
|
-
|
|
1337
|
+
console.log('opening wallet!');
|
|
1429
1338
|
// Instantiate new walletURL for this call
|
|
1430
1339
|
const walletURL = new URL(this.walletURL.href);
|
|
1431
1340
|
const windowSessionParams = new WindowSessionParams();
|
|
1432
|
-
if (path
|
|
1341
|
+
if (path) {
|
|
1433
1342
|
walletURL.pathname = path.toLowerCase();
|
|
1434
1343
|
}
|
|
1435
1344
|
|
|
@@ -1438,80 +1347,19 @@ class WindowMessageProvider extends BaseProviderTransport {
|
|
|
1438
1347
|
this._sessionId = `${performance.now()}`;
|
|
1439
1348
|
windowSessionParams.set('sid', this._sessionId);
|
|
1440
1349
|
if (intent) {
|
|
1441
|
-
// for the window-transport, we eagerly/optimistically set the origin host
|
|
1442
|
-
// when connecting to the wallet, however, this will be verified and enforced
|
|
1443
|
-
// on the wallet-side, so if a dapp provides the wrong origin, it will be dropped.
|
|
1444
|
-
if (intent.type === 'connect') {
|
|
1445
|
-
if (!intent.options) intent.options = {};
|
|
1446
|
-
|
|
1447
|
-
// skip setting origin host if we're in an browser extension execution context
|
|
1448
|
-
// allow origin that is passed in
|
|
1449
|
-
if (!isBrowserExtension() && !isUnityPlugin()) {
|
|
1450
|
-
intent.options.origin = window.location.origin;
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
1350
|
// encode intent as base64 url-encoded param
|
|
1454
1351
|
windowSessionParams.set('intent', base64EncodeObject(intent));
|
|
1455
1352
|
}
|
|
1456
1353
|
if (networkId) {
|
|
1457
1354
|
windowSessionParams.set('net', `${networkId}`);
|
|
1458
1355
|
}
|
|
1459
|
-
|
|
1460
|
-
// Open popup window on center of the app window
|
|
1461
|
-
let windowSize;
|
|
1462
|
-
let windowPos;
|
|
1463
|
-
if (isBrowserExtension()) {
|
|
1464
|
-
windowSize = [450, 750];
|
|
1465
|
-
windowPos = [Math.abs(window.screen.width / 2 - windowSize[0] / 2), Math.abs(window.screen.height / 2 - windowSize[1] / 2)];
|
|
1466
|
-
} else {
|
|
1467
|
-
windowSize = [450, 750];
|
|
1468
|
-
windowPos = [Math.abs(window.screenX + window.innerWidth / 2 - windowSize[0] / 2), Math.abs(window.screenY + window.innerHeight / 2 - windowSize[1] / 2)];
|
|
1469
|
-
}
|
|
1470
|
-
const windowFeatures = `toolbar=0,location=0,menubar=0,scrollbars=yes,status=yes` + `,width=${windowSize[0]},height=${windowSize[1]}` + `,left=${windowPos[0]},top=${windowPos[1]}`;
|
|
1471
|
-
|
|
1472
1356
|
// serialize params
|
|
1473
1357
|
walletURL.search = windowSessionParams.toString();
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
// TODO: move this somewhere else
|
|
1477
|
-
// TODO: perhaps we trigger a .on('openTimeout') event..? maybe.. could help.
|
|
1478
|
-
|
|
1479
|
-
// Popup blocking detection and notice
|
|
1480
|
-
// let warned = false
|
|
1481
|
-
// const warnPopupBlocked = () => {
|
|
1482
|
-
// if (warned) return
|
|
1483
|
-
// warned = true
|
|
1484
|
-
// // alert('popup is blocked! hey yo') // NOTE: for debug purposes only
|
|
1485
|
-
// throw new Error('popup is blocked')
|
|
1486
|
-
// }
|
|
1487
|
-
|
|
1488
|
-
// const popupCheck = setTimeout(() => {
|
|
1489
|
-
// if (!popup || popup.closed || typeof popup.closed === 'undefined') {
|
|
1490
|
-
// // popup is definitely blocked if we reach here.
|
|
1491
|
-
// warnPopupBlocked()
|
|
1492
|
-
// }
|
|
1493
|
-
// }, 1000)
|
|
1494
|
-
|
|
1495
|
-
// const popupBlocked = popup === null || popup === undefined
|
|
1496
|
-
// if (popupBlocked) {
|
|
1497
|
-
// warnPopupBlocked()
|
|
1498
|
-
// return
|
|
1499
|
-
// }
|
|
1358
|
+
console.log('opening wallet to', walletURL.href);
|
|
1359
|
+
window.open(walletURL.href);
|
|
1500
1360
|
};
|
|
1501
|
-
// onmessage, receives ProviderMessageResponse from the wallet
|
|
1502
|
-
this.
|
|
1503
|
-
// Security check, ensure message is coming from wallet origin url
|
|
1504
|
-
if (event.origin !== this.walletURL.origin) {
|
|
1505
|
-
// Safetly can skip events not from the wallet
|
|
1506
|
-
return;
|
|
1507
|
-
}
|
|
1508
|
-
let message;
|
|
1509
|
-
try {
|
|
1510
|
-
message = JSON.parse(event.data);
|
|
1511
|
-
} catch (err) {
|
|
1512
|
-
// event is not a ProviderMessage JSON object, skip
|
|
1513
|
-
return;
|
|
1514
|
-
}
|
|
1361
|
+
// onmessage, receives ProviderMessageResponse from the wallet unreal transport
|
|
1362
|
+
this.onUnrealCallback = message => {
|
|
1515
1363
|
if (!message) {
|
|
1516
1364
|
throw new Error('ProviderMessage object is empty');
|
|
1517
1365
|
}
|
|
@@ -1522,62 +1370,36 @@ class WindowMessageProvider extends BaseProviderTransport {
|
|
|
1522
1370
|
this.walletURL = new URL(walletAppURL);
|
|
1523
1371
|
}
|
|
1524
1372
|
closeWallet() {
|
|
1525
|
-
var _this$walletWindow;
|
|
1526
1373
|
this.close();
|
|
1527
|
-
(_this$walletWindow = this.walletWindow) == null ? void 0 : _this$walletWindow.close();
|
|
1528
1374
|
}
|
|
1375
|
+
// all lowercase is an annoying limitation of Unreal CEF BindUObject
|
|
1529
1376
|
sendMessage(message) {
|
|
1530
|
-
|
|
1531
|
-
logger.warn('WindowMessageProvider: sendMessage failed as walletWindow is unavailable');
|
|
1532
|
-
return;
|
|
1533
|
-
}
|
|
1377
|
+
var _window$ue3, _window$ue3$sequencew;
|
|
1534
1378
|
const postedMessage = typeof message !== 'string' ? JSON.stringify(message) : message;
|
|
1535
|
-
|
|
1379
|
+
console.log('Sending message to wallet:', postedMessage);
|
|
1380
|
+
(_window$ue3 = window.ue) == null ? void 0 : (_window$ue3$sequencew = _window$ue3.sequencewallettransport) == null ? void 0 : _window$ue3$sequencew.sendmessagetowallet(postedMessage);
|
|
1536
1381
|
}
|
|
1537
1382
|
}
|
|
1538
1383
|
|
|
1539
|
-
|
|
1384
|
+
// all lowercase is an annoying limitation of Unreal CEF BindUObject
|
|
1385
|
+
|
|
1386
|
+
/**
|
|
1387
|
+
* Initialized on Wallet side
|
|
1388
|
+
*/
|
|
1389
|
+
class UnrealMessageHandler extends BaseWalletTransport {
|
|
1540
1390
|
constructor(walletRequestHandler) {
|
|
1541
|
-
var _this;
|
|
1542
1391
|
super(walletRequestHandler);
|
|
1543
|
-
_this = this;
|
|
1544
|
-
this._isPopup = false;
|
|
1545
1392
|
// onmessage is called when (the wallet) receives request messages from the dapp
|
|
1546
|
-
// over the
|
|
1547
|
-
this.
|
|
1548
|
-
if (!event.origin || event.origin === '') {
|
|
1549
|
-
// skip same-origin or when event.origin is empty/undefined
|
|
1550
|
-
return;
|
|
1551
|
-
}
|
|
1552
|
-
if (_this.appOrigin && event.origin !== _this.appOrigin) {
|
|
1553
|
-
// skip message as not from expected app origin
|
|
1554
|
-
return;
|
|
1555
|
-
}
|
|
1556
|
-
|
|
1393
|
+
// over the unreal json-messaging transport
|
|
1394
|
+
this.onMessageFromUnreal = request => {
|
|
1557
1395
|
// Wallet always expects json-rpc request messages from a dapp
|
|
1558
|
-
let request;
|
|
1559
|
-
try {
|
|
1560
|
-
request = JSON.parse(event.data);
|
|
1561
|
-
} catch (err) {
|
|
1562
|
-
// event is not a ProviderMessage JSON object, skip
|
|
1563
|
-
return;
|
|
1564
|
-
}
|
|
1565
|
-
logger.debug('RECEIVED MESSAGE', request);
|
|
1566
1396
|
|
|
1567
|
-
|
|
1568
|
-
if (_this._init !== InitState.OK && _this.isValidInitAck(request)) {
|
|
1569
|
-
_this.appOrigin = event.origin;
|
|
1570
|
-
}
|
|
1571
|
-
if (_this._init === InitState.OK && (!_this.appOrigin || _this.appOrigin.length < 8)) {
|
|
1572
|
-
// impossible state
|
|
1573
|
-
logger.error('impossible state, init.OK and appOrigin required');
|
|
1574
|
-
return;
|
|
1575
|
-
}
|
|
1397
|
+
logger.debug('RECEIVED MESSAGE', request);
|
|
1576
1398
|
|
|
1577
1399
|
// Handle message via the base transport
|
|
1578
|
-
|
|
1400
|
+
this.handleMessage(request);
|
|
1579
1401
|
};
|
|
1580
|
-
this.
|
|
1402
|
+
this.getUnrealTransportSession = windowParams => {
|
|
1581
1403
|
const params = new WindowSessionParams(windowParams);
|
|
1582
1404
|
return {
|
|
1583
1405
|
sessionId: params.get('sid'),
|
|
@@ -1588,18 +1410,17 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
1588
1410
|
this._init = InitState.NIL;
|
|
1589
1411
|
}
|
|
1590
1412
|
async register(windowHref) {
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
if (isPopup !== true) {
|
|
1413
|
+
var _window$ue;
|
|
1414
|
+
if (((_window$ue = window.ue) == null ? void 0 : _window$ue.sequencewallettransport) === undefined) {
|
|
1594
1415
|
return;
|
|
1595
1416
|
}
|
|
1417
|
+
overrideLogs('wallet');
|
|
1596
1418
|
|
|
1597
1419
|
// record open details (sessionId + default network) from the window url
|
|
1598
1420
|
const {
|
|
1599
|
-
pathname,
|
|
1600
1421
|
search: rawParams
|
|
1601
1422
|
} = new URL(windowHref || window.location.href);
|
|
1602
|
-
let session = this.
|
|
1423
|
+
let session = this.getUnrealTransportSession(rawParams);
|
|
1603
1424
|
|
|
1604
1425
|
// provider should always include sid when opening a new window
|
|
1605
1426
|
const isNewWindowSession = !!session.sessionId;
|
|
@@ -1609,15 +1430,12 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
1609
1430
|
session = await this.getCachedTransportSession();
|
|
1610
1431
|
}
|
|
1611
1432
|
if (!session) {
|
|
1612
|
-
logger.error('
|
|
1433
|
+
logger.error('unreal session is undefined');
|
|
1613
1434
|
return;
|
|
1614
1435
|
}
|
|
1615
1436
|
|
|
1616
|
-
// record parent window instance for communication
|
|
1617
|
-
this.parentWindow = parent.window.opener;
|
|
1618
|
-
|
|
1619
1437
|
// listen for window-transport requests
|
|
1620
|
-
window.
|
|
1438
|
+
window.ue.sequencewallettransport.onmessagefromsequencejs = this.onMessageFromUnreal;
|
|
1621
1439
|
this._registered = true;
|
|
1622
1440
|
|
|
1623
1441
|
// send open event to the app which opened us
|
|
@@ -1642,79 +1460,670 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
1642
1460
|
});
|
|
1643
1461
|
}
|
|
1644
1462
|
unregister() {
|
|
1645
|
-
|
|
1463
|
+
var _window$ue2, _window$ue2$sequencew;
|
|
1464
|
+
if (((_window$ue2 = window.ue) == null ? void 0 : (_window$ue2$sequencew = _window$ue2.sequencewallettransport) == null ? void 0 : _window$ue2$sequencew.onmessagefromsequencejs) === this.onMessageFromUnreal) {
|
|
1465
|
+
delete window.ue.sequencewallettransport.onmessagefromsequencejs;
|
|
1466
|
+
}
|
|
1646
1467
|
this._registered = false;
|
|
1647
1468
|
}
|
|
1648
|
-
//
|
|
1469
|
+
// sendMessage sends message to the dapp window
|
|
1649
1470
|
sendMessage(message) {
|
|
1471
|
+
var _window$ue3, _window$ue3$sequencew;
|
|
1472
|
+
if (message.type !== EventType.INIT && this._init !== InitState.OK) {
|
|
1473
|
+
logger.error('impossible state, should not be calling postMessage until inited');
|
|
1474
|
+
return;
|
|
1475
|
+
}
|
|
1650
1476
|
// prepare payload
|
|
1651
1477
|
const payload = JSON.stringify(message);
|
|
1652
1478
|
|
|
1653
1479
|
// post-message to app.
|
|
1654
|
-
|
|
1655
|
-
if (message.type === EventType.INIT) {
|
|
1656
|
-
this.postMessage(payload, true);
|
|
1657
|
-
} else {
|
|
1658
|
-
this.postMessage(payload);
|
|
1659
|
-
}
|
|
1480
|
+
(_window$ue3 = window.ue) == null ? void 0 : (_window$ue3$sequencew = _window$ue3.sequencewallettransport) == null ? void 0 : _window$ue3$sequencew.sendmessagetosequencejs(payload);
|
|
1660
1481
|
}
|
|
1661
|
-
|
|
1662
|
-
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
const CHANNEL_ID = 'sequence-extension-message-handler';
|
|
1485
|
+
class ExtensionMessageHandler extends BaseWalletTransport {
|
|
1486
|
+
constructor(walletRequestHandler, runtime) {
|
|
1487
|
+
super(walletRequestHandler);
|
|
1488
|
+
this.runtime = runtime;
|
|
1489
|
+
this._init = InitState.OK;
|
|
1663
1490
|
}
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
}
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
// open message transmission
|
|
1674
|
-
if (this.appOrigin && this.appOrigin.length > 4) {
|
|
1675
|
-
// just above '.com'
|
|
1676
|
-
this.parentWindow.postMessage(message, this.appOrigin);
|
|
1677
|
-
} else {
|
|
1678
|
-
logger.error('unable to postMessage as parentOrigin is invalid');
|
|
1679
|
-
}
|
|
1680
|
-
}
|
|
1491
|
+
register() {
|
|
1492
|
+
this._registered = true;
|
|
1493
|
+
this.port = this.runtime.connect({
|
|
1494
|
+
name: CHANNEL_ID
|
|
1495
|
+
});
|
|
1496
|
+
}
|
|
1497
|
+
sendMessage(message) {
|
|
1498
|
+
logger.info('[ExtensionMessageHandler send]', message);
|
|
1499
|
+
this.port.postMessage(message);
|
|
1681
1500
|
}
|
|
1682
1501
|
}
|
|
1683
1502
|
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1503
|
+
class ExtensionMessageProvider extends BaseProviderTransport {
|
|
1504
|
+
constructor(runtime) {
|
|
1505
|
+
super();
|
|
1506
|
+
this.register = () => {
|
|
1507
|
+
this._registered = true;
|
|
1508
|
+
};
|
|
1509
|
+
runtime.onConnect.addListener(port => {
|
|
1510
|
+
if (port.name === CHANNEL_ID) {
|
|
1511
|
+
this._init = InitState.OK;
|
|
1512
|
+
port.onMessage.addListener(message => {
|
|
1513
|
+
this.handleMessage(message);
|
|
1514
|
+
});
|
|
1515
|
+
}
|
|
1516
|
+
});
|
|
1517
|
+
}
|
|
1518
|
+
sendMessage(message) {
|
|
1519
|
+
//noop
|
|
1520
|
+
}
|
|
1521
|
+
unregister() {
|
|
1522
|
+
//noop
|
|
1523
|
+
}
|
|
1524
|
+
openWallet(path, intent, networkId) {
|
|
1525
|
+
//noop
|
|
1526
|
+
}
|
|
1527
|
+
closeWallet() {
|
|
1528
|
+
//noop
|
|
1687
1529
|
}
|
|
1688
|
-
const [first, ...rest] = transactions;
|
|
1689
|
-
return _extends({}, first, {
|
|
1690
|
-
auxiliary: rest
|
|
1691
|
-
});
|
|
1692
|
-
}
|
|
1693
|
-
function fromExtended(transaction) {
|
|
1694
|
-
return [transaction, ...(transaction.auxiliary || [])];
|
|
1695
1530
|
}
|
|
1696
1531
|
|
|
1697
|
-
|
|
1698
|
-
class
|
|
1699
|
-
constructor(
|
|
1700
|
-
var _this
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
this.
|
|
1705
|
-
this.
|
|
1706
|
-
this.
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
// this is an unexpected state and should not happen
|
|
1710
|
-
throw new Error('prompter connect options are empty');
|
|
1532
|
+
// to be used on injected window.ethereum EIP1193 proxy
|
|
1533
|
+
class BaseInjectedTransport extends EventEmitter2 {
|
|
1534
|
+
constructor(stream) {
|
|
1535
|
+
var _this;
|
|
1536
|
+
super();
|
|
1537
|
+
_this = this;
|
|
1538
|
+
this.responseCallbacks = new Map();
|
|
1539
|
+
this._messageIdx = 0;
|
|
1540
|
+
this.nextMessageIdx = () => ++this._messageIdx;
|
|
1541
|
+
this.handleMessage = message => {
|
|
1542
|
+
if (!message.type || !message.data) {
|
|
1543
|
+
return;
|
|
1711
1544
|
}
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1545
|
+
logger.info('[received message]', message);
|
|
1546
|
+
const requestIdx = message.idx;
|
|
1547
|
+
const responseCallback = this.responseCallbacks.get(requestIdx);
|
|
1548
|
+
if (requestIdx) {
|
|
1549
|
+
this.responseCallbacks.delete(requestIdx);
|
|
1715
1550
|
}
|
|
1716
|
-
|
|
1717
|
-
|
|
1551
|
+
switch (message.type) {
|
|
1552
|
+
case EventType.MESSAGE:
|
|
1553
|
+
if (responseCallback) {
|
|
1554
|
+
this.emit(EventType.MESSAGE, message);
|
|
1555
|
+
responseCallback(message.data.error, message);
|
|
1556
|
+
} else {
|
|
1557
|
+
// NOTE: this would occur if 'idx' isn't set, which should never happen
|
|
1558
|
+
// or when we register two handler, or duplicate messages with the same idx are sent,
|
|
1559
|
+
// all of which should be prevented prior to getting to this point
|
|
1560
|
+
throw new Error('impossible state');
|
|
1561
|
+
}
|
|
1562
|
+
break;
|
|
1563
|
+
case EventType.DISCONNECT:
|
|
1564
|
+
case EventType.ACCOUNTS_CHANGED:
|
|
1565
|
+
case EventType.CHAIN_CHANGED:
|
|
1566
|
+
this.emit(message.type, message.data);
|
|
1567
|
+
break;
|
|
1568
|
+
default:
|
|
1569
|
+
console.error('unknown message type', message);
|
|
1570
|
+
break;
|
|
1571
|
+
}
|
|
1572
|
+
};
|
|
1573
|
+
this.sendMessageRequest = async function (message) {
|
|
1574
|
+
return new Promise((resolve, reject) => {
|
|
1575
|
+
if (!message.idx || message.idx <= 0) {
|
|
1576
|
+
reject(new Error('message idx not set'));
|
|
1577
|
+
}
|
|
1578
|
+
const responseCallback = (error, response) => {
|
|
1579
|
+
if (error) {
|
|
1580
|
+
reject(error);
|
|
1581
|
+
} else if (response) {
|
|
1582
|
+
resolve(response);
|
|
1583
|
+
} else {
|
|
1584
|
+
throw new Error('no valid response to return');
|
|
1585
|
+
}
|
|
1586
|
+
};
|
|
1587
|
+
const {
|
|
1588
|
+
idx
|
|
1589
|
+
} = message;
|
|
1590
|
+
if (!_this.responseCallbacks.get(idx)) {
|
|
1591
|
+
_this.responseCallbacks.set(idx, responseCallback);
|
|
1592
|
+
} else {
|
|
1593
|
+
reject(new Error('duplicate message idx, should never happen'));
|
|
1594
|
+
}
|
|
1595
|
+
_this.sendMessage(message);
|
|
1596
|
+
});
|
|
1597
|
+
};
|
|
1598
|
+
this.stream = stream;
|
|
1599
|
+
this.stream.on('data', this.handleMessage);
|
|
1600
|
+
}
|
|
1601
|
+
sendMessage(message) {
|
|
1602
|
+
if (!this.stream.writable) {
|
|
1603
|
+
console.error('window post message stream is not writable');
|
|
1604
|
+
}
|
|
1605
|
+
this.stream.write(message);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
// ..
|
|
1610
|
+
let registeredWindowMessageProvider;
|
|
1611
|
+
class WindowMessageProvider extends BaseProviderTransport {
|
|
1612
|
+
constructor(walletAppURL) {
|
|
1613
|
+
super();
|
|
1614
|
+
this.register = () => {
|
|
1615
|
+
if (registeredWindowMessageProvider) {
|
|
1616
|
+
// overriding the registered message provider
|
|
1617
|
+
registeredWindowMessageProvider.unregister();
|
|
1618
|
+
registeredWindowMessageProvider = this;
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
// listen for incoming messages from wallet
|
|
1622
|
+
window.addEventListener('message', this.onWindowEvent);
|
|
1623
|
+
registeredWindowMessageProvider = this;
|
|
1624
|
+
|
|
1625
|
+
// open heartbeat
|
|
1626
|
+
this.on('open', () => {
|
|
1627
|
+
// Heartbeat to track if window closed
|
|
1628
|
+
const popup = this.walletWindow;
|
|
1629
|
+
const interval = setInterval(() => {
|
|
1630
|
+
if (popup && popup.closed) {
|
|
1631
|
+
clearInterval(interval);
|
|
1632
|
+
this.close();
|
|
1633
|
+
}
|
|
1634
|
+
}, 500);
|
|
1635
|
+
});
|
|
1636
|
+
|
|
1637
|
+
// close clean up
|
|
1638
|
+
this.on('close', () => {
|
|
1639
|
+
if (this.walletWindow) {
|
|
1640
|
+
this.walletWindow.close();
|
|
1641
|
+
this.walletWindow = null;
|
|
1642
|
+
}
|
|
1643
|
+
});
|
|
1644
|
+
this._registered = true;
|
|
1645
|
+
};
|
|
1646
|
+
this.unregister = () => {
|
|
1647
|
+
this._registered = false;
|
|
1648
|
+
this.closeWallet();
|
|
1649
|
+
|
|
1650
|
+
// disable message listener
|
|
1651
|
+
if (registeredWindowMessageProvider === this) {
|
|
1652
|
+
registeredWindowMessageProvider = undefined;
|
|
1653
|
+
}
|
|
1654
|
+
window.removeEventListener('message', this.onWindowEvent);
|
|
1655
|
+
|
|
1656
|
+
// clear event listeners
|
|
1657
|
+
this.events.removeAllListeners();
|
|
1658
|
+
};
|
|
1659
|
+
this.openWallet = (path, intent, networkId) => {
|
|
1660
|
+
if (this.walletWindow && this.isOpened()) {
|
|
1661
|
+
// TODO: update the location of window to path
|
|
1662
|
+
this.walletWindow.focus();
|
|
1663
|
+
return;
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
// Instantiate new walletURL for this call
|
|
1667
|
+
const walletURL = new URL(this.walletURL.href);
|
|
1668
|
+
const windowSessionParams = new WindowSessionParams();
|
|
1669
|
+
if (path && path !== '') {
|
|
1670
|
+
walletURL.pathname = path.toLowerCase();
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
// Set session, intent and network id on walletURL
|
|
1674
|
+
this._init = InitState.NIL;
|
|
1675
|
+
this._sessionId = `${performance.now()}`;
|
|
1676
|
+
windowSessionParams.set('sid', this._sessionId);
|
|
1677
|
+
if (intent) {
|
|
1678
|
+
// for the window-transport, we eagerly/optimistically set the origin host
|
|
1679
|
+
// when connecting to the wallet, however, this will be verified and enforced
|
|
1680
|
+
// on the wallet-side, so if a dapp provides the wrong origin, it will be dropped.
|
|
1681
|
+
if (intent.type === 'connect') {
|
|
1682
|
+
if (!intent.options) intent.options = {
|
|
1683
|
+
app: window.location.origin
|
|
1684
|
+
};
|
|
1685
|
+
|
|
1686
|
+
// skip setting origin host if we're in an browser extension execution context
|
|
1687
|
+
// allow origin that is passed in
|
|
1688
|
+
if (!isBrowserExtension() && !isUnityPlugin() && intent.options) {
|
|
1689
|
+
intent.options.origin = window.location.origin;
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
// encode intent as base64 url-encoded param
|
|
1693
|
+
windowSessionParams.set('intent', base64EncodeObject(intent));
|
|
1694
|
+
}
|
|
1695
|
+
if (networkId) {
|
|
1696
|
+
windowSessionParams.set('net', `${networkId}`);
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
// Open popup window on center of the app window
|
|
1700
|
+
let windowSize;
|
|
1701
|
+
let windowPos;
|
|
1702
|
+
if (isBrowserExtension()) {
|
|
1703
|
+
windowSize = [450, 750];
|
|
1704
|
+
windowPos = [Math.abs(window.screen.width / 2 - windowSize[0] / 2), Math.abs(window.screen.height / 2 - windowSize[1] / 2)];
|
|
1705
|
+
} else {
|
|
1706
|
+
windowSize = [450, 750];
|
|
1707
|
+
windowPos = [Math.abs(window.screenX + window.innerWidth / 2 - windowSize[0] / 2), Math.abs(window.screenY + window.innerHeight / 2 - windowSize[1] / 2)];
|
|
1708
|
+
}
|
|
1709
|
+
const windowFeatures = `toolbar=0,location=0,menubar=0,scrollbars=yes,status=yes` + `,width=${windowSize[0]},height=${windowSize[1]}` + `,left=${windowPos[0]},top=${windowPos[1]}`;
|
|
1710
|
+
|
|
1711
|
+
// serialize params
|
|
1712
|
+
walletURL.search = windowSessionParams.toString();
|
|
1713
|
+
this.walletWindow = window.open(walletURL.href, 'sequence.app', windowFeatures);
|
|
1714
|
+
|
|
1715
|
+
// TODO: move this somewhere else
|
|
1716
|
+
// TODO: perhaps we trigger a .on('openTimeout') event..? maybe.. could help.
|
|
1717
|
+
|
|
1718
|
+
// Popup blocking detection and notice
|
|
1719
|
+
// let warned = false
|
|
1720
|
+
// const warnPopupBlocked = () => {
|
|
1721
|
+
// if (warned) return
|
|
1722
|
+
// warned = true
|
|
1723
|
+
// // alert('popup is blocked! hey yo') // NOTE: for debug purposes only
|
|
1724
|
+
// throw new Error('popup is blocked')
|
|
1725
|
+
// }
|
|
1726
|
+
|
|
1727
|
+
// const popupCheck = setTimeout(() => {
|
|
1728
|
+
// if (!popup || popup.closed || typeof popup.closed === 'undefined') {
|
|
1729
|
+
// // popup is definitely blocked if we reach here.
|
|
1730
|
+
// warnPopupBlocked()
|
|
1731
|
+
// }
|
|
1732
|
+
// }, 1000)
|
|
1733
|
+
|
|
1734
|
+
// const popupBlocked = popup === null || popup === undefined
|
|
1735
|
+
// if (popupBlocked) {
|
|
1736
|
+
// warnPopupBlocked()
|
|
1737
|
+
// return
|
|
1738
|
+
// }
|
|
1739
|
+
};
|
|
1740
|
+
// onmessage, receives ProviderMessageResponse from the wallet post-message transport
|
|
1741
|
+
this.onWindowEvent = event => {
|
|
1742
|
+
// Security check, ensure message is coming from wallet origin url
|
|
1743
|
+
if (event.origin !== this.walletURL.origin) {
|
|
1744
|
+
// Safetly can skip events not from the wallet
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1747
|
+
let message;
|
|
1748
|
+
try {
|
|
1749
|
+
message = JSON.parse(event.data);
|
|
1750
|
+
} catch (err) {
|
|
1751
|
+
// event is not a ProviderMessage JSON object, skip
|
|
1752
|
+
return;
|
|
1753
|
+
}
|
|
1754
|
+
if (!message) {
|
|
1755
|
+
throw new Error('ProviderMessage object is empty');
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
// handle message with base message provider
|
|
1759
|
+
this.handleMessage(message);
|
|
1760
|
+
};
|
|
1761
|
+
this.walletURL = new URL(walletAppURL);
|
|
1762
|
+
}
|
|
1763
|
+
closeWallet() {
|
|
1764
|
+
var _this$walletWindow;
|
|
1765
|
+
this.close();
|
|
1766
|
+
(_this$walletWindow = this.walletWindow) == null ? void 0 : _this$walletWindow.close();
|
|
1767
|
+
}
|
|
1768
|
+
sendMessage(message) {
|
|
1769
|
+
if (!this.walletWindow) {
|
|
1770
|
+
logger.warn('WindowMessageProvider: sendMessage failed as walletWindow is unavailable');
|
|
1771
|
+
return;
|
|
1772
|
+
}
|
|
1773
|
+
const postedMessage = typeof message !== 'string' ? JSON.stringify(message) : message;
|
|
1774
|
+
this.walletWindow.postMessage(postedMessage, this.walletURL.origin);
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
class WindowMessageHandler extends BaseWalletTransport {
|
|
1779
|
+
constructor(walletRequestHandler) {
|
|
1780
|
+
var _this;
|
|
1781
|
+
super(walletRequestHandler);
|
|
1782
|
+
_this = this;
|
|
1783
|
+
this._isPopup = false;
|
|
1784
|
+
// onmessage is called when (the wallet) receives request messages from the dapp
|
|
1785
|
+
// over the window post-messaging transport
|
|
1786
|
+
this.onWindowEvent = async function (event) {
|
|
1787
|
+
if (!event.origin || event.origin === '') {
|
|
1788
|
+
// skip same-origin or when event.origin is empty/undefined
|
|
1789
|
+
return;
|
|
1790
|
+
}
|
|
1791
|
+
if (_this.appOrigin && event.origin !== _this.appOrigin) {
|
|
1792
|
+
// skip message as not from expected app origin
|
|
1793
|
+
return;
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
// Wallet always expects json-rpc request messages from a dapp
|
|
1797
|
+
let request;
|
|
1798
|
+
try {
|
|
1799
|
+
request = JSON.parse(event.data);
|
|
1800
|
+
} catch (err) {
|
|
1801
|
+
// event is not a ProviderMessage JSON object, skip
|
|
1802
|
+
return;
|
|
1803
|
+
}
|
|
1804
|
+
logger.debug('RECEIVED MESSAGE', request);
|
|
1805
|
+
|
|
1806
|
+
// Record event origin for valid init ack
|
|
1807
|
+
if (_this._init !== InitState.OK && _this.isValidInitAck(request)) {
|
|
1808
|
+
_this.appOrigin = event.origin;
|
|
1809
|
+
}
|
|
1810
|
+
if (_this._init === InitState.OK && (!_this.appOrigin || _this.appOrigin.length < 8)) {
|
|
1811
|
+
// impossible state
|
|
1812
|
+
logger.error('impossible state, init.OK and appOrigin required');
|
|
1813
|
+
return;
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
// Handle message via the base transport
|
|
1817
|
+
_this.handleMessage(request);
|
|
1818
|
+
};
|
|
1819
|
+
this.getWindowTransportSession = windowParams => {
|
|
1820
|
+
const params = new WindowSessionParams(windowParams);
|
|
1821
|
+
return {
|
|
1822
|
+
sessionId: params.get('sid'),
|
|
1823
|
+
networkId: params.get('net'),
|
|
1824
|
+
intent: base64DecodeObject(params.get('intent'))
|
|
1825
|
+
};
|
|
1826
|
+
};
|
|
1827
|
+
this._init = InitState.NIL;
|
|
1828
|
+
}
|
|
1829
|
+
async register(windowHref) {
|
|
1830
|
+
const isPopup = parent.window.opener !== null;
|
|
1831
|
+
this._isPopup = isPopup;
|
|
1832
|
+
if (isPopup !== true) {
|
|
1833
|
+
return;
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
// record open details (sessionId + default network) from the window url
|
|
1837
|
+
const {
|
|
1838
|
+
pathname,
|
|
1839
|
+
search: rawParams
|
|
1840
|
+
} = new URL(windowHref || window.location.href);
|
|
1841
|
+
let session = this.getWindowTransportSession(rawParams);
|
|
1842
|
+
|
|
1843
|
+
// provider should always include sid when opening a new window
|
|
1844
|
+
const isNewWindowSession = !!session.sessionId;
|
|
1845
|
+
|
|
1846
|
+
// attempt to restore previous session in the case of a redirect or window reload
|
|
1847
|
+
if (!isNewWindowSession) {
|
|
1848
|
+
session = await this.getCachedTransportSession();
|
|
1849
|
+
}
|
|
1850
|
+
if (!session) {
|
|
1851
|
+
logger.error('window session is undefined');
|
|
1852
|
+
return;
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
// record parent window instance for communication
|
|
1856
|
+
this.parentWindow = parent.window.opener;
|
|
1857
|
+
|
|
1858
|
+
// listen for window-transport requests
|
|
1859
|
+
window.addEventListener('message', this.onWindowEvent, false);
|
|
1860
|
+
this._registered = true;
|
|
1861
|
+
|
|
1862
|
+
// send open event to the app which opened us
|
|
1863
|
+
this.open(session).then(opened => {
|
|
1864
|
+
if (!opened) {
|
|
1865
|
+
var _session;
|
|
1866
|
+
const err = `failed to open to network ${(_session = session) == null ? void 0 : _session.networkId}`;
|
|
1867
|
+
logger.error(err);
|
|
1868
|
+
this.notifyClose({
|
|
1869
|
+
message: err
|
|
1870
|
+
});
|
|
1871
|
+
window.close();
|
|
1872
|
+
}
|
|
1873
|
+
}).catch(e => {
|
|
1874
|
+
var _session2;
|
|
1875
|
+
const err = `failed to open to network ${(_session2 = session) == null ? void 0 : _session2.networkId}, due to: ${e}`;
|
|
1876
|
+
logger.error(err);
|
|
1877
|
+
this.notifyClose({
|
|
1878
|
+
message: err
|
|
1879
|
+
});
|
|
1880
|
+
window.close();
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
unregister() {
|
|
1884
|
+
window.removeEventListener('message', this.onWindowEvent);
|
|
1885
|
+
this._registered = false;
|
|
1886
|
+
}
|
|
1887
|
+
// postMessage sends message to the dapp window
|
|
1888
|
+
sendMessage(message) {
|
|
1889
|
+
// prepare payload
|
|
1890
|
+
const payload = JSON.stringify(message);
|
|
1891
|
+
|
|
1892
|
+
// post-message to app.
|
|
1893
|
+
// only for init requests, we send to '*' origin
|
|
1894
|
+
if (message.type === EventType.INIT) {
|
|
1895
|
+
this.postMessage(payload, true);
|
|
1896
|
+
} else {
|
|
1897
|
+
this.postMessage(payload);
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
get isPopup() {
|
|
1901
|
+
return this._isPopup;
|
|
1902
|
+
}
|
|
1903
|
+
postMessage(message, init = false) {
|
|
1904
|
+
if (init !== true && this._init !== InitState.OK) {
|
|
1905
|
+
logger.error('impossible state, should not be calling postMessage until inited');
|
|
1906
|
+
return;
|
|
1907
|
+
}
|
|
1908
|
+
if (init) {
|
|
1909
|
+
// init message transmission to global target -- for 'init' payloads only
|
|
1910
|
+
this.parentWindow.postMessage(message, '*');
|
|
1911
|
+
} else {
|
|
1912
|
+
// open message transmission
|
|
1913
|
+
if (this.appOrigin && this.appOrigin.length > 4) {
|
|
1914
|
+
// just above '.com'
|
|
1915
|
+
this.parentWindow.postMessage(message, this.appOrigin);
|
|
1916
|
+
} else {
|
|
1917
|
+
logger.error('unable to postMessage as parentOrigin is invalid');
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
|
|
1923
|
+
function isMuxTransportTemplate(obj) {
|
|
1924
|
+
return obj && typeof obj === 'object' && (obj.windowTransport && typeof obj.windowTransport === 'object' || obj.proxyTransport && typeof obj.proxyTransport === 'object' || obj.extensionTransport && typeof obj.extensionTransport === 'object' || obj.unrealTransport && typeof obj.unrealTransport === 'object') && (
|
|
1925
|
+
// One of the transports must be enabled
|
|
1926
|
+
obj.windowTransport && obj.windowTransport.enabled || obj.proxyTransport && obj.proxyTransport.enabled || obj.extensionTransport && obj.extensionTransport.enabled || obj.unrealTransport && obj.unrealTransport.enabled);
|
|
1927
|
+
}
|
|
1928
|
+
class MuxMessageProvider {
|
|
1929
|
+
constructor(...messageProviders) {
|
|
1930
|
+
var _this = this;
|
|
1931
|
+
this.register = () => {
|
|
1932
|
+
if (this.messageProviders.length === 1) {
|
|
1933
|
+
this.provider = this.messageProviders[0];
|
|
1934
|
+
this.provider.register();
|
|
1935
|
+
return;
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
// REVIEW/NOTE: ........ this method does not work for the chrome-extension. The issue becomes
|
|
1939
|
+
// when the browser quits or restarts, the "open" event is never triggered. Perhaps the code here is fine,
|
|
1940
|
+
// or maybe its not. What should happen is when a dapp makes a request, it will call openWallet
|
|
1941
|
+
// below, in which case one of the events will register. So perhaps this is fine.
|
|
1942
|
+
this.messageProviders.forEach(m => {
|
|
1943
|
+
m.register();
|
|
1944
|
+
m.once('open', () => {
|
|
1945
|
+
// the first one to open is the winner, and others will be unregistered
|
|
1946
|
+
if (!this.provider) {
|
|
1947
|
+
this.provider = m;
|
|
1948
|
+
|
|
1949
|
+
// unregister other providers
|
|
1950
|
+
this.messageProviders.forEach(m => {
|
|
1951
|
+
if (this.provider !== m) {
|
|
1952
|
+
m.unregister();
|
|
1953
|
+
}
|
|
1954
|
+
});
|
|
1955
|
+
}
|
|
1956
|
+
});
|
|
1957
|
+
});
|
|
1958
|
+
};
|
|
1959
|
+
this.unregister = () => {
|
|
1960
|
+
this.messageProviders.forEach(m => m.unregister());
|
|
1961
|
+
this.provider = undefined;
|
|
1962
|
+
};
|
|
1963
|
+
this.openWallet = (path, intent, networkId) => {
|
|
1964
|
+
if (this.provider) {
|
|
1965
|
+
this.provider.openWallet(path, intent, networkId);
|
|
1966
|
+
return;
|
|
1967
|
+
}
|
|
1968
|
+
this.messageProviders.forEach(m => m.openWallet(path, intent, networkId));
|
|
1969
|
+
};
|
|
1970
|
+
this.sendAsync = async function (request, callback, chainId) {
|
|
1971
|
+
if (_this.provider) {
|
|
1972
|
+
_this.provider.sendAsync(request, callback, chainId);
|
|
1973
|
+
return;
|
|
1974
|
+
}
|
|
1975
|
+
throw new Error('impossible state, wallet must be opened first');
|
|
1976
|
+
};
|
|
1977
|
+
this.sendMessageRequest = async function (message) {
|
|
1978
|
+
if (_this.provider) {
|
|
1979
|
+
return _this.provider.sendMessageRequest(message);
|
|
1980
|
+
}
|
|
1981
|
+
throw new Error('impossible state, wallet must be opened first');
|
|
1982
|
+
};
|
|
1983
|
+
this.waitUntilOpened = async function () {
|
|
1984
|
+
if (_this.provider) {
|
|
1985
|
+
return _this.provider.waitUntilOpened();
|
|
1986
|
+
}
|
|
1987
|
+
return Promise.race(_this.messageProviders.map(p => p.waitUntilOpened()));
|
|
1988
|
+
};
|
|
1989
|
+
this.waitUntilConnected = async function () {
|
|
1990
|
+
if (_this.provider) {
|
|
1991
|
+
return _this.provider.waitUntilConnected();
|
|
1992
|
+
}
|
|
1993
|
+
throw new Error('impossible state, wallet must be opened first');
|
|
1994
|
+
};
|
|
1995
|
+
this.messageProviders = messageProviders;
|
|
1996
|
+
this.provider = undefined;
|
|
1997
|
+
}
|
|
1998
|
+
static new(template) {
|
|
1999
|
+
var _template$windowTrans, _template$proxyTransp, _template$extensionTr, _template$unrealTrans;
|
|
2000
|
+
const muxMessageProvider = new MuxMessageProvider();
|
|
2001
|
+
if ((_template$windowTrans = template.windowTransport) != null && _template$windowTrans.enabled && typeof window === 'object' && template.walletAppURL) {
|
|
2002
|
+
const windowMessageProvider = new WindowMessageProvider(template.walletAppURL);
|
|
2003
|
+
muxMessageProvider.add(windowMessageProvider);
|
|
2004
|
+
}
|
|
2005
|
+
if ((_template$proxyTransp = template.proxyTransport) != null && _template$proxyTransp.enabled) {
|
|
2006
|
+
const proxyMessageProvider = new ProxyMessageProvider(template.proxyTransport.appPort);
|
|
2007
|
+
muxMessageProvider.add(proxyMessageProvider);
|
|
2008
|
+
}
|
|
2009
|
+
if ((_template$extensionTr = template.extensionTransport) != null && _template$extensionTr.enabled) {
|
|
2010
|
+
const extensionMessageProvider = new ExtensionMessageProvider(template.extensionTransport.runtime);
|
|
2011
|
+
muxMessageProvider.add(extensionMessageProvider);
|
|
2012
|
+
|
|
2013
|
+
// NOTE/REVIEW: see note in mux-message-provider
|
|
2014
|
+
//
|
|
2015
|
+
// We don't add the extensionMessageProvider here because we don't send requests to it anyways, we seem to
|
|
2016
|
+
// send all requests to the WindowMessageProvider anyways. By allowing it, if browser restarts, it will break
|
|
2017
|
+
// the entire extension because messageProvider.provider will be undefined. So this is a hack to fix it.
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
if ((_template$unrealTrans = template.unrealTransport) != null && _template$unrealTrans.enabled && template.windowTransport && template.walletAppURL) {
|
|
2021
|
+
const unrealMessageProvider = new UnrealMessageProvider(template.walletAppURL);
|
|
2022
|
+
muxMessageProvider.add(unrealMessageProvider);
|
|
2023
|
+
}
|
|
2024
|
+
muxMessageProvider.register();
|
|
2025
|
+
return muxMessageProvider;
|
|
2026
|
+
}
|
|
2027
|
+
add(...messageProviders) {
|
|
2028
|
+
this.messageProviders.push(...messageProviders);
|
|
2029
|
+
}
|
|
2030
|
+
closeWallet() {
|
|
2031
|
+
if (this.provider) {
|
|
2032
|
+
this.provider.closeWallet();
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
isOpened() {
|
|
2036
|
+
if (this.provider) {
|
|
2037
|
+
return this.provider.isOpened();
|
|
2038
|
+
}
|
|
2039
|
+
return false;
|
|
2040
|
+
}
|
|
2041
|
+
isConnected() {
|
|
2042
|
+
if (this.provider) {
|
|
2043
|
+
return this.provider.isConnected();
|
|
2044
|
+
}
|
|
2045
|
+
return false;
|
|
2046
|
+
}
|
|
2047
|
+
on(event, fn) {
|
|
2048
|
+
if (this.provider) {
|
|
2049
|
+
this.provider.on(event, fn);
|
|
2050
|
+
return;
|
|
2051
|
+
}
|
|
2052
|
+
this.messageProviders.forEach(m => {
|
|
2053
|
+
m.on(event, fn);
|
|
2054
|
+
});
|
|
2055
|
+
}
|
|
2056
|
+
once(event, fn) {
|
|
2057
|
+
if (this.provider) {
|
|
2058
|
+
this.provider.once(event, fn);
|
|
2059
|
+
return;
|
|
2060
|
+
}
|
|
2061
|
+
this.messageProviders.forEach(m => {
|
|
2062
|
+
m.once(event, fn);
|
|
2063
|
+
});
|
|
2064
|
+
}
|
|
2065
|
+
emit(event, ...args) {
|
|
2066
|
+
if (this.provider) {
|
|
2067
|
+
return this.provider.emit(event, ...args);
|
|
2068
|
+
}
|
|
2069
|
+
for (let i = 0; i < this.messageProviders.length; i++) {
|
|
2070
|
+
this.messageProviders[i].emit(event, ...args);
|
|
2071
|
+
}
|
|
2072
|
+
return true;
|
|
2073
|
+
}
|
|
2074
|
+
sendMessage(message) {
|
|
2075
|
+
if (!message.idx || message.idx <= 0) {
|
|
2076
|
+
throw new Error('message idx is empty');
|
|
2077
|
+
}
|
|
2078
|
+
if (this.provider) {
|
|
2079
|
+
this.provider.sendMessage(message);
|
|
2080
|
+
} else {
|
|
2081
|
+
throw new Error('impossible state, wallet must be opened first');
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
handleMessage(message) {
|
|
2085
|
+
if (this.provider) {
|
|
2086
|
+
this.provider.handleMessage(message);
|
|
2087
|
+
return;
|
|
2088
|
+
}
|
|
2089
|
+
throw new Error('impossible state, wallet must be opened first');
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
function toExtended(transactions) {
|
|
2094
|
+
if (transactions.length === 0) {
|
|
2095
|
+
throw new Error("No transaction provided");
|
|
2096
|
+
}
|
|
2097
|
+
const [first, ...rest] = transactions;
|
|
2098
|
+
return _extends({}, first, {
|
|
2099
|
+
auxiliary: rest
|
|
2100
|
+
});
|
|
2101
|
+
}
|
|
2102
|
+
function fromExtended(transaction) {
|
|
2103
|
+
return [transaction, ...(transaction.auxiliary || [])];
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
const SIGNER_READY_TIMEOUT = 10000;
|
|
2107
|
+
class WalletRequestHandler {
|
|
2108
|
+
constructor(_account, prompter, networks) {
|
|
2109
|
+
var _this = this;
|
|
2110
|
+
// signer interface of the wallet. A null value means there is no signer (ie. user not signed in). An undefined
|
|
2111
|
+
// value means the signer state is unknown, usually meaning the wallet app is booting up and initializing. Of course
|
|
2112
|
+
// a Signer value is the actually interface to a signed-in account
|
|
2113
|
+
this.signerReadyCallbacks = [];
|
|
2114
|
+
this.events = new EventEmitter2();
|
|
2115
|
+
this.onConnectOptionsChange = undefined;
|
|
2116
|
+
this.promptConnect = async function (options) {
|
|
2117
|
+
if (!options && !_this._connectOptions) {
|
|
2118
|
+
// this is an unexpected state and should not happen
|
|
2119
|
+
throw new Error('prompter connect options are empty');
|
|
2120
|
+
}
|
|
2121
|
+
if (!_this.prompter) {
|
|
2122
|
+
// if prompter is null, we'll auto connect
|
|
2123
|
+
return _this.connect(options);
|
|
2124
|
+
}
|
|
2125
|
+
const promptConnectDetails = await _this.prompter.promptConnect(options || _this._connectOptions).catch(_ => {
|
|
2126
|
+
return {
|
|
1718
2127
|
connected: false
|
|
1719
2128
|
};
|
|
1720
2129
|
});
|
|
@@ -2151,16 +2560,6 @@ class WalletRequestHandler {
|
|
|
2151
2560
|
var _this$prompter$getDef, _this$prompter;
|
|
2152
2561
|
return (_this$prompter$getDef = (_this$prompter = this.prompter) == null ? void 0 : _this$prompter.getDefaultChainId()) != null ? _this$prompter$getDef : this.networks[0].chainId;
|
|
2153
2562
|
}
|
|
2154
|
-
findNetworkID(network) {
|
|
2155
|
-
const lnet = typeof network === 'string' ? network.toLowerCase() : network;
|
|
2156
|
-
const networkId = this.networks.find(n => {
|
|
2157
|
-
return n.name.toLowerCase() === lnet || n.chainId.toString() === lnet || n.chainId === lnet;
|
|
2158
|
-
});
|
|
2159
|
-
if (!networkId) {
|
|
2160
|
-
throw new Error(`Network ${network} not found`);
|
|
2161
|
-
}
|
|
2162
|
-
return networkId.chainId;
|
|
2163
|
-
}
|
|
2164
2563
|
async signIn(account, options = {}) {
|
|
2165
2564
|
this.setAccount(account);
|
|
2166
2565
|
const {
|
|
@@ -2229,24 +2628,7 @@ class WalletRequestHandler {
|
|
|
2229
2628
|
error: 'unable to connect without signed in account'
|
|
2230
2629
|
};
|
|
2231
2630
|
}
|
|
2232
|
-
|
|
2233
|
-
switch (typeof (options == null ? void 0 : options.networkId)) {
|
|
2234
|
-
case 'string':
|
|
2235
|
-
// First see if it matches the name of a network
|
|
2236
|
-
try {
|
|
2237
|
-
chainId = this.findNetworkID(options.networkId);
|
|
2238
|
-
} catch (err) {
|
|
2239
|
-
// If not, try to parse it as a big number
|
|
2240
|
-
chainId = getChainId(options.networkId);
|
|
2241
|
-
}
|
|
2242
|
-
break;
|
|
2243
|
-
case 'number':
|
|
2244
|
-
chainId = options.networkId;
|
|
2245
|
-
break;
|
|
2246
|
-
default:
|
|
2247
|
-
chainId = (_this$prompter$getDef2 = (_this$prompter4 = this.prompter) == null ? void 0 : _this$prompter4.getDefaultChainId()) != null ? _this$prompter$getDef2 : 1;
|
|
2248
|
-
break;
|
|
2249
|
-
}
|
|
2631
|
+
const chainId = (_this$prompter$getDef2 = (_this$prompter4 = this.prompter) == null ? void 0 : _this$prompter4.getDefaultChainId()) != null ? _this$prompter$getDef2 : 1;
|
|
2250
2632
|
const connectDetails = {
|
|
2251
2633
|
connected: true,
|
|
2252
2634
|
chainId: ethers.BigNumber.from(chainId).toHexString()
|
|
@@ -2358,6 +2740,7 @@ class WalletRequestHandler {
|
|
|
2358
2740
|
}
|
|
2359
2741
|
notifyConnect(connectDetails, origin) {
|
|
2360
2742
|
var _connectDetails$sessi;
|
|
2743
|
+
console.log('emit connect', connectDetails);
|
|
2361
2744
|
this.events.emit('connect', connectDetails);
|
|
2362
2745
|
if ((_connectDetails$sessi = connectDetails.session) != null && _connectDetails$sessi.accountAddress) {
|
|
2363
2746
|
var _connectDetails$sessi2;
|
|
@@ -2443,1406 +2826,1082 @@ function getLegacyWalletState(chainId, status) {
|
|
|
2443
2826
|
}
|
|
2444
2827
|
const permittedJsonRpcMethods = ['net_version', 'eth_chainId', 'eth_getBalance', 'eth_getTransactionCount', 'eth_blockNumber', 'eth_getBlockByNumber', 'eth_getBlockByHash', 'eth_getTransactionByHash', 'eth_getCode', 'eth_estimateGas', 'eth_gasPrice', 'sequence_getWalletContext', 'sequence_getNetworks', 'sequence_setDefaultNetwork'];
|
|
2445
2828
|
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
this.
|
|
2829
|
+
class SequenceSigner {
|
|
2830
|
+
get utils() {
|
|
2831
|
+
return this.provider.utils;
|
|
2832
|
+
}
|
|
2833
|
+
constructor(client, provider) {
|
|
2834
|
+
this.singleNetworkSigners = {};
|
|
2835
|
+
this._isSigner = true;
|
|
2836
|
+
this._isSequenceSigner = true;
|
|
2837
|
+
this.client = client;
|
|
2838
|
+
this.provider = provider;
|
|
2839
|
+
}
|
|
2840
|
+
async getAddress() {
|
|
2841
|
+
return this.client.getAddress();
|
|
2842
|
+
}
|
|
2843
|
+
|
|
2844
|
+
// This method shouldn't be used directly
|
|
2845
|
+
// it exists to maintain compatibility with ethers.Signer
|
|
2846
|
+
connect(provider) {
|
|
2847
|
+
if (!SequenceProvider.is(provider)) {
|
|
2848
|
+
throw new Error("SequenceSigner can only be connected to a SequenceProvider");
|
|
2849
|
+
}
|
|
2850
|
+
return new SequenceSigner(this.client, provider);
|
|
2851
|
+
}
|
|
2852
|
+
getSigner(chainId) {
|
|
2853
|
+
// The signer for the default network is this signer
|
|
2854
|
+
if (!chainId) {
|
|
2855
|
+
return this;
|
|
2856
|
+
}
|
|
2857
|
+
const useChainId = this.provider.toChainId(chainId);
|
|
2858
|
+
if (!this.singleNetworkSigners[useChainId]) {
|
|
2859
|
+
this.singleNetworkSigners[useChainId] = new SingleNetworkSequenceSigner(this.client, this.provider, useChainId);
|
|
2860
|
+
}
|
|
2861
|
+
return this.singleNetworkSigners[useChainId];
|
|
2862
|
+
}
|
|
2863
|
+
|
|
2864
|
+
/**
|
|
2865
|
+
* Resolves the chainId to use for the given request. If no chainId is provided,
|
|
2866
|
+
* it uses the chainId defined by the client (default chainId). This can be
|
|
2867
|
+
* overriden to build a single-network SequenceProvider.
|
|
2868
|
+
*/
|
|
2869
|
+
useChainId(chainId) {
|
|
2870
|
+
return this.provider.toChainId(chainId) || this.client.getChainId();
|
|
2871
|
+
}
|
|
2872
|
+
async signMessage(message, options) {
|
|
2873
|
+
const {
|
|
2874
|
+
eip6492 = true
|
|
2875
|
+
} = options || {};
|
|
2876
|
+
const chainId = this.useChainId(options == null ? void 0 : options.chainId);
|
|
2877
|
+
return this.client.signMessage(message, {
|
|
2878
|
+
eip6492,
|
|
2879
|
+
chainId
|
|
2880
|
+
});
|
|
2881
|
+
}
|
|
2882
|
+
async signTypedData(domain, types, message, options) {
|
|
2883
|
+
const {
|
|
2884
|
+
eip6492 = true
|
|
2885
|
+
} = options || {};
|
|
2886
|
+
const chainId = this.useChainId(options == null ? void 0 : options.chainId);
|
|
2887
|
+
return this.client.signTypedData({
|
|
2888
|
+
domain,
|
|
2889
|
+
types,
|
|
2890
|
+
message
|
|
2891
|
+
}, {
|
|
2892
|
+
eip6492,
|
|
2893
|
+
chainId
|
|
2894
|
+
});
|
|
2895
|
+
}
|
|
2896
|
+
getProvider(chainId) {
|
|
2897
|
+
return this.provider.getProvider(chainId);
|
|
2898
|
+
}
|
|
2899
|
+
async sendTransaction(transaction, options) {
|
|
2900
|
+
const chainId = this.useChainId(options == null ? void 0 : options.chainId);
|
|
2901
|
+
const resolved = await resolveArrayProperties(transaction);
|
|
2902
|
+
const txHash = await this.client.sendTransaction(resolved, {
|
|
2903
|
+
chainId
|
|
2904
|
+
});
|
|
2905
|
+
const provider = this.getProvider(chainId);
|
|
2906
|
+
try {
|
|
2907
|
+
return await ethers.utils.poll(async function () {
|
|
2908
|
+
const tx = await provider.getTransaction(txHash);
|
|
2909
|
+
return tx ? provider._wrapTransaction(tx, txHash) : undefined;
|
|
2910
|
+
}, {
|
|
2911
|
+
onceBlock: provider
|
|
2912
|
+
});
|
|
2913
|
+
} catch (err) {
|
|
2914
|
+
err.transactionHash = txHash;
|
|
2915
|
+
throw err;
|
|
2916
|
+
}
|
|
2452
2917
|
}
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2918
|
+
async getWalletConfig(chainId) {
|
|
2919
|
+
const useChainId = this.useChainId(chainId);
|
|
2920
|
+
return this.client.getOnchainWalletConfig({
|
|
2921
|
+
chainId: useChainId
|
|
2457
2922
|
});
|
|
2458
2923
|
}
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
this.port.postMessage(message);
|
|
2924
|
+
getNetworks() {
|
|
2925
|
+
return this.client.getNetworks();
|
|
2462
2926
|
}
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
constructor(runtime) {
|
|
2467
|
-
super();
|
|
2468
|
-
this.register = () => {
|
|
2469
|
-
this._registered = true;
|
|
2470
|
-
};
|
|
2471
|
-
runtime.onConnect.addListener(port => {
|
|
2472
|
-
if (port.name === CHANNEL_ID) {
|
|
2473
|
-
this._init = InitState.OK;
|
|
2474
|
-
port.onMessage.addListener(message => {
|
|
2475
|
-
this.handleMessage(message);
|
|
2476
|
-
});
|
|
2477
|
-
}
|
|
2478
|
-
});
|
|
2927
|
+
async getBalance(blockTag, optionals) {
|
|
2928
|
+
const provider = this.getProvider(optionals == null ? void 0 : optionals.chainId);
|
|
2929
|
+
return provider.getBalance(this.getAddress(), blockTag);
|
|
2479
2930
|
}
|
|
2480
|
-
|
|
2481
|
-
|
|
2931
|
+
async estimateGas(transaction, optionals) {
|
|
2932
|
+
return this.getProvider(optionals == null ? void 0 : optionals.chainId).estimateGas(transaction);
|
|
2482
2933
|
}
|
|
2483
|
-
|
|
2484
|
-
|
|
2934
|
+
async call(transaction, blockTag, optionals) {
|
|
2935
|
+
return this.getProvider(optionals == null ? void 0 : optionals.chainId).call(transaction, blockTag);
|
|
2485
2936
|
}
|
|
2486
|
-
|
|
2487
|
-
|
|
2937
|
+
getChainId() {
|
|
2938
|
+
return Promise.resolve(this.client.getChainId());
|
|
2488
2939
|
}
|
|
2489
|
-
|
|
2490
|
-
|
|
2940
|
+
async getGasPrice(optionals) {
|
|
2941
|
+
return this.getProvider(optionals == null ? void 0 : optionals.chainId).getGasPrice();
|
|
2491
2942
|
}
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
// to be used on injected window.ethereum EIP1193 proxy
|
|
2495
|
-
class BaseInjectedTransport extends EventEmitter2 {
|
|
2496
|
-
constructor(stream) {
|
|
2497
|
-
var _this;
|
|
2498
|
-
super();
|
|
2499
|
-
_this = this;
|
|
2500
|
-
this.responseCallbacks = new Map();
|
|
2501
|
-
this._messageIdx = 0;
|
|
2502
|
-
this.nextMessageIdx = () => ++this._messageIdx;
|
|
2503
|
-
this.handleMessage = message => {
|
|
2504
|
-
if (!message.type || !message.data) {
|
|
2505
|
-
return;
|
|
2506
|
-
}
|
|
2507
|
-
logger.info('[received message]', message);
|
|
2508
|
-
const requestIdx = message.idx;
|
|
2509
|
-
const responseCallback = this.responseCallbacks.get(requestIdx);
|
|
2510
|
-
if (requestIdx) {
|
|
2511
|
-
this.responseCallbacks.delete(requestIdx);
|
|
2512
|
-
}
|
|
2513
|
-
switch (message.type) {
|
|
2514
|
-
case EventType.MESSAGE:
|
|
2515
|
-
if (responseCallback) {
|
|
2516
|
-
this.emit(EventType.MESSAGE, message);
|
|
2517
|
-
responseCallback(message.data.error, message);
|
|
2518
|
-
} else {
|
|
2519
|
-
// NOTE: this would occur if 'idx' isn't set, which should never happen
|
|
2520
|
-
// or when we register two handler, or duplicate messages with the same idx are sent,
|
|
2521
|
-
// all of which should be prevented prior to getting to this point
|
|
2522
|
-
throw new Error('impossible state');
|
|
2523
|
-
}
|
|
2524
|
-
break;
|
|
2525
|
-
case EventType.DISCONNECT:
|
|
2526
|
-
case EventType.ACCOUNTS_CHANGED:
|
|
2527
|
-
case EventType.CHAIN_CHANGED:
|
|
2528
|
-
this.emit(message.type, message.data);
|
|
2529
|
-
break;
|
|
2530
|
-
default:
|
|
2531
|
-
console.error('unknown message type', message);
|
|
2532
|
-
break;
|
|
2533
|
-
}
|
|
2534
|
-
};
|
|
2535
|
-
this.sendMessageRequest = async function (message) {
|
|
2536
|
-
return new Promise((resolve, reject) => {
|
|
2537
|
-
if (!message.idx || message.idx <= 0) {
|
|
2538
|
-
reject(new Error('message idx not set'));
|
|
2539
|
-
}
|
|
2540
|
-
const responseCallback = (error, response) => {
|
|
2541
|
-
if (error) {
|
|
2542
|
-
reject(error);
|
|
2543
|
-
} else if (response) {
|
|
2544
|
-
resolve(response);
|
|
2545
|
-
} else {
|
|
2546
|
-
throw new Error('no valid response to return');
|
|
2547
|
-
}
|
|
2548
|
-
};
|
|
2549
|
-
const {
|
|
2550
|
-
idx
|
|
2551
|
-
} = message;
|
|
2552
|
-
if (!_this.responseCallbacks.get(idx)) {
|
|
2553
|
-
_this.responseCallbacks.set(idx, responseCallback);
|
|
2554
|
-
} else {
|
|
2555
|
-
reject(new Error('duplicate message idx, should never happen'));
|
|
2556
|
-
}
|
|
2557
|
-
_this.sendMessage(message);
|
|
2558
|
-
});
|
|
2559
|
-
};
|
|
2560
|
-
this.stream = stream;
|
|
2561
|
-
this.stream.on('data', this.handleMessage);
|
|
2943
|
+
async getFeeData(optionals) {
|
|
2944
|
+
return this.getProvider(optionals == null ? void 0 : optionals.chainId).getFeeData();
|
|
2562
2945
|
}
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2946
|
+
async resolveName(name) {
|
|
2947
|
+
const res = await this.provider.resolveName(name);
|
|
2948
|
+
|
|
2949
|
+
// For some reason ethers.Signer expects this to return `string`
|
|
2950
|
+
// but ethers.providers.Provider expects this to return `string | null`.
|
|
2951
|
+
// The signer doesn't have any other source of information, so we'll
|
|
2952
|
+
// fail if the provider doesn't return a result.
|
|
2953
|
+
if (res === null) {
|
|
2954
|
+
throw new Error(`ENS name not found: ${name}`);
|
|
2566
2955
|
}
|
|
2567
|
-
|
|
2956
|
+
return res;
|
|
2568
2957
|
}
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
/**
|
|
2572
|
-
* This will redirect console logs from Sequence.js & the wallet to the Unreal console, for debugging purposes.
|
|
2573
|
-
*/
|
|
2574
|
-
function overrideLogs(side) {
|
|
2575
|
-
var _window$ue;
|
|
2576
|
-
if ((_window$ue = window.ue) != null && _window$ue.sequencewallettransport && !window.logsOverriddenForUnreal) {
|
|
2577
|
-
var _window$ue2;
|
|
2578
|
-
const t = (_window$ue2 = window.ue) == null ? void 0 : _window$ue2.sequencewallettransport;
|
|
2579
|
-
console.log = (...args) => {
|
|
2580
|
-
t.logfromjs(`${side}: ${stringify(args)}`);
|
|
2581
|
-
};
|
|
2582
|
-
console.warn = (...args) => {
|
|
2583
|
-
t.warnfromjs(`${side}: ${stringify(args)}`);
|
|
2584
|
-
};
|
|
2585
|
-
console.error = (...args) => {
|
|
2586
|
-
t.errorfromjs(`${side}: ${stringify(args)}`);
|
|
2587
|
-
};
|
|
2588
|
-
window.logsOverriddenForUnreal = true;
|
|
2958
|
+
_checkProvider(_operation) {
|
|
2959
|
+
// We always have a provider, so this is a noop
|
|
2589
2960
|
}
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
return things.map(a => typeof a === 'object' ? a instanceof Error ? a.message : JSON.stringify(a) : String(a)).join(' ');
|
|
2593
|
-
}
|
|
2594
|
-
|
|
2595
|
-
let registeredUnrealMessageProvider;
|
|
2596
|
-
|
|
2597
|
-
// all lowercase is an annoying limitation of Unreal CEF BindUObject
|
|
2598
|
-
|
|
2599
|
-
/**
|
|
2600
|
-
* Initialized on dApp side
|
|
2601
|
-
*/
|
|
2602
|
-
class UnrealMessageProvider extends BaseProviderTransport {
|
|
2603
|
-
constructor(walletAppURL) {
|
|
2604
|
-
super();
|
|
2605
|
-
this.register = () => {
|
|
2606
|
-
var _window$ue;
|
|
2607
|
-
overrideLogs('dapp');
|
|
2608
|
-
if (registeredUnrealMessageProvider) {
|
|
2609
|
-
// overriding the registered message provider
|
|
2610
|
-
registeredUnrealMessageProvider.unregister();
|
|
2611
|
-
registeredUnrealMessageProvider = this;
|
|
2612
|
-
}
|
|
2613
|
-
|
|
2614
|
-
// listen for incoming messages from wallet
|
|
2615
|
-
if ((_window$ue = window.ue) != null && _window$ue.sequencewallettransport) {
|
|
2616
|
-
window.ue.sequencewallettransport.onmessagefromwallet = this.onUnrealCallback;
|
|
2617
|
-
}
|
|
2618
|
-
registeredUnrealMessageProvider = this;
|
|
2619
|
-
this._registered = true;
|
|
2620
|
-
console.log('registering transport!');
|
|
2621
|
-
};
|
|
2622
|
-
this.unregister = () => {
|
|
2623
|
-
var _window$ue2, _window$ue2$sequencew;
|
|
2624
|
-
this._registered = false;
|
|
2625
|
-
this.closeWallet();
|
|
2626
|
-
|
|
2627
|
-
// disable message listener
|
|
2628
|
-
if (registeredUnrealMessageProvider === this) {
|
|
2629
|
-
registeredUnrealMessageProvider = undefined;
|
|
2630
|
-
}
|
|
2631
|
-
if (((_window$ue2 = window.ue) == null ? void 0 : (_window$ue2$sequencew = _window$ue2.sequencewallettransport) == null ? void 0 : _window$ue2$sequencew.onmessagefromwallet) === this.onUnrealCallback) {
|
|
2632
|
-
delete window.ue.sequencewallettransport.onmessagefromwallet;
|
|
2633
|
-
}
|
|
2634
|
-
|
|
2635
|
-
// clear event listeners
|
|
2636
|
-
this.events.removeAllListeners();
|
|
2637
|
-
};
|
|
2638
|
-
this.openWallet = (path, intent, networkId) => {
|
|
2639
|
-
if (this.isOpened()) {
|
|
2640
|
-
// TODO focus wallet
|
|
2641
|
-
console.log('wallet already open!');
|
|
2642
|
-
return;
|
|
2643
|
-
}
|
|
2644
|
-
console.log('opening wallet!');
|
|
2645
|
-
// Instantiate new walletURL for this call
|
|
2646
|
-
const walletURL = new URL(this.walletURL.href);
|
|
2647
|
-
const windowSessionParams = new WindowSessionParams();
|
|
2648
|
-
if (path) {
|
|
2649
|
-
walletURL.pathname = path.toLowerCase();
|
|
2650
|
-
}
|
|
2651
|
-
|
|
2652
|
-
// Set session, intent and network id on walletURL
|
|
2653
|
-
this._init = InitState.NIL;
|
|
2654
|
-
this._sessionId = `${performance.now()}`;
|
|
2655
|
-
windowSessionParams.set('sid', this._sessionId);
|
|
2656
|
-
if (intent) {
|
|
2657
|
-
// encode intent as base64 url-encoded param
|
|
2658
|
-
windowSessionParams.set('intent', base64EncodeObject(intent));
|
|
2659
|
-
}
|
|
2660
|
-
if (networkId) {
|
|
2661
|
-
windowSessionParams.set('net', `${networkId}`);
|
|
2662
|
-
}
|
|
2663
|
-
// serialize params
|
|
2664
|
-
walletURL.search = windowSessionParams.toString();
|
|
2665
|
-
console.log('opening wallet to', walletURL.href);
|
|
2666
|
-
window.open(walletURL.href);
|
|
2667
|
-
};
|
|
2668
|
-
// onmessage, receives ProviderMessageResponse from the wallet unreal transport
|
|
2669
|
-
this.onUnrealCallback = message => {
|
|
2670
|
-
if (!message) {
|
|
2671
|
-
throw new Error('ProviderMessage object is empty');
|
|
2672
|
-
}
|
|
2673
|
-
|
|
2674
|
-
// handle message with base message provider
|
|
2675
|
-
this.handleMessage(message);
|
|
2676
|
-
};
|
|
2677
|
-
this.walletURL = new URL(walletAppURL);
|
|
2961
|
+
populateTransaction(_transaction) {
|
|
2962
|
+
throw new Error("SequenceSigner does not support populateTransaction");
|
|
2678
2963
|
}
|
|
2679
|
-
|
|
2680
|
-
|
|
2964
|
+
checkTransaction(_transaction) {
|
|
2965
|
+
throw new Error("SequenceSigner does not support checkTransaction");
|
|
2681
2966
|
}
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
console.log('Sending message to wallet:', postedMessage);
|
|
2687
|
-
(_window$ue3 = window.ue) == null ? void 0 : (_window$ue3$sequencew = _window$ue3.sequencewallettransport) == null ? void 0 : _window$ue3$sequencew.sendmessagetowallet(postedMessage);
|
|
2967
|
+
getTransactionCount(_blockTag) {
|
|
2968
|
+
// We could try returning the sequence nonce here
|
|
2969
|
+
// but we aren't sure how ethers will use this nonce
|
|
2970
|
+
throw new Error("SequenceSigner does not support getTransactionCount");
|
|
2688
2971
|
}
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
//
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
super(walletRequestHandler);
|
|
2699
|
-
// onmessage is called when (the wallet) receives request messages from the dapp
|
|
2700
|
-
// over the unreal json-messaging transport
|
|
2701
|
-
this.onMessageFromUnreal = request => {
|
|
2702
|
-
// Wallet always expects json-rpc request messages from a dapp
|
|
2703
|
-
|
|
2704
|
-
logger.debug('RECEIVED MESSAGE', request);
|
|
2705
|
-
|
|
2706
|
-
// Handle message via the base transport
|
|
2707
|
-
this.handleMessage(request);
|
|
2708
|
-
};
|
|
2709
|
-
this.getUnrealTransportSession = windowParams => {
|
|
2710
|
-
const params = new WindowSessionParams(windowParams);
|
|
2711
|
-
return {
|
|
2712
|
-
sessionId: params.get('sid'),
|
|
2713
|
-
networkId: params.get('net'),
|
|
2714
|
-
intent: base64DecodeObject(params.get('intent'))
|
|
2715
|
-
};
|
|
2716
|
-
};
|
|
2717
|
-
this._init = InitState.NIL;
|
|
2972
|
+
signTransaction(_transaction) {
|
|
2973
|
+
// We could implement signTransaction/sendTransaction here
|
|
2974
|
+
// but first we need a way of serializing these signed transactions
|
|
2975
|
+
// and it could lead to more trouble, because the dapp could try to send this transaction
|
|
2976
|
+
// using a different provider, which would fail.
|
|
2977
|
+
throw new Error("SequenceWallet does not support signTransaction, use sendTransaction instead.");
|
|
2978
|
+
}
|
|
2979
|
+
static is(cand) {
|
|
2980
|
+
return cand && typeof cand === "object" && cand._isSequenceSigner === true;
|
|
2718
2981
|
}
|
|
2719
|
-
|
|
2720
|
-
var _window$ue;
|
|
2721
|
-
if (((_window$ue = window.ue) == null ? void 0 : _window$ue.sequencewallettransport) === undefined) {
|
|
2722
|
-
return;
|
|
2723
|
-
}
|
|
2724
|
-
overrideLogs('wallet');
|
|
2725
|
-
|
|
2726
|
-
// record open details (sessionId + default network) from the window url
|
|
2727
|
-
const {
|
|
2728
|
-
search: rawParams
|
|
2729
|
-
} = new URL(windowHref || window.location.href);
|
|
2730
|
-
let session = this.getUnrealTransportSession(rawParams);
|
|
2982
|
+
}
|
|
2731
2983
|
|
|
2732
|
-
|
|
2733
|
-
|
|
2984
|
+
/**
|
|
2985
|
+
* This is the same provider, but it only allows a single network at a time.
|
|
2986
|
+
* the network defined by the constructor is the only one that can be used.
|
|
2987
|
+
*
|
|
2988
|
+
* Attempting to call any method with a different network will throw an error.
|
|
2989
|
+
* Attempting to change the network of this provider will throw an error.
|
|
2990
|
+
*
|
|
2991
|
+
* NOTICE: These networks won't support ENS unless they are the mainnet.
|
|
2992
|
+
*/
|
|
2993
|
+
class SingleNetworkSequenceSigner extends SequenceSigner {
|
|
2994
|
+
constructor(client, provider, chainId) {
|
|
2995
|
+
super(client, provider.getProvider(chainId));
|
|
2996
|
+
this._isSingleNetworkSequenceSigner = true;
|
|
2997
|
+
this.chainId = chainId;
|
|
2998
|
+
}
|
|
2999
|
+
_useChainId(chainId) {
|
|
3000
|
+
const provided = this.provider.toChainId(chainId);
|
|
3001
|
+
if (provided && provided !== this.chainId) {
|
|
3002
|
+
throw new Error(`This signer only supports the network ${this.chainId}, but ${provided} was requested.`);
|
|
3003
|
+
}
|
|
3004
|
+
return provided || this.provider.toChainId(this.chainId);
|
|
3005
|
+
}
|
|
3006
|
+
useChainId(chainId) {
|
|
3007
|
+
return this._useChainId(chainId);
|
|
3008
|
+
}
|
|
3009
|
+
getChainId() {
|
|
3010
|
+
return Promise.resolve(this.provider.toChainId(this.chainId));
|
|
3011
|
+
}
|
|
2734
3012
|
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
3013
|
+
/**
|
|
3014
|
+
* Override getProvider and getSigner so they always use `useChainId`
|
|
3015
|
+
* this way they can't return providers and signers that can switch networks,
|
|
3016
|
+
* or that don't match the chainId of this signer.
|
|
3017
|
+
*/
|
|
3018
|
+
getProvider(chainId) {
|
|
3019
|
+
return super.getProvider(this._useChainId(chainId));
|
|
3020
|
+
}
|
|
3021
|
+
getSigner(chainId) {
|
|
3022
|
+
if (this._useChainId(chainId) !== this.chainId) {
|
|
3023
|
+
throw new Error(`Unreachable code`);
|
|
2738
3024
|
}
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
3025
|
+
return this;
|
|
3026
|
+
}
|
|
3027
|
+
static is(cand) {
|
|
3028
|
+
return cand && typeof cand === "object" && cand._isSingleNetworkSequenceSigner === true;
|
|
3029
|
+
}
|
|
3030
|
+
}
|
|
3031
|
+
|
|
3032
|
+
/**
|
|
3033
|
+
* This class is redundant with the SequenceSigner class, but it is here for now to
|
|
3034
|
+
* maintain compatibility with the old wallet API. Eventually we should move these
|
|
3035
|
+
* methods to the SequenceSigner class and deprecate this class.
|
|
3036
|
+
*/
|
|
3037
|
+
class WalletUtils {
|
|
3038
|
+
constructor(signer) {
|
|
3039
|
+
this.signer = signer;
|
|
3040
|
+
if (SingleNetworkSequenceSigner.is(signer)) {
|
|
3041
|
+
throw new Error('WalletUtils does not support SingleNetworkSequenceSigner');
|
|
2742
3042
|
}
|
|
3043
|
+
}
|
|
2743
3044
|
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
this.
|
|
3045
|
+
// Sign message on a specified chain, or DefaultChain by default
|
|
3046
|
+
signMessage(message, chainId, eip6492) {
|
|
3047
|
+
return this.signer.signMessage(message, {
|
|
3048
|
+
chainId,
|
|
3049
|
+
eip6492
|
|
3050
|
+
});
|
|
3051
|
+
}
|
|
2747
3052
|
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
logger.error(err);
|
|
2754
|
-
this.notifyClose({
|
|
2755
|
-
message: err
|
|
2756
|
-
});
|
|
2757
|
-
window.close();
|
|
2758
|
-
}
|
|
2759
|
-
}).catch(e => {
|
|
2760
|
-
var _session2;
|
|
2761
|
-
const err = `failed to open to network ${(_session2 = session) == null ? void 0 : _session2.networkId}, due to: ${e}`;
|
|
2762
|
-
logger.error(err);
|
|
2763
|
-
this.notifyClose({
|
|
2764
|
-
message: err
|
|
2765
|
-
});
|
|
2766
|
-
window.close();
|
|
3053
|
+
// Sign EIP-712 TypedData on a specified chain, or DefaultChain by default
|
|
3054
|
+
signTypedData(domain, types, message, chainId, eip6492) {
|
|
3055
|
+
return this.signer.signTypedData(domain, types, message, {
|
|
3056
|
+
chainId,
|
|
3057
|
+
eip6492
|
|
2767
3058
|
});
|
|
2768
3059
|
}
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
}
|
|
2774
|
-
this._registered = false;
|
|
3060
|
+
|
|
3061
|
+
// Verify signature of a digest, one of a message, typedData or other
|
|
3062
|
+
async isValidSignature(address, digest, signature, chainId) {
|
|
3063
|
+
return isValidSignature(address, digest, signature, this.signer.getProvider(chainId));
|
|
2775
3064
|
}
|
|
2776
|
-
// sendMessage sends message to the dapp window
|
|
2777
|
-
sendMessage(message) {
|
|
2778
|
-
var _window$ue3, _window$ue3$sequencew;
|
|
2779
|
-
if (message.type !== EventType.INIT && this._init !== InitState.OK) {
|
|
2780
|
-
logger.error('impossible state, should not be calling postMessage until inited');
|
|
2781
|
-
return;
|
|
2782
|
-
}
|
|
2783
|
-
// prepare payload
|
|
2784
|
-
const payload = JSON.stringify(message);
|
|
2785
3065
|
|
|
2786
|
-
|
|
2787
|
-
|
|
3066
|
+
// Verify message signature
|
|
3067
|
+
async isValidMessageSignature(address, message, signature, chainId) {
|
|
3068
|
+
const provider = this.signer.getProvider(chainId);
|
|
3069
|
+
const prefixed = prefixEIP191Message(message);
|
|
3070
|
+
const digest = encodeMessageDigest(prefixed);
|
|
3071
|
+
return isValidSignature(address, digest, signature, provider);
|
|
2788
3072
|
}
|
|
2789
|
-
}
|
|
2790
3073
|
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
return
|
|
3074
|
+
// Verify typedData signature
|
|
3075
|
+
isValidTypedDataSignature(address, typedData, signature, chainId) {
|
|
3076
|
+
return this.isValidSignature(address, encodeTypedDataDigest(typedData), signature, chainId);
|
|
2794
3077
|
}
|
|
2795
3078
|
|
|
2796
|
-
//
|
|
2797
|
-
//
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
3079
|
+
// sendTransaction()
|
|
3080
|
+
// sendTransactions()
|
|
3081
|
+
|
|
3082
|
+
// sendETH()
|
|
3083
|
+
// sendToken()
|
|
3084
|
+
// sendCoin() -- sugar for sendToken()
|
|
3085
|
+
// sendCollectible() -- sugar for sendToken()
|
|
3086
|
+
// callContract()
|
|
3087
|
+
|
|
3088
|
+
// transactionHistory()
|
|
3089
|
+
// getReceipt()
|
|
3090
|
+
// getLogs()
|
|
3091
|
+
// // ..
|
|
3092
|
+
|
|
3093
|
+
// validateSignature()
|
|
3094
|
+
// recoverWalletConfig()
|
|
3095
|
+
// recoverAddress()
|
|
3096
|
+
}
|
|
3097
|
+
|
|
3098
|
+
class SequenceProvider extends ethers.providers.BaseProvider {
|
|
3099
|
+
constructor(client, providerFor, networks = allNetworks) {
|
|
3100
|
+
// We support a lot of networks
|
|
3101
|
+
// but we start with the default one
|
|
3102
|
+
super(client.getChainId());
|
|
3103
|
+
|
|
3104
|
+
// Emit events as defined by EIP-1193
|
|
3105
|
+
this.singleNetworkProviders = {};
|
|
2803
3106
|
this._isSequenceProvider = true;
|
|
2804
|
-
this.
|
|
3107
|
+
this.client = client;
|
|
3108
|
+
this.providerFor = providerFor;
|
|
3109
|
+
this.networks = networks;
|
|
3110
|
+
client.onConnect(details => {
|
|
3111
|
+
this.emit('connect', details);
|
|
3112
|
+
});
|
|
3113
|
+
client.onDisconnect(error => {
|
|
3114
|
+
this.emit('disconnect', error);
|
|
3115
|
+
});
|
|
3116
|
+
client.onDefaultChainIdChanged(chainId => {
|
|
3117
|
+
this.emit('chainChanged', chainId);
|
|
3118
|
+
});
|
|
3119
|
+
client.onAccountsChanged(accounts => {
|
|
3120
|
+
this.emit('accountsChanged', accounts);
|
|
3121
|
+
});
|
|
3122
|
+
|
|
3123
|
+
// NOTICE: We don't emit 'open' and 'close' events
|
|
3124
|
+
// because these are handled by the library, and they
|
|
3125
|
+
// are not part of EIP-1193
|
|
3126
|
+
|
|
3127
|
+
// devs can still access them using
|
|
3128
|
+
// client.onOpen()
|
|
3129
|
+
// client.onClose()
|
|
3130
|
+
|
|
3131
|
+
// Create a Sequence signer too
|
|
3132
|
+
this.signer = new SequenceSigner(this.client, this);
|
|
3133
|
+
|
|
3134
|
+
// Create a utils instance
|
|
3135
|
+
this.utils = new WalletUtils(this.signer);
|
|
2805
3136
|
}
|
|
2806
|
-
|
|
2807
|
-
this.
|
|
3137
|
+
getSigner(chainId) {
|
|
3138
|
+
return this.signer.getSigner(chainId);
|
|
2808
3139
|
}
|
|
2809
|
-
|
|
2810
|
-
return this.
|
|
3140
|
+
connect(options) {
|
|
3141
|
+
return this.client.connect(options);
|
|
2811
3142
|
}
|
|
2812
|
-
|
|
2813
|
-
return this.
|
|
3143
|
+
disconnect() {
|
|
3144
|
+
return this.client.disconnect();
|
|
2814
3145
|
}
|
|
2815
|
-
|
|
2816
|
-
return
|
|
3146
|
+
isConnected() {
|
|
3147
|
+
return this.client.isConnected();
|
|
3148
|
+
}
|
|
3149
|
+
getSession() {
|
|
3150
|
+
return this.client.getSession();
|
|
3151
|
+
}
|
|
3152
|
+
listAccounts() {
|
|
3153
|
+
return [this.client.getAddress()];
|
|
2817
3154
|
}
|
|
2818
|
-
async getChainId() {
|
|
2819
|
-
// If we already have a default chainId, then we can just return it
|
|
2820
|
-
const defaultChainId = this._defaultChainId;
|
|
2821
|
-
if (defaultChainId) return defaultChainId;
|
|
2822
3155
|
|
|
2823
|
-
|
|
2824
|
-
|
|
3156
|
+
// @deprecated use getSigner() instead
|
|
3157
|
+
getAddress() {
|
|
3158
|
+
return this.client.getAddress();
|
|
2825
3159
|
}
|
|
2826
3160
|
getNetworks() {
|
|
2827
|
-
return this.
|
|
3161
|
+
return this.client.getNetworks();
|
|
2828
3162
|
}
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
const cand = provider;
|
|
2832
|
-
return cand && cand.send !== undefined && cand._isSequenceProvider === true;
|
|
2833
|
-
}
|
|
2834
|
-
class LocalWeb3Provider extends Web3Provider {
|
|
2835
|
-
constructor(account, networks) {
|
|
2836
|
-
const walletRequestHandler = new WalletRequestHandler(account, null, networks || []);
|
|
2837
|
-
super(walletRequestHandler);
|
|
3163
|
+
getChainId() {
|
|
3164
|
+
return this.client.getChainId();
|
|
2838
3165
|
}
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
this.
|
|
2847
|
-
|
|
2848
|
-
|
|
3166
|
+
setDefaultChainId(chainId) {
|
|
3167
|
+
return this.client.setDefaultChainId(this.toChainId(chainId));
|
|
3168
|
+
}
|
|
3169
|
+
isOpened() {
|
|
3170
|
+
return this.client.isOpened();
|
|
3171
|
+
}
|
|
3172
|
+
closeWallet() {
|
|
3173
|
+
return this.client.closeWallet();
|
|
3174
|
+
}
|
|
3175
|
+
getWalletContext() {
|
|
3176
|
+
return this.client.getWalletContext();
|
|
2849
3177
|
}
|
|
2850
3178
|
|
|
2851
|
-
//
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
if (this._address) return this._address;
|
|
2858
|
-
const accounts = await this.provider.send('eth_accounts', []);
|
|
2859
|
-
this._address = accounts[0];
|
|
2860
|
-
this._index = 0;
|
|
2861
|
-
return ethers.utils.getAddress(this._address);
|
|
3179
|
+
// @deprecated use getSigner() instead
|
|
3180
|
+
async getWalletConfig(chainId) {
|
|
3181
|
+
const useChainId = await this.useChainId(chainId);
|
|
3182
|
+
return this.client.getOnchainWalletConfig({
|
|
3183
|
+
chainId: useChainId
|
|
3184
|
+
});
|
|
2862
3185
|
}
|
|
2863
|
-
|
|
2864
|
-
//
|
|
2865
|
-
|
|
3186
|
+
authorize(options) {
|
|
3187
|
+
// Just an alias for connect with authorize: true
|
|
3188
|
+
return this.client.connect(_extends({}, options, {
|
|
3189
|
+
authorize: true
|
|
3190
|
+
}));
|
|
2866
3191
|
}
|
|
2867
|
-
|
|
2868
|
-
|
|
3192
|
+
async openWallet(path, intent) {
|
|
3193
|
+
await this.client.openWallet(path, intent);
|
|
3194
|
+
return true;
|
|
3195
|
+
}
|
|
3196
|
+
toChainId(chainId) {
|
|
3197
|
+
if (chainId === undefined) {
|
|
3198
|
+
return undefined;
|
|
3199
|
+
}
|
|
3200
|
+
const resolved = findNetworkConfig(this.networks, chainId);
|
|
3201
|
+
if (!resolved) {
|
|
3202
|
+
throw new Error(`Unsupported network ${chainId}`);
|
|
3203
|
+
}
|
|
3204
|
+
return resolved.chainId;
|
|
2869
3205
|
}
|
|
2870
3206
|
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
3207
|
+
/**
|
|
3208
|
+
* Resolves the chainId to use for the given request. If no chainId is provided,
|
|
3209
|
+
* it uses the chainId defined by the client (default chainId). This can be
|
|
3210
|
+
* overriden to build a single-network SequenceProvider.
|
|
3211
|
+
*/
|
|
3212
|
+
async useChainId(chainId) {
|
|
3213
|
+
return this.toChainId(chainId) || this.client.getChainId();
|
|
3214
|
+
}
|
|
2874
3215
|
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
3216
|
+
/**
|
|
3217
|
+
* This generates a provider that ONLY works for the given chainId.
|
|
3218
|
+
* the generated provider can't switch networks, and can't handle requests
|
|
3219
|
+
* for other networks.
|
|
3220
|
+
*/
|
|
3221
|
+
|
|
3222
|
+
getProvider(chainId) {
|
|
3223
|
+
// The provider without a chainId is... this one
|
|
3224
|
+
if (!chainId) {
|
|
3225
|
+
return this;
|
|
2884
3226
|
}
|
|
2885
|
-
|
|
3227
|
+
const useChainId = this.toChainId(chainId);
|
|
3228
|
+
if (!this.singleNetworkProviders[useChainId]) {
|
|
3229
|
+
this.singleNetworkProviders[useChainId] = new SingleNetworkSequenceProvider(this.client, this.providerFor, useChainId);
|
|
3230
|
+
}
|
|
3231
|
+
return this.singleNetworkProviders[useChainId];
|
|
2886
3232
|
}
|
|
2887
3233
|
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
this._providers[chainId] = new Web3Provider(new JsonRpcSender(this.provider, chainId), chainId);
|
|
3234
|
+
/**
|
|
3235
|
+
* This returns a subprovider, this is a regular non-sequence provider that
|
|
3236
|
+
* can be used to fulfill read only requests on a given network.
|
|
3237
|
+
*/
|
|
3238
|
+
async _getSubprovider(chainId) {
|
|
3239
|
+
const useChainId = await this.useChainId(chainId);
|
|
3240
|
+
|
|
3241
|
+
// Whoever implements providerFrom should memoize the generated provider
|
|
3242
|
+
// otherwise every instance of SequenceProvider will create a new subprovider
|
|
3243
|
+
const provider = this.providerFor(useChainId);
|
|
3244
|
+
if (!provider) {
|
|
3245
|
+
throw new Error(`Unsupported network ${useChainId}`);
|
|
2901
3246
|
}
|
|
2902
|
-
return
|
|
3247
|
+
return provider;
|
|
2903
3248
|
}
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
//
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
3249
|
+
async perform(method, params) {
|
|
3250
|
+
var _provider$prepareRequ;
|
|
3251
|
+
// First we check if the method should be handled by the client
|
|
3252
|
+
if (method === 'eth_chainId') {
|
|
3253
|
+
return this.useChainId();
|
|
3254
|
+
}
|
|
3255
|
+
if (method === 'eth_accounts') {
|
|
3256
|
+
return [this.client.getAddress()];
|
|
3257
|
+
}
|
|
3258
|
+
if (method === 'wallet_switchEthereumChain') {
|
|
3259
|
+
const args = params[0];
|
|
3260
|
+
const chainId = normalizeChainId(args);
|
|
3261
|
+
return this.setDefaultChainId(chainId);
|
|
3262
|
+
}
|
|
3263
|
+
|
|
3264
|
+
// Usually these methods aren't used by calling the provider
|
|
3265
|
+
// but to maximize compatibility we support them too.
|
|
3266
|
+
// The correct way of accessing these methods is by using .getSigner()
|
|
3267
|
+
if (method === 'eth_sendTransaction' || method === 'eth_sign' || method === 'eth_signTypedData' || method === 'eth_signTypedData_v4' || method === 'personal_sign' ||
|
|
3268
|
+
// These methods will use EIP-6492
|
|
3269
|
+
// but this is handled directly by the wallet
|
|
3270
|
+
method === 'sequence_sign' || method === 'sequence_signTypedData_v4') {
|
|
3271
|
+
// We pass the chainId to the client, if we don't pass one
|
|
3272
|
+
// the client will use its own default chainId
|
|
3273
|
+
return this.client.send({
|
|
3274
|
+
method,
|
|
3275
|
+
params
|
|
3276
|
+
}, this.getChainId());
|
|
3277
|
+
}
|
|
3278
|
+
|
|
3279
|
+
// Forward call to the corresponding provider
|
|
3280
|
+
// we use the provided chainId, or the default one provided by the client
|
|
3281
|
+
const provider = await this._getSubprovider();
|
|
3282
|
+
const prepared = (_provider$prepareRequ = provider.prepareRequest(method, params)) != null ? _provider$prepareRequ : [method, params];
|
|
3283
|
+
return provider.send(prepared[0], prepared[1]);
|
|
3284
|
+
}
|
|
3285
|
+
send(method, params) {
|
|
3286
|
+
return this.perform(method, params);
|
|
2910
3287
|
}
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
3288
|
+
request(request) {
|
|
3289
|
+
return this.perform(request.method, request.params);
|
|
3290
|
+
}
|
|
3291
|
+
async detectNetwork() {
|
|
3292
|
+
const chainId = this.client.getChainId();
|
|
3293
|
+
const network = findNetworkConfig(this.networks, chainId);
|
|
3294
|
+
if (!network) {
|
|
3295
|
+
throw new Error(`Unknown network ${chainId}`);
|
|
2914
3296
|
}
|
|
2915
|
-
return
|
|
3297
|
+
return network;
|
|
2916
3298
|
}
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
3299
|
+
|
|
3300
|
+
// Override most of the methods, so we add support for an optional chainId
|
|
3301
|
+
// argument, which is used to select the provider to use.
|
|
3302
|
+
//
|
|
3303
|
+
// NOTICE: We could use generics to avoid repeating the same code
|
|
3304
|
+
// but this would make the code harder to read, and it's not worth it
|
|
3305
|
+
// since we only have a few methods to override.
|
|
3306
|
+
|
|
3307
|
+
async waitForTransaction(transactionHash, confirmations, timeout, optionals) {
|
|
3308
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3309
|
+
return provider.waitForTransaction(transactionHash, confirmations, timeout);
|
|
2921
3310
|
}
|
|
2922
|
-
async
|
|
2923
|
-
const
|
|
2924
|
-
|
|
2925
|
-
return (await this.provider.send('sequence_getWalletState', [reqChainId], reqChainId))[0].status;
|
|
3311
|
+
async getBlockNumber(optionals) {
|
|
3312
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3313
|
+
return provider.getBlockNumber();
|
|
2926
3314
|
}
|
|
2927
|
-
async
|
|
2928
|
-
|
|
2929
|
-
return
|
|
3315
|
+
async getGasPrice(optionals) {
|
|
3316
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3317
|
+
return provider.getGasPrice();
|
|
2930
3318
|
}
|
|
2931
|
-
async
|
|
2932
|
-
const
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
3319
|
+
async getBalance(addressOrName, blockTag, optionals) {
|
|
3320
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3321
|
+
return provider.getBalance(addressOrName, blockTag);
|
|
3322
|
+
}
|
|
3323
|
+
async getTransactionCount(addressOrName, blockTag, optionals) {
|
|
3324
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3325
|
+
return provider.getTransactionCount(addressOrName, blockTag);
|
|
3326
|
+
}
|
|
3327
|
+
async getCode(addressOrName, blockTag, optionals) {
|
|
3328
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3329
|
+
return provider.getCode(addressOrName, blockTag);
|
|
3330
|
+
}
|
|
3331
|
+
async getStorageAt(addressOrName, position, blockTag, optionals) {
|
|
3332
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3333
|
+
return provider.getStorageAt(addressOrName, position, blockTag);
|
|
3334
|
+
}
|
|
3335
|
+
async call(transaction, blockTag, optionals) {
|
|
3336
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3337
|
+
return provider.call(transaction, blockTag);
|
|
3338
|
+
}
|
|
3339
|
+
async estimateGas(transaction, optionals) {
|
|
3340
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3341
|
+
return provider.estimateGas(transaction);
|
|
3342
|
+
}
|
|
3343
|
+
async getBlock(blockHashOrBlockTag, optionals) {
|
|
3344
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3345
|
+
return provider.getBlock(blockHashOrBlockTag);
|
|
3346
|
+
}
|
|
3347
|
+
async getTransaction(transactionHash, optionals) {
|
|
3348
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3349
|
+
return provider.getTransaction(transactionHash);
|
|
3350
|
+
}
|
|
3351
|
+
async getLogs(filter, optionals) {
|
|
3352
|
+
const provider = await this._getSubprovider(optionals == null ? void 0 : optionals.chainId);
|
|
3353
|
+
return provider.getLogs(filter);
|
|
2940
3354
|
}
|
|
2941
3355
|
|
|
2942
|
-
//
|
|
2943
|
-
// multi-chain support.
|
|
2944
|
-
async signMessage(message, chainId, sequenceVerified = true) {
|
|
2945
|
-
const provider = await this.getSender(maybeChainId(chainId) || this.defaultChainId);
|
|
2946
|
-
const data = typeof message === 'string' ? ethers.utils.toUtf8Bytes(message) : message;
|
|
2947
|
-
const address = await this.getAddress();
|
|
3356
|
+
// ENS methods
|
|
2948
3357
|
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
return provider.send(sequenceVerified ? 'sequence_sign' : 'personal_sign', [ethers.utils.hexlify(data), address]);
|
|
3358
|
+
async supportsENS() {
|
|
3359
|
+
const networks = await this.getNetworks();
|
|
3360
|
+
return networks.some(n => n.chainId === 1);
|
|
2953
3361
|
}
|
|
3362
|
+
async getResolver(name) {
|
|
3363
|
+
if (!(await this.supportsENS())) {
|
|
3364
|
+
return null;
|
|
3365
|
+
}
|
|
2954
3366
|
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
3367
|
+
// Resolver is always on the chainId 1
|
|
3368
|
+
const provider = await this._getSubprovider(1);
|
|
3369
|
+
return provider.getResolver(name);
|
|
3370
|
+
}
|
|
3371
|
+
async resolveName(name) {
|
|
3372
|
+
if (ethers.utils.isAddress(await name)) {
|
|
3373
|
+
return name;
|
|
3374
|
+
}
|
|
3375
|
+
if (!(await this.supportsENS())) {
|
|
3376
|
+
return null;
|
|
3377
|
+
}
|
|
2962
3378
|
|
|
2963
|
-
|
|
3379
|
+
// Resolver is always on the chainId 1
|
|
3380
|
+
const provider = await this._getSubprovider(1);
|
|
3381
|
+
return provider.resolveName(name);
|
|
2964
3382
|
}
|
|
3383
|
+
async lookupAddress(address) {
|
|
3384
|
+
if (!(await this.supportsENS())) {
|
|
3385
|
+
return null;
|
|
3386
|
+
}
|
|
2965
3387
|
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
3388
|
+
// Resolver is always on the chainId 1
|
|
3389
|
+
const provider = await this._getSubprovider(1);
|
|
3390
|
+
return provider.lookupAddress(address);
|
|
3391
|
+
}
|
|
3392
|
+
async getAvatar(nameOrAddress) {
|
|
3393
|
+
if (!(await this.supportsENS())) {
|
|
3394
|
+
return null;
|
|
3395
|
+
}
|
|
3396
|
+
const provider = await this._getSubprovider(1);
|
|
3397
|
+
return provider.getAvatar(nameOrAddress);
|
|
3398
|
+
}
|
|
3399
|
+
}
|
|
3400
|
+
SequenceProvider.is = provider => {
|
|
3401
|
+
return provider && typeof provider === 'object' && provider._isSequenceProvider === true;
|
|
3402
|
+
};
|
|
3403
|
+
function normalizeChainId(chainId) {
|
|
3404
|
+
if (typeof chainId === 'object') return normalizeChainId(chainId.chainId);
|
|
3405
|
+
return ethers.BigNumber.from(chainId).toNumber();
|
|
3406
|
+
}
|
|
2985
3407
|
|
|
2986
|
-
|
|
2987
|
-
|
|
3408
|
+
/**
|
|
3409
|
+
* This is the same provider, but it only allows a single network at a time.
|
|
3410
|
+
* the network defined by the constructor is the only one that can be used.
|
|
3411
|
+
*
|
|
3412
|
+
* Attempting to call any method with a different network will throw an error.
|
|
3413
|
+
* Attempting to change the network of this provider will throw an error.
|
|
3414
|
+
*
|
|
3415
|
+
* NOTICE: These networks won't support ENS unless they are the mainnet.
|
|
3416
|
+
*/
|
|
3417
|
+
class SingleNetworkSequenceProvider extends SequenceProvider {
|
|
3418
|
+
constructor(client, providerFor, chainId) {
|
|
3419
|
+
super(client, providerFor);
|
|
3420
|
+
this._isSingleNetworkSequenceProvider = true;
|
|
3421
|
+
this.chainId = chainId;
|
|
3422
|
+
}
|
|
3423
|
+
_useChainId(chainId) {
|
|
3424
|
+
const provided = this.toChainId(chainId);
|
|
3425
|
+
if (provided && provided !== this.chainId) {
|
|
3426
|
+
throw new Error(`This provider only supports the network ${this.chainId}, but ${provided} was requested.`);
|
|
3427
|
+
}
|
|
3428
|
+
return provided || super.toChainId(this.chainId);
|
|
3429
|
+
}
|
|
3430
|
+
useChainId(chainId) {
|
|
3431
|
+
return Promise.resolve(this._useChainId(chainId));
|
|
3432
|
+
}
|
|
3433
|
+
getChainId() {
|
|
3434
|
+
return super.toChainId(this.chainId);
|
|
3435
|
+
}
|
|
3436
|
+
async getNetwork() {
|
|
3437
|
+
const networks = await this.client.getNetworks();
|
|
3438
|
+
const res = findNetworkConfig(networks, this.chainId);
|
|
3439
|
+
if (!res) {
|
|
3440
|
+
throw new Error(`Unsupported network ${this.chainId}`);
|
|
3441
|
+
}
|
|
3442
|
+
return res;
|
|
2988
3443
|
}
|
|
2989
3444
|
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
3445
|
+
/**
|
|
3446
|
+
* Override getProvider and getSigner so they always use `useChainId`
|
|
3447
|
+
* this way they can't return providers and signers that can switch networks,
|
|
3448
|
+
* or that don't match the chainId of this signer.
|
|
3449
|
+
*/
|
|
3450
|
+
getProvider(chainId) {
|
|
3451
|
+
if (this._useChainId(chainId) !== this.chainId) {
|
|
3452
|
+
throw new Error(`Unreachable code`);
|
|
2996
3453
|
}
|
|
3454
|
+
return this;
|
|
3455
|
+
}
|
|
3456
|
+
getSigner(chainId) {
|
|
3457
|
+
return super.getSigner(this._useChainId(chainId));
|
|
3458
|
+
}
|
|
3459
|
+
setDefaultChainId(_chainId) {
|
|
3460
|
+
throw new Error(`This provider only supports the network ${this.chainId}; use the parent provider to switch networks.`);
|
|
3461
|
+
}
|
|
3462
|
+
static is(cand) {
|
|
3463
|
+
return cand && typeof cand === 'object' && cand._isSingleNetworkSequenceProvider === true;
|
|
3464
|
+
}
|
|
3465
|
+
}
|
|
2997
3466
|
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3467
|
+
/**
|
|
3468
|
+
* This session class is meant to persist the state of the wallet connection
|
|
3469
|
+
* whitin the dapp. This enables the client to retain the wallet address (and some more)
|
|
3470
|
+
* even if the user refreshes the page. Otherwise we would have to open the popup again.
|
|
3471
|
+
*/
|
|
3472
|
+
class SequenceClientSession {
|
|
3473
|
+
constructor(store) {
|
|
3474
|
+
this.store = store;
|
|
3475
|
+
}
|
|
3476
|
+
connectedSession() {
|
|
3477
|
+
const session = this.getSession();
|
|
3478
|
+
if (session && session.accountAddress && session.walletContext && session.networks) {
|
|
3479
|
+
return {
|
|
3480
|
+
accountAddress: session.accountAddress,
|
|
3481
|
+
walletContext: session.walletContext,
|
|
3482
|
+
networks: session.networks
|
|
3483
|
+
};
|
|
3484
|
+
}
|
|
3485
|
+
throw new Error('Sequence session not connected');
|
|
3486
|
+
}
|
|
3487
|
+
hasSession() {
|
|
3488
|
+
var _this$getSession;
|
|
3489
|
+
return ((_this$getSession = this.getSession()) == null ? void 0 : _this$getSession.accountAddress) !== undefined;
|
|
3490
|
+
}
|
|
3491
|
+
setSession(session) {
|
|
3492
|
+
return this.store.setItem(SequenceClientSession.SESSION_LOCALSTORE_KEY, JSON.stringify(session));
|
|
3493
|
+
}
|
|
3494
|
+
getSession() {
|
|
3495
|
+
const session = this.store.getItem(SequenceClientSession.SESSION_LOCALSTORE_KEY);
|
|
3496
|
+
if (session) {
|
|
3497
|
+
return JSON.parse(session);
|
|
3001
3498
|
}
|
|
3002
|
-
|
|
3003
|
-
|
|
3499
|
+
return undefined;
|
|
3500
|
+
}
|
|
3501
|
+
async clearSession() {
|
|
3502
|
+
return this.store.removeItem(SequenceClientSession.SESSION_LOCALSTORE_KEY);
|
|
3503
|
+
}
|
|
3504
|
+
}
|
|
3505
|
+
|
|
3506
|
+
/**
|
|
3507
|
+
* The wallet webapp doesn't really care what's the "default chain" for the user.
|
|
3508
|
+
* so we don't even bother to send this information to the wallet. Instead, we
|
|
3509
|
+
* track it locally using storage, that way the data stays always in sync.
|
|
3510
|
+
*/
|
|
3511
|
+
SequenceClientSession.SESSION_LOCALSTORE_KEY = '@sequence.session';
|
|
3512
|
+
class DefaultChainIdTracker {
|
|
3513
|
+
constructor(store, startingChainId = 1) {
|
|
3514
|
+
this.callbacks = [];
|
|
3515
|
+
this.store = store;
|
|
3516
|
+
this.startingChainId = startingChainId;
|
|
3517
|
+
store.onItemChange(DefaultChainIdTracker.SESSION_CHAIN_ID_KEY, value => {
|
|
3518
|
+
if (value) {
|
|
3519
|
+
const chainId = parseInt(value);
|
|
3520
|
+
this.callbacks.forEach(cb => cb(chainId));
|
|
3521
|
+
}
|
|
3522
|
+
});
|
|
3523
|
+
}
|
|
3524
|
+
onDefaultChainIdChanged(callback) {
|
|
3525
|
+
this.callbacks.push(callback);
|
|
3526
|
+
return () => {
|
|
3527
|
+
this.callbacks = this.callbacks.filter(cb => cb !== callback);
|
|
3528
|
+
};
|
|
3004
3529
|
}
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
return this.provider.send('eth_signTransaction', [transaction], maybeChainId(chainId) || this.defaultChainId);
|
|
3530
|
+
setDefaultChainId(chainId) {
|
|
3531
|
+
if (chainId !== this.getDefaultChainId()) {
|
|
3532
|
+
this.store.setItem(DefaultChainIdTracker.SESSION_CHAIN_ID_KEY, chainId.toString());
|
|
3533
|
+
}
|
|
3010
3534
|
}
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3535
|
+
getDefaultChainId() {
|
|
3536
|
+
const read = this.store.getItem(DefaultChainIdTracker.SESSION_CHAIN_ID_KEY);
|
|
3537
|
+
if (!read || read.length === 0) {
|
|
3538
|
+
return this.startingChainId;
|
|
3539
|
+
}
|
|
3540
|
+
return parseInt(read);
|
|
3014
3541
|
}
|
|
3542
|
+
}
|
|
3543
|
+
|
|
3544
|
+
/**
|
|
3545
|
+
* This is a wallet client for sequence wallet-webapp. It connects using *some* transport
|
|
3546
|
+
* and it allows to perform all sequence specific (or write) operations related to the wallet.
|
|
3547
|
+
*
|
|
3548
|
+
* It doesn't implement a full ethereum Provider, it doesn't include read-only methods.
|
|
3549
|
+
*/
|
|
3550
|
+
DefaultChainIdTracker.SESSION_CHAIN_ID_KEY = '@sequence.session.defaultChainId';
|
|
3551
|
+
class SequenceClient {
|
|
3552
|
+
constructor(transport, store, defaultChainId) {
|
|
3553
|
+
this.callbacks = {};
|
|
3554
|
+
if (isMuxTransportTemplate(transport)) {
|
|
3555
|
+
this.transport = MuxMessageProvider.new(transport);
|
|
3556
|
+
} else if (isProviderTransport(transport)) {
|
|
3557
|
+
this.transport = transport;
|
|
3558
|
+
} else {
|
|
3559
|
+
throw new Error('Invalid transport');
|
|
3560
|
+
}
|
|
3561
|
+
this.session = new SequenceClientSession(store);
|
|
3562
|
+
this.defaultChainId = new DefaultChainIdTracker(store, defaultChainId);
|
|
3563
|
+
this.transport.on('accountsChanged', accounts => {
|
|
3564
|
+
var _this$callbacks$accou;
|
|
3565
|
+
if (accounts.length > 1) {
|
|
3566
|
+
console.warn('SequenceClient: wallet-webapp returned more than one account');
|
|
3567
|
+
}
|
|
3568
|
+
(_this$callbacks$accou = this.callbacks.accountsChanged) == null ? void 0 : _this$callbacks$accou.forEach(cb => cb(accounts));
|
|
3569
|
+
});
|
|
3570
|
+
this.transport.on('connect', response => {
|
|
3571
|
+
var _this$callbacks$conne;
|
|
3572
|
+
const chainIdHex = ethers.BigNumber.from(this.getChainId()).toHexString();
|
|
3573
|
+
(_this$callbacks$conne = this.callbacks.connect) == null ? void 0 : _this$callbacks$conne.forEach(cb => cb(_extends({}, response, {
|
|
3574
|
+
// Ignore the full connect response
|
|
3575
|
+
// use the chainId defined locally
|
|
3576
|
+
chainId: chainIdHex
|
|
3577
|
+
})));
|
|
3578
|
+
});
|
|
3579
|
+
this.transport.on('disconnect', error => {
|
|
3580
|
+
var _this$callbacks$disco;
|
|
3581
|
+
(_this$callbacks$disco = this.callbacks.disconnect) == null ? void 0 : _this$callbacks$disco.forEach(cb => cb(error));
|
|
3582
|
+
});
|
|
3583
|
+
this.transport.on('networks', networks => {
|
|
3584
|
+
var _this$callbacks$netwo;
|
|
3585
|
+
(_this$callbacks$netwo = this.callbacks.networks) == null ? void 0 : _this$callbacks$netwo.forEach(cb => cb(networks));
|
|
3586
|
+
});
|
|
3587
|
+
this.transport.on('walletContext', context => {
|
|
3588
|
+
var _this$callbacks$walle;
|
|
3589
|
+
(_this$callbacks$walle = this.callbacks.walletContext) == null ? void 0 : _this$callbacks$walle.forEach(cb => cb(context));
|
|
3590
|
+
});
|
|
3591
|
+
this.transport.on('open', info => {
|
|
3592
|
+
var _this$callbacks$open;
|
|
3593
|
+
(_this$callbacks$open = this.callbacks.open) == null ? void 0 : _this$callbacks$open.forEach(cb => cb(info));
|
|
3594
|
+
});
|
|
3595
|
+
this.transport.on('close', () => {
|
|
3596
|
+
var _this$callbacks$close;
|
|
3597
|
+
(_this$callbacks$close = this.callbacks.close) == null ? void 0 : _this$callbacks$close.forEach(cb => cb());
|
|
3598
|
+
});
|
|
3015
3599
|
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
}
|
|
3024
|
-
const provider = await this.getSender(this.defaultChainId);
|
|
3025
|
-
return [config, provider._wrapTransaction(tx, tx.hash)];
|
|
3600
|
+
// We don't listen for the transport chainChanged event
|
|
3601
|
+
// instead we handle it locally, so we listen for changes in the store
|
|
3602
|
+
this.defaultChainId.onDefaultChainIdChanged(chainId => {
|
|
3603
|
+
var _this$callbacks$chain;
|
|
3604
|
+
const chainIdHex = ethers.BigNumber.from(chainId).toHexString();
|
|
3605
|
+
(_this$callbacks$chain = this.callbacks.chainChanged) == null ? void 0 : _this$callbacks$chain.forEach(cb => cb(chainIdHex));
|
|
3606
|
+
});
|
|
3026
3607
|
}
|
|
3027
3608
|
|
|
3028
|
-
//
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
if (tx === null) {
|
|
3034
|
-
return undefined;
|
|
3609
|
+
// Callbacks
|
|
3610
|
+
|
|
3611
|
+
registerCallback(eventName, callback) {
|
|
3612
|
+
if (!this.callbacks[eventName]) {
|
|
3613
|
+
this.callbacks[eventName] = [];
|
|
3035
3614
|
}
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
const walletCode = await provider.getCode(await this.getAddress());
|
|
3041
|
-
return !!walletCode && walletCode !== '0x';
|
|
3615
|
+
this.callbacks[eventName].push(callback);
|
|
3616
|
+
return () => {
|
|
3617
|
+
this.callbacks[eventName] = this.callbacks[eventName].filter(c => c !== callback);
|
|
3618
|
+
};
|
|
3042
3619
|
}
|
|
3043
3620
|
|
|
3044
|
-
//
|
|
3045
|
-
// ethers JsonRpcSigner methods
|
|
3046
|
-
//
|
|
3047
|
-
|
|
3048
|
-
async _legacySignMessage(message, chainId) {
|
|
3049
|
-
const provider = await this.getSender(maybeChainId(chainId) || this.defaultChainId);
|
|
3050
|
-
const data = typeof message === 'string' ? ethers.utils.toUtf8Bytes(message) : message;
|
|
3051
|
-
const address = await this.getAddress();
|
|
3621
|
+
// Individual callbacks lead to more idiomatic code
|
|
3052
3622
|
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
return await provider.send('eth_sign', [address, ethers.utils.hexlify(data)]);
|
|
3623
|
+
onOpen(callback) {
|
|
3624
|
+
return this.registerCallback('open', callback);
|
|
3056
3625
|
}
|
|
3057
|
-
|
|
3058
|
-
return this.
|
|
3626
|
+
onClose(callback) {
|
|
3627
|
+
return this.registerCallback('close', callback);
|
|
3059
3628
|
}
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
const fromAddress = this.getAddress();
|
|
3063
|
-
|
|
3064
|
-
// NOTE: we do not use provider estimation, and instead rely on our relayer to determine the gasLimit and gasPrice
|
|
3065
|
-
//
|
|
3066
|
-
// TODO: alternatively/one day, we could write a provider middleware to eth_estimateGas
|
|
3067
|
-
// and send it to our relayer url instead for estimation..
|
|
3068
|
-
//
|
|
3069
|
-
// if (!transaction.gasLimit) {
|
|
3070
|
-
// const estimate = shallowCopy(transaction)
|
|
3071
|
-
// estimate.from = fromAddress
|
|
3072
|
-
// transaction.gasLimit = this.provider.estimateGas(estimate)
|
|
3073
|
-
// }
|
|
3074
|
-
|
|
3075
|
-
const provider = await this.getSender(maybeChainId(chainId) || this.defaultChainId);
|
|
3076
|
-
return resolveProperties({
|
|
3077
|
-
tx: resolveProperties(transaction),
|
|
3078
|
-
sender: await fromAddress
|
|
3079
|
-
}).then(({
|
|
3080
|
-
tx,
|
|
3081
|
-
sender
|
|
3082
|
-
}) => {
|
|
3083
|
-
if (tx.from != null) {
|
|
3084
|
-
if (ethers.utils.getAddress(tx.from) !== sender) {
|
|
3085
|
-
// logger.throwArgumentError("from address mismatch", "transaction", transaction)
|
|
3086
|
-
throw new Error(`from address mismatch for transaction ${transaction}`);
|
|
3087
|
-
}
|
|
3088
|
-
} else {
|
|
3089
|
-
tx.from = sender;
|
|
3090
|
-
}
|
|
3091
|
-
const hexTx = hexlifyTransaction(tx);
|
|
3092
|
-
return provider.send('eth_sendTransaction', [hexTx]).then(hash => {
|
|
3093
|
-
return hash;
|
|
3094
|
-
}, error => {
|
|
3095
|
-
// return checkError("sendTransaction", error, hexTx)
|
|
3096
|
-
throw error;
|
|
3097
|
-
});
|
|
3098
|
-
});
|
|
3629
|
+
onConnect(callback) {
|
|
3630
|
+
return this.registerCallback('connect', callback);
|
|
3099
3631
|
}
|
|
3100
|
-
|
|
3101
|
-
return
|
|
3632
|
+
onDisconnect(callback) {
|
|
3633
|
+
return this.registerCallback('disconnect', callback);
|
|
3102
3634
|
}
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
return this.provider.send('personal_unlockAccount', [address, password, null]);
|
|
3635
|
+
onNetworks(callback) {
|
|
3636
|
+
return this.registerCallback('networks', callback);
|
|
3106
3637
|
}
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
// NOTE: method has been copied + modified from ethers.js JsonRpcProvider
|
|
3110
|
-
// Convert an ethers.js transaction into a JSON-RPC transaction
|
|
3111
|
-
|
|
3112
|
-
const allowedTransactionKeys = {
|
|
3113
|
-
chainId: true,
|
|
3114
|
-
data: true,
|
|
3115
|
-
gasLimit: true,
|
|
3116
|
-
gasPrice: true,
|
|
3117
|
-
nonce: true,
|
|
3118
|
-
to: true,
|
|
3119
|
-
value: true,
|
|
3120
|
-
from: true,
|
|
3121
|
-
auxiliary: true,
|
|
3122
|
-
expiration: true,
|
|
3123
|
-
afterNonce: true,
|
|
3124
|
-
delegateCall: true,
|
|
3125
|
-
revertOnError: true
|
|
3126
|
-
};
|
|
3127
|
-
const hexlifyTransaction = (transaction, allowExtra) => {
|
|
3128
|
-
// Check only allowed properties are given
|
|
3129
|
-
const allowed = shallowCopy(allowedTransactionKeys);
|
|
3130
|
-
if (allowExtra) {
|
|
3131
|
-
for (const key in allowExtra) {
|
|
3132
|
-
if (allowExtra[key]) {
|
|
3133
|
-
allowed[key] = true;
|
|
3134
|
-
}
|
|
3135
|
-
}
|
|
3638
|
+
onAccountsChanged(callback) {
|
|
3639
|
+
return this.registerCallback('accountsChanged', callback);
|
|
3136
3640
|
}
|
|
3137
|
-
ethers.utils.checkProperties(transaction, allowed);
|
|
3138
|
-
const result = {}
|
|
3139
3641
|
|
|
3140
|
-
//
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
const value = transaction[key];
|
|
3144
|
-
if (value === null || value === undefined) {
|
|
3145
|
-
return;
|
|
3146
|
-
}
|
|
3147
|
-
const hexValue = ethers.utils.hexValue(value);
|
|
3148
|
-
if (key === 'gasLimit') {
|
|
3149
|
-
key = 'gas';
|
|
3150
|
-
}
|
|
3151
|
-
result[key] = hexValue;
|
|
3152
|
-
});
|
|
3153
|
-
['from', 'to', 'data'].forEach(key => {
|
|
3154
|
-
if (!transaction[key]) {
|
|
3155
|
-
return;
|
|
3156
|
-
}
|
|
3157
|
-
result[key] = ethers.utils.hexlify(transaction[key]);
|
|
3158
|
-
});
|
|
3159
|
-
['delegateCall', 'revertOnError'].forEach(key => {
|
|
3160
|
-
const value = transaction[key];
|
|
3161
|
-
if (value !== undefined && value !== null) {
|
|
3162
|
-
result[key] = value;
|
|
3163
|
-
}
|
|
3164
|
-
});
|
|
3165
|
-
const auxiliary = transaction['auxiliary'];
|
|
3166
|
-
if (auxiliary && auxiliary.length > 0) {
|
|
3167
|
-
result['auxiliary'] = [];
|
|
3168
|
-
auxiliary.forEach(a => {
|
|
3169
|
-
result['auxiliary'].push(hexlifyTransaction(a));
|
|
3170
|
-
});
|
|
3642
|
+
// @deprecated
|
|
3643
|
+
onWalletContext(callback) {
|
|
3644
|
+
return this.registerCallback('walletContext', callback);
|
|
3171
3645
|
}
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
class UncheckedJsonRpcSigner extends Web3Signer {
|
|
3175
|
-
sendTransaction(transaction) {
|
|
3176
|
-
return this.sendUncheckedTransaction(transaction).then(hash => {
|
|
3177
|
-
return {
|
|
3178
|
-
chainId: 0,
|
|
3179
|
-
confirmations: 0,
|
|
3180
|
-
data: '',
|
|
3181
|
-
from: '',
|
|
3182
|
-
gasLimit: ethers.constants.Zero,
|
|
3183
|
-
gasPrice: ethers.constants.Zero,
|
|
3184
|
-
hash,
|
|
3185
|
-
nonce: 0,
|
|
3186
|
-
value: ethers.constants.Zero,
|
|
3187
|
-
wait: confirmations => this.provider.waitForTransaction(hash, confirmations)
|
|
3188
|
-
};
|
|
3189
|
-
});
|
|
3646
|
+
onDefaultChainIdChanged(callback) {
|
|
3647
|
+
return this.registerCallback('chainChanged', callback);
|
|
3190
3648
|
}
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
class WalletUtils {
|
|
3194
|
-
constructor(walletProvider) {
|
|
3195
|
-
this.wallet = walletProvider;
|
|
3649
|
+
getChainId() {
|
|
3650
|
+
return this.defaultChainId.getDefaultChainId();
|
|
3196
3651
|
}
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
signMessage(message, chainId, allSigners) {
|
|
3200
|
-
const signer = this.wallet.getSigner();
|
|
3201
|
-
if (!signer) throw new Error('unable to get signer');
|
|
3202
|
-
return signer.signMessage(message, chainId, allSigners);
|
|
3652
|
+
setDefaultChainId(chainId) {
|
|
3653
|
+
return this.defaultChainId.setDefaultChainId(chainId);
|
|
3203
3654
|
}
|
|
3204
3655
|
|
|
3205
|
-
//
|
|
3206
|
-
async signAuthMessage(message, allSigners) {
|
|
3207
|
-
const signer = this.wallet.getSigner();
|
|
3208
|
-
if (!signer) throw new Error('unable to get AuthChain signer');
|
|
3209
|
-
return signer.signMessage(message, await signer.getChainId(), allSigners);
|
|
3210
|
-
}
|
|
3656
|
+
// Proxy transport methods
|
|
3211
3657
|
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
return signer.signTypedData(domain, types, message, chainId, allSigners);
|
|
3658
|
+
async openWallet(path, intent) {
|
|
3659
|
+
this.transport.openWallet(path, intent, this.getChainId());
|
|
3660
|
+
await this.transport.waitUntilOpened();
|
|
3661
|
+
return this.isOpened();
|
|
3217
3662
|
}
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
async signAuthTypedData(domain, types, message, allSigners) {
|
|
3221
|
-
const signer = this.wallet.getSigner();
|
|
3222
|
-
if (!signer) throw new Error('unable to get AuthChain signer');
|
|
3223
|
-
return signer.signTypedData(domain, types, message, await signer.getChainId(), allSigners);
|
|
3663
|
+
closeWallet() {
|
|
3664
|
+
return this.transport.closeWallet();
|
|
3224
3665
|
}
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
async isValidSignature(address, digest, signature, chainId) {
|
|
3228
|
-
const provider = this.wallet.getProvider(chainId);
|
|
3229
|
-
if (!provider) throw new Error(`unable to get provider for chainId ${chainId}`);
|
|
3230
|
-
return isValidSignature(address, digest, signature, provider);
|
|
3666
|
+
isOpened() {
|
|
3667
|
+
return this.transport.isOpened();
|
|
3231
3668
|
}
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
async isValidMessageSignature(address, message, signature, chainId) {
|
|
3235
|
-
const provider = this.wallet.getProvider(chainId);
|
|
3236
|
-
if (!provider) throw new Error(`unable to get provider for chainId ${chainId}`);
|
|
3237
|
-
const prefixed = prefixEIP191Message(message);
|
|
3238
|
-
const digest = encodeMessageDigest(prefixed);
|
|
3239
|
-
return isValidSignature(address, digest, signature, provider);
|
|
3669
|
+
isConnected() {
|
|
3670
|
+
return this.session.hasSession();
|
|
3240
3671
|
}
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
isValidTypedDataSignature(address, typedData, signature, chainId) {
|
|
3244
|
-
return this.isValidSignature(address, encodeTypedDataDigest(typedData), signature, chainId);
|
|
3672
|
+
getSession() {
|
|
3673
|
+
return this.session.getSession();
|
|
3245
3674
|
}
|
|
3246
3675
|
|
|
3247
|
-
//
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
// chainId: BigNumberish,
|
|
3265
|
-
// walletContext?: WalletContext
|
|
3266
|
-
// ): Promise<WalletConfig> => {
|
|
3267
|
-
// walletContext = walletContext || (await this.wallet.getWalletContext())
|
|
3268
|
-
// return recoverWalletConfig(address, encodeMessageDigest(prefixEIP191Message(message)), signature, chainId, walletContext)
|
|
3269
|
-
// }
|
|
3270
|
-
|
|
3271
|
-
// // Recover the WalletConfig from a signature of a typedData object
|
|
3272
|
-
// recoverWalletConfigFromTypedData = async (
|
|
3273
|
-
// address: string,
|
|
3274
|
-
// typedData: TypedData,
|
|
3275
|
-
// signature: string | DecodedSignature,
|
|
3276
|
-
// chainId: BigNumberish,
|
|
3277
|
-
// walletContext?: WalletContext
|
|
3278
|
-
// ): Promise<WalletConfig> => {
|
|
3279
|
-
// walletContext = walletContext || (await this.wallet.getWalletContext())
|
|
3280
|
-
// return recoverWalletConfig(address, encodeTypedDataDigest(typedData), signature, chainId, walletContext)
|
|
3281
|
-
// }
|
|
3282
|
-
|
|
3283
|
-
// sendTransaction()
|
|
3284
|
-
// sendTransactions()
|
|
3285
|
-
|
|
3286
|
-
// sendETH()
|
|
3287
|
-
// sendToken()
|
|
3288
|
-
// sendCoin() -- sugar for sendToken()
|
|
3289
|
-
// sendCollectible() -- sugar for sendToken()
|
|
3290
|
-
// callContract()
|
|
3291
|
-
|
|
3292
|
-
// transactionHistory()
|
|
3293
|
-
// getReceipt()
|
|
3294
|
-
// getLogs()
|
|
3295
|
-
// // ..
|
|
3296
|
-
|
|
3297
|
-
// isWalletDeployed()
|
|
3298
|
-
// deployWallet()
|
|
3299
|
-
|
|
3300
|
-
// validateSignature()
|
|
3301
|
-
// recoverWalletConfig()
|
|
3302
|
-
// recoverAddress()
|
|
3303
|
-
}
|
|
3304
|
-
|
|
3305
|
-
const SESSION_LOCALSTORE_KEY = '@sequence.session';
|
|
3306
|
-
class Wallet {
|
|
3307
|
-
constructor(_network, config) {
|
|
3308
|
-
var _this = this;
|
|
3309
|
-
this.init = () => {
|
|
3310
|
-
var _this$config$transpor, _this$config$transpor2, _this$config$transpor3, _this$config$transpor4, _this$config$transpor5, _this$config$transpor6, _this$config$transpor7, _this$config$transpor8, _findSupportedNetwork;
|
|
3311
|
-
if (this.transport.provider) {
|
|
3312
|
-
// init must have already been called
|
|
3313
|
-
return;
|
|
3314
|
-
}
|
|
3315
|
-
|
|
3316
|
-
// Setup provider
|
|
3317
|
-
|
|
3318
|
-
this.transport.messageProvider = new MuxMessageProvider();
|
|
3319
|
-
|
|
3320
|
-
// multiple message provider setup, first one to connect will be the main transport
|
|
3321
|
-
if ((_this$config$transpor = this.config.transports) != null && (_this$config$transpor2 = _this$config$transpor.windowTransport) != null && _this$config$transpor2.enabled && typeof window === 'object') {
|
|
3322
|
-
this.transport.windowMessageProvider = new WindowMessageProvider(this.config.walletAppURL);
|
|
3323
|
-
this.transport.messageProvider.add(this.transport.windowMessageProvider);
|
|
3324
|
-
}
|
|
3325
|
-
if ((_this$config$transpor3 = this.config.transports) != null && (_this$config$transpor4 = _this$config$transpor3.proxyTransport) != null && _this$config$transpor4.enabled) {
|
|
3326
|
-
this.transport.proxyMessageProvider = new ProxyMessageProvider(this.config.transports.proxyTransport.appPort);
|
|
3327
|
-
this.transport.messageProvider.add(this.transport.proxyMessageProvider);
|
|
3328
|
-
}
|
|
3329
|
-
if ((_this$config$transpor5 = this.config.transports) != null && (_this$config$transpor6 = _this$config$transpor5.extensionTransport) != null && _this$config$transpor6.enabled) {
|
|
3330
|
-
this.transport.extensionMessageProvider = new ExtensionMessageProvider(this.config.transports.extensionTransport.runtime);
|
|
3331
|
-
// this.transport.extensionMessageProvider.register()
|
|
3332
|
-
this.transport.messageProvider.add(this.transport.extensionMessageProvider);
|
|
3333
|
-
|
|
3334
|
-
// NOTE/REVIEW: see note in mux-message-provider
|
|
3335
|
-
//
|
|
3336
|
-
// We don't add the extensionMessageProvider here because we don't send requests to it anyways, we seem to
|
|
3337
|
-
// send all requests to the WindowMessageProvider anyways. By allowing it, if browser restarts, it will break
|
|
3338
|
-
// the entire extension because messageProvider.provider will be undefined. So this is a hack to fix it.
|
|
3339
|
-
//
|
|
3340
|
-
// this.transport.messageProvider.add(this.transport.extensionMessageProvider)
|
|
3341
|
-
}
|
|
3342
|
-
|
|
3343
|
-
if ((_this$config$transpor7 = this.config.transports) != null && (_this$config$transpor8 = _this$config$transpor7.unrealTransport) != null && _this$config$transpor8.enabled) {
|
|
3344
|
-
this.transport.unrealMessageProvider = new UnrealMessageProvider(this.config.walletAppURL);
|
|
3345
|
-
this.transport.messageProvider.add(this.transport.unrealMessageProvider);
|
|
3346
|
-
}
|
|
3347
|
-
this.transport.messageProvider.register();
|
|
3348
|
-
|
|
3349
|
-
// .....
|
|
3350
|
-
this.transport.allowProvider = allowProviderMiddleware(request => {
|
|
3351
|
-
if (request.method === 'sequence_setDefaultNetwork') return true;
|
|
3352
|
-
const isConnected = this.isConnected();
|
|
3353
|
-
if (!isConnected) {
|
|
3354
|
-
throw new Error('Sequence: wallet not connected');
|
|
3355
|
-
}
|
|
3356
|
-
return isConnected;
|
|
3357
|
-
});
|
|
3358
|
-
|
|
3359
|
-
// ...
|
|
3360
|
-
this.transport.networkProvider = networkProviderMiddleware(request => {
|
|
3361
|
-
// return stub chainId of 0 when not connected to any
|
|
3362
|
-
if (!this.networks || this.networks.length === 0) return 0;
|
|
3363
|
-
|
|
3364
|
-
// return the default chainId as we're connected
|
|
3365
|
-
return findNetworkConfig(this.networks, this.config.defaultNetworkId).chainId;
|
|
3366
|
-
});
|
|
3367
|
-
|
|
3368
|
-
// Provider proxy to support middleware stack of logging, caching and read-only rpc calls
|
|
3369
|
-
this.transport.cachedProvider = new CachedProvider();
|
|
3370
|
-
this.transport.cachedProvider.onUpdate(() => {
|
|
3371
|
-
if (!this.session) this.session = {
|
|
3372
|
-
providerCache: {}
|
|
3373
|
-
};
|
|
3374
|
-
this.session.providerCache = this.transport.cachedProvider.getCache();
|
|
3375
|
-
this.saveSession(this.session);
|
|
3376
|
-
});
|
|
3377
|
-
|
|
3378
|
-
// ..
|
|
3379
|
-
this.transport.router = new JsonRpcRouter([loggingProviderMiddleware, this.transport.networkProvider, this.transport.allowProvider, exceptionProviderMiddleware, this.transport.cachedProvider], this.transport.messageProvider);
|
|
3380
|
-
|
|
3381
|
-
// TODO: Move finding this config to upper in the stack
|
|
3382
|
-
this.transport.provider = new Web3Provider(this.transport.router, (_findSupportedNetwork = findSupportedNetwork(this.config.defaultNetworkId)) == null ? void 0 : _findSupportedNetwork.chainId);
|
|
3383
|
-
|
|
3384
|
-
// NOTE: we don't listen on 'connect' even here as we handle it within connect() method
|
|
3385
|
-
// in more synchronous flow.
|
|
3386
|
-
|
|
3387
|
-
// below will update the wallet session object and persist it. In case the session
|
|
3388
|
-
// is undefined, we consider the session to have been removed by the user, so we clear it.
|
|
3389
|
-
this.transport.messageProvider.on('open', openInfo => {
|
|
3390
|
-
const {
|
|
3391
|
-
session
|
|
3392
|
-
} = openInfo;
|
|
3393
|
-
if (!session) {
|
|
3394
|
-
if (this.session && this.session.accountAddress) {
|
|
3395
|
-
// emit disconnect even if previously we had a session, and now we don't.
|
|
3396
|
-
this.transport.messageProvider.emit('disconnect');
|
|
3397
|
-
}
|
|
3398
|
-
this.clearSession();
|
|
3399
|
-
} else {
|
|
3400
|
-
this.useSession(session, true);
|
|
3401
|
-
}
|
|
3402
|
-
});
|
|
3403
|
-
|
|
3404
|
-
// below will update the account upon wallet connect/disconnect - aka, login/logout.
|
|
3405
|
-
// if an origin is provided, this operation should be performed only on that origin
|
|
3406
|
-
// and shouldn't affect the session of the wallet.
|
|
3407
|
-
this.transport.messageProvider.on('accountsChanged', (accounts, origin) => {
|
|
3408
|
-
if (origin) {
|
|
3409
|
-
if (accounts.length > 0) {
|
|
3410
|
-
this.useSession({
|
|
3411
|
-
accountAddress: accounts[0]
|
|
3412
|
-
}, true);
|
|
3413
|
-
}
|
|
3414
|
-
return;
|
|
3415
|
-
}
|
|
3416
|
-
if (!accounts || accounts.length === 0 || accounts[0] === '') {
|
|
3417
|
-
this.clearSession();
|
|
3418
|
-
} else {
|
|
3419
|
-
this.useSession({
|
|
3420
|
-
accountAddress: accounts[0]
|
|
3421
|
-
}, true);
|
|
3676
|
+
// Basic API
|
|
3677
|
+
getAddress() {
|
|
3678
|
+
const session = this.session.connectedSession();
|
|
3679
|
+
return session.accountAddress;
|
|
3680
|
+
}
|
|
3681
|
+
async connect(options) {
|
|
3682
|
+
if ((options == null ? void 0 : options.authorizeVersion) === undefined) {
|
|
3683
|
+
// Populate default authorize version if not provided
|
|
3684
|
+
options.authorizeVersion = 2;
|
|
3685
|
+
}
|
|
3686
|
+
if ((options == null ? void 0 : options.refresh) === true) {
|
|
3687
|
+
this.disconnect();
|
|
3688
|
+
}
|
|
3689
|
+
if (options) {
|
|
3690
|
+
if (options.authorize) {
|
|
3691
|
+
if (!options.app) {
|
|
3692
|
+
throw new Error(`connecting with 'authorize' option also requires 'app' to be set`);
|
|
3422
3693
|
}
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
// below will update the networks automatically when the wallet networks change
|
|
3426
|
-
this.transport.messageProvider.on('networks', networks => {
|
|
3427
|
-
this.useSession({
|
|
3428
|
-
networks: networks
|
|
3429
|
-
}, true);
|
|
3430
|
-
});
|
|
3431
|
-
|
|
3432
|
-
// below will update the wallet context automatically
|
|
3433
|
-
this.transport.messageProvider.on('walletContext', walletContext => {
|
|
3434
|
-
this.useSession({
|
|
3435
|
-
walletContext: walletContext
|
|
3436
|
-
}, true);
|
|
3437
|
-
});
|
|
3438
|
-
};
|
|
3439
|
-
this.loadSession = async function (preferredNetwork) {
|
|
3440
|
-
const data = await LocalStorage.getInstance().getItem(SESSION_LOCALSTORE_KEY);
|
|
3441
|
-
if (!data || data === '') {
|
|
3442
|
-
return undefined;
|
|
3443
|
-
}
|
|
3444
|
-
try {
|
|
3445
|
-
const session = JSON.parse(data);
|
|
3446
|
-
if (session) {
|
|
3447
|
-
// Setting preferredNetwork as default network if it's in session.networks
|
|
3448
|
-
if (preferredNetwork !== undefined) {
|
|
3449
|
-
var _session$networks;
|
|
3450
|
-
const preferredNetworkIdNum = typeof preferredNetwork === 'string' ? parseInt(preferredNetwork) : preferredNetwork;
|
|
3451
|
-
const isPreferredNetwork = n => n.name === preferredNetwork || n.chainId === preferredNetworkIdNum;
|
|
3452
|
-
const preferredNetworkInConfig = (_session$networks = session.networks) == null ? void 0 : _session$networks.find(isPreferredNetwork);
|
|
3453
|
-
const isAlreadyDefaultChain = preferredNetworkInConfig == null ? void 0 : preferredNetworkInConfig.isDefaultChain;
|
|
3454
|
-
if (session.networks && preferredNetworkInConfig && !isAlreadyDefaultChain) {
|
|
3455
|
-
const updatedNetworks = session.networks.map(n => _extends({}, n, {
|
|
3456
|
-
isDefaultChain: isPreferredNetwork(n)
|
|
3457
|
-
}));
|
|
3458
|
-
session.networks = sortNetworks(updatedNetworks);
|
|
3459
|
-
session.providerCache = undefined;
|
|
3460
|
-
}
|
|
3461
|
-
}
|
|
3462
|
-
_this.useSession(session, true);
|
|
3694
|
+
if (options.authorizeVersion === undefined) {
|
|
3695
|
+
options.authorizeVersion = 2;
|
|
3463
3696
|
}
|
|
3464
|
-
return session;
|
|
3465
|
-
} catch (err) {
|
|
3466
|
-
logger.warn('loadSession failed, unable to parse session payload from storage.');
|
|
3467
|
-
return undefined;
|
|
3468
3697
|
}
|
|
3469
|
-
}
|
|
3470
|
-
this.
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
}
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
if (_this.isConnected() && (await _this.isSiteConnected(options == null ? void 0 : options.origin)) && !!_this.session && !(options != null && options.authorize) && !(options != null && options.askForEmail)) {
|
|
3698
|
+
}
|
|
3699
|
+
await this.openWallet(undefined, {
|
|
3700
|
+
type: 'connect',
|
|
3701
|
+
options: _extends({}, options, {
|
|
3702
|
+
networkId: this.getChainId()
|
|
3703
|
+
})
|
|
3704
|
+
});
|
|
3705
|
+
const connectDetails = await this.transport.waitUntilConnected().catch(error => {
|
|
3706
|
+
if (error instanceof Error) {
|
|
3479
3707
|
return {
|
|
3480
|
-
connected:
|
|
3481
|
-
|
|
3482
|
-
chainId: ethers.utils.hexlify(await _this.getChainId())
|
|
3708
|
+
connected: false,
|
|
3709
|
+
error: error.message
|
|
3483
3710
|
};
|
|
3484
|
-
}
|
|
3485
|
-
if (options) {
|
|
3486
|
-
if (options.authorize) {
|
|
3487
|
-
if (!options.app) {
|
|
3488
|
-
throw new Error(`connecting with 'authorize' option also requires 'app' to be set`);
|
|
3489
|
-
}
|
|
3490
|
-
if (options.authorizeVersion === undefined) {
|
|
3491
|
-
options.authorizeVersion = 2;
|
|
3492
|
-
}
|
|
3493
|
-
}
|
|
3494
|
-
}
|
|
3495
|
-
await _this.openWallet(undefined, {
|
|
3496
|
-
type: 'connect',
|
|
3497
|
-
options
|
|
3498
|
-
});
|
|
3499
|
-
const connectDetails = await _this.transport.messageProvider.waitUntilConnected().catch(error => {
|
|
3500
|
-
if (error instanceof Error) {
|
|
3501
|
-
return {
|
|
3502
|
-
connected: false,
|
|
3503
|
-
error: error.message
|
|
3504
|
-
};
|
|
3505
|
-
} else {
|
|
3506
|
-
return {
|
|
3507
|
-
connected: false,
|
|
3508
|
-
error: JSON.stringify(error)
|
|
3509
|
-
};
|
|
3510
|
-
}
|
|
3511
|
-
});
|
|
3512
|
-
if (connectDetails.connected) {
|
|
3513
|
-
if (!!connectDetails.session) {
|
|
3514
|
-
_this.useSession(connectDetails.session, true);
|
|
3515
|
-
_this.addConnectedSite(options == null ? void 0 : options.origin);
|
|
3516
|
-
} else {
|
|
3517
|
-
throw new Error('impossible state, connect response is missing session');
|
|
3518
|
-
}
|
|
3519
|
-
}
|
|
3520
|
-
return connectDetails;
|
|
3521
|
-
};
|
|
3522
|
-
this.authorize = async function (options) {
|
|
3523
|
-
return _this.connect(_extends({}, options, {
|
|
3524
|
-
authorize: true
|
|
3525
|
-
}));
|
|
3526
|
-
};
|
|
3527
|
-
this.getSession = () => {
|
|
3528
|
-
if (!this.isConnected()) {
|
|
3529
|
-
return undefined;
|
|
3530
|
-
}
|
|
3531
|
-
return this.session;
|
|
3532
|
-
};
|
|
3533
|
-
this.getAddress = async function () {
|
|
3534
|
-
if (!_this.isConnected()) {
|
|
3535
|
-
throw new Error('connect first');
|
|
3536
|
-
}
|
|
3537
|
-
const session = _this.getSession();
|
|
3538
|
-
return session.accountAddress;
|
|
3539
|
-
};
|
|
3540
|
-
this.getNetworks = async function (chainId) {
|
|
3541
|
-
if (!_this.isConnected() || !_this.networks) {
|
|
3542
|
-
throw new Error('connect first');
|
|
3543
|
-
}
|
|
3544
|
-
if (chainId) {
|
|
3545
|
-
// filter list to just the specific chain requested
|
|
3546
|
-
const network = findNetworkConfig(_this.networks, chainId);
|
|
3547
|
-
return network ? [network] : [];
|
|
3548
|
-
}
|
|
3549
|
-
return _this.networks;
|
|
3550
|
-
};
|
|
3551
|
-
// getChainId returns the default chain id
|
|
3552
|
-
this.getChainId = async function () {
|
|
3553
|
-
if (!_this.networks || _this.networks.length < 1) {
|
|
3554
|
-
throw new Error('networks have not been set by session. connect first.');
|
|
3555
|
-
}
|
|
3556
|
-
const network = findNetworkConfig(_this.networks, _this.config.defaultNetworkId);
|
|
3557
|
-
if (!network) {
|
|
3558
|
-
throw new Error('networks must have a default chain specified');
|
|
3559
|
-
}
|
|
3560
|
-
return network.chainId;
|
|
3561
|
-
};
|
|
3562
|
-
this.openWallet = async function (path, intent, networkId) {
|
|
3563
|
-
if ((intent == null ? void 0 : intent.type) !== 'connect' && !_this.isConnected()) {
|
|
3564
|
-
throw new Error('connect first');
|
|
3565
|
-
}
|
|
3566
|
-
let currentNetworkId;
|
|
3567
|
-
if (!_this.networks || _this.networks.length < 1) {
|
|
3568
|
-
currentNetworkId = _this.config.defaultNetworkId;
|
|
3569
|
-
} else {
|
|
3570
|
-
currentNetworkId = await _this.getChainId();
|
|
3571
|
-
}
|
|
3572
|
-
_this.transport.messageProvider.openWallet(path, intent, networkId || currentNetworkId);
|
|
3573
|
-
await _this.transport.messageProvider.waitUntilOpened();
|
|
3574
|
-
return true;
|
|
3575
|
-
};
|
|
3576
|
-
this.closeWallet = () => {
|
|
3577
|
-
this.transport.messageProvider.closeWallet();
|
|
3578
|
-
};
|
|
3579
|
-
this.unregister = () => {
|
|
3580
|
-
var _this$transport$messa;
|
|
3581
|
-
this.disconnect();
|
|
3582
|
-
(_this$transport$messa = this.transport.messageProvider) == null ? void 0 : _this$transport$messa.unregister();
|
|
3583
|
-
};
|
|
3584
|
-
this.saveSession = async function (session) {
|
|
3585
|
-
logger.debug('wallet provider: saving session');
|
|
3586
|
-
const data = JSON.stringify(session);
|
|
3587
|
-
await LocalStorage.getInstance().setItem(SESSION_LOCALSTORE_KEY, data);
|
|
3588
|
-
};
|
|
3589
|
-
this.useSession = async function (session, autoSave = true) {
|
|
3590
|
-
if (!_this.session) _this.session = {};
|
|
3591
|
-
|
|
3592
|
-
// setup wallet context
|
|
3593
|
-
if (_this.config.walletContext) {
|
|
3594
|
-
_this.session.walletContext = _this.config.walletContext;
|
|
3595
|
-
} else if (session.walletContext) {
|
|
3596
|
-
_this.session.walletContext = session.walletContext;
|
|
3597
|
-
}
|
|
3598
|
-
|
|
3599
|
-
// setup account
|
|
3600
|
-
if (session.accountAddress) {
|
|
3601
|
-
_this.useAccountAddress(session.accountAddress);
|
|
3602
|
-
}
|
|
3603
|
-
|
|
3604
|
-
// setup networks
|
|
3605
|
-
if (session.networks) {
|
|
3606
|
-
_this.useNetworks(session.networks);
|
|
3607
|
-
}
|
|
3608
|
-
|
|
3609
|
-
// setup provider cache
|
|
3610
|
-
if (session.providerCache) {
|
|
3611
|
-
_this.transport.cachedProvider.setCache(session.providerCache);
|
|
3612
3711
|
} else {
|
|
3613
|
-
|
|
3712
|
+
return {
|
|
3713
|
+
connected: false,
|
|
3714
|
+
error: JSON.stringify(error)
|
|
3715
|
+
};
|
|
3614
3716
|
}
|
|
3717
|
+
});
|
|
3615
3718
|
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
};
|
|
3621
|
-
// config is a Partial, so that we may intersect it with the DefaultProviderConfig,
|
|
3622
|
-
// which allows easy overriding and control of the config.
|
|
3623
|
-
this.config = _extends({}, DefaultProviderConfig);
|
|
3624
|
-
if (config) {
|
|
3625
|
-
this.config = _extends({}, this.config, config);
|
|
3626
|
-
}
|
|
3627
|
-
if (_network) {
|
|
3628
|
-
this.config.defaultNetworkId = _network;
|
|
3629
|
-
} else if (!this.config.defaultNetworkId) {
|
|
3630
|
-
this.config.defaultNetworkId = 'mainnet';
|
|
3631
|
-
}
|
|
3632
|
-
if (config != null && config.localStorage) {
|
|
3633
|
-
LocalStorage.use(config.localStorage);
|
|
3634
|
-
}
|
|
3635
|
-
this.transport = {};
|
|
3636
|
-
this.networks = [];
|
|
3637
|
-
this.providers = {};
|
|
3638
|
-
this.connectedSites = new LocalStore('@sequence.connectedSites', []);
|
|
3639
|
-
this.utils = new WalletUtils(this);
|
|
3640
|
-
this.init();
|
|
3641
|
-
}
|
|
3642
|
-
async addConnectedSite(origin) {
|
|
3643
|
-
origin = origin || window.location.origin;
|
|
3644
|
-
const connectedSites = await this.connectedSites.get();
|
|
3645
|
-
if (connectedSites) {
|
|
3646
|
-
if (connectedSites.includes(origin)) {
|
|
3647
|
-
return;
|
|
3648
|
-
}
|
|
3649
|
-
this.connectedSites.set([...connectedSites, origin]);
|
|
3650
|
-
} else {
|
|
3651
|
-
this.connectedSites.set([origin]);
|
|
3719
|
+
// Normalize chainId into a decimal string
|
|
3720
|
+
// TODO: Remove this once wallet-webapp returns chainId as a string
|
|
3721
|
+
if (connectDetails.chainId) {
|
|
3722
|
+
connectDetails.chainId = ethers.BigNumber.from(connectDetails.chainId).toString();
|
|
3652
3723
|
}
|
|
3653
|
-
|
|
3654
|
-
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
this.
|
|
3724
|
+
if (connectDetails.connected) {
|
|
3725
|
+
if (!connectDetails.session) {
|
|
3726
|
+
throw new Error('impossible state, connect response is missing session');
|
|
3727
|
+
}
|
|
3728
|
+
this.session.setSession(connectDetails.session);
|
|
3658
3729
|
}
|
|
3659
|
-
|
|
3660
|
-
getConnectedSites() {
|
|
3661
|
-
return this.connectedSites.get();
|
|
3662
|
-
}
|
|
3663
|
-
async isSiteConnected(origin) {
|
|
3664
|
-
const authorized = await this.connectedSites.get();
|
|
3665
|
-
return !!authorized && authorized.includes(origin || window.location.origin);
|
|
3730
|
+
return connectDetails;
|
|
3666
3731
|
}
|
|
3667
3732
|
disconnect() {
|
|
3668
3733
|
if (this.isOpened()) {
|
|
3669
3734
|
this.closeWallet();
|
|
3670
3735
|
}
|
|
3671
|
-
return this.clearSession();
|
|
3736
|
+
return this.session.clearSession();
|
|
3672
3737
|
}
|
|
3673
3738
|
|
|
3674
|
-
//
|
|
3675
|
-
// and on successful response, will update the provider info here, etc.
|
|
3739
|
+
// Higher level API
|
|
3676
3740
|
|
|
3677
|
-
|
|
3678
|
-
|
|
3741
|
+
// Working with sendAsync is less idiomatic
|
|
3742
|
+
// but transport uses it instead of send, so we wrap it
|
|
3743
|
+
send(request, chainId) {
|
|
3744
|
+
return new Promise((resolve, reject) => {
|
|
3745
|
+
this.transport.sendAsync(request, (error, response) => {
|
|
3746
|
+
if (error) {
|
|
3747
|
+
reject(error);
|
|
3748
|
+
} else if (response === undefined) {
|
|
3749
|
+
reject(new Error(`Got undefined response for request: ${request}`));
|
|
3750
|
+
} else if (typeof response === 'object' && response.error) {
|
|
3751
|
+
reject(response.error);
|
|
3752
|
+
} else if (typeof response === 'object' && response.result) {
|
|
3753
|
+
resolve(response.result);
|
|
3754
|
+
} else {
|
|
3755
|
+
reject(new Error(`Got invalid response for request: ${request}`));
|
|
3756
|
+
}
|
|
3757
|
+
}, chainId || this.getChainId());
|
|
3758
|
+
});
|
|
3679
3759
|
}
|
|
3680
|
-
|
|
3681
|
-
|
|
3760
|
+
async getNetworks(pull) {
|
|
3761
|
+
const connectedSession = this.session.connectedSession();
|
|
3762
|
+
if (pull) {
|
|
3763
|
+
connectedSession.networks = await this.send({
|
|
3764
|
+
method: 'sequence_getNetworks'
|
|
3765
|
+
});
|
|
3766
|
+
this.session.setSession(connectedSession);
|
|
3767
|
+
}
|
|
3768
|
+
return connectedSession.networks;
|
|
3682
3769
|
}
|
|
3683
|
-
|
|
3684
|
-
|
|
3770
|
+
async signMessage(message, options) {
|
|
3771
|
+
const method = options != null && options.eip6492 ? 'sequence_sign' : 'personal_sign';
|
|
3772
|
+
|
|
3773
|
+
// Address is ignored by the wallet webapp
|
|
3774
|
+
return this.send({
|
|
3775
|
+
method,
|
|
3776
|
+
params: [message, this.getAddress()]
|
|
3777
|
+
}, options == null ? void 0 : options.chainId);
|
|
3685
3778
|
}
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
// and user has not logged in
|
|
3689
|
-
if (!this.isConnected()) {
|
|
3690
|
-
if (chainId) {
|
|
3691
|
-
throw new Error(`session is empty. connect and try again.`);
|
|
3692
|
-
} else {
|
|
3693
|
-
return this.transport.provider;
|
|
3694
|
-
}
|
|
3695
|
-
}
|
|
3696
|
-
let network = findNetworkConfig(this.networks, this.config.defaultNetworkId);
|
|
3697
|
-
if (chainId) {
|
|
3698
|
-
network = findNetworkConfig(this.networks, chainId);
|
|
3699
|
-
if (!network) {
|
|
3700
|
-
throw new Error(`network ${chainId} is not in the network list`);
|
|
3701
|
-
}
|
|
3702
|
-
}
|
|
3779
|
+
async signTypedData(typedData, options) {
|
|
3780
|
+
const method = options != null && options.eip6492 ? 'sequence_signTypedData_v4' : 'eth_signTypedData_v4';
|
|
3703
3781
|
|
|
3704
|
-
//
|
|
3705
|
-
|
|
3706
|
-
|
|
3707
|
-
}
|
|
3782
|
+
// TODO: Stop using ethers for this, this is the only place where we use it
|
|
3783
|
+
// and it makes the client depend on ethers.
|
|
3784
|
+
const encoded = ethers.utils._TypedDataEncoder.getPayload(typedData.domain, typedData.types, typedData.message);
|
|
3708
3785
|
|
|
3709
|
-
//
|
|
3710
|
-
|
|
3786
|
+
// The sign typed data will use one of the following chainIds, in order:
|
|
3787
|
+
// - The one provided in the options
|
|
3788
|
+
// - The one provided in the typedData.domain.chainId
|
|
3789
|
+
// - The default chainId
|
|
3711
3790
|
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
const router = new JsonRpcRouter([loggingProviderMiddleware, exceptionProviderMiddleware, new EagerProvider({
|
|
3717
|
-
accountAddress: this.session.accountAddress,
|
|
3718
|
-
walletContext: this.session.walletContext
|
|
3719
|
-
}), new SigningProvider(this.transport.provider), this.transport.cachedProvider], new JsonRpcSender(rpcProvider));
|
|
3720
|
-
provider = new Web3Provider(router, network.chainId);
|
|
3721
|
-
} else {
|
|
3722
|
-
// communicating with another chain will bind to that network, but will forward
|
|
3723
|
-
// any signing-related requests to the wallet message transport
|
|
3724
|
-
const router = new JsonRpcRouter([loggingProviderMiddleware, exceptionProviderMiddleware, new EagerProvider({
|
|
3725
|
-
accountAddress: this.session.accountAddress,
|
|
3726
|
-
walletContext: this.session.walletContext,
|
|
3727
|
-
chainId: network.chainId
|
|
3728
|
-
}), new SigningProvider(this.transport.provider), new CachedProvider({
|
|
3729
|
-
defaultChainId: network.chainId
|
|
3730
|
-
})], new JsonRpcSender(rpcProvider));
|
|
3731
|
-
provider = new Web3Provider(router, network.chainId);
|
|
3732
|
-
}
|
|
3733
|
-
this.providers[network.chainId] = provider;
|
|
3734
|
-
return provider;
|
|
3735
|
-
}
|
|
3736
|
-
getAllProviders() {
|
|
3737
|
-
return this.providers;
|
|
3738
|
-
}
|
|
3739
|
-
getSigner(chainId) {
|
|
3740
|
-
return this.getProvider(chainId).getSigner();
|
|
3741
|
-
}
|
|
3742
|
-
getWalletConfig(chainId) {
|
|
3743
|
-
return this.getSigner().getWalletConfig(chainId);
|
|
3744
|
-
}
|
|
3745
|
-
getWalletState(chainId) {
|
|
3746
|
-
return this.getSigner().getWalletState(chainId);
|
|
3747
|
-
}
|
|
3748
|
-
getWalletContext() {
|
|
3749
|
-
return this.getSigner().getWalletContext();
|
|
3750
|
-
}
|
|
3751
|
-
isDeployed(chainId) {
|
|
3752
|
-
return this.getSigner(chainId).isDeployed();
|
|
3791
|
+
return this.send({
|
|
3792
|
+
method,
|
|
3793
|
+
params: [this.getAddress(), encoded]
|
|
3794
|
+
}, (options == null ? void 0 : options.chainId) || typedData.domain.chainId && ethers.BigNumber.from(typedData.domain.chainId).toNumber() || this.getChainId());
|
|
3753
3795
|
}
|
|
3754
|
-
|
|
3755
|
-
|
|
3796
|
+
async sendTransaction(tx, options) {
|
|
3797
|
+
const sequenceTxs = Array.isArray(tx) ? tx : [tx];
|
|
3798
|
+
const extendedTxs = toExtended(sequenceTxs);
|
|
3799
|
+
return this.send({
|
|
3800
|
+
method: 'eth_sendTransaction',
|
|
3801
|
+
params: [extendedTxs]
|
|
3802
|
+
}, options == null ? void 0 : options.chainId);
|
|
3756
3803
|
}
|
|
3757
|
-
|
|
3758
|
-
this.
|
|
3804
|
+
async getWalletContext() {
|
|
3805
|
+
return this.send({
|
|
3806
|
+
method: 'sequence_getWalletContext'
|
|
3807
|
+
});
|
|
3759
3808
|
}
|
|
3760
|
-
|
|
3761
|
-
|
|
3762
|
-
|
|
3809
|
+
async getOnchainWalletConfig(options) {
|
|
3810
|
+
// NOTICE: sequence_getWalletConfig sends the chainId as a param
|
|
3811
|
+
const res = await this.send({
|
|
3812
|
+
method: 'sequence_getWalletConfig',
|
|
3813
|
+
params: [(options == null ? void 0 : options.chainId) || this.getChainId()]
|
|
3814
|
+
}, options == null ? void 0 : options.chainId);
|
|
3815
|
+
return Array.isArray(res) ? res[0] : res;
|
|
3763
3816
|
}
|
|
3764
|
-
useNetworks(networks) {
|
|
3765
|
-
// set networks in the session
|
|
3766
|
-
if (!this.session) this.session = {};
|
|
3767
|
-
|
|
3768
|
-
// set networks on session object
|
|
3769
|
-
this.session.networks = networks;
|
|
3770
|
-
|
|
3771
|
-
// short-circuit if setting empty network list (aka logged out state)
|
|
3772
|
-
if (!this.session.networks || this.session.networks.length === 0) {
|
|
3773
|
-
return;
|
|
3774
|
-
}
|
|
3775
|
-
|
|
3776
|
-
// check if any custom network settings, otherwise return early
|
|
3777
|
-
if (!this.config.networks && !this.config.networkRpcUrl) {
|
|
3778
|
-
this.networks = networks;
|
|
3779
|
-
return;
|
|
3780
|
-
}
|
|
3781
|
-
|
|
3782
|
-
// init networks
|
|
3783
|
-
this.networks = networks;
|
|
3784
|
-
|
|
3785
|
-
// combine custom network config with networks in the session
|
|
3786
|
-
if (this.config.networks) {
|
|
3787
|
-
this.networks = networks.map(n => _extends({}, n)); // copy
|
|
3788
|
-
this.config.networks.forEach(n => {
|
|
3789
|
-
const network = findNetworkConfig(this.networks, n.chainId || n.name);
|
|
3790
|
-
if (!network) return;
|
|
3791
|
-
updateNetworkConfig(n, network);
|
|
3792
|
-
});
|
|
3793
|
-
ensureValidNetworks(this.networks, true);
|
|
3794
|
-
}
|
|
3795
3817
|
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
|
|
3799
|
-
|
|
3800
|
-
network.rpcUrl = this.config.networkRpcUrl;
|
|
3801
|
-
}
|
|
3802
|
-
}
|
|
3803
|
-
}
|
|
3804
|
-
async clearSession() {
|
|
3805
|
-
var _this$transport$cache;
|
|
3806
|
-
logger.debug('wallet provider: clearing session');
|
|
3807
|
-
await LocalStorage.getInstance().removeItem(SESSION_LOCALSTORE_KEY);
|
|
3808
|
-
this.session = undefined;
|
|
3809
|
-
this.networks = [];
|
|
3810
|
-
this.providers = {};
|
|
3811
|
-
(_this$transport$cache = this.transport.cachedProvider) == null ? void 0 : _this$transport$cache.clearCache();
|
|
3812
|
-
}
|
|
3818
|
+
// NOTICE: We are leaving out all the "regular" methods os a tipical
|
|
3819
|
+
// JSON RPC Provider (eth_getBlockByNumber, eth_call, etc)
|
|
3820
|
+
// wallet-webapp does implement them, but this client is meant to be
|
|
3821
|
+
// exclusively used for Sequence specific methods
|
|
3813
3822
|
}
|
|
3814
|
-
const DefaultProviderConfig = {
|
|
3815
|
-
walletAppURL: 'https://sequence.app',
|
|
3816
|
-
// walletSessionURL: 'https://session.sequence.app',
|
|
3817
3823
|
|
|
3824
|
+
const DefaultProviderConfig = {
|
|
3818
3825
|
transports: {
|
|
3826
|
+
walletAppURL: 'https://sequence.app',
|
|
3819
3827
|
windowTransport: {
|
|
3820
3828
|
enabled: true
|
|
3821
3829
|
},
|
|
3822
3830
|
proxyTransport: {
|
|
3823
3831
|
enabled: false
|
|
3824
3832
|
}
|
|
3825
|
-
}
|
|
3833
|
+
},
|
|
3834
|
+
defaultNetwork: 1
|
|
3826
3835
|
};
|
|
3827
|
-
let
|
|
3828
|
-
const initWallet =
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
3832
|
-
|
|
3833
|
-
|
|
3834
|
-
|
|
3836
|
+
let sequenceWalletProvider;
|
|
3837
|
+
const initWallet = partialConfig => {
|
|
3838
|
+
var _config$networks$filt, _config$networks, _findNetworkConfig;
|
|
3839
|
+
if (sequenceWalletProvider) {
|
|
3840
|
+
return sequenceWalletProvider;
|
|
3841
|
+
}
|
|
3842
|
+
|
|
3843
|
+
// Combine both the provided config and the default config
|
|
3844
|
+
const config = _extends({}, DefaultProviderConfig, partialConfig, {
|
|
3845
|
+
transports: _extends({}, DefaultProviderConfig.transports, partialConfig == null ? void 0 : partialConfig.transports)
|
|
3846
|
+
});
|
|
3847
|
+
const rpcProviders = {};
|
|
3848
|
+
|
|
3849
|
+
// Find any new networks that aren't already defined in sequence.js
|
|
3850
|
+
// and add them to the list of networks, (they must have a rpcUrl and chainId)
|
|
3851
|
+
const newNetworks = (_config$networks$filt = (_config$networks = config.networks) == null ? void 0 : _config$networks.filter(n => {
|
|
3852
|
+
n.rpcUrl !== undefined && n.chainId !== undefined && !allNetworks.find(an => an.chainId === n.chainId);
|
|
3853
|
+
})) != null ? _config$networks$filt : [];
|
|
3854
|
+
|
|
3855
|
+
// Override any information about the networks using the config
|
|
3856
|
+
const combinedNetworks = allNetworks.map(n => {
|
|
3857
|
+
var _config$networks2;
|
|
3858
|
+
const network = (_config$networks2 = config.networks) == null ? void 0 : _config$networks2.find(cn => cn.chainId === n.chainId);
|
|
3859
|
+
return network ? _extends({}, n, network) : n;
|
|
3860
|
+
}).concat(newNetworks);
|
|
3861
|
+
|
|
3862
|
+
// This build a "public rpc" on demand, we build them on demand because we don't want to
|
|
3863
|
+
// generate a bunch of providers for networks that aren't used.
|
|
3864
|
+
const providerForChainId = chainId => {
|
|
3865
|
+
if (!rpcProviders[chainId]) {
|
|
3866
|
+
var _combinedNetworks$fin;
|
|
3867
|
+
const rpcUrl = (_combinedNetworks$fin = combinedNetworks.find(n => n.chainId === chainId)) == null ? void 0 : _combinedNetworks$fin.rpcUrl;
|
|
3868
|
+
if (!rpcUrl) {
|
|
3869
|
+
throw new Error(`no rpcUrl found for chainId: ${chainId}`);
|
|
3870
|
+
}
|
|
3871
|
+
const baseProvider = new ethers.providers.JsonRpcProvider(rpcUrl);
|
|
3872
|
+
const router = new JsonRpcRouter([loggingProviderMiddleware, exceptionProviderMiddleware, new CachedProvider()], new JsonRpcSender(baseProvider));
|
|
3873
|
+
rpcProviders[chainId] = new ethers.providers.Web3Provider(router, chainId);
|
|
3874
|
+
}
|
|
3875
|
+
return rpcProviders[chainId];
|
|
3876
|
+
};
|
|
3877
|
+
|
|
3878
|
+
// This is the starting default network (as defined by the config)
|
|
3879
|
+
// it can be later be changed using `wallet_switchEthereumChain` or some
|
|
3880
|
+
// of the other methods on the provider.
|
|
3881
|
+
const defaultNetwork = config.defaultNetwork ? (_findNetworkConfig = findNetworkConfig(combinedNetworks, config.defaultNetwork)) == null ? void 0 : _findNetworkConfig.chainId : undefined;
|
|
3882
|
+
if (!defaultNetwork && config.defaultNetwork) {
|
|
3883
|
+
throw new Error(`defaultNetwork not found for chainId: ${config.defaultNetwork}`);
|
|
3884
|
+
}
|
|
3885
|
+
|
|
3886
|
+
// Generate ItemStore
|
|
3887
|
+
const itemStore = config.localStorage || useBestStore();
|
|
3888
|
+
|
|
3889
|
+
// Create client, provider and return signer
|
|
3890
|
+
const client = new SequenceClient(config.transports, itemStore, defaultNetwork);
|
|
3891
|
+
sequenceWalletProvider = new SequenceProvider(client, providerForChainId);
|
|
3892
|
+
return sequenceWalletProvider;
|
|
3835
3893
|
};
|
|
3836
3894
|
const unregisterWallet = () => {
|
|
3837
|
-
if (!
|
|
3838
|
-
|
|
3839
|
-
|
|
3895
|
+
if (!sequenceWalletProvider) return;
|
|
3896
|
+
sequenceWalletProvider.client.closeWallet();
|
|
3897
|
+
sequenceWalletProvider.client.transport.unregister();
|
|
3898
|
+
sequenceWalletProvider = undefined;
|
|
3840
3899
|
};
|
|
3841
3900
|
const getWallet = () => {
|
|
3842
|
-
if (!
|
|
3843
|
-
throw new Error('Wallet has not been initialized, call sequence.initWallet(
|
|
3901
|
+
if (!sequenceWalletProvider) {
|
|
3902
|
+
throw new Error('Wallet has not been initialized, call sequence.initWallet(config) first.');
|
|
3844
3903
|
}
|
|
3845
|
-
return
|
|
3904
|
+
return sequenceWalletProvider;
|
|
3846
3905
|
};
|
|
3847
3906
|
|
|
3848
|
-
export { BaseInjectedTransport, BaseProviderTransport, BaseWalletTransport, CHANNEL_ID, DefaultProviderConfig, ErrSignedInRequired, EventType, ExtensionMessageHandler, ExtensionMessageProvider, InitState, LocalStorage,
|
|
3907
|
+
export { BaseInjectedTransport, BaseProviderTransport, BaseWalletTransport, CHANNEL_ID, DefaultChainIdTracker, DefaultProviderConfig, ErrSignedInRequired, EventType, ExtensionMessageHandler, ExtensionMessageProvider, InitState, LocalStorage, MemoryItemStore, MuxMessageProvider, OpenState, PROVIDER_OPEN_TIMEOUT, ProviderError, ProxyMessageChannel, ProxyMessageChannelPort, ProxyMessageHandler, ProxyMessageProvider, SequenceClient, SequenceClientSession, SequenceProvider, SequenceSigner, SingleNetworkSequenceProvider, SingleNetworkSequenceSigner, UnrealMessageHandler, UnrealMessageProvider, WalletRequestHandler, WindowMessageHandler, WindowMessageProvider, WindowSessionParams, getWallet, initWallet, isBrowserExtension, isMuxTransportTemplate, isProviderTransport, isUnityPlugin, isValidMessageSignature, isValidSignature, isValidTypedDataSignature, isWalletUpToDate, messageToBytes, nextMessageIdx, prefixEIP191Message, resolveArrayProperties, trimEIP191Prefix, unregisterWallet, useBestStore };
|