diff options
author | Joey Hess <joeyh@joeyh.name> | 2016-09-12 22:59:03 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2016-09-12 22:59:03 -0400 |
commit | e333a779338ff8bccdc4225fc953d6f4f0226db0 (patch) | |
tree | c07f1165ba101c2b10e914170fb07628e1d08d3a /HTTP/ProofOfWork.hs | |
parent | 13c408d2295597540f0b2dfb6f7b86e739876c90 (diff) | |
download | keysafe-e333a779338ff8bccdc4225fc953d6f4f0226db0.tar.gz |
add proof of work to countobjects
In this case, an empty string is hashed to generate the PoW.
Diffstat (limited to 'HTTP/ProofOfWork.hs')
-rw-r--r-- | HTTP/ProofOfWork.hs | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/HTTP/ProofOfWork.hs b/HTTP/ProofOfWork.hs index 04aec57..ef6ecfb 100644 --- a/HTTP/ProofOfWork.hs +++ b/HTTP/ProofOfWork.hs @@ -82,8 +82,19 @@ mkRandomSalt = do instance Raaz.Random Word8 -isValidProofOfWork :: ProofOfWork -> ProofOfWorkRequirement -> StorableObjectIdent -> Bool -isValidProofOfWork (ProofOfWork pow rsalt) req (StorableObjectIdent n) = +class POWIdent p where + getPOWIdent :: p -> B.ByteString + +instance POWIdent StorableObjectIdent where + getPOWIdent (StorableObjectIdent i) = i + +data NoPOWIdent = NoPOWIdent + +instance POWIdent NoPOWIdent where + getPOWIdent NoPOWIdent = B.empty + +isValidProofOfWork :: POWIdent p => ProofOfWork -> ProofOfWorkRequirement -> p -> Bool +isValidProofOfWork (ProofOfWork pow rsalt) req p = samesalts && enoughzeros where samesalts = rsalt == randomSalt req @@ -91,7 +102,7 @@ isValidProofOfWork (ProofOfWork pow rsalt) req (StorableObjectIdent n) = tunable = proofOfWorkHashTunable (addedArgon2Iterations req) salt = Salt $ POWSalt $ encodeUtf8 (fromRandomSalt (randomSalt req)) <> pow - ExpensiveHash _ hash = expensiveHash tunable salt n + ExpensiveHash _ hash = expensiveHash tunable salt (getPOWIdent p) -- Since expensiveHash generates an ascii encoded hash that -- includes the parameters, take the sha256 of it to get the -- bytestring that is what's checked for the neccesary number @@ -109,12 +120,12 @@ instance Encodable POWSalt where toByteString (POWSalt n) = n fromByteString = Just . POWSalt -genProofOfWork :: ProofOfWorkRequirement -> StorableObjectIdent -> ProofOfWork -genProofOfWork req i = go allByteStrings +genProofOfWork :: POWIdent p => ProofOfWorkRequirement -> p -> ProofOfWork +genProofOfWork req p = go allByteStrings where go [] = error "failed to generate Proof Of Work. This should be impossible!" go (b:bs) - | isValidProofOfWork candidate req i = candidate + | isValidProofOfWork candidate req p = candidate | otherwise = go bs where candidate = ProofOfWork b (randomSalt req) |