Skip to content

Commit

Permalink
automatically fetches calldata from sign() (#579)
Browse files Browse the repository at this point in the history
* automatically fetches calldata from `sign()`

* remove vitest form tsconfig to fix build

* remove extra script
  • Loading branch information
netbonus authored Oct 22, 2024
1 parent f24fbd1 commit dab6cf5
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 84 deletions.
100 changes: 66 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
"js-sha3": "^0.9.3"
},
"dependencies": {
"@ethereumjs/common": "4.3.0",
"@ethereumjs/common": "4.4.0",
"@ethereumjs/rlp": "^5.0.2",
"@ethereumjs/tx": "5.3.0",
"@ethereumjs/tx": "5.4.0",
"@ethersproject/abi": "^5.7.0",
"@metamask/eth-sig-util": "^7.0.3",
"@metamask/eth-sig-util": "^8.0.0",
"@types/uuid": "^10.0.0",
"aes-js": "^3.1.2",
"bech32": "^2.0.0",
Expand All @@ -66,7 +66,7 @@
"elliptic": "6.5.6",
"hash.js": "^1.1.7",
"js-sha3": "^0.9.3",
"secp256k1": "5.0.0",
"secp256k1": "5.0.1",
"uuid": "^10.0.0"
},
"devDependencies": {
Expand Down Expand Up @@ -106,7 +106,7 @@
"random-words": "^1.1.1",
"readline-sync": "^1.4.9",
"seedrandom": "^3.0.5",
"typescript": "^4.7.0",
"typescript": "^5.6.3",
"vite": "^3.0.2",
"vitest": "^0.15.2"
},
Expand Down
28 changes: 10 additions & 18 deletions src/__test__/e2e/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable quotes */
import { getClient } from './../../api/utilities';
import { Chain, Common, Hardfork } from '@ethereumjs/common';
import { TransactionFactory } from '@ethereumjs/tx';
import { question } from 'readline-sync';
import { RLP } from '@ethereumjs/rlp';
import {
Expand Down Expand Up @@ -98,25 +96,19 @@ describe('API', () => {
describe('transactions', () => {
const txData = {
type: 1,
maxFeePerGas: 1200000000,
maxPriorityFeePerGas: 1200000000,
chainId: 1,
maxFeePerGas: '1200000000',
maxPriorityFeePerGas: '1200000000',
nonce: 0,
gasLimit: 50000,
to: '0xe242e54155b1abc71fc118065270cecaaf8b7768',
value: 1000000000000,
data: '0x17e914679b7e160613be4f8c2d3203d236286d74eb9192f6d6f71b9118a42bb033ccd8e8',
gasPrice: 1200000000,
};
gasLimit: '50000',
to: '0x7a250d5630b4cf539739df2c5dacb4c659f2488d',
value: '1000000000000',
data: '0x38ed17390000000000000000000000000000000000000000000c1c173c5b782a5b154ab900000000000000000000000000000000000000000000000f380d77022fe8c32600000000000000000000000000000000000000000000000000000000000000a00000000000000000000000007ae7684581f0298241c3d6a6567a48d56b42b15c00000000000000000000000000000000000000000000000000000000622f8d27000000000000000000000000000000000000000000000000000000000000000300000000000000000000000095ad61b0a150d79219dcf64e1e6cc01f0b64c4ce000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000050522c769e01eb06c02bd299066509d8f97a69ae',
gasPrice: '1200000000',
} as const;

test('generic', async () => {
const common = new Common({
chain: Chain.Mainnet,
hardfork: Hardfork.London,
});
const tx = TransactionFactory.fromTxData(txData, { common });
const payload = tx.getMessageToSign(false);

await sign(payload);
await sign(txData);
});

test('legacy', async () => {
Expand Down
46 changes: 25 additions & 21 deletions src/api/signing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ethers } from 'ethers';
import { Constants } from '..';
import {
BTC_LEGACY_DERIVATION,
Expand All @@ -7,30 +8,33 @@ import {
DEFAULT_ETH_DERIVATION,
SOLANA_DERIVATION,
} from '../constants';
import { fetchDecoder } from '../functions/fetchDecoder';
import {
SignRequestParams,
SignData,
EIP712MessagePayload,
BitcoinSignPayload,
Currency,
EIP712MessagePayload,
SignData,
SigningPayload,
SignRequestParams,
TransactionRequest,
} from '../types';
import { isEIP712Payload, queue } from './utilities';

export const sign = async (
payload: Uint8Array | Buffer | Buffer[],
transaction: TransactionRequest,
overrides?: SignRequestParams,
): Promise<SignData> => {
const tx: SignRequestParams = {
data: {
signerPath: DEFAULT_ETH_DERIVATION,
curveType: Constants.SIGNING.CURVES.SECP256K1,
hashType: Constants.SIGNING.HASHES.KECCAK256,
encodingType: Constants.SIGNING.ENCODINGS.EVM,
payload,
},
...overrides,
const serializedTx = ethers.utils.serializeTransaction(transaction);

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: await fetchDecoder(transaction),
};
return queue((client) => client.sign(tx));

return queue((client) => client.sign({ data: payload, ...overrides }));
};

export const signMessage = async (
Expand All @@ -45,9 +49,9 @@ export const signMessage = async (
protocol: 'signPersonal',
payload,
...overrides,
},
currency: CURRENCIES.ETH_MSG as Currency,
} as SignRequestParams & { data: {protocol: string}};
} as SigningPayload,
currency: CURRENCIES.ETH_MSG,
};

if (isEIP712Payload(payload)) {
tx.data.protocol = 'eip712';
Expand All @@ -64,7 +68,7 @@ export const signBtcLegacyTx = async (
signerPath: BTC_LEGACY_DERIVATION,
...payload,
},
currency: 'BTC' as Currency,
currency: CURRENCIES.BTC,
};
return queue((client) => client.sign(tx));
};
Expand All @@ -77,7 +81,7 @@ export const signBtcSegwitTx = async (
signerPath: BTC_SEGWIT_DERIVATION,
...payload,
},
currency: 'BTC'as Currency,
currency: CURRENCIES.BTC,
};
return queue((client) => client.sign(tx));
};
Expand All @@ -90,7 +94,7 @@ export const signBtcWrappedSegwitTx = async (
signerPath: BTC_WRAPPED_SEGWIT_DERIVATION,
...payload,
},
currency: 'BTC'as Currency,
currency: CURRENCIES.BTC,
};
return queue((client) => client.sign(tx));
};
Expand Down
Loading

0 comments on commit dab6cf5

Please sign in to comment.