1import Common, { Chain, Hardfork } from '@ethereumjs/common';
2import { JsonTx, Transaction } from '@ethereumjs/tx';
3import { ITxData } from '@walletconnect/types';
4import { toHex } from 'web3-utils';
5import { safeFromWei, safeToWei } from './lib';
6import retry from 'async-retry';
8import { NETWORK_TYPES } from './network';
9import { ledgerSignTransaction } from './ledger';
10import { trezorSignTransaction } from './trezor';
11import { walletConnectSignTransaction } from './WalletConnect';
12import { metamaskSignTransaction } from './metamask';
13import { addHexPrefix } from './utils/address';
14import { CHECK_BLOCK_EVERY_MS, WALLET_TYPES } from './constants';
15import Web3 from 'web3';
16import { TransactionReceipt } from 'web3-core';
17import BridgeWallet from './types/BridgeWallet';
19const RETRY_OPTIONS = {
22 minTimeout: CHECK_BLOCK_EVERY_MS,
26type SignableTx = Transaction & {
34// catch-all type: Metamask passes an obj shaped like ITxData, WC passes JsonTx + from
35export type FakeSignableTx =
37 | (JsonTx & { from: string; to: string; gas: string })
40type TxSender = (txn: FakeSignableTx, web3: Web3) => Promise<string>; // Metamask requires Web3 (txhash)
43 serialize: () => string;
46export type FakeSignedTx = SignedTx & {
51const FakeSignResult = (txn: FakeSignableTx, send: TxSender): FakeSignedTx => {
55 return '???'; //NOTE update tank.js when changing this!
61interface signTransactionProps {
67 nonce: number | string;
68 chainId: number | string;
71 txnSigner?: (args: ITxData) => Promise<string>;
72 txnSender?: (args: ITxData) => Promise<string>;
75const signTransaction = async ({
83 gasPrice, // string, in gwei
84 gasLimit, // string | number
85 txnSigner, // optionally inject a transaction signing function,
86 txnSender, // and a sending function, for wallets that need these passed in.
87}: signTransactionProps) => {
88 // TODO: require these in txn object
90 chainId = toHex(chainId);
91 gasPrice = toHex(safeToWei(gasPrice, 'gwei'));
92 gasLimit = toHex(gasLimit);
93 const from = wallet.address;
95 const txParams = { nonce, chainId, gasPrice, gasLimit, from };
99 // Ledger does not seem to handle EIP-155 automatically. When using a Ledger,
100 // if the block number is at least FORK_BLKNUM = 2675000, one needs to
101 // pre-set the ECDSA signature parameters with r = 0, s = 0, and v = chainId
104 // The easiest way to handle this is to just branch on the network, since
105 // mainnet and Ropsten have obviously passed FORK_BLKNUM. This is somewhat
106 // awkward when dealing with offline transactions, since we might want to
107 // test them on a local network as well.
109 // The best thing to do is probably to add an 'advanced' tab to offline
110 // transaction generation where one can disable the defaulted-on EIP-155
111 // settings in this case. This is pretty low-priority, but is a
112 // comprehensive solution.
116 // See https://github.com/LedgerHQ/ledgerjs/issues/43#issuecomment-366984725
118 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
120 const eip155Params = {
126 const defaultEip155Networks = [
127 NETWORK_TYPES.MAINNET,
128 NETWORK_TYPES.ROPSTEN,
129 NETWORK_TYPES.OFFLINE,
132 const needEip155Params =
133 walletType === WALLET_TYPES.LEDGER &&
134 defaultEip155Networks.includes(networkType);
136 const signingParams = needEip155Params
137 ? Object.assign(txParams, eip155Params)
140 const utx: SignableTx = Object.assign(txn, signingParams);
142 const txConfig: any = { freeze: false };
143 // we must specify the chain *either* in the Common object,
144 // *or* eip155 style, but never both!
146 if (!needEip155Params) {
148 networkType === NETWORK_TYPES.ROPSTEN ? Chain.Ropsten : Chain.Mainnet;
149 txConfig.common = new Common({
151 hardfork: Hardfork.MuirGlacier,
155 let stx: Transaction | FakeSignedTx = Transaction.fromTxData(utx, txConfig);
157 //TODO should try-catch and display error message to user,
158 // ie ledger's "pls enable contract data"
159 // needs to maybe happen at call-site though
160 if (walletType === WALLET_TYPES.LEDGER) {
161 await ledgerSignTransaction(stx, walletHdPath);
162 } else if (walletType === WALLET_TYPES.TREZOR) {
163 await trezorSignTransaction(stx, walletHdPath);
164 } else if (walletType === WALLET_TYPES.METAMASK) {
165 return metamaskSignTransaction(utx);
166 } else if (walletType === WALLET_TYPES.WALLET_CONNECT) {
167 if (!(txnSender && txnSigner)) {
168 throw Error('WalletConnect TX signer unavailable');
171 stx = await walletConnectSignTransaction({
179 stx = stx.sign(wallet.privateKey!);
185const sendSignedTransaction = (
187 stx: Transaction | FakeSignedTx,
188 doubtNonceError: boolean
189): Promise<string> => {
190 // if we couldn't sign it, we depend on the given sender function
191 if (!(stx instanceof Transaction)) {
192 if (doubtNonceError) {
193 console.log('why doubting nonce error? tank unavailable without rawtx.');
196 throw new Error('no sign+sending function available');
198 return stx.send(stx.txn, web3);
201 let rawTx: string = hexify(stx.serialize());
202 let eventEmitter = web3.eth.sendSignedTransaction(rawTx);
203 return new Promise(async (resolve, reject) => {
205 .on('transactionHash', (hash: string) => {
208 .on('error', err => {
209 // if there's a nonce error, but we used the gas tank, it's likely
210 // that it's because the tank already submitted our transaction.
211 // we just wait for first confirmation here.
212 const message = typeof err === 'string' ? err : err.message || '';
214 message.includes('known transaction') ||
215 message.includes('already known');
217 message.includes("the tx doesn't have the correct nonce.") ||
218 message.includes('nonce too low');
219 if (rawTx && (isKnownError || (doubtNonceError && isNonceError))) {
221 'tx send error likely from gas tank submission, ignoring:',
224 const txHash = web3.utils.keccak256(rawTx);
228 const wrappedErr = err instanceof Error ? err : new Error(message);
235// returns a Promise<void>, throwing on tx failure
236const waitForTransactionConfirm = (web3: Web3, txHash: string) => {
237 return retry(async (bail, n) => {
238 const receipt = await web3.eth.getTransactionReceipt(txHash);
239 const confirmed = receipt !== null;
241 throw new Error('Transaction not confirmed.');
244 const success = receipt.status === true;
246 return bail(new Error('Transaction failed.'));
252// returns a Promise that resolves when all stxs have been sent & confirmed
253const sendAndAwaitAll = (
255 stxs: Transaction[] | FakeSignedTx[],
256 doubtNonceError: boolean
259 stxs.map(async tx => {
260 const txHash = await sendSignedTransaction(web3, tx, doubtNonceError);
261 return await waitForTransactionConfirm(web3, txHash);
266const sendAndAwaitAllSerial = (
268 stxs: Array<Transaction> | Array<FakeSignedTx>,
269 doubtNonceError: boolean
271 // tsc complains that stxs.reduce() is not callable
274 (promise: Promise<TransactionReceipt[]>, stx: Transaction | FakeSignedTx) =>
275 promise.then(async (receipts = []) => {
276 const txHash = await sendSignedTransaction(web3, stx, doubtNonceError);
277 return [...receipts, await waitForTransactionConfirm(web3, txHash)];
283const sendTransactionsAndAwaitConfirm = async (
285 signedTxs: Array<Transaction> | Array<FakeSignedTx>,
288 Promise.all(signedTxs.map(tx => sendSignedTransaction(web3, tx, usedTank)));
290const hexify = (val: string | Buffer) => addHexPrefix(val.toString('hex'));
292const getTxnInfo = async (web3: Web3, addr: string) => {
293 let nonce = await web3.eth.getTransactionCount(addr);
294 let chainId = await web3.eth.net.getId();
295 let gasPrice = await web3.eth.getGasPrice();
300 gasPrice: safeFromWei(gasPrice, 'gwei'),
308 sendSignedTransaction,
309 waitForTransactionConfirm,
310 sendTransactionsAndAwaitConfirm,
312 sendAndAwaitAllSerial,
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