Skip to content

Commit

Permalink
implement init and add to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 21, 2023
1 parent 4c35293 commit b3d1956
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
3 changes: 3 additions & 0 deletions packages/crypto/src/wallets/EthWalletConnectorBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,7 @@ export abstract class EthWalletConnectorBase extends EIP1193Events {
private logProviderMissing() {
console.warn('Cannot call this method because there is no browser provider connected. Please confirm that metamask is installed')
}

// Since init likely relies on derived class members, derived classes must implement it
abstract init(): void
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export class CoinbaseConnector extends EthWalletConnectorBase {

constructor(provider?: BrowserProvider) {
super(['EIP-1193'])
this.init(provider)
}

get installed() {
return !!(this.ethereum && this.ethereum.isCoinbaseWallet)
}

init(provider?: BrowserProvider) {
if (provider) {
this.provider = provider
} else if (window.ethereum) {
Expand All @@ -24,8 +32,4 @@ export class CoinbaseConnector extends EthWalletConnectorBase {
this.onChainChangedListener()
}
}

get installed() {
return !!(this.ethereum && this.ethereum.isCoinbaseWallet)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ export class MetaMaskConnector extends EthWalletConnectorBase {

constructor(provider?: BrowserProvider) {
super(['EIP-1193'])
this.init(provider)
}

get installed() {
return !!(this.ethereum && this.ethereum.isMetaMask)
}

init(provider?: BrowserProvider) {
if (provider) {
this.provider = provider
} else if (window.ethereum) {
this.provider = new BrowserProvider(window.ethereum)
} else {
console.warn('Attempting to use metamask class when its not installed')
console.warn(`Attempting to use ${this.providerName} class when its not installed`)
}
if (this.installed) {
this.onAccountsChangedListener()
this.onChainChangedListener()
}
}

get installed() {
return !!(this.ethereum && this.ethereum.isMetaMask)
}
}
12 changes: 8 additions & 4 deletions packages/crypto/src/wallets/third-party/Trust/TrustConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export class TrustConnector extends EthWalletConnectorBase {

constructor(provider?: BrowserProvider) {
super(['EIP-1193'])
this.init(provider)
}

get installed() {
return !!(this.ethereum && this.ethereum.isTrust)
}

init(provider?: BrowserProvider) {
if (provider) {
this.provider = provider
} else if (window.ethereum) {
Expand All @@ -24,8 +32,4 @@ export class TrustConnector extends EthWalletConnectorBase {
this.onChainChangedListener()
}
}

get installed() {
return !!(this.ethereum && this.ethereum.isTrust)
}
}

0 comments on commit b3d1956

Please sign in to comment.