Skip to content

Commit c6f668e

Browse files
committed
use [20]byte for WithdrawalCredentials address parameter
1 parent 3c49b2c commit c6f668e

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

crypto/beacon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const (
1818
)
1919

2020
// WithdrawalCredentials constructs 32-byte withdrawal credentials from a prefix byte and a 20-byte address.
21-
func WithdrawalCredentials(prefix byte, withdrawalAddr []byte) []byte {
21+
func WithdrawalCredentials(prefix byte, withdrawalAddr [20]byte) []byte {
2222
creds := make([]byte, 32)
2323
creds[0] = prefix
24-
copy(creds[12:], withdrawalAddr)
24+
copy(creds[12:], withdrawalAddr[:])
2525
return creds
2626
}
2727

crypto/beacon_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import (
1313
func TestWithdrawalCredentials(t *testing.T) {
1414
t.Run("0x01 from hex address", func(t *testing.T) {
1515
eth1Address := common.HexToAddress("d999bc994e0274235b65ca72ec430b8de3eb7df9")
16-
require.EqualValues(t, WithdrawalCredentials(ETH1WithdrawalPrefix, eth1Address[:]), []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xd9, 0x99, 0xbc, 0x99, 0x4e, 0x2, 0x74, 0x23, 0x5b, 0x65, 0xca, 0x72, 0xec, 0x43, 0xb, 0x8d, 0xe3, 0xeb, 0x7d, 0xf9})
16+
require.EqualValues(t, WithdrawalCredentials(ETH1WithdrawalPrefix, eth1Address), []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xd9, 0x99, 0xbc, 0x99, 0x4e, 0x2, 0x74, 0x23, 0x5b, 0x65, 0xca, 0x72, 0xec, 0x43, 0xb, 0x8d, 0xe3, 0xeb, 0x7d, 0xf9})
1717
})
1818

1919
t.Run("0x01 from byte address", func(t *testing.T) {
2020
eth1Address := common.Address{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
21-
require.EqualValues(t, WithdrawalCredentials(ETH1WithdrawalPrefix, eth1Address[:]), []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
21+
require.EqualValues(t, WithdrawalCredentials(ETH1WithdrawalPrefix, eth1Address), []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
2222
})
2323

2424
t.Run("0x02 compounding", func(t *testing.T) {
2525
addr := common.Address{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
26-
creds := WithdrawalCredentials(CompoundingWithdrawalPrefix, addr[:])
26+
creds := WithdrawalCredentials(CompoundingWithdrawalPrefix, addr)
2727

2828
require.Len(t, creds, 32)
2929
require.Equal(t, byte(2), creds[0], "first byte should be 0x02")
@@ -36,7 +36,7 @@ func TestWithdrawalCredentials(t *testing.T) {
3636

3737
func TestComputeDepositMessageSigningRoot(t *testing.T) {
3838
// Effective 20-byte address used across signing root tests.
39-
addr := []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}
39+
addr := [20]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}
4040

4141
t.Run("mainnet", func(t *testing.T) {
4242
r, err := ComputeDepositMessageSigningRoot(core.MainNetwork, &phase0.DepositMessage{
@@ -60,7 +60,7 @@ func TestComputeDepositMessageSigningRoot(t *testing.T) {
6060
}
6161

6262
func TestDepositDataRootForFork(t *testing.T) {
63-
addr := []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}
63+
addr := [20]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}
6464

6565
t.Run("mainnet", func(t *testing.T) {
6666
r, err := DepositDataRootForFork(
@@ -93,16 +93,16 @@ func TestValidateWithdrawalCredentials(t *testing.T) {
9393
}{
9494
{
9595
name: "valid 32-byte 0x01",
96-
input: WithdrawalCredentials(ETH1WithdrawalPrefix, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}),
96+
input: WithdrawalCredentials(ETH1WithdrawalPrefix, [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}),
9797
},
9898
{
9999
name: "valid 32-byte 0x02",
100-
input: WithdrawalCredentials(CompoundingWithdrawalPrefix, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}),
100+
input: WithdrawalCredentials(CompoundingWithdrawalPrefix, [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}),
101101
},
102102
{
103103
name: "non-zero padding accepted",
104104
input: func() []byte {
105-
c := WithdrawalCredentials(ETH1WithdrawalPrefix, make([]byte, 20))
105+
c := WithdrawalCredentials(ETH1WithdrawalPrefix, [20]byte{})
106106
c[5] = 0xFF
107107
return c
108108
}(),
@@ -148,7 +148,7 @@ func TestValidateWithdrawalCredentials(t *testing.T) {
148148

149149
func TestDepositDataRootForFork_0x02(t *testing.T) {
150150
t.Run("mainnet 0x02", func(t *testing.T) {
151-
creds := WithdrawalCredentials(CompoundingWithdrawalPrefix, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
151+
creds := WithdrawalCredentials(CompoundingWithdrawalPrefix, [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
152152
_, err := DepositDataRootForFork(
153153
phase0.Version{0, 0, 0, 0},
154154
[]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
@@ -167,7 +167,7 @@ func TestVerifyDepositData_0x02(t *testing.T) {
167167
pk := phase0.BLSPubKey{}
168168
copy(pk[:], sk.GetPublicKey().Serialize())
169169

170-
creds := WithdrawalCredentials(CompoundingWithdrawalPrefix, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
170+
creds := WithdrawalCredentials(CompoundingWithdrawalPrefix, [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20})
171171

172172
r, err := ComputeDepositMessageSigningRoot(core.MainNetwork, &phase0.DepositMessage{
173173
PublicKey: pk,

testing/fixtures/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
)
1515

1616
var (
17-
TestWithdrawalCred = crypto.WithdrawalCredentials(crypto.ETH1WithdrawalPrefix, make([]byte, 20))
18-
TestWithdrawalCred0x02 = crypto.WithdrawalCredentials(crypto.CompoundingWithdrawalPrefix, TestOwnerAddress.Bytes())
17+
TestWithdrawalCred = crypto.WithdrawalCredentials(crypto.ETH1WithdrawalPrefix, [20]byte{})
18+
TestWithdrawalCred0x02 = crypto.WithdrawalCredentials(crypto.CompoundingWithdrawalPrefix, TestOwnerAddress)
1919
TestFork = [4]byte{0, 0, 0, 0}
2020
TestNonce = uint64(0)
2121
TestOwnerAddress = common.Address{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

0 commit comments

Comments
 (0)