{-# LANGUAGE OverloadedStrings #-}
module Haskoin.Crypto.Signature (
putSig,
getSig,
signHash,
verifyHashSig,
isCanonicalHalfOrder,
decodeStrictSig,
exportSig,
) where
import Control.Monad (guard, unless, when)
import Crypto.Secp256k1
import Data.Binary (Binary (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Bytes.Get
import Data.Bytes.Put
import Data.Bytes.Serial
import Data.Maybe (fromMaybe, isNothing)
import Data.Serialize (Serialize (..))
import Haskoin.Crypto.Hash
import Numeric (showHex)
hashToMsg :: Hash256 -> Msg
hashToMsg :: Hash256 -> Msg
hashToMsg =
forall a. a -> Maybe a -> a
fromMaybe forall {a}. a
e forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe Msg
msg forall b c a. (b -> c) -> (a -> b) -> a -> c
. Put -> ByteString
runPutS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (m :: * -> *). (Serial a, MonadPut m) => a -> m ()
serialize
where
e :: a
e = forall a. HasCallStack => [Char] -> a
error [Char]
"Could not convert 32-byte hash to secp256k1 message"
signHash :: SecKey -> Hash256 -> Sig
signHash :: SecKey -> Hash256 -> Sig
signHash SecKey
k = SecKey -> Msg -> Sig
signMsg SecKey
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash256 -> Msg
hashToMsg
verifyHashSig :: Hash256 -> Sig -> PubKey -> Bool
verifyHashSig :: Hash256 -> Sig -> PubKey -> Bool
verifyHashSig Hash256
h Sig
s PubKey
p = PubKey -> Sig -> Msg -> Bool
verifySig PubKey
p Sig
norm (Hash256 -> Msg
hashToMsg Hash256
h)
where
norm :: Sig
norm = forall a. a -> Maybe a -> a
fromMaybe Sig
s (Sig -> Maybe Sig
normalizeSig Sig
s)
getSig :: MonadGet m => m Sig
getSig :: forall (m :: * -> *). MonadGet m => m Sig
getSig = do
Int
l <-
forall (m :: * -> *) a. MonadGet m => m a -> m a
lookAhead forall a b. (a -> b) -> a -> b
$ do
Word8
t <- forall (m :: * -> *). MonadGet m => m Word8
getWord8
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word8
t forall a. Eq a => a -> a -> Bool
== Word8
0x30) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall a b. (a -> b) -> a -> b
$
[Char]
"Bad DER identifier byte 0x" forall a. [a] -> [a] -> [a]
++ forall a. (Integral a, Show a) => a -> ShowS
showHex Word8
t [Char]
". Expecting 0x30"
Word8
l <- forall (m :: * -> *). MonadGet m => m Word8
getWord8
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word8
l forall a. Eq a => a -> a -> Bool
== Word8
0x00) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"Indeterminate form unsupported"
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word8
l forall a. Ord a => a -> a -> Bool
>= Word8
0x80) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"Multi-octect length not supported"
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
l
ByteString
bs <- forall (m :: * -> *). MonadGet m => Int -> m ByteString
getByteString forall a b. (a -> b) -> a -> b
$ Int
l forall a. Num a => a -> a -> a
+ Int
2
case ByteString -> Maybe Sig
decodeStrictSig ByteString
bs of
Just Sig
s -> forall (m :: * -> *) a. Monad m => a -> m a
return Sig
s
Maybe Sig
Nothing -> forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"Invalid signature"
putSig :: MonadPut m => Sig -> m ()
putSig :: forall (m :: * -> *). MonadPut m => Sig -> m ()
putSig Sig
s = forall (m :: * -> *). MonadPut m => ByteString -> m ()
putByteString forall a b. (a -> b) -> a -> b
$ Sig -> ByteString
exportSig Sig
s
isCanonicalHalfOrder :: Sig -> Bool
isCanonicalHalfOrder :: Sig -> Bool
isCanonicalHalfOrder = forall a. Maybe a -> Bool
isNothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sig -> Maybe Sig
normalizeSig
decodeStrictSig :: ByteString -> Maybe Sig
decodeStrictSig :: ByteString -> Maybe Sig
decodeStrictSig ByteString
bs = do
Sig
g <- ByteString -> Maybe Sig
importSig ByteString
bs
let compact :: CompactSig
compact = Sig -> CompactSig
exportCompactSig Sig
g
let zero :: ByteString
zero = Int -> Word8 -> ByteString
BS.replicate Int
32 Word8
0
forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
BS.take Int
32 (CompactSig -> ByteString
getCompactSig CompactSig
compact) forall a. Eq a => a -> a -> Bool
/= ByteString
zero
forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
BS.take Int
32 (Int -> ByteString -> ByteString
BS.drop Int
32 (CompactSig -> ByteString
getCompactSig CompactSig
compact)) forall a. Eq a => a -> a -> Bool
/= ByteString
zero
forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall a b. (a -> b) -> a -> b
$ Sig -> Bool
isCanonicalHalfOrder Sig
g
forall (m :: * -> *) a. Monad m => a -> m a
return Sig
g