Skip to content

Commit

Permalink
feat: create an Eip1193Wallet that allows connection to any wallet th…
Browse files Browse the repository at this point in the history
…at respect the eip-1993

- MetamaskWallet and BinanceChainWallet now inherits from the Eip1193Wallet class
  • Loading branch information
0Lilian committed Sep 4, 2022
1 parent f2c23a5 commit 55a75c5
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 109 deletions.
7 changes: 2 additions & 5 deletions src/composables/proxies/provider/placeholder.js
Expand Up @@ -26,11 +26,8 @@ export class TulipeProviderPlaceholder extends TulipePlaceholder {

_autoInstantiateFromWallet () {
for (const wallet of Object.values(dapp.wallets)) {
if (wallet) {
const walletProvider = wallet.getProvider();
if (walletProvider) {
this.proxy.ethersInstance = new ethers.providers.Web3Provider(walletProvider, "any");
}
if (wallet && wallet.provider) {
this.proxy.ethersInstance = wallet.provider;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/composables/proxies/signer/placeholder.js
Expand Up @@ -51,6 +51,7 @@ export class TulipeSignerPlaceholder extends TulipePlaceholder {
}

async _autoInstantiate () {
console.log(dapp.wallets)
for (const walletId of Object.keys(dapp.wallets)) {
await this.connectWallet(walletId, true);
if (this.proxy.ethersInstance) {
Expand Down
22 changes: 14 additions & 8 deletions src/composables/wallets/binance-chain.js
@@ -1,15 +1,21 @@
import { MetamaskWallet } from "./metamask.js";
import { Eip1193Wallet } from "./eip-1193.js";

export class BinanceChainWallet extends MetamaskWallet {
export class BinanceChainWallet extends Eip1193Wallet {

constructor () {
super();
this.id = "binanceChain";
this.provider = window.BinanceChain
super("binanceChain", window.BinanceChain);
}

async getSigner () {
if (this.provider) {
// const ethersProvider = this.getEthersProvider()
return await this.provider.getSigner();
}
return null
}

// async connect () inherited from MetamaskWallet
async connect () {
super.connect.call(this)
}
// async connect () {
// super.connect.call(this)
// }
}
96 changes: 96 additions & 0 deletions src/composables/wallets/eip-1193.js
@@ -0,0 +1,96 @@
import { Wallet } from "./wallet.js";
import { ethers } from "ethers";
import { WalletConnectionRejected } from "./errors.js";
import { dapp } from "../../index.js";

export class Eip1193Wallet extends Wallet {

constructor (id, exposedObject=null) {
super(id);
this.exposedObject = exposedObject;
this.provider = this.getProvider();
this.signer = this.getSigner();
}

getProvider () {
if (this.exposedObject) {
const ethersProvider = new ethers.providers.Web3Provider(this.exposedObject, "any")
return ethersProvider
}
return null
}

async getSigner () {
if (this.provider) {
// const ethersProvider = this.getEthersProvider()
return await this.provider.getSigner();
}
return null
}

async connect (lazy=false) {
if (this.provider) {
try {
await this.exposedObject.request({ method: 'eth_requestAccounts' })
// this.signer = this.getSigner()
}
catch (e) {
if (e.code === 4001) {
throw new WalletConnectionRejected(this.id);
}
else {
throw e
}
}
}
}


async addNetwork (id) {
const networkConfig = dapp.config.networks.getById(id);

await this.exposedObject.request({
method: "wallet_addEthereumChain",
params: [{
chainId: id,
rpcUrls: [networkConfig.defaultRPC],
chainName: networkConfig.name,
nativeCurrency: {
name: networkConfig.currency.symbol,
symbol: networkConfig.currency.symbol,
decimals: networkConfig.currency.decimals
},
blockExplorerUrls: networkConfig.explorer && networkConfig.explorer.url !== "" ? [networkConfig.explorer.url] : null,
}]
});
}

async changeNetwork (id, lazy=false) {
const networkConfig = dapp.config.networks.getById(id);

if (networkConfig) {
id = ethers.utils.hexlify(parseInt(id)).toString()
id = ethers.utils.hexValue(id)

try {
await this.exposedObject.request({
method: "wallet_switchEthereumChain",
params: [{
chainId: id,
}]
});
}
catch (e) {

// If the user don't have the network, add it.
if (e.code === 4902) {
this.addNetwork(id)
}
else {
throw e
}
}
}
}

}
73 changes: 4 additions & 69 deletions src/composables/wallets/metamask.js
@@ -1,74 +1,9 @@
import { Wallet } from "./wallet.js";
import { WalletConnectionRejected } from "./errors.js";
import { dapp } from "../../index.js";
import { ethers } from "ethers";
import { Eip1193Wallet } from "./eip-1193.js";

export class MetamaskWallet extends Wallet {

constructor () {
super();
this.id = "metamask";
this.provider = window.ethereum;
}

async connect () {
try {
await this.provider.request({ method: 'eth_requestAccounts' })
}
catch (e) {
if (e.code === 4001) {
throw new WalletConnectionRejected(this.id);
}
else {
throw e
}
}
}

async addNetwork (id) {
const networkConfig = dapp.config.networks.getById(id);
export class MetamaskWallet extends Eip1193Wallet {

await window.ethereum.request({
method: "wallet_addEthereumChain",
params: [{
chainId: id,
rpcUrls: [networkConfig.defaultRPC],
chainName: networkConfig.name,
nativeCurrency: {
name: networkConfig.currency.symbol,
symbol: networkConfig.currency.symbol,
decimals: networkConfig.currency.decimals
},
blockExplorerUrls: networkConfig.explorer && networkConfig.explorer.url !== "" ? [networkConfig.explorer.url] : null,
}]
});
}

async changeNetwork (id) {
const networkConfig = dapp.config.networks.getById(id);

if (networkConfig) {
id = ethers.utils.hexlify(parseInt(id)).toString()
id = ethers.utils.hexValue(id)

try {
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{
chainId: id,
}]
});
}
catch (e) {

// If the user don't have the network, add it.
if (e.code === 4902) {
this.addNetwork(id)
}
else {
throw e
}
}
}
constructor () {
super("metamask", window.ethereum);
}
}
31 changes: 4 additions & 27 deletions src/composables/wallets/wallet.js
@@ -1,33 +1,10 @@
import { ethers } from "ethers";

export class Wallet {

constructor () {
this.provider = null;
this.id = "";
}

getProvider () {
return null;
}

getEthersProvider () {
if (this.provider) {
const ethersProvider = new ethers.providers.Web3Provider(this.provider, "any")
return ethersProvider
constructor (id) {
this.id = id;
}
return null
}

async getSigner () {
if (this.provider) {
const ethersProvider = this.getEthersProvider()
return await ethersProvider.getSigner();
async connect () {
throw `connect() method of ${this.id} wallet is not implemented.`
}
return null
}

async connect () {
throw `connect() method of ${this.id} wallet is not implemented.`
}
}

0 comments on commit 55a75c5

Please sign in to comment.