-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: base implementation for reactive controller of wallet manager
- Loading branch information
Showing
7 changed files
with
201 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { ReactiveController, ReactiveControllerHost } from 'lit'; | ||
import { EvmWallet, SubstrateWallet } from '.'; | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { AddChain } from './types'; | ||
import { IWalletManagerController } from './wallets/interfaces'; | ||
|
||
export class WalletManagerController implements IWalletManagerController { | ||
host: ReactiveControllerHost; | ||
evmWallet?: EvmWallet; | ||
substrateWallet?: SubstrateWallet; | ||
account?: string; | ||
substrateAccount?: string; | ||
|
||
constructor(host: ReactiveControllerHost) { | ||
(this.host = host).addController(this as ReactiveController); | ||
} | ||
|
||
/** | ||
* @name initFromWeb3Provider | ||
* @param web3Provider Web3Provider | ||
* @description Initializes the EvmWallet from a Web3Provider | ||
*/ | ||
public initFromWeb3Provider(web3Provider: Web3Provider): void { | ||
this.evmWallet = EvmWallet.initFromWeb3Provider(web3Provider); | ||
this.appendProviderEvents(this.evmWallet); | ||
} | ||
|
||
/** | ||
* @name initFromWindow | ||
* @description Initializes the EvmWallet from a valid EIP-1193 provider | ||
*/ | ||
public initFromWindow(): void { | ||
this.evmWallet = EvmWallet.initFromWindow(); | ||
} | ||
|
||
/** | ||
* @name connectFromApiPromise | ||
* @param apiPromise | ||
* @description Initializes the SubstrateWallet from an ApiPromise | ||
*/ | ||
public connectFromApiPromise(apiPromise: ApiPromise): void { | ||
this.substrateWallet = SubstrateWallet.connectFromApiPromise(apiPromise); | ||
} | ||
|
||
/** | ||
* @name connectFromWssProvider | ||
* @param wssProvider | ||
* @description Initializes the SubstrateWallet from a wssProvider | ||
*/ | ||
public async connectFromWssProvider(wssProvider: string): Promise<void> { | ||
this.substrateWallet = | ||
await SubstrateWallet.connectFromWssProvider(wssProvider); | ||
} | ||
|
||
/** | ||
* @name addChain | ||
* @param { AddChain } | ||
* @description Adds a chain to the EvmWallet | ||
* @returns void | ||
*/ | ||
public async addChain({ | ||
chainId, | ||
chainName, | ||
rpcUrl, | ||
nativeCurrency | ||
}: AddChain): Promise<void> { | ||
try { | ||
await this.evmWallet?.addChain({ | ||
chainId, | ||
chainName, | ||
rpcUrl, | ||
nativeCurrency | ||
}); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* @name connect | ||
* @returns void | ||
* @description Connects the Substrate extension key manager | ||
*/ | ||
public async connectoToSubstrate(): Promise<void> { | ||
await this.substrateWallet?.connect(); | ||
this.substrateAccount = this.substrateWallet?.substrateAccount; | ||
this.host.requestUpdate(); | ||
} | ||
|
||
/** | ||
* @name connectEvmWallet | ||
* @returns void | ||
* @description Connects the EvmWallet | ||
*/ | ||
public async connectEvmWallet(): Promise<void> { | ||
await this.evmWallet?.connect(); | ||
this.account = this.evmWallet?.account; | ||
this.host.requestUpdate(); | ||
} | ||
|
||
get accountData(): string | undefined { | ||
return this.account; | ||
} | ||
|
||
get substrateAccountAddress(): string | undefined { | ||
return this.substrateAccount; | ||
} | ||
|
||
private appendProviderEvents(evmWallet: EvmWallet): void { | ||
evmWallet.addListener('walletAccountChanged', (account) => { | ||
this.account = account; | ||
this.host.requestUpdate(); | ||
}); | ||
} | ||
} |
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 +1,2 @@ | ||
export { EvmWallet, SubstrateWallet } from './wallets'; | ||
export { WalletManagerController } from './WalletManagerController'; |
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
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