Skip to content

Commit

Permalink
eth_test: add unit tests for ethsign and ethsignEIP1559
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
asi345 committed Aug 29, 2024
1 parent e78da17 commit b8e2672
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
73 changes: 73 additions & 0 deletions api/firmware/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package firmware

import (
"math/big"
"testing"

"github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/messages"
Expand Down Expand Up @@ -378,3 +379,75 @@ func TestSimulatorETHSignTypedMessage(t *testing.T) {
require.Len(t, sig, 65)
})
}

func TestSimulatorETHSign(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
t.Helper()
chainID := uint64(1)
keypath := []uint32{
44 + hardenedKeyStart,
60 + hardenedKeyStart,
0 + hardenedKeyStart,
0,
10,
}
nonce := uint64(8156)
gasPrice := new(big.Int).SetUint64(6000000000)
gasLimit := uint64(21000)
recipient := [20]byte{0x04, 0xf2, 0x64, 0xcf, 0x34, 0x44, 0x03, 0x13, 0xb4, 0xa0,
0x19, 0x2a, 0x35, 0x28, 0x14, 0xfb, 0xe9, 0x27, 0xb8, 0x85}
value := new(big.Int).SetUint64(530564000000000000)

sig, err := device.ETHSign(
chainID,
keypath,
nonce,
gasPrice,
gasLimit,
recipient,
value,
nil,
messages.ETHAddressCase_ETH_ADDRESS_CASE_MIXED,
)
require.NoError(t, err)

require.Len(t, sig, 65, "The signature should have exactly 65 bytes")
})
}

func TestSimulatorETHSignEIP1559(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
t.Helper()
chainID := uint64(1)
keypath := []uint32{
44 + hardenedKeyStart,
60 + hardenedKeyStart,
0 + hardenedKeyStart,
0,
10,
}
nonce := uint64(8156)
maxPriorityFeePerGas := new(big.Int)
maxFeePerGas := new(big.Int).SetUint64(6000000000)
gasLimit := uint64(21000)
recipient := [20]byte{0x04, 0xf2, 0x64, 0xcf, 0x34, 0x44, 0x03, 0x13, 0xb4, 0xa0,
0x19, 0x2a, 0x35, 0x28, 0x14, 0xfb, 0xe9, 0x27, 0xb8, 0x85}
value := new(big.Int).SetUint64(530564000000000000)

sig, err := device.ETHSignEIP1559(
chainID,
keypath,
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
recipient,
value,
nil,
messages.ETHAddressCase_ETH_ADDRESS_CASE_MIXED,
)
require.NoError(t, err)

require.Len(t, sig, 65, "The signature should have exactly 65 bytes")
})
}
1 change: 1 addition & 0 deletions cmd/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func main() {
[20]byte{0xd6, 0x10, 0x54, 0xf4, 0x45, 0x6d, 0x05, 0x55, 0xdc, 0x2d, 0xd8, 0x2b, 0x77, 0xf7, 0xad, 0x60, 0x74, 0x83, 0x61, 0x49},
new(big.Int).SetBytes([]byte("\x5a\xf3\x10\x7a\x40\x00")),
nil,
messages.ETHAddressCase_ETH_ADDRESS_CASE_MIXED,
)
errpanic(err)
fmt.Println(sig)
Expand Down

0 comments on commit b8e2672

Please sign in to comment.