Skip to content

Commit

Permalink
feat: additional review prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Nov 20, 2024
1 parent 21e1191 commit 348a71d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 43 deletions.
44 changes: 28 additions & 16 deletions apps/extension/src/Setup/Ledger/LedgerConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ type Props = {
setPath: (path: Bip44Path) => void;
};

enum LedgerReviewPrompt {
AddressAndPublicKey = "address and public key",
ViewingKey = "viewing key",
ProofGenerationKeys = "proof generation keys",
}

export const LedgerConnect: React.FC<Props> = ({ path, setPath }) => {
const navigate = useNavigate();
const [error, setError] = useState<string>();
const [isLedgerConnecting, setIsLedgerConnecting] = useState(false);
const [reviewPrompt, setReviewPrompt] = useState<LedgerReviewPrompt>();
const [ledger, setLedger] = useState<LedgerApp>();

const queryLedger = async (ledger: LedgerApp): Promise<void> => {
Expand All @@ -32,25 +38,31 @@ export const LedgerConnect: React.FC<Props> = ({ path, setPath }) => {
throw new Error(errorMessage);
}

setIsLedgerConnecting(true);
const { address, publicKey } = await ledger.showAddressAndPublicKey(
makeBip44Path(chains.namada.bip44.coinType, path)
);

const response = await ledger.getShieldedKeys(
makeBip44Path(chains.namada.bip44.coinType, path),
true
);
console.log({ shieldedResponse: response });
setIsLedgerConnecting(false);
const bip44Path = makeBip44Path(chains.namada.bip44.coinType, path);

setReviewPrompt(LedgerReviewPrompt.AddressAndPublicKey);
const { address, publicKey } =
await ledger.showAddressAndPublicKey(bip44Path);

setReviewPrompt(LedgerReviewPrompt.ViewingKey);
const viewingKey = await ledger.getViewingKey(bip44Path, true);
console.log({ viewingKey });

setReviewPrompt(LedgerReviewPrompt.ProofGenerationKeys);
const proofGenerationKeys =
await ledger.getProofGenerationKeys(bip44Path);

console.log({ proofGenerationKeys });
setReviewPrompt(undefined);

navigate(routes.ledgerImport(), {
state: {
address,
publicKey,
},
});
} catch (e) {
setIsLedgerConnecting(false);
setReviewPrompt(undefined);
handleError(e);
} finally {
await ledger.closeTransport();
Expand Down Expand Up @@ -100,8 +112,8 @@ export const LedgerConnect: React.FC<Props> = ({ path, setPath }) => {
</Alert>
)}

{isLedgerConnecting && (
<Alert type="warning">Review on your Ledger</Alert>
{reviewPrompt && (
<Alert type="warning">Review {reviewPrompt} on your Ledger</Alert>
)}

<LedgerStep
Expand All @@ -122,7 +134,7 @@ export const LedgerConnect: React.FC<Props> = ({ path, setPath }) => {
active={!!ledger}
complete={false}
onClick={() => connectNamadaApp()}
buttonDisabled={!ledger || isLedgerConnecting}
buttonDisabled={!ledger || Boolean(reviewPrompt)}
image={
<Image styleOverrides={{ width: "100%" }} imageName="LogoMinimal" />
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export {
} from "./ledger";
export type {
LedgerAddressAndPublicKey,
LedgerShieldedKeys,
LedgerStatus,
LedgerViewingKey,
} from "./ledger";

// Export types
Expand Down
65 changes: 39 additions & 26 deletions packages/sdk/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ import { makeBip44Path } from "./utils";
const { coinType } = chains.namada.bip44;

export type LedgerAddressAndPublicKey = { address: string; publicKey: string };
export type LedgerShieldedKeys = {
viewingKey: {
viewKey?: string;
ivk?: string;
ovk?: string;
};
proofGenerationKey: {
ak?: string;
nsk?: string;
};
// TODO: This should be xfvk, awaiting an updated version!
export type LedgerViewingKey = {
viewKey?: string;
ivk?: string;
ovk?: string;
};
export type LedgerProofGenerationKey = {
ak?: string;
nsk?: string;
};

export type LedgerStatus = {
Expand Down Expand Up @@ -148,42 +147,56 @@ export class Ledger {
}

/**
* Prompt user to get viewing and proof gen key associated with optional path, otherwise, use default path.
* Prompt user to get viewing key associated with optional path, otherwise, use default path.
* Throw exception if app is not initialized.
* @async
* @param [path] Bip44 path for deriving key
* @param [promptUser] boolean to determine whether to display on Ledger device and require approval
* @returns ShieldedKeys
* @returns LedgerViewingKey
*/
public async getShieldedKeys(
public async getViewingKey(
path: string = DEFAULT_LEDGER_BIP44_PATH,
promptUser = true
): Promise<LedgerShieldedKeys> {
): Promise<LedgerViewingKey> {
try {
const { viewKey, ivk, ovk }: ResponseViewKey =
await this.namadaApp.retrieveKeys(path, NamadaKeys.ViewKey, promptUser);

// NOTE: If promptUser is false, the proof generation keys will not be defined
return {
viewKey: viewKey ? toHex(new Uint8Array(viewKey)) : undefined,
ivk: ivk ? toHex(new Uint8Array(ivk)) : undefined,
ovk: ovk ? toHex(new Uint8Array(ovk)) : undefined,
};
} catch (e) {
throw new Error(`Could not retrieve Viewing Key: ${e}`);
}
}

/**
* Prompt user to get proof generation keys associated with optional path, otherwise, use default path.
* Throw exception if app is not initialized.
* @async
* @param [path] Bip44 path for deriving key
* @returns LedgerProofGenerationKey
*/
public async getProofGenerationKeys(
path: string = DEFAULT_LEDGER_BIP44_PATH
): Promise<LedgerProofGenerationKey> {
try {
const { ak, nsk }: ResponseProofGenKey =
await this.namadaApp.retrieveKeys(
path,
NamadaKeys.ProofGenerationKey,
promptUser
// NOTE: Setting this to false will result in undefined values
true
);

return {
viewingKey: {
viewKey: viewKey ? toHex(viewKey) : undefined,
ivk: ivk ? toHex(ivk) : undefined,
ovk: ovk ? toHex(ovk) : undefined,
},
proofGenerationKey: {
ak: ak ? toHex(ak) : undefined,
nsk: nsk ? toHex(nsk) : undefined,
},
ak: ak ? toHex(new Uint8Array(ak)) : undefined,
nsk: nsk ? toHex(new Uint8Array(nsk)) : undefined,
};
} catch (e) {
throw new Error(`Could not retrieve Viewing Key`);
throw new Error(`Could not retrive Proof Generation Keys: ${e}`);
}
}

Expand Down

0 comments on commit 348a71d

Please sign in to comment.