on master help sign in
wc: make reticket flow work for walletconnect
2228876Fang
src/lib/reticket.js+4
@@ -42,6 +42,8 @@ export async function reticketPointBetweenWallets({
42 onUpdate,
42 onUpdate,
43 transferEth = false,
43 transferEth = false,
44 nextRevision = INITIAL_NETWORK_KEY_REVISION,
44 nextRevision = INITIAL_NETWORK_KEY_REVISION,
45+ txnSigner,
46+ txnSender,
45}) {
47}) {
46 const askForFunding = (address, minBalance, balance) =>
48 const askForFunding = (address, minBalance, balance) =>
47 onUpdate({
49 onUpdate({
@@ -171,6 +173,8 @@ export async function reticketPointBetweenWallets({
171 chainId,
173 chainId,
172 gasPrice: gasPriceGwei,
174 gasPrice: gasPriceGwei,
173 gasLimit: txs[i].gas,
175 gasLimit: txs[i].gas,
176+ txnSigner,
177+ txnSender,
174 });
178 });
175 txPairs.push({
179 txPairs.push({
176 raw: hexify(stx.serialize()),
180 raw: hexify(stx.serialize()),
src/lib/useEthereumTransaction.js+1
@@ -119,6 +119,7 @@ export default function useEthereumTransaction(
119 // Due to React's Rules of Hooks, optionally inject the
119 // Due to React's Rules of Hooks, optionally inject the
120 // WC transaction signing function
120 // WC transaction signing function
121 // https://reactjs.org/docs/hooks-rules.html
121 // https://reactjs.org/docs/hooks-rules.html
122+ //TODO this is duplicated across relevant callsites, should refactor
122 const txnSigner =
123 const txnSigner =
123 walletType === WALLET_TYPES.WALLET_CONNECT ? wcSign : undefined;
124 walletType === WALLET_TYPES.WALLET_CONNECT ? wcSign : undefined;
124 const txnSender =
125 const txnSender =
src/lib/useWalletConnect.ts+1−1
@@ -168,7 +168,7 @@ export const useWalletConnect = () => {
168 }: ITxData): Promise<string> => {
168 }: ITxData): Promise<string> => {
169 return new Promise((resolve, reject) => {
169 return new Promise((resolve, reject) => {
170 if (!connector || !isConnected()) {
170 if (!connector || !isConnected()) {
171− reject(new Error('No connected wallet available for signing'));
171+ reject(new Error('No connected wallet available for sending'));
172 return;
172 return;
173 }
173 }
174
174
src/views/Activate/PassportTransfer.js+1
@@ -112,6 +112,7 @@ export default function PassportTransfer({ className, resetActivateRouter }) {
112 networkType,
112 networkType,
113 onUpdate: handleUpdate,
113 onUpdate: handleUpdate,
114 transferEth: true,
114 transferEth: true,
115+ //NOTE not passing wc function, we always have invite ticket wallet
115 });
116 });
116
117
117 // set the global wallet
118 // set the global wallet
src/views/UrbitID/ResetKeys/ResetExecute.js+34−5
@@ -1,4 +1,4 @@
1−import React, { useState, useCallback } from 'react';
1+import React, { useState, useCallback, useEffect } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
2import { Just, Nothing } from 'folktale/maybe';
3import { Grid, H4, Text, ErrorText } from 'indigo-react';
3import { Grid, H4, Text, ErrorText } from 'indigo-react';
4
4
@@ -7,7 +7,6 @@ import {
7 TRANSACTION_PROGRESS,
7 TRANSACTION_PROGRESS,
8} from 'lib/reticket';
8} from 'lib/reticket';
9import * as need from 'lib/need';
9import * as need from 'lib/need';
10−import useLifecycle from 'lib/useLifecycle';
11import { WALLET_TYPES } from 'lib/constants';
10import { WALLET_TYPES } from 'lib/constants';
12import { convertToInt } from 'lib/convertToInt';
11import { convertToInt } from 'lib/convertToInt';
13import useBlockWindowClose from 'lib/useBlockWindowClose';
12import useBlockWindowClose from 'lib/useBlockWindowClose';
@@ -18,6 +17,8 @@ import { usePointCursor } from 'store/pointCursor';
18import { usePointCache } from 'store/pointCache';
17import { usePointCache } from 'store/pointCache';
19import { useHistory } from 'store/history';
18import { useHistory } from 'store/history';
20
19
20+import { useWalletConnect } from 'lib/useWalletConnect';
21+
21import { RestartButton, ForwardButton } from 'components/Buttons';
22import { RestartButton, ForwardButton } from 'components/Buttons';
22import WarningBox from 'components/WarningBox';
23import WarningBox from 'components/WarningBox';
23import LoadingBar from 'components/LoadingBar';
24import LoadingBar from 'components/LoadingBar';
@@ -54,6 +55,11 @@ export default function ResetExecute({ newWallet, setNewWallet }) {
54 } = useWallet();
55 } = useWallet();
55 const { pointCursor } = usePointCursor();
56 const { pointCursor } = usePointCursor();
56 const { getDetails } = usePointCache();
57 const { getDetails } = usePointCache();
58+ const {
59+ signTransaction: wcSign,
60+ sendTransaction: wcSend,
61+ connector,
62+ } = useWalletConnect();
57
63
58 const [generalError, setGeneralError] = useState();
64 const [generalError, setGeneralError] = useState();
59 const [progress, setProgress] = useState(0);
65 const [progress, setProgress] = useState(0);
@@ -63,11 +69,28 @@ export default function ResetExecute({ newWallet, setNewWallet }) {
63 useBlockWindowClose();
69 useBlockWindowClose();
64
70
65 // start reticketing transactions on mount
71 // start reticketing transactions on mount
66− useLifecycle(() => {
72+ useEffect(() => {
67 (async () => {
73 (async () => {
74+ // due to react shenanigans we may need to wait for the connector
75+ if (
76+ walletType === WALLET_TYPES.WALLET_CONNECT &&
77+ (!connector || !connector.connected)
78+ ) {
79+ setGeneralError(new Error('Awaiting WalletConnect connection...'));
80+ return;
81+ }
82+ setGeneralError();
83+
68 const point = need.point(pointCursor);
84 const point = need.point(pointCursor);
69 const details = need.details(getDetails(point));
85 const details = need.details(getDetails(point));
70 const networkRevision = convertToInt(details.keyRevisionNumber, 10);
86 const networkRevision = convertToInt(details.keyRevisionNumber, 10);
87+
88+ // see also comment in useEthereumTransaction
89+ const txnSigner =
90+ walletType === WALLET_TYPES.WALLET_CONNECT ? wcSign : undefined;
91+ const txnSender =
92+ walletType === WALLET_TYPES.WALLET_CONNECT ? wcSend : undefined;
93+
71 try {
94 try {
72 await reticketPointBetweenWallets({
95 await reticketPointBetweenWallets({
73 fromWallet: need.wallet(wallet),
96 fromWallet: need.wallet(wallet),
@@ -80,13 +103,19 @@ export default function ResetExecute({ newWallet, setNewWallet }) {
80 networkType,
103 networkType,
81 onUpdate: handleUpdate,
104 onUpdate: handleUpdate,
82 nextRevision: networkRevision + 1,
105 nextRevision: networkRevision + 1,
106+ txnSigner,
107+ txnSender,
83 });
108 });
84 } catch (err) {
109 } catch (err) {
85 console.error(err);
110 console.error(err);
86 setGeneralError(err);
111 setGeneralError(err);
87 }
112 }
88 })();
113 })();
89− });
114+ //NOTE this was using useLifecycle previously (which is useEffect without
115+ // dependencies) and worked fine, but in the wc case we might need to
116+ // wait for the connector to get set due to react state shenanigans.
117+ // eslint-disable-next-line react-hooks/exhaustive-deps
118+ }, [connector]);
90
119
91 const goToRestart = useCallback(() => {
120 const goToRestart = useCallback(() => {
92 reset();
121 reset();
@@ -99,7 +128,7 @@ export default function ResetExecute({ newWallet, setNewWallet }) {
99 setWalletType(WALLET_TYPES.TICKET);
128 setWalletType(WALLET_TYPES.TICKET);
100 setUrbitWallet(Just(newWallet.value.wallet));
129 setUrbitWallet(Just(newWallet.value.wallet));
101 // (implicit) pointCursor stays the same
130 // (implicit) pointCursor stays the same
102− // rediect to point
131+ // redirect to point
103 popTo(names.POINT);
132 popTo(names.POINT);
104 }, [resetWallet, setWalletType, setUrbitWallet, newWallet, popTo, names]);
133 }, [resetWallet, setWalletType, setUrbitWallet, newWallet, popTo, names]);
105
134