Skip to content

Commit

Permalink
feat: add EDDSA algo
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriaFireblocks committed Jun 16, 2024
1 parent 7aa9b10 commit 2a9c47d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
17 changes: 11 additions & 6 deletions example/src/AppStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,17 +638,22 @@ export const useAppStore = create<IAppState>()((set, get) => {
),
}));
},
generateMPCKeys: async () => {
generateMPCKeys: async (algorithms: Set<TMPCAlgorithm>) => {
if (!fireblocksNCW) {
throw new Error('fireblocksNCW is not initialized');
}
// TODO: consolidate

const ALGORITHMS = new Set<TMPCAlgorithm>([
'MPC_ECDSA_SECP256K1',
'MPC_EDDSA_ED25519',
]);
await fireblocksNCW.generateMPCKeys(ALGORITHMS);
if (algorithms) {
await fireblocksNCW.generateMPCKeys(algorithms);
} else {
const ALGORITHMS = new Set<TMPCAlgorithm>([
'MPC_ECDSA_SECP256K1',
'MPC_EDDSA_ED25519',
]);

await fireblocksNCW.generateMPCKeys(ALGORITHMS);
}
},
stopMpcDeviceSetup: async () => {
if (!fireblocksNCW) {
Expand Down
2 changes: 1 addition & 1 deletion example/src/IAppState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface IAppState {
assignCurrentDevice: () => Promise<void>;
askToJoinWalletExisting: () => Promise<void>;
generateNewDeviceId: () => Promise<void>;
generateMPCKeys: () => Promise<void>;
generateMPCKeys: (algorithms: Set<TMPCAlgorithm>) => Promise<void>;
stopMpcDeviceSetup: () => Promise<void>;
createTransaction: (dataToSend?: INewTransactionData) => Promise<void>;
cancelTransaction: (txId: string) => Promise<void>;
Expand Down
87 changes: 82 additions & 5 deletions example/src/components/GenerateMPCKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { Text, View } from 'react-native';
import { Bar } from 'react-native-progress';
import { QRScanner } from './QRScanner';

import type { TKeyStatus } from '@fireblocks/react-native-ncw-sdk';
import type {
TKeyStatus,
TMPCAlgorithm,
} from '@fireblocks/react-native-ncw-sdk';

export const GenerateMPCKeys: React.FC = () => {
const [err, setErr] = React.useState<string | null>(null);
Expand All @@ -32,7 +35,59 @@ export const GenerateMPCKeys: React.FC = () => {
setErr(null);
setIsGenerateInProgress(true);
try {
await generateMPCKeys();
const ALGORITHMS = new Set<TMPCAlgorithm>([
'MPC_ECDSA_SECP256K1',
'MPC_EDDSA_ED25519',
]);
await generateMPCKeys(ALGORITHMS);
setGenerateMPCKeysResult('Success');
setIsGenerateInProgress(false);
} catch (e: unknown) {
if (e instanceof Error) {
setErr(e.message);
} else {
if (typeof e === 'string') {
setErr(e);
} else {
setErr('Unknown Error');
}
}
} finally {
setIsGenerateInProgress(false);
}
};

const doGenerateMPCKeyECDSA = async () => {
setGenerateMPCKeysResult(null);
setErr(null);
setIsGenerateInProgress(true);
try {
const ALGORITHMS = new Set<TMPCAlgorithm>(['MPC_ECDSA_SECP256K1']);
await generateMPCKeys(ALGORITHMS);
setGenerateMPCKeysResult('Success');
setIsGenerateInProgress(false);
} catch (e: unknown) {
if (e instanceof Error) {
setErr(e.message);
} else {
if (typeof e === 'string') {
setErr(e);
} else {
setErr('Unknown Error');
}
}
} finally {
setIsGenerateInProgress(false);
}
};

const doGenerateMPCKeyEdDSA = async () => {
setGenerateMPCKeysResult(null);
setErr(null);
setIsGenerateInProgress(true);
try {
const ALGORITHMS = new Set<TMPCAlgorithm>(['MPC_EDDSA_ED25519']);
await generateMPCKeys(ALGORITHMS);
setGenerateMPCKeysResult('Success');
setIsGenerateInProgress(false);
} catch (e: unknown) {
Expand Down Expand Up @@ -95,14 +150,24 @@ export const GenerateMPCKeys: React.FC = () => {
isDisabled: isGenerateInProgress || (secP256K1Ready && ed25519Ready),
isInProgress: isGenerateInProgress,
};

const generateActionECDSA: IActionButtonProps = {
label: 'Generate MPC ECDSA Key',
action: doGenerateMPCKeyECDSA,
isDisabled: secP256K1Ready,
isInProgress: isGenerateInProgress,
};
const generateActionEdDSA: IActionButtonProps = {
label: 'Generate MPC EdDsa Key',
action: doGenerateMPCKeyEdDSA,
isDisabled: ed25519Ready,
isInProgress: isGenerateInProgress,
};
const stopAction: IActionButtonProps = {
label: 'Stop MPC Device Setup',
action: doStopMPCDeviceSetup,
isDisabled: isStopInProgress || !isGenerateInProgress,
isInProgress: isStopInProgress,
};

const approveJoinWalletAction: IActionButtonProps = {
label: 'Approve Join Wallet',
action: () => setShowScanQr(true),
Expand All @@ -115,7 +180,13 @@ export const GenerateMPCKeys: React.FC = () => {
action: stopJoinExistingWallet,
};

const actions = [generateAction, stopAction, approveJoinWalletAction];
const actions = [
generateAction,
generateActionECDSA,
generateActionEdDSA,
stopAction,
approveJoinWalletAction,
];
if (ENV_CONFIG.DEV_MODE) {
actions.push(stopApproveWalletAction);
}
Expand All @@ -126,6 +197,7 @@ export const GenerateMPCKeys: React.FC = () => {
<Table>
<Row data={['Algorithm', 'Status']} />
<Row data={['ECDSA SECP256K1', secP256K1Status ?? 'N/A']} />
<Row data={['EDDSA_ED25519', ed25519Status ?? 'N/A']} />
</Table>
</View>
{secP256K1Status && (
Expand All @@ -136,6 +208,11 @@ export const GenerateMPCKeys: React.FC = () => {
/>
</View>
)}
{ed25519Status && (
<View>
<Bar progress={statusToProgress(ed25519Status) / 100} width={null} />
</View>
)}
{generateMPCKeysResult && (
<View>
<Text>Result: {generateMPCKeysResult}</Text>
Expand Down
17 changes: 16 additions & 1 deletion example/src/components/JoinExistingWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export const JoinExistingWallet: React.FC = () => {
};

const secP256K1Status = keysStatus?.MPC_ECDSA_SECP256K1?.keyStatus ?? null;
const ed25519Status = keysStatus?.MPC_EDDSA_ED25519?.keyStatus ?? null;

const statusToProgress = (status: TKeyStatus | null) => {
switch (status) {
case 'INITIATED':
Expand All @@ -71,7 +73,7 @@ export const JoinExistingWallet: React.FC = () => {
return 0;
}
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars

// const secP256K1Ready = secP256K1Status === 'READY';

const generateAction: IActionButtonProps = {
Expand Down Expand Up @@ -108,8 +110,21 @@ export const JoinExistingWallet: React.FC = () => {
statusToProgress(secP256K1Status),
]}
/>
<Row
data={[
'EDDSA ED25519',
ed25519Status,
statusToProgress(ed25519Status),
]}
/>
</Table>
<Text>ECDSA SECP256K1:</Text>
<Bar progress={statusToProgress(secP256K1Status) / 100} width={null} />
<Text>EDDSA ED25519:</Text>
<Bar
progress={statusToProgress(ed25519Status) / 100}
width={null}
/>{' '}
</View>
{joinExistingWalletResult && (
<Text>Result: {joinExistingWalletResult}</Text>
Expand Down

0 comments on commit 2a9c47d

Please sign in to comment.