1import { Just } from 'folktale/maybe';
2import Tx from 'ethereumjs-tx';
4import * as azimuth from 'azimuth-js';
5import * as kg from 'urbit-key-generation/dist';
6import * as wg from './walletgen';
7import * as tank from './tank';
9import JSZip from 'jszip';
10import saveAs from 'file-saver';
12import { sendAndAwaitAll, hexify } from './txn';
13import { attemptNetworkSeedDerivation } from './keys';
14import { addHexPrefix, WALLET_TYPES } from './wallet';
15import { GAS_LIMITS } from './constants';
17const INVITE_STAGES = {
18 INVITE_LOGIN: 'invite login',
19 INVITE_WALLET: 'invite wallet',
20 INVITE_VERIFY: 'invite verify',
21 INVITE_TRANSACTIONS: 'invite transactions',
24const WALLET_STATES = {
25 UNLOCKING: 'Unlocking invite wallet',
26 GENERATING: 'Generating your wallet',
27 RENDERING: 'Creating paper collateral',
28 PAPER_READY: 'Download your wallet',
29 DOWNLOADED: 'Wallet downloaded',
30 TRANSACTIONS: 'Sending transactions',
33const TRANSACTION_STATES = {
35 label: 'Generating Transactions...',
39 label: 'Signing Transactions...',
43 label: 'Funding Transactions...',
47 label: 'Claiming Invite...',
51 label: 'Cleaning Up...',
60async function generateWallet(point) {
61 const ticket = await wg.makeTicket(point);
62 const wallet = await wg.generateWallet(point, ticket);
66//TODO should be moved to lib/walletgen
67async function downloadWallet(paper) {
68 return new Promise((resolve, reject) => {
69 const zip = new JSZip();
70 //TODO the categories here aren't explained in bridge at all...
72 const bin0 = paper.filter(item => item.bin === '0');
73 const bin1 = paper.filter(item => item.bin === '1');
74 const bin2 = paper.filter(item => item.bin === '2');
75 const bin3 = paper.filter(item => item.bin === '3');
76 const bin4 = paper.filter(item => item.bin === '4');
78 const bin0Folder = zip.folder('0. Public');
79 const bin1Folder = zip.folder('1. Very High Friction Custody');
80 const bin2Folder = zip.folder('2. High Friction Custody');
81 const bin3Folder = zip.folder('3. Medium Friction Custody');
82 const bin4Folder = zip.folder('4. Low Friction Custody');
84 bin0.forEach(item => bin0Folder.file(`${item.pageTitle}.png`, item.png));
85 bin1.forEach(item => bin1Folder.file(`${item.pageTitle}.png`, item.png));
86 bin2.forEach(item => bin2Folder.file(`${item.pageTitle}.png`, item.png));
87 bin3.forEach(item => bin3Folder.file(`${item.pageTitle}.png`, item.png));
88 bin4.forEach(item => bin4Folder.file(`${item.pageTitle}.png`, item.png));
90 zip.generateAsync({ type: 'blob' }).then(content => {
91 saveAs(content, 'urbit-wallet.zip');
97async function claimPointFromInvite({
106 const askForFunding = (address, minBalance, balance) =>
109 value: { address, minBalance, balance },
112 const gotFunding = () => onUpdate({ type: 'gotFunding' });
113 const progress = state => onUpdate({ type: 'progress', state });
115 const inviteAddress = inviteWallet.address;
118 // generate transactions
121 progress(TRANSACTION_STATES.GENERATING);
123 // transfer to invite wallet, so it can configure
124 //NOTE doing this in the invite wallet instead of the final wallet makes
125 // failure cases "solvable" by just telling the user to fully retry.
126 // in other words, the invite flow is effectively idempotent.
128 //TODO no harm done if we already owned it, but should still get a bool arg
129 // for skipping this, if it isn't too big a burden for callers
130 const transferTmpTx = azimuth.ecliptic.transferPoint(
136 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
138 // configure networking public keys
140 //TODO feels like more of this logic should live in a lib?
141 const seed = await attemptNetworkSeedDerivation(true, {
142 walletType: WALLET_TYPES.TICKET,
143 urbitWallet: Just(wallet),
144 pointCursor: Just(point),
145 pointCache: { [point]: { keyRevisionNumber: 0 } },
148 const networkSeed = seed.matchWith({
150 throw new Error('network seed not derived');
155 const networkKeys = kg.deriveNetworkKeys(networkSeed);
157 const keysTx = azimuth.ecliptic.configureKeys(
160 addHexPrefix(networkKeys.crypt.public),
161 addHexPrefix(networkKeys.auth.public),
165 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
167 // configure management proxy
169 const managementTx = azimuth.ecliptic.setManagementProxy(
172 wallet.management.keys.address
174 managementTx.gas = GAS_LIMITS.SET_PROXY;
176 // set spawn & voting proxies situationally
178 let txs = [transferTmpTx, keysTx, managementTx];
180 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
182 const spawnTx = azimuth.ecliptic.setSpawnProxy(
185 wallet.spawn.keys.address
187 spawnTx.gas = GAS_LIMITS.SET_PROXY;
191 azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
193 const votingTx = azimuth.ecliptic.setVotingProxy(
196 wallet.voting.keys.address
198 votingTx.gas = GAS_LIMITS.SET_PROXY;
203 // transfer configured point to user's new wallet
205 const transferFinalTx = azimuth.ecliptic.transferPoint(
208 wallet.ownership.keys.address,
211 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
212 txs.push(transferFinalTx);
215 // finalizing & signing transactions
218 progress(TRANSACTION_STATES.SIGNING);
221 let inviteNonce = await web3.eth.getTransactionCount(inviteAddress);
222 txs = txs.map(tx => {
223 tx.from = inviteAddress;
224 tx.nonce = inviteNonce++;
225 tx.gasPrice = 20000000000; //NOTE we pay the premium for faster ux
226 totalCost = totalCost + tx.gasPrice * tx.gas;
230 let txPairs = txs.map(tx => {
231 let stx = new Tx(tx);
232 stx.sign(Buffer.from(inviteWallet.privateKey, 'hex'));
234 raw: hexify(stx.serialize()),
240 // ensuring funding for transactions
243 progress(TRANSACTION_STATES.FUNDING);
245 const usedTank = await tank.ensureFundsFor(
250 txPairs.map(p => p.raw),
256 // sending and awaiting transactions
259 progress(TRANSACTION_STATES.CLAIMING);
261 await sendAndAwaitAll(web3, txPairs.map(p => Just(p.signed)), usedTank);
267 progress(TRANSACTION_STATES.CLEANING);
269 // if non-trivial eth left in invite wallet, transfer to new ownership
270 let balance = await web3.eth.getBalance(inviteAddress);
271 const gasPrice = 20000000000;
272 const gasLimit = 21000;
273 const sendEthCost = gasPrice * gasLimit;
274 if (transferEth && balance > sendEthCost) {
275 const value = balance - sendEthCost;
277 to: wallet.ownership.keys.address,
281 nonce: inviteNonce++,
283 let stx = new Tx(tx);
284 stx.sign(inviteWallet.privateKey);
285 const rawTx = hexify(stx.serialize());
286 web3.eth.sendSignedTransaction(rawTx).catch(err => {
287 console.log('error sending value tx, who cares', err);
291 progress(TRANSACTION_STATES.DONE);
297 claimPointFromInvite,
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