Skip to content

Commit

Permalink
feat: hook up sdk package to extension and interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Feb 29, 2024
1 parent e27ca84 commit a251d8b
Show file tree
Hide file tree
Showing 38 changed files with 638 additions and 700 deletions.
1 change: 1 addition & 0 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@namada/chains": "0.2.1",
"@namada/components": "0.2.1",
"@namada/crypto": "0.2.1",
"@namada/sdk": "0.2.4",
"@namada/shared": "0.2.1",
"@namada/storage": "0.2.1",
"@namada/types": "0.2.1",
Expand Down
17 changes: 4 additions & 13 deletions apps/extension/src/Approvals/ApproveTx/ConfirmLedgerTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useCallback, useEffect, useState } from "react";

import { ActionButton, Alert, Stack } from "@namada/components";
import { TxType, TxTypeLabel } from "@namada/shared";
import { Message, TxMsgValue, TxProps } from "@namada/types";
import { TxProps } from "@namada/types";
import { LedgerError } from "@zondax/ledger-namada";
import { ApprovalDetails, Status } from "Approvals/Approvals";
import { QueryPublicKeyMsg } from "background/keyring";
Expand Down Expand Up @@ -53,21 +53,16 @@ export const ConfirmLedgerTx: React.FC<Props> = ({ details }) => {
throw new Error("Native token is required!");
}

const txArgs: TxProps = {
const txProps: TxProps = {
token: nativeToken,
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(20_000),
chainId: chain.chainId,
publicKey,
};

const msgValue = new TxMsgValue(txArgs);
const msg = new Message<TxMsgValue>();
const encoded = msg.encode(msgValue);

// Open Ledger transport
const { bytes, path } = await requester
.sendMessage(Ports.Background, new GetRevealPKBytesMsg(toBase64(encoded)))
.sendMessage(Ports.Background, new GetRevealPKBytesMsg(txProps))
.catch((e) => {
throw new Error(`Requester error: ${e}`);
});
Expand All @@ -88,11 +83,7 @@ export const ConfirmLedgerTx: React.FC<Props> = ({ details }) => {
await requester
.sendMessage(
Ports.Background,
new SubmitSignedRevealPKMsg(
toBase64(encoded),
toBase64(bytes),
signatures
)
new SubmitSignedRevealPKMsg(txProps, toBase64(bytes), signatures)
)
.catch((e) => {
throw new Error(`Requester error: ${e}`);
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/background/approvals/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export const getHandler: (service: ApprovalsService) => Handler = (service) => {
const handleApproveTxMsg: (
service: ApprovalsService
) => InternalHandler<ApproveTxMsg> = (service) => {
return async (_, { txType, specificMsg, txMsg, accountType }) => {
return await service.approveTx(txType, specificMsg, txMsg, accountType);
return async (_, { txType, props, txProps, accountType }) => {
return await service.approveTx(txType, txProps, props, accountType);
};
};

Expand Down
164 changes: 82 additions & 82 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { fromBase64 } from "@cosmjs/encoding";
import { deserialize } from "@dao-xyz/borsh";
import BigNumber from "bignumber.js";
import { v4 as uuid } from "uuid";
import browser, { Windows } from "webextension-polyfill";
Expand All @@ -8,21 +6,20 @@ import { SupportedTx, TxType } from "@namada/shared";
import { KVStore } from "@namada/storage";
import {
AccountType,
BondMsgValue,
EthBridgeTransferMsgValue,
IbcTransferMsgValue,
BondProps,
EthBridgeTransferProps,
IbcTransferProps,
SignatureResponse,
TransferMsgValue,
TxMsgValue,
UnbondMsgValue,
VoteProposalMsgValue,
WithdrawMsgValue,
TransferProps,
TxProps,
UnbondProps,
VoteProposalProps,
WithdrawProps,
} from "@namada/types";

import { assertNever, paramsToUrl } from "@namada/utils";

import { KeyRingService, TabStore } from "background/keyring";
import { LedgerService } from "background/ledger";

import { VaultService } from "background/vault";
import { ApprovedOriginsStore, TxStore } from "./types";
import {
Expand All @@ -31,10 +28,7 @@ import {
removeApprovedOrigin,
} from "./utils";

type GetParams = (
specificMsg: Uint8Array,
txDetails: TxMsgValue
) => Record<string, string>;
type GetParams = (props: unknown, txProps: TxProps) => Record<string, string>;

export class ApprovalsService {
// holds promises which can be resolved with a message from a pop-up window
Expand Down Expand Up @@ -124,19 +118,12 @@ export class ApprovalsService {

async approveTx(
txType: SupportedTx,
txMsg: string,
specificMsg: string,
txProps: TxProps,
props: unknown,
type: AccountType
): Promise<void> {
const msgId = uuid();
await this.txStore.set(msgId, { txType, txMsg, specificMsg });

// Decode tx details and launch approval screen
const txMsgBuffer = Buffer.from(fromBase64(txMsg));
const txDetails = deserialize(txMsgBuffer, TxMsgValue);

const specificMsgBuffer = Buffer.from(fromBase64(specificMsg));

await this.txStore.set(msgId, { txType, txProps, props });
const getParams =
txType === TxType.Bond
? ApprovalsService.getParamsBond
Expand All @@ -159,26 +146,24 @@ export class ApprovalsService {
)}#/approve-tx/${txType}`;

const url = paramsToUrl(baseUrl, {
...getParams(specificMsgBuffer, txDetails),
...getParams(props, txProps),
msgId,
accountType: type,
});

this._launchApprovalWindow(url);
}

static getParamsTransfer: GetParams = (specificMsg, txDetails) => {
const specificDetails = deserialize(specificMsg, TransferMsgValue);

static getParamsTransfer: GetParams = (props, txProps) => {
const {
source,
target,
token: tokenAddress,
amount: amountBN,
} = specificDetails;
} = props as TransferProps;
const amount = new BigNumber(amountBN.toString());

const { publicKey = "", token: nativeToken } = txDetails;
const { publicKey = "", token: nativeToken } = txProps;

return {
source,
Expand All @@ -190,18 +175,16 @@ export class ApprovalsService {
};
};

static getParamsIbcTransfer: GetParams = (specificMsg, txDetails) => {
const specificDetails = deserialize(specificMsg, IbcTransferMsgValue);

static getParamsIbcTransfer: GetParams = (props, txProps) => {
const {
source,
receiver: target,
token: tokenAddress,
amount: amountBN,
} = specificDetails;
} = props as IbcTransferProps;
const amount = new BigNumber(amountBN.toString());

const { publicKey = "", token: nativeToken } = txDetails;
const { publicKey = "", token: nativeToken } = txProps;

return {
source,
Expand All @@ -213,17 +196,15 @@ export class ApprovalsService {
};
};

static getParamsEthBridgeTransfer: GetParams = (specificMsg, txDetails) => {
const specificDetails = deserialize(specificMsg, EthBridgeTransferMsgValue);

static getParamsEthBridgeTransfer: GetParams = (props, txProps) => {
const {
asset: tokenAddress,
recipient: target,
sender: source,
amount,
} = specificDetails;
} = props as EthBridgeTransferProps;

const { publicKey = "", token: nativeToken } = txDetails;
const { publicKey = "", token: nativeToken } = txProps;

return {
source,
Expand All @@ -235,17 +216,15 @@ export class ApprovalsService {
};
};

static getParamsBond: GetParams = (specificMsg, txDetails) => {
const specificDetails = deserialize(specificMsg, BondMsgValue);

static getParamsBond: GetParams = (props, txProps) => {
const {
source,
nativeToken: tokenAddress,
amount: amountBN,
} = specificDetails;
} = props as BondProps;
const amount = new BigNumber(amountBN.toString());

const { publicKey = "" } = txDetails;
const { publicKey = "" } = txProps;

return {
source,
Expand All @@ -256,13 +235,11 @@ export class ApprovalsService {
};
};

static getParamsUnbond: GetParams = (specificMsg, txDetails) => {
const specificDetails = deserialize(specificMsg, UnbondMsgValue);

const { source, amount: amountBN } = specificDetails;
static getParamsUnbond: GetParams = (props, txProps) => {
const { source, amount: amountBN } = props as UnbondProps;
const amount = new BigNumber(amountBN.toString());

const { publicKey = "", token: nativeToken } = txDetails;
const { publicKey = "", token: nativeToken } = txProps;

return {
source,
Expand All @@ -272,12 +249,10 @@ export class ApprovalsService {
};
};

static getParamsWithdraw: GetParams = (specificMsg, txDetails) => {
const specificDetails = deserialize(specificMsg, WithdrawMsgValue);

const { source, validator } = specificDetails;
static getParamsWithdraw: GetParams = (props, txProps) => {
const { source, validator } = props as WithdrawProps;

const { publicKey = "", token: nativeToken } = txDetails;
const { publicKey = "", token: nativeToken } = txProps;

return {
source,
Expand All @@ -287,12 +262,10 @@ export class ApprovalsService {
};
};

static getParamsVoteProposal: GetParams = (specificMsg, txDetails) => {
const specificDetails = deserialize(specificMsg, VoteProposalMsgValue);

const { signer } = specificDetails;
static getParamsVoteProposal: GetParams = (props, txProps) => {
const { signer } = props as VoteProposalProps;

const { publicKey = "", token: nativeToken } = txDetails;
const { publicKey = "", token: nativeToken } = txProps;

//TODO: check this
return {
Expand All @@ -316,26 +289,53 @@ export class ApprovalsService {
throw new Error("Pending tx not found!");
}

const { txType, specificMsg, txMsg } = tx;

const submitFn =
txType === TxType.Bond
? this.keyRingService.submitBond
: txType === TxType.Unbond
? this.keyRingService.submitUnbond
: txType === TxType.Transfer
? this.keyRingService.submitTransfer
: txType === TxType.IBCTransfer
? this.keyRingService.submitIbcTransfer
: txType === TxType.EthBridgeTransfer
? this.keyRingService.submitEthBridgeTransfer
: txType === TxType.Withdraw
? this.keyRingService.submitWithdraw
: txType === TxType.VoteProposal
? this.keyRingService.submitVoteProposal
: assertNever(txType);

await submitFn.call(this.keyRingService, specificMsg, txMsg, msgId);
const { txType, props, txProps } = tx;

switch (txType) {
case TxType.Bond:
this.keyRingService.submitBond(props as BondProps, txProps, msgId);
break;
case TxType.Unbond:
this.keyRingService.submitUnbond(props as UnbondProps, txProps, msgId);
break;
case TxType.Withdraw:
this.keyRingService.submitWithdraw(
props as WithdrawProps,
txProps,
msgId
);
break;
case TxType.Transfer:
this.keyRingService.submitTransfer(
props as TransferProps,
txProps,
msgId
);
break;
case TxType.IBCTransfer:
this.keyRingService.submitIbcTransfer(
props as IbcTransferProps,
txProps,
msgId
);
break;
case TxType.VoteProposal:
this.keyRingService.submitVoteProposal(
props as VoteProposalProps,
txProps,
msgId
);
break;
case TxType.EthBridgeTransfer:
this.keyRingService.submitEthBridgeTransfer(
props as EthBridgeTransferProps,
txProps,
msgId
);
break;
default:
assertNever(txType);
}

return await this._clearPendingTx(msgId);
}
Expand Down
5 changes: 3 additions & 2 deletions apps/extension/src/background/approvals/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SupportedTx } from "@namada/shared";
import { TxProps } from "@namada/types";

export type ApprovedOriginsStore = string[];

export type TxStore = {
txType: SupportedTx;
txMsg: string;
specificMsg: string;
txProps: TxProps;
props: unknown;
};
Loading

0 comments on commit a251d8b

Please sign in to comment.