on master help sign in
Merge pull request #534 from pkova/master

bitcoin: implement bitcoin view

commit9916088c541250d1e27d33d7067d6f5b786411b6 authorfang <git@fang.io> dateWed 3 Mar 2021, 22:05 (6 years ago) parent29f4a14fb30b41
14 files changed+324−5
package.json+4−1 src/components/CopiableAddress.js+6−1 src/components/CopiableAddressWrap.js+10 src/components/CopiableWithTooltip.js+1−2 src/form/Inputs.js+11 src/form/validators.js+6 src/lib/need.js+4 src/lib/routeNames.js+3 src/lib/router.js+6 src/lib/validators.js+26 src/views/Bitcoin/Bitcoin.js+44 src/views/Bitcoin/SignTransaction.js+122 src/views/Bitcoin/Xpub.js+66 src/views/Point.js+15−1
package.json+4−1
@@ -25,7 +25,9 @@
2525 "azimuth-solidity": "^1.2.1",
2626 "bip32": "^1.0.2",
2727 "bip39": "^2.5.0",
28+ "bitcoinjs-lib": "^5.2.0",
2829 "bn.js": "^4.11.8",
30+ "buffer": "^5.6.1",
2931 "classnames": "^2.2.6",
3032 "copy-to-clipboard": "^3.2.0",
3133 "ethereum-blockies-base64": "^1.0.2",
@@ -51,8 +53,9 @@
5153 "react-scripts": "3.0.1",
5254 "react-teleporter": "^1.1.0",
5355 "secp256k1": "^3.5.2",
56+ "bs58check": "^2.1.2",
5457 "trezor-connect": "^8.1.14",
55− "urbit-key-generation": "^0.17.4",
58+ "urbit-key-generation": "^0.18.0",
5659 "urbit-ob": "^4.1.4",
5760 "urbit-paper-renderer": "^2.0.9",
5861 "web3": "1.0.0-beta.54",
src/components/CopiableAddress.js+6−1
@@ -4,5 +4,10 @@ import cn from 'classnames';
44import CopiableWithTooltip from './CopiableWithTooltip';
55
66export default function CopiableAddress({ className, ...rest }) {
7− return <CopiableWithTooltip className={cn(className, 'mono')} {...rest} />;
7+ return (
8+ <CopiableWithTooltip
9+ className={cn(className, 'mono', 'nowrap')}
10+ {...rest}
11+ />
12+ );
813}
src/components/CopiableAddressWrap.jsnew file+10
@@ -0,0 +1,10 @@
1+import React from 'react';
2+import cn from 'classnames';
3+
4+import CopiableWithTooltip from './CopiableWithTooltip';
5+
6+export default function CopiableAddressWrap({ className, ...rest }) {
7+ return (
8+ <CopiableWithTooltip className={cn(className, 'mono', 'wrap')} {...rest} />
9+ );
10+}
src/components/CopiableWithTooltip.js+1−2
@@ -1,5 +1,4 @@
11import React from 'react';
2−import cn from 'classnames';
32import { ReactComponent as Copy } from 'assets/copy.svg';
43
54import useCopiable from 'lib/useCopiable';
@@ -15,7 +14,7 @@ export default function CopiableWithTooltip({
1514 const [doCopy, didCopy] = useCopiable(text || children);
1615
1716 return (
18− <As className={cn(className, 'nowrap')} {...rest}>
17+ <As className={className} {...rest}>
1918 {children}
2019 <WithTooltip content={didCopy ? 'Copied!' : 'Copy'}>
2120 <Copy
src/form/Inputs.js+11
@@ -163,6 +163,17 @@ export function PrivateKeyInput({ ...rest }) {
163163 );
164164}
165165
166+export function PsbtInput({ ...rest }) {
167+ return (
168+ <HexInput
169+ type="text"
170+ placeholder=""
171+ config={{ parse: stripHexPrefix }}
172+ {...rest}
173+ />
174+ );
175+}
176+
166177export function AddressInput({ ...rest }) {
167178 return (
168179 <Input
src/form/validators.js+6
@@ -19,6 +19,8 @@ import {
1919 validateHexPrefix,
2020 validateLength,
2121 validateUnique,
22+ validatePsbt,
23+ validateSignablePsbt,
2224} from 'lib/validators';
2325import isPromise from 'lib/isPromise';
2426
@@ -121,6 +123,10 @@ export const buildEmailValidator = validate =>
121123
122124export const buildEmailArrayValidator = () =>
123125 buildValidator([validateUnique, buildArrayValidator(buildEmailValidator())]);
126+
127+export const buildPsbtValidator = btcWallet =>
128+ buildValidator([validatePsbt, validateSignablePsbt(btcWallet)]);
129+
124130// the form validator is the composition of all of the field validators
125131// plus an additional form validator function
126132export const composeValidator = (
src/lib/need.js+4
@@ -33,6 +33,10 @@ export const wallet = needBuilder(() => {
3333 throw new Error(BRIDGE_ERROR.MISSING_WALLET);
3434});
3535
36+export const urbitWallet = needBuilder(() => {
37+ throw new Error(BRIDGE_ERROR.MISSING_URBIT_WALLET);
38+});
39+
3640export const addressFromWallet = obj => wallet(obj).address;
3741
3842export const point = needBuilder(() => {
src/lib/routeNames.js+3
@@ -27,4 +27,7 @@ export const ROUTE_NAMES = {
2727 URBIT_ID: Symbol('URBIT_ID'),
2828 RESIDENTS: Symbol('RESIDENTS'),
2929 ADOPT: Symbol('ADOPT'),
30+ BITCOIN: Symbol('BITCOIN'),
31+ BITCOIN_XPUB: Symbol('BITCOIN_XPUB'),
32+ BITCOIN_SIGN_TRANSACTION: Symbol('BITCOIN_SIGN_TRANSACTION'),
3033};
src/lib/router.js+6
@@ -18,6 +18,9 @@ import UrbitOS from 'views/UrbitOS';
1818import UrbitID from 'views/UrbitID';
1919import Residents from 'views/Residents';
2020import Adopt from 'views/Adopt';
21+import Bitcoin from 'views/Bitcoin/Bitcoin';
22+import Xpub from 'views/Bitcoin/Xpub';
23+import SignTransaction from 'views/Bitcoin/SignTransaction';
2124
2225import { ROUTE_NAMES } from './routeNames';
2326
@@ -42,4 +45,7 @@ export const ROUTES = {
4245 [ROUTE_NAMES.URBIT_ID]: UrbitID,
4346 [ROUTE_NAMES.RESIDENTS]: Residents,
4447 [ROUTE_NAMES.ADOPT]: Adopt,
48+ [ROUTE_NAMES.BITCOIN]: Bitcoin,
49+ [ROUTE_NAMES.BITCOIN_XPUB]: Xpub,
50+ [ROUTE_NAMES.BITCOIN_SIGN_TRANSACTION]: SignTransaction,
4551};
src/lib/validators.js+26
@@ -1,4 +1,5 @@
11import * as bip39 from 'bip39';
2+import * as bitcoin from 'bitcoinjs-lib';
23import ob from 'urbit-ob';
34import { includes } from 'lodash';
45
@@ -132,3 +133,28 @@ export const validateUnique = arr => {
132133
133134export const validateChild = ourShip => ship =>
134135 ourShip !== ob.sein(ship) && `This point is not a child of ${ourShip}.`;
136+
137+export const validatePsbt = base64 => {
138+ try {
139+ bitcoin.Psbt.fromBase64(base64);
140+ } catch (e) {
141+ return 'Invalid Partially Signed Bitcoin Transaction';
142+ }
143+};
144+
145+export const validateSignablePsbt = btcWallet => base64 => {
146+ const psbt = bitcoin.Psbt.fromBase64(base64);
147+ return !psbt.data.inputs.some((input, idx) => {
148+ try {
149+ // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
150+ const path = input.bip32Derivation[0].path
151+ .split("m/84'/0'/0'/")
152+ .join('');
153+ const prv = btcWallet.derivePath(path).privateKey;
154+ psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
155+ return true;
156+ } catch (e) {
157+ return false;
158+ }
159+ }) && 'No inputs were signed';
160+ };
src/views/Bitcoin/Bitcoin.jsnew file+44
@@ -0,0 +1,44 @@
1+import React, { useCallback } from 'react';
2+import { Grid } from 'indigo-react';
3+
4+import { useLocalRouter } from 'lib/LocalRouter';
5+
6+import View from 'components/View';
7+import { ForwardButton } from 'components/Buttons';
8+
9+export default function Bitcoin() {
10+ const { pop, push, names } = useLocalRouter();
11+
12+ const goXpub = useCallback(() => push(names.BITCOIN_XPUB), [push, names]);
13+ const goSignTransaction = useCallback(
14+ () => push(names.BITCOIN_SIGN_TRANSACTION),
15+ [push, names]
16+ );
17+
18+ return (
19+ <View pop={pop} inset>
20+ <Grid.Item full className="mb4 f5">
21+ Bitcoin
22+ </Grid.Item>
23+ <Grid.Divider />
24+ <Grid className="pt2">
25+ <Grid.Item
26+ full
27+ as={ForwardButton}
28+ detail="Xpub for Landscape wallet"
29+ onClick={goXpub}>
30+ Xpub
31+ </Grid.Item>
32+ <Grid.Divider />
33+ <Grid.Item
34+ full
35+ as={ForwardButton}
36+ className="mt1"
37+ detail="Sign transaction from Landscape"
38+ onClick={goSignTransaction}>
39+ Sign Transaction
40+ </Grid.Item>
41+ </Grid>
42+ </View>
43+ );
44+}
src/views/Bitcoin/SignTransaction.jsnew file+122
@@ -0,0 +1,122 @@
1+import React, { useState } from 'react';
2+import { Grid, Text } from 'indigo-react';
3+
4+import { useLocalRouter } from 'lib/LocalRouter';
5+
6+import View from 'components/View';
7+import Highlighted from 'components/Highlighted';
8+import CopiableAddressWrap from 'components/CopiableAddressWrap';
9+import ViewHeader from 'components/ViewHeader';
10+import SubmitButton from 'form/SubmitButton';
11+
12+import { useWallet } from 'store/wallet';
13+
14+import * as need from 'lib/need';
15+import * as bitcoin from 'bitcoinjs-lib';
16+
17+import BridgeForm from 'form/BridgeForm';
18+import { PsbtInput } from 'form/Inputs';
19+
20+import { Buffer } from 'buffer';
21+import { composeValidator, buildPsbtValidator } from 'form/validators';
22+
23+export default function SignTransaction() {
24+ const { pop } = useLocalRouter();
25+ const { urbitWallet } = useWallet();
26+
27+ const [signedTx, setSignedTx] = useState('');
28+ const [psbt, setPsbt] = useState({});
29+
30+ const { private: privKey, chain } = need.urbitWallet(
31+ urbitWallet
32+ ).bitcoin.keys;
33+
34+ const btcWallet = bitcoin.bip32.fromPrivateKey(
35+ Buffer.from(privKey, 'hex'),
36+ Buffer.from(chain, 'hex'),
37+ bitcoin.networks.bitcoin
38+ );
39+
40+ const validate = composeValidator({
41+ unsignedTransaction: buildPsbtValidator(btcWallet),
42+ });
43+
44+ const onSubmit = values => {
45+ const newPsbt = bitcoin.Psbt.fromBase64(values.unsignedTransaction);
46+ const hex = newPsbt.data.inputs
47+ .reduce((psbt, input, idx) => {
48+ // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
49+ const path = input.bip32Derivation[0].path
50+ .split("m/84'/0'/0'/")
51+ .join('');
52+ const prv = btcWallet.derivePath(path).privateKey;
53+ try {
54+ return psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
55+ } catch (e) {
56+ return psbt;
57+ }
58+ }, newPsbt)
59+ .finalizeAllInputs()
60+ .extractTransaction()
61+ .toHex();
62+
63+ setPsbt(newPsbt);
64+ setSignedTx(hex);
65+ };
66+
67+ return (
68+ <View pop={pop} inset>
69+ <Grid className="mt4">
70+ <Grid.Item full className="mt4" as={Text}>
71+ Paste unsigned transactions from Landscape here
72+ </Grid.Item>
73+ <BridgeForm validate={validate} onSubmit={onSubmit}>
74+ {({ handleSubmit }) => (
75+ <>
76+ <Grid.Item
77+ full
78+ as={PsbtInput}
79+ name="unsignedTransaction"
80+ label="Unsigned transaction"
81+ className="mt4"
82+ />
83+ <Grid.Item
84+ full
85+ solid
86+ className="mt2"
87+ accessory=""
88+ handleSubmit={handleSubmit}
89+ as={SubmitButton}>
90+ Sign Transaction
91+ </Grid.Item>
92+ </>
93+ )}
94+ </BridgeForm>
95+ {signedTx && (
96+ <Grid.Item full className="mt4" as={Text}>
97+ Transaction outputs:
98+ </Grid.Item>
99+ )}
100+ {signedTx &&
101+ psbt.txOutputs.map(e => (
102+ <Grid.Item full className="mt4" key={e.address}>
103+ <span>{e.address}</span>
104+ <div className="mt1 mono black f6">{e.value} sats</div>
105+ </Grid.Item>
106+ ))}
107+ {signedTx && (
108+ <>
109+ <Grid.Item full className="mt4" as={ViewHeader}>
110+ Signed transaction:
111+ </Grid.Item>
112+ <Grid.Item full as={Text}>
113+ <Highlighted>
114+ <CopiableAddressWrap>{signedTx}</CopiableAddressWrap>
115+ </Highlighted>
116+ </Grid.Item>
117+ </>
118+ )}
119+ </Grid>
120+ </View>
121+ );
122+}
src/views/Bitcoin/Xpub.jsnew file+66
@@ -0,0 +1,66 @@
1+import React from 'react';
2+import { Grid, Text } from 'indigo-react';
3+import { useWallet } from 'store/wallet';
4+
5+import { useLocalRouter } from 'lib/LocalRouter';
6+
7+import ViewHeader from 'components/ViewHeader';
8+import View from 'components/View';
9+import WarningBox from 'components/WarningBox';
10+
11+import CopiableAddressWrap from 'components/CopiableAddressWrap';
12+import Highlighted from 'components/Highlighted';
13+
14+import * as need from 'lib/need';
15+import * as bitcoin from 'bitcoinjs-lib';
16+import * as bs58check from 'bs58check';
17+import { Buffer } from 'buffer';
18+
19+function xpubToZpub(xpub) {
20+ var data = bs58check.decode(xpub);
21+ data = data.slice(4);
22+ data = Buffer.concat([Buffer.from('04b24746', 'hex'), data]);
23+ return bs58check.encode(data);
24+}
25+
26+export default function Xpub() {
27+ const { pop } = useLocalRouter();
28+ const { urbitWallet } = useWallet();
29+
30+ const { public: pubKey, chain } = need.urbitWallet(urbitWallet).bitcoin.keys;
31+
32+ const zPub = xpubToZpub(
33+ bitcoin.bip32
34+ .fromPublicKey(
35+ Buffer.from(pubKey, 'hex'),
36+ Buffer.from(chain, 'hex'),
37+ bitcoin.networks.bitcoin
38+ )
39+ .toBase58()
40+ );
41+
42+ return (
43+ <View pop={pop} inset>
44+ <Grid className="mt4">
45+ <Grid.Item full as={ViewHeader}>
46+ Bitcoin
47+ </Grid.Item>
48+ <Grid.Item full as={WarningBox}>
49+ We do not recommend using this feature for significant amounts of
50+ money, since your private keys are exposed to the browser.
51+ </Grid.Item>
52+ <Grid.Item full className="mt4" as={Text}>
53+ The public key derived from your master ticket.
54+ </Grid.Item>
55+ <Grid.Item full className="mt4" as={Text}>
56+ Paste it into landscape while setting up your wallet.
57+ </Grid.Item>
58+ <Grid.Item full className="mt4" as={Text}>
59+ <Highlighted>
60+ <CopiableAddressWrap>{zPub}</CopiableAddressWrap>
61+ </Highlighted>
62+ </Grid.Item>
63+ </Grid>
64+ </View>
65+ );
66+}
src/views/Point.js+15−1
@@ -114,7 +114,7 @@ export default function Point() {
114114 const { pop, push, names } = useLocalRouter();
115115 const { pointCursor } = usePointCursor();
116116
117− const { wallet } = useWallet();
117+ const { wallet, urbitWallet } = useWallet();
118118 const point = need.point(pointCursor);
119119
120120 const { getResidents } = usePointCache();
@@ -130,6 +130,8 @@ export default function Point() {
130130 isOwner,
131131 } = useCurrentPermissions();
132132
133+ const canBitcoin = Just.hasInstance(urbitWallet);
134+
133135 // fetch the invites for the current cursor
134136 const invites = useInvites(point);
135137 const {
@@ -156,6 +158,8 @@ export default function Point() {
156158
157159 const goUrbitOS = useCallback(() => push(names.URBIT_OS), [push, names]);
158160
161+ const goBitcoin = useCallback(() => push(names.BITCOIN), [push, names]);
162+
159163 const goUrbitID = useCallback(() => push(names.URBIT_ID), [push, names]);
160164
161165 const goResidents = useCallback(() => push(names.RESIDENTS), [push, names]);
@@ -287,6 +291,16 @@ export default function Point() {
287291 OS
288292 </Grid.Item>
289293 <Grid.Divider />
294+ <Grid.Item
295+ full
296+ as={ForwardButton}
297+ disabled={!canBitcoin}
298+ className="mt1"
299+ detail="Bitcoin management"
300+ onClick={goBitcoin}>
301+ Bitcoin
302+ </Grid.Item>
303+ <Grid.Divider />
290304 {isParent && (
291305 <>
292306 <Grid.Item