-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added walletconnect 2.0 support
- Loading branch information
Showing
9 changed files
with
1,399 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 * from './is-wallet-provider'; | ||
export * from './is-wallet-connect-provider'; |
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,5 @@ | ||
import { EthereumProvider, WalletProviderLike } from '../interfaces'; | ||
|
||
export function isWalletConnectProvider(provider: WalletProviderLike): boolean { | ||
return typeof provider === 'object' && (provider as EthereumProvider)?.isWalletConnect; | ||
} |
60 changes: 60 additions & 0 deletions
60
src/sdk/wallet/providers/wallet-connect-2.wallet-provider.ts
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,60 @@ | ||
import { BytesLike } from 'ethers'; | ||
import { toHex } from '../../common'; | ||
import { DynamicWalletProvider } from './dynamic.wallet-provider'; | ||
import { EthereumProvider } from './interfaces'; | ||
|
||
export class WalletConnect2WalletProvider extends DynamicWalletProvider { | ||
constructor(readonly provider: EthereumProvider) { | ||
super('WalletConnect2'); | ||
|
||
try { | ||
const { | ||
accounts: [address], | ||
chainId, | ||
} = provider; | ||
|
||
this.setAddress(address); | ||
this.setNetworkName(chainId); | ||
} catch (err) { | ||
// | ||
} | ||
|
||
this.updateSessionHandler = this.updateSessionHandler.bind(this); | ||
|
||
provider.on('connect', this.updateSessionHandler); | ||
provider.on('session_event', this.updateSessionHandler); | ||
provider.on('disconnect', () => { | ||
this.setAddress(null); | ||
this.setNetworkName(null); | ||
}); | ||
} | ||
|
||
async signMessage(message: BytesLike): Promise<string> { | ||
const response = await this.provider.signer.request({ | ||
method: 'personal_sign', | ||
params: [toHex(message), this.address], | ||
}); | ||
|
||
return typeof response === 'string' ? response : null; | ||
} | ||
|
||
protected updateSessionHandler(error: Error, payload: { params: { accounts: string[]; chainId: number } }): void { | ||
let address: string = null; | ||
let chainId: number = null; | ||
|
||
if (!error) { | ||
try { | ||
({ | ||
accounts: [address], | ||
chainId, | ||
} = payload.params[0]); | ||
} catch (err) { | ||
address = null; | ||
chainId = null; | ||
} | ||
} | ||
|
||
this.setAddress(address); | ||
this.setNetworkName(chainId); | ||
} | ||
} |
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