From e333a779338ff8bccdc4225fc953d6f4f0226db0 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 12 Sep 2016 22:59:03 -0400 Subject: add proof of work to countobjects In this case, an empty string is hashed to generate the PoW. --- HTTP/ProofOfWork.hs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'HTTP/ProofOfWork.hs') 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) -- cgit v1.2.3