Update key.go

This commit is contained in:
Alvarez 2025-09-21 16:20:00 +02:00 committed by GitHub
parent 4414e2833f
commit 49ab541ce1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -146,6 +146,7 @@ func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {
// into the Direct ICAP spec. for simplicity and easier compatibility with other libs, we // into the Direct ICAP spec. for simplicity and easier compatibility with other libs, we
// retry until the first byte is 0. // retry until the first byte is 0.
func NewKeyForDirectICAP(rand io.Reader) *Key { func NewKeyForDirectICAP(rand io.Reader) *Key {
for {
randBytes := make([]byte, 64) randBytes := make([]byte, 64)
_, err := rand.Read(randBytes) _, err := rand.Read(randBytes)
if err != nil { if err != nil {
@ -157,11 +158,11 @@ func NewKeyForDirectICAP(rand io.Reader) *Key {
panic("key generation: ecdsa.GenerateKey failed: " + err.Error()) panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
} }
key := newKeyFromECDSA(privateKeyECDSA) key := newKeyFromECDSA(privateKeyECDSA)
if !strings.HasPrefix(key.Address.Hex(), "0x00") { if strings.HasPrefix(key.Address.Hex(), "0x00") {
return NewKeyForDirectICAP(rand)
}
return key return key
} }
}
}
func newKey(rand io.Reader) (*Key, error) { func newKey(rand io.Reader) (*Key, error) {
privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand) privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand)