Skip to content

Commit

Permalink
Add WalletSignMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
myxtype committed Dec 11, 2020
1 parent f01032f commit c94436e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
Params: nil,
}

// 离线签名
s, err := local.WalletSignMessage(types.KTSecp256k1, ki.PrivateKey, msg)
if err != nil {
panic(err)
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions local/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion pkg/secp256k1/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c94436e

Please sign in to comment.