88 "testing"
99
1010 "github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/messages"
11+ "github.com/BitBoxSwiss/bitbox02-api-go/util/semver"
1112 "github.com/stretchr/testify/require"
1213 "golang.org/x/crypto/sha3"
1314)
@@ -18,6 +19,53 @@ func hashKeccak(b []byte) []byte {
1819 return h .Sum (nil )
1920}
2021
22+ var eip712Msg = []byte (`
23+ {
24+ "types": {
25+ "EIP712Domain": [
26+ { "name": "name", "type": "string" },
27+ { "name": "version", "type": "string" },
28+ { "name": "chainId", "type": "uint256" },
29+ { "name": "verifyingContract", "type": "address" }
30+ ],
31+ "Attachment": [
32+ { "name": "contents", "type": "string" }
33+ ],
34+ "Person": [
35+ { "name": "name", "type": "string" },
36+ { "name": "wallet", "type": "address" },
37+ { "name": "age", "type": "uint8" }
38+ ],
39+ "Mail": [
40+ { "name": "from", "type": "Person" },
41+ { "name": "to", "type": "Person" },
42+ { "name": "contents", "type": "string" },
43+ { "name": "attachments", "type": "Attachment[]" }
44+ ]
45+ },
46+ "primaryType": "Mail",
47+ "domain": {
48+ "name": "Ether Mail",
49+ "version": "1",
50+ "chainId": 1,
51+ "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
52+ },
53+ "message": {
54+ "from": {
55+ "name": "Cow",
56+ "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
57+ "age": 20
58+ },
59+ "to": {
60+ "name": "Bob",
61+ "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
62+ "age": "0x1e"
63+ },
64+ "contents": "Hello, Bob!",
65+ "attachments": [{ "contents": "attachment1" }, { "contents": "attachment2" }]
66+ }
67+ }` )
68+
2169func parseTypeNoErr (t * testing.T , typ string , types map [string ]interface {}) * messages.ETHSignTypedMessageRequest_MemberType {
2270 t .Helper ()
2371 parsed , err := parseType (typ , types )
@@ -306,53 +354,6 @@ func TestSimulatorETHSignMessage(t *testing.T) {
306354func TestSimulatorETHSignTypedMessage (t * testing.T ) {
307355 testInitializedSimulators (t , func (t * testing.T , device * Device , stdOut * bytes.Buffer ) {
308356 t .Helper ()
309- msg := []byte (`
310- {
311- "types": {
312- "EIP712Domain": [
313- { "name": "name", "type": "string" },
314- { "name": "version", "type": "string" },
315- { "name": "chainId", "type": "uint256" },
316- { "name": "verifyingContract", "type": "address" }
317- ],
318- "Attachment": [
319- { "name": "contents", "type": "string" }
320- ],
321- "Person": [
322- { "name": "name", "type": "string" },
323- { "name": "wallet", "type": "address" },
324- { "name": "age", "type": "uint8" }
325- ],
326- "Mail": [
327- { "name": "from", "type": "Person" },
328- { "name": "to", "type": "Person" },
329- { "name": "contents", "type": "string" },
330- { "name": "attachments", "type": "Attachment[]" }
331- ]
332- },
333- "primaryType": "Mail",
334- "domain": {
335- "name": "Ether Mail",
336- "version": "1",
337- "chainId": 1,
338- "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
339- },
340- "message": {
341- "from": {
342- "name": "Cow",
343- "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
344- "age": 20
345- },
346- "to": {
347- "name": "Bob",
348- "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
349- "age": "0x1e"
350- },
351- "contents": "Hello, Bob!",
352- "attachments": [{ "contents": "attachment1" }, { "contents": "attachment2" }]
353- }
354- }` )
355-
356357 sig , err := device .ETHSignTypedMessage (
357358 1 ,
358359 []uint32 {
@@ -362,13 +363,64 @@ func TestSimulatorETHSignTypedMessage(t *testing.T) {
362363 0 ,
363364 10 ,
364365 },
365- msg ,
366+ eip712Msg ,
367+ true ,
366368 )
367369 require .NoError (t , err )
368370 require .Len (t , sig , 65 )
369371 })
370372}
371373
374+ func TestSimulatorETHSignTypedMessageAntikleptoEnabled (t * testing.T ) {
375+ testInitializedSimulators (t , func (t * testing.T , device * Device , stdOut * bytes.Buffer ) {
376+ t .Helper ()
377+ keypath := []uint32 {
378+ 44 + hardenedKeyStart ,
379+ 60 + hardenedKeyStart ,
380+ 0 + hardenedKeyStart ,
381+ 0 ,
382+ 10 ,
383+ }
384+
385+ sig1 , err := device .ETHSignTypedMessage (1 , keypath , eip712Msg , true )
386+ require .NoError (t , err )
387+ sig2 , err := device .ETHSignTypedMessage (1 , keypath , eip712Msg , true )
388+ require .NoError (t , err )
389+
390+ require .Len (t , sig1 , 65 )
391+ require .Len (t , sig2 , 65 )
392+ require .NotEqual (t , sig1 , sig2 )
393+ })
394+ }
395+
396+ func TestSimulatorETHSignTypedMessageAntikleptoDisabled (t * testing.T ) {
397+ testInitializedSimulators (t , func (t * testing.T , device * Device , stdOut * bytes.Buffer ) {
398+ t .Helper ()
399+ keypath := []uint32 {
400+ 44 + hardenedKeyStart ,
401+ 60 + hardenedKeyStart ,
402+ 0 + hardenedKeyStart ,
403+ 0 ,
404+ 10 ,
405+ }
406+
407+ if device .Version ().AtLeast (semver .NewSemVer (9 , 26 , 0 )) {
408+ sig1 , err := device .ETHSignTypedMessage (1 , keypath , eip712Msg , false )
409+ require .NoError (t , err )
410+ sig2 , err := device .ETHSignTypedMessage (1 , keypath , eip712Msg , false )
411+ require .NoError (t , err )
412+
413+ require .Len (t , sig1 , 65 )
414+ require .Len (t , sig2 , 65 )
415+ require .Equal (t , sig1 , sig2 )
416+ return
417+ }
418+
419+ _ , err := device .ETHSignTypedMessage (1 , keypath , eip712Msg , false )
420+ require .EqualError (t , err , UnsupportedError ("9.26.0" ).Error ())
421+ })
422+ }
423+
372424func TestSimulatorETHSign (t * testing.T ) {
373425 testInitializedSimulators (t , func (t * testing.T , device * Device , stdOut * bytes.Buffer ) {
374426 t .Helper ()
0 commit comments