on master help sign in
Move walletgen functions into lib
60b0fceFang
src/bridge/views/InviteWallet.js+3−65
@@ -15,19 +15,10 @@ import { BRIDGE_ERROR } from '../lib/error'
15import { Row, Col, Warning, Button, H1, H3 } from '../components/Base'
15import { Row, Col, Warning, Button, H1, H3 } from '../components/Base'
16
16
17// for wallet generation
17// for wallet generation
18−import * as kg from '../../../node_modules/urbit-key-generation/dist/index'
18+import * as wg from '../../walletgen/lib/lib'
19−import * as more from 'more-entropy'
20−import lodash from 'lodash'
21import PaperCollateralRenderer from 'PaperCollateralRenderer'
19import PaperCollateralRenderer from 'PaperCollateralRenderer'
22import JSZip from 'jszip'
20import JSZip from 'jszip'
23import saveAs from 'file-saver'
21import saveAs from 'file-saver'
24−import { MAX_GALAXY, MIN_STAR, MAX_STAR, MIN_PLANET,
25− GALAXY_ENTROPY_BITS, STAR_ENTROPY_BITS, PLANET_ENTROPY_BITS,
26− SEED_ENTROPY_BITS,
27− GEN_STATES
28− } from '../../walletgen/lib/constants'
29−const SEED_LENGTH_BYTES = SEED_ENTROPY_BITS / 8
30−const NEXT_STEP_NUM = 6;
31
22
32class InviteWallet extends React.Component {
23class InviteWallet extends React.Component {
33
24
@@ -74,63 +65,10 @@ class InviteWallet extends React.Component {
74 //
65 //
75 }
66 }
76
67
77− //TODO pulled from walletgen/views/Generate and Download, put into lib
78 async generateWallet(point) {
68 async generateWallet(point) {
79− const makeTicket = point => {
69+ const ticket = await wg.makeTicket(point);
80−
81− const bits = point < MIN_STAR
82− ? GALAXY_ENTROPY_BITS
83− : point < MIN_PLANET
84− ? STAR_ENTROPY_BITS
85− : PLANET_ENTROPY_BITS
86−
87− const bytes = bits / 8
88− const some = new Uint8Array(bytes)
89− window.crypto.getRandomValues(some)
90−
91− const gen = new more.Generator()
92−
93− return new Promise((resolve, reject) => {
94− gen.generate(bits, result => {
95− const chunked = lodash.chunk(result, 2)
96− const desired = chunked.slice(0, bytes) // only take required entropy
97− const more = lodash.flatMap(desired, arr => arr[0] ^ arr[1])
98− const entropy = lodash.zipWith(some, more, (x, y) => x ^ y)
99− const buf = Buffer.from(entropy)
100− const patq = ob.hex2patq(buf.toString('hex'))
101− resolve(patq)
102− reject('Entropy generation failed')
103− })
104− })
105− }
106−
107− const genWallet = async (point, ticket, cb) => {
108−
109− const config = {
110− ticket: ticket,
111− seedSize: SEED_LENGTH_BYTES,
112− ship: point,
113− password: '',
114− revisions: {},
115− boot: false //TODO should this generate networking keys here already?
116− };
117−
118− const wallet = await kg.generateWallet(config);
70+ const wallet = await wg.generateWallet(point, ticket);
119−
120− // This is here to notify the anyone who opens console because the thread
121− // hangs, blocking UI updates so this cannot be doen in the UI
122− console.log('Generating Wallet for point address: ', point);
123−
124− return wallet;
125− }
126−
127− const ticket = await makeTicket(point);
128−
129− const wallet = await genWallet(point, ticket);
130−
131 this.setState({ wallet: Just(wallet) });
71 this.setState({ wallet: Just(wallet) });
132− console.log('got wallet', wallet);
133− console.log('should start rendering now');
134 }
72 }
135
73
136 download() {
74 download() {
src/bridge/views/InvitesSend.js+3−33
@@ -23,10 +23,7 @@ import { Row, Col, Warning, Button, H1, H3,
23import StatelessTransaction from '../components/StatelessTransaction'
23import StatelessTransaction from '../components/StatelessTransaction'
24
24
25// for wallet generation
25// for wallet generation
26−import * as kg from '../../../node_modules/urbit-key-generation/dist/index'
27−import * as more from 'more-entropy'
28−import lodash from 'lodash'
29−import { PLANET_ENTROPY_BITS } from '../../walletgen/lib/constants'
26+import * as wg from '../../walletgen/lib/lib'
30
27
31// for transaction generation and signing
28// for transaction generation and signing
32import {
29import {
@@ -101,7 +98,6 @@ class InvitesSend extends React.Component {
101 );
98 );
102 invited = invited.map(async point => {
99 invited = invited.map(async point => {
103 const active = await azimuth.azimuth.isActive(this.contracts, point);
100 const active = await azimuth.azimuth.isActive(this.contracts, point);
104− console.log('point active', point, active);
105 const res = {point: Number(point), active:active};
101 const res = {point: Number(point), active:active};
106 return res;
102 return res;
107 });
103 });
@@ -123,35 +119,9 @@ class InvitesSend extends React.Component {
123 return res;
119 return res;
124 }
120 }
125
121
126− //TODO pulled from walletgen/views/Generate, put into lib
127 async generateWallet() {
122 async generateWallet() {
128− const makeTicket = () => {
129−
130− const bits = PLANET_ENTROPY_BITS
131−
132− const bytes = bits / 8
133− const some = new Uint8Array(bytes)
134− window.crypto.getRandomValues(some)
135−
136− const gen = new more.Generator()
137−
138− return new Promise((resolve, reject) => {
139− gen.generate(bits, result => {
140− const chunked = lodash.chunk(result, 2)
141− const desired = chunked.slice(0, bytes) // only take required entropy
142− const more = lodash.flatMap(desired, arr => arr[0] ^ arr[1])
143− const entropy = lodash.zipWith(some, more, (x, y) => x ^ y)
144− const buf = Buffer.from(entropy)
145− const patq = ob.hex2patq(buf.toString('hex'))
146− resolve(patq)
147− reject('Entropy generation failed')
148− })
149− })
150− };
151−
152− const ticket = await makeTicket(0x10000); // planet-sized ticket
123+ const ticket = await wg.makeTicket(0x10000); // planet-sized ticket
153− const wallet = await kg.generateWallet({ticket, ship:0});
124+ const wallet = await wg.generateWallet(0, ticket, false);
154− console.log('found ticket', ticket, wallet, wallet.ownership.keys.address);
155 this.setState({inviteWallet:
125 this.setState({inviteWallet:
156 Just({ticket, owner:wallet.ownership.keys.address})
126 Just({ticket, owner:wallet.ownership.keys.address})
157 });
127 });
src/walletgen/lib/lib.js+65
@@ -1,9 +1,74 @@
1
1
2+import * as ob from 'urbit-ob'
3+import * as kg from '../../../node_modules/urbit-key-generation/dist/index'
4+import * as more from 'more-entropy'
5+import lodash from 'lodash'
6+import {
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'
12+
13+const SEED_LENGTH_BYTES = SEED_ENTROPY_BITS / 8
14+
15+// returns a promise for a ticket string
16+const makeTicket = point => {
17+
18+ const bits = point < MIN_STAR
19+ ? GALAXY_ENTROPY_BITS
20+ : point < MIN_PLANET
21+ ? STAR_ENTROPY_BITS
22+ : PLANET_ENTROPY_BITS
23+
24+ const bytes = bits / 8
25+ const some = new Uint8Array(bytes)
26+ window.crypto.getRandomValues(some)
27+
28+ const gen = new more.Generator()
29+
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+ })
42+}
43+
44+// return a wallet object
45+const generateWallet = async (point, ticket, boot) => {
46+
47+ const config = {
48+ ticket: ticket,
49+ seedSize: SEED_LENGTH_BYTES,
50+ ship: point,
51+ password: '',
52+ revisions: {},
53+ boot: boot
54+ };
55+
56+ const wallet = await kg.generateWallet(config);
57+
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);
61+
62+ return wallet;
63+}
64+
2const seq = num => Array.from(Array(num), (_, i) => i)
65const seq = num => Array.from(Array(num), (_, i) => i)
3
66
4const compose = (...fs) => fs.reduceRight((pF, nF) => (...args) => nF(pF(...args)), v => v)
67const compose = (...fs) => fs.reduceRight((pF, nF) => (...args) => nF(pF(...args)), v => v)
5
68
6export {
69export {
70+ makeTicket,
71+ generateWallet,
7 compose,
72 compose,
8 seq,
73 seq,
9}
74}
src/walletgen/views/Generate.js+4−63
@@ -4,8 +4,7 @@ import lodash from 'lodash'
4import * as sigil from 'sigil-js'
4import * as sigil from 'sigil-js'
5import ob from 'urbit-ob'
5import ob from 'urbit-ob'
6import classnames from 'classnames'
6import classnames from 'classnames'
7−// temporarily use keygen-js with argon2u that is configured with 520MB
7+import * as wg from '../lib/lib'
8−import * as kg from '../../../node_modules/urbit-key-generation/dist/index'
9
8
10
9
11import Button from '../components/Button'
10import Button from '../components/Button'
@@ -169,65 +168,6 @@ const planetInfo = (ship, sig) =>
169 </div>
168 </div>
170 </div>
169 </div>
171
170
172−
173−
174−const makeTicket = ship => {
175−
176− const bits = ship < MIN_STAR
177− ? GALAXY_ENTROPY_BITS
178− : ship < MIN_PLANET
179− ? STAR_ENTROPY_BITS
180− : PLANET_ENTROPY_BITS
181−
182− const bytes = bits / 8
183− const some = new Uint8Array(bytes)
184− window.crypto.getRandomValues(some)
185−
186− const gen = new more.Generator()
187−
188− return new Promise((resolve, reject) => {
189− gen.generate(bits, result => {
190− const chunked = lodash.chunk(result, 2)
191− const desired = chunked.slice(0, bytes) // only take required entropy
192− const more = lodash.flatMap(desired, arr => arr[0] ^ arr[1])
193− const entropy = lodash.zipWith(some, more, (x, y) => x ^ y)
194− const buf = Buffer.from(entropy)
195− const patq = ob.hex2patq(buf.toString('hex'))
196− resolve(patq)
197− reject('Entropy generation failed')
198− })
199− })
200−}
201−
202−
203−
204−
205−const genWallet = async (ship, ticket, cb) => {
206−
207− const boot = ship < MIN_STAR || ship > MAX_STAR;
208−
209− const config = {
210− ticket: ticket,
211− seedSize: SEED_LENGTH_BYTES,
212− ship: ship,
213− password: '',
214− revisions: {},
215− boot: boot
216− };
217−
218− const wallet = await kg.generateWallet(config);
219−
220− // This is here to notify the anyone who opens console because the thread
221− // hangs, blocking UI updates so this cannot be doen in the UI
222− console.log('Generating Wallet for ship address: ', ship);
223−
224− return wallet;
225−}
226−
227−
228−
229−
230−
231class Generate extends React.Component {
171class Generate extends React.Component {
232 constructor(props) {
172 constructor(props) {
233 super(props)
173 super(props)
@@ -249,7 +189,7 @@ class Generate extends React.Component {
249 const enyPromises = ships.map(async ship => {
189 const enyPromises = ships.map(async ship => {
250 return {
190 return {
251 ship,
191 ship,
252− ticket: await makeTicket(ship),
192+ ticket: await wg.makeTicket(ship),
253 };
193 };
254 });
194 });
255
195
@@ -264,9 +204,10 @@ class Generate extends React.Component {
264 await this.forciblyExecuteArgon2Once(enyTable);
204 await this.forciblyExecuteArgon2Once(enyTable);
265
205
266 const walletPromises = enyTable.map(async ({ ship, ticket }) => {
206 const walletPromises = enyTable.map(async ({ ship, ticket }) => {
207+ const boot = ship < MIN_STAR || ship > MAX_STAR;
267 return {
208 return {
268 ship,
209 ship,
269− wallet: await genWallet(ship, ticket),
210+ wallet: await wg.generateWallet(ship, ticket, boot),
270 };
211 };
271 });
212 });
272
213