module Crypto.Ecdsa.Utils where
import Crypto.Number.Serialize (i2osp, os2ip)
import Crypto.PubKey.ECC.ECDSA (PrivateKey (..), PublicKey (..))
import Crypto.PubKey.ECC.Generate (generateQ)
import Crypto.PubKey.ECC.Types (CurveName (SEC_p256k1), Point (..),
getCurveByName)
import Data.ByteArray (ByteArray, ByteArrayAccess)
import Data.Monoid ((<>))
importKey :: ByteArrayAccess privateKey => privateKey -> PrivateKey
{-# INLINE importKey #-}
importKey = PrivateKey (getCurveByName SEC_p256k1) . os2ip
exportKey :: ByteArray privateKey => PrivateKey -> privateKey
{-# INLINE exportKey #-}
exportKey (PrivateKey _ key) = i2osp key
derivePubKey :: PrivateKey -> PublicKey
{-# INLINE derivePubKey #-}
derivePubKey (PrivateKey curve p) = PublicKey curve (generateQ curve p)
exportPubKey :: ByteArray publicKey => PublicKey -> publicKey
{-# INLINE exportPubKey #-}
exportPubKey (PublicKey _ (Point x y)) = i2osp x <> i2osp y
exportPubKey (PublicKey _ PointO) = mempty