Skip to content

Commit

Permalink
Fix issue with Reveal PK tx
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 18, 2023
1 parent cdf693a commit 43cd3e4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
53 changes: 32 additions & 21 deletions apps/extension/src/Approvals/ApproveBond/ConfirmLedgerBond.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const ConfirmLedgerBond: React.FC<Props> = ({
const [status, setStatus] = useState<Status>();
const [statusInfo, setStatusInfo] = useState("");

const revealPk = async (ledger: Ledger, publicKey: string): Promise<void> => {
const revealPk = async (publicKey: string): Promise<void> => {
const revealPKArgs: RevealPKProps = {
tx: {
token: Tokens.NAM.address || "",
Expand All @@ -58,24 +58,33 @@ export const ConfirmLedgerBond: React.FC<Props> = ({
const txMsg = new Message<RevealPKMsgValue>();
const serializedTx = txMsg.encode(SubmitRevealPKMsgSchema, revealTxValue);

// Open Ledger transport
const ledger = await Ledger.init();

try {
const { bytes, path } = await requester.sendMessage(
Ports.Background,
new GetRevealPKBytesMsg(toBase64(serializedTx))
);

// Sign with Ledger
// Sign with Ledgeg
const signatures = await ledger.sign(bytes, path);

// Submit signatures for tx
setStatusInfo("Submitting reveal pk tx...");
await requester.sendMessage(
Ports.Background,
new SubmitSignedRevealPKMsg(msgId, toBase64(bytes), signatures)
new SubmitSignedRevealPKMsg(
toBase64(serializedTx),
toBase64(bytes),
signatures
)
);
} catch (e) {
console.warn(e);
console.warn("An error occured: ", e);
throw new Error(`${e}`);
} finally {
await ledger.closeTransport();
}
};

Expand All @@ -90,50 +99,52 @@ export const ConfirmLedgerBond: React.FC<Props> = ({

const submitBond = async (): Promise<void> => {
setStatus(Status.Pending);
const ledger = await Ledger.init();
setStatusInfo("Querying for public key on chain...");

try {
setStatusInfo("Querying for public key on chain...");
const pk = await queryPublicKey(address);

if (!pk) {
setStatusInfo(
"Public key not found! Review and approve reveal pk on your Ledger"
);
await revealPk(ledger, publicKey);
}
const pk = await queryPublicKey(address);

setStatusInfo("Review and approve transaction on your Ledger");
if (!pk) {
setStatusInfo(
"Public key not found! Review and approve reveal pk on your Ledger"
);
await revealPk(publicKey);
}

// Open ledger transport
const ledger = await Ledger.init();

try {
// Constuct tx bytes from SDK
const { bytes, path } = await requester.sendMessage(
Ports.Background,
new GetBondBytesMsg(msgId)
);

setStatusInfo("Review and approve bond transaction on your Ledger");
// Sign with Ledger
const signatures = await ledger.sign(bytes, path);
const { errorMessage, returnCode } = signatures;

if (returnCode !== LedgerError.NoErrors) {
console.warn("Bond sign errors encountered, exiting: ", {
returnCode,
errorMessage,
});
setError(errorMessage);
return setStatus(Status.Failed);
}

// Submit signatures for tx
setStatusInfo("Submitting transaction...");
setStatusInfo("Submitting bond transaction...");
await requester.sendMessage(
Ports.Background,
new SubmitSignedBondMsg(msgId, toBase64(bytes), signatures)
);
setStatus(Status.Completed);
} catch (e) {
console.warn(e);
const ledgerErrors = await ledger.queryErrors();
setError(ledgerErrors);
setStatus(Status.Failed);
} finally {
ledger.closeTransport();
await ledger.closeTransport();
}
};

Expand Down
6 changes: 3 additions & 3 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ApprovalsService {
async approveTransfer(txMsg: string, type?: AccountType): Promise<void> {
const txMsgBuffer = Buffer.from(fromBase64(txMsg));
const id = uuid();
this.txStore.set(id, txMsg);
await this.txStore.set(id, txMsg);

// Decode tx details and launch approval screen
const txDetails = deserialize(
Expand All @@ -53,7 +53,7 @@ export class ApprovalsService {
): Promise<void> {
const txMsgBuffer = Buffer.from(fromBase64(txMsg));
const id = uuid();
this.txStore.set(id, txMsg);
await this.txStore.set(id, txMsg);

// Decode tx details and launch approval screen
const txDetails = deserialize(
Expand All @@ -80,7 +80,7 @@ export class ApprovalsService {
async approveUnbond(txMsg: string, type?: AccountType): Promise<void> {
const txMsgBuffer = Buffer.from(fromBase64(txMsg));
const id = uuid();
this.txStore.set(id, txMsg);
await this.txStore.set(id, txMsg);

// Decode tx details and launch approval screen
const txDetails = deserialize(
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/ledger/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ const handleSubmitSignedRevealPKMsg: (
) => InternalHandler<SubmitSignedRevealPKMsg> = (service) => {
return async (_, msg) => {
const { txMsg, bytes, signatures } = msg;
return await service.submitBond(txMsg, bytes, signatures);
return await service.submitRevealPk(txMsg, bytes, signatures);
};
};
4 changes: 3 additions & 1 deletion apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ export class LedgerService {
msgId: string
): Promise<{ bytes: Uint8Array; path: string }> {
const txMsg = await this.txStore.get(msgId);
const { coinType } = chains[this.chainId].bip44;

if (!txMsg) {
console.warn(`txMsg not found for msgId: ${msgId}`);
throw new Error(`Transfer Transaction ${msgId} not found!`);
}

const { coinType } = chains[this.chainId].bip44;

try {
// Deserialize txMsg to retrieve source
const { source } = deserialize(
Expand Down

0 comments on commit 43cd3e4

Please sign in to comment.