Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create an Eip1193Wallet that allows connection to any wallet th…
…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
Showing
6 changed files
with
121 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.` | ||
} | ||
} |