@@ -35,7 +35,8 @@ module Crypto.Secp256k1 (
35
35
exportPubKeyXY ,
36
36
importPubKeyXO ,
37
37
exportPubKeyXO ,
38
- importSignature ,
38
+ importSignatureCompact ,
39
+ importSignatureDer ,
39
40
exportSignatureCompact ,
40
41
exportSignatureDer ,
41
42
importRecoverableSignature ,
@@ -80,6 +81,7 @@ module Crypto.Secp256k1 (
80
81
ecdh ,
81
82
) where
82
83
84
+ import Control.Applicative (Alternative (.. ))
83
85
import Control.DeepSeq (NFData (.. ))
84
86
import Control.Monad (replicateM , unless , (<=<) )
85
87
import Control.Monad.Trans.Class (lift )
@@ -269,7 +271,7 @@ instance Show Signature where
269
271
instance Read Signature where
270
272
readsPrec i cs = case decodeBase16 $ B8. pack token of
271
273
Left e -> []
272
- Right a -> maybeToList $ (,rest) <$> importSignature a
274
+ Right a -> maybeToList $ (,rest) <$> (importSignatureCompact a <|> importSignatureDer a)
273
275
where
274
276
trimmed = dropWhile isSpace cs
275
277
(token, rest) = span isAlphaNum trimmed
@@ -414,24 +416,33 @@ exportPubKeyXO (PubKeyXO pkFPtr) = unsafePerformIO $ do
414
416
unsafePackByteString (outBuf, 32 )
415
417
416
418
417
- -- | Parses 'Signature' from DER (71 | 72 | 73 bytes) or Compact (64 bytes) representations .
418
- importSignature :: ByteString -> Maybe Signature
419
- importSignature bs = unsafePerformIO $
419
+ -- | Parses 'Signature' from Compact (64 bytes) representation .
420
+ importSignatureCompact :: ByteString -> Maybe Signature
421
+ importSignatureCompact bs = unsafePerformIO $
420
422
unsafeUseByteString bs $ \ (inBuf, len) -> do
421
423
outBuf <- mallocBytes 64
422
424
ret <-
423
- if
425
+ if len == 64
424
426
-- compact
425
- | len == 64 -> Prim. ecdsaSignatureParseCompact ctx outBuf inBuf
426
- -- der
427
- | len >= 69 && len <= 73 -> Prim. ecdsaSignatureParseDer ctx outBuf inBuf len
427
+ then Prim. ecdsaSignatureParseCompact ctx outBuf inBuf
428
428
-- invalid
429
- | otherwise -> pure 0
429
+ else pure 0
430
430
if isSuccess ret
431
431
then Just . Signature <$> newForeignPtr finalizerFree outBuf
432
432
else free outBuf $> Nothing
433
433
434
434
435
+ -- | Parses 'Signature' from DER representation.
436
+ importSignatureDer :: ByteString -> Maybe Signature
437
+ importSignatureDer bs = unsafePerformIO $
438
+ unsafeUseByteString bs $ \ (inBuf, len) -> do
439
+ outBuf <- mallocBytes 64
440
+ ret <- Prim. ecdsaSignatureParseDer ctx outBuf inBuf len
441
+ if isSuccess ret
442
+ then Just . Signature <$> newForeignPtr finalizerFree outBuf
443
+ else pure Nothing
444
+
445
+
435
446
-- | Serializes 'Signature' to Compact (64 byte) representation
436
447
exportSignatureCompact :: Signature -> ByteString
437
448
exportSignatureCompact (Signature fptr) = unsafePerformIO $ do
0 commit comments