on master help sign in
urbit-id: add logic for L2 transferPoint
commit0bb5d7eba1c4506fe8d8839b5c0ce6130658393e authoryosoyubik <yosoyubik@gmail.com> dateWed 29 Sep 2021, 05:37 (5 years ago) parenta130fe4
3 files changed+103−36
src/lib/useRoller.ts+52−20 src/lib/utils/roller.ts+5−5 src/views/UrbitID/Transfer.js+46−11
src/lib/useRoller.ts+52−20
@@ -28,6 +28,7 @@ import {
2828 getSpawnNonce,
2929 getOwnerNonce,
3030 getManagementNonce,
31+ getTransferNonce,
3132} from './utils/nonce';
3233import { isPlanet } from './utils/point';
3334
@@ -55,7 +56,7 @@ import {
5556 configureKeys,
5657 getTimeToNextBatch,
5758 spawn,
58− transferPoint,
59+ transferPointRequest,
5960 registerProxyAddress,
6061} from './utils/roller';
6162
@@ -219,7 +220,7 @@ export default function useRoller() {
219220 inviteWallet.management.keys.address
220221 );
221222
222− const transferPointRequest = await transferPoint(
223+ const transferRequest = await transferPointRequest(
223224 api,
224225 _wallet,
225226 planet,
@@ -232,7 +233,7 @@ export default function useRoller() {
232233 spawnRequest,
233234 configureKeysRequest,
234235 setManagementProxyRequest,
235− transferPointRequest
236+ transferRequest
236237 );
237238 tickets.push({
238239 ticket,
@@ -475,7 +476,7 @@ export default function useRoller() {
475476 );
476477
477478 const keysSigningKey = fromWallet.privateKey;
478− await api.configureKeys(
479+ const configureKeysRequest = await api.configureKeys(
479480 signTransactionHash(keysHash, Buffer.from(keysSigningKey, 'hex')),
480481 fromOwnerProxy,
481482 ownerAddress,
@@ -486,7 +487,7 @@ export default function useRoller() {
486487 // increaseNonce(point, operator.proxy!);
487488
488489 // 2. Set Management Proxy
489− await registerProxyAddress(
490+ const registerProxyAddressRequest = await registerProxyAddress(
490491 api,
491492 fromWallet,
492493 point,
@@ -497,27 +498,24 @@ export default function useRoller() {
497498 );
498499
499500 // 3. Transfer point
500− const transferData = { address: to, reset: false };
501− const transferHash: Hash = await api.hashTransaction(
501+ const transferTxRequest = await transferPointRequest(
502+ api,
503+ fromWallet,
504+ point,
505+ operator.proxy!,
502506 nonce + 2,
503− fromOwnerProxy,
504− 'transferPoint',
505− transferData
507+ to
506508 );
507−
508− const transferTxHash = await api.transferPoint(
509− signTransactionHash(transferHash, Buffer.from(keysSigningKey, 'hex')),
510− fromOwnerProxy,
511− ownerAddress,
512− transferData
513− );
514−
515− return transferTxHash;
509+ const hashes = await Promise.all([
510+ configureKeysRequest,
511+ registerProxyAddressRequest,
512+ transferTxRequest,
513+ ]);
514+ return hashes;
516515 };
517516
518517 const setProxyAddress = useCallback(
519518 async (proxyType: Proxy, address: EthAddress) => {
520− console.log(proxyType, address);
521519 const _point = need.point(pointCursor);
522520 const _wallet = wallet.getOrElse(null);
523521
@@ -551,6 +549,40 @@ export default function useRoller() {
551549 [api, pointCursor, wallet, nonces]
552550 );
553551
552+ const transferPoint = useCallback(
553+ async (address: EthAddress, reset?: boolean) => {
554+ const _point = need.point(pointCursor);
555+ const _wallet = wallet.getOrElse(null);
556+
557+ if (!_wallet) {
558+ // not using need because we want a custom error
559+ throw new Error('Internal Error: Missing Wallet');
560+ }
561+
562+ const pointDetails = nonces[_point];
563+ const operator =
564+ getTransferNonce(pointDetails, _wallet.address) ||
565+ getOwnerNonce(pointDetails, _wallet.address);
566+
567+ if (operator === undefined)
568+ throw new Error("Error: Address doesn't match proxy");
569+
570+ const txHash = await transferPointRequest(
571+ api,
572+ _wallet,
573+ _point,
574+ operator.proxy!,
575+ operator.nonce!,
576+ address,
577+ reset || false
578+ );
579+ const pendingTx = await api.getPendingTx(txHash);
580+
581+ return pendingTx;
582+ },
583+ [api, pointCursor, wallet, nonces]
584+ );
585+
554586 // On load, get initial config
555587 useEffect(() => {
556588 if (config) {
src/lib/utils/roller.ts+5−5
@@ -110,13 +110,14 @@ export const configureKeys = async (
110110 return api.configureKeys(sig, from, _wallet.address, data);
111111};
112112
113−export const transferPoint = async (
113+export const transferPointRequest = async (
114114 api: any,
115115 _wallet: any,
116− _point: number,
116+ _point: Ship,
117117 proxy: string,
118118 nonce: number,
119− address: string
119+ address: string,
120+ reset?: boolean
120121) => {
121122 const from = {
122123 ship: _point, // ship to transfer
@@ -125,9 +126,8 @@ export const transferPoint = async (
125126
126127 const data = {
127128 address,
128− reset: false,
129+ reset: reset || false,
129130 };
130−
131131 const hash = await api.hashTransaction(nonce, from, 'transferPoint', data);
132132 const sig = signTransactionHash(hash, _wallet.privateKey);
133133 return api.transferPoint(sig, from, _wallet.address, data);
src/views/UrbitID/Transfer.js+46−11
@@ -1,18 +1,20 @@
1−import React, { useCallback, useMemo } from 'react';
1+import React, { useCallback, useMemo, useState } from 'react';
22import cn from 'classnames';
3−import { Grid, Text } from 'indigo-react';
3+import { Grid, Text, Button } from 'indigo-react';
44import * as azimuth from 'azimuth-js';
55
66import { useNetwork } from 'store/network';
77import { usePointCursor } from 'store/pointCursor';
88import { usePointCache } from 'store/pointCache';
99import { useStarReleaseCache } from 'store/starRelease';
10+import { useRollerStore } from 'store/roller';
1011
1112import * as need from 'lib/need';
1213import { useLocalRouter } from 'lib/LocalRouter';
1314import useCurrentPointName from 'lib/useCurrentPointName';
1415import useEthereumTransaction from 'lib/useEthereumTransaction';
1516import { GAS_LIMITS } from 'lib/constants';
17+import useRoller from 'lib/useRoller';
1618
1719import ViewHeader from 'components/ViewHeader';
1820import NoticeBox from 'components/NoticeBox';
@@ -45,6 +47,9 @@ export default function AdminTransfer() {
4547 const name = useCurrentPointName();
4648 const { pointCursor } = usePointCursor();
4749 const { starReleaseDetails } = useStarReleaseCache();
50+ const { currentL2 } = useRollerStore();
51+ const { transferPoint, getPendingTransactions } = useRoller();
52+ const [owner, setNewOwner] = useState('');
4853
4954 const {
5055 construct,
@@ -54,29 +59,46 @@ export default function AdminTransfer() {
5459 bind,
5560 } = useTransfer();
5661
62+ const point = need.point(pointCursor);
63+
5764 const needLockupWarning = useMemo(() => {
58− const point = need.point(pointCursor);
5965 const az = azimuth.azimuth;
6066 return (
6167 az.getPointSize(point) === az.PointSize.Galaxy &&
6268 starReleaseDetails.map(a => a.kind).getOrElse('none') !== 'none'
6369 );
64− }, [pointCursor, starReleaseDetails]);
70+ }, [starReleaseDetails, point]);
6571
6672 const validate = useMemo(
6773 () => composeValidator({ address: buildAddressValidator() }),
6874 []
6975 );
7076
77+ const onSignTx = useCallback(async () => {
78+ // setLoading(true);
79+ if (owner === '') return;
80+ try {
81+ await transferPoint(owner);
82+ getPendingTransactions(point);
83+ pop();
84+ } catch (error) {
85+ // setError(error);
86+ } finally {
87+ // setLoading(false);
88+ }
89+ }, [owner, getPendingTransactions, point, pop, transferPoint]);
90+
7191 const onValues = useCallback(
7292 ({ valid, values }) => {
7393 if (valid) {
7494 construct(values.address);
95+ setNewOwner(values.address);
7596 } else {
7697 unconstruct();
98+ setNewOwner('');
7799 }
78100 },
79− [construct, unconstruct]
101+ [construct, unconstruct, setNewOwner]
80102 );
81103
82104 return (
@@ -127,12 +149,25 @@ export default function AdminTransfer() {
127149
128150 <Grid.Item full as={FormError} />
129151
130− <Grid.Item
131− full
132− as={InlineEthereumTransaction}
133− {...bind}
134− onReturn={() => pop()}
135− />
152+ {currentL2 ? (
153+ <Grid.Item
154+ as={Button}
155+ full
156+ className="set-proxy"
157+ center
158+ solid
159+ disabled={values.unset}
160+ onClick={onSignTx}>
161+ {'Sign Transaction'}
162+ </Grid.Item>
163+ ) : (
164+ <Grid.Item
165+ full
166+ as={InlineEthereumTransaction}
167+ {...bind}
168+ onReturn={() => pop()}
169+ />
170+ )}
136171 </>
137172 )}
138173 </BridgeForm>