on master help sign in
dx: more Typescript cleanup
commit64ad57d8397410b929eccfff007a80f15ca875aa authortomholford <tomholford@users.noreply.github.com> dateThu 30 Dec 2021, 12:21 (5 years ago) parente586a74
8 files changed+65−23
src/components/Highlighted.js+1−1 src/components/View.tsx+1−2 src/components/copiable/CopiableAddressWrap.js+1−1 src/lib/flowCommand.ts+31−3 src/lib/types/ValueOf.ts+1 src/views/Bitcoin/Bitcoin.tsx+25−15 src/views/Invite/Cohort.tsx+4 src/views/Login.tsx+1−1
src/components/Highlighted.js+1−1
@@ -3,7 +3,7 @@ import cn from 'classnames';
33
44export default function Highlighted({
55 as: As = 'span',
6− className,
6+ className = '',
77 warning = false,
88 ...rest
99}) {
src/components/View.tsx+1−2
@@ -30,13 +30,12 @@ function View({
3030 full = false,
3131 hideBack = false,
3232 pop = noop,
33− header,
33+ header = React.Fragment,
3434 id,
3535 centered = false,
3636 ...rest
3737}: ViewProps) {
3838 const { reset }: any = useHistory();
39− // @ts-ignore
4039 const isMobile: boolean = useBreakpoints([true, false, false]);
4140 const shouldInset = inset && !isMobile;
4241
src/components/copiable/CopiableAddressWrap.js+1−1
@@ -3,7 +3,7 @@ import cn from 'classnames';
33
44import CopiableWithTooltip from './CopiableWithTooltip';
55
6−export default function CopiableAddressWrap({ className, ...rest }) {
6+export default function CopiableAddressWrap({ className = '', ...rest }) {
77 return (
88 <CopiableWithTooltip className={cn(className, 'mono', 'wrap')} {...rest} />
99 );
src/lib/flowCommand.ts+31−3
@@ -21,14 +21,42 @@ const COMMANDS = {
2121 XPUB: 'xpub',
2222};
2323
24−export type FlowType = { [k: string]: string | boolean } | null;
24+interface LockupFlowKind {
25+ kind: 'takeLockup';
26+ lock: 'linear' | 'conditional';
27+ from: string;
28+ [k: string]: string | boolean;
29+}
30+
31+interface BitcoinFlowKind {
32+ kind: 'btc';
33+ utx: string;
34+ [k: string]: string | boolean;
35+}
36+
37+interface XpubFlowKind {
38+ kind: 'xpub';
39+ [k: string]: string | boolean;
40+}
41+
42+interface DefaultFlowKind {
43+ kind?: string;
44+ [k: string]: string | boolean | undefined;
45+}
46+
47+export type FlowType =
48+ | LockupFlowKind
49+ | BitcoinFlowKind
50+ | XpubFlowKind
51+ | DefaultFlowKind
52+ | null;
2553
2654const useFlowCommand = () => {
2755 const flow = useMemo(() => {
2856 let flow: FlowType = {};
2957
3058 window.location.search
31− .substr(1)
59+ .substring(1)
3260 .split('&')
3361 .forEach(arg => {
3462 if ('' === arg) return;
@@ -38,7 +66,7 @@ const useFlowCommand = () => {
3866 }
3967 });
4068
41− if (typeof flow === 'object') {
69+ if (flow && typeof flow === 'object') {
4270 switch (flow.kind) {
4371 case COMMANDS.TAKE_LOCKUP:
4472 flow.lock = flow.lock || 'linear';
src/lib/types/ValueOf.tsnew file+1
@@ -0,0 +1 @@
1+export type ValueOf<T> = T[keyof T];
src/views/Bitcoin/Bitcoin.tsx+25−15
@@ -6,12 +6,12 @@ import { useWallet } from 'store/wallet';
66
77import * as bitcoin from 'bitcoinjs-lib';
88import { useLocalRouter } from 'lib/LocalRouter';
9−import { COMMANDS, useFlowCommand } from 'lib/flowCommand';
9+import { COMMANDS, FlowType, useFlowCommand } from 'lib/flowCommand';
1010import * as need from 'lib/need';
1111import useCopiable from 'lib/useCopiable';
1212
1313import View from 'components/View';
14−import L2BackHeader from 'components/L2/Headers/L2BackHeader';
14+import L2BackHeader from 'components/L2/Headers/L2BackHeader';
1515import Window from 'components/L2/Window/Window';
1616import HeaderPane from 'components/L2/Window/HeaderPane';
1717import BodyPane from 'components/L2/Window/BodyPane';
@@ -52,10 +52,12 @@ const BITCOIN_TESTNET_INFO = {
5252export default function Bitcoin() {
5353 const { pop }: any = useLocalRouter();
5454 const { urbitWallet }: any = useWallet();
55− const flow = useFlowCommand();
55+ const flow: FlowType = useFlowCommand();
5656
5757 const [signedTx, setSignedTx] = useState('');
58− const [psbt, setPsbt] = useState({});
58+ const [psbt, setPsbt] = useState<ReturnType<
59+ typeof bitcoin.Psbt.fromBase64
60+ > | null>(null);
5961
6062 // set to false for BTC testnet
6163 const isMainnet = true;
@@ -77,13 +79,14 @@ export default function Bitcoin() {
7779
7880 const [doCopy, didCopy] = useCopiable(xpub);
7981
80− const onSubmit = (values: any) => {
82+ const onSubmit = (values: { unsignedTransaction: string }) => {
8183 const newPsbt = bitcoin.Psbt.fromBase64(values.unsignedTransaction);
82−
83− const isTestnet = newPsbt.data.inputs[0].bip32Derivation[0].path.startsWith(
84− "m/84'/1'/0'/"
85− );
86−
84+ const derivations = newPsbt.data.inputs[0]?.bip32Derivation;
85+ const firstDerivation =
86+ derivations && derivations.length > 0 ? derivations[0] : null;
87+ const firstDerivationPath = firstDerivation ? firstDerivation.path : null;
88+ const isTestnet =
89+ firstDerivationPath && firstDerivationPath.startsWith("m/84'/1'/0'/");
8790 const derivationPrefix = isTestnet ? "m/84'/1'/0'/" : "m/84'/0'/0'/";
8891
8992 const btcWallet = isTestnet
@@ -91,13 +94,17 @@ export default function Bitcoin() {
9194 : bitcoin.bip32.fromBase58(zprv, BITCOIN_MAINNET_INFO);
9295
9396 try {
97+ if (!firstDerivationPath) {
98+ throw new Error('No derivation path available');
99+ }
94100 const hex = newPsbt.data.inputs
95101 .reduce((psbt, input, idx) => {
96102 // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
97− const path = input.bip32Derivation[0].path
98− .split(derivationPrefix)
99− .join('');
103+ const path = firstDerivationPath.split(derivationPrefix).join('');
100104 const prv = btcWallet.derivePath(path).privateKey;
105+ if (!prv) {
106+ throw new Error('Private key unavailable');
107+ }
101108 return psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
102109 }, newPsbt)
103110 .finalizeAllInputs()
@@ -112,9 +119,11 @@ export default function Bitcoin() {
112119 }
113120 };
114121
115− const initialValues = {};
122+ let initialValues = {
123+ unsignedTransaction: '',
124+ };
116125 if (flow && COMMANDS.BITCOIN === flow.kind) {
117− initialValues.unsignedTransaction = flow.utx;
126+ initialValues.unsignedTransaction = flow.utx as string;
118127 }
119128
120129 return (
@@ -177,6 +186,7 @@ export default function Bitcoin() {
177186 </Grid.Item>
178187 )}
179188 {signedTx &&
189+ psbt &&
180190 psbt.txOutputs.map(e => (
181191 <Grid.Item full className="mt4" key={e.address}>
182192 <span>{e.address}</span>
src/views/Invite/Cohort.tsx+4
@@ -512,6 +512,8 @@ export default function InviteCohort() {
512512 {loading && <Box>{generatingCodesText}</Box>}
513513 </Box>
514514 <Row className="download-buttons">
515+ {/*
516+ // @ts-ignore */}
515517 <Button
516518 center
517519 className="ph4 close-button"
@@ -519,6 +521,8 @@ export default function InviteCohort() {
519521 onClick={() => setShowCsvModal(false)}>
520522 Close
521523 </Button>
524+ {/*
525+ // @ts-ignore */}
522526 <Button
523527 center
524528 className="ph4 download-button"
src/views/Login.tsx+1−1
@@ -64,7 +64,7 @@ export default function Login() {
6464 };
6565
6666 return (
67− <View inset>
67+ <View inset hideBack={true}>
6868 <Grid>
6969 <Grid.Item
7070 full