on master help sign in
txn: always throw an Error on failure

fixes: #478 #481

commitd20b79d429ffc18d0f3b6a9d6088b1c130a559f1 authorLiam Fitzgerald <liam@urizen.co> dateSat 27 Jun 2020, 13:24 (6 years ago) parent30f9f96
2 files changed+19−25
src/lib/metamask.js+8−7 src/lib/txn.js+11−18
src/lib/metamask.js+8−7
@@ -1,3 +1,5 @@
1+import { toHex } from 'web3-utils';
2+
13export function MetamaskWallet(address) {
24 this.address = address;
35}
@@ -14,13 +16,12 @@ export function FakeMetamaskTransaction(txnData) {
1416// Sign it actually when sending
1517export const metamaskSignTransaction = async (txn, from) => {
1618 const metamaskFormattedTxn = {
17− to: txn.to.toString('hex'),
18− value: txn.value.toString('hex'),
19− data: txn.data.toString('hex'),
20− gasLimit: txn.gasLimit.toString('hex'),
21− gasPrice: txn.gasPrice.toString('hex'),
22− nonce: txn.nonce.length === 0 ? '00' : txn.nonce.toString('hex'),
23− from: from,
19+ data: txn.data,
20+ gasLimit: txn.gasLimit,
21+ gasPrice: txn.gasPrice,
22+ nonce: txn.nonce,
23+ to: toHex(txn.to),
24+ from: toHex(from),
2425 };
2526
2627 return new FakeMetamaskTransaction(metamaskFormattedTxn);
src/lib/txn.js+11−18
@@ -92,7 +92,7 @@ const signTransaction = async ({
9292 } else if (walletType === WALLET_TYPES.TREZOR) {
9393 await trezorSignTransaction(stx, walletHdPath);
9494 } else if (walletType === WALLET_TYPES.METAMASK) {
95− return metamaskSignTransaction(stx, wallet.address);
95+ return metamaskSignTransaction(utx, wallet.address);
9696 } else {
9797 stx.sign(wallet.privateKey);
9898 }
@@ -102,24 +102,16 @@ const signTransaction = async ({
102102
103103const sendSignedTransaction = (web3, stx, doubtNonceError) => {
104104 // Handle fake metamask transaction
105+ let eventEmitter;
106+ let rawTx;
105107 if (stx instanceof FakeMetamaskTransaction) {
106− const { ethereum } = window;
107− const sendTxs = {
108− method: 'eth_sendTransaction',
109− params: [stx.txnData],
110− from: stx.txnData.from,
111− };
112− return new Promise((resolve, reject) =>
113− ethereum.sendAsync(sendTxs, (err, data) =>
114− err ? reject(err) : resolve(data.result)
115− )
116− );
108+ eventEmitter = web3.eth.sendTransaction(stx.txnData);
109+ } else {
110+ eventEmitter = web3.eth.sendSignedTransaction(rawTx);
111+ rawTx = hexify(stx.serialize());
117112 }
118− const rawTx = hexify(stx.serialize());
119−
120113 return new Promise(async (resolve, reject) => {
121− web3.eth
122− .sendSignedTransaction(rawTx)
114+ eventEmitter
123115 .on('transactionHash', hash => {
124116 resolve(hash);
125117 })
@@ -132,7 +124,7 @@ const sendSignedTransaction = (web3, stx, doubtNonceError) => {
132124 const isNonceError =
133125 message.includes("the tx doesn't have the correct nonce.") ||
134126 message.includes('nonce too low');
135− if (isKnownError || (doubtNonceError && isNonceError)) {
127+ if (rawTx && (isKnownError || (doubtNonceError && isNonceError))) {
136128 console.log(
137129 'tx send error likely from gas tank submission, ignoring:',
138130 message
@@ -141,7 +133,8 @@ const sendSignedTransaction = (web3, stx, doubtNonceError) => {
141133 resolve(txHash);
142134 } else {
143135 console.error(err);
144− reject(err.message || 'Transaction sending failed!');
136+ const wrappedErr = err instanceof Error ? err : new Error(message);
137+ reject(wrappedErr);
145138 }
146139 });
147140 });