Skip to content

Commit

Permalink
add SignMessage test
Browse files Browse the repository at this point in the history
  • Loading branch information
superwunc committed Jul 20, 2024
1 parent bf73032 commit cced50f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions internal/common_util/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common/hexutil"
)

func EthereumSignHexStr(msg string, privateKey *ecdsa.PrivateKey) (string, error) {
Expand All @@ -18,6 +19,20 @@ func EthereumSignHexStr(msg string, privateKey *ecdsa.PrivateKey) (string, error
return EncodeToHexStringWithPrefix(hash), nil
}
}

func SignMessage(message string, privateKey *ecdsa.PrivateKey) (string, error) {
messageHash := accounts.TextHash([]byte(message))

signature, err := crypto.Sign(messageHash, privateKey)
if err != nil {
return "", err
}

signature[crypto.RecoveryIDOffset] += 27

return hexutil.Encode(signature), nil
}

func DecodeStringWithPrefix(data string) ([]byte, error) {
if data[:2] == "0x" {
data = data[2:]
Expand Down
2 changes: 1 addition & 1 deletion plugins/passkey_relay_party/tx_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (relay *RelayParty) finishTxSignature(ctx *gin.Context) {
response.GetResponse().FailCode(ctx, 403, "SignIn failed: "+err.Error())
return
}
signHexStr, err := common_util.EthereumSignHexStr(signPayment.TxData, privateKey)
signHexStr, err := common_util.SignMessage(signPayment.TxData, privateKey)
txSigRlt := seedworks.TxSignatureResult{
Code: 200,
TxData: signPayment.TxData,
Expand Down

0 comments on commit cced50f

Please sign in to comment.