@@ -1,4 +1,4 @@
1−import { Just, Nothing } from 'folktale/maybe';
1+import { Just } from 'folktale/maybe';
22import Tx from 'ethereumjs-tx';
33
44import * as azimuth from 'azimuth-js';
@@ -9,8 +9,7 @@ import * as tank from './tank';
99import JSZip from 'jszip';
1010import saveAs from 'file-saver';
1111
12−import { sendSignedTransaction } from './txn';
13−import { BRIDGE_ERROR } from './error';
12+import { sendSignedTransaction, hexify } from './txn';
1413import { attemptNetworkSeedDerivation } from './keys';
1514import { addHexPrefix, WALLET_TYPES } from './wallet';
1615
@@ -32,36 +31,36 @@ const WALLET_STATES = {
3231
3332const TRANSACTION_STATES = {
3433 GENERATING: {
35− label: 'Generating transactions',
36− pct: '0%',
34+ label: 'Generating Transactions...',
35+ progress: 0.0,
3736 },
3837 SIGNING: {
39− label: 'Signing transactions',
40− pct: '15%',
38+ label: 'Signing Transactions...',
39+ progress: 0.15,
4140 },
4241 FUNDING_INVITE: {
43− label: 'Funding invite wallet',
44− pct: '30%',
42+ label: 'Funding Invite Wallet...',
43+ progress: 0.3,
4544 },
4645 CLAIMING: {
47− label: 'Claiming invite',
48− pct: '55%',
46+ label: 'Claiming Invite...',
47+ progress: 0.55,
4948 },
5049 FUNDING_RECIPIENT: {
51− label: 'Funding recipient wallet',
52− pct: '65%',
50+ label: 'Funding Recipient Wallet...',
51+ progress: 0.65,
5352 },
5453 CONFIGURING: {
55− label: 'Configuring planet',
56− pct: '85%',
54+ label: 'Configuring Point...',
55+ progress: 0.85,
5756 },
5857 CLEANING: {
59− label: 'Cleaning up',
60− pct: '95%',
58+ label: 'Cleaning Up...',
59+ progress: 0.95,
6160 },
6261 DONE: {
6362 label: 'Done',
64− pct: '100%',
63+ progress: 1.0,
6564 },
6665};
6766
@@ -102,93 +101,61 @@ async function downloadWallet(paper) {
102101 });
103102}
104103
105−async function startTransactions(args) {
106− let {
107− realPointM,
108− web3: web3M,
109− contracts: contractsM,
110− inviteWalletM,
111− realWalletM,
112− setUrbitWallet,
113− updateProgress,
114− } = args;
115−
116− const askForFunding = (address, amount, current) => {
117− updateProgress({
118− type: 'notify',
119− value: `Please make sure ${address} has at least ${amount} wei, we'll continue once that's true. Current balance: ${current}. Waiting`,
104+async function claimPointFromInvite({
105+ inviteWallet,
106+ wallet,
107+ point,
108+ web3,
109+ contracts,
110+ onUpdate,
111+}) {
112+ const askForFunding = (address, minBalance, balance) =>
113+ onUpdate({
114+ type: 'askFunding',
115+ value: { address, minBalance, balance },
120116 });
121− };
122117
123− if (Nothing.hasInstance(web3M)) {
124− throw BRIDGE_ERROR.MISSING_WEB3;
125− }
126− const web3 = web3M.value;
127−
128− if (Nothing.hasInstance(contractsM)) {
129− throw BRIDGE_ERROR.MISSING_CONTRACTS;
130− }
131− const contracts = contractsM.value;
132−
133− if (Nothing.hasInstance(inviteWalletM)) {
134− throw BRIDGE_ERROR.MISSING_WALLET;
135− }
136− const inviteWallet = inviteWalletM.value;
137−
138− if (Nothing.hasInstance(realWalletM)) {
139− throw BRIDGE_ERROR.MISSING_WALLET;
140− }
141− const realWallet = realWalletM.value;
142−
143− // const realWallet = await urbitWalletFromTicket(args.realTicket, point);
144−
145− if (Nothing.hasInstance(realPointM)) {
146− throw BRIDGE_ERROR.MISSING_POINT;
147− }
148− const point = realPointM.value;
118+ const gotFunding = () => onUpdate({ type: 'gotFunding' });
119+ const progress = state => onUpdate({ type: 'progress', state });
149120
150121 const inviteAddress = inviteWallet.address;
151− console.log('working as', inviteAddress);
152122
153− // transfer from invite wallet to new wallet
123+ progress(TRANSACTION_STATES.GENERATING);
154124
155− let transferTx = azimuth.ecliptic.transferPoint(
125+ // transfer from invite wallet to new wallet
126+ const transferTx = azimuth.ecliptic.transferPoint(
156127 contracts,
157128 point,
158− realWallet.ownership.keys.address
129+ wallet.ownership.keys.address
159130 );
160131 transferTx.gas = 500000; //TODO can maybe be lower?
161132
162133 // ping gas tank with txs if needed
163−
164134 let inviteNonce = await web3.eth.getTransactionCount(inviteAddress);
165135 transferTx.nonce = inviteNonce++;
166136 transferTx.gasPrice = 20000000000; //NOTE we pay the premium for faster ux
167137 transferTx.from = inviteAddress;
168138
169− //NOTE using web3.eth.accounts.signTransaction is broken (1.0.0-beta51)
170− let transferStx = new Tx(transferTx);
139+ progress(TRANSACTION_STATES.SIGNING);
140+ // NOTE: using web3.eth.accounts.signTransaction is broken (1.0.0-beta51)
141+ const transferStx = new Tx(transferTx);
171142 transferStx.sign(inviteWallet.privateKey);
172− let rawTransferStx = '0x' + transferStx.serialize().toString('hex');
173−
143+ const rawTransferStx = hexify(transferStx.serialize());
174144 const transferCost = transferTx.gas * transferTx.gasPrice;
175− updateProgress({
176− type: 'progress',
177− value: TRANSACTION_STATES.FUNDING_INVITE,
178− });
145+
146+ progress(TRANSACTION_STATES.FUNDING_INVITE);
147+
179148 let usedTank = await tank.ensureFundsFor(
180149 web3,
181150 point,
182151 inviteAddress,
183152 transferCost,
184153 [rawTransferStx],
185− askForFunding
154+ askForFunding,
155+ gotFunding
186156 );
187157
188− updateProgress({
189− type: 'progress',
190− value: TRANSACTION_STATES.CLAIMING,
191− });
158+ progress(TRANSACTION_STATES.CLAIMING);
192159
193160 // send transaction
194161 await sendAndAwaitConfirm(web3, [Just(transferStx)], usedTank);
@@ -197,15 +164,13 @@ async function startTransactions(args) {
197164 // we're gonna be operating as the new wallet from here on out, so change
198165 // the relevant values
199166 //
200− setUrbitWallet(Just(realWallet));
201− const newAddress = realWallet.ownership.keys.address;
167+ const newAddress = wallet.ownership.keys.address;
202168
203169 // configure management proxy
204−
205− let managementTx = azimuth.ecliptic.setManagementProxy(
170+ const managementTx = azimuth.ecliptic.setManagementProxy(
206171 contracts,
207172 point,
208− realWallet.management.keys.address
173+ wallet.management.keys.address
209174 );
210175 managementTx.gas = 200000;
211176 managementTx.nonce = 0;
@@ -214,7 +179,7 @@ async function startTransactions(args) {
214179 //TODO feels like more of this logic should live in a lib?
215180 const seed = await attemptNetworkSeedDerivation(true, {
216181 walletType: WALLET_TYPES.TICKET,
217− urbitWallet: Just(realWallet),
182+ urbitWallet: Just(wallet),
218183 pointCursor: Just(point),
219184 pointCache: { [point]: { keyRevisionNumber: 0 } },
220185 });
@@ -251,17 +216,15 @@ async function startTransactions(args) {
251216
252217 let txPairs = txs.map(tx => {
253218 let stx = new Tx(tx);
254− stx.sign(Buffer.from(realWallet.ownership.keys.private, 'hex'));
219+ stx.sign(Buffer.from(wallet.ownership.keys.private, 'hex'));
255220 return {
256221 raw: '0x' + stx.serialize().toString('hex'),
257222 signed: stx,
258223 };
259224 });
260225
261− updateProgress({
262− type: 'progress',
263− value: TRANSACTION_STATES.FUNDING_RECIPIENT,
264− });
226+ progress(TRANSACTION_STATES.FUNDING_RECIPIENT);
227+
265228 usedTank = await tank.ensureFundsFor(
266229 web3,
267230 point,
@@ -271,17 +234,11 @@ async function startTransactions(args) {
271234 askForFunding
272235 );
273236
274− updateProgress({
275− type: 'progress',
276− value: TRANSACTION_STATES.CONFIGURING,
277− });
237+ progress(TRANSACTION_STATES.CONFIGURING);
278238
279239 await sendAndAwaitConfirm(web3, txPairs.map(p => Just(p.signed)), usedTank);
280240
281− updateProgress({
282− type: 'progress',
283− value: TRANSACTION_STATES.CLEANING,
284− });
241+ progress(TRANSACTION_STATES.CLEANING);
285242
286243 // if non-trivial eth left in invite wallet, transfer to new ownership
287244 let balance = await web3.eth.getBalance(inviteAddress);
@@ -300,20 +257,14 @@ async function startTransactions(args) {
300257 };
301258 let stx = new Tx(tx);
302259 stx.sign(inviteWallet.privateKey);
303− const rawTx = '0x' + stx.serialize().toString('hex');
260+ const rawTx = hexify(stx.serialize());
304261 web3.eth.sendSignedTransaction(rawTx).catch(err => {
305262 console.log('error sending value tx, who cares', err);
306263 });
307264 console.log('sent old balance');
308265 }
309266
310− updateProgress({
311− type: 'progress',
312− value: TRANSACTION_STATES.DONE,
313− });
314−
315− // proceed without waiting for confirm
316− setUrbitWallet(Just(realWallet));
267+ progress(TRANSACTION_STATES.DONE);
317268}
318269
319270async function sendAndAwaitConfirm(web3, signedTxs, usedTank) {
@@ -329,7 +280,7 @@ async function sendAndAwaitConfirm(web3, signedTxs, usedTank) {
329280export {
330281 generateWallet,
331282 downloadWallet,
332− startTransactions,
283+ claimPointFromInvite,
333284 INVITE_STAGES,
334285 WALLET_STATES,
335286 TRANSACTION_STATES,