Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 26, 2023
1 parent a16b480 commit fb2bd33
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 44 deletions.
40 changes: 3 additions & 37 deletions apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fromBase64 } from "@cosmjs/encoding";
import { deserialize } from "@dao-xyz/borsh";

import { AccountType, Bip44Path, TransferMsgValue } from "@namada/types";
import { AccountType, Bip44Path } from "@namada/types";
import { ResponseSign } from "@namada/ledger-namada";
import { Sdk, TxType } from "@namada/shared";
import { IStore, KVStore, Store } from "@namada/storage";
Expand All @@ -14,7 +14,7 @@ import {
TabStore,
syncTabs,
} from "background/keyring";
import { encodeSignature, generateId, getEncodedTx } from "utils";
import { encodeSignature, generateId, getEncodedTxByType } from "utils";
import { ExtensionRequester } from "extension";
import { Ports } from "router";
import { UpdatedStakingEventMsg } from "content/events";
Expand Down Expand Up @@ -90,40 +90,6 @@ export class LedgerService {
}
}

async getTransferBytes(
msgId: string
): Promise<{ bytes: Uint8Array; path: string }> {
const txMsg = await this.txStore.get(msgId);
const { coinType } = chains[this.chainId].bip44;

if (!txMsg) {
throw new Error(`Transaction ${msgId} not found!`);
}

try {
// Deserialize txMsg to retrieve source
const { source } = deserialize(
Buffer.from(fromBase64(txMsg)),
TransferMsgValue
);

// Query account from Ledger storage to determine path for signer
const account = await this._ledgerStore.getRecord("address", source);

if (!account) {
throw new Error(`Ledger account not found for ${source}`);
}

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

return { bytes, path };
} catch (e) {
console.warn(e);
throw new Error(`${e}`);
}
}

async submitTx(
txType: TxType,
msgId: string,
Expand All @@ -136,7 +102,7 @@ export class LedgerService {
throw new Error(`Transaction ${msgId} not found!`);
}

const encodedTx = getEncodedTx(txType, txMsg);
const encodedTx = getEncodedTxByType(txType, txMsg);
const { wrapperSignature, rawSignature } = signatures;

// Serialize signatures
Expand Down
12 changes: 6 additions & 6 deletions apps/extension/src/hooks/useQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { sanitize } from "dompurify";

interface SanitizedURLSearchParams {
get(name: string): string | null;
};
}

const toSanitized = (urlSearchParams: URLSearchParams): SanitizedURLSearchParams => ({
const toSanitized = (
urlSearchParams: URLSearchParams
): SanitizedURLSearchParams => ({
get: (name: string): string | null => {
const unsanitized = urlSearchParams.get(name);
return unsanitized === null ?
unsanitized :
sanitize(unsanitized);
}
return unsanitized === null ? unsanitized : sanitize(unsanitized);
},
});

const useQuery = (): SanitizedURLSearchParams => {
Expand Down
8 changes: 7 additions & 1 deletion apps/extension/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ export const encodeTx = (tx: TxProps): Uint8Array => {
return msg.encode(txMsgValue);
};

export const getEncodedTx = (txType: TxType, txMsg: string): Uint8Array => {
/**
* Helper to get encoded Tx information by TxType
*/
export const getEncodedTxByType = (
txType: TxType,
txMsg: string
): Uint8Array => {
switch (txType) {
case TxType.Transfer: {
const { tx } = deserialize(fromBase64(txMsg), TransferMsgValue);
Expand Down

0 comments on commit fb2bd33

Please sign in to comment.