module Cometa.Crypto.Bip32.PrivateKey
( PrivateKey,
withKey,
withKeyHex,
withEntropy,
withClone,
withDerived,
withSigningKey,
close,
harden,
byteLength,
hexLength,
publicKey,
exportBytes,
exportHex,
)
where
import qualified Cometa.Crypto.Bip32.PublicKey as PublicKey
import Cometa.Crypto.Ed25519.SigningKey (SigningKey)
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Bip32PrivateKey as Internal
import qualified Cometa.Internal.Ed25519SigningKey as Signing
import Cometa.Internal.Error (fromNativeFailure)
import qualified Cometa.Internal.Hex as Hex
import Control.Monad (join)
import Data.Bits ((.|.))
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)
import Data.Word (Word32)
newtype PrivateKey = PrivateKey Internal.Key
withKey :: ByteString -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value)
withKey :: forall value.
ByteString
-> (PrivateKey -> IO (Either Error value))
-> IO (Either Error value)
withKey ByteString
bytes PrivateKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value.
ByteString -> (Key -> IO value) -> IO (Either Failure value)
Internal.withBytesKey ByteString
bytes (PrivateKey -> IO (Either Error value)
action (PrivateKey -> IO (Either Error value))
-> (Key -> PrivateKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> PrivateKey
PrivateKey)
withKeyHex :: Text -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value)
withKeyHex :: forall value.
Text
-> (PrivateKey -> IO (Either Error value))
-> IO (Either Error value)
withKeyHex Text
text PrivateKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value.
Text -> (Key -> IO value) -> IO (Either Failure value)
Internal.withHexKey Text
text (PrivateKey -> IO (Either Error value)
action (PrivateKey -> IO (Either Error value))
-> (Key -> PrivateKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> PrivateKey
PrivateKey)
withEntropy :: ByteString -> ByteString -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value)
withEntropy :: forall value.
ByteString
-> ByteString
-> (PrivateKey -> IO (Either Error value))
-> IO (Either Error value)
withEntropy ByteString
password ByteString
entropy PrivateKey -> IO (Either Error value)
action
| ByteString -> Int
ByteString.length ByteString
entropy Int -> [Int] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Int
16, Int
20, Int
24, Int
28, Int
32] = Either Error value -> IO (Either Error value)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error value
forall a b. a -> Either a b
Left (Int -> Error
InvalidBip39EntropyLength (ByteString -> Int
ByteString.length ByteString
entropy)))
| Bool
otherwise = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> ByteString
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value.
ByteString
-> ByteString -> (Key -> IO value) -> IO (Either Failure value)
Internal.withEntropy ByteString
password ByteString
entropy (PrivateKey -> IO (Either Error value)
action (PrivateKey -> IO (Either Error value))
-> (Key -> PrivateKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> PrivateKey
PrivateKey)
withClone :: PrivateKey -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value)
withClone :: forall value.
PrivateKey
-> (PrivateKey -> IO (Either Error value))
-> IO (Either Error value)
withClone (PrivateKey Key
key) PrivateKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value. Key -> (Key -> IO value) -> IO (Either Failure value)
Internal.withClone Key
key (PrivateKey -> IO (Either Error value)
action (PrivateKey -> IO (Either Error value))
-> (Key -> PrivateKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> PrivateKey
PrivateKey)
withDerived :: PrivateKey -> [Word32] -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value)
withDerived :: forall value.
PrivateKey
-> [Word32]
-> (PrivateKey -> IO (Either Error value))
-> IO (Either Error value)
withDerived (PrivateKey Key
key) [Word32]
path PrivateKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> [Word32]
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value.
Key -> [Word32] -> (Key -> IO value) -> IO (Either Failure value)
Internal.withDerived Key
key [Word32]
path (PrivateKey -> IO (Either Error value)
action (PrivateKey -> IO (Either Error value))
-> (Key -> PrivateKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> PrivateKey
PrivateKey)
withSigningKey :: PrivateKey -> (SigningKey -> IO (Either Error value)) -> IO (Either Error value)
withSigningKey :: forall value.
PrivateKey
-> (SigningKey -> IO (Either Error value))
-> IO (Either Error value)
withSigningKey (PrivateKey Key
key) SigningKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value. Key -> (Key -> IO value) -> IO (Either Failure value)
Internal.withSigningKey Key
key (SigningKey -> IO (Either Error value)
action (SigningKey -> IO (Either Error value))
-> (Key -> SigningKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> SigningKey
Signing.SigningKey)
harden :: Word32 -> Word32
harden :: Word32 -> Word32
harden Word32
index = Word32
index Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. Word32
0x80000000
close :: PrivateKey -> IO ()
close :: PrivateKey -> IO ()
close (PrivateKey Key
key) = Key -> IO ()
Internal.close Key
key
byteLength :: PrivateKey -> IO (Either Error Int)
byteLength :: PrivateKey -> IO (Either Error Int)
byteLength (PrivateKey Key
key) = Either Failure Int -> Either Error Int
forall value. Either Failure value -> Either Error value
mapFailure (Either Failure Int -> Either Error Int)
-> IO (Either Failure Int) -> IO (Either Error Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> IO (Either Failure Int)
Internal.byteLength Key
key
hexLength :: PrivateKey -> IO (Either Error Int)
hexLength :: PrivateKey -> IO (Either Error Int)
hexLength PrivateKey
key = (Int -> Int) -> Either Error Int -> Either Error Int
forall a b. (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) (Either Error Int -> Either Error Int)
-> IO (Either Error Int) -> IO (Either Error Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PrivateKey -> IO (Either Error Int)
byteLength PrivateKey
key
publicKey :: PrivateKey -> IO (Either Error PublicKey.PublicKey)
publicKey :: PrivateKey -> IO (Either Error PublicKey)
publicKey (PrivateKey Key
key) = do
Either Failure ByteString
result <- Key -> IO (Either Failure ByteString)
Internal.publicKeyBytes Key
key
Either Error PublicKey -> IO (Either Error PublicKey)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Failure ByteString -> Either Error ByteString
forall value. Either Failure value -> Either Error value
mapFailure Either Failure ByteString
result Either Error ByteString
-> (ByteString -> Either Error PublicKey) -> Either Error PublicKey
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error PublicKey
PublicKey.fromBytes)
exportBytes :: PrivateKey -> IO (Either Error ByteString)
exportBytes :: PrivateKey -> IO (Either Error ByteString)
exportBytes (PrivateKey Key
key) = Either Failure ByteString -> Either Error ByteString
forall value. Either Failure value -> Either Error value
mapFailure (Either Failure ByteString -> Either Error ByteString)
-> IO (Either Failure ByteString) -> IO (Either Error ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> IO (Either Failure ByteString)
Internal.exportBytes Key
key
exportHex :: PrivateKey -> IO (Either Error Text)
exportHex :: PrivateKey -> IO (Either Error Text)
exportHex PrivateKey
key = (ByteString -> Text)
-> Either Error ByteString -> Either Error Text
forall a b. (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
Hex.encodeHex (Either Error ByteString -> Either Error Text)
-> IO (Either Error ByteString) -> IO (Either Error Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PrivateKey -> IO (Either Error ByteString)
exportBytes PrivateKey
key
flatten :: Either Internal.Failure (Either Error value) -> Either Error value
flatten :: forall value.
Either Failure (Either Error value) -> Either Error value
flatten = Either Error (Either Error value) -> Either Error value
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Either Error (Either Error value) -> Either Error value)
-> (Either Failure (Either Error value)
-> Either Error (Either Error value))
-> Either Failure (Either Error value)
-> Either Error value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either Failure (Either Error value)
-> Either Error (Either Error value)
forall value. Either Failure value -> Either Error value
mapFailure
mapFailure :: Either Internal.Failure value -> Either Error value
mapFailure :: forall value. Either Failure value -> Either Error value
mapFailure = (Failure -> Either Error value)
-> (value -> Either Error value)
-> Either Failure value
-> Either Error value
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Error -> Either Error value
forall a b. a -> Either a b
Left (Error -> Either Error value)
-> (Failure -> Error) -> Failure -> Either Error value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Failure -> Error
publicFailure) value -> Either Error value
forall a b. b -> Either a b
Right
publicFailure :: Internal.Failure -> Error
publicFailure :: Failure -> Error
publicFailure Failure
failure = case Failure
failure of
Failure
Internal.Closed -> Error
Bip32PrivateKeyClosed
Internal.InvalidLength Int
_ Int
actual -> Int -> Error
InvalidBip32PrivateKeyLength Int
actual
Failure
Internal.InvalidHex -> Error
InvalidHex
Internal.NativeFailure NativeFailure
failureValue -> NativeFailure -> Error
fromNativeFailure NativeFailure
failureValue