on 04765bb help sign in
bridge/src/lib/walletgen.js
74 lines 2.0 kB history raw
1import * as ob from 'urbit-ob';
2import * as kg from 'urbit-key-generation/dist';
3import * as more from 'more-entropy';
4import { chunk, flatMap, zipWith } from 'lodash';
6import {
7 MIN_STAR,
8 MIN_PLANET,
9 SEED_ENTROPY_BITS,
10 GALAXY_ENTROPY_BITS,
11 STAR_ENTROPY_BITS,
12 PLANET_ENTROPY_BITS,
13} from './constants';
15const SEED_LENGTH_BYTES = SEED_ENTROPY_BITS / 8;
17// returns a promise for a ticket string
18export const makeTicket = point => {
19 const bits =
20 point < MIN_STAR
21 ? GALAXY_ENTROPY_BITS
22 : point < MIN_PLANET
23 ? STAR_ENTROPY_BITS
24 : PLANET_ENTROPY_BITS;
26 const bytes = bits / 8;
27 const some = new Uint8Array(bytes);
28 window.crypto.getRandomValues(some);
30 const gen = new more.Generator();
32 return new Promise((resolve, reject) => {
33 gen.generate(bits, result => {
34 const chunked = chunk(result, 2);
35 const desired = chunked.slice(0, bytes); // only take required entropy
36 const more = flatMap(desired, arr => arr[0] ^ arr[1]);
37 const entropy = zipWith(some, more, (x, y) => x ^ y);
38 const buf = Buffer.from(entropy);
39 const patq = ob.hex2patq(buf.toString('hex'));
40 resolve(patq);
41 reject('Entropy generation failed');
42 });
43 });
44};
46// return a wallet object
47export const generateWallet = async (point, ticket, boot) => {
48 const config = {
49 ticket: ticket,
50 seedSize: SEED_LENGTH_BYTES,
51 ship: point,
52 password: '',
53 revisions: {},
54 boot: boot,
55 };
57 const wallet = await kg.generateWallet(config);
59 // This is here to notify anyone who opens console because the thread
60 // hangs, blocking UI updates so this cannot be done in the UI
61 console.log('Generating Wallet for point address: ', point);
63 return wallet;
64};
66export const generateOwnershipWallet = (ship, ticket) =>
67 kg.generateOwnershipWallet({ ship, ticket });
69export const generateTemporaryTicketAndWallet = async point => {
70 const ticket = await makeTicket(point);
71 const owner = await generateOwnershipWallet(0, ticket);
73 return { ticket, owner };
74};