1import Tx from 'ethereumjs-tx';
2import * as azimuth from 'azimuth-js';
3import * as wg from './walletgen';
4import * as tank from './tank';
6import JSZip from 'jszip';
7import saveAs from 'file-saver';
9import { hexify } from './txn';
10import { deriveNetworkSeedFromUrbitWallet, deriveNetworkKeys } from './keys';
11import { addHexPrefix } from './wallet';
12import { sendAndAwaitAll } from './txn';
13import { GAS_LIMITS } from './constants';
15// the initial network key revision is always 1
16const INITIAL_NETWORK_KEY_REVISION = 1;
18const TRANSACTION_STATES = {
20 label: 'Generating Transactions...',
24 label: 'Signing Transactions...',
28 label: 'Funding Transactions...',
32 label: 'Transferring Point...',
36 label: 'Cleaning Up...',
45export async function generateWallet(point) {
46 const ticket = await wg.makeTicket(point);
47 const wallet = await wg.generateWallet(point, ticket);
51//TODO should be moved to lib/walletgen
52export async function downloadWallet(paper) {
53 return new Promise((resolve, reject) => {
54 const zip = new JSZip();
55 //TODO the categories here aren't explained in bridge at all...
57 const bin0 = paper.filter(item => item.bin === '0');
58 const bin1 = paper.filter(item => item.bin === '1');
59 const bin2 = paper.filter(item => item.bin === '2');
60 const bin3 = paper.filter(item => item.bin === '3');
61 const bin4 = paper.filter(item => item.bin === '4');
63 const bin0Folder = zip.folder('0. Public');
64 const bin1Folder = zip.folder('1. Very High Friction Custody');
65 const bin2Folder = zip.folder('2. High Friction Custody');
66 const bin3Folder = zip.folder('3. Medium Friction Custody');
67 const bin4Folder = zip.folder('4. Low Friction Custody');
69 bin0.forEach(item => bin0Folder.file(`${item.pageTitle}.png`, item.png));
70 bin1.forEach(item => bin1Folder.file(`${item.pageTitle}.png`, item.png));
71 bin2.forEach(item => bin2Folder.file(`${item.pageTitle}.png`, item.png));
72 bin3.forEach(item => bin3Folder.file(`${item.pageTitle}.png`, item.png));
73 bin4.forEach(item => bin4Folder.file(`${item.pageTitle}.png`, item.png));
75 zip.generateAsync({ type: 'blob' }).then(content => {
76 saveAs(content, 'urbit-wallet.zip');
82export async function reticketPointBetweenWallets({
91 const askForFunding = (address, minBalance, balance) =>
94 value: { address, minBalance, balance },
97 const gotFunding = () => onUpdate({ type: 'gotFunding' });
98 const progress = state => onUpdate({ type: 'progress', state });
101 // generate transactions
104 progress(TRANSACTION_STATES.GENERATING);
106 // transfer to invite wallet, so it can configure
107 //NOTE doing this in the invite wallet instead of the final wallet makes
108 // failure cases "solvable" by just telling the user to fully retry.
109 // in other words, the invite flow is effectively idempotent.
111 //TODO no harm done if we already owned it, but should still get a bool arg
112 // for skipping this, if it isn't too big a burden for callers
113 const transferTmpTx = azimuth.ecliptic.transferPoint(
119 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
121 // configure networking public keys
122 const seed = await deriveNetworkSeedFromUrbitWallet(
124 INITIAL_NETWORK_KEY_REVISION
127 const networkSeed = seed.matchWith({
129 throw new Error('network seed not derived');
134 const networkKeys = deriveNetworkKeys(networkSeed);
136 const keysTx = azimuth.ecliptic.configureKeys(
139 addHexPrefix(networkKeys.crypt.public),
140 addHexPrefix(networkKeys.auth.public),
144 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
146 // configure management proxy
148 const managementTx = azimuth.ecliptic.setManagementProxy(
151 toWallet.management.keys.address
153 managementTx.gas = GAS_LIMITS.SET_PROXY;
155 // set spawn & voting proxies situationally
157 let txs = [transferTmpTx, keysTx, managementTx];
159 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
161 const spawnTx = azimuth.ecliptic.setSpawnProxy(
164 toWallet.spawn.keys.address
166 spawnTx.gas = GAS_LIMITS.SET_PROXY;
170 azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
172 const votingTx = azimuth.ecliptic.setVotingProxy(
175 toWallet.voting.keys.address
177 votingTx.gas = GAS_LIMITS.SET_PROXY;
182 // transfer configured point to user's new wallet
184 const transferFinalTx = azimuth.ecliptic.transferPoint(
187 toWallet.ownership.keys.address,
190 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
191 txs.push(transferFinalTx);
194 // finalizing & signing transactions
197 progress(TRANSACTION_STATES.SIGNING);
200 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
201 txs = txs.map(tx => {
202 tx.from = fromWallet.address;
203 tx.nonce = inviteNonce++;
204 tx.gasPrice = 20000000000; //NOTE we pay the premium for faster ux
205 totalCost = totalCost + tx.gasPrice * tx.gas;
209 let txPairs = txs.map(tx => {
210 let stx = new Tx(tx);
211 stx.sign(Buffer.from(fromWallet.privateKey, 'hex'));
213 raw: hexify(stx.serialize()),
219 // ensuring funding for transactions
222 progress(TRANSACTION_STATES.FUNDING);
224 const usedTank = await tank.ensureFundsFor(
229 txPairs.map(p => p.raw),
235 // sending and awaiting transactions
238 progress(TRANSACTION_STATES.TRANSFERRING);
240 await sendAndAwaitAll(web3, txPairs.map(p => p.signed), usedTank);
246 progress(TRANSACTION_STATES.CLEANING);
248 // if non-trivial eth left in invite wallet, transfer to new ownership
249 let balance = await web3.eth.getBalance(fromWallet.address);
250 const gasPrice = 20000000000;
251 const gasLimit = 21000;
252 const sendEthCost = gasPrice * gasLimit;
253 if (transferEth && balance > sendEthCost) {
254 const value = balance - sendEthCost;
256 to: toWallet.ownership.keys.address,
260 nonce: inviteNonce++,
262 let stx = new Tx(tx);
263 stx.sign(fromWallet.privateKey);
264 const rawTx = hexify(stx.serialize());
265 web3.eth.sendSignedTransaction(rawTx).catch(err => {
266 console.log('error sending value tx, who cares', err);
270 progress(TRANSACTION_STATES.DONE);
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