1import React, { useState } from 'react';
2import { Grid, Text } from 'indigo-react';
3import { useWallet } from 'store/wallet';
5import { useLocalRouter } from 'lib/LocalRouter';
7import ViewHeader from 'components/ViewHeader';
8import View from 'components/View';
9import WarningBox from 'components/WarningBox';
11import CopiableAddressWrap from 'components/CopiableAddressWrap';
12import Highlighted from 'components/Highlighted';
14import { PsbtInput } from 'form/Inputs';
15import BridgeForm from 'form/BridgeForm';
17import * as need from 'lib/need';
18import * as bitcoin from 'bitcoinjs-lib';
19import * as bs58check from 'bs58check';
20import { Buffer } from 'buffer';
21import { composeValidator, buildPsbtValidator } from 'form/validators';
23function xpubToZpub(xpub) {
24 var data = bs58check.decode(xpub);
26 data = Buffer.concat([Buffer.from('04b24746', 'hex'), data]);
27 return bs58check.encode(data);
30export default function Bitcoin() {
31 const { pop } = useLocalRouter();
32 const { urbitWallet } = useWallet();
35 const empty = new bitcoin.Psbt();
37 const [signedTx, setSignedTx] = useState('');
38 const [psbt, setPsbt] = useState(empty);
40 const privKey = need.urbitWallet(urbitWallet).bitcoin.keys.private;
41 const pubKey = need.urbitWallet(urbitWallet).bitcoin.keys.public;
42 const chain = need.urbitWallet(urbitWallet).bitcoin.keys.chain;
44 const btcWallet = bitcoin.bip32.fromPrivateKey(
45 Buffer.from(privKey, 'hex'),
46 Buffer.from(chain, 'hex'),
47 bitcoin.networks.bitcoin
50 const zPub = xpubToZpub(
53 Buffer.from(pubKey, 'hex'),
54 Buffer.from(chain, 'hex'),
55 bitcoin.networks.bitcoin
60 const validate = composeValidator({
61 unsignedTransaction: buildPsbtValidator(btcWallet),
64 const onValues = ({ valid, values, form }) => {
66 const newPsbt = bitcoin.Psbt.fromBase64(values.unsignedTransaction);
67 const hex = newPsbt.data.inputs
68 .reduce((psbt, input, idx) => {
69 // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
70 const path = input.bip32Derivation[0].path
71 .split("m/84'/0'/0'/")
73 const prv = btcWallet.derivePath(path).privateKey;
75 return psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
84 if (psbt.toHex() !== newPsbt.toHex()) {
94 <View pop={pop} inset>
95 <Grid className="mt4">
96 <Grid.Item full as={ViewHeader}>
99 <Grid.Item full as={WarningBox}>
100 We do not recommend using this feature for significant amounts of
101 money, since your private keys are exposed to the browser.
103 <Grid.Item full className="mt4" as={Text}>
104 Below is the bitcoin extended public key derived from your master
105 ticket. You can paste it to Landscape to generate an unsigned
108 <Grid.Item full className="mt4" as={Text}>
110 <CopiableAddressWrap>{zPub}</CopiableAddressWrap>
113 <Grid.Item full className="mt4" as={Text}>
114 Next, paste the unsigned transaction below to sign it with your
117 <BridgeForm validate={validate} onValues={onValues}>
122 name="unsignedTransaction"
123 label="Unsigned transaction"
129 <Grid.Item full className="mt4" as={Text}>
134 psbt.txOutputs.map(e => (
135 <Grid.Item full className="mt4" key={e.address}>
136 <span>{e.address}</span>
137 <div className="mt1 mono black f6">{e.value} Satoshis</div>
142 <Grid.Item full className="mt4" as={ViewHeader}>
145 <Grid.Item full as={Text}>
147 <CopiableAddressWrap>{signedTx}</CopiableAddressWrap>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list