on master help sign in
Metamask signing roller txns WIP
commitf181773a5b61f94733d69927283ed140f4384716 authorWill Galebach <willbach@gmail.com> dateMon 8 Nov 2021, 21:07 (5 years ago) parent0ca9e14
4 files changed+133−44
src/lib/useRoller.ts+46−13 src/lib/utils/roller.ts+79−26 src/views/Invite/Cohort.tsx+7−4 src/views/Point/Point.scss+1−1
src/lib/useRoller.ts+46−13
@@ -62,7 +62,13 @@ const inviteTemplate = (
6262});
6363
6464export 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();
6672 const { pointCursor }: any = usePointCursor();
6773 const { web3, contracts }: any = useNetwork();
6874 const allPoints: any = usePointCache();
@@ -189,7 +195,9 @@ export default function useRoller() {
189195 _point,
190196 proxy,
191197 nonceInc,
192− planet
198+ planet,
199+ walletType,
200+ _web3
193201 );
194202
195203 const networkSeed = await deriveNetworkSeedFromUrbitWallet(
@@ -202,7 +210,9 @@ export default function useRoller() {
202210 planet,
203211 'own',
204212 0,
205− networkSeed
213+ networkSeed,
214+ walletType,
215+ _web3
206216 );
207217
208218 const setManagementProxyRequest = registerProxyAddress(
@@ -212,7 +222,9 @@ export default function useRoller() {
212222 'own',
213223 'manage',
214224 1,
215− inviteWallet.management.keys.address
225+ inviteWallet.management.keys.address,
226+ walletType,
227+ _web3
216228 );
217229
218230 const transferRequest = transferPointRequest(
@@ -221,7 +233,9 @@ export default function useRoller() {
221233 planet,
222234 'own',
223235 2,
224− inviteWallet.ownership.keys.address
236+ inviteWallet.ownership.keys.address,
237+ walletType,
238+ _web3
225239 );
226240
227241 requests.push(
@@ -248,6 +262,7 @@ export default function useRoller() {
248262 contracts,
249263 pointCursor,
250264 wallet,
265+ walletType,
251266 web3,
252267 getDetails,
253268 ls,
@@ -448,8 +463,9 @@ export default function useRoller() {
448463 const _point = need.point(pointCursor);
449464 const azimuthPoint = await api.getPoint(_point);
450465 const _wallet = wallet.getOrElse(null);
466+ const _web3 = web3.getOrElse(null);
451467 const _details = getDetails(_point);
452− if (!_wallet || !_details) {
468+ if (!_wallet || !_details || !_web3) {
453469 // not using need because we want a custom error
454470 throw new Error('Internal Error: Missing Wallet/Details');
455471 }
@@ -484,6 +500,8 @@ export default function useRoller() {
484500 proxy!,
485501 nonce,
486502 networkSeed,
503+ walletType,
504+ _web3,
487505 breach
488506 );
489507 const pendingTx = await api.getPendingTx(txHash);
@@ -508,6 +526,7 @@ export default function useRoller() {
508526 }: ReticketParams) => {
509527 const azimuthPoint = await api.getPoint(point);
510528 const proxy = 'own';
529+ const _web3 = web3.getOrElse(null);
511530 let nonce = await api.getNonce({ ship: point, proxy });
512531
513532 let requests = [];
@@ -525,7 +544,9 @@ export default function useRoller() {
525544 point,
526545 proxy,
527546 nonce,
528− networkSeed
547+ networkSeed,
548+ walletType,
549+ _web3
529550 );
530551 nonce = nonce + 1;
531552 requests.push(configureKeysRequest);
@@ -538,7 +559,9 @@ export default function useRoller() {
538559 proxy,
539560 'manage',
540561 nonce,
541− manager
562+ manager,
563+ walletType,
564+ _web3
542565 );
543566 nonce = nonce + 1;
544567 requests.push(registerMgmtRequest);
@@ -552,7 +575,9 @@ export default function useRoller() {
552575 proxy,
553576 'spawn',
554577 nonce,
555− toWallet.spawn.keys.address
578+ toWallet.spawn.keys.address,
579+ walletType,
580+ _web3
556581 );
557582 nonce = nonce + 1;
558583 requests.push(registerSpawnRequest);
@@ -564,7 +589,9 @@ export default function useRoller() {
564589 point,
565590 proxy,
566591 nonce,
567− to
592+ to,
593+ walletType,
594+ _web3
568595 );
569596 requests.push(transferTxRequest);
570597
@@ -576,6 +603,7 @@ export default function useRoller() {
576603 async (proxyType: Proxy, address: EthAddress) => {
577604 const _point = need.point(pointCursor);
578605 const _wallet = wallet.getOrElse(null);
606+ const _web3 = web3.getOrElse(null);
579607
580608 if (!_wallet) {
581609 // not using need because we want a custom error
@@ -598,20 +626,23 @@ export default function useRoller() {
598626 proxy,
599627 proxyType,
600628 nonce,
601− address
629+ address,
630+ walletType,
631+ _web3
602632 );
603633
604634 const pendingTx = await api.getPendingTx(txHash);
605635
606636 return pendingTx;
607637 },
608− [api, pointCursor, wallet]
638+ [api, pointCursor, wallet, web3, walletType]
609639 );
610640
611641 const transferPoint = useCallback(
612642 async (address: EthAddress, reset?: boolean) => {
613643 const _point = need.point(pointCursor);
614644 const _wallet = wallet.getOrElse(null);
645+ const _web3 = web3.getOrElse(null);
615646
616647 if (!_wallet) {
617648 // not using need because we want a custom error
@@ -633,13 +664,15 @@ export default function useRoller() {
633664 proxy,
634665 nonce,
635666 address,
667+ walletType,
668+ _web3,
636669 reset || false
637670 );
638671 const pendingTx = await api.getPendingTx(txHash);
639672
640673 return pendingTx;
641674 },
642− [api, pointCursor, wallet]
675+ [api, pointCursor, wallet, web3, walletType]
643676 );
644677
645678 // On load, get initial config
src/lib/utils/roller.ts+79−26
@@ -18,13 +18,14 @@ import RollerRPCAPI, {
1818import { deriveNetworkKeys, CRYPTO_SUITE_VERSION } from 'lib/keys';
1919import { addHexPrefix } from 'lib/utils/address';
2020import { makeDeterministicTicket, generateWallet } from 'lib/walletgen';
21+import { WALLET_TYPES } from 'lib/constants';
2122
2223export const SECOND = 1000;
2324export const MINUTE = SECOND * 60;
2425export const HOUR = MINUTE * 60;
2526
2627export const padZero = (amount: number) =>
27− `${amount < 10 && amount > 0 ? '0' : ''}${amount}`;
28+ `${amount < 10 && amount >= 0 ? '0' : ''}${amount}`;
2829
2930export const hasPoint = (point: number) => (invite: Invite) =>
3031 invite.planet === point;
@@ -51,13 +52,35 @@ export const generateInviteWallet = async (point: number, seed: string) => {
5152 return { ticket, inviteWallet };
5253};
5354
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+
5475export const spawn = async (
5576 api: RollerRPCAPI,
56− _wallet: any,
77+ wallet: any,
5778 _point: number,
5879 proxy: string,
5980 nonce: number,
60− planet: number
81+ planet: number,
82+ walletType: symbol,
83+ web3: any
6184) => {
6285 const from = {
6386 ship: _point, //ship that is spawning the planet
@@ -65,13 +88,21 @@ export const spawn = async (
6588 };
6689
6790 const data = {
68− address: _wallet.address,
91+ address: wallet.address,
6992 ship: planet, // ship to spawn
7093 };
7194
72− const hash = await api.getUnsignedTx(nonce, from, 'spawn', data);
73− const sSig = signTransactionHash(hash, _wallet.privateKey);
74− return api.spawn(sSig, from, _wallet.address, data);
95+ const sig = await generateHashAndSign(
96+ api,
97+ wallet,
98+ nonce,
99+ from,
100+ 'spawn',
101+ data,
102+ walletType,
103+ web3
104+ );
105+ return api.spawn(sig, from, wallet.address, data);
75106};
76107
77108export const configureKeys = async (
@@ -81,6 +112,8 @@ export const configureKeys = async (
81112 proxy: string,
82113 nonce: number,
83114 networkSeed: any,
115+ walletType: symbol,
116+ web3: any,
84117 breach?: boolean
85118) => {
86119 const from = {
@@ -107,18 +140,28 @@ export const configureKeys = async (
107140 breach: breach || false,
108141 };
109142
110− const hash = await api.getUnsignedTx(nonce, from, 'configureKeys', data);
111− const sig = signTransactionHash(hash, wallet.privateKey);
143+ const sig = await generateHashAndSign(
144+ api,
145+ wallet,
146+ nonce,
147+ from,
148+ 'configureKeys',
149+ data,
150+ walletType,
151+ web3
152+ );
112153 return api.configureKeys(sig, from, wallet.address, data);
113154};
114155
115156export const transferPointRequest = async (
116157 api: RollerRPCAPI,
117− _wallet: any,
158+ wallet: any,
118159 pointToTransfer: Ship,
119160 proxy: string,
120161 nonce: number,
121162 address: string,
163+ walletType: symbol,
164+ web3: any,
122165 reset?: boolean
123166) => {
124167 const from = {
@@ -130,9 +173,19 @@ export const transferPointRequest = async (
130173 address,
131174 reset: reset || false,
132175 };
133− const hash = await api.getUnsignedTx(nonce, from, 'transferPoint', data);
134− const sig = signTransactionHash(hash, _wallet.privateKey);
135− return api.transferPoint(sig, from, _wallet.address, data);
176+
177+ const sig = await generateHashAndSign(
178+ api,
179+ wallet,
180+ nonce,
181+ from,
182+ 'transferPoint',
183+ data,
184+ walletType,
185+ web3
186+ );
187+
188+ return api.transferPoint(sig, from, wallet.address, data);
136189};
137190
138191const proxyType = (proxy: Proxy) => {
@@ -170,33 +223,33 @@ const setProxy = async (
170223
171224export const registerProxyAddress = async (
172225 api: RollerRPCAPI,
173− _wallet: any,
226+ wallet: any,
174227 _point: Ship,
175228 proxy: string,
176229 proxyAddressType: string,
177230 nonce: number,
178− address: string
231+ address: string,
232+ walletType: symbol,
233+ web3: any
179234) => {
180235 const from = {
181236 ship: _point,
182237 proxy,
183238 };
184− const proxyData = { address };
185− const proxyHash = await api.getUnsignedTx(
239+ const data = { address };
240+
241+ const sig = await generateHashAndSign(
242+ api,
243+ wallet,
186244 nonce,
187245 from,
188246 proxyType(proxyAddressType),
189− proxyData
247+ data,
248+ walletType,
249+ web3
190250 );
191251
192− return setProxy(
193− api,
194− proxyAddressType,
195− signTransactionHash(proxyHash, _wallet.privateKey),
196− from,
197− _wallet.address,
198− proxyData
199− );
252+ return setProxy(api, proxyAddressType, sig, from, wallet.address, data);
200253};
201254
202255export const reticketL2Point = async () => {};
src/views/Invite/Cohort.tsx+7−4
@@ -111,6 +111,10 @@ export default function InviteCohort() {
111111 useEffect(() => {
112112 const _authToken = authToken.getOrElse(null);
113113
114+ if (!_authToken) {
115+ return;
116+ }
117+
114118 const setUpInvite = async () => {
115119 setDownloadingCsv(true);
116120 const possiblePoints = await azimuth.azimuth.getUnspawnedChildren(
@@ -137,7 +141,7 @@ export default function InviteCohort() {
137141 _authToken
138142 );
139143
140− construct(invitePoint, inviteWallet.keys.address);
144+ construct(invitePoint, inviteWallet.ownership.keys.address);
141145 setL1Invite({ ticket, planet: invitePoint });
142146 } else {
143147 setError('No available planets');
@@ -148,7 +152,7 @@ export default function InviteCohort() {
148152
149153 if (!currentL2 && _contracts && point && _authToken && showInviteForm) {
150154 setUpInvite();
151− } else {
155+ } else if (!currentL2 && !showInviteForm) {
152156 unconstruct();
153157 }
154158 }, [
@@ -197,7 +201,6 @@ export default function InviteCohort() {
197201 getPendingTransactions();
198202 pop();
199203 } catch (error) {
200− console.log(error);
201204 // TODO: error is not used anywhere on the UI
202205 setError(error as string);
203206 } finally {
@@ -428,7 +431,7 @@ export default function InviteCohort() {
428431 </div>
429432 {showGenerateButton && (
430433 <Button
431− className="generate-button"
434+ className="generate-button ph4"
432435 center
433436 onClick={() => setShowInviteForm(true)}>
434437 Generate Codes
src/views/Point/Point.scss+1−1
@@ -23,7 +23,7 @@
2323 .rollup-timer {
2424 background-color: $rollup-yellow;
2525 padding: 4px 8px;
26− width: 110px;
26+ width: 112px;
2727 border-radius: $medium-border-radius;
2828 display: flex;
2929 flex-direction: row;