packages feed

haskoin-bitcoind (empty) → 0.3.0

raw patch · 7 files changed

+308/−0 lines, 7 filesdep +basedep +base16-bytestringdep +bytestringsetup-changed

Dependencies added: base, base16-bytestring, bytestring, cereal, haskoin-core, monad-control, mtl, network-bitcoin, text, transformers, transformers-base

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Bitnomial, Inc.++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Bitnomial, Inc. nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ haskoin-bitcoind.cabal view
@@ -0,0 +1,34 @@+name:                haskoin-bitcoind+version:             0.3.0+synopsis:            An adapter for haskoin to network-bitcoin+-- description:+homepage:            haskoin.com+license:             BSD3+license-file:        LICENSE+author:              Matthew Wraith+maintainer:          wraithm@gmail.com+copyright:           2015 Bitnomial, Inc+category:            Network+build-type:          Simple+cabal-version:       >=1.10++library+  hs-source-dirs:   src+  default-language: Haskell2010++  exposed-modules: Network.Bitcoin.Haskoin+                 , Network.Bitcoin.Haskoin.Trans+                 , Network.Bitcoin.Mining.Haskoin+                 , Network.Bitcoin.Wallet.Haskoin++  build-depends: base                >= 4.8 && < 4.12+               , base16-bytestring   >= 0.1.1.6+               , bytestring          >= 0.10.6+               , cereal              >= 0.5 && < 0.6+               , haskoin-core        >= 0.4.3+               , monad-control       >= 0.1+               , mtl                 >= 2.2+               , network-bitcoin     >= 1.8.2+               , text                >= 1.2.1.3+               , transformers        >= 0.3+               , transformers-base   >= 0.4
+ src/Network/Bitcoin/Haskoin.hs view
@@ -0,0 +1,91 @@+module Network.Bitcoin.Haskoin+    ( transactionOutputAddress+    , transactionInputAddress+    , getTransaction+    , getTransactionOutput+    , outpointAddress+    , importAddress++    , -- * Utility functions+      addressToHex+    , decodeHexTx+    , hexTxHash+    , hexToAddress+    , transactionIdToTxHash++    , -- * network-bitcoin reexports+      getClient+    , Client++    , -- * haskoin Rexports+      outputAddress+    , inputAddress+    ) where++import           Control.Monad               ((<=<))+import qualified Data.ByteString.Base16      as B16+import           Data.Maybe                  (fromMaybe)+import           Data.Serialize              (decode)+import           Data.Text.Encoding          as E++import           Network.Bitcoin             (Client, RawTransaction,+                                              TransactionID, getClient,+                                              getRawTransaction)+import qualified Network.Bitcoin             as B+import           Network.Haskoin.Crypto      (Address, addrToBase58,+                                              base58ToAddr)+import           Network.Haskoin.Script      (decodeInputBS, decodeOutputBS,+                                              inputAddress, outputAddress)+import           Network.Haskoin.Transaction (OutPoint (..), Tx (..), TxHash,+                                              TxIn, TxOut, hexToTxHash,+                                              scriptInput, scriptOutput,+                                              txHashToHex)+++transactionOutputAddress :: TxOut -> Either String Address+transactionOutputAddress = outputAddress <=< decodeOutputBS . scriptOutput+++transactionInputAddress :: TxIn -> Either String Address+transactionInputAddress = inputAddress <=< decodeInputBS . scriptInput+++addressToHex :: Address -> B.Address+addressToHex = decodeUtf8 . addrToBase58+++hexToAddress :: B.Address -> Address+hexToAddress = fromMaybe (error "Unable to parse address") . base58ToAddr . encodeUtf8+++-- | TODO Catch bad decodes+decodeHexTx :: RawTransaction -> Tx+decodeHexTx = fromRight . decode . fst . B16.decode . E.encodeUtf8+  where+    fromRight (Right x) = x+    fromRight (Left e)  = error e+++hexTxHash :: TxHash -> TransactionID+hexTxHash = E.decodeUtf8 . txHashToHex+++transactionIdToTxHash :: TransactionID -> TxHash+transactionIdToTxHash = fromMaybe (error "Unable to parse txid") . hexToTxHash . encodeUtf8+++-- | TODO Catch errors from bitcoind+getTransaction :: Client -> TxHash -> IO Tx+getTransaction c hash = decodeHexTx <$> getRawTransaction c (hexTxHash hash)+++getTransactionOutput :: Client -> OutPoint -> IO TxOut+getTransactionOutput cl (OutPoint hash i) = (!! fromIntegral i) . txOut <$> getTransaction cl hash+++outpointAddress :: Client -> OutPoint -> IO (Either String Address)+outpointAddress c op = transactionOutputAddress <$> getTransactionOutput c op+++importAddress :: Client -> Address -> Maybe B.Account -> Maybe Bool -> IO ()+importAddress client addr = B.importAddress client (E.decodeUtf8 $ addrToBase58 addr)
+ src/Network/Bitcoin/Haskoin/Trans.hs view
@@ -0,0 +1,90 @@+{-# LANGUAGE FlexibleContexts           #-}+{-# LANGUAGE FlexibleInstances          #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE MultiParamTypeClasses      #-}+{-# LANGUAGE OverloadedStrings          #-}+{-# LANGUAGE UndecidableInstances       #-}++module Network.Bitcoin.Haskoin.Trans+    ( BitcoinT (..)+    , runBitcoinT++    , -- * Bitcoind operations+      getTransaction+    , getTransactionOutput+    , outpointAddress+    , importAddress++    , -- * Reexports+      Tx (..)+    , TxHash+    , Address+    , TxOut (..)+    , TxIn (..)+    , MonadBase+    , liftBase+    , MonadIO+    , liftIO++    , -- * Utilities+      withClient+    , withClientIO+    ) where++import           Control.Monad.Base          (MonadBase, liftBase)+import           Control.Monad.IO.Class+import           Control.Monad.Reader        (MonadReader, ReaderT (..), ask)+import           Control.Monad.Trans         (MonadTrans (..))++import           Network.Bitcoin             (Account)+import           Network.Bitcoin.Haskoin     (Client)+import qualified Network.Bitcoin.Haskoin     as B+import           Network.Haskoin.Crypto      (Address)+import           Network.Haskoin.Transaction (OutPoint, Tx (..), TxHash,+                                              TxIn (..), TxOut (..))+++newtype BitcoinT m a = BitcoinT { unBitcoinT :: ReaderT Client m a }+    deriving (Functor, Applicative, Monad, MonadTrans, MonadIO, MonadReader Client)+++instance MonadBase b m => MonadBase b (BitcoinT m) where+    liftBase = lift . liftBase+++runBitcoinT :: Client -> BitcoinT m a -> m a+runBitcoinT c = flip runReaderT c . unBitcoinT+-- TODO Handle exceptions+++withClient :: Monad m => (Client -> m a) -> BitcoinT m a+withClient f = do+    cl <- ask+    lift (f cl)+++withClientIO :: MonadIO m => (Client -> IO a) -> BitcoinT m a+withClientIO f = do+    cl <- ask+    liftIO (f cl)+++getTransaction :: MonadIO m => TxHash -> BitcoinT m Tx+getTransaction hash = withClientIO (`B.getTransaction` hash)+++getTransactionOutput :: MonadIO m => OutPoint -> BitcoinT m TxOut+getTransactionOutput op = withClientIO (`B.getTransactionOutput` op)+++outpointAddress :: MonadIO m => OutPoint -> BitcoinT m (Either String Address)+outpointAddress op = withClientIO (`B.outpointAddress` op)+++importAddress' :: MonadIO m => Address -> Maybe Account -> Maybe Bool -> BitcoinT m ()+importAddress' addr macct mrescan =+    withClientIO (\cl -> B.importAddress cl addr macct mrescan)+++importAddress :: MonadIO m => Address -> BitcoinT m ()+importAddress addr = importAddress' addr (Just "") (Just False)
+ src/Network/Bitcoin/Mining/Haskoin.hs view
@@ -0,0 +1,27 @@+module Network.Bitcoin.Mining.Haskoin+  ( generate+  ) where++import           Data.Fixed                  (Fixed (MkFixed))+import           Data.Maybe                  (fromMaybe, maybe)+import           Data.Text                   (Text)+import           Data.Text.Encoding          (decodeUtf8, encodeUtf8)+import           Data.Word                   (Word64)+import           Network.Bitcoin.Haskoin     (addressToHex,+                                              transactionIdToTxHash)+import qualified Network.Bitcoin.Mining      as NBM+import qualified Network.Bitcoin.Types       as NBT+import           Network.Bitcoin.Wallet      (Client)+import qualified Network.Bitcoin.Wallet      as W+import           Network.Haskoin.Crypto      (addrToBase58, base58ToAddr)+import qualified Network.Haskoin.Crypto      as HSK+import           Network.Haskoin.Transaction (TxHash, hexToTxHash)+++generate :: Client -> Int -> Maybe Int -> IO [TxHash]+generate client blocks maxTries = fmap transactionIdToTxHash <$> NBM.generate client blocks maxTries+++generateToAddress :: Client -> Int -> HSK.Address -> Maybe Int -> IO [TxHash]+generateToAddress client blocks toAddr maxTries =+    fmap transactionIdToTxHash <$> NBM.generateToAddress client blocks (addressToHex toAddr) maxTries
+ src/Network/Bitcoin/Wallet/Haskoin.hs view
@@ -0,0 +1,34 @@+module Network.Bitcoin.Wallet.Haskoin+    ( sendToAddress+    , getNewAddress+    ) where++import           Data.Fixed                  (Fixed (MkFixed))+import           Data.Maybe                  (fromMaybe, maybe)+import           Data.Text                   (Text)+import           Data.Text.Encoding          (decodeUtf8, encodeUtf8)+import           Data.Word                   (Word64)+import           Network.Bitcoin.Haskoin     (addressToHex, hexToAddress,+                                              transactionIdToTxHash)+import qualified Network.Bitcoin.Types       as NBT+import           Network.Bitcoin.Wallet      (Client)+import qualified Network.Bitcoin.Wallet      as W+import           Network.Haskoin.Crypto      (addrToBase58, base58ToAddr)+import qualified Network.Haskoin.Crypto      as HSK+import           Network.Haskoin.Transaction (TxHash, hexToTxHash)+++type Satoshi = Word64+++sendToAddress :: Client -> HSK.Address -> Satoshi -> Maybe Text -> Maybe Text -> IO TxHash+sendToAddress client addr sat cmt cmtTo =+    transactionIdToTxHash <$> W.sendToAddress client (addressToHex addr) (satToBTC sat) cmt cmtTo+++getNewAddress :: Client -> IO HSK.Address+getNewAddress client = hexToAddress <$> W.getNewAddress client Nothing+++satToBTC :: Satoshi -> NBT.BTC+satToBTC = MkFixed . fromIntegral