Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: verifySignature function #2776

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions gnovm/stdlibs/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions gnovm/stdlibs/std/native.gno
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func DecodeBech32(addr Address) (prefix string, bz [20]byte, ok bool) {
return decodeBech32(string(addr))
}

func VerifySignature(pubKeySigner string, signature string, msg string) (bool, string) {
return verifySignature(pubKeySigner, msg, signature)
}

// Variations which don't use named types.
func origSend() (denoms []string, amounts []int64)
func origCaller() string
Expand All @@ -65,3 +69,4 @@ func getRealm(height int) (address string, pkgPath string)
func derivePkgAddr(pkgPath string) string
func encodeBech32(prefix string, bz [20]byte) string
func decodeBech32(addr string) (prefix string, bz [20]byte, ok bool)
func verifySignature(pubKeySigner string, msg string, signature string) (bool, string)
18 changes: 18 additions & 0 deletions gnovm/stdlibs/std/native.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package std

import (
"encoding/hex"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/tm2/pkg/bech32"
"github.com/gnolang/gno/tm2/pkg/crypto"
Expand Down Expand Up @@ -150,6 +152,22 @@
return b32
}

func X_verifySignature(pubKeySigner string, msg string, signature string) (bool, string) {
key, err := crypto.PubKeyFromBech32(pubKeySigner)
if err != nil {
panic(err) // should not happen

Check warning on line 158 in gnovm/stdlibs/std/native.go

View check run for this annotation

Codecov / codecov/patch

gnovm/stdlibs/std/native.go#L158

Added line #L158 was not covered by tests
}

decodedData, err := hex.DecodeString(signature)
if err != nil {
panic(err) // should not happen

Check warning on line 163 in gnovm/stdlibs/std/native.go

View check run for this annotation

Codecov / codecov/patch

gnovm/stdlibs/std/native.go#L163

Added line #L163 was not covered by tests
}

validSignature := key.VerifyBytes([]byte(msg), decodedData)

return validSignature, key.Address().String()
}

func X_decodeBech32(addr string) (prefix string, bytes [20]byte, ok bool) {
prefix, bz, err := bech32.Decode(addr)
if err != nil || len(bz) != 20 {
Expand Down
32 changes: 32 additions & 0 deletions gnovm/stdlibs/std/native_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package std

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/assert"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
)

const DefaultAccount_Seed = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast"

func TestPrevRealmIsOrigin(t *testing.T) {
var (
user = gno.DerivePkgAddr("user1.gno").Bech32()
Expand Down Expand Up @@ -192,3 +196,31 @@ func TestPrevRealmIsOrigin(t *testing.T) {
})
}
}

func TestVerify(t *testing.T) {
kb := keys.NewInMemory()
pass := "hardPass"
info, err := kb.CreateAccount("user", DefaultAccount_Seed, pass, pass, 0, 0)
assert.NoError(t, err)

publicKey := info.GetPubKey().String() // gpub1pgfj7ard9eg82cjtv4u4xetrwqer2dntxyfzxz3pqfzjcj8wph4wl0x7tqu3k7geuqsz2d45eddys0hgf0xd7dr2dupnqukpghs
goodMessage := "Verification Ok"
maliciousMessage := "Malicious Message"
signature, _, err := kb.Sign("user", pass, []byte(goodMessage))
assert.NoError(t, err)
signatureHex := hex.EncodeToString(signature)
signatureValid, signer := X_verifySignature(publicKey, goodMessage, signatureHex)

if !signatureValid {
t.Error("verify failed")
}

if signer != info.GetAddress().String() {
t.Error("signer is not equal to address")
}

signatureValid, _ = X_verifySignature(publicKey, maliciousMessage, signatureHex)
if signatureValid {
t.Error("verify worked on malicious message")
}
}
14 changes: 14 additions & 0 deletions gnovm/tests/files/std12_stdlibs.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "std"

func main() {
defer func() {
// assert panic is recoverable
println(recover())
}()
std.VerifySignature("","","")
}

// Error:
// decoding Bech32 failed: must provide a valid bech32 string
2 changes: 1 addition & 1 deletion gnovm/tests/files/std5_stdlibs.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {

// Stacktrace:
// panic: frame not found
// callerAt<VPBlock(3,44)>(n<VPBlock(1,0)>)
// callerAt<VPBlock(3,45)>(n<VPBlock(1,0)>)
// gonative:std.callerAt
// std<VPBlock(2,0)>.GetCallerAt(2)
// std/native.gno:44
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/std8_stdlibs.gno
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {

// Stacktrace:
// panic: frame not found
// callerAt<VPBlock(3,44)>(n<VPBlock(1,0)>)
// callerAt<VPBlock(3,45)>(n<VPBlock(1,0)>)
// gonative:std.callerAt
// std<VPBlock(2,0)>.GetCallerAt(4)
// std/native.gno:44
Expand Down
Loading