lotus/lib/sigs/secp/init.go
Kevin Li 9d1208c9ff api: ethrpc: implement eth_sendRawTransaction (#9334)
Co-authored-by: Raúl Kripalani <raul@protocol.ai>
2022-10-13 16:39:14 +02:00

61 lines
1.1 KiB
Go

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