bitcoin-tx 0.12.0 → 0.12.1
raw patch · 3 files changed
+77/−37 lines, 3 filesdep +bitcoin-typesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies added: bitcoin-types
API changes (from Hackage documentation)
+ Data.Bitcoin.Transaction: data Coinbase
- Data.Bitcoin.Transaction: transactionId :: Transaction -> HexString
+ Data.Bitcoin.Transaction: transactionId :: Transaction -> TransactionId
Files
- bitcoin-tx.cabal +2/−1
- src/Data/Bitcoin/Transaction.hs +6/−3
- src/Data/Bitcoin/Transaction/Types.hs +69/−33
bitcoin-tx.cabal view
@@ -1,6 +1,6 @@ name: bitcoin-tx category: Network, Finance -version: 0.12.0 +version: 0.12.1 license: MIT license-file: LICENSE copyright: (c) 2015 Leon Mergen @@ -36,6 +36,7 @@ , cryptohash + , bitcoin-types , bitcoin-script test-suite test-suite
src/Data/Bitcoin/Transaction.hs view
@@ -3,7 +3,8 @@ , transactionId , Transaction (..) , TransactionIn (..) - , TransactionOut (..)) where + , TransactionOut (..) + , Coinbase ) where import qualified Data.Binary as B (encode) @@ -13,6 +14,8 @@ import qualified Crypto.Hash.SHA256 as Sha256 import qualified Data.HexString as HS +import Data.Bitcoin.Types ( TransactionId ) + import Data.Bitcoin.Transaction.Types -- | Decodes a hex representation of a transaction into a 'Transaction' object. @@ -25,9 +28,9 @@ -> HS.HexString -- ^ The hexadecimal representation of the transaction encode = HS.fromBinary --- | Calculates the transaction id of a 'Transaction' as a 'HS.HexString' so it +-- | Calculates the transaction id of a 'Transaction' as a 'TransactionId' so it -- can be used in RPC interfaces. -transactionId :: Transaction -> HS.HexString +transactionId :: Transaction -> TransactionId transactionId = -- Bitcoin uses a "double sha256", also known as sha256d as its hash algo let sha256d = Sha256.hash . Sha256.hash
src/Data/Bitcoin/Transaction/Types.hs view
@@ -2,8 +2,12 @@ module Data.Bitcoin.Transaction.Types where -import Control.Applicative ((<$>),(<*>)) -import Control.Monad (liftM2, replicateM, forM_) +import Control.Applicative ( (<$>) + , (<*>) ) +import Control.Monad ( liftM2 + , replicateM + , forM_ + , unless ) import Data.Word ( Word32 , Word64 ) @@ -15,48 +19,18 @@ import Data.Binary ( Binary, get, put, encode, decode ) import Data.Binary.Get ( getByteString - , getWord8 - , getWord16le , getWord32le , getWord64le , getWord64be ) import Data.Binary.Put ( putByteString - , putWord8 - , putWord16le , putWord32le , putWord64le , putWord64be ) +import Data.Bitcoin.Types ( VarInt (..) ) import qualified Data.Bitcoin.Script as Btc ( Script (..) ) --- | Data type representing a variable length integer. The 'VarInt' type --- usually precedes an array or a string that can vary in length. -newtype VarInt = VarInt { getVarInt :: Word64 } - deriving (Eq, Show, Read) - -instance Binary VarInt where - - get = VarInt <$> ( getWord8 >>= go ) - where - go 0xff = getWord64le - go 0xfe = fromIntegral <$> getWord32le - go 0xfd = fromIntegral <$> getWord16le - go x = fromIntegral <$> return x - - put (VarInt x) - | x < 0xfd = - putWord8 $ fromIntegral x - | x <= 0xffff = do - putWord8 0xfd - putWord16le $ fromIntegral x - | x <= 0xffffffff = do - putWord8 0xfe - putWord32le $ fromIntegral x - | otherwise = do - putWord8 0xff - putWord64le x - data TxnOutputType = TxnPubKey -- ^ JSON of "pubkey" received. | TxnPubKeyHash -- ^ JSON of "pubkeyhash" received. | TxnScriptHash -- ^ JSON of "scripthash" received. @@ -193,3 +167,65 @@ put $ VarInt $ fromIntegral $ length os forM_ os put putWord32le l + + +-- | Data type representing the coinbase transaction of a 'Block'. Coinbase +-- transactions are special types of transactions which are created by miners +-- when they find a new block. Coinbase transactions have no inputs. They have +-- outputs sending the newly generated bitcoins together with all the block's +-- fees to a bitcoin address (usually the miners address). Data can be embedded +-- in a Coinbase transaction which can be chosen by the miner of a block. This +-- data also typically contains some randomness which is used, together with +-- the nonce, to find a partial hash collision on the block's hash. +data Coinbase = Coinbase { + + -- | Transaction data format version. + cbVersion :: Word32, + + -- | Previous outpoint. This is ignored for + -- coinbase transactions but preserved for computing + -- the correct txid. + cbPrevOutput :: OutPoint, + + -- | Data embedded inside the coinbase transaction. + cbData :: BS.ByteString, + + -- | Transaction sequence number. This is ignored for + -- coinbase transactions but preserved for computing + -- the correct txid. + cbInSequence :: Word32, + + -- | List of transaction outputs. + cbOut :: [TransactionOut], + + -- | The block number of timestamp at which this + -- transaction is locked. + cbLockTime :: Word32 + + } deriving (Eq, Show, Read) + +instance Binary Coinbase where + + get = do + v <- getWord32le + (VarInt len) <- get + unless (len == 1) $ fail "Coinbase get: Input size is not 1" + op <- get + (VarInt cbLen) <- get + cb <- getByteString (fromIntegral cbLen) + sq <- getWord32le + (VarInt oLen) <- get + os <- replicateM (fromIntegral oLen) get + lt <- getWord32le + return $ Coinbase v op cb sq os lt + + put (Coinbase v op cb sq os lt) = do + putWord32le v + put $ VarInt 1 + put op + put $ VarInt $ fromIntegral $ BS.length cb + putByteString cb + putWord32le sq + put $ VarInt $ fromIntegral $ length os + forM_ os put + putWord32le lt