Skip to content

Commit 8625187

Browse files
committed
backend/keystore: use accountConfig&derivation in VerifyAddressBTC
A BTC address is derived from an account using change/addressIndex. There is no "address-level" signing configuration. `signingConfig.Derive()` assumes there is one base level keypath and xpub, which is not true for a descriptor in general. For example, a multisig descriptor has multiple xpubs, each with their own keypath.
1 parent 6dd183c commit 8625187

File tree

7 files changed

+44
-28
lines changed

7 files changed

+44
-28
lines changed

backend/coins/btc/account.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,10 @@ func (account *Account) VerifyAddress(addressID string) (bool, error) {
781781
return false, err
782782
}
783783
if canVerifyAddress {
784-
return true, keystore.VerifyAddressBTC(address.Configuration, account.Coin())
784+
return true, keystore.VerifyAddressBTC(
785+
address.AccountConfiguration,
786+
address.Derivation,
787+
account.Coin())
785788
}
786789
return false, nil
787790
}

backend/coins/btc/addresses/address.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type AccountAddress struct {
3939
AccountConfiguration *signing.Configuration
4040
// Configuration contains the absolute keypath and the extended public keys of the address.
4141
Configuration *signing.Configuration
42+
Derivation types.Derivation
4243

4344
// redeemScript stores the redeem script of a BIP16 P2SH output or nil if address type is P2PKH.
4445
redeemScript []byte
@@ -111,6 +112,7 @@ func NewAccountAddress(
111112
Address: address,
112113
AccountConfiguration: accountConfiguration,
113114
Configuration: configuration,
115+
Derivation: derivation,
114116
redeemScript: redeemScript,
115117
log: log,
116118
}

backend/devices/bitbox02/keystore.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,25 @@ func (keystore *keystore) CanVerifyAddress(coin coinpkg.Coin) (bool, bool, error
145145

146146
// VerifyAddressBTC implements keystore.Keystore.
147147
func (keystore *keystore) VerifyAddressBTC(
148-
configuration *signing.Configuration, coin coinpkg.Coin) error {
148+
accountConfiguration *signing.Configuration,
149+
derivation types.Derivation, coin coinpkg.Coin) error {
149150
canVerifyAddress, _, err := keystore.CanVerifyAddress(coin)
150151
if err != nil {
151152
return err
152153
}
153154
if !canVerifyAddress {
154155
panic("CanVerifyAddress must be true")
155156
}
156-
msgScriptType, ok := btcMsgScriptTypeMap[configuration.ScriptType()]
157+
msgScriptType, ok := btcMsgScriptTypeMap[accountConfiguration.ScriptType()]
157158
if !ok {
158159
panic("unsupported scripttype")
159160
}
161+
keypath := accountConfiguration.AbsoluteKeypath().
162+
Child(derivation.SimpleChainIndex(), false).
163+
Child(derivation.AddressIndex, false)
160164
_, err = keystore.device.BTCAddress(
161165
btcMsgCoinMap[coin.Code()],
162-
configuration.AbsoluteKeypath().ToUInt32(),
166+
keypath.ToUInt32(),
163167
firmware.NewBTCScriptConfigSimple(msgScriptType),
164168
true,
165169
)

backend/devices/bitbox02/keystore_simulator_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,10 @@ func TestSimulatorVerifyAddressBTC(t *testing.T) {
614614

615615
for _, test := range tests {
616616
stdOut.Truncate(0)
617-
618-
addressConfiguration, err := test.accountConfiguration.Derive(
619-
signing.NewEmptyRelativeKeypath().
620-
Child(test.derivation.SimpleChainIndex(), signing.NonHardened).
621-
Child(test.derivation.AddressIndex, signing.NonHardened),
622-
)
623-
require.NoError(t, err)
624-
require.NoError(t, device.Keystore().VerifyAddressBTC(addressConfiguration, test.coin))
617+
require.NoError(t, device.Keystore().VerifyAddressBTC(
618+
test.accountConfiguration,
619+
test.derivation,
620+
test.coin))
625621
require.Eventually(t,
626622
func() bool {
627623
return strings.Contains(

backend/keystore/keystore.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package keystore
1818
import (
1919
"errors"
2020

21+
btctypes "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/types"
2122
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
2223
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/signing"
2324
"github.com/btcsuite/btcd/btcutil/hdkeychain"
@@ -94,7 +95,10 @@ type Keystore interface {
9495

9596
// VerifyAddressBTC displays a Bitcoin address for verification.
9697
// Please note that this is only supported if the keystore has a secure output channel.
97-
VerifyAddressBTC(*signing.Configuration, coin.Coin) error
98+
VerifyAddressBTC(
99+
accountConfiguration *signing.Configuration,
100+
derivation btctypes.Derivation,
101+
coin coin.Coin) error
98102

99103
// VerifyAddressBTC displays an Ethereum address for verification.
100104
// Please note that this is only supported if the keystore has a secure output channel.

backend/keystore/mocks/keystore.go

Lines changed: 21 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/keystore/software/software.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (keystore *Keystore) CanVerifyAddress(coin.Coin) (bool, bool, error) {
157157
}
158158

159159
// VerifyAddressBTC implements keystore.Keystore.
160-
func (keystore *Keystore) VerifyAddressBTC(*signing.Configuration, coin.Coin) error {
160+
func (keystore *Keystore) VerifyAddressBTC(*signing.Configuration, types.Derivation, coin.Coin) error {
161161
return errp.New("The software-based keystore has no secure output to display the address.")
162162
}
163163

0 commit comments

Comments
 (0)