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 { hexify, sendTransactionsAndAwaitConfirm } from './txn';
13import { deriveNetworkSeedFromUrbitWallet } from './keys';
14import { addHexPrefix } from './wallet';
15import { GAS_LIMITS } from './constants';
17// the initial network key revision is always 1
18const INITIAL_NETWORK_KEY_REVISION = 1;
20const TRANSACTION_STATES = {
22 label: 'Generating Transactions...',
26 label: 'Signing Transactions...',
30 label: 'Funding Transactions...',
34 label: 'Transferring Point...',
38 label: 'Cleaning Up...',
47export async function generateWallet(point) {
48 const ticket = await wg.makeTicket(point);
49 const wallet = await wg.generateWallet(point, ticket);
53//TODO should be moved to lib/walletgen
54export async function downloadWallet(paper) {
55 return new Promise((resolve, reject) => {
56 const zip = new JSZip();
57 //TODO the categories here aren't explained in bridge at all...
59 const bin0 = paper.filter(item => item.bin === '0');
60 const bin1 = paper.filter(item => item.bin === '1');
61 const bin2 = paper.filter(item => item.bin === '2');
62 const bin3 = paper.filter(item => item.bin === '3');
63 const bin4 = paper.filter(item => item.bin === '4');
65 const bin0Folder = zip.folder('0. Public');
66 const bin1Folder = zip.folder('1. Very High Friction Custody');
67 const bin2Folder = zip.folder('2. High Friction Custody');
68 const bin3Folder = zip.folder('3. Medium Friction Custody');
69 const bin4Folder = zip.folder('4. Low Friction Custody');
71 bin0.forEach(item => bin0Folder.file(`${item.pageTitle}.png`, item.png));
72 bin1.forEach(item => bin1Folder.file(`${item.pageTitle}.png`, item.png));
73 bin2.forEach(item => bin2Folder.file(`${item.pageTitle}.png`, item.png));
74 bin3.forEach(item => bin3Folder.file(`${item.pageTitle}.png`, item.png));
75 bin4.forEach(item => bin4Folder.file(`${item.pageTitle}.png`, item.png));
77 zip.generateAsync({ type: 'blob' }).then(content => {
78 saveAs(content, 'urbit-wallet.zip');
84export async function reticketPointBetweenWallets({
93 const askForFunding = (address, minBalance, balance) =>
96 value: { address, minBalance, balance },
99 const gotFunding = () => onUpdate({ type: 'gotFunding' });
100 const progress = state => onUpdate({ type: 'progress', state });
103 // generate transactions
106 progress(TRANSACTION_STATES.GENERATING);
108 // transfer to invite wallet, so it can configure
109 //NOTE doing this in the invite wallet instead of the final wallet makes
110 // failure cases "solvable" by just telling the user to fully retry.
111 // in other words, the invite flow is effectively idempotent.
113 //TODO no harm done if we already owned it, but should still get a bool arg
114 // for skipping this, if it isn't too big a burden for callers
115 const transferTmpTx = azimuth.ecliptic.transferPoint(
121 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
123 // configure networking public keys
124 const seed = await deriveNetworkSeedFromUrbitWallet(
126 INITIAL_NETWORK_KEY_REVISION
129 const networkSeed = seed.matchWith({
131 throw new Error('network seed not derived');
136 const networkKeys = kg.deriveNetworkKeys(networkSeed);
138 const keysTx = azimuth.ecliptic.configureKeys(
141 addHexPrefix(networkKeys.crypt.public),
142 addHexPrefix(networkKeys.auth.public),
146 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
148 // configure management proxy
150 const managementTx = azimuth.ecliptic.setManagementProxy(
153 toWallet.management.keys.address
155 managementTx.gas = GAS_LIMITS.SET_PROXY;
157 // set spawn & voting proxies situationally
159 let txs = [transferTmpTx, keysTx, managementTx];
161 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
163 const spawnTx = azimuth.ecliptic.setSpawnProxy(
166 toWallet.spawn.keys.address
168 spawnTx.gas = GAS_LIMITS.SET_PROXY;
172 azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
174 const votingTx = azimuth.ecliptic.setVotingProxy(
177 toWallet.voting.keys.address
179 votingTx.gas = GAS_LIMITS.SET_PROXY;
184 // transfer configured point to user's new wallet
186 const transferFinalTx = azimuth.ecliptic.transferPoint(
189 toWallet.ownership.keys.address,
192 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
193 txs.push(transferFinalTx);
196 // finalizing & signing transactions
199 progress(TRANSACTION_STATES.SIGNING);
202 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
203 txs = txs.map(tx => {
204 tx.from = fromWallet.address;
205 tx.nonce = inviteNonce++;
206 tx.gasPrice = 20000000000; //NOTE we pay the premium for faster ux
207 totalCost = totalCost + tx.gasPrice * tx.gas;
211 let txPairs = txs.map(tx => {
212 let stx = new Tx(tx);
213 stx.sign(Buffer.from(fromWallet.privateKey, 'hex'));
215 raw: hexify(stx.serialize()),
221 // ensuring funding for transactions
224 progress(TRANSACTION_STATES.FUNDING);
226 const usedTank = await tank.ensureFundsFor(
231 txPairs.map(p => p.raw),
237 // sending and awaiting transactions
240 progress(TRANSACTION_STATES.TRANSFERRING);
242 await sendTransactionsAndAwaitConfirm(
244 txPairs.map(p => Just(p.signed)),
252 progress(TRANSACTION_STATES.CLEANING);
254 // if non-trivial eth left in invite wallet, transfer to new ownership
255 let balance = await web3.eth.getBalance(fromWallet.address);
256 const gasPrice = 20000000000;
257 const gasLimit = 21000;
258 const sendEthCost = gasPrice * gasLimit;
259 if (transferEth && balance > sendEthCost) {
260 const value = balance - sendEthCost;
262 to: toWallet.ownership.keys.address,
266 nonce: inviteNonce++,
268 let stx = new Tx(tx);
269 stx.sign(fromWallet.privateKey);
270 const rawTx = hexify(stx.serialize());
271 web3.eth.sendSignedTransaction(rawTx).catch(err => {
272 console.log('error sending value tx, who cares', err);
276 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