1import * as ob from 'urbit-ob';
2import { Just } from 'folktale/maybe';
3import Tx from 'ethereumjs-tx';
4import { toWei, fromWei, toHex } from 'web3-utils';
6import { BRIDGE_ERROR } from './error';
7import { NETWORK_TYPES } from './network';
8import { ledgerSignTransaction } from './ledger';
9import { trezorSignTransaction } from './trezor';
10import { WALLET_TYPES, addHexPrefix } from './wallet';
12const CHECK_BLOCK_EVERY_MS =
13 process.env.NODE_ENV === 'development' ? 1000 : 5000;
15const signTransaction = async config => {
30 chainId = toHex(chainId);
31 gasPrice = toHex(toWei(gasPrice, 'gwei'));
32 gasLimit = toHex(gasLimit);
34 const txParams = { nonce, chainId, gasPrice, gasLimit };
38 // Ledger does not seem to handle EIP-155 automatically. When using a Ledger,
39 // if the block number is at least FORK_BLKNUM = 2675000, one needs to
40 // pre-set the ECDSA signature parameters with r = 0, s = 0, and v = chainId
43 // The easiest way to handle this is to just branch on the network, since
44 // mainnet and Ropsten have obviously passed FORK_BLKNUM. This is somewhat
45 // awkward when dealing with offline transactions, since we might want to
46 // test them on a local network as well.
48 // The best thing to do is probably to add an 'advanced' tab to offline
49 // transaction generation where one can disable the defaulted-on EIP-155
50 // settings in this case. This is pretty low-priority, but is a
51 // comprehensive solution.
55 // See https://github.com/LedgerHQ/ledgerjs/issues/43#issuecomment-366984725
57 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
59 const eip155Params = {
65 const defaultEip155Networks = [
66 NETWORK_TYPES.MAINNET,
67 NETWORK_TYPES.ROPSTEN,
68 NETWORK_TYPES.OFFLINE,
71 const needEip155Params =
72 walletType === WALLET_TYPES.LEDGER &&
73 defaultEip155Networks.includes(networkType);
75 const signingParams = needEip155Params
76 ? Object.assign(txParams, eip155Params)
79 const wal = wallet.matchWith({
82 throw BRIDGE_ERROR.MISSING_WALLET;
86 const sec = wal.privateKey;
88 const utx = txn.matchWith({
89 Just: tx => Object.assign(tx.value, signingParams),
91 throw BRIDGE_ERROR.MISSING_TXN;
95 const stx = new Tx(utx);
97 if (walletType === WALLET_TYPES.LEDGER) {
98 await ledgerSignTransaction(stx, walletHdPath);
99 } else if (walletType === WALLET_TYPES.TREZOR) {
100 await trezorSignTransaction(stx, walletHdPath);
109const sendSignedTransaction = (web3, stx, doubtNonceError, confirmationCb) => {
110 const txn = stx.matchWith({
111 Just: tx => tx.value,
113 throw BRIDGE_ERROR.MISSING_TXN;
117 const rawTx = hexify(txn.serialize());
119 return new Promise(async (resolve, reject) => {
121 .sendSignedTransaction(rawTx)
122 .on('transactionHash', hash => {
125 //TODO do we also reach this if network is slow? web3 only tries a set
126 // amount of times... should we instead do waitForTransactionConfirm
127 // in on-transactionHash? we don't care (much) about additional
129 .on('confirmation', (confirmationNum, txn) => {
130 confirmationCb(txn.transactionHash, confirmationNum + 1);
131 resolve(txn.transactionHash);
133 .on('error', err => {
134 // if there's a nonce error, but we used the gas tank, it's likely
135 // that it's because the tank already submitted our transaction.
136 // we just wait for first confirmation here.
140 (err.message || '').includes("the tx doesn't have the correct nonce.")
142 console.log('nonce error, likely from gas tank submission, ignoring');
143 const txHash = web3.utils.keccak256(rawTx);
144 //TODO can we do does-exists check first?
145 //TODO max wait time before assume fail?
146 waitForTransactionConfirm(web3, txHash).then(res => {
149 confirmationCb(txHash, 1);
151 reject('Unexpected tx failure');
161// returns a Promise<bool>, where the bool indicates tx success/failure
162const waitForTransactionConfirm = (web3, txHash) => {
163 return new Promise((resolve, reject) => {
164 const checkForConfirm = async () => {
165 console.log('checking for confirm', txHash);
166 const receipt = await web3.eth.getTransactionReceipt(txHash);
167 console.log('tried, got', receipt);
168 let confirmed = receipt !== null;
169 if (confirmed) resolve(receipt.status === true);
170 else setTimeout(checkForConfirm, CHECK_BLOCK_EVERY_MS);
176const isTransactionConfirmed = async (web3, txHash) => {
177 const receipt = await web3.eth.getTransactionReceipt(txHash);
178 console.log('got confirm state', receipt !== null, receipt.confirmations);
179 return receipt !== null;
182async function sendAndAwaitConfirm(web3, signedTxs, usedTank) {
184 signedTxs.map(tx => {
185 return new Promise((resolve, reject) => {
186 sendSignedTransaction(web3, tx, usedTank, resolve);
192const hexify = val => addHexPrefix(val.toString('hex'));
194const renderSignedTx = stx => ({
195 messageHash: hexify(stx.hash()),
199 rawTransaction: hexify(stx.serialize()),
202const getTxnInfo = async (web3, addr) => {
203 let nonce = await web3.eth.getTransactionCount(addr);
204 let chainId = await web3.eth.net.getId();
205 let gasPrice = await web3.eth.getGasPrice();
209 gasPrice: fromWei(gasPrice, 'gwei'),
213// TODO(shrugs): deprecate, unifiy with other callsites
214const canDecodePatp = p => {
225 sendSignedTransaction,
226 waitForTransactionConfirm,
228 isTransactionConfirmed,
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list