@@ -6,12 +6,12 @@ import { useWallet } from 'store/wallet';
66
77import * as bitcoin from 'bitcoinjs-lib';
88import { useLocalRouter } from 'lib/LocalRouter';
9−import { COMMANDS, useFlowCommand } from 'lib/flowCommand';
9+import { COMMANDS, FlowType, useFlowCommand } from 'lib/flowCommand';
1010import * as need from 'lib/need';
1111import useCopiable from 'lib/useCopiable';
1212
1313import View from 'components/View';
14−import L2BackHeader from 'components/L2/Headers/L2BackHeader';
14+import L2BackHeader from 'components/L2/Headers/L2BackHeader';
1515import Window from 'components/L2/Window/Window';
1616import HeaderPane from 'components/L2/Window/HeaderPane';
1717import BodyPane from 'components/L2/Window/BodyPane';
@@ -52,10 +52,12 @@ const BITCOIN_TESTNET_INFO = {
5252export default function Bitcoin() {
5353 const { pop }: any = useLocalRouter();
5454 const { urbitWallet }: any = useWallet();
55− const flow = useFlowCommand();
55+ const flow: FlowType = useFlowCommand();
5656
5757 const [signedTx, setSignedTx] = useState('');
58− const [psbt, setPsbt] = useState({});
58+ const [psbt, setPsbt] = useState<ReturnType<
59+ typeof bitcoin.Psbt.fromBase64
60+ > | null>(null);
5961
6062 // set to false for BTC testnet
6163 const isMainnet = true;
@@ -77,13 +79,14 @@ export default function Bitcoin() {
7779
7880 const [doCopy, didCopy] = useCopiable(xpub);
7981
80− const onSubmit = (values: any) => {
82+ const onSubmit = (values: { unsignedTransaction: string }) => {
8183 const newPsbt = bitcoin.Psbt.fromBase64(values.unsignedTransaction);
82−
83− const isTestnet = newPsbt.data.inputs[0].bip32Derivation[0].path.startsWith(
84− "m/84'/1'/0'/"
85− );
86−
84+ const derivations = newPsbt.data.inputs[0]?.bip32Derivation;
85+ const firstDerivation =
86+ derivations && derivations.length > 0 ? derivations[0] : null;
87+ const firstDerivationPath = firstDerivation ? firstDerivation.path : null;
88+ const isTestnet =
89+ firstDerivationPath && firstDerivationPath.startsWith("m/84'/1'/0'/");
8790 const derivationPrefix = isTestnet ? "m/84'/1'/0'/" : "m/84'/0'/0'/";
8891
8992 const btcWallet = isTestnet
@@ -91,13 +94,17 @@ export default function Bitcoin() {
9194 : bitcoin.bip32.fromBase58(zprv, BITCOIN_MAINNET_INFO);
9295
9396 try {
97+ if (!firstDerivationPath) {
98+ throw new Error('No derivation path available');
99+ }
94100 const hex = newPsbt.data.inputs
95101 .reduce((psbt, input, idx) => {
96102 // removing already derived part, eg m/84'/0'/0'/0/0 becomes 0/0
97− const path = input.bip32Derivation[0].path
98− .split(derivationPrefix)
99− .join('');
103+ const path = firstDerivationPath.split(derivationPrefix).join('');
100104 const prv = btcWallet.derivePath(path).privateKey;
105+ if (!prv) {
106+ throw new Error('Private key unavailable');
107+ }
101108 return psbt.signInput(idx, bitcoin.ECPair.fromPrivateKey(prv));
102109 }, newPsbt)
103110 .finalizeAllInputs()
@@ -112,9 +119,11 @@ export default function Bitcoin() {
112119 }
113120 };
114121
115− const initialValues = {};
122+ let initialValues = {
123+ unsignedTransaction: '',
124+ };
116125 if (flow && COMMANDS.BITCOIN === flow.kind) {
117− initialValues.unsignedTransaction = flow.utx;
126+ initialValues.unsignedTransaction = flow.utx as string;
118127 }
119128
120129 return (
@@ -177,6 +186,7 @@ export default function Bitcoin() {
177186 </Grid.Item>
178187 )}
179188 {signedTx &&
189+ psbt &&
180190 psbt.txOutputs.map(e => (
181191 <Grid.Item full className="mt4" key={e.address}>
182192 <span>{e.address}</span>