Added verification of PoWs and made the format actually make sense

This commit is contained in:
Tracker-Friendly 2024-09-22 15:31:23 +01:00
parent 3349e93696
commit cb4dc1e79f
3 changed files with 33 additions and 2 deletions

View File

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" /> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project> </project>

View File

@ -31,7 +31,36 @@ func PoW(difficulty uint64, resource string) (string, error) {
difficultyString.WriteString("0") difficultyString.WriteString("0")
} }
if strings.HasPrefix(output, difficultyString.String()) { if strings.HasPrefix(output, difficultyString.String()) {
return strconv.FormatUint(difficulty, 10) + ":" + strconv.FormatInt(initialTime, 10) + ":" + hex.EncodeToString(nonce[:]) + ":" + output, nil 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
}
}

BIN
pow

Binary file not shown.