summaryrefslogtreecommitdiffhomepage
path: root/HTTP/ProofOfWork.hs
diff options
context:
space:
mode:
Diffstat (limited to 'HTTP/ProofOfWork.hs')
-rw-r--r--HTTP/ProofOfWork.hs23
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)