summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-28 05:29:37 -0400
committerjoe <joe@jerkface.net>2017-07-28 05:29:37 -0400
commit025364434a3a57260e8149df4a532c14332aacfe (patch)
tree7fb85ed52b06f154a8174b0a3608ff04928d3d25
parent8dd4d8caac71bbfdc598ba6d8ff35538cd8a8503 (diff)
Reject invalid sized NodeIds.
-rw-r--r--Mainline.hs11
1 files changed, 10 insertions, 1 deletions
diff --git a/Mainline.hs b/Mainline.hs
index 0f6d7f16..5180352a 100644
--- a/Mainline.hs
+++ b/Mainline.hs
@@ -79,7 +79,16 @@ import qualified Data.Aeson as JSON
79import Text.Read 79import Text.Read
80 80
81newtype NodeId = NodeId ByteString 81newtype NodeId = NodeId ByteString
82 deriving (Eq,Ord,ByteArrayAccess, BEncode, Bits, Hashable) 82 deriving (Eq,Ord,ByteArrayAccess, Bits, Hashable)
83
84instance BEncode NodeId where
85 fromBEncode bval = do
86 bs <- fromBEncode bval
87 if B.length bs /= 20
88 then Left "Invalid length node id."
89 else Right $ NodeId bs
90
91 toBEncode (NodeId bs) = toBEncode bs
83 92
84instance Show NodeId where 93instance Show NodeId where
85 show (NodeId bs) = Char8.unpack $ Base16.encode bs 94 show (NodeId bs) = Char8.unpack $ Base16.encode bs