Skip to content

Commit 5f29479

Browse files
committed
backend/btc: remove SendingAccount
No need anymore that we don't need to track if an address belongs to the same account or not.
1 parent d94fcde commit 5f29479

File tree

9 files changed

+20
-17
lines changed

9 files changed

+20
-17
lines changed

backend/accounts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ func (backend *Backend) createAndAddAccount(coin coinpkg.Coin, persistedConfig *
10631063

10641064
// This function is passed as a callback to the BTC account constructor. It is called when the
10651065
// keystore needs to determine whether an address belongs to an account on its same keystore.
1066-
getAddressCallback := func(askingAccount *btc.Account, scriptHashHex blockchain.ScriptHashHex) (*addresses.AccountAddress, error) {
1066+
getAddressCallback := func(coinCode coinpkg.Code, scriptHashHex blockchain.ScriptHashHex) (*addresses.AccountAddress, error) {
10671067
accountsByKeystore, err := backend.AccountsByKeystore()
10681068
if err != nil {
10691069
return nil, err
@@ -1079,7 +1079,7 @@ func (backend *Backend) createAndAddAccount(coin coinpkg.Coin, persistedConfig *
10791079
continue
10801080
}
10811081
// Only return an address if the coin codes match.
1082-
if btcAccount.Coin().Code() != askingAccount.Coin().Code() {
1082+
if btcAccount.Coin().Code() != coinCode {
10831083
continue
10841084
}
10851085
if address := btcAccount.GetAddress(scriptHashHex); address != nil {

backend/accounts_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ func TestKeystoresBalance(t *testing.T) {
14911491
b := newBackend(t, testnetDisabled, regtestDisabled)
14921492
defer b.Close()
14931493

1494-
b.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(*btc.Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
1494+
b.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(coinpkg.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
14951495
accountMock := MockBtcAccount(t, config, coin, gapLimits, log)
14961496
accountMock.BalanceFunc = func() (*accounts.Balance, error) {
14971497
return accounts.NewBalance(coinpkg.NewAmountFromInt64(1e8), coinpkg.NewAmountFromInt64(0)), nil
@@ -1561,7 +1561,7 @@ func TestCoinsTotalBalance(t *testing.T) {
15611561
b := newBackend(t, testnetDisabled, regtestDisabled)
15621562
defer b.Close()
15631563

1564-
b.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(*btc.Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
1564+
b.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(coinpkg.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
15651565
accountMock := MockBtcAccount(t, config, coin, gapLimits, log)
15661566
accountMock.BalanceFunc = func() (*accounts.Balance, error) {
15671567
return accounts.NewBalance(coinpkg.NewAmountFromInt64(1e8), coinpkg.NewAmountFromInt64(0)), nil
@@ -1622,7 +1622,7 @@ func TestAccountsFiatAndCoinBalance(t *testing.T) {
16221622
b := newBackend(t, testnetDisabled, regtestDisabled)
16231623
defer b.Close()
16241624

1625-
b.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(*btc.Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
1625+
b.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(coinpkg.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
16261626
accountMock := MockBtcAccount(t, config, coin, gapLimits, log)
16271627
accountMock.BalanceFunc = func() (*accounts.Balance, error) {
16281628
return accounts.NewBalance(coinpkg.NewAmountFromInt64(1e8), coinpkg.NewAmountFromInt64(0)), nil

backend/backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ type Backend struct {
229229

230230
// makeBtcAccount creates a BTC account. In production this is `btc.NewAccount`, but can be
231231
// overridden in unit tests for mocking.
232-
makeBtcAccount func(*accounts.AccountConfig, *btc.Coin, *types.GapLimits, func(*btc.Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), *logrus.Entry) accounts.Interface
232+
makeBtcAccount func(*accounts.AccountConfig, *btc.Coin, *types.GapLimits, func(coinpkg.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), *logrus.Entry) accounts.Interface
233233
// makeEthAccount creates an ETH account. In production this is `eth.NewAccount`, but can be
234234
// overridden in unit tests for mocking.
235235
makeEthAccount func(*accounts.AccountConfig, *eth.Coin, *http.Client, *logrus.Entry) accounts.Interface
@@ -298,7 +298,7 @@ func NewBackend(arguments *arguments.Arguments, environment Environment) (*Backe
298298
coins: map[coinpkg.Code]coinpkg.Coin{},
299299
accounts: []accounts.Interface{},
300300
aopp: AOPP{State: aoppStateInactive},
301-
makeBtcAccount: func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(*btc.Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
301+
makeBtcAccount: func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(coinpkg.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
302302
return btc.NewAccount(config, coin, gapLimits, getAddress, log, hclient)
303303
},
304304
makeEthAccount: func(config *accounts.AccountConfig, coin *eth.Coin, httpClient *http.Client, log *logrus.Entry) accounts.Interface {

backend/backend_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func newBackend(t *testing.T, testing, regtest bool) *Backend {
281281
}
282282
b.ratesUpdater.SetCoingeckoURL("unused") // avoid hitting real API
283283

284-
b.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(*btc.Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
284+
b.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(coinpkg.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
285285
return MockBtcAccount(t, config, coin, gapLimits, log)
286286
}
287287
b.makeEthAccount = func(config *accounts.AccountConfig, coin *eth.Coin, httpClient *http.Client, log *logrus.Entry) accounts.Interface {

backend/coins/btc/account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ type Account struct {
122122
httpClient *http.Client
123123

124124
// getAddressFromSameKeystore is a function that retrieves an address from any account on the same keystore as this one.
125-
getAddressFromSameKeystore func(*Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error)
125+
getAddressFromSameKeystore func(coin.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error)
126126
}
127127

128128
// NewAccount creates a new account.
@@ -133,7 +133,7 @@ func NewAccount(
133133
config *accounts.AccountConfig,
134134
coin *Coin,
135135
forceGapLimits *types.GapLimits,
136-
getAddressFromSameKeystore func(*Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error),
136+
getAddressFromSameKeystore func(coin.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error),
137137
log *logrus.Entry,
138138
httpClient *http.Client,
139139
) *Account {

backend/coins/btc/sign.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/maketx"
2323
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/types"
2424
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
25+
coinpkg "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
2526
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/signing"
2627
"github.com/BitBoxSwiss/bitbox-wallet-app/util/errp"
2728
"github.com/btcsuite/btcd/btcec/v2/schnorr"
@@ -42,8 +43,7 @@ type ProposedTransaction struct {
4243
FormatUnit coin.BtcUnit
4344
// GetKeystoreAddress returns the address from the same keystore given the script hash,
4445
// or nil if not found.
45-
GetKeystoreAddress func(*Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error)
46-
SendingAccount *Account
46+
GetKeystoreAddress func(coinpkg.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error)
4747
}
4848

4949
// Update populates the PSBT with all information we have about the inputs and outputs required for signing:
@@ -109,7 +109,7 @@ func (p *ProposedTransaction) Update() error {
109109
for index, txOut := range txProposal.Psbt.UnsignedTx.TxOut {
110110
// outputAddress is the recipient address if it belongs to the same keystore. It is nil if
111111
// the address is external.
112-
outputAddress, err := p.GetKeystoreAddress(p.SendingAccount, blockchain.NewScriptHashHex(txOut.PkScript))
112+
outputAddress, err := p.GetKeystoreAddress(p.TXProposal.Coin.Code(), blockchain.NewScriptHashHex(txOut.PkScript))
113113
if err != nil {
114114
return errp.Newf("failed to get address: %v", err)
115115
}
@@ -180,7 +180,6 @@ func (account *Account) signTransaction(
180180
TXProposal: txProposal,
181181
AccountSigningConfigurations: signingConfigs,
182182
GetKeystoreAddress: account.getAddressFromSameKeystore,
183-
SendingAccount: account,
184183
GetPrevTx: getPrevTx,
185184
Signatures: make([]*types.Signature, len(txProposal.Psbt.UnsignedTx.TxIn)),
186185
FormatUnit: account.coin.formatUnit,

backend/devices/bitbox02/keystore_simulator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func makeTx(t *testing.T, device *Device, recipient *maketx.OutputInfo) *btc.Pro
476476
GetPrevTx: func(chainhash.Hash) (*wire.MsgTx, error) {
477477
return prevTx, nil
478478
},
479-
GetKeystoreAddress: func(account *btc.Account, scriptHashHex blockchain.ScriptHashHex) (*addresses.AccountAddress, error) {
479+
GetKeystoreAddress: func(coinCode coinpkg.Code, scriptHashHex blockchain.ScriptHashHex) (*addresses.AccountAddress, error) {
480480
for _, address := range addrs {
481481
if address.PubkeyScriptHashHex() == scriptHashHex {
482482
return address, nil

backend/keystore/software/software.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ func (keystore *Keystore) signBTCTransaction(btcProposedTx *btc.ProposedTransact
212212
keystore.log.Error("There needs to be exactly one output being spent per input.")
213213
return errp.New("There needs to be exactly one output being spent per input.")
214214
}
215-
address, err := btcProposedTx.GetKeystoreAddress(btcProposedTx.SendingAccount, spentOutput.Address.PubkeyScriptHashHex())
215+
address, err := btcProposedTx.GetKeystoreAddress(
216+
btcProposedTx.TXProposal.Coin.Code(),
217+
spentOutput.Address.PubkeyScriptHashHex(),
218+
)
216219
if err != nil {
217220
return err
218221
}

backend/notes_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/addresses"
2929
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/blockchain"
3030
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/types"
31+
coinpkg "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
3132
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth"
3233
"github.com/BitBoxSwiss/bitbox-wallet-app/util/test"
3334
"github.com/sirupsen/logrus"
@@ -81,7 +82,7 @@ func (s *notesTestSuite) SetupTest() {
8182
}
8283
}
8384

84-
s.backend.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(*btc.Account, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
85+
s.backend.makeBtcAccount = func(config *accounts.AccountConfig, coin *btc.Coin, gapLimits *types.GapLimits, getAddress func(coinpkg.Code, blockchain.ScriptHashHex) (*addresses.AccountAddress, error), log *logrus.Entry) accounts.Interface {
8586
accountMock := MockBtcAccount(s.T(), config, coin, gapLimits, log)
8687
accountMock.NotesFunc = notesFunc(config.Config.Code)
8788
accountMock.TransactionsFunc = transactionsFunc(config.Config.Code)

0 commit comments

Comments
 (0)