@0xsequence/provider 0.39.6 → 0.40.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.
|
@@ -3238,7 +3238,7 @@ class WalletUtils {
|
|
|
3238
3238
|
}
|
|
3239
3239
|
|
|
3240
3240
|
class Wallet {
|
|
3241
|
-
constructor(
|
|
3241
|
+
constructor(_network, config) {
|
|
3242
3242
|
var _this = this;
|
|
3243
3243
|
|
|
3244
3244
|
this.utils = void 0;
|
|
@@ -3366,13 +3366,28 @@ class Wallet {
|
|
|
3366
3366
|
this.useSession({
|
|
3367
3367
|
walletContext: walletContext
|
|
3368
3368
|
}, true);
|
|
3369
|
-
});
|
|
3369
|
+
});
|
|
3370
|
+
};
|
|
3371
|
+
|
|
3372
|
+
this.loadSession = async function () {
|
|
3373
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3374
|
+
|
|
3375
|
+
if (!data || data === '') {
|
|
3376
|
+
return undefined;
|
|
3377
|
+
}
|
|
3378
|
+
|
|
3379
|
+
try {
|
|
3380
|
+
const session = JSON.parse(data);
|
|
3370
3381
|
|
|
3371
|
-
this.loadSession().then(session => {
|
|
3372
3382
|
if (session) {
|
|
3373
|
-
|
|
3383
|
+
_this.useSession(session, false);
|
|
3374
3384
|
}
|
|
3375
|
-
|
|
3385
|
+
|
|
3386
|
+
return session;
|
|
3387
|
+
} catch (err) {
|
|
3388
|
+
utils.logger.warn('loadSession failed, unable to parse session payload from storage.');
|
|
3389
|
+
return undefined;
|
|
3390
|
+
}
|
|
3376
3391
|
};
|
|
3377
3392
|
|
|
3378
3393
|
this.connect = async function (options) {
|
|
@@ -3506,26 +3521,10 @@ class Wallet {
|
|
|
3506
3521
|
this.transport.messageProvider.closeWallet();
|
|
3507
3522
|
};
|
|
3508
3523
|
|
|
3509
|
-
this.
|
|
3510
|
-
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3511
|
-
|
|
3512
|
-
if (!data || data === '') {
|
|
3513
|
-
return undefined;
|
|
3514
|
-
}
|
|
3515
|
-
|
|
3516
|
-
try {
|
|
3517
|
-
const session = JSON.parse(data);
|
|
3518
|
-
return session;
|
|
3519
|
-
} catch (err) {
|
|
3520
|
-
utils.logger.warn('loadSession failed, unable to parse session payload from localStorage.');
|
|
3521
|
-
return undefined;
|
|
3522
|
-
}
|
|
3523
|
-
};
|
|
3524
|
-
|
|
3525
|
-
this.saveSession = session => {
|
|
3524
|
+
this.saveSession = async function (session) {
|
|
3526
3525
|
utils.logger.debug('wallet provider: saving session');
|
|
3527
3526
|
const data = JSON.stringify(session);
|
|
3528
|
-
LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3527
|
+
await LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3529
3528
|
};
|
|
3530
3529
|
|
|
3531
3530
|
this.useSession = async function (session, autoSave = true) {
|
|
@@ -3566,8 +3565,8 @@ class Wallet {
|
|
|
3566
3565
|
this.config = _extends({}, this.config, config);
|
|
3567
3566
|
}
|
|
3568
3567
|
|
|
3569
|
-
if (
|
|
3570
|
-
this.config.defaultNetworkId =
|
|
3568
|
+
if (_network) {
|
|
3569
|
+
this.config.defaultNetworkId = _network;
|
|
3571
3570
|
} else if (!this.config.defaultNetworkId) {
|
|
3572
3571
|
this.config.defaultNetworkId = 'mainnet';
|
|
3573
3572
|
}
|
|
@@ -3622,7 +3621,9 @@ class Wallet {
|
|
|
3622
3621
|
}
|
|
3623
3622
|
|
|
3624
3623
|
this.clearSession();
|
|
3625
|
-
}
|
|
3624
|
+
} // TODO: add switchNetwork(network: string | number) which will call wallet_switchEthereumChain
|
|
3625
|
+
// and on successful response, will update the provider info here, etc.
|
|
3626
|
+
|
|
3626
3627
|
|
|
3627
3628
|
getProviderConfig() {
|
|
3628
3629
|
return this.config;
|
|
@@ -3802,6 +3803,23 @@ const DefaultProviderConfig = {
|
|
|
3802
3803
|
}
|
|
3803
3804
|
}
|
|
3804
3805
|
};
|
|
3806
|
+
let walletInstance;
|
|
3807
|
+
const initWallet = async (network, config) => {
|
|
3808
|
+
if (walletInstance && walletInstance.isOpened()) {
|
|
3809
|
+
walletInstance.closeWallet();
|
|
3810
|
+
}
|
|
3811
|
+
|
|
3812
|
+
walletInstance = new Wallet(network, config);
|
|
3813
|
+
await walletInstance.loadSession();
|
|
3814
|
+
return walletInstance;
|
|
3815
|
+
};
|
|
3816
|
+
const getWallet = () => {
|
|
3817
|
+
if (!walletInstance) {
|
|
3818
|
+
throw new Error('Wallet has not been initialized, call sequence.initWallet(network, config) first.');
|
|
3819
|
+
}
|
|
3820
|
+
|
|
3821
|
+
return walletInstance;
|
|
3822
|
+
};
|
|
3805
3823
|
|
|
3806
3824
|
exports.BaseInjectedTransport = BaseInjectedTransport;
|
|
3807
3825
|
exports.BaseProviderTransport = BaseProviderTransport;
|
|
@@ -3828,6 +3846,8 @@ exports.Web3Signer = Web3Signer;
|
|
|
3828
3846
|
exports.WindowMessageHandler = WindowMessageHandler;
|
|
3829
3847
|
exports.WindowMessageProvider = WindowMessageProvider;
|
|
3830
3848
|
exports.WindowSessionParams = WindowSessionParams;
|
|
3849
|
+
exports.getWallet = getWallet;
|
|
3850
|
+
exports.initWallet = initWallet;
|
|
3831
3851
|
exports.isBrowserExtension = isBrowserExtension;
|
|
3832
3852
|
exports.isSequenceProvider = isSequenceProvider;
|
|
3833
3853
|
exports.isValidMessageSignature = isValidMessageSignature;
|
|
@@ -3238,7 +3238,7 @@ class WalletUtils {
|
|
|
3238
3238
|
}
|
|
3239
3239
|
|
|
3240
3240
|
class Wallet {
|
|
3241
|
-
constructor(
|
|
3241
|
+
constructor(_network, config) {
|
|
3242
3242
|
var _this = this;
|
|
3243
3243
|
|
|
3244
3244
|
this.utils = void 0;
|
|
@@ -3366,13 +3366,28 @@ class Wallet {
|
|
|
3366
3366
|
this.useSession({
|
|
3367
3367
|
walletContext: walletContext
|
|
3368
3368
|
}, true);
|
|
3369
|
-
});
|
|
3369
|
+
});
|
|
3370
|
+
};
|
|
3371
|
+
|
|
3372
|
+
this.loadSession = async function () {
|
|
3373
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3374
|
+
|
|
3375
|
+
if (!data || data === '') {
|
|
3376
|
+
return undefined;
|
|
3377
|
+
}
|
|
3378
|
+
|
|
3379
|
+
try {
|
|
3380
|
+
const session = JSON.parse(data);
|
|
3370
3381
|
|
|
3371
|
-
this.loadSession().then(session => {
|
|
3372
3382
|
if (session) {
|
|
3373
|
-
|
|
3383
|
+
_this.useSession(session, false);
|
|
3374
3384
|
}
|
|
3375
|
-
|
|
3385
|
+
|
|
3386
|
+
return session;
|
|
3387
|
+
} catch (err) {
|
|
3388
|
+
utils.logger.warn('loadSession failed, unable to parse session payload from storage.');
|
|
3389
|
+
return undefined;
|
|
3390
|
+
}
|
|
3376
3391
|
};
|
|
3377
3392
|
|
|
3378
3393
|
this.connect = async function (options) {
|
|
@@ -3506,26 +3521,10 @@ class Wallet {
|
|
|
3506
3521
|
this.transport.messageProvider.closeWallet();
|
|
3507
3522
|
};
|
|
3508
3523
|
|
|
3509
|
-
this.
|
|
3510
|
-
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3511
|
-
|
|
3512
|
-
if (!data || data === '') {
|
|
3513
|
-
return undefined;
|
|
3514
|
-
}
|
|
3515
|
-
|
|
3516
|
-
try {
|
|
3517
|
-
const session = JSON.parse(data);
|
|
3518
|
-
return session;
|
|
3519
|
-
} catch (err) {
|
|
3520
|
-
utils.logger.warn('loadSession failed, unable to parse session payload from localStorage.');
|
|
3521
|
-
return undefined;
|
|
3522
|
-
}
|
|
3523
|
-
};
|
|
3524
|
-
|
|
3525
|
-
this.saveSession = session => {
|
|
3524
|
+
this.saveSession = async function (session) {
|
|
3526
3525
|
utils.logger.debug('wallet provider: saving session');
|
|
3527
3526
|
const data = JSON.stringify(session);
|
|
3528
|
-
LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3527
|
+
await LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3529
3528
|
};
|
|
3530
3529
|
|
|
3531
3530
|
this.useSession = async function (session, autoSave = true) {
|
|
@@ -3566,8 +3565,8 @@ class Wallet {
|
|
|
3566
3565
|
this.config = _extends({}, this.config, config);
|
|
3567
3566
|
}
|
|
3568
3567
|
|
|
3569
|
-
if (
|
|
3570
|
-
this.config.defaultNetworkId =
|
|
3568
|
+
if (_network) {
|
|
3569
|
+
this.config.defaultNetworkId = _network;
|
|
3571
3570
|
} else if (!this.config.defaultNetworkId) {
|
|
3572
3571
|
this.config.defaultNetworkId = 'mainnet';
|
|
3573
3572
|
}
|
|
@@ -3622,7 +3621,9 @@ class Wallet {
|
|
|
3622
3621
|
}
|
|
3623
3622
|
|
|
3624
3623
|
this.clearSession();
|
|
3625
|
-
}
|
|
3624
|
+
} // TODO: add switchNetwork(network: string | number) which will call wallet_switchEthereumChain
|
|
3625
|
+
// and on successful response, will update the provider info here, etc.
|
|
3626
|
+
|
|
3626
3627
|
|
|
3627
3628
|
getProviderConfig() {
|
|
3628
3629
|
return this.config;
|
|
@@ -3802,6 +3803,23 @@ const DefaultProviderConfig = {
|
|
|
3802
3803
|
}
|
|
3803
3804
|
}
|
|
3804
3805
|
};
|
|
3806
|
+
let walletInstance;
|
|
3807
|
+
const initWallet = async (network, config) => {
|
|
3808
|
+
if (walletInstance && walletInstance.isOpened()) {
|
|
3809
|
+
walletInstance.closeWallet();
|
|
3810
|
+
}
|
|
3811
|
+
|
|
3812
|
+
walletInstance = new Wallet(network, config);
|
|
3813
|
+
await walletInstance.loadSession();
|
|
3814
|
+
return walletInstance;
|
|
3815
|
+
};
|
|
3816
|
+
const getWallet = () => {
|
|
3817
|
+
if (!walletInstance) {
|
|
3818
|
+
throw new Error('Wallet has not been initialized, call sequence.initWallet(network, config) first.');
|
|
3819
|
+
}
|
|
3820
|
+
|
|
3821
|
+
return walletInstance;
|
|
3822
|
+
};
|
|
3805
3823
|
|
|
3806
3824
|
exports.BaseInjectedTransport = BaseInjectedTransport;
|
|
3807
3825
|
exports.BaseProviderTransport = BaseProviderTransport;
|
|
@@ -3828,6 +3846,8 @@ exports.Web3Signer = Web3Signer;
|
|
|
3828
3846
|
exports.WindowMessageHandler = WindowMessageHandler;
|
|
3829
3847
|
exports.WindowMessageProvider = WindowMessageProvider;
|
|
3830
3848
|
exports.WindowSessionParams = WindowSessionParams;
|
|
3849
|
+
exports.getWallet = getWallet;
|
|
3850
|
+
exports.initWallet = initWallet;
|
|
3831
3851
|
exports.isBrowserExtension = isBrowserExtension;
|
|
3832
3852
|
exports.isSequenceProvider = isSequenceProvider;
|
|
3833
3853
|
exports.isValidMessageSignature = isValidMessageSignature;
|
|
@@ -3234,7 +3234,7 @@ class WalletUtils {
|
|
|
3234
3234
|
}
|
|
3235
3235
|
|
|
3236
3236
|
class Wallet {
|
|
3237
|
-
constructor(
|
|
3237
|
+
constructor(_network, config) {
|
|
3238
3238
|
var _this = this;
|
|
3239
3239
|
|
|
3240
3240
|
this.utils = void 0;
|
|
@@ -3362,13 +3362,28 @@ class Wallet {
|
|
|
3362
3362
|
this.useSession({
|
|
3363
3363
|
walletContext: walletContext
|
|
3364
3364
|
}, true);
|
|
3365
|
-
});
|
|
3365
|
+
});
|
|
3366
|
+
};
|
|
3367
|
+
|
|
3368
|
+
this.loadSession = async function () {
|
|
3369
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3370
|
+
|
|
3371
|
+
if (!data || data === '') {
|
|
3372
|
+
return undefined;
|
|
3373
|
+
}
|
|
3374
|
+
|
|
3375
|
+
try {
|
|
3376
|
+
const session = JSON.parse(data);
|
|
3366
3377
|
|
|
3367
|
-
this.loadSession().then(session => {
|
|
3368
3378
|
if (session) {
|
|
3369
|
-
|
|
3379
|
+
_this.useSession(session, false);
|
|
3370
3380
|
}
|
|
3371
|
-
|
|
3381
|
+
|
|
3382
|
+
return session;
|
|
3383
|
+
} catch (err) {
|
|
3384
|
+
logger.warn('loadSession failed, unable to parse session payload from storage.');
|
|
3385
|
+
return undefined;
|
|
3386
|
+
}
|
|
3372
3387
|
};
|
|
3373
3388
|
|
|
3374
3389
|
this.connect = async function (options) {
|
|
@@ -3502,26 +3517,10 @@ class Wallet {
|
|
|
3502
3517
|
this.transport.messageProvider.closeWallet();
|
|
3503
3518
|
};
|
|
3504
3519
|
|
|
3505
|
-
this.
|
|
3506
|
-
const data = await LocalStorage.getInstance().getItem('@sequence.session');
|
|
3507
|
-
|
|
3508
|
-
if (!data || data === '') {
|
|
3509
|
-
return undefined;
|
|
3510
|
-
}
|
|
3511
|
-
|
|
3512
|
-
try {
|
|
3513
|
-
const session = JSON.parse(data);
|
|
3514
|
-
return session;
|
|
3515
|
-
} catch (err) {
|
|
3516
|
-
logger.warn('loadSession failed, unable to parse session payload from localStorage.');
|
|
3517
|
-
return undefined;
|
|
3518
|
-
}
|
|
3519
|
-
};
|
|
3520
|
-
|
|
3521
|
-
this.saveSession = session => {
|
|
3520
|
+
this.saveSession = async function (session) {
|
|
3522
3521
|
logger.debug('wallet provider: saving session');
|
|
3523
3522
|
const data = JSON.stringify(session);
|
|
3524
|
-
LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3523
|
+
await LocalStorage.getInstance().setItem('@sequence.session', data);
|
|
3525
3524
|
};
|
|
3526
3525
|
|
|
3527
3526
|
this.useSession = async function (session, autoSave = true) {
|
|
@@ -3562,8 +3561,8 @@ class Wallet {
|
|
|
3562
3561
|
this.config = _extends({}, this.config, config);
|
|
3563
3562
|
}
|
|
3564
3563
|
|
|
3565
|
-
if (
|
|
3566
|
-
this.config.defaultNetworkId =
|
|
3564
|
+
if (_network) {
|
|
3565
|
+
this.config.defaultNetworkId = _network;
|
|
3567
3566
|
} else if (!this.config.defaultNetworkId) {
|
|
3568
3567
|
this.config.defaultNetworkId = 'mainnet';
|
|
3569
3568
|
}
|
|
@@ -3618,7 +3617,9 @@ class Wallet {
|
|
|
3618
3617
|
}
|
|
3619
3618
|
|
|
3620
3619
|
this.clearSession();
|
|
3621
|
-
}
|
|
3620
|
+
} // TODO: add switchNetwork(network: string | number) which will call wallet_switchEthereumChain
|
|
3621
|
+
// and on successful response, will update the provider info here, etc.
|
|
3622
|
+
|
|
3622
3623
|
|
|
3623
3624
|
getProviderConfig() {
|
|
3624
3625
|
return this.config;
|
|
@@ -3798,5 +3799,22 @@ const DefaultProviderConfig = {
|
|
|
3798
3799
|
}
|
|
3799
3800
|
}
|
|
3800
3801
|
};
|
|
3802
|
+
let walletInstance;
|
|
3803
|
+
const initWallet = async (network, config) => {
|
|
3804
|
+
if (walletInstance && walletInstance.isOpened()) {
|
|
3805
|
+
walletInstance.closeWallet();
|
|
3806
|
+
}
|
|
3807
|
+
|
|
3808
|
+
walletInstance = new Wallet(network, config);
|
|
3809
|
+
await walletInstance.loadSession();
|
|
3810
|
+
return walletInstance;
|
|
3811
|
+
};
|
|
3812
|
+
const getWallet = () => {
|
|
3813
|
+
if (!walletInstance) {
|
|
3814
|
+
throw new Error('Wallet has not been initialized, call sequence.initWallet(network, config) first.');
|
|
3815
|
+
}
|
|
3816
|
+
|
|
3817
|
+
return walletInstance;
|
|
3818
|
+
};
|
|
3801
3819
|
|
|
3802
|
-
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 };
|
|
3820
|
+
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, getWallet, initWallet, isBrowserExtension, isSequenceProvider, isValidMessageSignature, isValidSignature, isValidTypedDataSignature, isWalletUpToDate, messageToBytes, nextMessageIdx, prefixEIP191Message, recoverWalletConfig };
|
|
@@ -9,13 +9,13 @@ import { Runtime } from 'webextension-polyfill-ts';
|
|
|
9
9
|
export interface WalletProvider {
|
|
10
10
|
connect(options?: ConnectOptions): Promise<ConnectDetails>;
|
|
11
11
|
disconnect(): void;
|
|
12
|
-
isOpened(): boolean;
|
|
13
12
|
isConnected(): boolean;
|
|
14
13
|
getSession(): WalletSession | undefined;
|
|
15
14
|
getAddress(): Promise<string>;
|
|
16
15
|
getNetworks(chainId?: ChainIdLike): Promise<NetworkConfig[]>;
|
|
17
16
|
getChainId(): Promise<number>;
|
|
18
17
|
getAuthChainId(): Promise<number>;
|
|
18
|
+
isOpened(): boolean;
|
|
19
19
|
openWallet(path?: string, intent?: OpenWalletIntent, networkId?: string | number): Promise<boolean>;
|
|
20
20
|
closeWallet(): void;
|
|
21
21
|
getProvider(chainId?: ChainIdLike): Web3Provider | undefined;
|
|
@@ -37,8 +37,9 @@ export declare class Wallet implements WalletProvider {
|
|
|
37
37
|
private transport;
|
|
38
38
|
private networks;
|
|
39
39
|
private providers;
|
|
40
|
-
constructor(
|
|
40
|
+
constructor(network?: string | number, config?: Partial<ProviderConfig>);
|
|
41
41
|
private init;
|
|
42
|
+
loadSession: () => Promise<WalletSession | undefined>;
|
|
42
43
|
connect: (options?: ConnectOptions | undefined) => Promise<ConnectDetails>;
|
|
43
44
|
addConnectedSite(origin: string | undefined): Promise<void>;
|
|
44
45
|
removeConnectedSite(origin: string): Promise<void>;
|
|
@@ -70,7 +71,6 @@ export declare class Wallet implements WalletProvider {
|
|
|
70
71
|
isDeployed(chainId?: ChainIdLike): Promise<boolean>;
|
|
71
72
|
on<K extends keyof ProviderEventTypes>(event: K, fn: ProviderEventTypes[K]): void;
|
|
72
73
|
once<K extends keyof ProviderEventTypes>(event: K, fn: ProviderEventTypes[K]): void;
|
|
73
|
-
private loadSession;
|
|
74
74
|
private saveSession;
|
|
75
75
|
private useSession;
|
|
76
76
|
private useAccountAddress;
|
|
@@ -99,3 +99,5 @@ export interface ProviderConfig {
|
|
|
99
99
|
walletContext?: WalletContext;
|
|
100
100
|
}
|
|
101
101
|
export declare const DefaultProviderConfig: ProviderConfig;
|
|
102
|
+
export declare const initWallet: (network?: string | number | undefined, config?: Partial<ProviderConfig> | undefined) => Promise<Wallet>;
|
|
103
|
+
export declare const getWallet: () => Wallet;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xsequence/provider",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.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.40.2",
|
|
17
|
+
"@0xsequence/auth": "^0.40.2",
|
|
18
|
+
"@0xsequence/config": "^0.40.2",
|
|
19
|
+
"@0xsequence/network": "^0.40.2",
|
|
20
|
+
"@0xsequence/transactions": "^0.40.2",
|
|
21
|
+
"@0xsequence/utils": "^0.40.2",
|
|
22
|
+
"@0xsequence/wallet": "^0.40.2",
|
|
23
23
|
"@ethersproject/abstract-signer": "^5.5.0",
|
|
24
24
|
"@ethersproject/hash": "^5.5.0",
|
|
25
25
|
"@ethersproject/providers": "^5.5.1",
|
package/src/wallet.ts
CHANGED
|
@@ -32,18 +32,17 @@ import { Runtime } from 'webextension-polyfill-ts'
|
|
|
32
32
|
|
|
33
33
|
export interface WalletProvider {
|
|
34
34
|
connect(options?: ConnectOptions): Promise<ConnectDetails>
|
|
35
|
-
// authorize(options?: ConnectOptions): Promise<ConnectDetails>
|
|
36
35
|
disconnect(): void
|
|
37
36
|
|
|
38
|
-
isOpened(): boolean
|
|
39
37
|
isConnected(): boolean
|
|
40
38
|
getSession(): WalletSession | undefined
|
|
41
|
-
|
|
39
|
+
|
|
42
40
|
getAddress(): Promise<string>
|
|
43
41
|
getNetworks(chainId?: ChainIdLike): Promise<NetworkConfig[]>
|
|
44
42
|
getChainId(): Promise<number>
|
|
45
43
|
getAuthChainId(): Promise<number>
|
|
46
|
-
|
|
44
|
+
|
|
45
|
+
isOpened(): boolean
|
|
47
46
|
openWallet(path?: string, intent?: OpenWalletIntent, networkId?: string | number): Promise<boolean>
|
|
48
47
|
closeWallet(): void
|
|
49
48
|
|
|
@@ -91,15 +90,15 @@ export class Wallet implements WalletProvider {
|
|
|
91
90
|
private networks: NetworkConfig[]
|
|
92
91
|
private providers: { [chainId: number]: Web3Provider }
|
|
93
92
|
|
|
94
|
-
constructor(
|
|
93
|
+
constructor(network?: string | number, config?: Partial<ProviderConfig>) {
|
|
95
94
|
// config is a Partial, so that we may intersect it with the DefaultProviderConfig,
|
|
96
95
|
// which allows easy overriding and control of the config.
|
|
97
96
|
this.config = { ...DefaultProviderConfig }
|
|
98
97
|
if (config) {
|
|
99
98
|
this.config = { ...this.config, ...config }
|
|
100
99
|
}
|
|
101
|
-
if (
|
|
102
|
-
this.config.defaultNetworkId =
|
|
100
|
+
if (network) {
|
|
101
|
+
this.config.defaultNetworkId = network
|
|
103
102
|
} else if (!this.config.defaultNetworkId) {
|
|
104
103
|
this.config.defaultNetworkId = 'mainnet'
|
|
105
104
|
}
|
|
@@ -237,13 +236,23 @@ export class Wallet implements WalletProvider {
|
|
|
237
236
|
this.transport.messageProvider.on('walletContext', (walletContext: WalletContext) => {
|
|
238
237
|
this.useSession({ walletContext: walletContext }, true)
|
|
239
238
|
})
|
|
239
|
+
}
|
|
240
240
|
|
|
241
|
-
|
|
242
|
-
|
|
241
|
+
loadSession = async (): Promise<WalletSession | undefined> => {
|
|
242
|
+
const data = await LocalStorage.getInstance().getItem('@sequence.session')
|
|
243
|
+
if (!data || data === '') {
|
|
244
|
+
return undefined
|
|
245
|
+
}
|
|
246
|
+
try {
|
|
247
|
+
const session = JSON.parse(data) as WalletSession
|
|
243
248
|
if (session) {
|
|
244
249
|
this.useSession(session, false)
|
|
245
250
|
}
|
|
246
|
-
|
|
251
|
+
return session
|
|
252
|
+
} catch (err) {
|
|
253
|
+
logger.warn('loadSession failed, unable to parse session payload from storage.')
|
|
254
|
+
return undefined
|
|
255
|
+
}
|
|
247
256
|
}
|
|
248
257
|
|
|
249
258
|
connect = async (options?: ConnectOptions): Promise<ConnectDetails> => {
|
|
@@ -333,6 +342,9 @@ export class Wallet implements WalletProvider {
|
|
|
333
342
|
this.clearSession()
|
|
334
343
|
}
|
|
335
344
|
|
|
345
|
+
// TODO: add switchNetwork(network: string | number) which will call wallet_switchEthereumChain
|
|
346
|
+
// and on successful response, will update the provider info here, etc.
|
|
347
|
+
|
|
336
348
|
getProviderConfig(): ProviderConfig {
|
|
337
349
|
return this.config
|
|
338
350
|
}
|
|
@@ -545,24 +557,10 @@ export class Wallet implements WalletProvider {
|
|
|
545
557
|
this.transport.messageProvider!.once(event, fn)
|
|
546
558
|
}
|
|
547
559
|
|
|
548
|
-
private
|
|
549
|
-
const data = await LocalStorage.getInstance().getItem('@sequence.session')
|
|
550
|
-
if (!data || data === '') {
|
|
551
|
-
return undefined
|
|
552
|
-
}
|
|
553
|
-
try {
|
|
554
|
-
const session = JSON.parse(data) as WalletSession
|
|
555
|
-
return session
|
|
556
|
-
} catch (err) {
|
|
557
|
-
logger.warn('loadSession failed, unable to parse session payload from localStorage.')
|
|
558
|
-
return undefined
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
private saveSession = (session: WalletSession) => {
|
|
560
|
+
private saveSession = async (session: WalletSession) => {
|
|
563
561
|
logger.debug('wallet provider: saving session')
|
|
564
562
|
const data = JSON.stringify(session)
|
|
565
|
-
LocalStorage.getInstance().setItem('@sequence.session', data)
|
|
563
|
+
await LocalStorage.getInstance().setItem('@sequence.session', data)
|
|
566
564
|
}
|
|
567
565
|
|
|
568
566
|
private useSession = async (session: WalletSession, autoSave: boolean = true) => {
|
|
@@ -713,3 +711,21 @@ export const DefaultProviderConfig: ProviderConfig = {
|
|
|
713
711
|
proxyTransport: { enabled: false }
|
|
714
712
|
}
|
|
715
713
|
}
|
|
714
|
+
|
|
715
|
+
let walletInstance: Wallet | undefined
|
|
716
|
+
|
|
717
|
+
export const initWallet = async (network?: string | number, config?: Partial<ProviderConfig>) => {
|
|
718
|
+
if (walletInstance && walletInstance.isOpened()) {
|
|
719
|
+
walletInstance.closeWallet()
|
|
720
|
+
}
|
|
721
|
+
walletInstance = new Wallet(network, config)
|
|
722
|
+
await walletInstance.loadSession()
|
|
723
|
+
return walletInstance
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
export const getWallet = () => {
|
|
727
|
+
if (!walletInstance) {
|
|
728
|
+
throw new Error('Wallet has not been initialized, call sequence.initWallet(network, config) first.')
|
|
729
|
+
}
|
|
730
|
+
return walletInstance
|
|
731
|
+
}
|