on master help sign in
Metamask signing roller txns WIP
f181773Will Galebach
src/lib/useRoller.ts+46−13
@@ -62,7 +62,13 @@ const inviteTemplate = (
62});
62});
63
63
64export default function useRoller() {
64export default function useRoller() {
65− const { wallet, authToken, authMnemonic, urbitWallet }: any = useWallet();
65+ const {
66+ wallet,
67+ authToken,
68+ authMnemonic,
69+ urbitWallet,
70+ walletType,
71+ }: any = useWallet();
66 const { pointCursor }: any = usePointCursor();
72 const { pointCursor }: any = usePointCursor();
67 const { web3, contracts }: any = useNetwork();
73 const { web3, contracts }: any = useNetwork();
68 const allPoints: any = usePointCache();
74 const allPoints: any = usePointCache();
@@ -189,7 +195,9 @@ export default function useRoller() {
189 _point,
195 _point,
190 proxy,
196 proxy,
191 nonceInc,
197 nonceInc,
192− planet
198+ planet,
199+ walletType,
200+ _web3
193 );
201 );
194
202
195 const networkSeed = await deriveNetworkSeedFromUrbitWallet(
203 const networkSeed = await deriveNetworkSeedFromUrbitWallet(
@@ -202,7 +210,9 @@ export default function useRoller() {
202 planet,
210 planet,
203 'own',
211 'own',
204 0,
212 0,
205− networkSeed
213+ networkSeed,
214+ walletType,
215+ _web3
206 );
216 );
207
217
208 const setManagementProxyRequest = registerProxyAddress(
218 const setManagementProxyRequest = registerProxyAddress(
@@ -212,7 +222,9 @@ export default function useRoller() {
212 'own',
222 'own',
213 'manage',
223 'manage',
214 1,
224 1,
215− inviteWallet.management.keys.address
225+ inviteWallet.management.keys.address,
226+ walletType,
227+ _web3
216 );
228 );
217
229
218 const transferRequest = transferPointRequest(
230 const transferRequest = transferPointRequest(
@@ -221,7 +233,9 @@ export default function useRoller() {
221 planet,
233 planet,
222 'own',
234 'own',
223 2,
235 2,
224− inviteWallet.ownership.keys.address
236+ inviteWallet.ownership.keys.address,
237+ walletType,
238+ _web3
225 );
239 );
226
240
227 requests.push(
241 requests.push(
@@ -248,6 +262,7 @@ export default function useRoller() {
248 contracts,
262 contracts,
249 pointCursor,
263 pointCursor,
250 wallet,
264 wallet,
265+ walletType,
251 web3,
266 web3,
252 getDetails,
267 getDetails,
253 ls,
268 ls,
@@ -448,8 +463,9 @@ export default function useRoller() {
448 const _point = need.point(pointCursor);
463 const _point = need.point(pointCursor);
449 const azimuthPoint = await api.getPoint(_point);
464 const azimuthPoint = await api.getPoint(_point);
450 const _wallet = wallet.getOrElse(null);
465 const _wallet = wallet.getOrElse(null);
466+ const _web3 = web3.getOrElse(null);
451 const _details = getDetails(_point);
467 const _details = getDetails(_point);
452− if (!_wallet || !_details) {
468+ if (!_wallet || !_details || !_web3) {
453 // not using need because we want a custom error
469 // not using need because we want a custom error
454 throw new Error('Internal Error: Missing Wallet/Details');
470 throw new Error('Internal Error: Missing Wallet/Details');
455 }
471 }
@@ -484,6 +500,8 @@ export default function useRoller() {
484 proxy!,
500 proxy!,
485 nonce,
501 nonce,
486 networkSeed,
502 networkSeed,
503+ walletType,
504+ _web3,
487 breach
505 breach
488 );
506 );
489 const pendingTx = await api.getPendingTx(txHash);
507 const pendingTx = await api.getPendingTx(txHash);
@@ -508,6 +526,7 @@ export default function useRoller() {
508 }: ReticketParams) => {
526 }: ReticketParams) => {
509 const azimuthPoint = await api.getPoint(point);
527 const azimuthPoint = await api.getPoint(point);
510 const proxy = 'own';
528 const proxy = 'own';
529+ const _web3 = web3.getOrElse(null);
511 let nonce = await api.getNonce({ ship: point, proxy });
530 let nonce = await api.getNonce({ ship: point, proxy });
512
531
513 let requests = [];
532 let requests = [];
@@ -525,7 +544,9 @@ export default function useRoller() {
525 point,
544 point,
526 proxy,
545 proxy,
527 nonce,
546 nonce,
528− networkSeed
547+ networkSeed,
548+ walletType,
549+ _web3
529 );
550 );
530 nonce = nonce + 1;
551 nonce = nonce + 1;
531 requests.push(configureKeysRequest);
552 requests.push(configureKeysRequest);
@@ -538,7 +559,9 @@ export default function useRoller() {
538 proxy,
559 proxy,
539 'manage',
560 'manage',
540 nonce,
561 nonce,
541− manager
562+ manager,
563+ walletType,
564+ _web3
542 );
565 );
543 nonce = nonce + 1;
566 nonce = nonce + 1;
544 requests.push(registerMgmtRequest);
567 requests.push(registerMgmtRequest);
@@ -552,7 +575,9 @@ export default function useRoller() {
552 proxy,
575 proxy,
553 'spawn',
576 'spawn',
554 nonce,
577 nonce,
555− toWallet.spawn.keys.address
578+ toWallet.spawn.keys.address,
579+ walletType,
580+ _web3
556 );
581 );
557 nonce = nonce + 1;
582 nonce = nonce + 1;
558 requests.push(registerSpawnRequest);
583 requests.push(registerSpawnRequest);
@@ -564,7 +589,9 @@ export default function useRoller() {
564 point,
589 point,
565 proxy,
590 proxy,
566 nonce,
591 nonce,
567− to
592+ to,
593+ walletType,
594+ _web3
568 );
595 );
569 requests.push(transferTxRequest);
596 requests.push(transferTxRequest);
570
597
@@ -576,6 +603,7 @@ export default function useRoller() {
576 async (proxyType: Proxy, address: EthAddress) => {
603 async (proxyType: Proxy, address: EthAddress) => {
577 const _point = need.point(pointCursor);
604 const _point = need.point(pointCursor);
578 const _wallet = wallet.getOrElse(null);
605 const _wallet = wallet.getOrElse(null);
606+ const _web3 = web3.getOrElse(null);
579
607
580 if (!_wallet) {
608 if (!_wallet) {
581 // not using need because we want a custom error
609 // not using need because we want a custom error
@@ -598,20 +626,23 @@ export default function useRoller() {
598 proxy,
626 proxy,
599 proxyType,
627 proxyType,
600 nonce,
628 nonce,
601− address
629+ address,
630+ walletType,
631+ _web3
602 );
632 );
603
633
604 const pendingTx = await api.getPendingTx(txHash);
634 const pendingTx = await api.getPendingTx(txHash);
605
635
606 return pendingTx;
636 return pendingTx;
607 },
637 },
608− [api, pointCursor, wallet]
638+ [api, pointCursor, wallet, web3, walletType]
609 );
639 );
610
640
611 const transferPoint = useCallback(
641 const transferPoint = useCallback(
612 async (address: EthAddress, reset?: boolean) => {
642 async (address: EthAddress, reset?: boolean) => {
613 const _point = need.point(pointCursor);
643 const _point = need.point(pointCursor);
614 const _wallet = wallet.getOrElse(null);
644 const _wallet = wallet.getOrElse(null);
645+ const _web3 = web3.getOrElse(null);
615
646
616 if (!_wallet) {
647 if (!_wallet) {
617 // not using need because we want a custom error
648 // not using need because we want a custom error
@@ -633,13 +664,15 @@ export default function useRoller() {
633 proxy,
664 proxy,
634 nonce,
665 nonce,
635 address,
666 address,
667+ walletType,
668+ _web3,
636 reset || false
669 reset || false
637 );
670 );
638 const pendingTx = await api.getPendingTx(txHash);
671 const pendingTx = await api.getPendingTx(txHash);
639
672
640 return pendingTx;
673 return pendingTx;
641 },
674 },
642− [api, pointCursor, wallet]
675+ [api, pointCursor, wallet, web3, walletType]
643 );
676 );
644
677
645 // On load, get initial config
678 // On load, get initial config
src/lib/utils/roller.ts+79−26
@@ -18,13 +18,14 @@ import RollerRPCAPI, {
18import { deriveNetworkKeys, CRYPTO_SUITE_VERSION } from 'lib/keys';
18import { deriveNetworkKeys, CRYPTO_SUITE_VERSION } from 'lib/keys';
19import { addHexPrefix } from 'lib/utils/address';
19import { addHexPrefix } from 'lib/utils/address';
20import { makeDeterministicTicket, generateWallet } from 'lib/walletgen';
20import { makeDeterministicTicket, generateWallet } from 'lib/walletgen';
21+import { WALLET_TYPES } from 'lib/constants';
21
22
22export const SECOND = 1000;
23export const SECOND = 1000;
23export const MINUTE = SECOND * 60;
24export const MINUTE = SECOND * 60;
24export const HOUR = MINUTE * 60;
25export const HOUR = MINUTE * 60;
25
26
26export const padZero = (amount: number) =>
27export const padZero = (amount: number) =>
27− `${amount < 10 && amount > 0 ? '0' : ''}${amount}`;
28+ `${amount < 10 && amount >= 0 ? '0' : ''}${amount}`;
28
29
29export const hasPoint = (point: number) => (invite: Invite) =>
30export const hasPoint = (point: number) => (invite: Invite) =>
30 invite.planet === point;
31 invite.planet === point;
@@ -51,13 +52,35 @@ export const generateInviteWallet = async (point: number, seed: string) => {
51 return { ticket, inviteWallet };
52 return { ticket, inviteWallet };
52};
53};
53
54
55+export const generateHashAndSign = async (
56+ api: RollerRPCAPI,
57+ wallet: any,
58+ nonce: number,
59+ from: From,
60+ type: string,
61+ data: any,
62+ walletType: symbol,
63+ web3: any
64+) => {
65+ if (walletType === WALLET_TYPES.METAMASK) {
66+ const hash = await api.getUnsignedTx(nonce, from, type, data);
67+ const sig = await web3.eth.personal.sign(hash, wallet.address, '');
68+ return sig;
69+ } else {
70+ const hash = await api.hashTransaction(nonce, from, type, data);
71+ return signTransactionHash(hash, wallet.privateKey);
72+ }
73+};
74+
54export const spawn = async (
75export const spawn = async (
55 api: RollerRPCAPI,
76 api: RollerRPCAPI,
56− _wallet: any,
77+ wallet: any,
57 _point: number,
78 _point: number,
58 proxy: string,
79 proxy: string,
59 nonce: number,
80 nonce: number,
60− planet: number
81+ planet: number,
82+ walletType: symbol,
83+ web3: any
61) => {
84) => {
62 const from = {
85 const from = {
63 ship: _point, //ship that is spawning the planet
86 ship: _point, //ship that is spawning the planet
@@ -65,13 +88,21 @@ export const spawn = async (
65 };
88 };
66
89
67 const data = {
90 const data = {
68− address: _wallet.address,
91+ address: wallet.address,
69 ship: planet, // ship to spawn
92 ship: planet, // ship to spawn
70 };
93 };
71
94
72− const hash = await api.getUnsignedTx(nonce, from, 'spawn', data);
95+ const sig = await generateHashAndSign(
73− const sSig = signTransactionHash(hash, _wallet.privateKey);
96+ api,
97+ wallet,
98+ nonce,
99+ from,
100+ 'spawn',
101+ data,
102+ walletType,
103+ web3
104+ );
74− return api.spawn(sSig, from, _wallet.address, data);
105+ return api.spawn(sig, from, wallet.address, data);
75};
106};
76
107
77export const configureKeys = async (
108export const configureKeys = async (
@@ -81,6 +112,8 @@ export const configureKeys = async (
81 proxy: string,
112 proxy: string,
82 nonce: number,
113 nonce: number,
83 networkSeed: any,
114 networkSeed: any,
115+ walletType: symbol,
116+ web3: any,
84 breach?: boolean
117 breach?: boolean
85) => {
118) => {
86 const from = {
119 const from = {
@@ -107,18 +140,28 @@ export const configureKeys = async (
107 breach: breach || false,
140 breach: breach || false,
108 };
141 };
109
142
110− const hash = await api.getUnsignedTx(nonce, from, 'configureKeys', data);
143+ const sig = await generateHashAndSign(
111− const sig = signTransactionHash(hash, wallet.privateKey);
144+ api,
145+ wallet,
146+ nonce,
147+ from,
148+ 'configureKeys',
149+ data,
150+ walletType,
151+ web3
152+ );
112 return api.configureKeys(sig, from, wallet.address, data);
153 return api.configureKeys(sig, from, wallet.address, data);
113};
154};
114
155
115export const transferPointRequest = async (
156export const transferPointRequest = async (
116 api: RollerRPCAPI,
157 api: RollerRPCAPI,
117− _wallet: any,
158+ wallet: any,
118 pointToTransfer: Ship,
159 pointToTransfer: Ship,
119 proxy: string,
160 proxy: string,
120 nonce: number,
161 nonce: number,
121 address: string,
162 address: string,
163+ walletType: symbol,
164+ web3: any,
122 reset?: boolean
165 reset?: boolean
123) => {
166) => {
124 const from = {
167 const from = {
@@ -130,9 +173,19 @@ export const transferPointRequest = async (
130 address,
173 address,
131 reset: reset || false,
174 reset: reset || false,
132 };
175 };
133− const hash = await api.getUnsignedTx(nonce, from, 'transferPoint', data);
176+
134− const sig = signTransactionHash(hash, _wallet.privateKey);
177+ const sig = await generateHashAndSign(
178+ api,
179+ wallet,
180+ nonce,
181+ from,
182+ 'transferPoint',
183+ data,
184+ walletType,
185+ web3
186+ );
187+
135− return api.transferPoint(sig, from, _wallet.address, data);
188+ return api.transferPoint(sig, from, wallet.address, data);
136};
189};
137
190
138const proxyType = (proxy: Proxy) => {
191const proxyType = (proxy: Proxy) => {
@@ -170,33 +223,33 @@ const setProxy = async (
170
223
171export const registerProxyAddress = async (
224export const registerProxyAddress = async (
172 api: RollerRPCAPI,
225 api: RollerRPCAPI,
173− _wallet: any,
226+ wallet: any,
174 _point: Ship,
227 _point: Ship,
175 proxy: string,
228 proxy: string,
176 proxyAddressType: string,
229 proxyAddressType: string,
177 nonce: number,
230 nonce: number,
178− address: string
231+ address: string,
232+ walletType: symbol,
233+ web3: any
179) => {
234) => {
180 const from = {
235 const from = {
181 ship: _point,
236 ship: _point,
182 proxy,
237 proxy,
183 };
238 };
184− const proxyData = { address };
239+ const data = { address };
185− const proxyHash = await api.getUnsignedTx(
240+
241+ const sig = await generateHashAndSign(
242+ api,
243+ wallet,
186 nonce,
244 nonce,
187 from,
245 from,
188 proxyType(proxyAddressType),
246 proxyType(proxyAddressType),
189− proxyData
247+ data,
248+ walletType,
249+ web3
190 );
250 );
191
251
192− return setProxy(
193− api,
194− proxyAddressType,
252+ return setProxy(api, proxyAddressType, sig, from, wallet.address, data);
195− signTransactionHash(proxyHash, _wallet.privateKey),
196− from,
197− _wallet.address,
198− proxyData
199− );
200};
253};
201
254
202export const reticketL2Point = async () => {};
255export const reticketL2Point = async () => {};
src/views/Invite/Cohort.tsx+7−4
@@ -111,6 +111,10 @@ export default function InviteCohort() {
111 useEffect(() => {
111 useEffect(() => {
112 const _authToken = authToken.getOrElse(null);
112 const _authToken = authToken.getOrElse(null);
113
113
114+ if (!_authToken) {
115+ return;
116+ }
117+
114 const setUpInvite = async () => {
118 const setUpInvite = async () => {
115 setDownloadingCsv(true);
119 setDownloadingCsv(true);
116 const possiblePoints = await azimuth.azimuth.getUnspawnedChildren(
120 const possiblePoints = await azimuth.azimuth.getUnspawnedChildren(
@@ -137,7 +141,7 @@ export default function InviteCohort() {
137 _authToken
141 _authToken
138 );
142 );
139
143
140− construct(invitePoint, inviteWallet.keys.address);
144+ construct(invitePoint, inviteWallet.ownership.keys.address);
141 setL1Invite({ ticket, planet: invitePoint });
145 setL1Invite({ ticket, planet: invitePoint });
142 } else {
146 } else {
143 setError('No available planets');
147 setError('No available planets');
@@ -148,7 +152,7 @@ export default function InviteCohort() {
148
152
149 if (!currentL2 && _contracts && point && _authToken && showInviteForm) {
153 if (!currentL2 && _contracts && point && _authToken && showInviteForm) {
150 setUpInvite();
154 setUpInvite();
151− } else {
155+ } else if (!currentL2 && !showInviteForm) {
152 unconstruct();
156 unconstruct();
153 }
157 }
154 }, [
158 }, [
@@ -197,7 +201,6 @@ export default function InviteCohort() {
197 getPendingTransactions();
201 getPendingTransactions();
198 pop();
202 pop();
199 } catch (error) {
203 } catch (error) {
200− console.log(error);
201 // TODO: error is not used anywhere on the UI
204 // TODO: error is not used anywhere on the UI
202 setError(error as string);
205 setError(error as string);
203 } finally {
206 } finally {
@@ -428,7 +431,7 @@ export default function InviteCohort() {
428 </div>
431 </div>
429 {showGenerateButton && (
432 {showGenerateButton && (
430 <Button
433 <Button
431− className="generate-button"
434+ className="generate-button ph4"
432 center
435 center
433 onClick={() => setShowInviteForm(true)}>
436 onClick={() => setShowInviteForm(true)}>
434 Generate Codes
437 Generate Codes
src/views/Point/Point.scss+1−1
@@ -23,7 +23,7 @@
23 .rollup-timer {
23 .rollup-timer {
24 background-color: $rollup-yellow;
24 background-color: $rollup-yellow;
25 padding: 4px 8px;
25 padding: 4px 8px;
26− width: 110px;
26+ width: 112px;
27 border-radius: $medium-border-radius;
27 border-radius: $medium-border-radius;
28 display: flex;
28 display: flex;
29 flex-direction: row;
29 flex-direction: row;