on 4c8c607 help sign in
bridge/src/lib/trezor.js
54 lines 1.4 kB history raw
1import TrezorConnect from 'trezor-connect';
2import convertToInt from './convertToInt';
4const TREZOR_PATH = "m/44'/60'/0'/0/x";
6// handles 0x-prefixed hex string as well as number type
7const formatChainId = val =>
8 typeof val === 'number' ? val : convertToInt(val.slice(2), 16);
10const trezorSignTransaction = async (txn, hdpath) => {
11 const trezorFormattedTxn = {
12 to: txn.to.toString('hex'),
13 value: txn.value.toString('hex'),
14 data: txn.data.toString('hex'),
15 gasLimit: txn.gasLimit.toString('hex'),
16 gasPrice: txn.gasPrice.toString('hex'),
17 nonce: txn.nonce.length === 0 ? '00' : txn.nonce.toString('hex'),
18 chainId: formatChainId(txn.getChainId()),
19 };
21 const sig = await TrezorConnect.ethereumSignTransaction({
22 path: hdpath,
23 transaction: trezorFormattedTxn,
24 });
26 if (!sig.success) {
27 throw new Error(sig.payload.error);
28 }
30 const payload = sig.payload;
32 txn.v = Buffer.from(payload.v.slice(2), 'hex');
33 txn.r = Buffer.from(payload.r.slice(2), 'hex');
34 txn.s = Buffer.from(payload.s.slice(2), 'hex');
36 return txn;
37};
39export const trezorSignMessage = async (message, hdPath) => {
40 const sig = await TrezorConnect.ethereumSignMessage({
41 path: hdPath,
42 message,
43 });
45 if (!sig.success) {
46 throw new Error(sig.payload.error);
47 }
49 const { signature } = sig.payload;
51 return signature;
52};
54export { TREZOR_PATH, trezorSignTransaction };