Removed the weird WASM bindings, added a wait that prevents spamming of the CPU

This commit is contained in:
Tracker-Friendly 2024-09-29 11:15:24 +01:00
parent 8f9af6dce3
commit 2e04713865
2 changed files with 64 additions and 86 deletions

View File

@ -1,66 +0,0 @@
package library
import (
"bytes"
"strconv"
"strings"
"time"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"golang.org/x/crypto/argon2"
)
func PoW(difficulty uint64, resource string) (string, error) {
for {
initialTime := time.Now().Unix()
var timestamp [8]byte
binary.LittleEndian.PutUint64(timestamp[:], uint64(initialTime))
var nonce [16]byte
_, err := rand.Read(nonce[:])
if err != nil {
return "", err
}
output := hex.EncodeToString(argon2.IDKey(nonce[:], bytes.Join([][]byte{timestamp[:], []byte(resource)}, []byte{}), 1, 64*1024, 4, 32))
var difficultyString strings.Builder
for range difficulty {
difficultyString.WriteString("0")
}
if strings.HasPrefix(output, difficultyString.String()) {
return strconv.FormatUint(difficulty, 10) + ":" + strconv.FormatInt(initialTime, 10) + ":" + hex.EncodeToString(nonce[:]) + ":" + resource + ":", nil
}
}
}
func VerifyPoW(pow string) bool {
powSplit := strings.Split(pow, ":")
difficulty, err := strconv.ParseUint(powSplit[0], 10, 64)
if err != nil {
return false
}
timestamp, err := strconv.ParseInt(powSplit[1], 10, 64)
if err != nil {
return false
}
timestampBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(timestampBytes, uint64(timestamp))
nonce, err := hex.DecodeString(powSplit[2])
if err != nil {
return false
}
resource := powSplit[3]
output := hex.EncodeToString(argon2.IDKey(nonce, bytes.Join([][]byte{timestampBytes, []byte(resource)}, []byte{}), 1, 64*1024, 4, 32))
var difficultyString strings.Builder
for range difficulty {
difficultyString.WriteString("0")
}
if strings.HasPrefix(output, difficultyString.String()) {
return true
} else {
return false
}
}

82
main.go
View File

@ -1,27 +1,71 @@
package main package ailur_pow
import ( import (
"git.ailur.dev/ailur/pow-argon2/library" "bytes"
"strconv"
"strings"
"time"
"fmt" "crypto/rand"
"syscall/js" "encoding/binary"
"encoding/hex"
"golang.org/x/crypto/argon2"
) )
func main() { func PoW(difficulty uint64, resource string, wait int64) (string, error) {
fmt.Println("Proof of work module online") for {
resource := js.Global().Get("resource").String() initialTime := time.Now().Unix()
difficulty := js.Global().Get("difficulty").Int() var timestamp [8]byte
fmt.Println("Beginning PoW with difficulty", difficulty, "and resource", resource) binary.LittleEndian.PutUint64(timestamp[:], uint64(initialTime))
result, err := library.PoW(uint64(difficulty), resource)
var nonce [16]byte
_, err := rand.Read(nonce[:])
if err != nil { if err != nil {
fmt.Println("Error:", err) return "", err
js.Global().Set("return", js.ValueOf(err.Error())) }
js.Global().Set("returnCode", js.ValueOf(1))
js.Global().Call("WASMComplete") output := hex.EncodeToString(argon2.IDKey(nonce[:], bytes.Join([][]byte{timestamp[:], []byte(resource)}, []byte{}), 1, 64*1024, 4, 32))
} else { var difficultyString strings.Builder
fmt.Println("Result:", result) for range difficulty {
js.Global().Set("return", js.ValueOf(result)) difficultyString.WriteString("0")
js.Global().Set("returnCode", js.ValueOf(0)) }
js.Global().Call("WASMComplete") if strings.HasPrefix(output, difficultyString.String()) {
return strconv.FormatUint(difficulty, 10) + ":" + strconv.FormatInt(initialTime, 10) + ":" + hex.EncodeToString(nonce[:]) + ":" + resource + ":", nil
}
if wait > 0 {
// Wait for a while before trying again
time.Sleep(time.Duration(wait) * time.Millisecond)
}
}
}
func VerifyPoW(pow string) bool {
powSplit := strings.Split(pow, ":")
difficulty, err := strconv.ParseUint(powSplit[0], 10, 64)
if err != nil {
return false
}
timestamp, err := strconv.ParseInt(powSplit[1], 10, 64)
if err != nil {
return false
}
timestampBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(timestampBytes, uint64(timestamp))
nonce, err := hex.DecodeString(powSplit[2])
if err != nil {
return false
}
resource := powSplit[3]
output := hex.EncodeToString(argon2.IDKey(nonce, bytes.Join([][]byte{timestampBytes, []byte(resource)}, []byte{}), 1, 64*1024, 4, 32))
var difficultyString strings.Builder
for range difficulty {
difficultyString.WriteString("0")
}
if strings.HasPrefix(output, difficultyString.String()) {
return true
} else {
return false
} }
} }