Skip to content

Commit

Permalink
US-2005 EOA Core Wallet Implementation (#814)
Browse files Browse the repository at this point in the history
* feat: EOA Wallet + adjustment for future wallets' structure

* feat: account for previously created wallets from KMS

* feat: account for old wallets created with KMS

* lint: fix defined but never used

* test: fix the useEnhancedWithGas.test

* test: fix lib validate address test

* fix: spelling of an createWallet error

* refactor: remove KeyManagementSystem from NewMasterKeyScreen

* fix: showing of Deploy Wallet screen.

* feat: add comfirmation to _signedTypeData and signMessage requests

* fix: CreateKeys and RootNav issues

* fix: not unlocking is __DEV__ mode on ios

* feat: show RBTC for EOAWallet + formatting (#815)

* feat: RelayWallet implementation based on EOAWallet

* refactor: remove unnecessary code related to old RIFWallet

* fix: getFeeSymbol isRelayWallet param

* fix: extra consent for _signTypedData

* refactor: reject with err in signMessage, _signTypedData, sendTransaction

* fix: correct estimation on fees

* Bump version of Relay SDK that removes gas correction.

* fix: getFormattedTokenValue function return value

* feat: EOA Wallet + adjustment for future wallets' structure

* feat: add USE_RELAY var into .env

* feat: create easy switch between Relay and EOA Wallet

* lint: format lib import

* refactor: cleanup old wallet import/create code

* feat: save chainId, use it to generate new private key if it changes

* refactor: remove smartWalletAddress from EOAWallet

* fix: txRequest payload error

* fix: RBTC fee value being incorrect

* fix: RBTC fee displayed incorrectly

* fix: request.payload incorrectly assumed as an array

* feat: formatLongAssNumbers util

* fix: showing RBTC fee on SendScreen + small amounts formatting

* fix: remove ass from the longNumber

* fix: failing tests + remove unused components

* fix: yarn.lock being unresolved

* refactor: make 0.0000001 a const value

* fix: increase back package versions

* refactor: estimateGas function to exclude circular call smartWallet

* chore: update to latest version of rif-relay-light-sdk

* chore: relay beta 3 update

* Add RIF Faucet as a known address in testnet. (#874)

* Bump sdk to 1.1.1 to fix issue with RDOC estimations.

* refactor: remove rif-wallet-core and its types

* fix: token values formatting

---------

Co-authored-by: Jesse Clark <[email protected]>
  • Loading branch information
TravellerOnTheRun and jessgusclark authored Feb 2, 2024
1 parent be4f18e commit 2caeb8f
Show file tree
Hide file tree
Showing 62 changed files with 1,252 additions and 743 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ DEFAULT_CHAIN_TYPE=TESTNET

RIF_WALLET_KEY=RIF_WALLET
WALLETCONNECT2_PROJECT_ID=d9224e919473fd749ba8298879ce7569

USE_RELAY=false
14 changes: 14 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare module 'react-native-config' {
export interface NativeConfig {
APP_ENV: string
RIF_WALLET_SERVICE_URL: string
RIF_WALLET_SERVICE_PUBLIC_KEY: string
DEFAULT_CHAIN_TYPE: string
RIF_WALLET_KEY: string
WALLETCONNECT2_PROJECT_ID: string
USE_RELAY: string
}

export const Config: NativeConfig
export default Config
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@
"@react-navigation/stack": "^6.0.9",
"@reduxjs/toolkit": "^1.9.0",
"@rsksmart/rif-id-mnemonic": "^0.1.1",
"@rsksmart/rif-relay-light-sdk": "^1.0.17",
"@rsksmart/rif-relay-light-sdk": "1.1.1",
"@rsksmart/rif-wallet-abi-enhancer": "^1.0.10",
"@rsksmart/rif-wallet-adapters": "^1.0.4",
"@rsksmart/rif-wallet-bitcoin": "^1.2.3",
"@rsksmart/rif-wallet-core": "^1.0.5",
"@rsksmart/rif-wallet-eip681": "1.0.1",
"@rsksmart/rif-wallet-services": "^1.2.2",
"@rsksmart/rif-wallet-token": "^1.0.2",
Expand Down
72 changes: 38 additions & 34 deletions src/components/accounts/AccountBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { CheckIcon } from '../icons/CheckIcon'

interface AccountBoxProps {
address: string
smartWalletAddress: string
smartWalletAddress: string | null
chainId: ChainTypesByIdType
walletIsDeployed: WalletIsDeployed
publicKeys: PublicKeyItemType[]
Expand All @@ -54,10 +54,8 @@ export const AccountBox = ({
useState<boolean>(false)

const eoaAddressObject = getAddressDisplayText(address ?? '', chainId)
const smartWalletAddressObject = getAddressDisplayText(
smartWalletAddress ?? '',
chainId,
)
const smartWalletAddressObject =
smartWalletAddress && getAddressDisplayText(smartWalletAddress, chainId)
const onEdit = () => setShowAccountInput(true)

const onChangeAccountName = (text: string) => {
Expand Down Expand Up @@ -93,7 +91,7 @@ export const AccountBox = ({
</Typography>
<AppTouchable width={110} onPress={onEdit}>
<Typography type={'h4'} style={styles.accountEditButton}>
{t('settings_screen_edit_name_label')}
{t('accounts_screen_edit_name_label')}
</Typography>
</AppTouchable>
</View>
Expand All @@ -118,12 +116,12 @@ export const AccountBox = ({
)}
</View>
<View style={styles.statusContainer}>
<Typography type={'h4'}>{t('settings_screen_status_label')}</Typography>
<Typography type={'h4'}>{t('accounts_screen_status_label')}</Typography>
<View style={styles.status}>
<Typography type={'h4'} style={styles.statusText}>
{walletIsDeployed.isDeployed
? t('settings_screen_deployed_label')
: t('settings_screen_not_deployed_label')}
? t('accounts_screen_deployed_label')
: t('accounts_screen_not_deployed_label')}
</Typography>
{walletIsDeployed.isDeployed ? (
<Icon
Expand All @@ -142,8 +140,12 @@ export const AccountBox = ({
</View>
<Input
style={sharedStyles.marginTop20}
label={t('settings_screen_eoa_account_label')}
inputName="EOA Address"
label={
smartWalletAddressObject
? t('accounts_screen_eoa_account_label')
: t('accounts_screen_address_label')
}
inputName={'EOA Address'}
rightIcon={
<Icon
name={'copy'}
Expand All @@ -159,37 +161,39 @@ export const AccountBox = ({
isReadOnly
testID={'TestID.eoaAddress'}
/>
<Input
style={sharedStyles.marginTop20}
label={t('settings_screen_smart_wallet_address_label')}
inputName="Smart Wallet Address"
rightIcon={
<Icon
name={'copy'}
style={styles.copyIcon}
color={sharedColors.white}
size={defaultIconSize}
onPress={() =>
Clipboard.setString(
smartWalletAddressObject.checksumAddress || '',
)
}
/>
}
placeholder={smartWalletAddressObject.displayAddress}
isReadOnly
testID={'TestID.smartWalletAddress'}
/>
{smartWalletAddressObject && (
<Input
style={sharedStyles.marginTop20}
label={t('accounts_screen_smart_wallet_address_label')}
inputName="Smart Wallet Address"
rightIcon={
<Icon
name={'copy'}
style={styles.copyIcon}
color={sharedColors.white}
size={defaultIconSize}
onPress={() =>
Clipboard.setString(
smartWalletAddressObject.checksumAddress || '',
)
}
/>
}
placeholder={smartWalletAddressObject.displayAddress}
isReadOnly
testID={'TestID.smartWalletAddress'}
/>
)}

{publicKeys.map(publicKey => (
<Input
key={publicKey.publicKey}
style={sharedStyles.marginTop20}
label={`${publicKey.networkName} ${t(
'settings_screen_public_key_label',
'accounts_screen_public_key_label',
)}`}
inputName={`${publicKey.networkName} ${t(
'settings_screen_public_key_label',
'accounts_screen_public_key_label',
)}`}
rightIcon={
<Icon
Expand Down
8 changes: 6 additions & 2 deletions src/components/address/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ describe('validate address', () => {
smartWalletAddress: testnetCase.lower,
}
test('same address', () =>
expect(isMyAddress(wallet, testnetCase.checksummed)).toBeTruthy())
expect(
isMyAddress(wallet.smartWalletAddress, testnetCase.checksummed),
).toBeTruthy())
test('different address', () =>
expect(isMyAddress(wallet, '0x1234567890')).toBeFalsy())
expect(
isMyAddress(wallet.smartWalletAddress, '0x1234567890'),
).toBeFalsy())
})
})
12 changes: 4 additions & 8 deletions src/components/address/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,11 @@ export const validateAddress = (
}

export const isMyAddress = (
wallet: { smartWalletAddress: string } | null,
address: string,
userAddress: string,
compareAddress: string,
): boolean => {
if (wallet) {
const myAddress = toChecksumAddress(wallet.smartWalletAddress)
return myAddress.toLowerCase() === address?.toLowerCase()
}

return false
const myAddress = toChecksumAddress(userAddress)
return myAddress.toLowerCase() === compareAddress.toLowerCase()
}

export const getAddressDisplayText = (
Expand Down
8 changes: 6 additions & 2 deletions src/components/loading/LoadingScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { sharedColors, sharedStyles } from 'shared/constants'
import { castStyle } from 'shared/utils'
import { AppSpinner } from 'components/index'

export const LoadingScreen = () => {
interface Props {
isVisible: boolean
}

export const LoadingScreen = ({ isVisible }: Props) => {
return (
<Modal animationType="none" transparent visible>
<Modal animationType="none" transparent visible={isVisible}>
<View
style={[
sharedStyles.flex,
Expand Down
12 changes: 9 additions & 3 deletions src/components/token/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ContactCard } from 'screens/contacts/components'
import { TokenImage, TokenSymbol } from 'screens/home/TokenImage'
import { noop, sharedColors, sharedStyles, testIDs } from 'shared/constants'
import { ContactWithAddressRequired } from 'shared/types'
import { castStyle } from 'shared/utils'
import { castStyle, formatTokenValues } from 'shared/utils'

import { DollarIcon } from '../icons/DollarIcon'
import { EyeIcon } from '../icons/EyeIcon'
Expand Down Expand Up @@ -102,7 +102,11 @@ export const TokenBalance = ({
)}
<TextInput
onChangeText={handleAmountChange}
value={hide ? '\u002A\u002A\u002A\u002A' : firstValue.balance}
value={
hide
? '\u002A\u002A\u002A\u002A'
: formatTokenValues(firstValue.balance)
}
keyboardType="numeric"
accessibilityLabel={'Amount.Input'}
placeholder="0"
Expand Down Expand Up @@ -133,7 +137,9 @@ export const TokenBalance = ({
<Typography type="body1" style={styles.subTitle}>
{hide
? '\u002A\u002A\u002A\u002A\u002A\u002A'
: secondValue?.balance}
: secondValue
? formatTokenValues(secondValue.balance)
: ''}
</Typography>
)}
{error && (
Expand Down
27 changes: 10 additions & 17 deletions src/core/Core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import {
RootTabsParamsList,
} from 'navigation/rootNavigator'
import { RequestHandler } from 'src/ux/requestsModal/RequestHandler'
import { LoadingScreen } from 'components/loading/LoadingScreen'
import { useAppDispatch, useAppSelector } from 'store/storeUtils'
import {
closeRequest,
selectRequests,
selectTopColor,
selectWholeSettingsState,
unlockApp,
} from 'store/slices/settingsSlice'
import { sharedStyles } from 'shared/constants'
Expand All @@ -33,11 +31,10 @@ export const navigationContainerRef =

export const Core = () => {
const dispatch = useAppDispatch()
const settings = useAppSelector(selectWholeSettingsState)
const requests = useAppSelector(selectRequests)
const topColor = useAppSelector(selectTopColor)
const isOffline = useIsOffline()
const { unlocked, active } = useStateSubscription()
const { active } = useStateSubscription()
const { wallet, initializeWallet } = useContext(WalletContext)

const unlockAppFn = useCallback(async () => {
Expand All @@ -59,19 +56,15 @@ export const Core = () => {
{!active && <Cover />}
<NavigationContainer ref={navigationContainerRef}>
<WalletConnect2Provider wallet={wallet}>
{settings.loading && !unlocked ? (
<LoadingScreen />
) : (
<>
<RootNavigationComponent />
{requests.length !== 0 && (
<RequestHandler
request={requests[0]}
closeRequest={() => dispatch(closeRequest())}
/>
)}
</>
)}
<>
<RootNavigationComponent />
{requests.length !== 0 && (
<RequestHandler
request={requests[0]}
closeRequest={() => dispatch(closeRequest())}
/>
)}
</>
</WalletConnect2Provider>
</NavigationContainer>
</View>
Expand Down
21 changes: 16 additions & 5 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import testnetContracts from '@rsksmart/rsk-testnet-contract-metadata'
import mainnetContracts from '@rsksmart/rsk-contract-metadata'
import config from 'config.json'
import ReactNativeConfig from 'react-native-config'
import { constants } from 'ethers'

import { ChainTypeEnum } from 'shared/constants/chainConstants'
import {
ChainTypeEnum,
ChainTypesByIdType,
} from 'shared/constants/chainConstants'
import { SETTINGS } from 'core/types'
import { TokenSymbol } from 'screens/home/TokenImage'

/**
* This function will get the environment settings from the config.json
Expand Down Expand Up @@ -33,18 +38,24 @@ export const getWalletSetting = (
*/
export const getEnvSetting = (setting: SETTINGS) => ReactNativeConfig[setting]

export const getTokenAddress = (symbol: string, chainType: ChainTypeEnum) => {
const contracts =
chainType === ChainTypeEnum.TESTNET ? testnetContracts : mainnetContracts
export const getTokenAddress = (
symbol: TokenSymbol,
chainId: ChainTypesByIdType,
) => {
const contracts = chainId === 31 ? testnetContracts : mainnetContracts

const result = Object.keys(contracts).find(
(address: string) =>
contracts[address].symbol.toUpperCase() === symbol.toUpperCase(),
)

if (!result) {
if (symbol === TokenSymbol.TRBTC || symbol === TokenSymbol.RBTC) {
return constants.AddressZero.toLowerCase()
}

throw new Error(
`Token with the symbol ${symbol} not found on ${chainType}. Did you forget a t?`,
`Token with the symbol ${symbol} not found on ${chainId}. Did you forget a t?`,
)
}
return result.toLowerCase()
Expand Down
Loading

0 comments on commit 2caeb8f

Please sign in to comment.