-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90454aa
commit b9cc0c3
Showing
15 changed files
with
343 additions
and
94 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
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,20 @@ | ||
import { createIcon } from '@chakra-ui/react' | ||
|
||
export const PhantomIcon = createIcon({ | ||
displayName: 'PhantomIcon', | ||
path: ( | ||
<svg | ||
width='593' | ||
height='493' | ||
viewBox='0 0 593 493' | ||
fill='none' | ||
xmlns='http://www.w3.org/2000/svg' | ||
> | ||
<path | ||
d='M70.0546 493C145.604 493 202.38 427.297 236.263 375.378C232.142 386.865 229.852 398.351 229.852 409.378C229.852 439.703 247.252 461.297 281.592 461.297C328.753 461.297 379.119 419.946 405.218 375.378C403.386 381.811 402.471 387.784 402.471 393.297C402.471 414.432 414.375 427.757 438.643 427.757C515.108 427.757 592.03 292.216 592.03 173.676C592.03 81.3243 545.327 0 428.112 0C222.069 0 0 251.784 0 414.432C0 478.297 34.3405 493 70.0546 493ZM357.141 163.568C357.141 140.595 369.962 124.514 388.734 124.514C407.049 124.514 419.87 140.595 419.87 163.568C419.87 186.541 407.049 203.081 388.734 203.081C369.962 203.081 357.141 186.541 357.141 163.568ZM455.126 163.568C455.126 140.595 467.947 124.514 486.719 124.514C505.034 124.514 517.855 140.595 517.855 163.568C517.855 186.541 505.034 203.081 486.719 203.081C467.947 203.081 455.126 186.541 455.126 163.568Z' | ||
fill='#AB9FF2' | ||
/> | ||
</svg> | ||
), | ||
viewBox: '0 0 593 493', | ||
}) |
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 React, { useCallback, useState } from 'react' | ||
import type { RouteComponentProps } from 'react-router-dom' | ||
import type { ActionTypes } from 'context/WalletProvider/actions' | ||
import { WalletActions } from 'context/WalletProvider/actions' | ||
import { KeyManager } from 'context/WalletProvider/KeyManager' | ||
import { useLocalWallet } from 'context/WalletProvider/local-wallet' | ||
import { removeAccountsAndChainListeners } from 'context/WalletProvider/WalletProvider' | ||
import { useWallet } from 'hooks/useWallet/useWallet' | ||
|
||
import { ConnectModal } from '../../components/ConnectModal' | ||
import type { LocationState } from '../../NativeWallet/types' | ||
import { PhantomConfig } from '../config' | ||
|
||
export interface PhantomSetupProps | ||
extends RouteComponentProps< | ||
{}, | ||
any, // history | ||
LocationState | ||
> { | ||
dispatch: React.Dispatch<ActionTypes> | ||
} | ||
|
||
export const PhantomConnect = ({ history }: PhantomSetupProps) => { | ||
const { dispatch, getAdapter, onProviderChange } = useWallet() | ||
const localWallet = useLocalWallet() | ||
const [loading, setLoading] = useState(false) | ||
const [error, setError] = useState<string | null>(null) | ||
|
||
const setErrorLoading = useCallback((e: string | null) => { | ||
setError(e) | ||
setLoading(false) | ||
}, []) | ||
|
||
const pairDevice = useCallback(async () => { | ||
setError(null) | ||
setLoading(true) | ||
|
||
const adapter = await getAdapter(KeyManager.Phantom) | ||
if (adapter) { | ||
try { | ||
// Remove all provider event listeners from previously connected wallets | ||
await removeAccountsAndChainListeners() | ||
|
||
const wallet = await adapter.pairDevice() | ||
if (!wallet) { | ||
setErrorLoading('walletProvider.errors.walletNotFound') | ||
throw new Error('Call to hdwallet-phantom::pairDevice returned null or undefined') | ||
} | ||
|
||
await onProviderChange(KeyManager.Phantom, wallet) | ||
|
||
const { name, icon } = PhantomConfig | ||
const deviceId = await wallet.getDeviceID() | ||
const isLocked = await wallet.isLocked() | ||
await wallet.initialize() | ||
dispatch({ | ||
type: WalletActions.SET_WALLET, | ||
payload: { wallet, name, icon, deviceId, connectedType: KeyManager.Phantom }, | ||
}) | ||
dispatch({ type: WalletActions.SET_IS_CONNECTED, payload: true }) | ||
dispatch({ type: WalletActions.SET_IS_LOCKED, payload: isLocked }) | ||
localWallet.setLocalWalletTypeAndDeviceId(KeyManager.Phantom, deviceId) | ||
dispatch({ type: WalletActions.SET_WALLET_MODAL, payload: false }) | ||
} catch (e: any) { | ||
console.error(e, 'Phantom Connect: There was an error initializing the wallet') | ||
setErrorLoading(e.message) | ||
history.push('/phantom/failure') | ||
} | ||
} | ||
setLoading(false) | ||
}, [dispatch, getAdapter, history, localWallet, onProviderChange, setErrorLoading]) | ||
|
||
return ( | ||
<ConnectModal | ||
headerText={'walletProvider.phantom.connect.header'} | ||
bodyText={'walletProvider.phantom.connect.body'} | ||
buttonText={'walletProvider.phantom.connect.button'} | ||
onPairDeviceClick={pairDevice} | ||
loading={loading} | ||
error={error} | ||
/> | ||
) | ||
} |
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,10 @@ | ||
import { FailureModal } from 'context/WalletProvider/components/FailureModal' | ||
|
||
export const PhantomFailure = () => { | ||
return ( | ||
<FailureModal | ||
headerText={'walletProvider.metaMask.failure.header'} | ||
bodyText={'walletProvider.metaMask.failure.body'} | ||
/> | ||
) | ||
} |
21 changes: 21 additions & 0 deletions
21
src/context/WalletProvider/Phantom/components/SnapInstall.tsx
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,21 @@ | ||
import { useCallback } from 'react' | ||
import { SnapContent } from 'components/Modals/Snaps/SnapContent' | ||
import { WalletActions } from 'context/WalletProvider/actions' | ||
import { useWallet } from 'hooks/useWallet/useWallet' | ||
|
||
export const SnapInstall = () => { | ||
const { dispatch } = useWallet() | ||
|
||
const handleClose = useCallback(() => { | ||
dispatch({ type: WalletActions.SET_WALLET_MODAL, payload: false }) | ||
}, [dispatch]) | ||
|
||
return ( | ||
<SnapContent | ||
onClose={handleClose} | ||
// If we land here, we don't care about versioning, the user does *not* have the snap installed yet | ||
isCorrectVersion | ||
isSnapInstalled={false} | ||
/> | ||
) | ||
} |
17 changes: 17 additions & 0 deletions
17
src/context/WalletProvider/Phantom/components/SnapUpdate.tsx
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,17 @@ | ||
import { useCallback } from 'react' | ||
import { SnapContent } from 'components/Modals/Snaps/SnapContent' | ||
import { WalletActions } from 'context/WalletProvider/actions' | ||
import { useWallet } from 'hooks/useWallet/useWallet' | ||
|
||
export const SnapUpdate = () => { | ||
const { dispatch } = useWallet() | ||
|
||
const handleClose = useCallback(() => { | ||
dispatch({ type: WalletActions.SET_WALLET_MODAL, payload: false }) | ||
}, [dispatch]) | ||
|
||
return ( | ||
// If we land here, we know the version is incorrect | ||
<SnapContent onClose={handleClose} isCorrectVersion={false} isSnapInstalled={true} /> | ||
) | ||
} |
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,16 @@ | ||
import type { PhantomAdapter } from '@shapeshiftoss/hdwallet-phantom' | ||
import { PhantomIcon } from 'components/Icons/PhantomIcon' | ||
import type { SupportedWalletInfo } from 'context/WalletProvider/config' | ||
|
||
type PhantomConfigType = Omit<SupportedWalletInfo<typeof PhantomAdapter>, 'routes'> | ||
|
||
export const PhantomConfig: PhantomConfigType = { | ||
adapters: [ | ||
{ | ||
loadAdapter: () => import('@shapeshiftoss/hdwallet-phantom').then(m => m.PhantomAdapter), | ||
}, | ||
], | ||
supportsMobile: 'browser', | ||
icon: PhantomIcon, | ||
name: 'Phantom', | ||
} |
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
Oops, something went wrong.