on efbcbeb help sign in
74 lines 2.0 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 MAX_GALAXY, MIN_STAR, MAX_STAR, MIN_PLANET,
8 GALAXY_ENTROPY_BITS, STAR_ENTROPY_BITS, PLANET_ENTROPY_BITS,
9 SEED_ENTROPY_BITS,
10 GEN_STATES
11} from './constants'
13const SEED_LENGTH_BYTES = SEED_ENTROPY_BITS / 8
15// returns a promise for a ticket string
16const makeTicket = point => {
18 const bits = point < MIN_STAR
19 ? GALAXY_ENTROPY_BITS
20 : point < MIN_PLANET
21 ? STAR_ENTROPY_BITS
22 : PLANET_ENTROPY_BITS
24 const bytes = bits / 8
25 const some = new Uint8Array(bytes)
26 window.crypto.getRandomValues(some)
28 const gen = new more.Generator()
30 return new Promise((resolve, reject) => {
31 gen.generate(bits, result => {
32 const chunked = lodash.chunk(result, 2)
33 const desired = chunked.slice(0, bytes) // only take required entropy
34 const more = lodash.flatMap(desired, arr => arr[0] ^ arr[1])
35 const entropy = lodash.zipWith(some, more, (x, y) => x ^ y)
36 const buf = Buffer.from(entropy)
37 const patq = ob.hex2patq(buf.toString('hex'))
38 resolve(patq)
39 reject('Entropy generation failed')
40 })
41 })
44// return a wallet object
45const generateWallet = async (point, ticket, boot) => {
47 const config = {
48 ticket: ticket,
49 seedSize: SEED_LENGTH_BYTES,
50 ship: point,
51 password: '',
52 revisions: {},
53 boot: boot
54 };
56 const wallet = await kg.generateWallet(config);
58 // This is here to notify the anyone who opens console because the thread
59 // hangs, blocking UI updates so this cannot be doen in the UI
60 console.log('Generating Wallet for point address: ', point);
62 return wallet;
65const seq = num => Array.from(Array(num), (_, i) => i)
67const compose = (...fs) => fs.reduceRight((pF, nF) => (...args) => nF(pF(...args)), v => v)
69export {
70 makeTicket,
71 generateWallet,
72 compose,
73 seq,