1import * as ob from 'urbit-ob';
2import { Just } from 'folktale/maybe';
3import Tx from 'ethereumjs-tx';
4import { toWei, fromWei, toHex } from 'web3-utils';
5import * as retry from 'async-retry';
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 RETRY_OPTIONS = {
18 minTimeout: CHECK_BLOCK_EVERY_MS,
23 SET_MANAGEMENT_PROXY: Symbol('SET_MANAGEMENT_PROXY'),
24 SET_TRANSFER_PROXY: Symbol('SET_TRANSFER_PROXY'),
25 SET_SPAWN_PROXY: Symbol('SET_SPAWN_PROXY'),
26 CREATE_GALAXY: Symbol('CREATE_GALAXY'),
27 ISSUE_CHILD: Symbol('ISSUE_CHILD'),
28 SET_KEYS: Symbol('SET_KEYS'),
29 TRANSFER: Symbol('TRANSFER'),
30 CANCEL_TRANSFER: Symbol('CANCEL_TRANSFER'),
33const renderTxnPurpose = purpose =>
34 purpose === TXN_PURPOSE.SET_MANAGEMENT_PROXY
35 ? 'set this management proxy'
36 : purpose === TXN_PURPOSE.SET_SPAWN_PROXY
37 ? 'set this spawn proxy'
38 : purpose === TXN_PURPOSE.SET_TRANSFER_PROXY
39 ? 'set this transfer proxy'
40 : purpose === TXN_PURPOSE.CREATE_GALAXY
41 ? 'create this galaxy'
42 : purpose === TXN_PURPOSE.ISSUE_CHILD
44 : purpose === TXN_PURPOSE.SET_KEYS
45 ? 'set these network keys'
46 : purpose === TXN_PURPOSE.TRANSFER
47 ? 'transfer this point'
48 : purpose === TXN_PURPOSE.CANCEL_TRANSFER
49 ? 'cancel this transfer'
50 : 'perform this transaction';
52const signTransaction = async config => {
67 chainId = toHex(chainId);
68 gasPrice = toHex(toWei(gasPrice, 'gwei'));
69 gasLimit = toHex(gasLimit);
71 const txParams = { nonce, chainId, gasPrice, gasLimit };
75 // Ledger does not seem to handle EIP-155 automatically. When using a Ledger,
76 // if the block number is at least FORK_BLKNUM = 2675000, one needs to
77 // pre-set the ECDSA signature parameters with r = 0, s = 0, and v = chainId
80 // The easiest way to handle this is to just branch on the network, since
81 // mainnet and Ropsten have obviously passed FORK_BLKNUM. This is somewhat
82 // awkward when dealing with offline transactions, since we might want to
83 // test them on a local network as well.
85 // The best thing to do is probably to add an 'advanced' tab to offline
86 // transaction generation where one can disable the defaulted-on EIP-155
87 // settings in this case. This is pretty low-priority, but is a
88 // comprehensive solution.
92 // See https://github.com/LedgerHQ/ledgerjs/issues/43#issuecomment-366984725
94 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
96 const eip155Params = {
102 const defaultEip155Networks = [
103 NETWORK_TYPES.MAINNET,
104 NETWORK_TYPES.ROPSTEN,
105 NETWORK_TYPES.OFFLINE,
108 const needEip155Params =
109 walletType === WALLET_TYPES.LEDGER &&
110 defaultEip155Networks.includes(networkType);
112 const signingParams = needEip155Params
113 ? Object.assign(txParams, eip155Params)
116 const utx = Object.assign(txn, signingParams);
118 const stx = new Tx(utx);
120 if (walletType === WALLET_TYPES.LEDGER) {
121 await ledgerSignTransaction(stx, walletHdPath);
122 } else if (walletType === WALLET_TYPES.TREZOR) {
123 await trezorSignTransaction(stx, walletHdPath);
125 stx.sign(wallet.privateKey);
132const sendSignedTransaction = (web3, stx, doubtNonceError) => {
133 const rawTx = hexify(stx.serialize());
135 return new Promise(async (resolve, reject) => {
137 .sendSignedTransaction(rawTx)
138 .on('transactionHash', hash => {
141 .on('error', err => {
142 // if there's a nonce error, but we used the gas tank, it's likely
143 // that it's because the tank already submitted our transaction.
144 // we just wait for first confirmation here.
146 const isKnownError = (err.message || '').includes(
147 'known transaction: '
149 const isNonceError = (err.message || '').includes(
150 "the tx doesn't have the correct nonce."
152 if (isKnownError || (doubtNonceError && isNonceError)) {
154 'tx send error likely from gas tank submission, ignoring'
156 const txHash = web3.utils.keccak256(rawTx);
159 reject(err.message || 'Transaction sending failed!');
165// returns a Promise<bool>, where the bool indicates tx success/failure
166const waitForTransactionConfirm = (web3, txHash) => {
167 return retry(async (bail, n) => {
168 const receipt = await web3.eth.getTransactionReceipt(txHash);
169 let confirmed = receipt !== null;
170 if (confirmed) return receipt.status === true;
171 else throw new Error('retrying');
175// returns a Promise that resolves when all stxs have been sent & confirmed
176const sendAndAwaitAll = (web3, stxs, doubtNonceError) => {
178 stxs.map(async tx => {
179 const txHash = await sendSignedTransaction(web3, tx, doubtNonceError);
180 await waitForTransactionConfirm(web3, txHash);
185const hexify = val => addHexPrefix(val.toString('hex'));
187const renderSignedTx = stx => ({
188 messageHash: hexify(stx.hash()),
192 rawTransaction: hexify(stx.serialize()),
195const getTxnInfo = async (web3, addr) => {
196 let nonce = await web3.eth.getTransactionCount(addr);
197 let chainId = await web3.eth.net.getId();
198 let gasPrice = await web3.eth.getGasPrice();
202 gasPrice: fromWei(gasPrice, 'gwei'),
206const canDecodePatp = p => {
218 sendSignedTransaction,
219 waitForTransactionConfirm,
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