@0xsequence/provider 0.38.0 → 0.39.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 +55 -29
- package/dist/0xsequence-provider.cjs.prod.js +55 -29
- package/dist/0xsequence-provider.esm.js +55 -30
- package/dist/declarations/src/transports/base-wallet-transport.d.ts +1 -1
- package/dist/declarations/src/transports/proxy-transport/proxy-message-handler.d.ts +1 -1
- package/dist/declarations/src/transports/window-transport/window-message-handler.d.ts +1 -1
- package/dist/declarations/src/types.d.ts +5 -0
- package/dist/declarations/src/utils.d.ts +12 -1
- package/dist/declarations/src/wallet.d.ts +5 -3
- package/package.json +8 -8
- package/src/transports/base-wallet-transport.ts +4 -4
- package/src/transports/proxy-transport/proxy-message-handler.ts +3 -5
- package/src/transports/window-transport/window-message-handler.ts +4 -3
- package/src/types.ts +6 -0
- package/src/utils.ts +34 -11
- package/src/wallet.ts +24 -22
|
@@ -1703,7 +1703,7 @@ const hexlifyTransaction = (transaction, allowExtra) => {
|
|
|
1703
1703
|
return result;
|
|
1704
1704
|
};
|
|
1705
1705
|
|
|
1706
|
-
const eip191prefix = ethers.ethers.utils.toUtf8Bytes(
|
|
1706
|
+
const eip191prefix = ethers.ethers.utils.toUtf8Bytes('\x19Ethereum Signed Message:\n');
|
|
1707
1707
|
const messageToBytes = message => {
|
|
1708
1708
|
if (ethers.ethers.utils.isBytes(message) || ethers.ethers.utils.isHexString(message)) {
|
|
1709
1709
|
return ethers.ethers.utils.arrayify(message);
|
|
@@ -1799,8 +1799,29 @@ const isWalletUpToDate = async (signer, chainId) => {
|
|
|
1799
1799
|
|
|
1800
1800
|
const isUpToDate = config.isConfigEqual(authChainConfig, requiredChainConfig);
|
|
1801
1801
|
return isDeployed && isUpToDate;
|
|
1802
|
-
};
|
|
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
|
+
}
|
|
1803
1821
|
|
|
1822
|
+
} // window.localstorage helper
|
|
1823
|
+
|
|
1824
|
+
LocalStorage._instance = void 0;
|
|
1804
1825
|
class LocalStore {
|
|
1805
1826
|
constructor(key, def) {
|
|
1806
1827
|
this.def = def;
|
|
@@ -1808,8 +1829,8 @@ class LocalStore {
|
|
|
1808
1829
|
this.key = key;
|
|
1809
1830
|
}
|
|
1810
1831
|
|
|
1811
|
-
get() {
|
|
1812
|
-
const val =
|
|
1832
|
+
async get() {
|
|
1833
|
+
const val = await LocalStorage.getInstance().getItem(this.key);
|
|
1813
1834
|
|
|
1814
1835
|
if (val === null) {
|
|
1815
1836
|
return this.def;
|
|
@@ -1825,11 +1846,11 @@ class LocalStore {
|
|
|
1825
1846
|
}
|
|
1826
1847
|
|
|
1827
1848
|
set(val) {
|
|
1828
|
-
val ?
|
|
1849
|
+
val ? LocalStorage.getInstance().setItem(this.key, JSON.stringify(val)) : LocalStorage.getInstance().removeItem(this.key);
|
|
1829
1850
|
}
|
|
1830
1851
|
|
|
1831
1852
|
del() {
|
|
1832
|
-
|
|
1853
|
+
LocalStorage.getInstance().removeItem(this.key);
|
|
1833
1854
|
}
|
|
1834
1855
|
|
|
1835
1856
|
}
|
|
@@ -2052,11 +2073,11 @@ class BaseWalletTransport {
|
|
|
2052
2073
|
};
|
|
2053
2074
|
|
|
2054
2075
|
this.saveTransportSession = session => {
|
|
2055
|
-
|
|
2076
|
+
LocalStorage.getInstance().setItem(TRANSPORT_SESSION_LS_KEY, JSON.stringify(session));
|
|
2056
2077
|
};
|
|
2057
2078
|
|
|
2058
|
-
this.getCachedTransportSession = ()
|
|
2059
|
-
const session =
|
|
2079
|
+
this.getCachedTransportSession = async function () {
|
|
2080
|
+
const session = await LocalStorage.getInstance().getItem(TRANSPORT_SESSION_LS_KEY);
|
|
2060
2081
|
|
|
2061
2082
|
try {
|
|
2062
2083
|
return session ? JSON.parse(session) : null;
|
|
@@ -2433,14 +2454,14 @@ class ProxyMessageHandler extends BaseWalletTransport {
|
|
|
2433
2454
|
this._registered = true;
|
|
2434
2455
|
} // note: we can't decide whether to restore the session within register(), because session info is
|
|
2435
2456
|
// received asyncronously via EventType.OPEN after register() is executed.
|
|
2436
|
-
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
2457
|
+
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
2437
2458
|
// because the wallet is already open.
|
|
2438
2459
|
//
|
|
2439
2460
|
// call this method from wallet redirect hander when a session restore is needed
|
|
2440
2461
|
|
|
2441
2462
|
|
|
2442
|
-
restoreSession() {
|
|
2443
|
-
const cachedSession = this.getCachedTransportSession();
|
|
2463
|
+
async restoreSession() {
|
|
2464
|
+
const cachedSession = await this.getCachedTransportSession();
|
|
2444
2465
|
|
|
2445
2466
|
if (cachedSession) {
|
|
2446
2467
|
this.open(cachedSession);
|
|
@@ -2864,7 +2885,7 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
2864
2885
|
this._init = exports.InitState.NIL;
|
|
2865
2886
|
}
|
|
2866
2887
|
|
|
2867
|
-
register(windowHref) {
|
|
2888
|
+
async register(windowHref) {
|
|
2868
2889
|
const isPopup = parent.window.opener !== null;
|
|
2869
2890
|
this._isPopup = isPopup;
|
|
2870
2891
|
|
|
@@ -2882,7 +2903,7 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
2882
2903
|
const isNewWindowSession = !!session.sessionId; // attempt to restore previous session in the case of a redirect or window reload
|
|
2883
2904
|
|
|
2884
2905
|
if (!isNewWindowSession) {
|
|
2885
|
-
session = this.getCachedTransportSession();
|
|
2906
|
+
session = await this.getCachedTransportSession();
|
|
2886
2907
|
}
|
|
2887
2908
|
|
|
2888
2909
|
if (!session) {
|
|
@@ -3326,11 +3347,11 @@ class Wallet {
|
|
|
3326
3347
|
}, true);
|
|
3327
3348
|
}); // Load existing session from localStorage
|
|
3328
3349
|
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
}
|
|
3350
|
+
this.loadSession().then(session => {
|
|
3351
|
+
if (session) {
|
|
3352
|
+
this.useSession(session, false);
|
|
3353
|
+
}
|
|
3354
|
+
});
|
|
3334
3355
|
};
|
|
3335
3356
|
|
|
3336
3357
|
this.connect = async function (options) {
|
|
@@ -3464,8 +3485,8 @@ class Wallet {
|
|
|
3464
3485
|
this.transport.messageProvider.closeWallet();
|
|
3465
3486
|
};
|
|
3466
3487
|
|
|
3467
|
-
this.loadSession = ()
|
|
3468
|
-
const data =
|
|
3488
|
+
this.loadSession = async function () {
|
|
3489
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3469
3490
|
|
|
3470
3491
|
if (!data || data === '') {
|
|
3471
3492
|
return undefined;
|
|
@@ -3483,7 +3504,7 @@ class Wallet {
|
|
|
3483
3504
|
this.saveSession = session => {
|
|
3484
3505
|
utils.logger.debug('wallet provider: saving session');
|
|
3485
3506
|
const data = JSON.stringify(session);
|
|
3486
|
-
|
|
3507
|
+
LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3487
3508
|
};
|
|
3488
3509
|
|
|
3489
3510
|
this.useSession = async function (session, autoSave = true) {
|
|
@@ -3530,6 +3551,10 @@ class Wallet {
|
|
|
3530
3551
|
this.config.defaultNetworkId = 'mainnet';
|
|
3531
3552
|
}
|
|
3532
3553
|
|
|
3554
|
+
if (config != null && config.localStorage) {
|
|
3555
|
+
LocalStorage.use(config.localStorage);
|
|
3556
|
+
}
|
|
3557
|
+
|
|
3533
3558
|
this.transport = {};
|
|
3534
3559
|
this.networks = [];
|
|
3535
3560
|
this.providers = {};
|
|
@@ -3538,9 +3563,9 @@ class Wallet {
|
|
|
3538
3563
|
this.init();
|
|
3539
3564
|
}
|
|
3540
3565
|
|
|
3541
|
-
addConnectedSite(origin) {
|
|
3566
|
+
async addConnectedSite(origin) {
|
|
3542
3567
|
origin = origin || window.location.origin;
|
|
3543
|
-
const connectedSites = this.connectedSites.get();
|
|
3568
|
+
const connectedSites = await this.connectedSites.get();
|
|
3544
3569
|
|
|
3545
3570
|
if (connectedSites) {
|
|
3546
3571
|
if (connectedSites.includes(origin)) {
|
|
@@ -3553,8 +3578,8 @@ class Wallet {
|
|
|
3553
3578
|
}
|
|
3554
3579
|
}
|
|
3555
3580
|
|
|
3556
|
-
removeConnectedSite(origin) {
|
|
3557
|
-
const authorized = this.connectedSites.get();
|
|
3581
|
+
async removeConnectedSite(origin) {
|
|
3582
|
+
const authorized = await this.connectedSites.get();
|
|
3558
3583
|
|
|
3559
3584
|
if (authorized) {
|
|
3560
3585
|
this.connectedSites.set(authorized.filter(domain => domain !== origin));
|
|
@@ -3565,8 +3590,8 @@ class Wallet {
|
|
|
3565
3590
|
return this.connectedSites.get();
|
|
3566
3591
|
}
|
|
3567
3592
|
|
|
3568
|
-
isSiteConnected(origin) {
|
|
3569
|
-
const authorized = this.connectedSites.get();
|
|
3593
|
+
async isSiteConnected(origin) {
|
|
3594
|
+
const authorized = await this.connectedSites.get();
|
|
3570
3595
|
return !!authorized && authorized.includes(origin || window.location.origin);
|
|
3571
3596
|
}
|
|
3572
3597
|
|
|
@@ -3730,7 +3755,7 @@ class Wallet {
|
|
|
3730
3755
|
var _this$transport$cache;
|
|
3731
3756
|
|
|
3732
3757
|
utils.logger.debug('wallet provider: clearing session');
|
|
3733
|
-
|
|
3758
|
+
LocalStorage.getInstance().removeItem('@sequence.session');
|
|
3734
3759
|
this.session = undefined;
|
|
3735
3760
|
this.networks = [];
|
|
3736
3761
|
this.providers = {};
|
|
@@ -3759,6 +3784,7 @@ exports.DefaultProviderConfig = DefaultProviderConfig;
|
|
|
3759
3784
|
exports.ErrSignedInRequired = ErrSignedInRequired;
|
|
3760
3785
|
exports.ExtensionMessageHandler = ExtensionMessageHandler;
|
|
3761
3786
|
exports.ExtensionMessageProvider = ExtensionMessageProvider;
|
|
3787
|
+
exports.LocalStorage = LocalStorage;
|
|
3762
3788
|
exports.LocalStore = LocalStore;
|
|
3763
3789
|
exports.LocalWeb3Provider = LocalWeb3Provider;
|
|
3764
3790
|
exports.MuxMessageProvider = MuxMessageProvider;
|
|
@@ -1703,7 +1703,7 @@ const hexlifyTransaction = (transaction, allowExtra) => {
|
|
|
1703
1703
|
return result;
|
|
1704
1704
|
};
|
|
1705
1705
|
|
|
1706
|
-
const eip191prefix = ethers.ethers.utils.toUtf8Bytes(
|
|
1706
|
+
const eip191prefix = ethers.ethers.utils.toUtf8Bytes('\x19Ethereum Signed Message:\n');
|
|
1707
1707
|
const messageToBytes = message => {
|
|
1708
1708
|
if (ethers.ethers.utils.isBytes(message) || ethers.ethers.utils.isHexString(message)) {
|
|
1709
1709
|
return ethers.ethers.utils.arrayify(message);
|
|
@@ -1799,8 +1799,29 @@ const isWalletUpToDate = async (signer, chainId) => {
|
|
|
1799
1799
|
|
|
1800
1800
|
const isUpToDate = config.isConfigEqual(authChainConfig, requiredChainConfig);
|
|
1801
1801
|
return isDeployed && isUpToDate;
|
|
1802
|
-
};
|
|
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
|
+
}
|
|
1803
1821
|
|
|
1822
|
+
} // window.localstorage helper
|
|
1823
|
+
|
|
1824
|
+
LocalStorage._instance = void 0;
|
|
1804
1825
|
class LocalStore {
|
|
1805
1826
|
constructor(key, def) {
|
|
1806
1827
|
this.def = def;
|
|
@@ -1808,8 +1829,8 @@ class LocalStore {
|
|
|
1808
1829
|
this.key = key;
|
|
1809
1830
|
}
|
|
1810
1831
|
|
|
1811
|
-
get() {
|
|
1812
|
-
const val =
|
|
1832
|
+
async get() {
|
|
1833
|
+
const val = await LocalStorage.getInstance().getItem(this.key);
|
|
1813
1834
|
|
|
1814
1835
|
if (val === null) {
|
|
1815
1836
|
return this.def;
|
|
@@ -1825,11 +1846,11 @@ class LocalStore {
|
|
|
1825
1846
|
}
|
|
1826
1847
|
|
|
1827
1848
|
set(val) {
|
|
1828
|
-
val ?
|
|
1849
|
+
val ? LocalStorage.getInstance().setItem(this.key, JSON.stringify(val)) : LocalStorage.getInstance().removeItem(this.key);
|
|
1829
1850
|
}
|
|
1830
1851
|
|
|
1831
1852
|
del() {
|
|
1832
|
-
|
|
1853
|
+
LocalStorage.getInstance().removeItem(this.key);
|
|
1833
1854
|
}
|
|
1834
1855
|
|
|
1835
1856
|
}
|
|
@@ -2052,11 +2073,11 @@ class BaseWalletTransport {
|
|
|
2052
2073
|
};
|
|
2053
2074
|
|
|
2054
2075
|
this.saveTransportSession = session => {
|
|
2055
|
-
|
|
2076
|
+
LocalStorage.getInstance().setItem(TRANSPORT_SESSION_LS_KEY, JSON.stringify(session));
|
|
2056
2077
|
};
|
|
2057
2078
|
|
|
2058
|
-
this.getCachedTransportSession = ()
|
|
2059
|
-
const session =
|
|
2079
|
+
this.getCachedTransportSession = async function () {
|
|
2080
|
+
const session = await LocalStorage.getInstance().getItem(TRANSPORT_SESSION_LS_KEY);
|
|
2060
2081
|
|
|
2061
2082
|
try {
|
|
2062
2083
|
return session ? JSON.parse(session) : null;
|
|
@@ -2433,14 +2454,14 @@ class ProxyMessageHandler extends BaseWalletTransport {
|
|
|
2433
2454
|
this._registered = true;
|
|
2434
2455
|
} // note: we can't decide whether to restore the session within register(), because session info is
|
|
2435
2456
|
// received asyncronously via EventType.OPEN after register() is executed.
|
|
2436
|
-
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
2457
|
+
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
2437
2458
|
// because the wallet is already open.
|
|
2438
2459
|
//
|
|
2439
2460
|
// call this method from wallet redirect hander when a session restore is needed
|
|
2440
2461
|
|
|
2441
2462
|
|
|
2442
|
-
restoreSession() {
|
|
2443
|
-
const cachedSession = this.getCachedTransportSession();
|
|
2463
|
+
async restoreSession() {
|
|
2464
|
+
const cachedSession = await this.getCachedTransportSession();
|
|
2444
2465
|
|
|
2445
2466
|
if (cachedSession) {
|
|
2446
2467
|
this.open(cachedSession);
|
|
@@ -2864,7 +2885,7 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
2864
2885
|
this._init = exports.InitState.NIL;
|
|
2865
2886
|
}
|
|
2866
2887
|
|
|
2867
|
-
register(windowHref) {
|
|
2888
|
+
async register(windowHref) {
|
|
2868
2889
|
const isPopup = parent.window.opener !== null;
|
|
2869
2890
|
this._isPopup = isPopup;
|
|
2870
2891
|
|
|
@@ -2882,7 +2903,7 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
2882
2903
|
const isNewWindowSession = !!session.sessionId; // attempt to restore previous session in the case of a redirect or window reload
|
|
2883
2904
|
|
|
2884
2905
|
if (!isNewWindowSession) {
|
|
2885
|
-
session = this.getCachedTransportSession();
|
|
2906
|
+
session = await this.getCachedTransportSession();
|
|
2886
2907
|
}
|
|
2887
2908
|
|
|
2888
2909
|
if (!session) {
|
|
@@ -3326,11 +3347,11 @@ class Wallet {
|
|
|
3326
3347
|
}, true);
|
|
3327
3348
|
}); // Load existing session from localStorage
|
|
3328
3349
|
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
}
|
|
3350
|
+
this.loadSession().then(session => {
|
|
3351
|
+
if (session) {
|
|
3352
|
+
this.useSession(session, false);
|
|
3353
|
+
}
|
|
3354
|
+
});
|
|
3334
3355
|
};
|
|
3335
3356
|
|
|
3336
3357
|
this.connect = async function (options) {
|
|
@@ -3464,8 +3485,8 @@ class Wallet {
|
|
|
3464
3485
|
this.transport.messageProvider.closeWallet();
|
|
3465
3486
|
};
|
|
3466
3487
|
|
|
3467
|
-
this.loadSession = ()
|
|
3468
|
-
const data =
|
|
3488
|
+
this.loadSession = async function () {
|
|
3489
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3469
3490
|
|
|
3470
3491
|
if (!data || data === '') {
|
|
3471
3492
|
return undefined;
|
|
@@ -3483,7 +3504,7 @@ class Wallet {
|
|
|
3483
3504
|
this.saveSession = session => {
|
|
3484
3505
|
utils.logger.debug('wallet provider: saving session');
|
|
3485
3506
|
const data = JSON.stringify(session);
|
|
3486
|
-
|
|
3507
|
+
LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3487
3508
|
};
|
|
3488
3509
|
|
|
3489
3510
|
this.useSession = async function (session, autoSave = true) {
|
|
@@ -3530,6 +3551,10 @@ class Wallet {
|
|
|
3530
3551
|
this.config.defaultNetworkId = 'mainnet';
|
|
3531
3552
|
}
|
|
3532
3553
|
|
|
3554
|
+
if (config != null && config.localStorage) {
|
|
3555
|
+
LocalStorage.use(config.localStorage);
|
|
3556
|
+
}
|
|
3557
|
+
|
|
3533
3558
|
this.transport = {};
|
|
3534
3559
|
this.networks = [];
|
|
3535
3560
|
this.providers = {};
|
|
@@ -3538,9 +3563,9 @@ class Wallet {
|
|
|
3538
3563
|
this.init();
|
|
3539
3564
|
}
|
|
3540
3565
|
|
|
3541
|
-
addConnectedSite(origin) {
|
|
3566
|
+
async addConnectedSite(origin) {
|
|
3542
3567
|
origin = origin || window.location.origin;
|
|
3543
|
-
const connectedSites = this.connectedSites.get();
|
|
3568
|
+
const connectedSites = await this.connectedSites.get();
|
|
3544
3569
|
|
|
3545
3570
|
if (connectedSites) {
|
|
3546
3571
|
if (connectedSites.includes(origin)) {
|
|
@@ -3553,8 +3578,8 @@ class Wallet {
|
|
|
3553
3578
|
}
|
|
3554
3579
|
}
|
|
3555
3580
|
|
|
3556
|
-
removeConnectedSite(origin) {
|
|
3557
|
-
const authorized = this.connectedSites.get();
|
|
3581
|
+
async removeConnectedSite(origin) {
|
|
3582
|
+
const authorized = await this.connectedSites.get();
|
|
3558
3583
|
|
|
3559
3584
|
if (authorized) {
|
|
3560
3585
|
this.connectedSites.set(authorized.filter(domain => domain !== origin));
|
|
@@ -3565,8 +3590,8 @@ class Wallet {
|
|
|
3565
3590
|
return this.connectedSites.get();
|
|
3566
3591
|
}
|
|
3567
3592
|
|
|
3568
|
-
isSiteConnected(origin) {
|
|
3569
|
-
const authorized = this.connectedSites.get();
|
|
3593
|
+
async isSiteConnected(origin) {
|
|
3594
|
+
const authorized = await this.connectedSites.get();
|
|
3570
3595
|
return !!authorized && authorized.includes(origin || window.location.origin);
|
|
3571
3596
|
}
|
|
3572
3597
|
|
|
@@ -3730,7 +3755,7 @@ class Wallet {
|
|
|
3730
3755
|
var _this$transport$cache;
|
|
3731
3756
|
|
|
3732
3757
|
utils.logger.debug('wallet provider: clearing session');
|
|
3733
|
-
|
|
3758
|
+
LocalStorage.getInstance().removeItem('@sequence.session');
|
|
3734
3759
|
this.session = undefined;
|
|
3735
3760
|
this.networks = [];
|
|
3736
3761
|
this.providers = {};
|
|
@@ -3759,6 +3784,7 @@ exports.DefaultProviderConfig = DefaultProviderConfig;
|
|
|
3759
3784
|
exports.ErrSignedInRequired = ErrSignedInRequired;
|
|
3760
3785
|
exports.ExtensionMessageHandler = ExtensionMessageHandler;
|
|
3761
3786
|
exports.ExtensionMessageProvider = ExtensionMessageProvider;
|
|
3787
|
+
exports.LocalStorage = LocalStorage;
|
|
3762
3788
|
exports.LocalStore = LocalStore;
|
|
3763
3789
|
exports.LocalWeb3Provider = LocalWeb3Provider;
|
|
3764
3790
|
exports.MuxMessageProvider = MuxMessageProvider;
|
|
@@ -1699,7 +1699,7 @@ const hexlifyTransaction = (transaction, allowExtra) => {
|
|
|
1699
1699
|
return result;
|
|
1700
1700
|
};
|
|
1701
1701
|
|
|
1702
|
-
const eip191prefix = ethers.utils.toUtf8Bytes(
|
|
1702
|
+
const eip191prefix = ethers.utils.toUtf8Bytes('\x19Ethereum Signed Message:\n');
|
|
1703
1703
|
const messageToBytes = message => {
|
|
1704
1704
|
if (ethers.utils.isBytes(message) || ethers.utils.isHexString(message)) {
|
|
1705
1705
|
return ethers.utils.arrayify(message);
|
|
@@ -1795,8 +1795,29 @@ const isWalletUpToDate = async (signer, chainId) => {
|
|
|
1795
1795
|
|
|
1796
1796
|
const isUpToDate = isConfigEqual(authChainConfig, requiredChainConfig);
|
|
1797
1797
|
return isDeployed && isUpToDate;
|
|
1798
|
-
};
|
|
1798
|
+
};
|
|
1799
|
+
class LocalStorage {
|
|
1800
|
+
constructor() {}
|
|
1801
|
+
|
|
1802
|
+
static getInstance() {
|
|
1803
|
+
if (!LocalStorage._instance) {
|
|
1804
|
+
LocalStorage._instance = {
|
|
1805
|
+
getItem: key => Promise.resolve(window.localStorage.getItem(key)),
|
|
1806
|
+
setItem: (key, value) => Promise.resolve(window.localStorage.setItem(key, value)),
|
|
1807
|
+
removeItem: key => Promise.resolve(window.localStorage.removeItem(key))
|
|
1808
|
+
};
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
return this._instance;
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
static use(instance) {
|
|
1815
|
+
LocalStorage._instance = instance;
|
|
1816
|
+
}
|
|
1799
1817
|
|
|
1818
|
+
} // window.localstorage helper
|
|
1819
|
+
|
|
1820
|
+
LocalStorage._instance = void 0;
|
|
1800
1821
|
class LocalStore {
|
|
1801
1822
|
constructor(key, def) {
|
|
1802
1823
|
this.def = def;
|
|
@@ -1804,8 +1825,8 @@ class LocalStore {
|
|
|
1804
1825
|
this.key = key;
|
|
1805
1826
|
}
|
|
1806
1827
|
|
|
1807
|
-
get() {
|
|
1808
|
-
const val =
|
|
1828
|
+
async get() {
|
|
1829
|
+
const val = await LocalStorage.getInstance().getItem(this.key);
|
|
1809
1830
|
|
|
1810
1831
|
if (val === null) {
|
|
1811
1832
|
return this.def;
|
|
@@ -1821,11 +1842,11 @@ class LocalStore {
|
|
|
1821
1842
|
}
|
|
1822
1843
|
|
|
1823
1844
|
set(val) {
|
|
1824
|
-
val ?
|
|
1845
|
+
val ? LocalStorage.getInstance().setItem(this.key, JSON.stringify(val)) : LocalStorage.getInstance().removeItem(this.key);
|
|
1825
1846
|
}
|
|
1826
1847
|
|
|
1827
1848
|
del() {
|
|
1828
|
-
|
|
1849
|
+
LocalStorage.getInstance().removeItem(this.key);
|
|
1829
1850
|
}
|
|
1830
1851
|
|
|
1831
1852
|
}
|
|
@@ -2048,11 +2069,11 @@ class BaseWalletTransport {
|
|
|
2048
2069
|
};
|
|
2049
2070
|
|
|
2050
2071
|
this.saveTransportSession = session => {
|
|
2051
|
-
|
|
2072
|
+
LocalStorage.getInstance().setItem(TRANSPORT_SESSION_LS_KEY, JSON.stringify(session));
|
|
2052
2073
|
};
|
|
2053
2074
|
|
|
2054
|
-
this.getCachedTransportSession = ()
|
|
2055
|
-
const session =
|
|
2075
|
+
this.getCachedTransportSession = async function () {
|
|
2076
|
+
const session = await LocalStorage.getInstance().getItem(TRANSPORT_SESSION_LS_KEY);
|
|
2056
2077
|
|
|
2057
2078
|
try {
|
|
2058
2079
|
return session ? JSON.parse(session) : null;
|
|
@@ -2429,14 +2450,14 @@ class ProxyMessageHandler extends BaseWalletTransport {
|
|
|
2429
2450
|
this._registered = true;
|
|
2430
2451
|
} // note: we can't decide whether to restore the session within register(), because session info is
|
|
2431
2452
|
// 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,
|
|
2453
|
+
// And in the case of a redirect/reload, EventType.OPEN is not sent at all,
|
|
2433
2454
|
// because the wallet is already open.
|
|
2434
2455
|
//
|
|
2435
2456
|
// call this method from wallet redirect hander when a session restore is needed
|
|
2436
2457
|
|
|
2437
2458
|
|
|
2438
|
-
restoreSession() {
|
|
2439
|
-
const cachedSession = this.getCachedTransportSession();
|
|
2459
|
+
async restoreSession() {
|
|
2460
|
+
const cachedSession = await this.getCachedTransportSession();
|
|
2440
2461
|
|
|
2441
2462
|
if (cachedSession) {
|
|
2442
2463
|
this.open(cachedSession);
|
|
@@ -2860,7 +2881,7 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
2860
2881
|
this._init = InitState.NIL;
|
|
2861
2882
|
}
|
|
2862
2883
|
|
|
2863
|
-
register(windowHref) {
|
|
2884
|
+
async register(windowHref) {
|
|
2864
2885
|
const isPopup = parent.window.opener !== null;
|
|
2865
2886
|
this._isPopup = isPopup;
|
|
2866
2887
|
|
|
@@ -2878,7 +2899,7 @@ class WindowMessageHandler extends BaseWalletTransport {
|
|
|
2878
2899
|
const isNewWindowSession = !!session.sessionId; // attempt to restore previous session in the case of a redirect or window reload
|
|
2879
2900
|
|
|
2880
2901
|
if (!isNewWindowSession) {
|
|
2881
|
-
session = this.getCachedTransportSession();
|
|
2902
|
+
session = await this.getCachedTransportSession();
|
|
2882
2903
|
}
|
|
2883
2904
|
|
|
2884
2905
|
if (!session) {
|
|
@@ -3322,11 +3343,11 @@ class Wallet {
|
|
|
3322
3343
|
}, true);
|
|
3323
3344
|
}); // Load existing session from localStorage
|
|
3324
3345
|
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
}
|
|
3346
|
+
this.loadSession().then(session => {
|
|
3347
|
+
if (session) {
|
|
3348
|
+
this.useSession(session, false);
|
|
3349
|
+
}
|
|
3350
|
+
});
|
|
3330
3351
|
};
|
|
3331
3352
|
|
|
3332
3353
|
this.connect = async function (options) {
|
|
@@ -3460,8 +3481,8 @@ class Wallet {
|
|
|
3460
3481
|
this.transport.messageProvider.closeWallet();
|
|
3461
3482
|
};
|
|
3462
3483
|
|
|
3463
|
-
this.loadSession = ()
|
|
3464
|
-
const data =
|
|
3484
|
+
this.loadSession = async function () {
|
|
3485
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3465
3486
|
|
|
3466
3487
|
if (!data || data === '') {
|
|
3467
3488
|
return undefined;
|
|
@@ -3479,7 +3500,7 @@ class Wallet {
|
|
|
3479
3500
|
this.saveSession = session => {
|
|
3480
3501
|
logger.debug('wallet provider: saving session');
|
|
3481
3502
|
const data = JSON.stringify(session);
|
|
3482
|
-
|
|
3503
|
+
LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3483
3504
|
};
|
|
3484
3505
|
|
|
3485
3506
|
this.useSession = async function (session, autoSave = true) {
|
|
@@ -3526,6 +3547,10 @@ class Wallet {
|
|
|
3526
3547
|
this.config.defaultNetworkId = 'mainnet';
|
|
3527
3548
|
}
|
|
3528
3549
|
|
|
3550
|
+
if (config != null && config.localStorage) {
|
|
3551
|
+
LocalStorage.use(config.localStorage);
|
|
3552
|
+
}
|
|
3553
|
+
|
|
3529
3554
|
this.transport = {};
|
|
3530
3555
|
this.networks = [];
|
|
3531
3556
|
this.providers = {};
|
|
@@ -3534,9 +3559,9 @@ class Wallet {
|
|
|
3534
3559
|
this.init();
|
|
3535
3560
|
}
|
|
3536
3561
|
|
|
3537
|
-
addConnectedSite(origin) {
|
|
3562
|
+
async addConnectedSite(origin) {
|
|
3538
3563
|
origin = origin || window.location.origin;
|
|
3539
|
-
const connectedSites = this.connectedSites.get();
|
|
3564
|
+
const connectedSites = await this.connectedSites.get();
|
|
3540
3565
|
|
|
3541
3566
|
if (connectedSites) {
|
|
3542
3567
|
if (connectedSites.includes(origin)) {
|
|
@@ -3549,8 +3574,8 @@ class Wallet {
|
|
|
3549
3574
|
}
|
|
3550
3575
|
}
|
|
3551
3576
|
|
|
3552
|
-
removeConnectedSite(origin) {
|
|
3553
|
-
const authorized = this.connectedSites.get();
|
|
3577
|
+
async removeConnectedSite(origin) {
|
|
3578
|
+
const authorized = await this.connectedSites.get();
|
|
3554
3579
|
|
|
3555
3580
|
if (authorized) {
|
|
3556
3581
|
this.connectedSites.set(authorized.filter(domain => domain !== origin));
|
|
@@ -3561,8 +3586,8 @@ class Wallet {
|
|
|
3561
3586
|
return this.connectedSites.get();
|
|
3562
3587
|
}
|
|
3563
3588
|
|
|
3564
|
-
isSiteConnected(origin) {
|
|
3565
|
-
const authorized = this.connectedSites.get();
|
|
3589
|
+
async isSiteConnected(origin) {
|
|
3590
|
+
const authorized = await this.connectedSites.get();
|
|
3566
3591
|
return !!authorized && authorized.includes(origin || window.location.origin);
|
|
3567
3592
|
}
|
|
3568
3593
|
|
|
@@ -3726,7 +3751,7 @@ class Wallet {
|
|
|
3726
3751
|
var _this$transport$cache;
|
|
3727
3752
|
|
|
3728
3753
|
logger.debug('wallet provider: clearing session');
|
|
3729
|
-
|
|
3754
|
+
LocalStorage.getInstance().removeItem('@sequence.session');
|
|
3730
3755
|
this.session = undefined;
|
|
3731
3756
|
this.networks = [];
|
|
3732
3757
|
this.providers = {};
|
|
@@ -3747,4 +3772,4 @@ const DefaultProviderConfig = {
|
|
|
3747
3772
|
}
|
|
3748
3773
|
};
|
|
3749
3774
|
|
|
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 };
|
|
3775
|
+
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 };
|
|
@@ -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
|
}
|
|
@@ -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
|
}
|
|
@@ -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;
|
|
@@ -149,6 +149,11 @@ export interface Settings {
|
|
|
149
149
|
* Note that this setting will not be persisted, use wallet.open with 'openWithOptions' intent
|
|
150
150
|
* to set when you open the wallet for user. */
|
|
151
151
|
defaultFundingCurrency?: CurrencyOption;
|
|
152
|
+
/** Specify default purchase amount as an integer, for prefilling the funding amount.
|
|
153
|
+
* If not specified, the default is 100.
|
|
154
|
+
* Note that this setting will not be persisted, use wallet.open with 'openWithOptions' intent
|
|
155
|
+
* to set when you open the wallet for user. */
|
|
156
|
+
defaultPurchaseAmount?: number;
|
|
152
157
|
/** If true, lockFundingCurrencyToDefault disables picking any currency provided by payment
|
|
153
158
|
* providers other than the defaultFundingCurrency.
|
|
154
159
|
* If false, it allows picking any currency provided by payment providers.
|
|
@@ -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,9 +40,9 @@ 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
|
|
43
|
+
addConnectedSite(origin: string | undefined): Promise<void>;
|
|
44
|
+
removeConnectedSite(origin: string): Promise<void>;
|
|
45
|
+
getConnectedSites(): Promise<string[] | undefined>;
|
|
45
46
|
private isSiteConnected;
|
|
46
47
|
authorize: (options?: ConnectOptions | undefined) => Promise<ConnectDetails>;
|
|
47
48
|
disconnect(): void;
|
|
@@ -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.0",
|
|
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.0",
|
|
17
|
+
"@0xsequence/auth": "^0.39.0",
|
|
18
|
+
"@0xsequence/config": "^0.39.0",
|
|
19
|
+
"@0xsequence/network": "^0.39.0",
|
|
20
|
+
"@0xsequence/transactions": "^0.39.0",
|
|
21
|
+
"@0xsequence/utils": "^0.39.0",
|
|
22
|
+
"@0xsequence/wallet": "^0.39.0",
|
|
23
23
|
"@ethersproject/abstract-signer": "^5.5.0",
|
|
24
24
|
"@ethersproject/hash": "^5.5.0",
|
|
25
25
|
"@ethersproject/providers": "^5.5.1",
|
|
@@ -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
|
|
|
@@ -435,11 +435,11 @@ export abstract class BaseWalletTransport implements WalletTransport {
|
|
|
435
435
|
}
|
|
436
436
|
|
|
437
437
|
private saveTransportSession = (session: TransportSession) => {
|
|
438
|
-
|
|
438
|
+
LocalStorage.getInstance().setItem(TRANSPORT_SESSION_LS_KEY, JSON.stringify(session))
|
|
439
439
|
}
|
|
440
440
|
|
|
441
|
-
protected getCachedTransportSession = (): TransportSession | null => {
|
|
442
|
-
const session =
|
|
441
|
+
protected getCachedTransportSession = async (): Promise<TransportSession | null> => {
|
|
442
|
+
const session = await LocalStorage.getInstance().getItem(TRANSPORT_SESSION_LS_KEY)
|
|
443
443
|
|
|
444
444
|
try {
|
|
445
445
|
return session ? (JSON.parse(session) as TransportSession) : null
|
|
@@ -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
|
}
|
|
@@ -22,7 +22,7 @@ export class WindowMessageHandler extends BaseWalletTransport {
|
|
|
22
22
|
this._init = InitState.NIL
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
register(windowHref?: any) {
|
|
25
|
+
async register(windowHref?: any) {
|
|
26
26
|
const isPopup = parent.window.opener !== null
|
|
27
27
|
this._isPopup = isPopup
|
|
28
28
|
if (isPopup !== true) {
|
|
@@ -39,7 +39,7 @@ export class WindowMessageHandler extends BaseWalletTransport {
|
|
|
39
39
|
|
|
40
40
|
// attempt to restore previous session in the case of a redirect or window reload
|
|
41
41
|
if (!isNewWindowSession) {
|
|
42
|
-
session = this.getCachedTransportSession()
|
|
42
|
+
session = await this.getCachedTransportSession()
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
if (!session) {
|
|
@@ -143,7 +143,8 @@ export class WindowMessageHandler extends BaseWalletTransport {
|
|
|
143
143
|
this.parentWindow.postMessage(message, '*')
|
|
144
144
|
} else {
|
|
145
145
|
// open message transmission
|
|
146
|
-
if (this.appOrigin && this.appOrigin.length > 4) {
|
|
146
|
+
if (this.appOrigin && this.appOrigin.length > 4) {
|
|
147
|
+
// just above '.com'
|
|
147
148
|
this.parentWindow.postMessage(message, this.appOrigin)
|
|
148
149
|
} else {
|
|
149
150
|
logger.error('unable to postMessage as parentOrigin is invalid')
|
package/src/types.ts
CHANGED
|
@@ -192,6 +192,12 @@ export interface Settings {
|
|
|
192
192
|
* to set when you open the wallet for user. */
|
|
193
193
|
defaultFundingCurrency?: CurrencyOption
|
|
194
194
|
|
|
195
|
+
/** Specify default purchase amount as an integer, for prefilling the funding amount.
|
|
196
|
+
* If not specified, the default is 100.
|
|
197
|
+
* Note that this setting will not be persisted, use wallet.open with 'openWithOptions' intent
|
|
198
|
+
* to set when you open the wallet for user. */
|
|
199
|
+
defaultPurchaseAmount?: number
|
|
200
|
+
|
|
195
201
|
/** If true, lockFundingCurrencyToDefault disables picking any currency provided by payment
|
|
196
202
|
* providers other than the defaultFundingCurrency.
|
|
197
203
|
* If false, it allows picking any currency provided by payment providers.
|
package/src/utils.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { WalletContext } from '@0xsequence/network'
|
|
|
3
3
|
import { WalletConfig, addressOf, DecodedSignature, isConfigEqual } from '@0xsequence/config'
|
|
4
4
|
import { packMessageData, encodeMessageDigest, TypedData, encodeTypedDataDigest } from '@0xsequence/utils'
|
|
5
5
|
import { Web3Provider } from './provider'
|
|
6
|
-
import { isValidSignature as _isValidSignature, recoverConfig, Signer
|
|
6
|
+
import { isValidSignature as _isValidSignature, recoverConfig, Signer } from '@0xsequence/wallet'
|
|
7
7
|
|
|
8
|
-
const eip191prefix = ethers.utils.toUtf8Bytes(
|
|
8
|
+
const eip191prefix = ethers.utils.toUtf8Bytes('\x19Ethereum Signed Message:\n')
|
|
9
9
|
|
|
10
10
|
export const messageToBytes = (message: BytesLike): Uint8Array => {
|
|
11
11
|
if (ethers.utils.isBytes(message) || ethers.utils.isHexString(message)) {
|
|
@@ -17,11 +17,7 @@ export const messageToBytes = (message: BytesLike): Uint8Array => {
|
|
|
17
17
|
|
|
18
18
|
export const prefixEIP191Message = (message: BytesLike): Uint8Array => {
|
|
19
19
|
const messageBytes = messageToBytes(message)
|
|
20
|
-
return ethers.utils.concat([
|
|
21
|
-
eip191prefix,
|
|
22
|
-
ethers.utils.toUtf8Bytes(String(messageBytes.length)),
|
|
23
|
-
messageBytes
|
|
24
|
-
])
|
|
20
|
+
return ethers.utils.concat([eip191prefix, ethers.utils.toUtf8Bytes(String(messageBytes.length)), messageBytes])
|
|
25
21
|
}
|
|
26
22
|
|
|
27
23
|
export const isValidSignature = async (
|
|
@@ -130,6 +126,33 @@ export const isWalletUpToDate = async (signer: Signer, chainId: number): Promise
|
|
|
130
126
|
return isDeployed && isUpToDate
|
|
131
127
|
}
|
|
132
128
|
|
|
129
|
+
export interface ItemStore {
|
|
130
|
+
getItem(key: string): Promise<string | null>
|
|
131
|
+
setItem(key: string, value: string): Promise<void>
|
|
132
|
+
removeItem(key: string): Promise<void>
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export class LocalStorage {
|
|
136
|
+
private static _instance: ItemStore
|
|
137
|
+
|
|
138
|
+
private constructor() {}
|
|
139
|
+
|
|
140
|
+
static getInstance(): ItemStore {
|
|
141
|
+
if (!LocalStorage._instance) {
|
|
142
|
+
LocalStorage._instance = {
|
|
143
|
+
getItem: (key: string) => Promise.resolve(window.localStorage.getItem(key)),
|
|
144
|
+
setItem: (key: string, value: string) => Promise.resolve(window.localStorage.setItem(key, value)),
|
|
145
|
+
removeItem: (key: string) => Promise.resolve(window.localStorage.removeItem(key))
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return this._instance
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static use(instance: ItemStore) {
|
|
152
|
+
LocalStorage._instance = instance
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
133
156
|
// window.localstorage helper
|
|
134
157
|
export class LocalStore<T extends Object = string> {
|
|
135
158
|
readonly key: string
|
|
@@ -138,8 +161,8 @@ export class LocalStore<T extends Object = string> {
|
|
|
138
161
|
this.key = key
|
|
139
162
|
}
|
|
140
163
|
|
|
141
|
-
get(): T | undefined {
|
|
142
|
-
const val =
|
|
164
|
+
async get(): Promise<T | undefined> {
|
|
165
|
+
const val = await LocalStorage.getInstance().getItem(this.key)
|
|
143
166
|
|
|
144
167
|
if (val === null) {
|
|
145
168
|
return this.def
|
|
@@ -155,10 +178,10 @@ export class LocalStore<T extends Object = string> {
|
|
|
155
178
|
}
|
|
156
179
|
|
|
157
180
|
set(val: T | undefined) {
|
|
158
|
-
val ?
|
|
181
|
+
val ? LocalStorage.getInstance().setItem(this.key, JSON.stringify(val)) : LocalStorage.getInstance().removeItem(this.key)
|
|
159
182
|
}
|
|
160
183
|
|
|
161
184
|
del() {
|
|
162
|
-
|
|
185
|
+
LocalStorage.getInstance().removeItem(this.key)
|
|
163
186
|
}
|
|
164
187
|
}
|
package/src/wallet.ts
CHANGED
|
@@ -1,25 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
2
|
NetworkConfig,
|
|
3
3
|
WalletContext,
|
|
4
|
-
sequenceContext,
|
|
5
4
|
ChainIdLike,
|
|
6
|
-
getChainId,
|
|
7
5
|
JsonRpcSender,
|
|
8
6
|
JsonRpcRouter,
|
|
9
7
|
JsonRpcMiddleware,
|
|
10
8
|
allowProviderMiddleware,
|
|
11
9
|
CachedProvider,
|
|
12
|
-
PublicProvider,
|
|
13
10
|
loggingProviderMiddleware,
|
|
14
11
|
SigningProvider,
|
|
15
12
|
EagerProvider,
|
|
16
13
|
exceptionProviderMiddleware,
|
|
17
14
|
networkProviderMiddleware,
|
|
18
|
-
JsonRpcExternalProvider,
|
|
19
|
-
JsonRpcHandlerFunc,
|
|
20
15
|
JsonRpcRequest,
|
|
21
|
-
JsonRpcResponse,
|
|
22
|
-
JsonRpcResponseCallback,
|
|
23
16
|
findNetworkConfig,
|
|
24
17
|
updateNetworkConfig,
|
|
25
18
|
ensureValidNetworks
|
|
@@ -32,7 +25,7 @@ import { MuxMessageProvider, WindowMessageProvider, ProxyMessageProvider, ProxyM
|
|
|
32
25
|
import { WalletSession, ProviderEventTypes, ConnectOptions, OpenWalletIntent, ConnectDetails } from './types'
|
|
33
26
|
import { ethers } from 'ethers'
|
|
34
27
|
import { ExtensionMessageProvider } from './transports/extension-transport/extension-message-provider'
|
|
35
|
-
import { LocalStore } from './utils'
|
|
28
|
+
import { LocalStore, ItemStore, LocalStorage } from './utils'
|
|
36
29
|
import { WalletUtils } from './utils/index'
|
|
37
30
|
|
|
38
31
|
import { Runtime } from 'webextension-polyfill-ts'
|
|
@@ -111,6 +104,10 @@ export class Wallet implements WalletProvider {
|
|
|
111
104
|
this.config.defaultNetworkId = 'mainnet'
|
|
112
105
|
}
|
|
113
106
|
|
|
107
|
+
if (config?.localStorage) {
|
|
108
|
+
LocalStorage.use(config.localStorage)
|
|
109
|
+
}
|
|
110
|
+
|
|
114
111
|
this.transport = {}
|
|
115
112
|
this.networks = []
|
|
116
113
|
this.providers = {}
|
|
@@ -233,10 +230,11 @@ export class Wallet implements WalletProvider {
|
|
|
233
230
|
})
|
|
234
231
|
|
|
235
232
|
// Load existing session from localStorage
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
233
|
+
this.loadSession().then(session => {
|
|
234
|
+
if (session) {
|
|
235
|
+
this.useSession(session, false)
|
|
236
|
+
}
|
|
237
|
+
})
|
|
240
238
|
}
|
|
241
239
|
|
|
242
240
|
connect = async (options?: ConnectOptions): Promise<ConnectDetails> => {
|
|
@@ -282,10 +280,10 @@ export class Wallet implements WalletProvider {
|
|
|
282
280
|
return connectDetails
|
|
283
281
|
}
|
|
284
282
|
|
|
285
|
-
addConnectedSite(origin: string | undefined) {
|
|
283
|
+
async addConnectedSite(origin: string | undefined) {
|
|
286
284
|
origin = origin || window.location.origin
|
|
287
285
|
|
|
288
|
-
const connectedSites = this.connectedSites.get()
|
|
286
|
+
const connectedSites = await this.connectedSites.get()
|
|
289
287
|
|
|
290
288
|
if (connectedSites) {
|
|
291
289
|
if (connectedSites.includes(origin)) {
|
|
@@ -297,8 +295,8 @@ export class Wallet implements WalletProvider {
|
|
|
297
295
|
}
|
|
298
296
|
}
|
|
299
297
|
|
|
300
|
-
removeConnectedSite(origin: string) {
|
|
301
|
-
const authorized = this.connectedSites.get()
|
|
298
|
+
async removeConnectedSite(origin: string) {
|
|
299
|
+
const authorized = await this.connectedSites.get()
|
|
302
300
|
|
|
303
301
|
if (authorized) {
|
|
304
302
|
this.connectedSites.set(authorized.filter(domain => domain !== origin))
|
|
@@ -309,8 +307,8 @@ export class Wallet implements WalletProvider {
|
|
|
309
307
|
return this.connectedSites.get()
|
|
310
308
|
}
|
|
311
309
|
|
|
312
|
-
private isSiteConnected(origin: string | undefined): boolean {
|
|
313
|
-
const authorized = this.connectedSites.get()
|
|
310
|
+
private async isSiteConnected(origin: string | undefined): Promise<boolean> {
|
|
311
|
+
const authorized = await this.connectedSites.get()
|
|
314
312
|
|
|
315
313
|
return !!authorized && authorized.includes(origin || window.location.origin)
|
|
316
314
|
}
|
|
@@ -538,8 +536,8 @@ export class Wallet implements WalletProvider {
|
|
|
538
536
|
this.transport.messageProvider!.once(event, fn)
|
|
539
537
|
}
|
|
540
538
|
|
|
541
|
-
private loadSession = (): WalletSession | undefined => {
|
|
542
|
-
const data =
|
|
539
|
+
private loadSession = async (): Promise<WalletSession | undefined> => {
|
|
540
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session')
|
|
543
541
|
if (!data || data === '') {
|
|
544
542
|
return undefined
|
|
545
543
|
}
|
|
@@ -555,7 +553,7 @@ export class Wallet implements WalletProvider {
|
|
|
555
553
|
private saveSession = (session: WalletSession) => {
|
|
556
554
|
logger.debug('wallet provider: saving session')
|
|
557
555
|
const data = JSON.stringify(session)
|
|
558
|
-
|
|
556
|
+
LocalStorage.getInstance().setItem('@sequence.session', data)
|
|
559
557
|
}
|
|
560
558
|
|
|
561
559
|
private useSession = async (session: WalletSession, autoSave: boolean = true) => {
|
|
@@ -631,7 +629,7 @@ export class Wallet implements WalletProvider {
|
|
|
631
629
|
|
|
632
630
|
private clearSession(): void {
|
|
633
631
|
logger.debug('wallet provider: clearing session')
|
|
634
|
-
|
|
632
|
+
LocalStorage.getInstance().removeItem('@sequence.session')
|
|
635
633
|
this.session = undefined
|
|
636
634
|
this.networks = []
|
|
637
635
|
this.providers = {}
|
|
@@ -640,6 +638,10 @@ export class Wallet implements WalletProvider {
|
|
|
640
638
|
}
|
|
641
639
|
|
|
642
640
|
export interface ProviderConfig {
|
|
641
|
+
// The local storage dependency for the wallet provider, defaults to window.localStorage.
|
|
642
|
+
// For example, this option should be used when using React Native since window.localStorage is not available.
|
|
643
|
+
localStorage?: ItemStore
|
|
644
|
+
|
|
643
645
|
// Sequence Wallet App URL, default: https://sequence.app
|
|
644
646
|
walletAppURL: string
|
|
645
647
|
|