diff --git a/src/components/chain/BlockChain.js b/src/components/chain/BlockChain.js index d87c6dc..174442a 100644 --- a/src/components/chain/BlockChain.js +++ b/src/components/chain/BlockChain.js @@ -132,6 +132,20 @@ export class BlockChain { return token+suffix } + /** + * 如果是用passport登录的, 直接返回store中的token, 否则调用passport的getAccessToken + * @returns + */ + async passportToken() { + if (this.store.walletType == 3) { + return this.store.token + } + if (!this.passportProvider) { + return '' + } + const res = await new PassportWallet().getAccessToken(); + return res.token + } async logout() { if (this.store.walletType != 3 && this.passportProvider) { @@ -148,11 +162,12 @@ export class BlockChain { /** * 检查并切换到目标链, 各上链前须调用该方法 */ - async checkAndChangeChain() { + async checkAndChangeChain(targetChainId) { + targetChainId = targetChainId || cfgChainId; let chainId = await this.getChainId(); - if (chainId !== cfgChainId) { - console.log(`current chain: ${chainId}, want: ${cfgChainId}`) - chainId = await switchEthereumChain(this.web3Provider.provider, cfgChainId); + if (chainId !== targetChainId) { + console.log(`current chain: ${chainId}, want: ${targetChainId}`) + chainId = await switchEthereumChain(this.web3Provider.provider, targetChainId); } } @@ -175,4 +190,26 @@ export class BlockChain { await this.appendPassport(); } } + + async selectAddress({title, subTitle, targetChainId}) { + const initData = {} + initData[this.store.walletType] = this.store.address + const rewardModal = createModal(WalletSelectModel, { + title: title || 'Select Address', + message: subTitle || 'Please select the address you want to use', + initData: { 1: 'MetaMask', 2: 'OKX Wallet', 3: 'Passport' } + }); + const { errcode, errmsg, walletInstance, provider, accounts } = await rewardModal.show(); + if (errcode) { + console.log(`select address result : ${errmsg}`); + throw new Error(errmsg); + } + targetChainId = targetChainId || cfgChainId; + let chainId = await walletInstance.getChainId(); + if (chainId !== targetChainId) { + console.log(`current chain: ${chainId}, want: ${targetChainId}`) + chainId = await switchEthereumChain(provider.provider, targetChainId); + } + return { provider, address: accounts[0] }; + } } diff --git a/src/components/chain/WalletSelectModel.vue b/src/components/chain/WalletSelectModel.vue index 6eddbfc..e0b7874 100644 --- a/src/components/chain/WalletSelectModel.vue +++ b/src/components/chain/WalletSelectModel.vue @@ -3,7 +3,7 @@