@0xsequence/provider 0.38.2 → 0.39.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/0xsequence-provider.cjs.dev.js +88 -45
- package/dist/0xsequence-provider.cjs.prod.js +88 -45
- package/dist/0xsequence-provider.esm.js +88 -46
- package/dist/declarations/src/transports/base-provider-transport.d.ts +4 -1
- package/dist/declarations/src/transports/base-wallet-transport.d.ts +2 -2
- package/dist/declarations/src/transports/extension-transport/base-injected-transport.d.ts +1 -2
- package/dist/declarations/src/transports/proxy-transport/proxy-message-handler.d.ts +1 -1
- package/dist/declarations/src/transports/wallet-request-handler.d.ts +2 -2
- package/dist/declarations/src/transports/window-transport/window-message-handler.d.ts +1 -1
- package/dist/declarations/src/types.d.ts +2 -1
- package/dist/declarations/src/utils.d.ts +12 -1
- package/dist/declarations/src/wallet.d.ts +6 -4
- package/package.json +8 -8
- package/src/transports/base-provider-transport.ts +8 -5
- package/src/transports/base-wallet-transport.ts +9 -8
- package/src/transports/extension-transport/base-injected-transport.ts +1 -1
- package/src/transports/proxy-transport/proxy-message-handler.ts +3 -5
- package/src/transports/wallet-request-handler.ts +4 -5
- package/src/transports/window-transport/window-message-handler.ts +4 -3
- package/src/types.ts +2 -1
- package/src/utils.ts +34 -11
- package/src/wallet.ts +36 -25
|
@@ -374,13 +374,18 @@ class BaseProviderTransport {
|
|
|
374
374
|
|
|
375
375
|
|
|
376
376
|
if (message.type === EventType.ACCOUNTS_CHANGED) {
|
|
377
|
-
this.accountsChangedPayload =
|
|
377
|
+
this.accountsChangedPayload = {
|
|
378
|
+
accounts: []
|
|
379
|
+
};
|
|
378
380
|
|
|
379
381
|
if (message.data && message.data.length > 0) {
|
|
380
|
-
this.accountsChangedPayload =
|
|
381
|
-
|
|
382
|
+
this.accountsChangedPayload = {
|
|
383
|
+
accounts: [ethers.utils.getAddress(message.data[0])],
|
|
384
|
+
origin: message.origin
|
|
385
|
+
};
|
|
386
|
+
this.events.emit('accountsChanged', this.accountsChangedPayload.accounts, this.accountsChangedPayload.origin);
|
|
382
387
|
} else {
|
|
383
|
-
this.events.emit('accountsChanged', []);
|
|
388
|
+
this.events.emit('accountsChanged', [], message.origin);
|
|
384
389
|
}
|
|
385
390
|
|
|
386
391
|
return;
|
|
@@ -1188,7 +1193,7 @@ class WalletRequestHandler {
|
|
|
1188
1193
|
};
|
|
1189
1194
|
}
|
|
1190
1195
|
|
|
1191
|
-
notifyConnect(connectDetails) {
|
|
1196
|
+
notifyConnect(connectDetails, origin) {
|
|
1192
1197
|
var _connectDetails$sessi;
|
|
1193
1198
|
|
|
1194
1199
|
this.events.emit('connect', connectDetails);
|
|
@@ -1196,13 +1201,12 @@ class WalletRequestHandler {
|
|
|
1196
1201
|
if ((_connectDetails$sessi = connectDetails.session) != null && _connectDetails$sessi.accountAddress) {
|
|
1197
1202
|
var _connectDetails$sessi2;
|
|
1198
1203
|
|
|
1199
|
-
this.events.emit('accountsChanged', [(_connectDetails$sessi2 = connectDetails.session) == null ? void 0 : _connectDetails$sessi2.accountAddress]);
|
|
1204
|
+
this.events.emit('accountsChanged', [(_connectDetails$sessi2 = connectDetails.session) == null ? void 0 : _connectDetails$sessi2.accountAddress], origin);
|
|
1200
1205
|
}
|
|
1201
1206
|
}
|
|
1202
1207
|
|
|
1203
|
-
notifyDisconnect() {
|
|
1204
|
-
this.events.emit('accountsChanged', []);
|
|
1205
|
-
this.events.emit('networks', []);
|
|
1208
|
+
notifyDisconnect(origin) {
|
|
1209
|
+
this.events.emit('accountsChanged', [], origin);
|
|
1206
1210
|
this.events.emit('disconnect');
|
|
1207
1211
|
}
|
|
1208
1212
|
|
|
@@ -1699,7 +1703,7 @@ const hexlifyTransaction = (transaction, allowExtra) => {
|
|
|
1699
1703
|
return result;
|
|
1700
1704
|
};
|
|
1701
1705
|
|
|
1702
|
-
const eip191prefix = ethers.utils.toUtf8Bytes(
|
|
1706
|
+
const eip191prefix = ethers.utils.toUtf8Bytes('\x19Ethereum Signed Message:\n');
|
|
1703
1707
|
const messageToBytes = message => {
|
|
1704
1708
|
if (ethers.utils.isBytes(message) || ethers.utils.isHexString(message)) {
|
|
1705
1709
|
return ethers.utils.arrayify(message);
|
|
@@ -1795,8 +1799,29 @@ const isWalletUpToDate = async (signer, chainId) => {
|
|
|
1795
1799
|
|
|
1796
1800
|
const isUpToDate = isConfigEqual(authChainConfig, requiredChainConfig);
|
|
1797
1801
|
return isDeployed && isUpToDate;
|
|
1798
|
-
};
|
|
1802
|
+
};
|
|
1803
|
+
class LocalStorage {
|
|
1804
|
+
constructor() {}
|
|
1805
|
+
|
|
1806
|
+
static getInstance() {
|
|
1807
|
+
if (!LocalStorage._instance) {
|
|
1808
|
+
LocalStorage._instance = {
|
|
1809
|
+
getItem: key => Promise.resolve(window.localStorage.getItem(key)),
|
|
1810
|
+
setItem: (key, value) => Promise.resolve(window.localStorage.setItem(key, value)),
|
|
1811
|
+
removeItem: key => Promise.resolve(window.localStorage.removeItem(key))
|
|
1812
|
+
};
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
return this._instance;
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
static use(instance) {
|
|
1819
|
+
LocalStorage._instance = instance;
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
} // window.localstorage helper
|
|
1799
1823
|
|
|
1824
|
+
LocalStorage._instance = void 0;
|
|
1800
1825
|
class LocalStore {
|
|
1801
1826
|
constructor(key, def) {
|
|
1802
1827
|
this.def = def;
|
|
@@ -1804,8 +1829,8 @@ class LocalStore {
|
|
|
1804
1829
|
this.key = key;
|
|
1805
1830
|
}
|
|
1806
1831
|
|
|
1807
|
-
get() {
|
|
1808
|
-
const val =
|
|
1832
|
+
async get() {
|
|
1833
|
+
const val = await LocalStorage.getInstance().getItem(this.key);
|
|
1809
1834
|
|
|
1810
1835
|
if (val === null) {
|
|
1811
1836
|
return this.def;
|
|
@@ -1821,11 +1846,11 @@ class LocalStore {
|
|
|
1821
1846
|
}
|
|
1822
1847
|
|
|
1823
1848
|
set(val) {
|
|
1824
|
-
val ?
|
|
1849
|
+
val ? LocalStorage.getInstance().setItem(this.key, JSON.stringify(val)) : LocalStorage.getInstance().removeItem(this.key);
|
|
1825
1850
|
}
|
|
1826
1851
|
|
|
1827
1852
|
del() {
|
|
1828
|
-
|
|
1853
|
+
LocalStorage.getInstance().removeItem(this.key);
|
|
1829
1854
|
}
|
|
1830
1855
|
|
|
1831
1856
|
}
|
|
@@ -2048,11 +2073,11 @@ class BaseWalletTransport {
|
|
|
2048
2073
|
};
|
|
2049
2074
|
|
|
2050
2075
|
this.saveTransportSession = session => {
|
|
2051
|
-
|
|
2076
|
+
LocalStorage.getInstance().setItem(TRANSPORT_SESSION_LS_KEY, JSON.stringify(session));
|
|
2052
2077
|
};
|
|
2053
2078
|
|
|
2054
|
-
this.getCachedTransportSession = ()
|
|
2055
|
-
const session =
|
|
2079
|
+
this.getCachedTransportSession = async function () {
|
|
2080
|
+
const session = await LocalStorage.getInstance().getItem(TRANSPORT_SESSION_LS_KEY);
|
|
2056
2081
|
|
|
2057
2082
|
try {
|
|
2058
2083
|
return session ? JSON.parse(session) : null;
|
|
@@ -2074,9 +2099,9 @@ class BaseWalletTransport {
|
|
|
2074
2099
|
|
|
2075
2100
|
this.notifyDisconnect(error);
|
|
2076
2101
|
});
|
|
2077
|
-
this.walletRequestHandler.on('accountsChanged', accounts => {
|
|
2102
|
+
this.walletRequestHandler.on('accountsChanged', (accounts, origin) => {
|
|
2078
2103
|
if (!this.registered) return;
|
|
2079
|
-
this.notifyAccountsChanged(accounts);
|
|
2104
|
+
this.notifyAccountsChanged(accounts, origin);
|
|
2080
2105
|
});
|
|
2081
2106
|
this.walletRequestHandler.on('networks', networks => {
|
|
2082
2107
|
if (!this.registered) return;
|
|
@@ -2161,11 +2186,12 @@ class BaseWalletTransport {
|
|
|
2161
2186
|
});
|
|
2162
2187
|
}
|
|
2163
2188
|
|
|
2164
|
-
notifyAccountsChanged(accounts) {
|
|
2189
|
+
notifyAccountsChanged(accounts, origin) {
|
|
2165
2190
|
this.sendMessage({
|
|
2166
2191
|
idx: -1,
|
|
2167
2192
|
type: EventType.ACCOUNTS_CHANGED,
|
|
2168
|
-
data: accounts
|
|
2193
|
+
data: accounts,
|
|
2194
|
+
origin: origin
|
|
2169
2195
|
});
|
|
2170
2196
|
}
|
|
2171
2197
|
|
|
@@ -2429,14 +2455,14 @@ class ProxyMessageHandler extends BaseWalletTransport {
|
|
|
2429
2455
|
this._registered = true;
|
|
2430
2456
|
} // note: we can't decide whether to restore the session within register(), because session info is
|
|
2431
2457
|
// received asyncronously via EventType.OPEN after register() is executed.
|
|
2432
|
-
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
2458
|
+
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
2433
2459
|
// because the wallet is already open.
|
|
2434
2460
|
//
|
|
2435
2461
|
// call this method from wallet redirect hander when a session restore is needed
|
|
2436
2462
|
|
|
2437
2463
|
|
|
2438
|
-
restoreSession() {
|
|
2439
|
-
const cachedSession = this.getCachedTransportSession();
|
|
2464
|
+
async restoreSession() {
|
|
2465
|
+
const cachedSession = await this.getCachedTransportSession();
|
|
2440
2466
|
|
|
2441
2467
|
if (cachedSession) {
|
|
2442
2468
|
this.open(cachedSession);
|
|
@@ -2860,7 +2886,7 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
2860
2886
|
this._init = InitState.NIL;
|
|
2861
2887
|
}
|
|
2862
2888
|
|
|
2863
|
-
register(windowHref) {
|
|
2889
|
+
async register(windowHref) {
|
|
2864
2890
|
const isPopup = parent.window.opener !== null;
|
|
2865
2891
|
this._isPopup = isPopup;
|
|
2866
2892
|
|
|
@@ -2878,7 +2904,7 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
2878
2904
|
const isNewWindowSession = !!session.sessionId; // attempt to restore previous session in the case of a redirect or window reload
|
|
2879
2905
|
|
|
2880
2906
|
if (!isNewWindowSession) {
|
|
2881
|
-
session = this.getCachedTransportSession();
|
|
2907
|
+
session = await this.getCachedTransportSession();
|
|
2882
2908
|
}
|
|
2883
2909
|
|
|
2884
2910
|
if (!session) {
|
|
@@ -3298,9 +3324,21 @@ class Wallet {
|
|
|
3298
3324
|
} else {
|
|
3299
3325
|
this.useSession(session, true);
|
|
3300
3326
|
}
|
|
3301
|
-
}); // below will update the account upon wallet connect/disconnect
|
|
3327
|
+
}); // below will update the account upon wallet connect/disconnect - aka, login/logout.
|
|
3328
|
+
// if an origin is provided, this operation should be performed only on that origin
|
|
3329
|
+
// and shouldn't affect the session of the wallet.
|
|
3330
|
+
|
|
3331
|
+
this.transport.messageProvider.on('accountsChanged', (accounts, origin) => {
|
|
3332
|
+
if (origin) {
|
|
3333
|
+
if (accounts.length > 0) {
|
|
3334
|
+
this.useSession({
|
|
3335
|
+
accountAddress: accounts[0]
|
|
3336
|
+
}, true);
|
|
3337
|
+
}
|
|
3338
|
+
|
|
3339
|
+
return;
|
|
3340
|
+
}
|
|
3302
3341
|
|
|
3303
|
-
this.transport.messageProvider.on('accountsChanged', accounts => {
|
|
3304
3342
|
if (!accounts || accounts.length === 0 || accounts[0] === '') {
|
|
3305
3343
|
this.clearSession();
|
|
3306
3344
|
} else {
|
|
@@ -3322,11 +3360,11 @@ class Wallet {
|
|
|
3322
3360
|
}, true);
|
|
3323
3361
|
}); // Load existing session from localStorage
|
|
3324
3362
|
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
}
|
|
3363
|
+
this.loadSession().then(session => {
|
|
3364
|
+
if (session) {
|
|
3365
|
+
this.useSession(session, false);
|
|
3366
|
+
}
|
|
3367
|
+
});
|
|
3330
3368
|
};
|
|
3331
3369
|
|
|
3332
3370
|
this.connect = async function (options) {
|
|
@@ -3334,7 +3372,7 @@ class Wallet {
|
|
|
3334
3372
|
_this.disconnect();
|
|
3335
3373
|
}
|
|
3336
3374
|
|
|
3337
|
-
if (_this.isConnected() && _this.isSiteConnected(options == null ? void 0 : options.origin) && !!_this.session && !(options != null && options.authorize) && !(options != null && options.askForEmail)) {
|
|
3375
|
+
if (_this.isConnected() && (await _this.isSiteConnected(options == null ? void 0 : options.origin)) && !!_this.session && !(options != null && options.authorize) && !(options != null && options.askForEmail)) {
|
|
3338
3376
|
return {
|
|
3339
3377
|
connected: true,
|
|
3340
3378
|
session: _this.session,
|
|
@@ -3460,8 +3498,8 @@ class Wallet {
|
|
|
3460
3498
|
this.transport.messageProvider.closeWallet();
|
|
3461
3499
|
};
|
|
3462
3500
|
|
|
3463
|
-
this.loadSession = ()
|
|
3464
|
-
const data =
|
|
3501
|
+
this.loadSession = async function () {
|
|
3502
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3465
3503
|
|
|
3466
3504
|
if (!data || data === '') {
|
|
3467
3505
|
return undefined;
|
|
@@ -3479,7 +3517,7 @@ class Wallet {
|
|
|
3479
3517
|
this.saveSession = session => {
|
|
3480
3518
|
logger.debug('wallet provider: saving session');
|
|
3481
3519
|
const data = JSON.stringify(session);
|
|
3482
|
-
|
|
3520
|
+
LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3483
3521
|
};
|
|
3484
3522
|
|
|
3485
3523
|
this.useSession = async function (session, autoSave = true) {
|
|
@@ -3526,6 +3564,10 @@ class Wallet {
|
|
|
3526
3564
|
this.config.defaultNetworkId = 'mainnet';
|
|
3527
3565
|
}
|
|
3528
3566
|
|
|
3567
|
+
if (config != null && config.localStorage) {
|
|
3568
|
+
LocalStorage.use(config.localStorage);
|
|
3569
|
+
}
|
|
3570
|
+
|
|
3529
3571
|
this.transport = {};
|
|
3530
3572
|
this.networks = [];
|
|
3531
3573
|
this.providers = {};
|
|
@@ -3534,9 +3576,9 @@ class Wallet {
|
|
|
3534
3576
|
this.init();
|
|
3535
3577
|
}
|
|
3536
3578
|
|
|
3537
|
-
addConnectedSite(origin) {
|
|
3579
|
+
async addConnectedSite(origin) {
|
|
3538
3580
|
origin = origin || window.location.origin;
|
|
3539
|
-
const connectedSites = this.connectedSites.get();
|
|
3581
|
+
const connectedSites = await this.connectedSites.get();
|
|
3540
3582
|
|
|
3541
3583
|
if (connectedSites) {
|
|
3542
3584
|
if (connectedSites.includes(origin)) {
|
|
@@ -3549,8 +3591,8 @@ class Wallet {
|
|
|
3549
3591
|
}
|
|
3550
3592
|
}
|
|
3551
3593
|
|
|
3552
|
-
removeConnectedSite(origin) {
|
|
3553
|
-
const authorized = this.connectedSites.get();
|
|
3594
|
+
async removeConnectedSite(origin) {
|
|
3595
|
+
const authorized = await this.connectedSites.get();
|
|
3554
3596
|
|
|
3555
3597
|
if (authorized) {
|
|
3556
3598
|
this.connectedSites.set(authorized.filter(domain => domain !== origin));
|
|
@@ -3561,8 +3603,8 @@ class Wallet {
|
|
|
3561
3603
|
return this.connectedSites.get();
|
|
3562
3604
|
}
|
|
3563
3605
|
|
|
3564
|
-
isSiteConnected(origin) {
|
|
3565
|
-
const authorized = this.connectedSites.get();
|
|
3606
|
+
async isSiteConnected(origin) {
|
|
3607
|
+
const authorized = await this.connectedSites.get();
|
|
3566
3608
|
return !!authorized && authorized.includes(origin || window.location.origin);
|
|
3567
3609
|
}
|
|
3568
3610
|
|
|
@@ -3726,7 +3768,7 @@ class Wallet {
|
|
|
3726
3768
|
var _this$transport$cache;
|
|
3727
3769
|
|
|
3728
3770
|
logger.debug('wallet provider: clearing session');
|
|
3729
|
-
|
|
3771
|
+
LocalStorage.getInstance().removeItem('@sequence.session');
|
|
3730
3772
|
this.session = undefined;
|
|
3731
3773
|
this.networks = [];
|
|
3732
3774
|
this.providers = {};
|
|
@@ -3747,4 +3789,4 @@ const DefaultProviderConfig = {
|
|
|
3747
3789
|
}
|
|
3748
3790
|
};
|
|
3749
3791
|
|
|
3750
|
-
export { BaseInjectedTransport, BaseProviderTransport, BaseWalletTransport, CHANNEL_ID, DefaultProviderConfig, ErrSignedInRequired, EventType, ExtensionMessageHandler, ExtensionMessageProvider, InitState, LocalStore, LocalWeb3Provider, MuxMessageProvider, OpenState, PROVIDER_OPEN_TIMEOUT, ProviderError, ProxyMessageChannel, ProxyMessageChannelPort, ProxyMessageHandler, ProxyMessageProvider, Wallet, WalletRequestHandler, Web3Provider, Web3Signer, WindowMessageHandler, WindowMessageProvider, WindowSessionParams, isBrowserExtension, isSequenceProvider, isValidMessageSignature, isValidSignature, isValidTypedDataSignature, isWalletUpToDate, messageToBytes, nextMessageIdx, prefixEIP191Message, recoverWalletConfig };
|
|
3792
|
+
export { BaseInjectedTransport, BaseProviderTransport, BaseWalletTransport, CHANNEL_ID, DefaultProviderConfig, ErrSignedInRequired, EventType, ExtensionMessageHandler, ExtensionMessageProvider, InitState, LocalStorage, LocalStore, LocalWeb3Provider, MuxMessageProvider, OpenState, PROVIDER_OPEN_TIMEOUT, ProviderError, ProxyMessageChannel, ProxyMessageChannelPort, ProxyMessageHandler, ProxyMessageProvider, Wallet, WalletRequestHandler, Web3Provider, Web3Signer, WindowMessageHandler, WindowMessageProvider, WindowSessionParams, isBrowserExtension, isSequenceProvider, isValidMessageSignature, isValidSignature, isValidTypedDataSignature, isWalletUpToDate, messageToBytes, nextMessageIdx, prefixEIP191Message, recoverWalletConfig };
|
|
@@ -13,7 +13,10 @@ export declare abstract class BaseProviderTransport implements ProviderTransport
|
|
|
13
13
|
session?: WalletSession;
|
|
14
14
|
} | undefined;
|
|
15
15
|
protected connectPayload: ConnectDetails | undefined;
|
|
16
|
-
protected accountsChangedPayload:
|
|
16
|
+
protected accountsChangedPayload: {
|
|
17
|
+
accounts: string[];
|
|
18
|
+
origin?: string;
|
|
19
|
+
} | undefined;
|
|
17
20
|
protected networksPayload: NetworkConfig[] | undefined;
|
|
18
21
|
protected walletContextPayload: WalletContext | undefined;
|
|
19
22
|
protected _sessionId?: string;
|
|
@@ -26,7 +26,7 @@ export declare abstract class BaseWalletTransport implements WalletTransport {
|
|
|
26
26
|
notifyClose(error?: ProviderRpcError): void;
|
|
27
27
|
notifyConnect(connectDetails: ConnectDetails): void;
|
|
28
28
|
notifyDisconnect(error?: ProviderRpcError): void;
|
|
29
|
-
notifyAccountsChanged(accounts: string[]): void;
|
|
29
|
+
notifyAccountsChanged(accounts: string[], origin?: string): void;
|
|
30
30
|
notifyChainChanged(chainIdHex: string): void;
|
|
31
31
|
notifyNetworks(networks: NetworkConfig[]): void;
|
|
32
32
|
notifyWalletContext(walletContext: WalletContext): void;
|
|
@@ -34,5 +34,5 @@ export declare abstract class BaseWalletTransport implements WalletTransport {
|
|
|
34
34
|
private init;
|
|
35
35
|
protected open: ({ sessionId, intent, networkId }: TransportSession) => Promise<boolean>;
|
|
36
36
|
private saveTransportSession;
|
|
37
|
-
protected getCachedTransportSession: () => TransportSession | null
|
|
37
|
+
protected getCachedTransportSession: () => Promise<TransportSession | null>;
|
|
38
38
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter2 as EventEmitter } from 'eventemitter2';
|
|
2
2
|
import { ProviderMessageResponseCallback, ProviderMessageRequest, ProviderMessageResponse } from '../../types';
|
|
3
|
-
interface Stream {
|
|
3
|
+
export interface Stream {
|
|
4
4
|
on(ev: string | symbol, fn: (...args: any[]) => void): void;
|
|
5
5
|
writable: boolean;
|
|
6
6
|
write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
|
|
@@ -15,4 +15,3 @@ export declare abstract class BaseInjectedTransport extends EventEmitter {
|
|
|
15
15
|
protected sendMessageRequest: (message: ProviderMessageRequest) => Promise<ProviderMessageResponse>;
|
|
16
16
|
private sendMessage;
|
|
17
17
|
}
|
|
18
|
-
export {};
|
|
@@ -6,7 +6,7 @@ export declare class ProxyMessageHandler extends BaseWalletTransport {
|
|
|
6
6
|
private port;
|
|
7
7
|
constructor(walletRequestHandler: WalletRequestHandler, port: ProxyMessageChannelPort);
|
|
8
8
|
register(): void;
|
|
9
|
-
restoreSession(): void
|
|
9
|
+
restoreSession(): Promise<void>;
|
|
10
10
|
unregister(): void;
|
|
11
11
|
sendMessage(message: ProviderMessage<any>): void;
|
|
12
12
|
}
|
|
@@ -41,8 +41,8 @@ export declare class WalletRequestHandler implements ExternalProvider, JsonRpcHa
|
|
|
41
41
|
setDefaultNetwork(chainId: string | number, notifyNetworks?: boolean): Promise<number | undefined>;
|
|
42
42
|
getNetworks(jsonRpcResponse?: boolean): Promise<NetworkConfig[]>;
|
|
43
43
|
walletSession(): Promise<WalletSession | undefined>;
|
|
44
|
-
notifyConnect(connectDetails: ConnectDetails): void;
|
|
45
|
-
notifyDisconnect(): void;
|
|
44
|
+
notifyConnect(connectDetails: ConnectDetails, origin?: string): void;
|
|
45
|
+
notifyDisconnect(origin?: string): void;
|
|
46
46
|
notifyNetworks(networks?: NetworkConfig[]): Promise<void>;
|
|
47
47
|
notifyWalletContext(): Promise<void>;
|
|
48
48
|
notifyClose(error?: ProviderRpcError): void;
|
|
@@ -5,7 +5,7 @@ export declare class WindowMessageHandler extends BaseWalletTransport {
|
|
|
5
5
|
protected parentWindow: Window;
|
|
6
6
|
private _isPopup;
|
|
7
7
|
constructor(walletRequestHandler: WalletRequestHandler);
|
|
8
|
-
register(windowHref?: any): void
|
|
8
|
+
register(windowHref?: any): Promise<void>;
|
|
9
9
|
unregister(): void;
|
|
10
10
|
private onWindowEvent;
|
|
11
11
|
sendMessage(message: ProviderMessage<any>): void;
|
|
@@ -33,6 +33,7 @@ export interface ProviderMessage<T> {
|
|
|
33
33
|
type: string;
|
|
34
34
|
data: T;
|
|
35
35
|
chainId?: number;
|
|
36
|
+
origin?: string;
|
|
36
37
|
}
|
|
37
38
|
export declare type ProviderMessageRequest = ProviderMessage<JsonRpcRequest>;
|
|
38
39
|
export declare type ProviderMessageResponse = ProviderMessage<JsonRpcResponse>;
|
|
@@ -87,7 +88,7 @@ export interface WalletEventTypes {
|
|
|
87
88
|
close: (error?: ProviderRpcError) => void;
|
|
88
89
|
connect: (connectDetails: ConnectDetails) => void;
|
|
89
90
|
disconnect: (error?: ProviderRpcError) => void;
|
|
90
|
-
accountsChanged: (accounts: string[]) => void;
|
|
91
|
+
accountsChanged: (accounts: string[], origin?: string) => void;
|
|
91
92
|
chainChanged: (chainIdHex: string) => void;
|
|
92
93
|
networks: (networks: NetworkConfig[]) => void;
|
|
93
94
|
walletContext: (walletContext: WalletContext) => void;
|
|
@@ -19,11 +19,22 @@ export declare const isBrowserExtension: () => boolean;
|
|
|
19
19
|
* @return {Promise<boolean>} Promise that returns true if the wallet is up to date, false otherwise
|
|
20
20
|
*/
|
|
21
21
|
export declare const isWalletUpToDate: (signer: Signer, chainId: number) => Promise<boolean>;
|
|
22
|
+
export interface ItemStore {
|
|
23
|
+
getItem(key: string): Promise<string | null>;
|
|
24
|
+
setItem(key: string, value: string): Promise<void>;
|
|
25
|
+
removeItem(key: string): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export declare class LocalStorage {
|
|
28
|
+
private static _instance;
|
|
29
|
+
private constructor();
|
|
30
|
+
static getInstance(): ItemStore;
|
|
31
|
+
static use(instance: ItemStore): void;
|
|
32
|
+
}
|
|
22
33
|
export declare class LocalStore<T extends Object = string> {
|
|
23
34
|
def?: T | undefined;
|
|
24
35
|
readonly key: string;
|
|
25
36
|
constructor(key: string, def?: T | undefined);
|
|
26
|
-
get(): T | undefined
|
|
37
|
+
get(): Promise<T | undefined>;
|
|
27
38
|
set(val: T | undefined): void;
|
|
28
39
|
del(): void;
|
|
29
40
|
}
|
|
@@ -3,6 +3,7 @@ import { WalletConfig, WalletState } from '@0xsequence/config';
|
|
|
3
3
|
import { Web3Provider, Web3Signer } from './provider';
|
|
4
4
|
import { ProxyMessageChannelPort } from './transports';
|
|
5
5
|
import { WalletSession, ProviderEventTypes, ConnectOptions, OpenWalletIntent, ConnectDetails } from './types';
|
|
6
|
+
import { ItemStore } from './utils';
|
|
6
7
|
import { WalletUtils } from './utils/index';
|
|
7
8
|
import { Runtime } from 'webextension-polyfill-ts';
|
|
8
9
|
export interface WalletProvider {
|
|
@@ -39,10 +40,10 @@ export declare class Wallet implements WalletProvider {
|
|
|
39
40
|
constructor(defaultNetworkId?: string | number, config?: Partial<ProviderConfig>);
|
|
40
41
|
private init;
|
|
41
42
|
connect: (options?: ConnectOptions | undefined) => Promise<ConnectDetails>;
|
|
42
|
-
addConnectedSite(origin: string | undefined): void
|
|
43
|
-
removeConnectedSite(origin: string): void
|
|
44
|
-
getConnectedSites(): string[] | undefined
|
|
45
|
-
|
|
43
|
+
addConnectedSite(origin: string | undefined): Promise<void>;
|
|
44
|
+
removeConnectedSite(origin: string): Promise<void>;
|
|
45
|
+
getConnectedSites(): Promise<string[] | undefined>;
|
|
46
|
+
isSiteConnected(origin: string | undefined): Promise<boolean>;
|
|
46
47
|
authorize: (options?: ConnectOptions | undefined) => Promise<ConnectDetails>;
|
|
47
48
|
disconnect(): void;
|
|
48
49
|
getProviderConfig(): ProviderConfig;
|
|
@@ -77,6 +78,7 @@ export declare class Wallet implements WalletProvider {
|
|
|
77
78
|
private clearSession;
|
|
78
79
|
}
|
|
79
80
|
export interface ProviderConfig {
|
|
81
|
+
localStorage?: ItemStore;
|
|
80
82
|
walletAppURL: string;
|
|
81
83
|
networks?: Partial<NetworkConfig>[];
|
|
82
84
|
networkRpcUrl?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xsequence/provider",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.2",
|
|
4
4
|
"description": "provider sub-package for Sequence",
|
|
5
5
|
"repository": "https://github.com/0xsequence/sequence.js/tree/master/packages/provider",
|
|
6
6
|
"source": "src/index.ts",
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
"typecheck": "tsc --noEmit"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@0xsequence/abi": "^0.
|
|
17
|
-
"@0xsequence/auth": "^0.
|
|
18
|
-
"@0xsequence/config": "^0.
|
|
19
|
-
"@0xsequence/network": "^0.
|
|
20
|
-
"@0xsequence/transactions": "^0.
|
|
21
|
-
"@0xsequence/utils": "^0.
|
|
22
|
-
"@0xsequence/wallet": "^0.
|
|
16
|
+
"@0xsequence/abi": "^0.39.2",
|
|
17
|
+
"@0xsequence/auth": "^0.39.2",
|
|
18
|
+
"@0xsequence/config": "^0.39.2",
|
|
19
|
+
"@0xsequence/network": "^0.39.2",
|
|
20
|
+
"@0xsequence/transactions": "^0.39.2",
|
|
21
|
+
"@0xsequence/utils": "^0.39.2",
|
|
22
|
+
"@0xsequence/wallet": "^0.39.2",
|
|
23
23
|
"@ethersproject/abstract-signer": "^5.5.0",
|
|
24
24
|
"@ethersproject/hash": "^5.5.0",
|
|
25
25
|
"@ethersproject/providers": "^5.5.1",
|
|
@@ -37,7 +37,7 @@ export abstract class BaseProviderTransport implements ProviderTransport {
|
|
|
37
37
|
|
|
38
38
|
protected openPayload: { sessionId?: string; session?: WalletSession } | undefined
|
|
39
39
|
protected connectPayload: ConnectDetails | undefined
|
|
40
|
-
protected accountsChangedPayload: string | undefined
|
|
40
|
+
protected accountsChangedPayload: { accounts: string[]; origin?: string } | undefined
|
|
41
41
|
protected networksPayload: NetworkConfig[] | undefined
|
|
42
42
|
protected walletContextPayload: WalletContext | undefined
|
|
43
43
|
|
|
@@ -224,12 +224,15 @@ export abstract class BaseProviderTransport implements ProviderTransport {
|
|
|
224
224
|
|
|
225
225
|
// ACCOUNTS_CHANGED -- when a user logs in or out
|
|
226
226
|
if (message.type === EventType.ACCOUNTS_CHANGED) {
|
|
227
|
-
this.accountsChangedPayload =
|
|
227
|
+
this.accountsChangedPayload = { accounts: [] }
|
|
228
228
|
if (message.data && message.data.length > 0) {
|
|
229
|
-
this.accountsChangedPayload =
|
|
230
|
-
|
|
229
|
+
this.accountsChangedPayload = {
|
|
230
|
+
accounts: [ethers.utils.getAddress(message.data[0])],
|
|
231
|
+
origin: message.origin
|
|
232
|
+
}
|
|
233
|
+
this.events.emit('accountsChanged', this.accountsChangedPayload.accounts, this.accountsChangedPayload.origin)
|
|
231
234
|
} else {
|
|
232
|
-
this.events.emit('accountsChanged', [])
|
|
235
|
+
this.events.emit('accountsChanged', [], message.origin)
|
|
233
236
|
}
|
|
234
237
|
return
|
|
235
238
|
}
|
|
@@ -21,7 +21,7 @@ import { logger, sanitizeAlphanumeric, sanitizeHost, sanitizeNumberString } from
|
|
|
21
21
|
import { AuthorizationOptions } from '@0xsequence/auth'
|
|
22
22
|
|
|
23
23
|
import { PROVIDER_OPEN_TIMEOUT } from './base-provider-transport'
|
|
24
|
-
import { isBrowserExtension } from '../utils'
|
|
24
|
+
import { isBrowserExtension, LocalStorage } from '../utils'
|
|
25
25
|
|
|
26
26
|
const TRANSPORT_SESSION_LS_KEY = '@sequence.transportSession'
|
|
27
27
|
|
|
@@ -55,9 +55,9 @@ export abstract class BaseWalletTransport implements WalletTransport {
|
|
|
55
55
|
this.notifyDisconnect(error)
|
|
56
56
|
})
|
|
57
57
|
|
|
58
|
-
this.walletRequestHandler.on('accountsChanged', (accounts: string[]) => {
|
|
58
|
+
this.walletRequestHandler.on('accountsChanged', (accounts: string[], origin?: string) => {
|
|
59
59
|
if (!this.registered) return
|
|
60
|
-
this.notifyAccountsChanged(accounts)
|
|
60
|
+
this.notifyAccountsChanged(accounts, origin)
|
|
61
61
|
})
|
|
62
62
|
|
|
63
63
|
this.walletRequestHandler.on('networks', (networks: NetworkConfig[]) => {
|
|
@@ -206,11 +206,12 @@ export abstract class BaseWalletTransport implements WalletTransport {
|
|
|
206
206
|
})
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
notifyAccountsChanged(accounts: string[]) {
|
|
209
|
+
notifyAccountsChanged(accounts: string[], origin?: string) {
|
|
210
210
|
this.sendMessage({
|
|
211
211
|
idx: -1,
|
|
212
212
|
type: EventType.ACCOUNTS_CHANGED,
|
|
213
|
-
data: accounts
|
|
213
|
+
data: accounts,
|
|
214
|
+
origin: origin
|
|
214
215
|
})
|
|
215
216
|
}
|
|
216
217
|
|
|
@@ -435,11 +436,11 @@ export abstract class BaseWalletTransport implements WalletTransport {
|
|
|
435
436
|
}
|
|
436
437
|
|
|
437
438
|
private saveTransportSession = (session: TransportSession) => {
|
|
438
|
-
|
|
439
|
+
LocalStorage.getInstance().setItem(TRANSPORT_SESSION_LS_KEY, JSON.stringify(session))
|
|
439
440
|
}
|
|
440
441
|
|
|
441
|
-
protected getCachedTransportSession = (): TransportSession | null => {
|
|
442
|
-
const session =
|
|
442
|
+
protected getCachedTransportSession = async (): Promise<TransportSession | null> => {
|
|
443
|
+
const session = await LocalStorage.getInstance().getItem(TRANSPORT_SESSION_LS_KEY)
|
|
443
444
|
|
|
444
445
|
try {
|
|
445
446
|
return session ? (JSON.parse(session) as TransportSession) : null
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
ProviderMessageResponse
|
|
10
10
|
} from '../../types'
|
|
11
11
|
|
|
12
|
-
interface Stream {
|
|
12
|
+
export interface Stream {
|
|
13
13
|
on(ev: string | symbol, fn: (...args: any[]) => void): void
|
|
14
14
|
writable: boolean
|
|
15
15
|
write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean
|
|
@@ -4,7 +4,6 @@ import { InitState, ProviderMessage } from '../../types'
|
|
|
4
4
|
import { ProxyMessageChannelPort } from './proxy-message-channel'
|
|
5
5
|
|
|
6
6
|
export class ProxyMessageHandler extends BaseWalletTransport {
|
|
7
|
-
|
|
8
7
|
private port: ProxyMessageChannelPort
|
|
9
8
|
|
|
10
9
|
constructor(walletRequestHandler: WalletRequestHandler, port: ProxyMessageChannelPort) {
|
|
@@ -22,12 +21,12 @@ export class ProxyMessageHandler extends BaseWalletTransport {
|
|
|
22
21
|
|
|
23
22
|
// note: we can't decide whether to restore the session within register(), because session info is
|
|
24
23
|
// received asyncronously via EventType.OPEN after register() is executed.
|
|
25
|
-
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
24
|
+
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
26
25
|
// because the wallet is already open.
|
|
27
26
|
//
|
|
28
27
|
// call this method from wallet redirect hander when a session restore is needed
|
|
29
|
-
restoreSession() {
|
|
30
|
-
const cachedSession = this.getCachedTransportSession()
|
|
28
|
+
async restoreSession() {
|
|
29
|
+
const cachedSession = await this.getCachedTransportSession()
|
|
31
30
|
if (cachedSession) {
|
|
32
31
|
this.open(cachedSession)
|
|
33
32
|
}
|
|
@@ -42,5 +41,4 @@ export class ProxyMessageHandler extends BaseWalletTransport {
|
|
|
42
41
|
sendMessage(message: ProviderMessage<any>) {
|
|
43
42
|
this.port.sendMessage(message)
|
|
44
43
|
}
|
|
45
|
-
|
|
46
44
|
}
|
|
@@ -702,16 +702,15 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
702
702
|
}
|
|
703
703
|
}
|
|
704
704
|
|
|
705
|
-
notifyConnect(connectDetails: ConnectDetails) {
|
|
705
|
+
notifyConnect(connectDetails: ConnectDetails, origin?: string) {
|
|
706
706
|
this.events.emit('connect', connectDetails)
|
|
707
707
|
if (connectDetails.session?.accountAddress) {
|
|
708
|
-
this.events.emit('accountsChanged', [connectDetails.session?.accountAddress])
|
|
708
|
+
this.events.emit('accountsChanged', [connectDetails.session?.accountAddress], origin)
|
|
709
709
|
}
|
|
710
710
|
}
|
|
711
711
|
|
|
712
|
-
notifyDisconnect() {
|
|
713
|
-
this.events.emit('accountsChanged', [])
|
|
714
|
-
this.events.emit('networks', [])
|
|
712
|
+
notifyDisconnect(origin?: string) {
|
|
713
|
+
this.events.emit('accountsChanged', [], origin)
|
|
715
714
|
this.events.emit('disconnect')
|
|
716
715
|
}
|
|
717
716
|
|