Skip to content

Commit

Permalink
Consolidate tx build calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 24, 2023
1 parent 5bce1b7 commit 97f8cf2
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 81 deletions.
2 changes: 1 addition & 1 deletion apps/extension/src/Approvals/Approvals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ThemeProvider } from "styled-components";
import { Routes, Route } from "react-router-dom";

import { getTheme } from "@namada/utils";
import { TxType } from "@namada/types";
import { TxType } from "@namada/shared";

import {
AppContainer,
Expand Down
11 changes: 6 additions & 5 deletions apps/extension/src/Approvals/ApproveTx/ApproveTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { useCallback, useEffect } from "react";

import { Button, ButtonVariant } from "@namada/components";
import { shortenAddress } from "@namada/utils";
import { AccountType, Tokens, TxType } from "@namada/types";
import { AccountType, Tokens } from "@namada/types";
import { TxType } from "@namada/shared";

import { useQuery } from "hooks";
import { Address } from "App/Accounts/AccountListing.components";
import {
ApprovalContainer,
ButtonContainer,
} from "Approvals/Approvals.components";
import { TopLevelRoute } from "Approvals/types";
import { TopLevelRoute, TxTypeLabel } from "Approvals/types";
import { Ports } from "router";
import { RejectTxMsg } from "background/approvals";
import { useRequester } from "hooks/useRequester";
Expand All @@ -35,7 +36,7 @@ export const ApproveTx: React.FC<Props> = ({
const requester = useRequester();

const params = useSanitizedParams();
const { type } = params;
const type = parseInt(params?.type || "0");

const query = useQuery();
const accountType = query.get("accountType") || "";
Expand All @@ -57,7 +58,7 @@ export const ApproveTx: React.FC<Props> = ({
setPublicKey(publicKey);
}
if (type) {
setTxType(type as TxType);
setTxType(type);
}
}, [source, publicKey]);

Expand Down Expand Up @@ -85,7 +86,7 @@ export const ApproveTx: React.FC<Props> = ({
return (
<ApprovalContainer>
<p>
Approve this <strong>{type}</strong> transaction?
Approve this <strong>{TxTypeLabel[type as TxType]}</strong> transaction?
</p>
<p>Source:&nbsp;</p>
<Address>{shortenAddress(source)}</Address>
Expand Down
31 changes: 17 additions & 14 deletions apps/extension/src/Approvals/ApproveTx/ConfirmLedgerTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import BigNumber from "bignumber.js";
import { LedgerError, ResponseSign } from "@namada/ledger-namada";
import { Button, ButtonVariant } from "@namada/components";
import { defaultChainId as chainId } from "@namada/chains";
import { TxType } from "@namada/shared";
import {
Message,
RevealPKProps,
SubmitRevealPKMsgValue,
Tokens,
} from "@namada/types";

import { Ledger } from "background/ledger";
import {
Expand All @@ -24,14 +31,9 @@ import {
ButtonContainer,
} from "Approvals/Approvals.components";
import { InfoHeader, InfoLoader } from "Approvals/Approvals.components";
import {
Message,
RevealPKProps,
SubmitRevealPKMsgValue,
Tokens,
TxType,
} from "@namada/types";

import { QueryPublicKeyMsg } from "background/keyring";
import { TxTypeLabel } from "Approvals/types";

type Props = {
address: string;
Expand Down Expand Up @@ -126,12 +128,12 @@ export const ConfirmLedgerTx: React.FC<Props> = ({
type?: TxType
): Promise<{ bytes: Uint8Array; path: string }> => {
switch (type) {
case TxType.bond:
case TxType.Bond:
return await requester.sendMessage(
Ports.Background,
new GetBondBytesMsg(msgId)
);
case TxType.transfer:
case TxType.Transfer:
return await requester.sendMessage(
Ports.Background,
new GetTransferBytesMsg(msgId)
Expand All @@ -148,12 +150,12 @@ export const ConfirmLedgerTx: React.FC<Props> = ({
type?: TxType
): Promise<void> => {
switch (type) {
case TxType.bond:
case TxType.Bond:
return await requester.sendMessage(
Ports.Background,
new SubmitSignedBondMsg(msgId, toBase64(bytes), signatures)
);
case TxType.transfer:
case TxType.Transfer:
return await requester.sendMessage(
Ports.Background,
new SubmitSignedTransferMsg(msgId, toBase64(bytes), signatures)
Expand All @@ -166,18 +168,19 @@ export const ConfirmLedgerTx: React.FC<Props> = ({
const submitTx = async (): Promise<void> => {
// Open ledger transport
const ledger = await Ledger.init();
const txLabel = TxTypeLabel[txType as TxType];

try {
// Constuct tx bytes from SDK
const { bytes, path } = await getBytesByType(txType);
setStatusInfo(`Review and approve ${txType} transaction on your Ledger`);
setStatusInfo(`Review and approve ${txLabel} transaction on your Ledger`);

// Sign with Ledger
const signatures = await ledger.sign(bytes, path);
const { errorMessage, returnCode } = signatures;

if (returnCode !== LedgerError.NoErrors) {
console.warn(`${txType} signing errors encountered, exiting: `, {
console.warn(`${txLabel} signing errors encountered, exiting: `, {
returnCode,
errorMessage,
});
Expand All @@ -186,7 +189,7 @@ export const ConfirmLedgerTx: React.FC<Props> = ({
}

// Submit signatures for tx
setStatusInfo(`Submitting ${txType} transaction...`);
setStatusInfo(`Submitting ${txLabel} transaction...`);
await submitByType(bytes, signatures, txType);
setStatus(Status.Completed);
} catch (e) {
Expand Down
15 changes: 9 additions & 6 deletions apps/extension/src/Approvals/ApproveTx/ConfirmTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
InputVariants,
} from "@namada/components";
import { shortenAddress } from "@namada/utils";
import { TxType } from "@namada/types";
import { TxType } from "@namada/shared";

import { Status } from "Approvals/Approvals";
import {
Expand All @@ -27,6 +27,7 @@ import {
import { Address } from "App/Accounts/AccountListing.components";
import { closeCurrentTab } from "utils";
import { FetchAndStoreMaspParamsMsg, HasMaspParamsMsg } from "provider";
import { TxTypeLabel } from "Approvals/types";

type Props = {
msgId: string;
Expand All @@ -40,14 +41,16 @@ export const ConfirmTx: React.FC<Props> = ({ address, msgId, txType }) => {
const [password, setPassword] = useState("");
const [error, setError] = useState<string>();
const [status, setStatus] = useState<Status>();
const [statusInfo, setStatusInfo] = useState<string>("");
const [statusInfo, setStatusInfo] = useState("");

const handleApproveTx = useCallback(async (): Promise<void> => {
setStatus(Status.Pending);
setStatusInfo(`Decrypting keys and submitting ${txType}...`);
setStatusInfo(
`Decrypting keys and submitting ${TxTypeLabel[txType as TxType]}...`
);

switch (txType) {
case TxType.bond: {
case TxType.Bond: {
await requester.sendMessage(
Ports.Background,
new SubmitApprovedBondMsg(msgId, password)
Expand All @@ -56,7 +59,7 @@ export const ConfirmTx: React.FC<Props> = ({ address, msgId, txType }) => {
setStatus(Status.Completed);
break;
}
case TxType.transfer: {
case TxType.Transfer: {
const hasMaspParams = await requester.sendMessage(
Ports.Background,
new HasMaspParamsMsg()
Expand Down Expand Up @@ -88,7 +91,7 @@ export const ConfirmTx: React.FC<Props> = ({ address, msgId, txType }) => {
}
break;
}
case TxType.unbond: {
case TxType.Unbond: {
await requester.sendMessage(
Ports.Background,
new SubmitApprovedUnbondMsg(msgId, password)
Expand Down
10 changes: 10 additions & 0 deletions apps/extension/src/Approvals/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TxType } from "@namada/shared";

export enum TopLevelRoute {
Default = "/",

Expand All @@ -9,3 +11,11 @@ export enum TopLevelRoute {
ConfirmTx = "/confirm-tx",
ConfirmLedgerTx = "/confirm-ledger-tx",
}

export const TxTypeLabel: Record<TxType, string> = {
[TxType.Bond]: "bond",
[TxType.Unbond]: "unbond",
[TxType.Transfer]: "transfer",
[TxType.Withdraw]: "withdraw",
[TxType.RevealPK]: "reveal-pk",
};
8 changes: 4 additions & 4 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
AccountType,
SubmitBondMsgValue,
TransferMsgValue,
TxType,
} from "@namada/types";
import { TxType } from "@namada/shared";
import { KVStore } from "@namada/storage";

import { KeyRingService, TabStore } from "background/keyring";
Expand All @@ -35,7 +35,7 @@ export class ApprovalsService {
const { source, target, token, amount: amountBN } = txDetails;
const amount = new BigNumber(amountBN.toString());
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${
TxType.transfer
TxType.Transfer
}`;

const url = paramsToUrl(baseUrl, {
Expand Down Expand Up @@ -66,7 +66,7 @@ export class ApprovalsService {
const { source, nativeToken: token, amount: amountBN } = txDetails;
const amount = new BigNumber(amountBN.toString());
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${
TxType.bond
TxType.Bond
}`;

const url = paramsToUrl(baseUrl, {
Expand Down Expand Up @@ -95,7 +95,7 @@ export class ApprovalsService {
const amount = new BigNumber(amountBN.toString());
// TODO: This query should include perhaps a "type" indicating whether it's a bond or unbond tx:
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${
TxType.unbond
TxType.Unbond
}`;

const url = paramsToUrl(baseUrl, {
Expand Down
8 changes: 4 additions & 4 deletions apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TransferMsgValue,
} from "@namada/types";
import { ResponseSign } from "@namada/ledger-namada";
import { Sdk } from "@namada/shared";
import { Sdk, TxType } from "@namada/shared";
import { IStore, KVStore, Store } from "@namada/storage";
import { chains } from "@namada/chains";
import { makeBip44Path } from "@namada/utils";
Expand Down Expand Up @@ -62,7 +62,7 @@ export class LedgerService {
throw new Error(`Ledger account not found for ${publicKey}`);
}

const bytes = await this.sdk.build_reveal_pk(fromBase64(txMsg));
const bytes = await this.sdk.build_tx(TxType.RevealPK, fromBase64(txMsg));
const path = makeBip44Path(coinType, account.path);

return { bytes, path };
Expand Down Expand Up @@ -119,7 +119,7 @@ export class LedgerService {
throw new Error(`Ledger account not found for ${source}`);
}

const bytes = await this.sdk.build_transfer(fromBase64(txMsg));
const bytes = await this.sdk.build_tx(TxType.Transfer, fromBase64(txMsg));
const path = makeBip44Path(coinType, account.path);

return { bytes, path };
Expand Down Expand Up @@ -187,7 +187,7 @@ export class LedgerService {
throw new Error(`Ledger account not found for ${source}`);
}

const bytes = await this.sdk.build_bond(fromBase64(txMsg));
const bytes = await this.sdk.build_tx(TxType.Bond, fromBase64(txMsg));
const path = makeBip44Path(coinType, account.path);

return { bytes, path };
Expand Down
Loading

0 comments on commit 97f8cf2

Please sign in to comment.