go-waku/waku/v2/utils/crypto.go

24 lines
880 B
Go
Raw Normal View History

2022-05-27 15:55:35 -04:00
package utils
import (
"crypto/ecdsa"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
2022-10-19 15:39:32 -04:00
"github.com/libp2p/go-libp2p/core/crypto"
2022-05-27 15:55:35 -04:00
)
2022-10-19 15:39:32 -04:00
// EcdsaPubKeyToSecp256k1PublicKey converts an `ecdsa.PublicKey` into a libp2p `crypto.Secp256k1PublicKey“
2022-05-27 15:55:35 -04:00
func EcdsaPubKeyToSecp256k1PublicKey(pubKey *ecdsa.PublicKey) *crypto.Secp256k1PublicKey {
xFieldVal := &secp256k1.FieldVal{}
yFieldVal := &secp256k1.FieldVal{}
2022-05-27 15:55:35 -04:00
xFieldVal.SetByteSlice(pubKey.X.Bytes())
yFieldVal.SetByteSlice(pubKey.Y.Bytes())
return (*crypto.Secp256k1PublicKey)(secp256k1.NewPublicKey(xFieldVal, yFieldVal))
2022-05-27 15:55:35 -04:00
}
// EcdsaPrivKeyToSecp256k1PrivKey converts an `ecdsa.PrivateKey` into a libp2p `crypto.Secp256k1PrivateKey“
2022-05-27 15:55:35 -04:00
func EcdsaPrivKeyToSecp256k1PrivKey(privKey *ecdsa.PrivateKey) *crypto.Secp256k1PrivateKey {
privK := secp256k1.PrivKeyFromBytes(privKey.D.Bytes())
2022-05-27 15:55:35 -04:00
return (*crypto.Secp256k1PrivateKey)(privK)
}