on a8a2d64 help sign in
bridge/src/lib/walletgen.js
80 lines 2.2 kB history raw
1import ob from 'urbit-ob';
2import kg from 'urbit-key-generation';
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 ZOD,
14} from './constants';
16const SEED_LENGTH_BYTES = SEED_ENTROPY_BITS / 8;
18// returns a promise for a ticket string
19export const makeTicket = point => {
20 const bits =
21 point < MIN_STAR
22 ? GALAXY_ENTROPY_BITS
23 : point < MIN_PLANET
24 ? STAR_ENTROPY_BITS
25 : PLANET_ENTROPY_BITS;
27 const bytes = bits / 8;
28 const some = new Uint8Array(bytes);
29 window.crypto.getRandomValues(some);
31 const gen = new more.Generator();
33 return new Promise((resolve, reject) => {
34 gen.generate(bits, result => {
35 const chunked = chunk(result, 2);
36 const desired = chunked.slice(0, bytes); // only take required entropy
37 const more = flatMap(desired, arr => arr[0] ^ arr[1]);
38 const entropy = zipWith(some, more, (x, y) => x ^ y);
39 const buf = Buffer.from(entropy);
40 const patq = ob.hex2patq(buf.toString('hex'));
41 resolve(patq);
42 reject('Entropy generation failed');
43 });
44 });
45};
47// return a wallet object
48export const generateWallet = async (point, ticket, boot) => {
49 const config = {
50 ticket: ticket,
51 seedSize: SEED_LENGTH_BYTES,
52 ship: point,
53 password: '',
54 revisions: {},
55 boot: boot,
56 };
58 // This is here to notify anyone who opens console because the thread
59 // hangs, blocking UI updates so this cannot be done in the UI
60 console.log('Generating Wallet for point address: ', point);
62 const wallet = await kg.generateWallet(config);
64 return wallet;
65};
67export const generateOwnershipWallet = (ship, ticket) =>
68 kg.generateOwnershipWallet({ ship, ticket });
70// temporary wallets need to be derivable from just the ticket,
71// so we always use ~zod as the wallet point.
72export const generateTemporaryOwnershipWallet = ticket =>
73 generateOwnershipWallet(ZOD, ticket);
75export const generateTemporaryTicketAndWallet = async point => {
76 const ticket = await makeTicket(point);
77 const owner = await generateOwnershipWallet(ZOD, ticket);
79 return { ticket, owner };
80};