Skip to content

Commit 2daaf84

Browse files
committed
backend: add verify address to keystore simulator tests
1 parent ad422d6 commit 2daaf84

File tree

1 file changed

+129
-3
lines changed

1 file changed

+129
-3
lines changed

backend/devices/bitbox02/keystore_simulator_test.go

Lines changed: 129 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"path/filepath"
3535
"regexp"
3636
"runtime"
37+
"strings"
3738
"sync"
3839
"testing"
3940
"time"
@@ -44,6 +45,8 @@ import (
4445
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/maketx"
4546
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/types"
4647
coinpkg "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
48+
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth"
49+
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/ltc"
4750
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/config"
4851
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/signing"
4952
"github.com/BitBoxSwiss/bitbox-wallet-app/util/errp"
@@ -61,13 +64,16 @@ import (
6164
"github.com/btcsuite/btcd/chaincfg"
6265
"github.com/btcsuite/btcd/chaincfg/chainhash"
6366
"github.com/btcsuite/btcd/wire"
67+
"github.com/ethereum/go-ethereum/params"
6468
"github.com/stretchr/testify/require"
6569
)
6670

6771
var (
6872
log = logging.Get().WithGroup("simulator tx signing test")
6973
network = &chaincfg.MainNetParams
70-
coin = btc.NewCoin(coinpkg.CodeBTC, "Bitcoin", "BTC", coinpkg.BtcUnitDefault, network, ".", []*config.ServerInfo{}, "https://blockstream.info/testnet/tx/", socksproxy.NewSocksProxy(false, ""))
74+
coinBTC = btc.NewCoin(coinpkg.CodeBTC, "Bitcoin", "BTC", coinpkg.BtcUnitDefault, network, ".", []*config.ServerInfo{}, "https://blockstream.info/testnet/tx/", socksproxy.NewSocksProxy(false, ""))
75+
coinLTC = btc.NewCoin(coinpkg.CodeLTC, "Litecoin", "LTC", coinpkg.BtcUnitDefault, &ltc.MainNetParams, ".", []*config.ServerInfo{}, "", socksproxy.NewSocksProxy(false, ""))
76+
coinETH = eth.NewCoin(nil, "Etheruem", "ETH", "ETH", "ETH", params.MainnetChainConfig, "", nil, nil)
7177
)
7278

7379
func mustKeypath(keypath string) signing.AbsoluteKeypath {
@@ -309,14 +315,15 @@ func TestSimulatorExtendedPublicKeyBTC(t *testing.T) {
309315
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
310316
t.Helper()
311317
keypath := mustKeypath("m/84'/1'/0'")
312-
xpub, err := device.Keystore().ExtendedPublicKey(coin, keypath)
318+
xpub, err := device.Keystore().ExtendedPublicKey(coinBTC, keypath)
313319
require.NoError(t, err)
314320
require.Equal(t,
315321
"xpub6CAkM5q77qFTdrsoqguwTxAnnPVRd4hyHntZaYr9FTcefWi3AaTevG1YTvWzkNuqtshjQnJxpw1YjKLtuQvfvDiDiLVx2XYKZW5baGsRUuC",
316322
xpub.String(),
317323
)
318324
})
319325
}
326+
320327
func makeConfig(t *testing.T, device *Device, scriptType signing.ScriptType, keypath signing.AbsoluteKeypath) *signing.Configuration {
321328
t.Helper()
322329
xpubStr, err := device.BTCXPub(messages.BTCCoin_BTC, keypath.ToUInt32(), messages.BTCPubRequest_XPUB, false)
@@ -395,7 +402,7 @@ func makeTx(t *testing.T, device *Device, recipient *maketx.OutputInfo) *btc.Pro
395402
outputAmount := int64(250_000_000)
396403
feePerKb := btcutil.Amount(1000)
397404
txProposal, err := maketx.NewTx(
398-
coin,
405+
coinBTC,
399406
spendableOutputs,
400407
recipient,
401408
outputAmount,
@@ -550,3 +557,122 @@ func TestSimulatorSignBTCTransactionSilentPayment(t *testing.T) {
550557
require.True(t, found)
551558
})
552559
}
560+
561+
func TestSimulatorVerifyAddressBTC(t *testing.T) {
562+
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
563+
t.Helper()
564+
565+
type test struct {
566+
coin coinpkg.Coin
567+
accountConfiguration *signing.Configuration
568+
derivation types.Derivation
569+
expectedAddress string
570+
}
571+
tests := []test{
572+
{
573+
coin: coinBTC,
574+
accountConfiguration: makeConfig(t, device, signing.ScriptTypeP2TR, mustKeypath("m/86'/0'/0'")),
575+
derivation: types.Derivation{Change: false, AddressIndex: 0},
576+
expectedAddress: "bc1pg848p0rvmj0r3j064prlpw0gecyzkwlpt7ndzdlmh2mvkyu299psetgxhf",
577+
},
578+
{
579+
coin: coinBTC,
580+
accountConfiguration: makeConfig(t, device, signing.ScriptTypeP2TR, mustKeypath("m/86'/0'/0'")),
581+
derivation: types.Derivation{Change: true, AddressIndex: 10},
582+
expectedAddress: "bc1pru6697hz78fxc7dvz36qvgkne59kht86eyk7cefc6hpvgud4z76qxjc8we",
583+
},
584+
{
585+
coin: coinBTC,
586+
accountConfiguration: makeConfig(t, device, signing.ScriptTypeP2WPKH, mustKeypath("m/84'/0'/0'")),
587+
derivation: types.Derivation{Change: false, AddressIndex: 0},
588+
expectedAddress: "bc1qljxfdh29amtq6u2xgltc0e6d9vemtt5m6mc5nd",
589+
},
590+
{
591+
coin: coinBTC,
592+
accountConfiguration: makeConfig(t, device, signing.ScriptTypeP2WPKHP2SH, mustKeypath("m/49'/0'/0'")),
593+
derivation: types.Derivation{Change: false, AddressIndex: 0},
594+
expectedAddress: "3PSdXEXzzmqVXSV6DECyaaaaDYVyaHQ8AD",
595+
},
596+
}
597+
if device.Version().AtLeast(semver.NewSemVer(9, 23, 0)) {
598+
// Litecoin was not enabled in the simulator before v9.23.0.
599+
tests = append(tests,
600+
test{
601+
coin: coinLTC,
602+
accountConfiguration: makeConfig(t, device, signing.ScriptTypeP2WPKH, mustKeypath("m/84'/2'/0'")),
603+
derivation: types.Derivation{Change: false, AddressIndex: 0},
604+
expectedAddress: "ltc1qmgh3jwrt636mya08dytcsm7gxsxva8ckcu93jm",
605+
},
606+
test{
607+
coin: coinLTC,
608+
accountConfiguration: makeConfig(t, device, signing.ScriptTypeP2WPKHP2SH, mustKeypath("m/49'/2'/0'")),
609+
derivation: types.Derivation{Change: false, AddressIndex: 0},
610+
expectedAddress: "MFbeAVCwWdVvi5SeZh4zTpyz2gfPFdhs9c",
611+
},
612+
)
613+
}
614+
615+
for _, test := range tests {
616+
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().VerifyAddress(addressConfiguration, test.coin))
625+
require.Eventually(t,
626+
func() bool {
627+
return strings.Contains(
628+
stdOut.String(),
629+
fmt.Sprintf(
630+
"CONFIRM SCREEN START\nTITLE: %s\nBODY: %s\nCONFIRM SCREEN END\n",
631+
test.coin.Name(), test.expectedAddress))
632+
},
633+
time.Second, 10*time.Millisecond)
634+
}
635+
})
636+
}
637+
638+
func TestSimulatorVerifyAddressETH(t *testing.T) {
639+
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
640+
t.Helper()
641+
642+
type test struct {
643+
keypath signing.AbsoluteKeypath
644+
expectedAddress string
645+
}
646+
647+
tests := []test{
648+
{
649+
keypath: mustKeypath("m/44'/60'/0'/0/0"),
650+
expectedAddress: "0x416E88840Eb6353E49252Da2a2c140eA1f969D1a",
651+
},
652+
{
653+
keypath: mustKeypath("m/44'/60'/0'/0/1"),
654+
expectedAddress: "0x6A2A567cB891DeF8eA8C215C85f93d2f0F844ceB",
655+
},
656+
}
657+
658+
for _, test := range tests {
659+
xpub, err := device.Keystore().ExtendedPublicKey(coinETH, test.keypath)
660+
require.NoError(t, err)
661+
stdOut.Truncate(0)
662+
663+
addressConfiguration := signing.NewEthereumConfiguration(
664+
[]byte{1, 2, 3, 4}, test.keypath, xpub)
665+
666+
require.NoError(t, device.Keystore().VerifyAddress(addressConfiguration, coinETH))
667+
require.Eventually(t,
668+
func() bool {
669+
return strings.Contains(
670+
stdOut.String(),
671+
fmt.Sprintf(
672+
"CONFIRM SCREEN START\nTITLE: Ethereum\nBODY: %s\nCONFIRM SCREEN END\n",
673+
test.expectedAddress))
674+
},
675+
time.Second, 10*time.Millisecond)
676+
}
677+
})
678+
}

0 commit comments

Comments
 (0)