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 kCheckBlockEveryMs = process.env.NODE_ENV === 'development' ? 1000 : 5000;
15 SET_MANAGEMENT_PROXY: Symbol('SET_MANAGEMENT_PROXY'),
16 SET_TRANSFER_PROXY: Symbol('SET_TRANSFER_PROXY'),
17 SET_SPAWN_PROXY: Symbol('SET_SPAWN_PROXY'),
18 CREATE_GALAXY: Symbol('CREATE_GALAXY'),
19 ISSUE_CHILD: Symbol('ISSUE_CHILD'),
20 SET_KEYS: Symbol('SET_KEYS'),
21 TRANSFER: Symbol('TRANSFER'),
22 CANCEL_TRANSFER: Symbol('CANCEL_TRANSFER'),
25const renderTxnPurpose = purpose =>
26 purpose === TXN_PURPOSE.SET_MANAGEMENT_PROXY
27 ? 'set this management proxy'
28 : purpose === TXN_PURPOSE.SET_SPAWN_PROXY
29 ? 'set this spawn proxy'
30 : purpose === TXN_PURPOSE.SET_TRANSFER_PROXY
31 ? 'set this transfer proxy'
32 : purpose === TXN_PURPOSE.CREATE_GALAXY
33 ? 'create this galaxy'
34 : purpose === TXN_PURPOSE.ISSUE_CHILD
36 : purpose === TXN_PURPOSE.SET_KEYS
37 ? 'set these network keys'
38 : purpose === TXN_PURPOSE.TRANSFER
39 ? 'transfer this point'
40 : purpose === TXN_PURPOSE.CANCEL_TRANSFER
41 ? 'cancel this transfer'
42 : 'perform this transaction';
44const signTransaction = async config => {
59 chainId = toHex(chainId);
60 gasPrice = toHex(toWei(gasPrice, 'gwei'));
61 gasLimit = toHex(gasLimit);
63 const txParams = { nonce, chainId, gasPrice, gasLimit };
67 // Ledger does not seem to handle EIP-155 automatically. When using a Ledger,
68 // if the block number is at least FORK_BLKNUM = 2675000, one needs to
69 // pre-set the ECDSA signature parameters with r = 0, s = 0, and v = chainId
72 // The easiest way to handle this is to just branch on the network, since
73 // mainnet and Ropsten have obviously passed FORK_BLKNUM. This is somewhat
74 // awkward when dealing with offline transactions, since we might want to
75 // test them on a local network as well.
77 // The best thing to do is probably to add an 'advanced' tab to offline
78 // transaction generation where one can disable the defaulted-on EIP-155
79 // settings in this case. This is pretty low-priority, but is a
80 // comprehensive solution.
84 // See https://github.com/LedgerHQ/ledgerjs/issues/43#issuecomment-366984725
86 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
88 const eip155Params = {
94 const defaultEip155Networks = [
95 NETWORK_TYPES.MAINNET,
96 NETWORK_TYPES.ROPSTEN,
97 NETWORK_TYPES.OFFLINE,
100 const needEip155Params =
101 walletType === WALLET_TYPES.LEDGER &&
102 defaultEip155Networks.includes(networkType);
104 const signingParams = needEip155Params
105 ? Object.assign(txParams, eip155Params)
108 const wal = wallet.matchWith({
111 throw BRIDGE_ERROR.MISSING_WALLET;
115 const sec = wal.privateKey;
117 const utx = txn.matchWith({
118 Just: tx => Object.assign(tx.value, signingParams),
120 throw BRIDGE_ERROR.MISSING_TXN;
124 const stx = new Tx(utx);
126 if (walletType === WALLET_TYPES.LEDGER) {
127 await ledgerSignTransaction(stx, walletHdPath);
128 } else if (walletType === WALLET_TYPES.TREZOR) {
129 await trezorSignTransaction(stx, walletHdPath);
138const sendSignedTransaction = (web3, stx, doubtNonceError, confirmationCb) => {
139 const txn = stx.matchWith({
140 Just: tx => tx.value,
142 throw BRIDGE_ERROR.MISSING_TXN;
146 const rawTx = addHexPrefix(txn.serialize().toString('hex'));
148 return new Promise(async (resolve, reject) => {
150 .sendSignedTransaction(rawTx)
151 .on('transactionHash', hash => {
154 //TODO do we also reach this if network is slow? web3 only tries a set
155 // amount of times... should we instead do waitForTransactionConfirm
156 // in on-transactionHash? we don't care (much) about additional
158 .on('confirmation', (confirmationNum, txn) => {
159 confirmationCb(txn.transactionHash, confirmationNum + 1);
160 resolve(txn.transactionHash);
162 .on('error', err => {
163 // if there's a nonce error, but we used the gas tank, it's likely
164 // that it's because the tank already submitted our transaction.
165 // we just wait for first confirmation here.
169 (err.message || '').includes("the tx doesn't have the correct nonce.")
171 console.log('nonce error, likely from gas tank submission, ignoring');
172 const txHash = web3.utils.keccak256(rawTx);
173 //TODO can we do does-exists check first?
174 //TODO max wait time before assume fail?
175 waitForTransactionConfirm(web3, txHash).then(res => {
178 confirmationCb(txHash, 1);
180 reject('Unexpected tx failure');
190// returns a Promise<bool>, where the bool indicates tx success/failure
191const waitForTransactionConfirm = (web3, txHash) => {
192 return new Promise((resolve, reject) => {
193 const checkForConfirm = async () => {
194 console.log('checking for confirm', txHash);
195 const receipt = await web3.eth.getTransactionReceipt(txHash);
196 console.log('tried, got', receipt);
197 let confirmed = receipt !== null;
198 if (confirmed) resolve(receipt.status === true);
199 else setTimeout(checkForConfirm, kCheckBlockEveryMs);
205const isTransactionConfirmed = async (web3, txHash) => {
206 const receipt = await web3.eth.getTransactionReceipt(txHash);
207 console.log('got confirm state', receipt !== null, receipt.confirmations);
208 return receipt !== null;
211const hexify = val => addHexPrefix(val.toString('hex'));
213const renderSignedTx = stx => ({
214 messageHash: hexify(stx.hash()),
218 rawTransaction: hexify(stx.serialize()),
221const getTxnInfo = async (web3, addr) => {
222 let nonce = await web3.eth.getTransactionCount(addr);
223 let chainId = await web3.eth.net.getId();
224 let gasPrice = await web3.eth.getGasPrice();
228 gasPrice: fromWei(gasPrice, 'gwei'),
232const canDecodePatp = p => {
243 sendSignedTransaction,
244 waitForTransactionConfirm,
245 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