on master help sign in
bitcoin: add testnet support
commita028757538a020f012e6131da7192c776675c2f7 authorpkova <pyry.kovanen@gmail.com> dateMon 24 May 2021, 22:00 (5 years ago) parent28669e8
5 files changed+92−76
package.json+12−4 src/form/validators.js+1−2 src/lib/validators.js−19 src/views/Bitcoin/SignTransaction.js+56−30 src/views/Bitcoin/Xpub.js+23−21
package.json+12−4
@@ -18,6 +18,11 @@
1818 "@hot-loader/react-dom": "^16.8.6",
1919 "@ledgerhq/hw-app-eth": "^5.5.0",
2020 "@ledgerhq/hw-transport-u2f": "^5.5.0",
21+ "@reach/disclosure": "^0.15.0",
22+ "@reach/menu-button": "^0.15.1",
23+ "@reach/tabs": "^0.15.0",
24+ "@tlon/indigo-light": "^1.0.7",
25+ "@tlon/indigo-react": "^1.2.23",
2126 "@tlon/sigil-js": "^1.4.1",
2227 "@welldone-software/why-did-you-render": "^3.2.3",
2328 "async-retry": "^1.2.3",
@@ -37,25 +42,28 @@
3742 "final-form-arrays": "^3.0.1",
3843 "final-form-set-field-data": "^1.0.2",
3944 "folktale": "^2.3.1",
45+ "formik": "^2.2.7",
4046 "indigo-tokens": "^1.2.3",
4147 "jszip": "^3.1.5",
4248 "keccak": "^1.4.0",
4349 "keythereum": "^1.0.4",
4450 "lodash": "^4.17.14",
51+ "markdown-to-jsx": "^7.1.2",
4552 "more-entropy": "^0.0.7",
4653 "new-github-issue-url": "^0.2.1",
4754 "prop-types": "^15.6.2",
48− "react": "^16.8.6",
49− "react-dom": "^16.8.6",
55+ "react": "^16.14.0",
56+ "react-dom": "^16.14.0",
5057 "react-final-form": "^6.3.0",
5158 "react-final-form-arrays": "^3.1.0",
5259 "react-hot-loader": "^4.12.0",
5360 "react-scripts": "3.0.1",
5461 "react-teleporter": "^1.1.0",
5562 "secp256k1": "^3.5.2",
56− "bs58check": "^2.1.2",
63+ "styled-components": "^5.3.0",
64+ "styled-system": "^5.1.5",
5765 "trezor-connect": "^8.1.14",
58− "urbit-key-generation": "^0.18.0",
66+ "urbit-key-generation": "^0.19.0",
5967 "urbit-ob": "^4.1.4",
6068 "urbit-paper-renderer": "^2.0.9",
6169 "web3": "1.0.0-beta.54",
src/form/validators.js+1−2
@@ -20,7 +20,6 @@ import {
2020 validateLength,
2121 validateUnique,
2222 validatePsbt,
23− validateSignablePsbt,
2423} from 'lib/validators';
2524import isPromise from 'lib/isPromise';
2625
@@ -125,7 +124,7 @@ export const buildEmailArrayValidator = () =>
125124 buildValidator([validateUnique, buildArrayValidator(buildEmailValidator())]);
126125
127126export const buildPsbtValidator = btcWallet =>
128− buildValidator([validatePsbt, validateSignablePsbt(btcWallet)]);
127+ buildValidator([validatePsbt]);
129128
130129// the form validator is the composition of all of the field validators
131130// plus an additional form validator function
src/lib/validators.js−19
@@ -141,22 +141,3 @@ export const validatePsbt = base64 => {
141141 return 'Invalid Partially Signed Bitcoin Transaction';
142142 }
143143};
144−
145−export const validateSignablePsbt = btcWallet => base64 => {
146− const psbt = bitcoin.Psbt.fromBase64(base64);
147− return !(
148− psbt.data.inputs.some((input, idx) => {
149− try {
150− // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
151− const path = input.bip32Derivation[0].path
152− .split("m/84'/0'/0'/")
153− .join('');
154− const prv = btcWallet.derivePath(path).privateKey;
155− psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
156− return true;
157− } catch (e) {
158− return false;
159− }
160− }) && 'No inputs were signed'
161− );
162−};
src/views/Bitcoin/SignTransaction.js+56−30
@@ -18,9 +18,32 @@ import * as bitcoin from 'bitcoinjs-lib';
1818import BridgeForm from 'form/BridgeForm';
1919import { PsbtInput } from 'form/Inputs';
2020
21−import { Buffer } from 'buffer';
2221import { composeValidator, buildPsbtValidator } from 'form/validators';
2322
23+const BITCOIN_MAINNET_INFO = {
24+ messagePrefix: '\x18Bitcoin Signed Message:\n',
25+ bech32: 'bc',
26+ bip32: {
27+ public: 0x04b24746,
28+ private: 0x04b2430c,
29+ },
30+ pubKeyHash: 0x00,
31+ scriptHash: 0x05,
32+ wif: 0x80,
33+};
34+
35+const BITCOIN_TESTNET_INFO = {
36+ messagePrefix: '\x18Bitcoin Signed Message:\n',
37+ bech32: 'tb',
38+ bip32: {
39+ public: 0x045f1cf6,
40+ private: 0x045f18bc,
41+ },
42+ pubKeyHash: 0x6f,
43+ scriptHash: 0xc4,
44+ wif: 0xef,
45+};
46+
2447export default function SignTransaction() {
2548 const { pop } = useLocalRouter();
2649 const { urbitWallet } = useWallet();
@@ -29,41 +52,44 @@ export default function SignTransaction() {
2952 const [signedTx, setSignedTx] = useState('');
3053 const [psbt, setPsbt] = useState({});
3154
32− const { private: privKey, chain } = need.urbitWallet(
33− urbitWallet
34− ).bitcoin.keys;
35−
36− const btcWallet = bitcoin.bip32.fromPrivateKey(
37− Buffer.from(privKey, 'hex'),
38− Buffer.from(chain, 'hex'),
39− bitcoin.networks.bitcoin
40− );
55+ const { xprv: zprv } = need.urbitWallet(urbitWallet).bitcoinMainnet.keys;
56+ const { xprv: vprv } = need.urbitWallet(urbitWallet).bitcoinTestnet.keys;
4157
4258 const validate = composeValidator({
43− unsignedTransaction: buildPsbtValidator(btcWallet),
59+ unsignedTransaction: buildPsbtValidator(),
4460 });
4561
46− const onSubmit = values => {
62+ const onSubmit = (values, form) => {
4763 const newPsbt = bitcoin.Psbt.fromBase64(values.unsignedTransaction);
48− const hex = newPsbt.data.inputs
49− .reduce((psbt, input, idx) => {
50− // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
51− const path = input.bip32Derivation[0].path
52− .split("m/84'/0'/0'/")
53− .join('');
54− const prv = btcWallet.derivePath(path).privateKey;
55− try {
64+
65+ const isTestnet = newPsbt.data.inputs[0].bip32Derivation[0].path.startsWith(
66+ "m/84'/1'/0'/"
67+ );
68+
69+ const derivationPrefix = isTestnet ? "m/84'/1'/0'/" : "m/84'/0'/0'/";
70+
71+ const btcWallet = isTestnet
72+ ? bitcoin.bip32.fromBase58(vprv, BITCOIN_TESTNET_INFO)
73+ : bitcoin.bip32.fromBase58(zprv, BITCOIN_MAINNET_INFO);
74+
75+ try {
76+ const hex = newPsbt.data.inputs
77+ .reduce((psbt, input, idx) => {
78+ // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
79+ const path = input.bip32Derivation[0].path
80+ .split(derivationPrefix)
81+ .join('');
82+ const prv = btcWallet.derivePath(path).privateKey;
5683 return psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
57− } catch (e) {
58− return psbt;
59− }
60− }, newPsbt)
61− .finalizeAllInputs()
62− .extractTransaction()
63− .toHex();
64−
65− setPsbt(newPsbt);
66− setSignedTx(hex);
84+ }, newPsbt)
85+ .finalizeAllInputs()
86+ .extractTransaction()
87+ .toHex();
88+ setPsbt(newPsbt);
89+ setSignedTx(hex);
90+ } catch (error) {
91+ return { unsignedTransaction: 'Can not sign transaction' };
92+ }
6793 };
6894
6995 const initialValues = {};
src/views/Bitcoin/Xpub.js+23−21
@@ -1,5 +1,7 @@
11import React from 'react';
2+import { useState } from 'react';
23import { Grid, Text } from 'indigo-react';
4+import { ToggleSwitch } from '@tlon/indigo-react';
35import { useWallet } from 'store/wallet';
46
57import { useLocalRouter } from 'lib/LocalRouter';
@@ -12,32 +14,16 @@ import CopiableAddressWrap from 'components/CopiableAddressWrap';
1214import Highlighted from 'components/Highlighted';
1315
1416import * 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−}
2517
2618export default function Xpub() {
2719 const { pop } = useLocalRouter();
2820 const { urbitWallet } = useWallet();
2921
30− const { public: pubKey, chain } = need.urbitWallet(urbitWallet).bitcoin.keys;
22+ const { xpub: zpub } = need.urbitWallet(urbitWallet).bitcoinMainnet.keys;
23+ const { xpub: vpub } = need.urbitWallet(urbitWallet).bitcoinTestnet.keys;
3124
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− );
25+ const [xpub, setXpub] = useState(zpub);
26+ const [btcMainnet, setBtcMainnet] = useState(true);
4127
4228 return (
4329 <View pop={pop} inset>
@@ -55,9 +41,25 @@ export default function Xpub() {
5541 <Grid.Item full className="mt4" as={Text}>
5642 Paste it into landscape while setting up your wallet.
5743 </Grid.Item>
44+ <Grid.Item
45+ full
46+ className="mt4"
47+ as={Text}
48+ style={{ fontSize: '14px', color: 'gray' }}>
49+ {btcMainnet ? 'BTC Mainnet' : 'BTC Testnet'}
50+ </Grid.Item>
51+ <Grid.Item full className="mt1">
52+ <ToggleSwitch
53+ selected={btcMainnet}
54+ onClick={() => {
55+ btcMainnet ? setXpub(vpub) : setXpub(zpub);
56+ setBtcMainnet(!btcMainnet);
57+ }}
58+ />
59+ </Grid.Item>
5860 <Grid.Item full className="mt4" as={Text}>
5961 <Highlighted>
60− <CopiableAddressWrap>{zPub}</CopiableAddressWrap>
62+ <CopiableAddressWrap>{xpub}</CopiableAddressWrap>
6163 </Highlighted>
6264 </Grid.Item>
6365 </Grid>