Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
netbonus committed Feb 20, 2025
1 parent 3d307ba commit 2d22afa
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 217 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"hash.js": "^1.1.7",
"js-sha3": "^0.9.3",
"lodash": "^4.17.21",
"ox": "^0.6.9",
"secp256k1": "5.0.1",
"uuid": "^10.0.0",
"viem": "^2.22.19"
Expand Down
64 changes: 29 additions & 35 deletions src/__test__/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import * as fs from 'fs';
import { question } from 'readline-sync';
import { getClient, pair, setup } from '../..';
import * as dotenv from 'dotenv';
import { expect } from 'vitest';
import { Client } from '../../client';
import { getEnv } from './getters';
import { setupTestClient } from './helpers';

dotenv.config();

if (!globalThis.fetch) {
Expand All @@ -17,10 +12,22 @@ if (!globalThis.fetch) {
globalThis.Request = Request;
}

let storedClient: Client | null = null;
expect.extend({
toEqualElseLog(received: any, expected: any, message: string) {
return {
pass: received === expected,
message: () =>
message ? message : `Expected ${received} to equal ${expected}`,
};
},
});

export const setStoredClient = (client: Client) => {
storedClient = client;
export const setStoredClient = async (data: string) => {
try {
fs.writeFileSync('./client.temp', data);
} catch (err) {
return;
}
};

export const getStoredClient = async () => {
Expand All @@ -32,32 +39,19 @@ export const getStoredClient = async () => {
};

export const setupClient = async () => {
if (storedClient) {
return storedClient;
}
const env = getEnv();
const client = setupTestClient(env);
if (env.DEVICE_ID) {
await client.connect(env.DEVICE_ID);
const deviceId = process.env.DEVICE_ID;
const password = process.env.PASSWORD || 'password';
const name = process.env.APP_NAME || 'SDK Test';
const isPaired = await setup({
deviceId,
password,
name,
getStoredClient,
setStoredClient,
});
if (!isPaired) {
const secret = question('Please enter the pairing secret: ');
await pair(secret.toUpperCase());
}
return client;
return getClient();
};

// Add custom matchers for vitest
expect.extend({
toEqualElseLog(received: any, expected: any, message?: string) {
const pass = this.equals(received, expected);
if (pass) {
return {
message: () => 'Values are equal',
pass: true,
};
} else {
console.error(message || `Expected ${expected} but received ${received}`);
return {
message: () => message || `Expected ${expected} but received ${received}`,
pass: false,
};
}
},
});
144 changes: 0 additions & 144 deletions src/__test__/utils/viem.ts

This file was deleted.

33 changes: 9 additions & 24 deletions src/api/signing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer } from 'buffer';
import { Transaction } from 'ethers';
import { Constants } from '..';
import {
BTC_LEGACY_DERIVATION,
Expand All @@ -18,27 +18,20 @@ import {
TransactionRequest,
} from '../types';
import { isEIP712Payload, queue } from './utilities';
import {
encodeViemTransaction,
encodeViemTypedData,
encodeViemPersonalMessage,
} from '../calldata/evm';

export const sign = async (
transaction: TransactionRequest,
overrides?: SignRequestParams,
): Promise<SignData> => {
const serializedTx = encodeViemTransaction(transaction);

const { def } = await fetchDecoder(transaction);
const serializedTx = Transaction.from(transaction).unsignedSerialized;

const payload: SigningPayload = {
signerPath: DEFAULT_ETH_DERIVATION,
curveType: Constants.SIGNING.CURVES.SECP256K1,
hashType: Constants.SIGNING.HASHES.KECCAK256,
encodingType: Constants.SIGNING.ENCODINGS.EVM,
payload: serializedTx,
decoder: def,
decoder: await fetchDecoder(transaction),
};

return queue((client) => client.sign({ data: payload, ...overrides }));
Expand All @@ -48,30 +41,22 @@ export const signMessage = async (
payload: string | Uint8Array | Buffer | Buffer[] | EIP712MessagePayload,
overrides?: SignRequestParams,
): Promise<SignData> => {
let processedPayload = payload;
let protocol = 'signPersonal';

if (isEIP712Payload(payload)) {
protocol = 'eip712';
processedPayload = encodeViemTypedData(payload as any);
} else if (typeof payload !== 'string') {
processedPayload = encodeViemPersonalMessage(
payload as Buffer | Uint8Array,
);
}

const tx = {
data: {
signerPath: DEFAULT_ETH_DERIVATION,
curveType: Constants.SIGNING.CURVES.SECP256K1,
hashType: Constants.SIGNING.HASHES.KECCAK256,
protocol,
payload: processedPayload,
protocol: 'signPersonal',
payload,
...overrides,
} as SigningPayload,
currency: CURRENCIES.ETH_MSG,
};

if (isEIP712Payload(payload)) {
tx.data.protocol = 'eip712';
}

return queue((client) => client.sign(tx));
};

Expand Down
6 changes: 2 additions & 4 deletions src/calldata/evm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { keccak256 } from 'js-sha3';
import { decodeAbiParameters, parseAbiParameters, type Hex } from 'viem';
import { decodeAbiParameters, type Hex } from 'viem';

/**
* Look through an ABI definition to see if there is a function that matches the signature provided.
Expand Down Expand Up @@ -76,11 +76,9 @@ export const getNestedCalldata = function (def, calldata) {
// Skip past first item, which is the function name
const defParams = def.slice(1);
const strParams = getParamStrNames(defParams);

// Convert string params to viem ABI parameters format
const abiParams = strParams.map((type) => ({ type }));
const hexData = `0x${calldata.slice(4).toString('hex')}` as Hex;
const decoded = decodeAbiParameters(abiParams, hexData) as Array<any>;
const decoded = decodeAbiParameters(abiParams, hexData);

function couldBeNestedDef(x) {
return (x.length - 4) % 32 === 0;
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: string;
value: string;
data: string;
chainId: number;
nonce: number;
gasLimit: string;
maxFeePerGas?: string;
maxPriorityFeePerGas?: string;
from?: string;
to?: `0x${string}`;
value?: bigint;
data?: `0x${string}`;
chainId?: number;
nonce?: number;
gas?: bigint;
maxFeePerGas?: bigint;
maxPriorityFeePerGas?: bigint;
from?: `0x${string}`;
accessList?: Array<{ address: string; storageKeys: string[] }>;
type?: (typeof TRANSACTION_TYPE)[keyof typeof TRANSACTION_TYPE];
};
Expand Down

0 comments on commit 2d22afa

Please sign in to comment.