1import React, { useState } from 'react';
2import { Grid, Text } from 'indigo-react';
4import { useLocalRouter } from 'lib/LocalRouter';
5import { COMMANDS, useFlowCommand } from 'lib/flowCommand';
7import View from 'components/View';
8import Highlighted from 'components/Highlighted';
9import CopiableAddressWrap from 'components/copiable/CopiableAddressWrap';
10import ViewHeader from 'components/ViewHeader';
11import SubmitButton from 'form/SubmitButton';
13import { useWallet } from 'store/wallet';
15import * as need from 'lib/need';
16import * as bitcoin from 'bitcoinjs-lib';
18import BridgeForm from 'form/BridgeForm';
19import { PsbtInput } from 'form/Inputs';
21import { composeValidator, buildPsbtValidator } from 'form/validators';
23const BITCOIN_MAINNET_INFO = {
24 messagePrefix: '\x18Bitcoin Signed Message:\n',
35const BITCOIN_TESTNET_INFO = {
36 messagePrefix: '\x18Bitcoin Signed Message:\n',
47export default function SignTransaction() {
48 const { pop } = useLocalRouter();
49 const { urbitWallet } = useWallet();
50 const flow = useFlowCommand();
52 const [signedTx, setSignedTx] = useState('');
53 const [psbt, setPsbt] = useState({});
55 const { xprv: zprv } = need.urbitWallet(urbitWallet).bitcoinMainnet.keys;
56 const { xprv: vprv } = need.urbitWallet(urbitWallet).bitcoinTestnet.keys;
58 const validate = composeValidator({
59 unsignedTransaction: buildPsbtValidator(),
62 const onSubmit = values => {
63 const newPsbt = bitcoin.Psbt.fromBase64(values.unsignedTransaction);
65 const isTestnet = newPsbt.data.inputs[0].bip32Derivation[0].path.startsWith(
69 const derivationPrefix = isTestnet ? "m/84'/1'/0'/" : "m/84'/0'/0'/";
71 const btcWallet = isTestnet
72 ? bitcoin.bip32.fromBase58(vprv, BITCOIN_TESTNET_INFO)
73 : bitcoin.bip32.fromBase58(zprv, BITCOIN_MAINNET_INFO);
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)
82 const prv = btcWallet.derivePath(path).privateKey;
83 return psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
92 unsignedTransaction: 'Cannot sign transaction, mismatching keys.',
97 const initialValues = {};
98 if (flow && COMMANDS.BITCOIN === flow.kind) {
99 initialValues.unsignedTransaction = flow.utx;
103 <View pop={pop} inset>
104 <Grid className="mt4">
105 <Grid.Item full className="mt4" as={Text}>
106 Paste unsigned transactions from Landscape here
110 initialValues={initialValues}
112 {({ handleSubmit }) => (
117 name="unsignedTransaction"
118 label="Unsigned transaction"
126 handleSubmit={handleSubmit}
134 <Grid.Item full className="mt4" as={Text}>
139 psbt.txOutputs.map(e => (
140 <Grid.Item full className="mt4" key={e.address}>
141 <span>{e.address}</span>
142 <div className="mt1 mono black f6">{e.value} sats</div>
147 <Grid.Item full className="mt4" as={ViewHeader}>
150 <Grid.Item full as={Text}>
152 <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