Skip to content

Commit

Permalink
backend/btc: refactor pkScript encoding/decoding
Browse files Browse the repository at this point in the history
We use functions from the btcd library to convert addresses to
pubKeyScripts and vice versa.

While the address type has support for Taproot addresses, the pubkey
script functions in btcd don't handle them yet. See also:
btcsuite/btcd#1768

To speed up send-to-taproot support, refactor the pubkey script
encoding/decoding into separate unit-tested functions, to which we can
easily add taproot support in the next commit.
  • Loading branch information
benma committed Nov 22, 2021
1 parent 785ab52 commit d742e19
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 13 deletions.
6 changes: 3 additions & 3 deletions backend/coins/btc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strconv"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/digitalbitbox/bitbox-wallet-app/backend/accounts"
Expand All @@ -29,6 +28,7 @@ import (
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/blockchain"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/maketx"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/transactions"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/util"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
)
Expand Down Expand Up @@ -80,9 +80,9 @@ func (account *Account) newTx(args *accounts.TxProposalArgs) (
if err != nil {
return nil, nil, err
}
pkScript, err := txscript.PayToAddrScript(address)
pkScript, err := util.PkScriptFromAddress(address)
if err != nil {
return nil, nil, errp.WithStack(err)
return nil, nil, err
}
utxo := account.transactions.SpendableOutputs()
wireUTXO := make(map[wire.OutPoint]maketx.UTXO, len(utxo))
Expand Down
12 changes: 12 additions & 0 deletions backend/coins/btc/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"strings"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
)

Expand All @@ -39,3 +41,13 @@ func ParseOutPoint(outPointBytes []byte) (*wire.OutPoint, error) {
}
return wire.NewOutPoint(txHash, uint32(index)), nil
}

// PkScriptFromAddress decodes an address into the pubKeyScript that can be used in a transaction
// output.
func PkScriptFromAddress(address btcutil.Address) ([]byte, error) {
pkScript, err := txscript.PayToAddrScript(address)
if err != nil {
return nil, errp.WithStack(err)
}
return pkScript, nil
}
63 changes: 63 additions & 0 deletions backend/coins/btc/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2021 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"testing"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/stretchr/testify/require"
)

func TestPkScriptFromAddress(t *testing.T) {
hash := []byte("\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab")
net := &chaincfg.MainNetParams

var address btcutil.Address

address, err := btcutil.NewAddressPubKeyHash(hash, net)
require.NoError(t, err)
pkScript, err := PkScriptFromAddress(address)
require.NoError(t, err)
require.Equal(t,
[]byte("\x76\xa9\x14\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab\x88\xac"),
pkScript)

address, err = btcutil.NewAddressWitnessPubKeyHash(hash, net)
require.NoError(t, err)
pkScript, err = PkScriptFromAddress(address)
require.NoError(t, err)
require.Equal(t,
[]byte("\x00\x14\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab"),
pkScript)

address, err = btcutil.NewAddressScriptHashFromHash(hash, net)
require.NoError(t, err)
pkScript, err = PkScriptFromAddress(address)
require.NoError(t, err)
require.Equal(t,
[]byte("\xa9\x14\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab\x87"),
pkScript)

scriptHash := []byte("\x4a\xf2\xe4\x54\x9a\x5c\xbb\x73\x6e\x77\xce\xf5\x2f\xe3\x0b\x9d\xf8\x12\x1d\x73\x56\xab\x20\x05\x46\x3e\xcb\x08\x97\x23\x45\x8d")
address, err = btcutil.NewAddressWitnessScriptHash(scriptHash, net)
require.NoError(t, err)
pkScript, err = PkScriptFromAddress(address)
require.NoError(t, err)
require.Equal(t,
[]byte("\x00\x20\x4a\xf2\xe4\x54\x9a\x5c\xbb\x73\x6e\x77\xce\xf5\x2f\xe3\x0b\x9d\xf8\x12\x1d\x73\x56\xab\x20\x05\x46\x3e\xcb\x08\x97\x23\x45\x8d"),
pkScript)
}
28 changes: 18 additions & 10 deletions backend/devices/bitbox02/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,21 @@ func (keystore *keystore) ExtendedPublicKey(
}
}

func btcDecodePkScript(pkScript []byte, net *chaincfg.Params) (messages.BTCOutputType, []byte, error) {
scriptClass, addresses, _, err := txscript.ExtractPkScriptAddrs(pkScript, net)
if err != nil {
return 0, nil, errp.WithStack(err)
}
if len(addresses) != 1 {
return 0, nil, errp.New("couldn't parse pkScript")
}
msgOutputType, ok := btcMsgOutputTypeMap[scriptClass]
if !ok {
return 0, nil, errp.Newf("unsupported output type: %d", scriptClass)
}
return msgOutputType, addresses[0].ScriptAddress(), nil
}

func (keystore *keystore) signBTCTransaction(btcProposedTx *btc.ProposedTransaction) error {
tx := btcProposedTx.TXProposal.Transaction

Expand Down Expand Up @@ -351,16 +366,9 @@ func (keystore *keystore) signBTCTransaction(btcProposedTx *btc.ProposedTransact
}
outputs := make([]*messages.BTCSignOutputRequest, len(tx.TxOut))
for index, txOut := range tx.TxOut {
scriptClass, addresses, _, err := txscript.ExtractPkScriptAddrs(txOut.PkScript, coin.Net())
msgOutputType, payload, err := btcDecodePkScript(txOut.PkScript, coin.Net())
if err != nil {
return errp.WithStack(err)
}
if len(addresses) != 1 {
return errp.New("couldn't parse pkScript")
}
msgOutputType, ok := btcMsgOutputTypeMap[scriptClass]
if !ok {
return errp.Newf("unsupported output type: %d", scriptClass)
return err
}
changeAddress := btcProposedTx.TXProposal.ChangeAddress
isChange := changeAddress != nil && bytes.Equal(
Expand All @@ -375,7 +383,7 @@ func (keystore *keystore) signBTCTransaction(btcProposedTx *btc.ProposedTransact
Ours: isChange,
Type: msgOutputType,
Value: uint64(txOut.Value),
Hash: addresses[0].ScriptAddress(),
Payload: payload,
Keypath: keypath,
}
}
Expand Down
69 changes: 69 additions & 0 deletions backend/devices/bitbox02/keystore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2021 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bitbox02

import (
"testing"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/util"
"github.com/digitalbitbox/bitbox02-api-go/api/firmware/messages"
"github.com/stretchr/testify/require"
)

func TesBtcDecodePkScript(t *testing.T) {
hash := []byte("\x92\x95\x3b\x69\x91\x29\x70\x02\xfa\xa6\x2a\x1d\xd2\x43\x13\xff\x62\x1e\x10\xab")
net := &chaincfg.MainNetParams

var address btcutil.Address

address, err := btcutil.NewAddressPubKeyHash(hash, net)
require.NoError(t, err)
pkScript, err := util.PkScriptFromAddress(address)
require.NoError(t, err)
outputType, payload, err := btcDecodePkScript(pkScript, &chaincfg.MainNetParams)
require.NoError(t, err)
require.Equal(t, messages.BTCOutputType_P2PKH, outputType)
require.Equal(t, address.ScriptAddress(), payload)

address, err = btcutil.NewAddressWitnessPubKeyHash(hash, net)
require.NoError(t, err)
pkScript, err = util.PkScriptFromAddress(address)
require.NoError(t, err)
outputType, payload, err = btcDecodePkScript(pkScript, &chaincfg.MainNetParams)
require.NoError(t, err)
require.Equal(t, messages.BTCOutputType_P2WPKH, outputType)
require.Equal(t, address.ScriptAddress(), payload)

address, err = btcutil.NewAddressScriptHashFromHash(hash, net)
require.NoError(t, err)
pkScript, err = util.PkScriptFromAddress(address)
require.NoError(t, err)
outputType, payload, err = btcDecodePkScript(pkScript, &chaincfg.MainNetParams)
require.NoError(t, err)
require.Equal(t, messages.BTCOutputType_P2SH, outputType)
require.Equal(t, address.ScriptAddress(), payload)

scriptHash := []byte("\x4a\xf2\xe4\x54\x9a\x5c\xbb\x73\x6e\x77\xce\xf5\x2f\xe3\x0b\x9d\xf8\x12\x1d\x73\x56\xab\x20\x05\x46\x3e\xcb\x08\x97\x23\x45\x8d")
address, err = btcutil.NewAddressWitnessScriptHash(scriptHash, net)
require.NoError(t, err)
pkScript, err = util.PkScriptFromAddress(address)
require.NoError(t, err)
outputType, payload, err = btcDecodePkScript(pkScript, &chaincfg.MainNetParams)
require.NoError(t, err)
require.Equal(t, messages.BTCOutputType_P2WSH, outputType)
require.Equal(t, address.ScriptAddress(), payload)
}

0 comments on commit d742e19

Please sign in to comment.