@@ -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 = {};