2023-11-03 20:17:04 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
|
|
|
)
|
|
|
|
|
2023-11-03 22:38:23 +00:00
|
|
|
func GenerateKeyPair() (*secp256k1.PrivateKey, *secp256k1.PublicKey, error) {
|
2023-11-03 20:17:04 +00:00
|
|
|
var (
|
|
|
|
sk *secp256k1.PrivateKey
|
2023-11-03 22:38:23 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
if sk, err = secp256k1.GeneratePrivateKey(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return sk, sk.PubKey(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Sign(sk *secp256k1.PrivateKey, k1_ string) (string, error) {
|
|
|
|
var (
|
2023-11-03 20:17:04 +00:00
|
|
|
k1 []byte
|
|
|
|
sig []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if k1, err = hex.DecodeString(k1_); err != nil {
|
2023-11-03 22:38:23 +00:00
|
|
|
return "", err
|
2023-11-03 20:17:04 +00:00
|
|
|
}
|
|
|
|
if sig, err = ecdsa.SignASN1(rand.Reader, sk.ToECDSA(), k1); err != nil {
|
2023-11-03 22:38:23 +00:00
|
|
|
return "", err
|
2023-11-03 20:17:04 +00:00
|
|
|
}
|
2023-11-03 22:38:23 +00:00
|
|
|
return hex.EncodeToString(sig), nil
|
2023-11-03 20:17:04 +00:00
|
|
|
}
|