From c94436e44a2b23e0620fab06863faa274e9beeb2 Mon Sep 17 00:00:00 2001 From: zhaobo Date: Fri, 11 Dec 2020 12:42:36 +0800 Subject: [PATCH] Add WalletSignMessage --- examples/local/main.go | 6 ++++++ local/wallet.go | 9 +++++++++ pkg/secp256k1/secp256k1.go | 9 ++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/examples/local/main.go b/examples/local/main.go index 0620a83..374e134 100644 --- a/examples/local/main.go +++ b/examples/local/main.go @@ -39,6 +39,7 @@ func main() { Params: nil, } + // 离线签名 s, err := local.WalletSignMessage(types.KTSecp256k1, ki.PrivateKey, msg) if err != nil { panic(err) @@ -47,6 +48,11 @@ func main() { println(hex.EncodeToString(s.Signature.Data)) // 47bcbb167fd9040bd02dba02789bc7bc0463c290db1be9b07065c12a64fb84dc546bef7aedfba789d0d7ce2c4532f8fa0d2dd998985ad3ec1a8b064c26e4625a01 + // 验证签名 + if err := local.WalletVerifyMessage(s); err != nil { + panic(err) + } + client := filecoin.New("https://1lB5G4SmGdSTikOo7l6vYlsktdd:b58884915362a99b4fc18c2bf8af8358@filecoin.infura.io") mid, err := client.MpoolPush(context.Background(), s) diff --git a/local/wallet.go b/local/wallet.go index af51cbe..8eb664b 100644 --- a/local/wallet.go +++ b/local/wallet.go @@ -64,6 +64,11 @@ func WalletSign(typ types.KeyType, pk []byte, data []byte) (*crypto.Signature, e return sigs.Sign(ActSigType(typ), pk, data) } +// WalletVerify verify the signed message +func WalletVerify(sig *crypto.Signature, addr address.Address, msg []byte) error { + return sigs.Verify(sig, addr, msg) +} + // WalletSignMessage signs the given message using the given private key. func WalletSignMessage(typ types.KeyType, pk []byte, msg *types.Message) (*types.SignedMessage, error) { mb, err := msg.ToStorageBlock() @@ -82,6 +87,10 @@ func WalletSignMessage(typ types.KeyType, pk []byte, msg *types.Message) (*types }, nil } +func WalletVerifyMessage(sm *types.SignedMessage) error { + return WalletVerify(sm.Signature, sm.Message.From, sm.Message.Cid().Bytes()) +} + func ActSigType(typ types.KeyType) crypto.SigType { switch typ { case types.KTBLS: diff --git a/pkg/secp256k1/secp256k1.go b/pkg/secp256k1/secp256k1.go index 7ed71f2..a613f8a 100644 --- a/pkg/secp256k1/secp256k1.go +++ b/pkg/secp256k1/secp256k1.go @@ -56,7 +56,14 @@ func Sign(sk, msg []byte) ([]byte, error) { // EcRecover recovers the public key from a message, signature pair. func EcRecover(msg, signature []byte) ([]byte, error) { - pk, _, err := btcec.RecoverCompact(btcec.S256(), signature, msg) + var sig = make([]byte, 65) + copy(sig, signature) + + v := sig[64] + 27 + copy(sig[1:], sig[:64]) + sig[0] = v + + pk, _, err := btcec.RecoverCompact(btcec.S256(), sig, msg) if err != nil { return nil, err }