on d760cbf help sign in
bridge/src/lib/trezor.js
33 lines 983 B history raw
1import TrezorConnect from 'trezor-connect';
3const TREZOR_PATH = "m/44'/60'/0'/0/x";
5const formatChainId = val =>
6 typeof val === 'number' ? val : parseInt(val.slice(2), 16); // assume 0x-prefixed hex string
8const trezorSignTransaction = async (txn, hdpath) => {
9 const trezorFormattedTxn = {
10 to: txn.to.toString('hex'),
11 value: txn.value.toString('hex'),
12 data: txn.data.toString('hex'),
13 gasLimit: txn.gasLimit.toString('hex'),
14 gasPrice: txn.gasPrice.toString('hex'),
15 nonce: txn.nonce.length === 0 ? '00' : txn.nonce.toString('hex'),
16 chainId: formatChainId(txn.getChainId()),
17 };
19 const sig = await TrezorConnect.ethereumSignTransaction({
20 path: hdpath,
21 transaction: trezorFormattedTxn,
22 });
24 const payload = sig.payload;
26 txn.v = Buffer.from(payload.v.slice(2), 'hex');
27 txn.r = Buffer.from(payload.r.slice(2), 'hex');
28 txn.s = Buffer.from(payload.s.slice(2), 'hex');
30 return txn;
31};
33export { TREZOR_PATH, trezorSignTransaction };