1
1
// Copyright 2018-2019 Shift Cryptosecurity AG
2
+ // Copyright 2024 Shift Crypto AG
2
3
//
3
4
// Licensed under the Apache License, Version 2.0 (the "License");
4
5
// you may not use this file except in compliance with the License.
@@ -20,12 +21,26 @@ import (
20
21
21
22
"github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/messages"
22
23
"github.com/BitBoxSwiss/bitbox02-api-go/util/semver"
24
+ "github.com/btcsuite/btcd/btcec/v2"
25
+ "github.com/btcsuite/btcd/btcec/v2/ecdsa"
26
+ "github.com/btcsuite/btcd/btcutil/hdkeychain"
27
+ "github.com/btcsuite/btcd/chaincfg/chainhash"
23
28
"github.com/stretchr/testify/require"
24
29
"google.golang.org/protobuf/proto"
25
30
)
26
31
27
32
const hardenedKeyStart = 0x80000000
28
33
34
+ func parseECDSASignature (t * testing.T , sig []byte ) * ecdsa.Signature {
35
+ t .Helper ()
36
+ require .Len (t , sig , 64 )
37
+ r := new (btcec.ModNScalar )
38
+ r .SetByteSlice (sig [:32 ])
39
+ s := new (btcec.ModNScalar )
40
+ s .SetByteSlice (sig [32 :])
41
+ return ecdsa .NewSignature (r , s )
42
+ }
43
+
29
44
func TestNewXPub (t * testing.T ) {
30
45
xpub , err := NewXPub (
31
46
"xpub6FEZ9Bv73h1vnE4TJG4QFj2RPXJhhsPbnXgFyH3ErLvpcZrDcynY65bhWga8PazWHLSLi23PoBhGcLcYW6JRiJ12zXZ9Aop4LbAqsS3gtcy" )
@@ -39,7 +54,79 @@ func TestNewXPub(t *testing.T) {
39
54
}, xpub )
40
55
}
41
56
42
- func TestBTCXPub (t * testing.T ) {
57
+ func TestBTCXpub (t * testing.T ) {
58
+ testInitializedSimulators (t , func (t * testing.T , device * Device ) {
59
+ t .Helper ()
60
+ xpub , err := device .BTCXPub (messages .BTCCoin_TBTC , []uint32 {
61
+ 49 + hardenedKeyStart ,
62
+ 1 + hardenedKeyStart ,
63
+ 0 + hardenedKeyStart ,
64
+ }, messages .BTCPubRequest_YPUB , false )
65
+ require .NoError (t , err )
66
+ require .Equal (t , "ypub6WqXiL3fbDK5QNPe3hN4uSVkEvuE8wXoNCcecgggSuKVpU3Kc4fTvhuLgUhtnbAdaTb9gpz5PQdvzcsKPTLgW2CPkF5ZNRzQeKFT4NSc1xN" , xpub )
67
+ })
68
+ }
69
+
70
+ func TestBTCAddress (t * testing.T ) {
71
+ testInitializedSimulators (t , func (t * testing.T , device * Device ) {
72
+ t .Helper ()
73
+ address , err := device .BTCAddress (
74
+ messages .BTCCoin_TBTC ,
75
+ []uint32 {
76
+ 84 + hardenedKeyStart ,
77
+ 1 + hardenedKeyStart ,
78
+ 0 + hardenedKeyStart ,
79
+ 1 ,
80
+ 10 ,
81
+ },
82
+ NewBTCScriptConfigSimple (messages .BTCScriptConfig_P2WPKH ),
83
+ false ,
84
+ )
85
+ require .NoError (t , err )
86
+ require .Equal (t , "tb1qq064dxjgl9h9wzgsmzy6t6306qew42w9ka02u3" , address )
87
+ })
88
+ }
89
+
90
+ func parseXPub (t * testing.T , xpubStr string , keypath ... uint32 ) * hdkeychain.ExtendedKey {
91
+ t .Helper ()
92
+ xpub , err := hdkeychain .NewKeyFromString (xpubStr )
93
+ require .NoError (t , err )
94
+
95
+ for _ , child := range keypath {
96
+ xpub , err = xpub .Derive (child )
97
+ require .NoError (t , err )
98
+ }
99
+ return xpub
100
+ }
101
+
102
+ func TestSimulatorBTCSignMessage (t * testing.T ) {
103
+ testInitializedSimulators (t , func (t * testing.T , device * Device ) {
104
+ t .Helper ()
105
+ coin := messages .BTCCoin_BTC
106
+ accountKeypath := []uint32 {49 + hardenedKeyStart , 0 + hardenedKeyStart , 0 + hardenedKeyStart }
107
+
108
+ xpubStr , err := device .BTCXPub (coin , accountKeypath , messages .BTCPubRequest_XPUB , false )
109
+ require .NoError (t , err )
110
+
111
+ xpub := parseXPub (t , xpubStr , 0 , 10 )
112
+ pubKey , err := xpub .ECPubKey ()
113
+ require .NoError (t , err )
114
+
115
+ sig , _ , _ , err := device .BTCSignMessage (
116
+ coin ,
117
+ & messages.BTCScriptConfigWithKeypath {
118
+ ScriptConfig : NewBTCScriptConfigSimple (messages .BTCScriptConfig_P2WPKH_P2SH ),
119
+ Keypath : append (accountKeypath , 0 , 10 ),
120
+ },
121
+ []byte ("message" ),
122
+ )
123
+ require .NoError (t , err )
124
+ sigHash := chainhash .DoubleHashB ([]byte ("\x18 Bitcoin Signed Message:\n \x07 message" ))
125
+ require .True (t , parseECDSASignature (t , sig ).Verify (sigHash , pubKey ))
126
+ })
127
+ }
128
+
129
+ func TestSimulatorBTCXPub (t * testing.T ) {
43
130
testConfigurations (t , func (t * testing.T , env * testEnv ) {
44
131
t .Helper ()
45
132
expected := "mocked-xpub"
@@ -110,7 +197,7 @@ func TestBTCXPub(t *testing.T) {
110
197
})
111
198
}
112
199
113
- func TestBTCAddress (t * testing.T ) {
200
+ func TestSimulatorBTCAddress (t * testing.T ) {
114
201
testConfigurations (t , func (t * testing.T , env * testEnv ) {
115
202
t .Helper ()
116
203
expected := "mocked-address"
0 commit comments