Skip to content

Commit

Permalink
Updated imports to use wallet
Browse files Browse the repository at this point in the history
Closes #298
  • Loading branch information
tombeckenham committed Dec 18, 2024
1 parent 1f174cf commit d895b0e
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ export class WalletController extends BaseController {
findAddressWithPrivateKey = (pk: string, address: string) => {
return findAddressWithPK(pk, address);
};
findAddressWithSeedPhrase = (seed: string, address: string) => {
return findAddressWithSeed(seed, address);
findAddressWithSeedPhrase = (seed: string, address: string, isTemp: boolean = false) => {
return findAddressWithSeed(seed, address, isTemp);
};
getPreMnemonics = () => keyringService.getPreMnemonics();
generatePreMnemonic = () => keyringService.generatePreMnemonic();
Expand Down
4 changes: 2 additions & 2 deletions src/shared/types/algo-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type SignAlgoType = 'ECDSA_P256' | 'ECDSA_secp256k1' | 'unknown';

export type HashAlgoType = 'SHA256' | 'SHA2_256' | 'SHA3_256' | 'SHA2_384' | 'SHA3_384' | 'unknown';
export type KeyType = 'Passkey' | 'GoogleDrive' | 'SeedPhrase' | 'Keystore' | 'PrivateKey';
export type ImportKeyType = 'Passkey' | 'GoogleDrive' | 'SeedPhrase' | 'Keystore' | 'PrivateKey';

export type RecoveryMechanismType = KeyType | 'Multi-Backup' | 'Device-Backup';
export type RecoveryMechanismType = ImportKeyType | 'Multi-Backup' | 'Device-Backup';
15 changes: 10 additions & 5 deletions src/shared/types/tracking-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { HashAlgoType, SignAlgoType, KeyType, RecoveryMechanismType } from './algo-types';
import type {
HashAlgoType,
SignAlgoType,
ImportKeyType,
RecoveryMechanismType,
} from './algo-types';

type OnRampSourceType = 'moonpay' | 'coinbase';

Expand Down Expand Up @@ -42,11 +47,11 @@ export type TrackingEvents = {
// Backup Events
multi_backup_created: {
address: string; // Address of the account that set up multi-backup
providers: KeyType[]; // Providers used in the multi-backup, GoogleDrive, iCloud, Seed e.g. google_drive icloud seed_phrase
providers: ImportKeyType[]; // Providers used in the multi-backup, GoogleDrive, iCloud, Seed e.g. google_drive icloud seed_phrase
};
multi_backup_creation_failed: {
address: string; // Address of the account that set up multi-backup
providers: KeyType[]; // Providers used in the multi-backup, GoogleDrive, iCloud, Seed e.g. google_drive icloud seed_phrase
providers: ImportKeyType[]; // Providers used in the multi-backup, GoogleDrive, iCloud, Seed e.g. google_drive icloud seed_phrase
};

// Transaction Events
Expand Down Expand Up @@ -90,7 +95,7 @@ export type TrackingEvents = {
// Account Events
account_created: {
public_key: string; // The public key used for creating the new account
key_type?: KeyType; // The key type of the account (if available)
key_type?: ImportKeyType; // The key type of the account (if available)
sign_algo: SignAlgoType; // Signature algorithm of the key
hash_algo: HashAlgoType; // Hash algo Hash algorithm of the key
};
Expand All @@ -101,7 +106,7 @@ export type TrackingEvents = {
account_recovered: {
address: string; // Address that was recovered
mechanism: RecoveryMechanismType; // The way the account was recovered
methods: KeyType[]; // Array of providers used in the multi-backup, GoogleDrive, iCloud, Seed
methods: ImportKeyType[]; // Array of providers used in the multi-backup, GoogleDrive, iCloud, Seed
};
};

Expand Down
4 changes: 2 additions & 2 deletions src/shared/utils/algo-constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type SignAlgoType, type HashAlgoType } from '../types/algo-types';
import { type SignAlgoType, type HashAlgoType, type ImportKeyType } from '../types/algo-types';

const FLOW_BIP44_PATH = "m/44'/539'/0'/0/0";

const KEY_TYPE = {
const KEY_TYPE: { [key: string]: ImportKeyType } = {
PASSKEY: 'Passkey',
GOOGLE_DRIVE: 'GoogleDrive',
SEED_PHRASE: 'SeedPhrase',
Expand Down
4 changes: 0 additions & 4 deletions src/ui/utils/modules/findAddressWithPK.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/ui/utils/modules/passkey.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { useEffect, useState, useContext } from 'react';
import { findAddressWithPK } from '../../../../utils/modules/findAddressWithPK';
import { KEY_TYPE } from '../../../../utils/modules/constants';
import React from 'react';
import { Visibility, VisibilityOff } from '@mui/icons-material';
import {
Box,
Button,
Expand All @@ -11,11 +8,14 @@ import {
TextareaAutosize,
InputAdornment,
} from '@mui/material';
import { Visibility, VisibilityOff } from '@mui/icons-material';
import { makeStyles } from '@mui/styles';
import React, { useState } from 'react';

import { KEY_TYPE } from '@/shared/utils/algo-constants';
import { useWallet } from '@/ui/utils/WalletContext';
import { LLSpinner } from 'ui/FRWComponent';

import ErrorModel from '../../../../FRWComponent/PopupModal/errorModel';
import { jsonToKey } from '../../../../utils/modules/passkey';

const useStyles = makeStyles((theme) => ({
form: {
Expand Down Expand Up @@ -61,6 +61,7 @@ const useStyles = makeStyles((theme) => ({

const JsonImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
const classes = useStyles();
const wallet = useWallet();
const [isLoading, setLoading] = useState(false);
const [isInvalid, setIsInvalid] = useState(false);
const [json, setJson] = useState('');
Expand Down Expand Up @@ -91,14 +92,13 @@ const JsonImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
}
const password = e.target[2].value;
const address = e.target[5].value;
const pk = await jsonToKey(keystore, password);
if (pk == null) {
const pk = await wallet.jsonToPrivateKey(keystore, password);
if (pk === null) {
setErrorMessage('Password incorrect');
return;
}
const pkHex = Buffer.from(pk.data()).toString('hex');
const result = await findAddressWithPK(pkHex, address);
console.log(result);
const result = await wallet.findAddressWithPrivateKey(pkHex, address);
setPk(pkHex);
if (!result) {
onOpen();
Expand Down Expand Up @@ -180,7 +180,7 @@ const JsonImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
</Typography>
</Button>
</form>
{errorMesssage != '' && (
{errorMesssage !== '' && (
<ErrorModel
isOpen={errorMesssage !== ''}
onOpenChange={() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, useState, useContext } from 'react';
import { findAddressWithPK } from '../../../../utils/modules/findAddressWithPK';
import { KEY_TYPE } from '../../../../utils/modules/constants';
import React from 'react';
import { Box, Button, Typography, TextField, TextareaAutosize } from '@mui/material';
import { makeStyles } from '@mui/styles';
import React, { useState } from 'react';

import { KEY_TYPE } from '@/shared/utils/algo-constants';
import { useWallet } from '@/ui/utils/WalletContext';
import { LLSpinner } from 'ui/FRWComponent';

const useStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -31,7 +31,7 @@ const useStyles = makeStyles((theme) => ({
const KeyImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
// const classes = useStyles();
const classes = useStyles();

const wallet = useWallet();
const [isLoading, setLoading] = useState(false);

const handleImport = async (e) => {
Expand All @@ -43,7 +43,7 @@ const KeyImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
const inputValue = e.target[2].value;
setPk(pk);
const address = flowAddressRegex.test(inputValue) ? inputValue : null;
const result = await findAddressWithPK(pk, address);
const result = await wallet.findAddressWithPrivateKey(pk, address);
if (!result) {
onOpen();
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useEffect, useState, useContext } from 'react';
import { findAddressWithSeed } from '../../../../utils/modules/findAddressWithPK';
import { KEY_TYPE } from '../../../../utils/modules/constants';
import React from 'react';
import { Box, Button, Typography, TextField, TextareaAutosize } from '@mui/material';
import { makeStyles } from '@mui/styles';
import React, { useState } from 'react';

import { useWallet } from '@/ui/utils/WalletContext';
import { LLSpinner } from 'ui/FRWComponent';

import KeyPathInput from '../../../../FRWComponent/KeyPathInputs';
import { KEY_TYPE } from '../../../../utils/modules/constants';

const useStyles = makeStyles((theme) => ({
form: {
Expand All @@ -32,6 +33,7 @@ const useStyles = makeStyles((theme) => ({

const SeedPhraseImport = ({ onOpen, onImport, setmnemonic, isSignLoading }) => {
const classes = useStyles();
const wallet = useWallet();
const [isLoading, setLoading] = useState(false);

const handleImport = async (e) => {
Expand All @@ -44,7 +46,7 @@ const SeedPhraseImport = ({ onOpen, onImport, setmnemonic, isSignLoading }) => {
const inputValue = e.target[2].value;
const address = flowAddressRegex.test(inputValue) ? inputValue : null;

const result = await findAddressWithSeed(seed, address, true);
const result = await wallet.findAddressWithSeedPhrase(seed, address, true);
if (!result) {
onOpen();
return;
Expand Down
22 changes: 11 additions & 11 deletions src/ui/views/AddressImport/importComponent/JsonImport.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { useEffect, useState, useContext } from 'react';
import { findAddressWithPK } from '../../../utils/modules/findAddressWithPK';
import { KEY_TYPE } from '../../../utils/modules/constants';
import React from 'react';
import { Visibility, VisibilityOff } from '@mui/icons-material';
import {
Box,
Button,
Expand All @@ -11,11 +8,14 @@ import {
TextareaAutosize,
InputAdornment,
} from '@mui/material';
import { Visibility, VisibilityOff } from '@mui/icons-material';
import { makeStyles } from '@mui/styles';
import React, { useState } from 'react';

import { useWallet } from '@/ui/utils/WalletContext';
import { LLSpinner } from 'ui/FRWComponent';
import { jsonToKey } from '../../../utils/modules/passkey';

import ErrorModel from '../../../FRWComponent/PopupModal/errorModel';
import { KEY_TYPE } from '../../../utils/modules/constants';

const useStyles = makeStyles((theme) => ({
form: {
Expand Down Expand Up @@ -61,6 +61,7 @@ const useStyles = makeStyles((theme) => ({

const JsonImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
const classes = useStyles();
const wallet = useWallet();
const [isLoading, setLoading] = useState(false);
const [isInvalid, setIsInvalid] = useState(false);
const [json, setJson] = useState('');
Expand Down Expand Up @@ -91,14 +92,13 @@ const JsonImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
}
const password = e.target[2].value;
const address = e.target[5].value;
const pk = await jsonToKey(keystore, password);
if (pk == null) {
const pk = await wallet.jsonToPrivateKey(keystore, password);
if (pk === null) {
setErrorMessage('Password incorrect');
return;
}
const pkHex = Buffer.from(pk!.data()).toString('hex');
const result = await findAddressWithPK(pkHex, address);
console.log(result);
const result = await wallet.findAddressWithPrivateKey(pkHex, address);
setPk(pkHex);
if (!result) {
onOpen();
Expand Down Expand Up @@ -179,7 +179,7 @@ const JsonImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
</Typography>
</Button>
</form>
{errorMesssage != '' && (
{errorMesssage !== '' && (
<ErrorModel
isOpen={errorMesssage !== ''}
onOpenChange={() => {
Expand Down
12 changes: 7 additions & 5 deletions src/ui/views/AddressImport/importComponent/KeyImport.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useEffect, useState, useContext } from 'react';
import { findAddressWithPK } from '../../../utils/modules/findAddressWithPK';
import { KEY_TYPE } from '../../../utils/modules/constants';
import React from 'react';
import { Box, Button, Typography, TextField, TextareaAutosize } from '@mui/material';
import { makeStyles } from '@mui/styles';
import React, { useState } from 'react';

import { useWallet } from '@/ui/utils/WalletContext';
import { LLSpinner } from 'ui/FRWComponent';

import { KEY_TYPE } from '../../../utils/modules/constants';

const useStyles = makeStyles((theme) => ({
form: {
width: '100%', // Fix full width
Expand All @@ -31,6 +32,7 @@ const useStyles = makeStyles((theme) => ({
const KeyImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
// const classes = useStyles();
const classes = useStyles();
const wallet = useWallet();

const [isLoading, setLoading] = useState(false);

Expand All @@ -43,7 +45,7 @@ const KeyImport = ({ onOpen, onImport, setPk, isSignLoading }) => {
const inputValue = e.target[2].value;
setPk(pk);
const address = flowAddressRegex.test(inputValue) ? inputValue : null;
const result = await findAddressWithPK(pk, address);
const result = await wallet.findAddressWithPrivateKey(pk, address);
if (!result) {
onOpen();
return;
Expand Down
13 changes: 7 additions & 6 deletions src/ui/views/AddressImport/importComponent/SeedPhrase.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useEffect, useState, useContext } from 'react';
import { findAddressWithSeed } from '../../../utils/modules/findAddressWithPK';
import { KEY_TYPE } from '../../../utils/modules/constants';
import React from 'react';
import { Box, Button, Typography, TextareaAutosize } from '@mui/material';
import { makeStyles } from '@mui/styles';
import React, { useState } from 'react';

import { useWallet } from '@/ui/utils/WalletContext';
import { LLSpinner } from 'ui/FRWComponent';

import KeyPathInput from '../../../FRWComponent/KeyPathInputs';
import { KEY_TYPE } from '../../../utils/modules/constants';

const useStyles = makeStyles((theme) => ({
form: {
Expand All @@ -32,6 +33,7 @@ const useStyles = makeStyles((theme) => ({

const SeedPhraseImport = ({ onOpen, onImport, setmnemonic, isSignLoading }) => {
const classes = useStyles();
const wallet = useWallet();
const [isLoading, setLoading] = useState(false);

const handleImport = async (e) => {
Expand All @@ -45,8 +47,7 @@ const SeedPhraseImport = ({ onOpen, onImport, setmnemonic, isSignLoading }) => {

const address = flowAddressRegex.test(inputValue) ? inputValue : null;

console.log('address ', address);
const result = await findAddressWithSeed(seed, address, true);
const result = await wallet.findAddressWithSeedPhrase(seed, address, true);
if (!result) {
onOpen();
return;
Expand Down

0 comments on commit d895b0e

Please sign in to comment.