2022-05-27 15:55:35 -04:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
2024-11-26 03:06:12 +08:00
|
|
|
"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 {
|
2024-11-26 03:06:12 +08:00
|
|
|
xFieldVal := &secp256k1.FieldVal{}
|
|
|
|
yFieldVal := &secp256k1.FieldVal{}
|
2022-05-27 15:55:35 -04:00
|
|
|
xFieldVal.SetByteSlice(pubKey.X.Bytes())
|
|
|
|
yFieldVal.SetByteSlice(pubKey.Y.Bytes())
|
2024-11-26 03:06:12 +08:00
|
|
|
return (*crypto.Secp256k1PublicKey)(secp256k1.NewPublicKey(xFieldVal, yFieldVal))
|
2022-05-27 15:55:35 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 10:24:05 -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 {
|
2024-11-26 03:06:12 +08:00
|
|
|
privK := secp256k1.PrivKeyFromBytes(privKey.D.Bytes())
|
2022-05-27 15:55:35 -04:00
|
|
|
return (*crypto.Secp256k1PrivateKey)(privK)
|
|
|
|
}
|