on 4bd2eee help sign in
72 lines 1.9 kB history raw
2import * as ob from 'urbit-ob'
3import * as kg from '../../../node_modules/urbit-key-generation/dist/index'
4import * as more from 'more-entropy'
5import lodash from 'lodash'
6import {
7 MIN_STAR, MIN_PLANET, SEED_ENTROPY_BITS,
8 GALAXY_ENTROPY_BITS, STAR_ENTROPY_BITS, PLANET_ENTROPY_BITS,
9} from './constants'
11const SEED_LENGTH_BYTES = SEED_ENTROPY_BITS / 8
13// returns a promise for a ticket string
14const makeTicket = point => {
16 const bits = point < MIN_STAR
17 ? GALAXY_ENTROPY_BITS
18 : point < MIN_PLANET
19 ? STAR_ENTROPY_BITS
20 : PLANET_ENTROPY_BITS
22 const bytes = bits / 8
23 const some = new Uint8Array(bytes)
24 window.crypto.getRandomValues(some)
26 const gen = new more.Generator()
28 return new Promise((resolve, reject) => {
29 gen.generate(bits, result => {
30 const chunked = lodash.chunk(result, 2)
31 const desired = chunked.slice(0, bytes) // only take required entropy
32 const more = lodash.flatMap(desired, arr => arr[0] ^ arr[1])
33 const entropy = lodash.zipWith(some, more, (x, y) => x ^ y)
34 const buf = Buffer.from(entropy)
35 const patq = ob.hex2patq(buf.toString('hex'))
36 resolve(patq)
37 reject('Entropy generation failed')
38 })
39 })
42// return a wallet object
43const generateWallet = async (point, ticket, boot) => {
45 const config = {
46 ticket: ticket,
47 seedSize: SEED_LENGTH_BYTES,
48 ship: point,
49 password: '',
50 revisions: {},
51 boot: boot
52 };
54 const wallet = await kg.generateWallet(config);
56 // This is here to notify the anyone who opens console because the thread
57 // hangs, blocking UI updates so this cannot be doen in the UI
58 console.log('Generating Wallet for point address: ', point);
60 return wallet;
63const seq = num => Array.from(Array(num), (_, i) => i)
65const compose = (...fs) => fs.reduceRight((pF, nF) => (...args) => nF(pF(...args)), v => v)
67export {
68 makeTicket,
69 generateWallet,
70 compose,
71 seq,