|
| 1 | +import { ConnectorUpdate } from '@web3-react/types' |
| 2 | +import { AbstractConnector } from '@web3-react/abstract-connector' |
| 3 | +import invariant from 'tiny-invariant' |
| 4 | + |
| 5 | +type NetworkName = 'mainnet' | 'ropsten' | 'rinkeby' | 'kovan' |
| 6 | + |
| 7 | +const chainIdToNetwork: { [network: number]: NetworkName } = { |
| 8 | + 1: 'mainnet', |
| 9 | + 3: 'ropsten', |
| 10 | + 4: 'rinkeby', |
| 11 | + 42: 'kovan' |
| 12 | +} |
| 13 | + |
| 14 | +interface MagicConnectorArguments { |
| 15 | + apiKey: string |
| 16 | + chainId: number |
| 17 | + email: string |
| 18 | +} |
| 19 | + |
| 20 | +export class UserRejectedRequestError extends Error { |
| 21 | + public constructor() { |
| 22 | + super() |
| 23 | + this.name = this.constructor.name |
| 24 | + this.message = 'The user rejected the request.' |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export class FailedVerificationError extends Error { |
| 29 | + public constructor() { |
| 30 | + super() |
| 31 | + this.name = this.constructor.name |
| 32 | + this.message = 'The email verification failed.' |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export class MagicLinkRateLimitError extends Error { |
| 37 | + public constructor() { |
| 38 | + super() |
| 39 | + this.name = this.constructor.name |
| 40 | + this.message = 'The Magic rate limit has been reached.' |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export class MagicLinkExpiredError extends Error { |
| 45 | + public constructor() { |
| 46 | + super() |
| 47 | + this.name = this.constructor.name |
| 48 | + this.message = 'The Magic link has expired.' |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export class MagicConnector extends AbstractConnector { |
| 53 | + private readonly apiKey: string |
| 54 | + private readonly chainId: number |
| 55 | + private readonly email: string |
| 56 | + |
| 57 | + public magic: any |
| 58 | + |
| 59 | + constructor({ apiKey, chainId, email }: MagicConnectorArguments) { |
| 60 | + invariant(Object.keys(chainIdToNetwork).includes(chainId.toString()), `Unsupported chainId ${chainId}`) |
| 61 | + invariant(email && email.includes('@'), `Invalid email: ${email}`) |
| 62 | + super({ supportedChainIds: [chainId] }) |
| 63 | + |
| 64 | + this.apiKey = apiKey |
| 65 | + this.chainId = chainId |
| 66 | + this.email = email |
| 67 | + } |
| 68 | + |
| 69 | + public async activate(): Promise<ConnectorUpdate> { |
| 70 | + const MagicSDK = await import('magic-sdk').then(m => m?.default ?? m) |
| 71 | + const { Magic, RPCError, RPCErrorCode } = MagicSDK |
| 72 | + |
| 73 | + if (!this.magic) { |
| 74 | + this.magic = new Magic(this.apiKey, { network: chainIdToNetwork[this.chainId] }) |
| 75 | + } |
| 76 | + |
| 77 | + const isLoggedIn = await this.magic.user.isLoggedIn() |
| 78 | + const loggedInEmail = isLoggedIn ? (await this.magic.user.getMetadata()).email : null |
| 79 | + |
| 80 | + if (isLoggedIn && loggedInEmail !== this.email) { |
| 81 | + await this.magic.user.logout() |
| 82 | + } |
| 83 | + |
| 84 | + if (!isLoggedIn) { |
| 85 | + try { |
| 86 | + await this.magic.auth.loginWithMagicLink({ email: this.email }) |
| 87 | + } catch (err) { |
| 88 | + if (!(err instanceof RPCError)) { |
| 89 | + throw err |
| 90 | + } |
| 91 | + if (err.code === RPCErrorCode.MagicLinkFailedVerification) { |
| 92 | + throw new FailedVerificationError() |
| 93 | + } |
| 94 | + if (err.code === RPCErrorCode.MagicLinkExpired) { |
| 95 | + throw new MagicLinkExpiredError() |
| 96 | + } |
| 97 | + if (err.code === RPCErrorCode.MagicLinkRateLimited) { |
| 98 | + throw new MagicLinkRateLimitError() |
| 99 | + } |
| 100 | + // This error gets thrown when users close the login window. |
| 101 | + // -32603 = JSON-RPC InternalError |
| 102 | + if (err.code === -32603) { |
| 103 | + throw new UserRejectedRequestError() |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + const provider = this.magic.rpcProvider |
| 109 | + const account = await provider.enable().then((accounts: string[]): string => accounts[0]) |
| 110 | + |
| 111 | + return { provider, chainId: this.chainId, account } |
| 112 | + } |
| 113 | + |
| 114 | + public async getProvider(): Promise<any> { |
| 115 | + return this.magic.rpcProvider |
| 116 | + } |
| 117 | + |
| 118 | + public async getChainId(): Promise<number | string> { |
| 119 | + return this.chainId |
| 120 | + } |
| 121 | + |
| 122 | + public async getAccount(): Promise<null | string> { |
| 123 | + return this.magic.rpcProvider.send('eth_accounts').then((accounts: string[]): string => accounts[0]) |
| 124 | + } |
| 125 | + |
| 126 | + public deactivate() {} |
| 127 | + |
| 128 | + public async close() { |
| 129 | + await this.magic.user.logout() |
| 130 | + this.emitDeactivate() |
| 131 | + } |
| 132 | +} |
0 commit comments