@@ -1,4 +1,3 @@
1−import Tx from 'ethereumjs-tx';
21import * as azimuth from 'azimuth-js';
32import * as tank from './tank';
43
@@ -9,7 +8,11 @@ import {
98 CRYPTO_SUITE_VERSION,
109} from './keys';
1110import { addHexPrefix } from './wallet';
12−import { sendAndAwaitAll } from './txn';
11+import {
12+ sendAndAwaitAllSerial,
13+ signTransaction,
14+ sendSignedTransaction,
15+} from './txn';
1316import getSuggestedGasPrice from './getSuggestedGasPrice';
1417import { GAS_LIMITS } from './constants';
1518import { toBN } from 'web3-utils';
@@ -29,6 +32,8 @@ export const TRANSACTION_PROGRESS = {
2932
3033export async function reticketPointBetweenWallets({
3134 fromWallet,
35+ fromWalletType,
36+ fromWalletHdPath,
3237 toWallet,
3338 point,
3439 web3,
@@ -144,26 +149,37 @@ export async function reticketPointBetweenWallets({
144149 progress(TRANSACTION_PROGRESS.SIGNING);
145150
146151 const suggestedGasPrice = await getSuggestedGasPrice(networkType);
152+ const chainId = await web3.eth.net.getId();
147153 const gasPrice = safeToWei(suggestedGasPrice.toFixed(), 'gwei');
148154 const gasPriceBN = toBN(gasPrice);
149− let totalCost = toBN(0);
150155 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
151− txs = txs.map(tx => {
152− tx.from = fromWallet.address;
153− tx.nonce = inviteNonce++;
154− tx.gasPrice = gasPriceBN;
155− totalCost = totalCost.add(gasPriceBN.mul(toBN(tx.gas)));
156− return tx;
157− });
156+ const totalCost = txs.reduce(
157+ (acc, tx) => acc.add(gasPriceBN.mul(toBN(tx.gas))),
158+ toBN(0)
159+ );
158160
159− let txPairs = txs.map(tx => {
160− let stx = new Tx(tx);
161− stx.sign(Buffer.from(fromWallet.privateKey, 'hex'));
162− return {
161+ let txPairs = [];
162+
163+ // Must be done in serial else hardware will give nonce errors
164+ for (let i = 0; i < txs.length; i++) {
165+ const stx = await signTransaction({
166+ wallet: fromWallet,
167+ walletType: fromWalletType,
168+ walletHdPath: fromWalletHdPath,
169+ networkType,
170+ txn: txs[i],
171+ nonce: inviteNonce + i,
172+ chainId,
173+ gasPrice: suggestedGasPrice.toFixed(),
174+ gasLimit: txs[i].gas,
175+ });
176+ txPairs.push({
163177 raw: hexify(stx.serialize()),
164178 signed: stx,
165− };
166− });
179+ });
180+ }
181+
182+ inviteNonce = inviteNonce + txs.length;
167183
168184 //
169185 // ensuring funding for transactions
@@ -175,6 +191,7 @@ export async function reticketPointBetweenWallets({
175191 web3,
176192 point,
177193 fromWallet.address,
194+ fromWalletType,
178195 totalCost,
179196 txPairs.map(p => p.raw),
180197 askForFunding,
@@ -187,7 +204,7 @@ export async function reticketPointBetweenWallets({
187204
188205 progress(TRANSACTION_PROGRESS.TRANSFERRING);
189206
190− await sendAndAwaitAll(web3, txPairs.map(p => p.signed), usedTank);
207+ await sendAndAwaitAllSerial(web3, txPairs.map(p => p.signed), usedTank);
191208
192209 //
193210 // move leftover eth
@@ -201,17 +218,24 @@ export async function reticketPointBetweenWallets({
201218 const sendEthCost = gasPriceBN.mul(toBN(gasLimit));
202219 if (transferEth && balance.gt(sendEthCost)) {
203220 const value = balance.sub(sendEthCost);
204− const tx = {
221+ const txn = {
205222 to: toWallet.ownership.keys.address,
206223 value: value,
224+ };
225+ const stx = signTransaction({
226+ wallet: fromWallet,
227+ walletType: fromWalletType,
228+ walletHdPath: fromWalletHdPath,
229+ txn,
230+ chainId,
231+ networkType,
207232 gasPrice: gasPriceBN,
208− gas: gasLimit,
233+ gasLimit,
209234 nonce: inviteNonce++,
210− };
211− let stx = new Tx(tx);
212− stx.sign(fromWallet.privateKey);
213− const rawTx = hexify(stx.serialize());
214− web3.eth.sendSignedTransaction(rawTx).catch(err => {
235+ });
236+ // let stx = new Tx(tx);
237+ // stx.sign(fromWallet.privateKey);
238+ sendSignedTransaction(web3, stx).catch(err => {
215239 console.log('error sending value tx, who cares', err);
216240 });
217241 }