Skip to content

Commit

Permalink
Consolidate messages into SubmitSignedTx
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 26, 2023
1 parent 5f1ae75 commit cefbc58
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 251 deletions.
38 changes: 7 additions & 31 deletions apps/extension/src/Approvals/ApproveTx/ConfirmLedgerTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useState } from "react";
import { toBase64 } from "@cosmjs/encoding";
import BigNumber from "bignumber.js";

import { LedgerError, ResponseSign } from "@namada/ledger-namada";
import { LedgerError } from "@namada/ledger-namada";
import { Button, ButtonVariant } from "@namada/components";
import { defaultChainId as chainId } from "@namada/chains";
import { TxType } from "@namada/shared";
Expand All @@ -12,10 +12,8 @@ import { Ledger } from "background/ledger";
import {
GetRevealPKBytesMsg,
GetTxBytesMsg,
SubmitSignedBondMsg,
SubmitSignedRevealPKMsg,
SubmitSignedTransferMsg,
SubmitSignedUnbondMsg,
SubmitSignedTxMsg,
} from "background/ledger/messages";
import { Ports } from "router";
import { closeCurrentTab } from "utils";
Expand Down Expand Up @@ -113,32 +111,6 @@ export const ConfirmLedgerTx: React.FC<Props> = ({ details }) => {
}
}, [source, publicKey]);

const submitByType = async (
bytes: Uint8Array,
signatures: ResponseSign,
type?: TxType
): Promise<void> => {
switch (type) {
case TxType.Bond:
return await requester.sendMessage(
Ports.Background,
new SubmitSignedBondMsg(msgId, toBase64(bytes), signatures)
);
case TxType.Unbond:
return await requester.sendMessage(
Ports.Background,
new SubmitSignedUnbondMsg(msgId, toBase64(bytes), signatures)
);
case TxType.Transfer:
return await requester.sendMessage(
Ports.Background,
new SubmitSignedTransferMsg(msgId, toBase64(bytes), signatures)
);
default:
throw new Error("Invalid transaction type!");
}
};

const submitTx = async (): Promise<void> => {
// Open ledger transport
const ledger = await Ledger.init();
Expand Down Expand Up @@ -175,7 +147,11 @@ export const ConfirmLedgerTx: React.FC<Props> = ({ details }) => {

// Submit signatures for tx
setStatusInfo(`Submitting ${txLabel} transaction...`);
await submitByType(bytes, signatures, txType);
await requester.sendMessage(
Ports.Background,
new SubmitSignedTxMsg(txType, msgId, toBase64(bytes), signatures)
);

setStatus(Status.Completed);
} catch (e) {
console.warn(e);
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/Approvals/ApproveTx/ConfirmTx.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useContext, useEffect, useState } from "react";
import { useCallback, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

import {
Expand Down
57 changes: 12 additions & 45 deletions apps/extension/src/background/ledger/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { LedgerService } from "./service";
import {
AddLedgerAccountMsg,
GetTxBytesMsg,
SubmitSignedTransferMsg,
SubmitSignedBondMsg,
SubmitSignedRevealPKMsg,
GetRevealPKBytesMsg,
SubmitSignedUnbondMsg,
SubmitSignedTxMsg,
} from "./messages";

export const getHandler: (service: LedgerService) => Handler = (service) => {
Expand All @@ -28,21 +26,8 @@ export const getHandler: (service: LedgerService) => Handler = (service) => {
env,
msg as SubmitSignedRevealPKMsg
);
case SubmitSignedTransferMsg:
return handleSubmitSignedTransferMsg(service)(
env,
msg as SubmitSignedTransferMsg
);
case SubmitSignedBondMsg:
return handleSubmitSignedBondMsg(service)(
env,
msg as SubmitSignedBondMsg
);
case SubmitSignedUnbondMsg:
return handleSubmitSignedUnbondMsg(service)(
env,
msg as SubmitSignedBondMsg
);
case SubmitSignedTxMsg:
return handleSubmitSignedTxMsg(service)(env, msg as SubmitSignedTxMsg);
default:
throw new Error("Unknown msg type");
}
Expand All @@ -58,33 +43,6 @@ const handleAddLedgerAccountMsg: (
};
};

const handleSubmitSignedTransferMsg: (
service: LedgerService
) => InternalHandler<SubmitSignedTransferMsg> = (service) => {
return async (_, msg) => {
const { bytes, msgId, signatures } = msg;
return await service.submitTransfer(msgId, bytes, signatures);
};
};

const handleSubmitSignedBondMsg: (
service: LedgerService
) => InternalHandler<SubmitSignedBondMsg> = (service) => {
return async (_, msg) => {
const { bytes, msgId, signatures } = msg;
return await service.submitBond(msgId, bytes, signatures);
};
};

const handleSubmitSignedUnbondMsg: (
service: LedgerService
) => InternalHandler<SubmitSignedUnbondMsg> = (service) => {
return async (_, msg) => {
const { bytes, msgId, signatures } = msg;
return await service.submitUnbond(msgId, bytes, signatures);
};
};
``;
const handleGetTxBytesMsg: (
service: LedgerService
) => InternalHandler<GetTxBytesMsg> = (service) => {
Expand All @@ -111,3 +69,12 @@ const handleSubmitSignedRevealPKMsg: (
return await service.submitRevealPk(txMsg, bytes, signatures);
};
};

const handleSubmitSignedTxMsg: (
service: LedgerService
) => InternalHandler<SubmitSignedTxMsg> = (service) => {
return async (_, msg) => {
const { txType, bytes, msgId, signatures } = msg;
return await service.submitTx(txType, msgId, bytes, signatures);
};
};
8 changes: 2 additions & 6 deletions apps/extension/src/background/ledger/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import {
AddLedgerAccountMsg,
GetTxBytesMsg,
GetRevealPKBytesMsg,
SubmitSignedBondMsg,
SubmitSignedTxMsg,
SubmitSignedRevealPKMsg,
SubmitSignedUnbondMsg,
SubmitSignedTransferMsg,
} from "./messages";
import { getHandler } from "./handler";
import { LedgerService } from "./service";
Expand All @@ -16,10 +14,8 @@ export function init(router: Router, service: LedgerService): void {
router.registerMessage(AddLedgerAccountMsg);
router.registerMessage(GetTxBytesMsg);
router.registerMessage(GetRevealPKBytesMsg);
router.registerMessage(SubmitSignedBondMsg);
router.registerMessage(SubmitSignedTxMsg);
router.registerMessage(SubmitSignedRevealPKMsg);
router.registerMessage(SubmitSignedUnbondMsg);
router.registerMessage(SubmitSignedTransferMsg);

router.addHandler(ROUTE, getHandler(service));
}
86 changes: 6 additions & 80 deletions apps/extension/src/background/ledger/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@ enum MessageType {
AddLedgerAccount = "add-ledger-account",
GetTxBytes = "get-tx-bytes",
GetRevealPKBytes = "get-reveal-pk-bytes",

// TODO: - Implement for single SubmitSignedTx
SubmitSignedTx = "submit-signed-tx",

// TODO: Remove:
SubmitSignedRevealPK = "submit-signed-reveal-pk",
SubmitSignedTransfer = "submit-signed-transfer",
SubmitSignedBond = "submit-signed-bond",
SubmitSignedUnbond = "submit-signed-unbond",
}

export class AddLedgerAccountMsg extends Message<void> {
Expand Down Expand Up @@ -161,48 +154,13 @@ export class SubmitSignedRevealPKMsg extends Message<void> {
}
}

export class SubmitSignedTransferMsg extends Message<void> {
public static type(): MessageType {
return MessageType.SubmitSignedTransfer;
}

constructor(
public readonly msgId: string,
public readonly bytes: string,
public readonly signatures: ResponseSign
) {
super();
}

validate(): void {
if (!this.msgId) {
throw new Error("msgId was not provided!");
}

if (!this.bytes) {
throw new Error("bytes were not provided!");
}

if (!this.signatures) {
throw new Error("No signatures were provided!");
}
}

route(): string {
return ROUTE;
}

type(): string {
return SubmitSignedTransferMsg.type();
}
}

export class SubmitSignedBondMsg extends Message<void> {
export class SubmitSignedTxMsg extends Message<void> {
public static type(): MessageType {
return MessageType.SubmitSignedBond;
return MessageType.SubmitSignedTx;
}

constructor(
public readonly txType: TxType,
public readonly msgId: string,
public readonly bytes: string,
public readonly signatures: ResponseSign
Expand All @@ -211,42 +169,10 @@ export class SubmitSignedBondMsg extends Message<void> {
}

validate(): void {
if (!this.msgId) {
throw new Error("msgId was not provided!");
}

if (!this.bytes) {
throw new Error("bytes were not provided!");
}

if (!this.signatures) {
throw new Error("No signatures were provided!");
if (!this.txType) {
throw new Error("txType was not provided!");
}
}

route(): string {
return ROUTE;
}

type(): string {
return SubmitSignedBondMsg.type();
}
}

export class SubmitSignedUnbondMsg extends Message<void> {
public static type(): MessageType {
return MessageType.SubmitSignedUnbond;
}

constructor(
public readonly msgId: string,
public readonly bytes: string,
public readonly signatures: ResponseSign
) {
super();
}

validate(): void {
if (!this.msgId) {
throw new Error("msgId was not provided!");
}
Expand All @@ -265,6 +191,6 @@ export class SubmitSignedUnbondMsg extends Message<void> {
}

type(): string {
return SubmitSignedUnbondMsg.type();
return SubmitSignedTxMsg.type();
}
}
Loading

0 comments on commit cefbc58

Please sign in to comment.