-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Generic blockchain implementation. -- -- Please see README.md @package blockchain @version 0.0.3 module Data.Blockchain.Crypto -- | KeyPairs are used to store and sign transactions. The PublicKey -- piece is used as the public address on the blockchain where funds can -- be sent to and from. The PrivateKey is used to sign -- transactions, which provides a guarantee that the transaction was -- initiated by the owner of the address. data KeyPair KeyPair :: PublicKey -> PrivateKey -> KeyPair [publicKey] :: KeyPair -> PublicKey [privateKey] :: KeyPair -> PrivateKey -- | Generates a new KeyPair. The PublicKey can be shared -- openly, while the PrivateKey should be kept secret. generate :: IO KeyPair newtype Signature Signature :: Signature -> Signature [unSignature] :: Signature -> Signature newtype PublicKey PublicKey :: PublicPoint -> PublicKey [unPublicKey] :: PublicKey -> PublicPoint newtype PrivateKey PrivateKey :: PrivateNumber -> PrivateKey [unPrivateKey] :: PrivateKey -> PrivateNumber sign :: PrivateKey -> ByteString -> IO Signature verify :: PublicKey -> Signature -> ByteString -> Bool data Hash a class ToHash a where hash = Hash . hash . toStrict . encode hash :: ToHash a => a -> Hash a hash :: (ToHash a, ToJSON a) => a -> Hash a hashToHex :: Hash a -> Hex256 fromByteString :: ByteString -> Maybe (Hash a) unsafeFromByteString :: ByteString -> Hash a data HashTreeRoot a unHashTreeRoot :: HashTreeRoot a -> Hash a hashTreeRoot :: forall a. ToHash a => [a] -> HashTreeRoot a module Data.Blockchain.Types data Validated data Unvalidated -- | Core blockchain data type. Uses a validation tag to declare if it is -- known to abide by expected blockchain rules. Will be either -- Blockchain Validated or Blockchain -- Unvalidated. -- -- Note: both Blockchain Validated and -- Blockchain Unvalidated can be serialized to -- json, while only Blockchain Unvalidated can be -- deserialized from json. data Blockchain a blockchainConfig :: Blockchain a -> BlockchainConfig blockchainNode :: Blockchain a -> BlockchainNode data BlockchainNode BlockchainNode :: Block -> [BlockchainNode] -> BlockchainNode [nodeBlock] :: BlockchainNode -> Block [nodeNodes] :: BlockchainNode -> [BlockchainNode] data ValidationException GenesisBlockHasTransactions :: ValidationException GenesisBlockException :: BlockException -> ValidationException BlockValidationException :: BlockException -> ValidationException data BlockException BlockAlreadyExists :: BlockException NoParentFound :: BlockException TimestampTooOld :: BlockException TimestampTooFarIntoFuture :: BlockException InvalidDifficultyReference :: BlockException InvalidDifficulty :: BlockException InvalidCoinbaseTransactionHash :: BlockException InvalidTransactionHashTreeRoot :: BlockException InvalidCoinbaseTransactionValue :: BlockException InvalidTransactionValues :: BlockException TransactionOutRefNotFound :: BlockException InvalidTransactionSignature :: BlockException -- | Constructs an unvalidated blockchain from a config and a node. Allows -- arbitrary blockchains to be constructed. However, blockchains are -- generally not useful until validated. construct :: BlockchainConfig -> BlockchainNode -> Blockchain Unvalidated -- | Validates a blockchain. Returns a ValidationException if -- provided blockchain does not meet expected rules. validate :: Blockchain Unvalidated -> Either ValidationException (Blockchain Validated) -- | Isomorphic - useful for sending api responses unvalidate :: Blockchain Validated -> Blockchain Unvalidated -- | Adds a block to a validated blockchain. Returns a -- BlockException if block is not able to be inserted into the -- blockchain. addBlock :: Block -> Blockchain Validated -> Either BlockException (Blockchain Validated) validateTransaction :: Blockchain Validated -> Transaction -> Either BlockException () validateTransactions :: Blockchain Validated -> [Transaction] -> Either BlockException () blockHeaderHashDifficulty :: Hex256 -> BlockHeader -> Difficulty addressValues :: Blockchain Validated -> HashMap PublicKey Word unspentTransactionOutputs :: Blockchain Validated -> HashMap PublicKey [(TransactionOutRef, TransactionOut)] longestChain :: Blockchain Validated -> NonEmpty Block flatten :: Blockchain Validated -> NonEmpty (NonEmpty Block) data Block Block :: BlockHeader -> CoinbaseTransaction -> [Transaction] -> Block [blockHeader] :: Block -> BlockHeader [coinbaseTransaction] :: Block -> CoinbaseTransaction [transactions] :: Block -> [Transaction] data BlockHeader BlockHeader :: Int -> Hash BlockHeader -> Hash CoinbaseTransaction -> HashTreeRoot Transaction -> UTCTime -> Difficulty -> Int -> BlockHeader [version] :: BlockHeader -> Int [prevBlockHeaderHash] :: BlockHeader -> Hash BlockHeader [coinbaseTransactionHash] :: BlockHeader -> Hash CoinbaseTransaction [transactionHashTreeRoot] :: BlockHeader -> HashTreeRoot Transaction [time] :: BlockHeader -> UTCTime [difficulty] :: BlockHeader -> Difficulty [nonce] :: BlockHeader -> Int data BlockchainConfig BlockchainConfig :: Difficulty -> Hex256 -> Word -> Word -> Word -> Word -> BlockchainConfig [initialDifficulty] :: BlockchainConfig -> Difficulty [difficulty1Target] :: BlockchainConfig -> Hex256 [targetSecondsPerBlock] :: BlockchainConfig -> Word [difficultyRecalculationHeight] :: BlockchainConfig -> Word [initialMiningReward] :: BlockchainConfig -> Word [miningRewardHalvingHeight] :: BlockchainConfig -> Word -- | A reasonable default config to use for testing. Mines blocks quickly -- and changes difficulty and rewards frequently. Note: reward will go to -- zero after 1100 blocks, which will take about 180 minutes of mining. -- --
--   defaultConfig :: BlockchainConfig
--   defaultConfig = BlockchainConfig
--       { initialDifficulty             = Difficulty 1
--       , difficulty1Target             = hex256LeadingZeros 4
--       , targetSecondsPerBlock         = 10
--       , difficultyRecalculationHeight = 50
--       , initialMiningReward           = 1024
--       , miningRewardHalvingHeight     = 100
--       }
--   
defaultConfig :: BlockchainConfig -- | Calculates the target reward for a blockchain. Uses the longest chain. targetReward :: BlockchainConfig -> Word -> Word -- | Calculates the target difficulty for a blockchain. Uses the longest -- chain. targetDifficulty :: BlockchainConfig -> [Block] -> Difficulty newtype Difficulty Difficulty :: Natural -> Difficulty [unDifficulty] :: Difficulty -> Natural newtype Hex256 Hex256 :: Natural -> Hex256 [unHex256] :: Hex256 -> Natural hex256 :: String -> Maybe Hex256 -- | Create a Hex256 value with the specificed amount of leading zeros. -- Useful for creating a difficulty1Target when creating a -- blockchain. -- --
--   >>> hex256LeadingZeros 4
--   0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
--   
hex256LeadingZeros :: Word -> Hex256 data Transaction Transaction :: NonEmpty TransactionIn -> NonEmpty TransactionOut -> Transaction [transactionIn] :: Transaction -> NonEmpty TransactionIn [transactionOut] :: Transaction -> NonEmpty TransactionOut newtype CoinbaseTransaction CoinbaseTransaction :: NonEmpty TransactionOut -> CoinbaseTransaction [coinbaseTransactionOut] :: CoinbaseTransaction -> NonEmpty TransactionOut data TransactionIn TransactionIn :: TransactionOutRef -> Signature -> TransactionIn [transactionOutRef] :: TransactionIn -> TransactionOutRef -- | Signature from prev transaction, using pubkey from prev transaction [signature] :: TransactionIn -> Signature -- | Pointer to a specific TransactionOut data TransactionOutRef TransactionOutRef :: Either (Hash CoinbaseTransaction) (Hash Transaction) -> Word -> TransactionOutRef [transactionHash] :: TransactionOutRef -> Either (Hash CoinbaseTransaction) (Hash Transaction) [transactionOutIndex] :: TransactionOutRef -> Word data TransactionOut TransactionOut :: Word -> PublicKey -> TransactionOut [value] :: TransactionOut -> Word -- | Address of where funds go [signaturePubKey] :: TransactionOut -> PublicKey signTransaction :: PrivateKey -> TransactionOut -> IO Signature verifyTransactionSignature :: Signature -> TransactionOut -> Bool module Data.Blockchain.Builder data CreateTransactionException SourceAddressEmpty :: CreateTransactionException SourceAddressInsufficientFunds :: CreateTransactionException InvalidPrivateKey :: CreateTransactionException -- | Create a transaction from a single public key address. createSimpleTransaction :: KeyPair -> PublicKey -> Word -> Word -> Blockchain Validated -> IO (Either CreateTransactionException Transaction) instance GHC.Show.Show Data.Blockchain.Builder.CreateTransactionException instance GHC.Classes.Eq Data.Blockchain.Builder.CreateTransactionException module Data.Blockchain module Data.Blockchain.Mining data MineBlockException InvalidTransactionList :: MineBlockException -- | Finds the next block of a blockchain. Depending on blockchain -- configuration, this function may take a long time to complete. mineBlock :: PublicKey -> [Transaction] -> Blockchain Validated -> IO (Either MineBlockException Block) -- | Finds the next block of a blockchain, without including any -- transactions. Most useful for testing - removes the invariant of an -- invalid transaction list. Depending on blockchain configuration, this -- function may take a long time to complete. mineEmptyBlock :: PublicKey -> Blockchain Validated -> IO (Either MineBlockException Block) -- | Finds the first block of a blockchain. Depending on blockchain -- configuration, this function may take a long time to complete. Note: -- this generates a keypair but throws away the private key. Coinbase -- reward in genesis block cannot never be spent. mineGenesisBlock :: BlockchainConfig -> IO Block -- | Creates a blockchain from the given config. This includes mining a -- genesis block. mineBlockchain :: BlockchainConfig -> IO (Blockchain Validated)