65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
|
package secp
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/filecoin-project/go-address"
|
||
|
"github.com/filecoin-project/go-crypto"
|
||
|
"github.com/minio/blake2b-simd"
|
||
|
|
||
|
"github.com/Secured-Finance/dione/sigs"
|
||
|
"github.com/Secured-Finance/dione/types"
|
||
|
)
|
||
|
|
||
|
type secpSigner struct{}
|
||
|
|
||
|
func (secpSigner) GenPrivate() ([]byte, error) {
|
||
|
priv, err := crypto.GenerateKey()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return priv, nil
|
||
|
}
|
||
|
|
||
|
func (secpSigner) ToPublic(pk []byte) ([]byte, error) {
|
||
|
return crypto.PublicKey(pk), nil
|
||
|
}
|
||
|
|
||
|
func (secpSigner) Sign(pk []byte, msg []byte) ([]byte, error) {
|
||
|
b2sum := blake2b.Sum256(msg)
|
||
|
sig, err := crypto.Sign(pk, b2sum[:])
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return sig, nil
|
||
|
}
|
||
|
|
||
|
func (secpSigner) Verify(sig []byte, a []byte, msg []byte) error {
|
||
|
b2sum := blake2b.Sum256(msg)
|
||
|
pubk, err := crypto.EcRecover(b2sum[:], sig)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
maybeaddr, err := address.NewSecp256k1Address(pubk)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
actualAddr, err := address.NewSecp256k1Address(a)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if actualAddr != maybeaddr {
|
||
|
return fmt.Errorf("signature did not match")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
sigs.RegisterSignature(types.SigTypeSecp256k1, secpSigner{})
|
||
|
}
|