on master help sign in
Merge pull request #570 from pkova/master
3a4849afang
package.json+1−1
@@ -55,7 +55,7 @@
55 "react-teleporter": "^1.1.0",
55 "react-teleporter": "^1.1.0",
56 "secp256k1": "^3.5.2",
56 "secp256k1": "^3.5.2",
57 "trezor-connect": "^8.1.14",
57 "trezor-connect": "^8.1.14",
58− "urbit-key-generation": "^0.18.0",
58+ "urbit-key-generation": "^0.19.0",
59 "urbit-ob": "^4.1.4",
59 "urbit-ob": "^4.1.4",
60 "urbit-paper-renderer": "^2.0.9",
60 "urbit-paper-renderer": "^2.0.9",
61 "web3": "1.0.0-beta.54",
61 "web3": "1.0.0-beta.54",
src/form/validators.js+1−2
@@ -20,7 +20,6 @@ import {
20 validateLength,
20 validateLength,
21 validateUnique,
21 validateUnique,
22 validatePsbt,
22 validatePsbt,
23− validateSignablePsbt,
24} from 'lib/validators';
23} from 'lib/validators';
25import isPromise from 'lib/isPromise';
24import isPromise from 'lib/isPromise';
26
25
@@ -125,7 +124,7 @@ export const buildEmailArrayValidator = () =>
125 buildValidator([validateUnique, buildArrayValidator(buildEmailValidator())]);
124 buildValidator([validateUnique, buildArrayValidator(buildEmailValidator())]);
126
125
127export const buildPsbtValidator = btcWallet =>
126export const buildPsbtValidator = btcWallet =>
128− buildValidator([validatePsbt, validateSignablePsbt(btcWallet)]);
127+ buildValidator([validatePsbt]);
129
128
130// the form validator is the composition of all of the field validators
129// the form validator is the composition of all of the field validators
131// plus an additional form validator function
130// plus an additional form validator function
src/lib/validators.js−19
@@ -141,22 +141,3 @@ export const validatePsbt = base64 => {
141 return 'Invalid Partially Signed Bitcoin Transaction';
141 return 'Invalid Partially Signed Bitcoin Transaction';
142 }
142 }
143};
143};
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+55−29
@@ -18,9 +18,32 @@ import * as bitcoin from 'bitcoinjs-lib';
18import BridgeForm from 'form/BridgeForm';
18import BridgeForm from 'form/BridgeForm';
19import { PsbtInput } from 'form/Inputs';
19import { PsbtInput } from 'form/Inputs';
20
20
21−import { Buffer } from 'buffer';
22import { composeValidator, buildPsbtValidator } from 'form/validators';
21import { composeValidator, buildPsbtValidator } from 'form/validators';
23
22
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+
24export default function SignTransaction() {
47export default function SignTransaction() {
25 const { pop } = useLocalRouter();
48 const { pop } = useLocalRouter();
26 const { urbitWallet } = useWallet();
49 const { urbitWallet } = useWallet();
@@ -29,41 +52,44 @@ export default function SignTransaction() {
29 const [signedTx, setSignedTx] = useState('');
52 const [signedTx, setSignedTx] = useState('');
30 const [psbt, setPsbt] = useState({});
53 const [psbt, setPsbt] = useState({});
31
54
32− const { private: privKey, chain } = need.urbitWallet(
55+ const { xprv: zprv } = need.urbitWallet(urbitWallet).bitcoinMainnet.keys;
33− urbitWallet
56+ const { xprv: vprv } = need.urbitWallet(urbitWallet).bitcoinTestnet.keys;
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− );
41
57
42 const validate = composeValidator({
58 const validate = composeValidator({
43− unsignedTransaction: buildPsbtValidator(btcWallet),
59+ unsignedTransaction: buildPsbtValidator(),
44 });
60 });
45
61
46 const onSubmit = values => {
62 const onSubmit = values => {
47 const newPsbt = bitcoin.Psbt.fromBase64(values.unsignedTransaction);
63 const newPsbt = bitcoin.Psbt.fromBase64(values.unsignedTransaction);
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 {
48− const hex = newPsbt.data.inputs
76+ const hex = newPsbt.data.inputs
49− .reduce((psbt, input, idx) => {
77+ .reduce((psbt, input, idx) => {
50− // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
78+ // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
51− const path = input.bip32Derivation[0].path
79+ const path = input.bip32Derivation[0].path
52− .split("m/84'/0'/0'/")
80+ .split(derivationPrefix)
53− .join('');
81+ .join('');
54− const prv = btcWallet.derivePath(path).privateKey;
82+ const prv = btcWallet.derivePath(path).privateKey;
55− try {
56 return psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
83 return psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
57− } catch (e) {
58− return psbt;
59− }
60− }, newPsbt)
84+ }, newPsbt)
61− .finalizeAllInputs()
85+ .finalizeAllInputs()
62− .extractTransaction()
86+ .extractTransaction()
63− .toHex();
87+ .toHex();
64−
65− setPsbt(newPsbt);
88+ setPsbt(newPsbt);
66− setSignedTx(hex);
89+ setSignedTx(hex);
90+ } catch (error) {
91+ return { unsignedTransaction: 'Cannot sign transaction, mismatching keys.' };
92+ }
67 };
93 };
68
94
69 const initialValues = {};
95 const initialValues = {};
src/views/Bitcoin/Xpub.js+5−21
@@ -12,32 +12,16 @@ import CopiableAddressWrap from 'components/CopiableAddressWrap';
12import Highlighted from 'components/Highlighted';
12import Highlighted from 'components/Highlighted';
13
13
14import * as need from 'lib/need';
14import * 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
15
26export default function Xpub() {
16export default function Xpub() {
27 const { pop } = useLocalRouter();
17 const { pop } = useLocalRouter();
28 const { urbitWallet } = useWallet();
18 const { urbitWallet } = useWallet();
29
19
20+ const { xpub: zpub } = need.urbitWallet(urbitWallet).bitcoinMainnet.keys;
30− const { public: pubKey, chain } = need.urbitWallet(urbitWallet).bitcoin.keys;
21+ const { xpub: vpub } = need.urbitWallet(urbitWallet).bitcoinTestnet.keys;
31
22
32− const zPub = xpubToZpub(
23+ // set to false for BTC testnet
33− bitcoin.bip32
24+ const isMainnet = true;
34− .fromPublicKey(
35− Buffer.from(pubKey, 'hex'),
36− Buffer.from(chain, 'hex'),
37− bitcoin.networks.bitcoin
38− )
39− .toBase58()
40− );
41
25
42 return (
26 return (
43 <View pop={pop} inset>
27 <View pop={pop} inset>
@@ -57,7 +41,7 @@ export default function Xpub() {
57 </Grid.Item>
41 </Grid.Item>
58 <Grid.Item full className="mt4" as={Text}>
42 <Grid.Item full className="mt4" as={Text}>
59 <Highlighted>
43 <Highlighted>
60− <CopiableAddressWrap>{zPub}</CopiableAddressWrap>
44+ <CopiableAddressWrap>{isMainnet ? zpub : vpub}</CopiableAddressWrap>
61 </Highlighted>
45 </Highlighted>
62 </Grid.Item>
46 </Grid.Item>
63 </Grid>
47 </Grid>