crypto: MarshalPubkey unit test (#4846)

This commit is contained in:
battlmonstr 2022-07-28 04:23:58 +02:00 committed by GitHub
parent d85e3c626e
commit 620d65ef10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,6 +33,7 @@ import (
var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791"
var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
var testPubkeyHex = "7db227d7094ce215c3a0f57e1bcc732551fe351f94249471934567e0f5dc1bf795962b8cccb87a2eb56b29fbe37d614e2f4c3c45b789ae4f1f51f4cb21972ffd"
// These tests are sanity checks.
// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
@ -93,6 +94,26 @@ func TestUnmarshalPubkey(t *testing.T) {
}
}
func TestMarshalPubkey(t *testing.T) {
check := func(privateKeyHex, expectedPubkeyHex string) {
key, err := HexToECDSA(privateKeyHex)
if err != nil {
t.Errorf("bad private key: %s", err)
return
}
pubkeyHex := hex.EncodeToString(MarshalPubkey(&key.PublicKey))
if pubkeyHex != expectedPubkeyHex {
t.Errorf("unexpected public key: %s", pubkeyHex)
}
}
check(testPrivHex, testPubkeyHex)
check(
"36a7edad64d51a568b00e51d3fa8cd340aa704153010edf7f55ab3066ca4ef21",
"24bfa2cdce7c6a41184fa0809ad8d76969b7280952e9aa46179d90cfbab90f7d2b004928f0364389a1aa8d5166281f2ff7568493c1f719e8f6148ef8cf8af42d",
)
}
func TestSign(t *testing.T) {
key, _ := HexToECDSA(testPrivHex)
addr := common.HexToAddress(testAddrHex)