Skip to content

Commit

Permalink
feat: phantom wire-up (#7766)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre committed Sep 25, 2024
1 parent 0f84983 commit a6d3a38
Show file tree
Hide file tree
Showing 28 changed files with 373 additions and 133 deletions.
1 change: 1 addition & 0 deletions .env.base
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ REACT_APP_FEATURE_USDT_APPROVAL_RESET=true
REACT_APP_FEATURE_PORTALS_SWAPPER=true
REACT_APP_FEATURE_RUNEPOOL=true
REACT_APP_FEATURE_MARKETS=false
REACT_APP_FEATURE_PHANTOM_WALLET=false

# absolute URL prefix
REACT_APP_ABSOLUTE_URL_PREFIX=https://app.shapeshift.com
Expand Down
1 change: 1 addition & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ REACT_APP_THORCHAIN_NODE_URL=https://dev-daemon.thorchain.shapeshift.com
REACT_APP_MIDGARD_URL=https://dev-indexer.thorchain.shapeshift.com/v2

REACT_APP_FEATURE_MARKETS=true
REACT_APP_FEATURE_PHANTOM_WALLET=true
1 change: 1 addition & 0 deletions .env.develop
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ REACT_APP_THORCHAIN_NODE_URL=https://dev-daemon.thorchain.shapeshift.com
REACT_APP_MIDGARD_URL=https://dev-indexer.thorchain.shapeshift.com/v2

REACT_APP_FEATURE_MARKETS=true
REACT_APP_FEATURE_PHANTOM_WALLET=true
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@
"@shapeshiftoss/caip": "workspace:^",
"@shapeshiftoss/chain-adapters": "workspace:^",
"@shapeshiftoss/errors": "workspace:^",
"@shapeshiftoss/hdwallet-coinbase": "1.55.5",
"@shapeshiftoss/hdwallet-core": "1.55.5",
"@shapeshiftoss/hdwallet-keepkey": "1.55.5",
"@shapeshiftoss/hdwallet-keepkey-webusb": "1.55.5",
"@shapeshiftoss/hdwallet-keplr": "1.55.5",
"@shapeshiftoss/hdwallet-ledger": "1.55.5",
"@shapeshiftoss/hdwallet-ledger-webusb": "1.55.5",
"@shapeshiftoss/hdwallet-metamask": "1.55.5",
"@shapeshiftoss/hdwallet-native": "1.55.5",
"@shapeshiftoss/hdwallet-native-vault": "1.55.5",
"@shapeshiftoss/hdwallet-shapeshift-multichain": "1.55.5",
"@shapeshiftoss/hdwallet-walletconnectv2": "1.55.5",
"@shapeshiftoss/hdwallet-xdefi": "1.55.5",
"@shapeshiftoss/hdwallet-coinbase": "1.55.9",
"@shapeshiftoss/hdwallet-core": "1.55.9",
"@shapeshiftoss/hdwallet-keepkey": "1.55.9",
"@shapeshiftoss/hdwallet-keepkey-webusb": "1.55.9",
"@shapeshiftoss/hdwallet-keplr": "1.55.9",
"@shapeshiftoss/hdwallet-ledger": "1.55.9",
"@shapeshiftoss/hdwallet-ledger-webusb": "1.55.9",
"@shapeshiftoss/hdwallet-metamask": "1.55.9",
"@shapeshiftoss/hdwallet-native": "1.55.9",
"@shapeshiftoss/hdwallet-native-vault": "1.55.9",
"@shapeshiftoss/hdwallet-phantom": "1.55.9",
"@shapeshiftoss/hdwallet-shapeshift-multichain": "1.55.9",
"@shapeshiftoss/hdwallet-walletconnectv2": "1.55.9",
"@shapeshiftoss/hdwallet-xdefi": "1.55.9",
"@shapeshiftoss/swapper": "workspace:^",
"@shapeshiftoss/types": "workspace:^",
"@shapeshiftoss/unchained-client": "workspace:^",
Expand Down
6 changes: 3 additions & 3 deletions packages/chain-adapters/src/utxo/UtxoBaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,12 @@ export abstract class UtxoBaseAdapter<T extends UtxoChainId> implements IChainAd

const signTxInputs: BTCSignTxInput[] = []
for (const input of inputs) {
if (!input.path) continue

const data = await this.providers.http.getTransaction({ txid: input.txid })

signTxInputs.push({
addressNList: bip32ToAddressNList(input.path),
addressNList: input.path
? bip32ToAddressNList(input.path)
: toAddressNList({ ...bip44Params }),
scriptType: accountTypeToScriptType[accountType],
amount: String(input.value),
vout: input.vout,
Expand Down
18 changes: 12 additions & 6 deletions src/assets/translations/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,6 @@
"button": "Pair"
},
"failure": {
"header": "Error",
"body": "Unable to connect Keplr wallet"
}
},
Expand Down Expand Up @@ -1506,7 +1505,6 @@
"pairExistingDeviceBody": "Connecting a different Ledger device? Click 'Pair new device' to start fresh."
},
"failure": {
"header": "Error",
"body": "Unable to connect Ledger wallet"
},
"connectWarning": "Before connecting a chain, make sure you have the app open on your device.",
Expand All @@ -1524,7 +1522,6 @@
"button": "Pair"
},
"failure": {
"header": "Error",
"body": "Unable to connect MetaMask wallet"
},
"redirect": {
Expand All @@ -1533,6 +1530,18 @@
"button": "Open"
}
},
"phantom": {
"errors": {
"unknown": "An unexpected error occurred communicating with Phantom",
"connectFailure": "Unable to connect Phantom wallet",
"multipleWallets": "Detected Ethereum provider is not Phantom. Do you have multiple wallets installed?"
},
"connect": {
"header": "Pair Phantom",
"body": "Click Pair and login to Phantom from the popup window",
"button": "Pair"
}
},
"metaMaskSnap": {
"title": "Multichain support is now available for MetaMask!",
"subtitle": "Add the Multichain Snap on Metamask to send, receive, track, trade, and earn with the following chains:",
Expand Down Expand Up @@ -1586,7 +1595,6 @@
"button": "Pair"
},
"failure": {
"header": "Unable to connect Coinbase wallet",
"body": "Unable to connect Coinbase wallet"
}
},
Expand All @@ -1601,7 +1609,6 @@
"button": "Pair"
},
"failure": {
"header": "Error",
"body": "Unable to connect WalletConnect wallet"
}
},
Expand All @@ -1617,7 +1624,6 @@
"button": "Pair"
},
"failure": {
"header": "Error",
"body": "Unable to connect XDEFI wallet"
},
"redirect": {
Expand Down
20 changes: 20 additions & 0 deletions src/components/Icons/PhantomIcon.tsx
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',
})
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ const validators = {
REACT_APP_FEATURE_MARKETS: bool({ default: false }),
REACT_APP_PORTALS_BASE_URL: url(),
REACT_APP_ZERION_BASE_URL: url(),
REACT_APP_FEATURE_PHANTOM_WALLET: bool({ default: false }),
}

function reporter<T>({ errors }: envalid.ReporterOptions<T>) {
Expand Down
2 changes: 1 addition & 1 deletion src/context/WalletProvider/Coinbase/components/Failure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FailureModal } from 'context/WalletProvider/components/FailureModal'
export const CoinbaseFailure = () => {
return (
<FailureModal
headerText={'walletProvider.coinbase.failure.header'}
headerText={'common.error'}
bodyText={'walletProvider.coinbase.failure.body'}
></FailureModal>
)
Expand Down
2 changes: 1 addition & 1 deletion src/context/WalletProvider/Keplr/components/Failure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FailureModal } from 'context/WalletProvider/components/FailureModal'
export const KeplrFailure = () => {
return (
<FailureModal
headerText={'walletProvider.keplr.failure.header'}
headerText={'common.error'}
bodyText={'walletProvider.keplr.failure.body'}
></FailureModal>
)
Expand Down
1 change: 1 addition & 0 deletions src/context/WalletProvider/KeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum KeyManager {
Native = 'native',
KeepKey = 'keepkey',
MetaMask = 'metamask',
Phantom = 'phantom',
Demo = 'demo',
XDefi = 'xdefi',
Keplr = 'keplr',
Expand Down
2 changes: 1 addition & 1 deletion src/context/WalletProvider/Ledger/components/Failure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FailureModal } from 'context/WalletProvider/components/FailureModal'
export const LedgerFailure = () => {
return (
<FailureModal
headerText={'walletProvider.ledger.failure.header'}
headerText={'common.error'}
bodyText={'walletProvider.ledger.failure.body'}
></FailureModal>
)
Expand Down
5 changes: 1 addition & 4 deletions src/context/WalletProvider/MetaMask/components/Failure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { FailureModal } from 'context/WalletProvider/components/FailureModal'

export const MetaMaskFailure = () => {
return (
<FailureModal
headerText={'walletProvider.metaMask.failure.header'}
bodyText={'walletProvider.metaMask.failure.body'}
/>
<FailureModal headerText={'common.error'} bodyText={'walletProvider.metaMask.failure.body'} />
)
}
83 changes: 83 additions & 0 deletions src/context/WalletProvider/Phantom/components/Connect.tsx
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}
/>
)
}
10 changes: 10 additions & 0 deletions src/context/WalletProvider/Phantom/components/Failure.tsx
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={'common.error'}
bodyText={'walletProvider.phantom.errors.connectFailure'}
/>
)
}
16 changes: 16 additions & 0 deletions src/context/WalletProvider/Phantom/config.ts
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',
}
3 changes: 3 additions & 0 deletions src/context/WalletProvider/SelectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const WalletSelectItem = ({
const isCoinbaseEnabled = getConfig().REACT_APP_FEATURE_COINBASE_WALLET
if (walletType === KeyManager.Coinbase && !isCoinbaseEnabled) return null

const isPhantomEnabled = getConfig().REACT_APP_FEATURE_PHANTOM_WALLET
if (walletType === KeyManager.Phantom && !isPhantomEnabled) return null

const isWalletConnectV2Enabled = getConfig().REACT_APP_FEATURE_WALLET_CONNECT_V2
if (walletType === KeyManager.WalletConnectV2 && !isWalletConnectV2Enabled) return null

Expand Down
Loading

0 comments on commit a6d3a38

Please sign in to comment.