Skip to content

Commit 05608ba

Browse files
committed
feat: stealing Mateusz code, show payment address in confirmation
1 parent 0ce1cd4 commit 05608ba

File tree

18 files changed

+204
-69
lines changed

18 files changed

+204
-69
lines changed

apps/extension/src/App/Accounts/ParentAccounts.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import invariant from "invariant";
12
import { useContext, useEffect } from "react";
23
import { Outlet, useNavigate } from "react-router-dom";
34

@@ -29,17 +30,14 @@ export const ParentAccounts = (): JSX.Element => {
2930

3031
// We check which accounts need to be re-imported
3132
const accounts = allAccounts
32-
.filter(
33-
(account) => account.parentId || account.type === AccountType.Ledger
34-
)
33+
.filter((account) => account.parentId)
3534
.map((account) => {
3635
const outdated =
3736
account.type !== AccountType.Ledger &&
3837
typeof account.pseudoExtendedKey === "undefined";
3938

40-
// The only account without a parent is the ledger account
41-
const parent =
42-
parentAccounts.find((pa) => pa.id === account.parentId) || account;
39+
const parent = parentAccounts.find((pa) => pa.id === account.parentId);
40+
invariant(parent, `Parent account not found for account ${account.id}`);
4341

4442
return { ...parent, outdated };
4543
});

apps/extension/src/Setup/Common/Completion.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import browser from "webextension-polyfill";
44
import { chains } from "@namada/chains";
55
import { ActionButton, Alert, Loading, ViewKeys } from "@namada/components";
66
import { makeBip44Path } from "@namada/sdk/web";
7-
import { Bip44Path, DerivedAccount } from "@namada/types";
7+
import { Bip44Path } from "@namada/types";
88
import {
99
AccountSecret,
1010
AccountStore,
@@ -20,7 +20,7 @@ type Props = {
2020
status?: CompletionStatus;
2121
statusInfo: string;
2222
parentAccountStore?: AccountStore;
23-
shieldedAccount?: DerivedAccount;
23+
paymentAddress?: string;
2424
password?: string;
2525
passwordRequired: boolean | undefined;
2626
path: Bip44Path;
@@ -34,7 +34,7 @@ export const Completion: React.FC<Props> = (props) => {
3434
passwordRequired,
3535
path,
3636
parentAccountStore,
37-
shieldedAccount,
37+
paymentAddress,
3838
status,
3939
statusInfo,
4040
} = props;
@@ -84,7 +84,7 @@ export const Completion: React.FC<Props> = (props) => {
8484
publicKeyAddress={parentAccountStore?.publicKey}
8585
transparentAccountAddress={parentAccountStore?.address}
8686
transparentAccountPath={transparentAccountPath}
87-
shieldedAccountAddress={shieldedAccount?.address}
87+
shieldedAccountAddress={paymentAddress}
8888
trimCharacters={35}
8989
footer={
9090
<ActionButton

apps/extension/src/Setup/Ledger/LedgerConfirmation.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export const LedgerConfirmation = (): JSX.Element => {
1313
return <></>;
1414
}
1515

16-
const account = location.state.account as DerivedAccount;
16+
const account = location.state.account as DerivedAccount & {
17+
paymentAddress: string;
18+
};
1719
return (
1820
<Stack gap={4} className="h-[470px]">
1921
<p className="text-white text-center text-base w-full -mt-3 mb-8">
@@ -22,6 +24,7 @@ export const LedgerConfirmation = (): JSX.Element => {
2224
<ViewKeys
2325
publicKeyAddress={account.publicKey}
2426
transparentAccountAddress={account.address}
27+
shieldedAccountAddress={account.paymentAddress}
2528
trimCharacters={35}
2629
/>
2730
<ActionButton size="lg" onClick={closeCurrentTab}>

apps/extension/src/Setup/Ledger/LedgerConnect.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { chains } from "@namada/chains";
22
import { ActionButton, Alert, Image, Stack } from "@namada/components";
33
import {
4+
ExtendedViewingKey,
45
Ledger as LedgerApp,
56
makeBip44Path,
67
makeSaplingPath,
8+
ProofGenerationKey,
9+
PseudoExtendedKey,
710
} from "@namada/sdk/web";
11+
import initWasm from "@namada/sdk/web-init";
812
import { Bip44Path } from "@namada/types";
913
import { LedgerError } from "@zondax/ledger-namada";
1014
import { LedgerStep } from "Setup/Common";
@@ -48,12 +52,31 @@ export const LedgerConnect: React.FC<Props> = ({ path, setPath }) => {
4852
const { xfvk } = await ledger.getViewingKey(zip32Path);
4953
const { ak, nsk } = await ledger.getProofGenerationKey(zip32Path);
5054

51-
console.log("TODO", { zip32Path, xfvk, ak, nsk });
55+
// SDK wasm init must be called
56+
await initWasm();
57+
58+
const extendedViewingKey = new ExtendedViewingKey(xfvk);
59+
const encodedExtendedViewingKey = extendedViewingKey.encode();
60+
const encodedPaymentAddress = extendedViewingKey
61+
.default_payment_address()
62+
.encode();
63+
64+
const proofGenerationKey = ProofGenerationKey.from_bytes(ak, nsk);
65+
const pseudoExtendedKey = PseudoExtendedKey.from(
66+
extendedViewingKey,
67+
proofGenerationKey
68+
);
69+
const encodedPseudoExtendedKey = pseudoExtendedKey.encode();
70+
5271
setIsLedgerConnecting(false);
72+
5373
navigate(routes.ledgerImport(), {
5474
state: {
5575
address,
5676
publicKey,
77+
extendedViewingKey: encodedExtendedViewingKey,
78+
paymentAddress: encodedPaymentAddress,
79+
pseudoExtendedKey: encodedPseudoExtendedKey,
5780
},
5881
});
5982
} catch (e) {

apps/extension/src/Setup/Ledger/LedgerImport.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { useLocation, useNavigate } from "react-router-dom";
1010
type LedgerImportLocationState = {
1111
address: string;
1212
publicKey: string;
13+
extendedViewingKey: string;
14+
pseudoExtendedKey: string;
15+
paymentAddress: string;
1316
};
1417

1518
type LedgerProps = {
@@ -55,16 +58,25 @@ export const LedgerImport = ({
5558
await accountManager.savePassword(password);
5659
}
5760

58-
const { address, publicKey } = locationState;
61+
const {
62+
address,
63+
publicKey,
64+
extendedViewingKey,
65+
paymentAddress,
66+
pseudoExtendedKey,
67+
} = locationState;
5968
const account = await accountManager.saveLedgerAccount({
6069
alias,
6170
address,
6271
publicKey,
6372
path,
73+
paymentAddress,
74+
extendedViewingKey,
75+
pseudoExtendedKey,
6476
});
6577

6678
navigate(routes.ledgerComplete(), {
67-
state: { account: { ...account } },
79+
state: { account: { ...account, paymentAddress } },
6880
});
6981
} catch (e) {
7082
console.warn(e);

apps/extension/src/Setup/Setup.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
Container,
1212
LifecycleExecutionWrapper as Wrapper,
1313
} from "@namada/components";
14-
import { Bip44Path, DerivedAccount } from "@namada/types";
14+
import { Bip44Path } from "@namada/types";
1515
import { assertNever } from "@namada/utils";
1616
import { AccountSecret, AccountStore } from "background/keyring";
1717
import { AnimatePresence, motion } from "framer-motion";
@@ -81,7 +81,7 @@ export const Setup: React.FC = () => {
8181
});
8282

8383
const [parentAccountStore, setParentAccountStore] = useState<AccountStore>();
84-
const [shieldedAccount, setShieldedAccount] = useState<DerivedAccount>();
84+
const [paymentAddress, setPaymentAddress] = useState<string>();
8585
const [completionStatus, setCompletionStatus] = useState<CompletionStatus>();
8686
const [completionStatusInfo, setCompletionStatusInfo] = useState<string>("");
8787

@@ -127,7 +127,7 @@ export const Setup: React.FC = () => {
127127
details,
128128
parentAccount
129129
);
130-
setShieldedAccount(shieldedAccount);
130+
setPaymentAddress(shieldedAccount?.address);
131131
setCompletionStatus(CompletionStatus.Completed);
132132
setCompletionStatusInfo("Done!");
133133
} catch (e) {
@@ -275,7 +275,7 @@ export const Setup: React.FC = () => {
275275
password={accountCreationDetails.password || ""}
276276
path={path}
277277
parentAccountStore={parentAccountStore}
278-
shieldedAccount={shieldedAccount}
278+
paymentAddress={paymentAddress}
279279
status={completionStatus}
280280
statusInfo={completionStatusInfo}
281281
/>
@@ -352,7 +352,7 @@ export const Setup: React.FC = () => {
352352
password={accountCreationDetails.password || ""}
353353
path={path}
354354
parentAccountStore={parentAccountStore}
355-
shieldedAccount={shieldedAccount}
355+
paymentAddress={paymentAddress}
356356
status={completionStatus}
357357
statusInfo={completionStatusInfo}
358358
/>

apps/extension/src/Setup/query.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,26 @@ export class AccountManager {
8888
async saveLedgerAccount(
8989
details: LedgerAccountDetails
9090
): Promise<AccountStore> {
91-
const { alias, address, publicKey, path } = details;
91+
const {
92+
alias,
93+
address,
94+
publicKey,
95+
path,
96+
extendedViewingKey,
97+
pseudoExtendedKey,
98+
paymentAddress,
99+
} = details;
92100
return (await this.requester.sendMessage(
93101
Ports.Background,
94-
new AddLedgerAccountMsg(alias, address, publicKey, path)
102+
new AddLedgerAccountMsg(
103+
alias,
104+
address,
105+
publicKey,
106+
path,
107+
extendedViewingKey,
108+
pseudoExtendedKey,
109+
paymentAddress
110+
)
95111
)) as AccountStore;
96112
}
97113
}

apps/extension/src/Setup/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ export type LedgerAccountDetails = {
1919
path: Bip44Path;
2020
address: string;
2121
publicKey: string;
22+
extendedViewingKey: string;
23+
pseudoExtendedKey: string;
24+
paymentAddress: string;
2225
};

apps/extension/src/background/keyring/handler.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,24 @@ const handleAddLedgerAccountMsg: (
105105
service: KeyRingService
106106
) => InternalHandler<AddLedgerAccountMsg> = (service) => {
107107
return async (_, msg) => {
108-
const { alias, address, publicKey, bip44Path } = msg;
109-
return await service.saveLedger(alias, address, publicKey, bip44Path);
108+
const {
109+
alias,
110+
address,
111+
publicKey,
112+
bip44Path,
113+
extendedViewingKey,
114+
pseudoExtendedKey,
115+
paymentAddress,
116+
} = msg;
117+
return await service.saveLedger(
118+
alias,
119+
address,
120+
publicKey,
121+
bip44Path,
122+
extendedViewingKey,
123+
pseudoExtendedKey,
124+
paymentAddress
125+
);
110126
};
111127
};
112128

apps/extension/src/background/keyring/keyring.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ export class KeyRing {
101101
alias: string,
102102
address: string,
103103
publicKey: string,
104-
bip44Path: Bip44Path
104+
bip44Path: Bip44Path,
105+
pseudoExtendedKey: string,
106+
extendedViewingKey: string,
107+
paymentAddress: string
105108
): Promise<AccountStore | false> {
106109
const id = generateId(UUID_NAMESPACE, alias, address);
107110
const accountStore: AccountStore = {
@@ -125,6 +128,30 @@ export class KeyRing {
125128
sensitive,
126129
});
127130

131+
const shieldedId = generateId(UUID_NAMESPACE, alias, paymentAddress);
132+
const shieldedAccountStore: AccountStore = {
133+
id: shieldedId,
134+
alias,
135+
address: paymentAddress,
136+
publicKey,
137+
owner: extendedViewingKey,
138+
path: bip44Path,
139+
pseudoExtendedKey,
140+
parentId: id,
141+
type: AccountType.ShieldedKeys,
142+
source: "imported",
143+
timestamp: 0,
144+
};
145+
146+
const shieldedSensitive = await this.vaultService.encryptSensitiveData({
147+
text: "",
148+
passphrase: "",
149+
});
150+
await this.vaultStorage.add(KeyStore, {
151+
public: shieldedAccountStore,
152+
sensitive: shieldedSensitive,
153+
});
154+
128155
await this.setActiveAccount(id, AccountType.Ledger);
129156
return accountStore;
130157
}

apps/extension/src/background/keyring/messages.ts

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,23 @@ export class AddLedgerAccountMsg extends Message<AccountStore | false> {
186186
public readonly address: string,
187187
public readonly publicKey: string,
188188
public readonly bip44Path: Bip44Path,
189-
public readonly parentId?: string
189+
public readonly extendedViewingKey: string,
190+
public readonly pseudoExtendedKey: string,
191+
public readonly paymentAddress: string
190192
) {
191193
super();
192194
}
193195

194196
validate(): void {
195-
if (!this.alias) {
196-
throw new Error("Alias must not be empty!");
197-
}
198-
199-
if (!this.address) {
200-
throw new Error("Address was not provided!");
201-
}
202-
203-
if (!this.publicKey) {
204-
throw new Error("Public key was not provided!");
205-
}
206-
207-
if (!this.bip44Path) {
208-
throw new Error("BIP44 Path was not provided!");
209-
}
197+
validateProps(this, [
198+
"alias",
199+
"address",
200+
"publicKey",
201+
"bip44Path",
202+
"extendedViewingKey",
203+
"pseudoExtendedKey",
204+
"paymentAddress",
205+
]);
210206
}
211207

212208
route(): string {
@@ -269,13 +265,7 @@ export class SetActiveAccountMsg extends Message<void> {
269265
}
270266

271267
validate(): void {
272-
if (!this.accountId) {
273-
throw new Error("Account ID is not set!");
274-
}
275-
276-
if (!this.accountType) {
277-
throw new Error("Account Type is required!");
278-
}
268+
validateProps(this, ["accountId", "accountType"]);
279269
}
280270

281271
route(): string {
@@ -369,10 +359,7 @@ export class QueryAccountDetailsMsg extends Message<
369359
}
370360

371361
validate(): void {
372-
if (!this.address) {
373-
throw new Error("Account address is required!");
374-
}
375-
return;
362+
validateProps(this, ["address"]);
376363
}
377364

378365
route(): string {
@@ -397,12 +384,7 @@ export class AppendLedgerSignatureMsg extends Message<Uint8Array> {
397384
}
398385

399386
validate(): void {
400-
if (!this.txBytes) {
401-
throw new Error("txBytes is required!");
402-
}
403-
if (!this.signature) {
404-
throw new Error("signature is required!");
405-
}
387+
validateProps(this, ["txBytes", "signature"]);
406388
}
407389

408390
route(): string {

0 commit comments

Comments
 (0)