-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🦊 Metamask integration POC #4835
Open
thesan
wants to merge
10
commits into
dev
Choose a base branch
from
feature/metamask-snap
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e50b017
Temporarily generate the http-rpc endpoint from the ws one
thesan 95ef07e
Add Metamask wallet
thesan d02f3ac
Use `@chainsafe/polkadot-snap`
thesan 6985973
Merge branch 'dev' of github.com:Joystream/pioneer into feature/metam…
thesan 90436ef
Set RPC node HTTP endpoint
thesan 751174d
Fix failing test
thesan 1aa49cb
Fix metamask detection
thesan 3f3ed8d
Remove unnecessary code
thesan 3e87533
Merge branch 'dev' of github.com:Joystream/pioneer into feature/metam…
thesan 645192a
Do not auto select wallet extensions
thesan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { enablePolkadotSnap } from '@chainsafe/metamask-polkadot-adapter' | ||
import { MetamaskSnapApi } from '@chainsafe/metamask-polkadot-adapter/build/types' | ||
import { SnapNetworks } from '@chainsafe/metamask-polkadot-types' | ||
import { Signer } from '@polkadot/types/types' | ||
import { BaseDotsamaWallet, SubscriptionFn, WalletAccount } from 'injectweb3-connect' | ||
|
||
import MetamaskLogo from '@/app/assets/images/logos/Metamask.svg' | ||
import { CHAIN_PROPERTIES } from '@/app/constants/chain' | ||
|
||
const networkName = 'joystream' as SnapNetworks | ||
const addressPrefix = CHAIN_PROPERTIES.ss58Format | ||
const unit = { symbol: CHAIN_PROPERTIES.tokenSymbol[0], decimals: CHAIN_PROPERTIES.tokenDecimals[0] } | ||
|
||
export class Metamask extends BaseDotsamaWallet { | ||
protected _snapId: string | ||
protected _httpRpcUrl: string | ||
protected _snapApi: MetamaskSnapApi | undefined | ||
protected _accounts: WalletAccount[] | undefined | ||
protected _txId = 0 | ||
|
||
constructor(snapId: string, httpRpcUrl: string) { | ||
super({ | ||
extensionName: 'Metamask', | ||
title: 'Metamask', | ||
logo: { src: MetamaskLogo, alt: 'Metamask Logo' }, | ||
}) | ||
|
||
this._snapId = snapId | ||
this._httpRpcUrl = httpRpcUrl | ||
} | ||
|
||
public enable = async (): Promise<void> => { | ||
const snap = await enablePolkadotSnap( | ||
{ networkName, wsRpcUrl: this._httpRpcUrl, addressPrefix, unit }, | ||
this._snapId | ||
) | ||
|
||
this._snapApi = await snap.getMetamaskSnapApi() | ||
const address = await this._snapApi.getAddress() | ||
this._accounts = [ | ||
{ | ||
name: 'Metamask account', | ||
address, | ||
source: this.extensionName, | ||
}, | ||
] | ||
|
||
this._snapApi.signPayloadJSON | ||
} | ||
|
||
public getAccounts = async (): Promise<WalletAccount[]> => { | ||
return this._accounts ?? [] | ||
} | ||
|
||
public subscribeAccounts: (callback: SubscriptionFn) => Promise<() => void> = (callback) => { | ||
callback(this._accounts ?? []) | ||
return Promise.resolve(() => undefined) | ||
} | ||
|
||
public get signer(): Signer { | ||
return { | ||
signPayload: async (payload) => { | ||
if (!this._snapApi) { | ||
throw Error('Metamask was accessed before it was enabled') | ||
} | ||
|
||
const signature = (await this._snapApi.signPayloadJSON(payload)) as `0x${string}` | ||
|
||
return { id: this._txId++, signature } | ||
}, | ||
|
||
signRaw: async (raw) => { | ||
if (!this._snapApi) { | ||
throw Error('Metamask was accessed before it was enabled') | ||
} | ||
|
||
const signature = (await this._snapApi.signPayloadRaw(raw)) as `0x${string}` | ||
|
||
return { id: this._txId++, signature } | ||
}, | ||
} | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need those TODOs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I did check !