2022-05-27 19:55:35 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2022-05-27 19:55:35 +00:00
|
|
|
)
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
// EcdsaPubKeyToSecp256k1PublicKey converts an `ecdsa.PublicKey` into a libp2p `crypto.Secp256k1PublicKey“
|
2022-05-27 19:55:35 +00:00
|
|
|
func EcdsaPubKeyToSecp256k1PublicKey(pubKey *ecdsa.PublicKey) *crypto.Secp256k1PublicKey {
|
|
|
|
xFieldVal := &btcec.FieldVal{}
|
|
|
|
yFieldVal := &btcec.FieldVal{}
|
|
|
|
xFieldVal.SetByteSlice(pubKey.X.Bytes())
|
|
|
|
yFieldVal.SetByteSlice(pubKey.Y.Bytes())
|
|
|
|
return (*crypto.Secp256k1PublicKey)(btcec.NewPublicKey(xFieldVal, yFieldVal))
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
// EcdsaPrivKeyToSecp256k1PrivKey converts an `ecdsa.PrivateKey` into a libp2p `crypto.Secp256k1PrivateKey“
|
2022-05-27 19:55:35 +00:00
|
|
|
func EcdsaPrivKeyToSecp256k1PrivKey(privKey *ecdsa.PrivateKey) *crypto.Secp256k1PrivateKey {
|
|
|
|
privK, _ := btcec.PrivKeyFromBytes(privKey.D.Bytes())
|
|
|
|
return (*crypto.Secp256k1PrivateKey)(privK)
|
|
|
|
}
|