feat: cleanup module, add examples and tests
This commit is contained in:
parent
b443540d61
commit
8257f9304b
6 changed files with 72 additions and 11 deletions
6
Makefile
6
Makefile
|
@ -7,3 +7,9 @@ build-rust:
|
|||
|
||||
test:
|
||||
cd ./mcaptcha-pow-ffi/ && cargo test
|
||||
|
||||
example:
|
||||
go run examples/main.go ^C
|
||||
|
||||
test:
|
||||
go test
|
||||
|
|
22
examples/main.go
Normal file
22
examples/main.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"C"
|
||||
"fmt"
|
||||
|
||||
"git.batsense.net/mCaptcha/gomcaptcha"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
salt := "56126a526f6f1e6728e904d8b574ebb8"
|
||||
phrase := "ef055efeac72b99ed24af687e49d6ec7"
|
||||
difficulty := uint(50000)
|
||||
|
||||
res := gomcaptcha.ProveWork(salt, phrase, difficulty)
|
||||
fmt.Println(res.Nonce)
|
||||
fmt.Println(res.Result)
|
||||
fmt.Println(res.IsSufficientDifficulty(salt, difficulty))
|
||||
fmt.Println(res.IsValidProof(phrase, salt))
|
||||
|
||||
}
|
9
go.mod
9
go.mod
|
@ -1,3 +1,10 @@
|
|||
module git.batsense.net/mCaptcha/mcaptcha-pow-go
|
||||
module git.batsense.net/mCaptcha/gomcaptcha
|
||||
|
||||
go 1.21.5
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.8.4 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
9
go.sum
Normal file
9
go.sum
Normal file
|
@ -0,0 +1,9 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@ -1,4 +1,4 @@
|
|||
package "mcaptcha_pow_go"
|
||||
package gomcaptcha
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: ./lib/libmcaptcha_pow_ffi.a -ldl
|
||||
|
@ -6,11 +6,10 @@ package "mcaptcha_pow_go"
|
|||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import "fmt"
|
||||
|
||||
type ProofOfWork struct {
|
||||
nonce uint64
|
||||
result string
|
||||
Nonce uint64
|
||||
Result string
|
||||
}
|
||||
|
||||
func ProveWork(salt string, phrase string, difficulty uint) ProofOfWork {
|
||||
|
@ -18,16 +17,16 @@ func ProveWork(salt string, phrase string, difficulty uint) ProofOfWork {
|
|||
C.CString(phrase), C.uint(difficulty))
|
||||
|
||||
return ProofOfWork{
|
||||
nonce: uint64(res.nonce),
|
||||
result: C.GoString(res.result),
|
||||
Nonce: uint64(res.nonce),
|
||||
Result: C.GoString(res.result),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (v ProofOfWork) IsSufficientDifficulty(salt string, difficulty uint) bool {
|
||||
res := C.is_sufficient_difficulty(
|
||||
C.ulong(v.nonce),
|
||||
C.CString(v.result),
|
||||
C.ulong(v.Nonce),
|
||||
C.CString(v.Result),
|
||||
C.CString(salt),
|
||||
C.uint(difficulty))
|
||||
return bool(res)
|
||||
|
@ -36,8 +35,8 @@ func (v ProofOfWork) IsSufficientDifficulty(salt string, difficulty uint) bool {
|
|||
|
||||
func (v ProofOfWork) IsValidProof(phrase string, salt string) bool {
|
||||
res := C.is_valid_proof(
|
||||
C.ulong(v.nonce),
|
||||
C.CString(v.result),
|
||||
C.ulong(v.Nonce),
|
||||
C.CString(v.Result),
|
||||
C.CString(phrase),
|
||||
C.CString(salt))
|
||||
return bool(res)
|
18
gomcaptcha_test.go
Normal file
18
gomcaptcha_test.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package gomcaptcha
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGoMcaptcha(t *testing.T) {
|
||||
|
||||
salt := "56126a526f6f1e6728e904d8b574ebb8"
|
||||
phrase := "ef055efeac72b99ed24af687e49d6ec7"
|
||||
difficulty := uint(50000)
|
||||
|
||||
res := ProveWork(salt, phrase, difficulty)
|
||||
assert.True(t, res.IsSufficientDifficulty(salt, difficulty))
|
||||
assert.True(t, res.IsValidProof(phrase, salt))
|
||||
}
|
Loading…
Reference in a new issue