Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
netbonus committed Feb 20, 2025
1 parent 87d0871 commit d149992
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
17 changes: 6 additions & 11 deletions src/__test__/utils/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '../../constants';
import type { Currency, SignRequestParams, SigningPath } from '../../types';
import type { FirmwareConstants } from '../../types/firmware';
import type { ETH_MESSAGE_PROTOCOLS } from '../../types/sign';
import type { TestRequestPayload } from '../../types/utils';
import { randomBytes } from '../../util';
import { MSG_PAYLOAD_METADATA_SZ } from './constants';
Expand Down Expand Up @@ -302,10 +301,12 @@ export const buildEncDefs = (vectors: any) => {
});

// The calldata is already in hex format, we just need to ensure it has 0x prefix
const encDefsCalldata = vectors.canonicalNames.map((_: string, idx: number) => {
const calldata = `0x${idx.toString(16).padStart(8, '0')}`;
return calldata;
});
const encDefsCalldata = vectors.canonicalNames.map(
(_: string, idx: number) => {
const calldata = `0x${idx.toString(16).padStart(8, '0')}`;
return calldata;
},
);

return { encDefs, encDefsCalldata };
};
Expand Down Expand Up @@ -376,9 +377,3 @@ export function buildMockConnectedClient(opts) {
stateData: JSON.stringify(stateData),
});
}

export const buildEthMsgRequest = (protocol: ETH_MESSAGE_PROTOCOLS = 'signPersonal') => ({
protocol,
payload: Buffer.from('test message'),
signerPath: [0x80000000 + 44, 0x80000000 + 60, 0x80000000, 0, 0],
});
5 changes: 3 additions & 2 deletions src/api/signing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Transaction } from 'ethers';
import { serializeTransaction } from 'viem';
import { Constants } from '..';
import {
BTC_LEGACY_DERIVATION,
Expand All @@ -8,6 +8,7 @@ import {
DEFAULT_ETH_DERIVATION,
SOLANA_DERIVATION,
} from '../constants';
import { toViemTransaction } from '../ethereum';
import { fetchDecoder } from '../functions/fetchDecoder';
import {
BitcoinSignPayload,
Expand All @@ -23,7 +24,7 @@ export const sign = async (
transaction: TransactionRequest,
overrides?: SignRequestParams,
): Promise<SignData> => {
const serializedTx = Transaction.from(transaction).unsignedSerialized;
const serializedTx = serializeTransaction(toViemTransaction(transaction));

const payload: SigningPayload = {
signerPath: DEFAULT_ETH_DERIVATION,
Expand Down
18 changes: 13 additions & 5 deletions src/calldata/evm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { keccak256 } from 'js-sha3';
import { decodeAbiParameters, type Hex } from 'viem';

import { decodeAbiParameters, parseAbiParameters } from 'viem';
/**
* Look through an ABI definition to see if there is a function that matches the signature provided.
* @param sig a 0x-prefixed hex string containing 4 bytes of info
Expand Down Expand Up @@ -76,9 +75,18 @@ export const getNestedCalldata = function (def, calldata) {
// Skip past first item, which is the function name
const defParams = def.slice(1);
const strParams = getParamStrNames(defParams);
const abiParams = strParams.map((type) => ({ type }));
const hexData = `0x${calldata.slice(4).toString('hex')}` as Hex;
const decoded = decodeAbiParameters(abiParams, hexData);
const hexStr = ('0x' + calldata.slice(4).toString('hex')) as `0x${string}`;
// Convert strParams to viem's format
const viemParams = strParams.map((type) => {
// Convert tuple format from 'tuple(uint256,uint128)' to '(uint256,uint128)'
if (type.startsWith('tuple(')) {
return type.replace('tuple', '');
}
return type;
});

const abiParams = parseAbiParameters(viemParams.join(','));
const decoded = decodeAbiParameters(abiParams, hexStr);

function couldBeNestedDef(x) {
return (x.length - 4) % 32 === 0;
Expand Down
37 changes: 37 additions & 0 deletions src/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
} from './util';
import cbor from 'cbor';
import bdec from 'cbor-bigdecimal';
import { TransactionSerializable } from 'viem';
import { TRANSACTION_TYPE, TransactionRequest } from './types';

bdec(cbor);

const buildEthereumMsgRequest = function (input) {
Expand Down Expand Up @@ -983,6 +986,40 @@ const ethConvertLegacyToGenericReq = function (req) {
}
};

// Convert an ethers `TransactionRequest` to a viem `TransactionSerializable`
export const toViemTransaction = (
tx: TransactionRequest,
): TransactionSerializable => {
const base = {
to: tx.to as `0x${string}`,
value: tx.value ? BigInt(tx.value) : undefined,
data: tx.data as `0x${string}`,
nonce: tx.nonce,
gas: tx.gasLimit ? BigInt(tx.gasLimit) : undefined,
};

if (tx.type === TRANSACTION_TYPE.EIP1559) {
return {
...base,
type: 'eip1559',
maxFeePerGas: tx.maxFeePerGas ? BigInt(tx.maxFeePerGas) : undefined,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas
? BigInt(tx.maxPriorityFeePerGas)
: undefined,
chainId: tx.chainId,
accessList: tx.accessList?.map((item) => ({
address: item.address as `0x${string}`,
storageKeys: item.storageKeys as `0x${string}`[],
})),
};
}

return {
...base,
gasPrice: tx.maxFeePerGas ? BigInt(tx.maxFeePerGas) : undefined,
};
};

export default {
buildEthereumMsgRequest,
validateEthereumMsgResponse,
Expand Down
18 changes: 9 additions & 9 deletions src/types/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export const TRANSACTION_TYPE = {
};

export type TransactionRequest = {
to?: `0x${string}`;
value?: bigint;
data?: `0x${string}`;
chainId?: number;
nonce?: number;
gas?: bigint;
maxFeePerGas?: bigint;
maxPriorityFeePerGas?: bigint;
from?: `0x${string}`;
to: string;
value: string;
data: string;
chainId: number;
nonce: number;
gasLimit: string;
maxFeePerGas?: string;
maxPriorityFeePerGas?: string;
from?: string;
accessList?: Array<{ address: string; storageKeys: string[] }>;
type?: (typeof TRANSACTION_TYPE)[keyof typeof TRANSACTION_TYPE];
};
Expand Down

0 comments on commit d149992

Please sign in to comment.