@@ -1,52 +1,8 @@
1−import Tx from 'ethereumjs-tx';
2−import * as azimuth from 'azimuth-js';
3import * as wg from './walletgen';
1import * as wg from './walletgen';
4−import * as tank from './tank';
6import JSZip from 'jszip';
3import JSZip from 'jszip';
7import saveAs from 'file-saver';
4import saveAs from 'file-saver';
9−import { hexify } from './txn';
10−import { deriveNetworkSeedFromUrbitWallet, deriveNetworkKeys } from './keys';
11−import { addHexPrefix } from './wallet';
12−import { sendAndAwaitAll } from './txn';
15− DEFAULT_GAS_PRICE_GWEI,
16− CRYPTO_SUITE_VERSION,
18−import { toWei } from 'web3-utils';
20−// the initial network key revision is always 1
21−const INITIAL_NETWORK_KEY_REVISION = 1;
23−const TRANSACTION_STATES = {
25− label: 'Generating Transactions...',
29− label: 'Signing Transactions...',
33− label: 'Funding Transactions...',
37− label: 'Transferring Point...',
41− label: 'Cleaning Up...',
50export async function generateWallet(point) {
6export async function generateWallet(point) {
51 const ticket = await wg.makeTicket(point);
7 const ticket = await wg.makeTicket(point);
52 const wallet = await wg.generateWallet(point, ticket);
8 const wallet = await wg.generateWallet(point, ticket);
@@ -83,194 +39,3 @@ export async function downloadWallet(paper) {
87−export async function reticketPointBetweenWallets({
96− const askForFunding = (address, minBalance, balance) =>
99− value: { address, minBalance, balance },
102− const gotFunding = () => onUpdate({ type: 'gotFunding' });
103− const progress = state => onUpdate({ type: 'progress', state });
106− // generate transactions
109− progress(TRANSACTION_STATES.GENERATING);
111− // transfer to invite wallet, so it can configure
112− //NOTE doing this in the invite wallet instead of the final wallet makes
113− // failure cases "solvable" by just telling the user to fully retry.
114− // in other words, the invite flow is effectively idempotent.
116− //TODO no harm done if we already owned it, but should still get a bool arg
117− // for skipping this, if it isn't too big a burden for callers
118− const transferTmpTx = azimuth.ecliptic.transferPoint(
124− transferTmpTx.gas = GAS_LIMITS.TRANSFER;
126− // configure networking public keys
127− const seed = await deriveNetworkSeedFromUrbitWallet(
129− INITIAL_NETWORK_KEY_REVISION
132− const networkSeed = seed.matchWith({
134− throw new Error('network seed not derived');
139− const networkKeys = deriveNetworkKeys(networkSeed);
141− const keysTx = azimuth.ecliptic.configureKeys(
144− addHexPrefix(networkKeys.crypt.public),
145− addHexPrefix(networkKeys.auth.public),
146− CRYPTO_SUITE_VERSION,
147− /* discontinuous */ false
149− keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
151− // configure management proxy
153− const managementTx = azimuth.ecliptic.setManagementProxy(
156− toWallet.management.keys.address
158− managementTx.gas = GAS_LIMITS.SET_PROXY;
160− // set spawn & voting proxies situationally
162− let txs = [transferTmpTx, keysTx, managementTx];
164− azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
166− const spawnTx = azimuth.ecliptic.setSpawnProxy(
169− toWallet.spawn.keys.address
171− spawnTx.gas = GAS_LIMITS.SET_PROXY;
175− azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
177− const votingTx = azimuth.ecliptic.setVotingProxy(
180− toWallet.voting.keys.address
182− votingTx.gas = GAS_LIMITS.SET_PROXY;
187− // transfer configured point to user's new wallet
189− const transferFinalTx = azimuth.ecliptic.transferPoint(
192− toWallet.ownership.keys.address,
195− transferFinalTx.gas = GAS_LIMITS.TRANSFER;
196− txs.push(transferFinalTx);
199− // finalizing & signing transactions
202− progress(TRANSACTION_STATES.SIGNING);
205− let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
206− txs = txs.map(tx => {
207− tx.from = fromWallet.address;
208− tx.nonce = inviteNonce++;
209− tx.gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'), 10);
210− totalCost = totalCost + tx.gasPrice * tx.gas;
214− let txPairs = txs.map(tx => {
215− let stx = new Tx(tx);
216− stx.sign(Buffer.from(fromWallet.privateKey, 'hex'));
218− raw: hexify(stx.serialize()),
224− // ensuring funding for transactions
227− progress(TRANSACTION_STATES.FUNDING);
229− const usedTank = await tank.ensureFundsFor(
234− txPairs.map(p => p.raw),
240− // sending and awaiting transactions
243− progress(TRANSACTION_STATES.TRANSFERRING);
245− await sendAndAwaitAll(web3, txPairs.map(p => p.signed), usedTank);
248− // move leftover eth
251− progress(TRANSACTION_STATES.CLEANING);
253− // if non-trivial eth left in invite wallet, transfer to new ownership
254− let balance = await web3.eth.getBalance(fromWallet.address);
255− const gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'));
256− const gasLimit = GAS_LIMITS.SEND_ETH;
257− const sendEthCost = gasPrice * gasLimit;
258− if (transferEth && balance > sendEthCost) {
259− const value = balance - sendEthCost;
261− to: toWallet.ownership.keys.address,
265− nonce: inviteNonce++,
267− let stx = new Tx(tx);
268− stx.sign(fromWallet.privateKey);
269− const rawTx = hexify(stx.serialize());
270− web3.eth.sendSignedTransaction(rawTx).catch(err => {
271− console.log('error sending value tx, who cares', err);
275− progress(TRANSACTION_STATES.DONE);