cometa-0.1.0.0: Idiomatic Haskell bindings to cardano-c
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cometa.Crypto.Bip32.PrivateKey

Description

Scoped Cardano Ed25519-BIP32 private keys. A key contains a 64-byte expanded Ed25519 secret and a 32-byte chain code. It has no Show or equality instance. Handles reject operations after scope exit or explicit close; escaped handles do not extend usable lifetime. Exceptions and cancellation propagate after cleanup. Closing cannot erase application inputs or explicitly exported copies.

import qualified Cometa.Crypto.Bip32.PrivateKey as PrivateKey

-- bytes must contain a 96-byte application key.
derivePublic bytes = PrivateKey.withKey bytes $ root ->
  PrivateKey.withDerived root [PrivateKey.harden 1852, PrivateKey.harden 1815,
    PrivateKey.harden 0, 0, 0] PrivateKey.publicKey
Synopsis

Documentation

data PrivateKey Source #

Opaque scoped 96-byte secret. Independent derived keys and clones may outlive the source's usable lifetime only within their own active scopes.

Scoped acquisition

withKey :: ByteString -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value) Source #

Import exactly 96 bytes and run the continuation. Only encoded length is checked; invalid scalars may fail later operations. Wrong lengths return InvalidBip32PrivateKeyLength without invoking the continuation. Acquisition and callback failures share one Either; callback exceptions propagate.

withKeyHex :: Text -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value) Source #

Import exactly 192 ASCII hexadecimal digits, accepting either case without whitespace or prefix. Empty/malformed text returns InvalidHex; valid hex of another length returns InvalidBip32PrivateKeyLength. Input text remains in application memory after closure. Cleanup matches withKey.

withEntropy :: ByteString -> ByteString -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value) Source #

Derive a root from raw password bytes and BIP39 entropy bytes, respectively. Entropy must contain 16, 20, 24, 28 or 32 bytes; other lengths return InvalidBip39EntropyLength. Password may be empty and may contain NUL bytes. This uses Cardano's PBKDF2-HMAC-SHA512 with 4096 iterations and 96 output bytes, followed by scalar clamping. No text normalization is performed. This accepts entropy, not a mnemonic sentence or a 64-byte BIP39 seed. Inputs remain in application memory; failures and cleanup match withKey.

withClone :: PrivateKey -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value) Source #

Run the continuation with an independent copy. Closing either key does not affect the other; the copy closes on every exit. Closed sources return Bip32PrivateKeyClosed.

withDerived :: PrivateKey -> [Word32] -> (PrivateKey -> IO (Either Error value)) -> IO (Either Error value) Source #

Derive indices in order into an independent scope. All Word32 values are accepted: 0..2^31-1 are soft and 2^31..2^32-1 are hardened. Use harden to set the high bit. An empty path makes an independent clone. The source remains unchanged. Closed sources return Bip32PrivateKeyClosed; operation failures return NativeError. The derived key closes before callback exceptions propagate.

withSigningKey :: PrivateKey -> (SigningKey -> IO (Either Error value)) -> IO (Either Error value) Source #

Run the continuation with an independent extended Ed25519 signing key, discarding the chain code. Closing the source does not close the signing key. The signing key closes on every exit and rejects escaped uses. Closed sources return Bip32PrivateKeyClosed; failures return NativeError.

close :: PrivateKey -> IO () Source #

Close this handle, waiting for active operations. Repeated closure is harmless. Scope exit also closes it; no later operation can use this handle.

Derivation indices

harden :: Word32 -> Word32 Source #

Set the high bit of an encoded index. This is idempotent: an already hardened index is unchanged. For example, harden 1852 == 2147485500.

Inspection

byteLength :: PrivateKey -> IO (Either Error Int) Source #

Secret byte count, always 96. Closed handles return Bip32PrivateKeyClosed.

hexLength :: PrivateKey -> IO (Either Error Int) Source #

Hexadecimal digit count, always 192, excluding a terminator. Closed handles return Bip32PrivateKeyClosed.

publicKey :: PrivateKey -> IO (Either Error PublicKey) Source #

Derive an immutable 64-byte public key and chain code, usable after this secret closes. A closed handle returns Bip32PrivateKeyClosed; unusable scalar or other operation failures return NativeError without secret diagnostics.

Explicit secret export

exportBytes :: PrivateKey -> IO (Either Error ByteString) Source #

Explicitly copy all 96 secret bytes into a Haskell ByteString. The result can outlive this scope; closing cannot erase it or control subsequent application copies. A closed handle returns Bip32PrivateKeyClosed.

exportHex :: PrivateKey -> IO (Either Error Text) Source #

Explicitly export 192 lowercase ASCII hex digits without prefix. The text has application-managed lifetime and is not erased on closure. A closed handle returns Bip32PrivateKeyClosed.