Skip to content

Commit cccb6e3

Browse files
committed
eth_test: add unit tests for ethsign and ethsignEIP1559
There was no unit tests for these functionalities before. For any occasion, we added them so that modifications to these functions will be tested. Signed-off-by: asi345 <inanata15@gmail.com>
1 parent e771169 commit cccb6e3

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

api/firmware/eth_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package firmware
1616

1717
import (
18+
"math/big"
1819
"testing"
1920

2021
"github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/messages"
@@ -378,3 +379,75 @@ func TestSimulatorETHSignTypedMessage(t *testing.T) {
378379
require.Len(t, sig, 65)
379380
})
380381
}
382+
383+
func TestSimulatorETHSign(t *testing.T) {
384+
testInitializedSimulators(t, func(t *testing.T, device *Device) {
385+
t.Helper()
386+
chainID := uint64(1)
387+
keypath := []uint32{
388+
44 + hardenedKeyStart,
389+
60 + hardenedKeyStart,
390+
0 + hardenedKeyStart,
391+
0,
392+
10,
393+
}
394+
nonce := uint64(8156)
395+
gasPrice := new(big.Int).SetUint64(6000000000)
396+
gasLimit := uint64(21000)
397+
recipient := [20]byte{0x04, 0xf2, 0x64, 0xcf, 0x34, 0x44, 0x03, 0x13, 0xb4, 0xa0,
398+
0x19, 0x2a, 0x35, 0x28, 0x14, 0xfb, 0xe9, 0x27, 0xb8, 0x85}
399+
value := new(big.Int).SetUint64(530564000000000000)
400+
401+
sig, err := device.ETHSign(
402+
chainID,
403+
keypath,
404+
nonce,
405+
gasPrice,
406+
gasLimit,
407+
recipient,
408+
value,
409+
nil,
410+
messages.ETHAddressCase_ETH_ADDRESS_CASE_MIXED,
411+
)
412+
require.NoError(t, err)
413+
414+
require.Len(t, sig, 65, "The signature should have exactly 65 bytes")
415+
})
416+
}
417+
418+
func TestSimulatorETHSignEIP1559(t *testing.T) {
419+
testInitializedSimulators(t, func(t *testing.T, device *Device) {
420+
t.Helper()
421+
chainID := uint64(1)
422+
keypath := []uint32{
423+
44 + hardenedKeyStart,
424+
60 + hardenedKeyStart,
425+
0 + hardenedKeyStart,
426+
0,
427+
10,
428+
}
429+
nonce := uint64(8156)
430+
maxPriorityFeePerGas := new(big.Int)
431+
maxFeePerGas := new(big.Int).SetUint64(6000000000)
432+
gasLimit := uint64(21000)
433+
recipient := [20]byte{0x04, 0xf2, 0x64, 0xcf, 0x34, 0x44, 0x03, 0x13, 0xb4, 0xa0,
434+
0x19, 0x2a, 0x35, 0x28, 0x14, 0xfb, 0xe9, 0x27, 0xb8, 0x85}
435+
value := new(big.Int).SetUint64(530564000000000000)
436+
437+
sig, err := device.ETHSignEIP1559(
438+
chainID,
439+
keypath,
440+
nonce,
441+
maxPriorityFeePerGas,
442+
maxFeePerGas,
443+
gasLimit,
444+
recipient,
445+
value,
446+
nil,
447+
messages.ETHAddressCase_ETH_ADDRESS_CASE_MIXED,
448+
)
449+
require.NoError(t, err)
450+
451+
require.Len(t, sig, 65, "The signature should have exactly 65 bytes")
452+
})
453+
}

cmd/playground/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ func main() {
221221
[20]byte{0xd6, 0x10, 0x54, 0xf4, 0x45, 0x6d, 0x05, 0x55, 0xdc, 0x2d, 0xd8, 0x2b, 0x77, 0xf7, 0xad, 0x60, 0x74, 0x83, 0x61, 0x49},
222222
new(big.Int).SetBytes([]byte("\x5a\xf3\x10\x7a\x40\x00")),
223223
nil,
224+
messages.ETHAddressCase_ETH_ADDRESS_CASE_MIXED,
224225
)
225226
errpanic(err)
226227
fmt.Println(sig)

0 commit comments

Comments
 (0)