on 05c1914 help sign in
bridge/src/lib/trezor.js
39 lines 990 B history raw
1import TrezorConnect from 'trezor-connect'
3const TREZOR_BASE_PATH = "m/44'/60'/0'/0/0"
5const formatChainId = val =>
6 typeof val === 'number'
7 ? val
8 : parseInt(val.slice(2), 16) // assume 0x-prefixed hex string
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 const payload = sig.payload
28 txn.v = Buffer.from(payload.v.slice(2), 'hex')
29 txn.r = Buffer.from(payload.r.slice(2), 'hex')
30 txn.s = Buffer.from(payload.s.slice(2), 'hex')
32 return txn
35export {
36 TREZOR_BASE_PATH,
37 trezorSignTransaction