on master help sign in
flows: add flow for bitcoin view

Also corrects some linter problems in validators code.

commit79c674bbb1a779c5b811e70ef7c0d639a3ce62ce authorfang <git@fang.io> dateThu 25 Feb 2021, 14:29 (6 years ago) parentf3b6250
4 files changed+37−16
src/lib/flowCommand.js+11−9 src/lib/validators.js+8−6 src/views/Bitcoin/SignTransaction.js+11−1 src/views/Login.js+7
src/lib/flowCommand.js+11−9
@@ -1,19 +1,13 @@
1−import ob from 'urbit-ob';
2−
3−import { ensurePatFormat } from 'form/formatters';
4−
51// looks into the GET parameters and attempts to construct a "flow" object,
62// which would indicate bridge is to be used for one specific flow.
73// expects to find any of the following sets of parameters, varying by kind.
84//
9−// ? kind = invite
10−// & as = ~ship // who to send the invite as
11−// & ship = ~ship // what planet to send as the invite
12−// & email = ex@amp.le // email to send the invite code to
5+// ? kind = btc
6+// & utx = somebase64string
137//
148
159const COMMANDS = {
16− //
10+ BITCOIN: 'btc',
1711};
1812
1913const useFlowCommand = () => {
@@ -29,6 +23,14 @@ const useFlowCommand = () => {
2923
3024 if (typeof flow === 'object') {
3125 switch (flow.kind) {
26+ case COMMANDS.BITCOIN:
27+ try {
28+ atob(flow.utx);
29+ } catch (e) {
30+ flow = null;
31+ }
32+ break;
33+ //
3234 default:
3335 console.log('unrecognized kind of flow:', flow.kind);
3436 // eslint-disable-next-line no-fallthrough
src/lib/validators.js+8−6
@@ -143,18 +143,20 @@ export const validatePsbt = base64 => {
143143};
144144
145145export const validateSignablePsbt = btcWallet => base64 => {
146− const psbt = bitcoin.Psbt.fromBase64(base64);
147− return !psbt.data.inputs.some((input, idx) => {
146+ const psbt = bitcoin.Psbt.fromBase64(base64);
147+ return !(
148+ psbt.data.inputs.some((input, idx) => {
148149 try {
149150 // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
150151 const path = input.bip32Derivation[0].path
151− .split("m/84'/0'/0'/")
152− .join('');
152+ .split("m/84'/0'/0'/")
153+ .join('');
153154 const prv = btcWallet.derivePath(path).privateKey;
154155 psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
155156 return true;
156157 } catch (e) {
157158 return false;
158159 }
159− }) && 'No inputs were signed';
160− };
160+ }) && 'No inputs were signed'
161+ );
162+};
src/views/Bitcoin/SignTransaction.js+11−1
@@ -2,6 +2,7 @@ import React, { useState } from 'react';
22import { Grid, Text } from 'indigo-react';
33
44import { useLocalRouter } from 'lib/LocalRouter';
5+import { COMMANDS, useFlowCommand } from 'lib/flowCommand';
56
67import View from 'components/View';
78import Highlighted from 'components/Highlighted';
@@ -23,6 +24,7 @@ import { composeValidator, buildPsbtValidator } from 'form/validators';
2324export default function SignTransaction() {
2425 const { pop } = useLocalRouter();
2526 const { urbitWallet } = useWallet();
27+ const flow = useFlowCommand();
2628
2729 const [signedTx, setSignedTx] = useState('');
2830 const [psbt, setPsbt] = useState({});
@@ -64,13 +66,21 @@ export default function SignTransaction() {
6466 setSignedTx(hex);
6567 };
6668
69+ const initialValues = {};
70+ if (COMMANDS.BITCOIN === flow.kind) {
71+ initialValues.unsignedTransaction = flow.utx;
72+ }
73+
6774 return (
6875 <View pop={pop} inset>
6976 <Grid className="mt4">
7077 <Grid.Item full className="mt4" as={Text}>
7178 Paste unsigned transactions from Landscape here
7279 </Grid.Item>
73− <BridgeForm validate={validate} onSubmit={onSubmit}>
80+ <BridgeForm
81+ validate={validate}
82+ initialValues={initialValues}
83+ onSubmit={onSubmit}>
7484 {({ handleSubmit }) => (
7585 <>
7686 <Grid.Item
src/views/Login.js+7
@@ -61,6 +61,10 @@ export default function Login() {
6161 push(names.POINTS);
6262 } else {
6363 switch (flow.kind) {
64+ case COMMANDS.BITCOIN:
65+ push(names.BITCOIN_SIGN_TRANSACTION);
66+ break;
67+ //
6468 default:
6569 throw new Error('unimplemented flow ' + flow.kind);
6670 }
@@ -69,6 +73,9 @@ export default function Login() {
6973
7074 const flowDescription = command => {
7175 switch (command.kind) {
76+ case COMMANDS.BITCOIN:
77+ return <>To sign a Bitcoin transaction, please sign in.</>;
78+ //
7279 default:
7380 return <>Flow: {command.kind}</>;
7481 }