Skip to content

Commit d4d359e

Browse files
committed
Add the Metamask wallet
1 parent c9c50fb commit d4d359e

File tree

7 files changed

+99
-4
lines changed

7 files changed

+99
-4
lines changed

packages/atlas/atlas.config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ features:
276276
name: Atlas
277277
description: Web3 video streaming platform
278278
icons: ['https://dev.gleev.xyz/favicon.ico']
279+
metamask:
280+
snapId: '$VITE_METAMASK_SNAP_ID' # Snap ID for Metamask - npm:@chainsafe/polkadot-snap or (`local:http://localhost:8081` for local development)
279281
playback:
280282
playbackRates: [2, 1.5, 1.25, 1, 0.5, 0.25] # Playback rates available in the player
281283
comments:

packages/atlas/src/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ VITE_GEOLOCATION_SERVICE_URL=https://geolocation.joystream.org
1818
VITE_HCAPTCHA_SITE_KEY=41cae189-7676-4f6b-aa56-635be26d3ceb
1919
VITE_CHANGENOW_PUBLIC_API_KEY=0d8a58104f82b860a70e9460bccf62ae1e0fca4a93fd7e0c27c90448187b988f
2020
VITE_WALLET_CONNECT_PROJECT_ID=33b2609463e399daee8c51726546c8dd
21+
VITE_METAMASK_SNAP_ID=npm:@chainsafe/polkadot-snap
2122

2223
# YPP configuration
2324
VITE_GOOGLE_CONSOLE_CLIENT_ID=246331758613-rc1psegmsr9l4e33nqu8rre3gno5dsca.apps.googleusercontent.com

packages/atlas/src/config/configSchema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ export const configSchema = z.object({
130130
icons: z.array(z.string()).nullable(),
131131
}),
132132
}),
133+
metamask: z.object({
134+
snapId: z.string().nullable(),
135+
}),
133136
playback: z.object({ playbackRates: z.array(z.number()) }),
134137
comments: z.object({
135138
reactions: z.array(z.object({ id: z.number(), emoji: z.string(), name: z.string() })),

packages/atlas/src/joystream-lib/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const NFT_DEFAULT_EXTENSION_PERIOD = 5
44
export const PERBILL_ONE_PERCENT = 10_000_000
55
export const PERMILL_PER_PERCENT = 10_000
66
export const JOYSTREAM_SS58_PREFIX = 126
7-
export const HAPI_TO_JOY_RATE = 10 ** 10
7+
export const JOY_DECIMALS = 10
8+
export const HAPI_TO_JOY_RATE = 10 ** JOY_DECIMALS
89

910
export const AMM_DESCO_CURVE_CONST = 0.4522
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { enablePolkadotSnap } from '@chainsafe/metamask-polkadot-adapter'
2+
import { MetamaskSnapApi } from '@chainsafe/metamask-polkadot-adapter/build/types'
3+
import { SnapNetworks } from '@chainsafe/metamask-polkadot-types'
4+
import { Signer } from '@polkadot/types/types'
5+
import { BaseDotsamaWallet, SubscriptionFn, WalletAccount } from '@talismn/connect-wallets'
6+
7+
import { atlasConfig } from '@/config'
8+
import { NODE_HTTP_URL } from '@/config/env'
9+
import { JOY_DECIMALS } from '@/joystream-lib/config'
10+
11+
import { JOYSTREAM_SS58_PREFIX } from './consts'
12+
13+
const networkName = 'joystream' as SnapNetworks
14+
const addressPrefix = JOYSTREAM_SS58_PREFIX
15+
const unit = { symbol: atlasConfig.joystream.tokenTicker, decimals: JOY_DECIMALS }
16+
17+
export class MetamaskWallet extends BaseDotsamaWallet {
18+
protected _snapId: string
19+
protected _snapApi: MetamaskSnapApi | undefined
20+
protected _accounts: WalletAccount[] | undefined
21+
protected _txId = 0
22+
23+
public get installed(): boolean {
24+
return '_metamask' in window.ethereum
25+
}
26+
27+
constructor(snapId: string) {
28+
super()
29+
30+
this.extensionName = 'Metamask'
31+
this.title = 'Metamask'
32+
this.logo = { src: 'https://metamask.io/images/metamask-logo.png', alt: 'Metamask Logo' }
33+
34+
this._snapId = snapId
35+
}
36+
37+
public enable = async (): Promise<void> => {
38+
const snap = await enablePolkadotSnap({ networkName, wsRpcUrl: NODE_HTTP_URL, addressPrefix, unit }, this._snapId)
39+
40+
this._snapApi = await snap.getMetamaskSnapApi()
41+
const address = await this._snapApi.getAddress()
42+
this._accounts = [
43+
{
44+
name: 'Metamask account',
45+
address,
46+
source: this.extensionName,
47+
},
48+
]
49+
50+
this._snapApi.signPayloadJSON
51+
}
52+
53+
public getAccounts = async (): Promise<WalletAccount[]> => {
54+
return this._accounts ?? []
55+
}
56+
57+
public subscribeAccounts: (callback: SubscriptionFn) => Promise<() => void> = (callback) => {
58+
callback(this._accounts ?? [])
59+
return Promise.resolve(() => undefined)
60+
}
61+
62+
public get signer(): Signer {
63+
return {
64+
signPayload: async (payload) => {
65+
if (!this._snapApi) {
66+
throw Error('Metamask was accessed before it was enabled')
67+
}
68+
69+
const signature = (await this._snapApi.signPayloadJSON(payload)) as `0x${string}`
70+
71+
return { id: this._txId++, signature }
72+
},
73+
74+
signRaw: async (raw) => {
75+
if (!this._snapApi) {
76+
throw Error('Metamask was accessed before it was enabled')
77+
}
78+
79+
const signature = (await this._snapApi.signPayloadRaw(raw)) as `0x${string}`
80+
81+
return { id: this._txId++, signature }
82+
},
83+
}
84+
}
85+
}

packages/atlas/src/providers/wallet/wallet.helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { BaseDotsamaWallet, Wallet, getWallets } from '@talismn/connect-wallets'
22

33
import { atlasConfig } from '@/config'
44

5+
import { MetamaskWallet } from './tmpwallet/metamask'
6+
57
export const getWalletsList = () => {
68
const supportedWallets = getWallets()
79
const supportedWalletsNames = supportedWallets.map((wallet) => wallet.extensionName)
@@ -26,10 +28,13 @@ export const getWalletsList = () => {
2628
installed: true,
2729
} as Wallet
2830

31+
const metamaskSnapId = atlasConfig.features.metamask.snapId
32+
2933
return [
3034
...supportedWallets,
3135
...unknownWallets,
3236
...(atlasConfig.features.walletConnect.walletConnectProjectId ? [wcWallet] : []),
37+
...(metamaskSnapId ? [new MetamaskWallet(metamaskSnapId)] : []),
3338
]
3439
}
3540

packages/atlas/src/providers/wallet/wallet.provider.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ export const WalletProvider: FC<PropsWithChildren> = ({ children }) => {
9999

100100
await selectedWallet.enable(atlasConfig.general.appName)
101101

102-
// taken from https://github.com/TalismanSociety/talisman-connect/blob/47cfefee9f1333326c0605c159d6ee8ebfba3e84/libs/wallets/src/lib/base-dotsama-wallet/index.ts#L98-L107
103-
// should be part of future talisman-connect release
104-
const accounts = await selectedWallet.extension.accounts.get()
102+
const accounts = await selectedWallet.getAccounts()
105103

106104
const accountsWithWallet = accounts
107105
.filter(filterUnsupportedAccounts)

0 commit comments

Comments
 (0)