Skip to content

Feature/new secp #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions bitcoin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ library
src
build-depends:
QuickCheck >=2.13.2
, aeson >=1.4.6.0
, array >=0.5.4.0
, base >=4.9 && <5
, base16 >=0.3.0.1
Expand All @@ -107,13 +106,12 @@ library
, entropy >=0.4.1.5
, hashable >=1.3.0.0
, hspec >=2.7.1
, libsecp256k1 >=0.1.0
, memory >=0.15.0
, mtl >=2.2.2
, murmur3 >=1.0.3
, network >=3.1.1.1
, safe >=0.3.18
, scientific >=0.3.6.2
, secp256k1-haskell >=0.4.0
, split >=0.2.3.3
, string-conversions >=0.4.0.1
, text >=1.2.3.0
Expand Down Expand Up @@ -147,7 +145,6 @@ test-suite spec
build-depends:
HUnit >=1.6.0.0
, QuickCheck >=2.13.2
, aeson >=1.4.6.0
, array >=0.5.4.0
, base >=4.9 && <5
, base16 >=0.3.0.1
Expand All @@ -165,13 +162,12 @@ test-suite spec
, hspec >=2.7.1
, lens >=4.18.1
, lens-aeson >=1.1
, libsecp256k1 >=0.1.0
, memory >=0.15.0
, mtl >=2.2.2
, murmur3 >=1.0.3
, network >=3.1.1.1
, safe >=0.3.18
, scientific >=0.3.6.2
, secp256k1-haskell >=0.4.0
, split >=0.2.3.3
, string-conversions >=0.4.0.1
, text >=1.2.3.0
Expand Down
4 changes: 1 addition & 3 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ extra-source-files:
- README.md
- CHANGELOG.md
dependencies:
- aeson >= 1.4.6.0
- array >= 0.5.4.0
- base >=4.9 && <5
- base16 >= 0.3.0.1
Expand All @@ -34,14 +33,13 @@ dependencies:
- hashable >= 1.3.0.0
- hspec >= 2.7.1
- memory >= 0.15.0
- mtl >= 2.2.2
- murmur3 >= 1.0.3
- network >= 3.1.1.1
- QuickCheck >= 2.13.2
- split >= 0.2.3.3
- safe >= 0.3.18
- scientific >= 0.3.6.2
- secp256k1-haskell >= 0.4.0
- libsecp256k1 >= 0.1.0
- string-conversions >= 0.4.0.1
- text >= 1.2.3.0
- time >= 1.9.3
Expand Down
38 changes: 7 additions & 31 deletions src/Bitcoin/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ module Bitcoin.Address (
textToAddr,
bech32ToAddr,
base58ToAddr,
addrToJSON,
addrToEncoding,
addrFromJSON,
pubKeyAddr,
pubKeyWitnessAddr,
pubKeyCompatWitnessAddr,
Expand All @@ -46,13 +43,17 @@ module Bitcoin.Address (
module Bitcoin.Address.Bech32,
) where

import Bitcoin.Address.Base58
import Bitcoin.Address.Bech32
import Bitcoin.Crypto
import Bitcoin.Data
import Bitcoin.Keys.Common
import Bitcoin.Script
import Bitcoin.Util
import Control.Applicative
import Control.Arrow (second)
import Control.DeepSeq
import Control.Monad
import Data.Aeson as A
import Data.Aeson.Encoding as A
import Data.Aeson.Types
import Data.Binary (Binary (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
Expand All @@ -66,13 +67,6 @@ import Data.Text (Text)
import qualified Data.Text as T
import Data.Word (Word8)
import GHC.Generics (Generic)
import Bitcoin.Address.Base58
import Bitcoin.Address.Bech32
import Bitcoin.Crypto
import Bitcoin.Data
import Bitcoin.Keys.Common
import Bitcoin.Script
import Bitcoin.Util


-- | Address format for Bitcoin
Expand Down Expand Up @@ -181,24 +175,6 @@ isWitnessAddress WitnessAddress{} = True
isWitnessAddress _ = False


addrToJSON :: Network -> Address -> Value
addrToJSON net a = toJSON (addrToText net a)


addrToEncoding :: Network -> Address -> Encoding
addrToEncoding net = maybe null_ text . addrToText net


-- | JSON parsing for Bitcoin addresses. Works with 'Base58', and
-- 'Bech32'.
addrFromJSON :: Network -> Value -> Parser Address
addrFromJSON net =
withText "address" $ \t ->
case textToAddr net t of
Nothing -> fail "could not decode address"
Just x -> return x


-- | Convert address to human-readable string. Uses 'Base58', or 'Bech32'
-- depending on network.
addrToText :: Network -> Address -> Maybe Text
Expand Down
83 changes: 4 additions & 79 deletions src/Bitcoin/Block/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,12 @@ module Bitcoin.Block.Common (
encodeCompact,
) where

import Bitcoin.Crypto.Hash
import Bitcoin.Network.Common
import Bitcoin.Transaction.Common
import Bitcoin.Util
import Control.DeepSeq
import Control.Monad (forM_, liftM2, mzero, replicateM, (<=<))
import Data.Aeson (
FromJSON (..),
ToJSON (..),
Value (..),
object,
toJSON,
withObject,
withText,
(.:),
(.=),
)
import Data.Aeson.Encoding (pairs, unsafeToEncoding)
import Data.Binary (Binary (..))
import Data.Bits (shiftL, shiftR, (.&.), (.|.))
import qualified Data.ByteString as B
Expand All @@ -66,10 +58,6 @@ import Data.String.Conversions (cs)
import Data.Text (Text)
import Data.Word (Word32)
import GHC.Generics (Generic)
import Bitcoin.Crypto.Hash
import Bitcoin.Network.Common
import Bitcoin.Transaction.Common
import Bitcoin.Util
import qualified Text.Read as R


Expand Down Expand Up @@ -111,17 +99,6 @@ instance Binary Block where
put = serialize


instance ToJSON Block where
toJSON (Block h t) = object ["header" .= h, "transactions" .= t]
toEncoding (Block h t) = pairs $ "header" .= h <> "transactions" .= t


instance FromJSON Block where
parseJSON =
withObject "Block" $ \o ->
Block <$> o .: "header" <*> o .: "transactions"


-- | Block header hash. To be serialized reversed for display purposes.
newtype BlockHash = BlockHash
{ getBlockHash :: Hash256
Expand Down Expand Up @@ -155,21 +132,6 @@ instance IsString BlockHash where
in fromMaybe e $ hexToBlockHash $ cs s


instance FromJSON BlockHash where
parseJSON =
withText "BlockHash" $
maybe mzero return . hexToBlockHash


instance ToJSON BlockHash where
toJSON = String . blockHashToHex
toEncoding h =
unsafeToEncoding $
char7 '"'
<> hexBuilder (BL.reverse (runPutL (serialize h)))
<> char7 '"'


-- | Block hashes are reversed with respect to the in-memory byte order in a
-- block hash when displayed.
blockHashToHex :: BlockHash -> Text
Expand Down Expand Up @@ -212,43 +174,6 @@ data BlockHeader = BlockHeader
deriving (Eq, Ord, Show, Read, Generic, Hashable, NFData)


-- 80 bytes

instance ToJSON BlockHeader where
toJSON (BlockHeader v p m t b n) =
object
[ "version" .= v
, "prevblock" .= p
, "merkleroot" .= encodeHex (runPutS (serialize m))
, "timestamp" .= t
, "bits" .= b
, "nonce" .= n
]
toEncoding (BlockHeader v p m t b n) =
pairs
( "version" .= v
<> "prevblock" .= p
<> "merkleroot" .= encodeHex (runPutS (serialize m))
<> "timestamp" .= t
<> "bits" .= b
<> "nonce" .= n
)


instance FromJSON BlockHeader where
parseJSON =
withObject "BlockHeader" $ \o ->
BlockHeader
<$> o .: "version"
<*> o .: "prevblock"
<*> (f =<< o .: "merkleroot")
<*> o .: "timestamp"
<*> o .: "bits"
<*> o .: "nonce"
where
f = maybe mzero return . (eitherToMaybe . runGetS deserialize <=< decodeHex)


instance Serial BlockHeader where
deserialize = do
v <- getWord32le
Expand Down
33 changes: 12 additions & 21 deletions src/Bitcoin/Block/Headers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,18 @@ module Bitcoin.Block.Headers (
lastSmallerOrEqual,
) where

import Bitcoin.Block.Common
import Bitcoin.Crypto
import Bitcoin.Data
import Bitcoin.Transaction.Genesis
import Bitcoin.Util
import Control.Applicative ((<|>))
import Control.DeepSeq
import Control.Monad (guard, mzero, unless, when)
import Control.Monad.Except (
ExceptT (..),
runExceptT,
throwError,
)
import Control.Monad.State.Strict as State (
StateT,
get,
gets,
lift,
modify,
)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except (ExceptT (..), runExceptT, throwE)
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.State.Strict as State (StateT, get, gets, modify)
import Data.Binary (Binary (..))
import Data.Bits (shiftL, shiftR, (.&.))
import qualified Data.ByteString as B
Expand All @@ -97,11 +93,6 @@ import Data.Serialize (Serialize (..))
import Data.Typeable (Typeable)
import Data.Word (Word32, Word64)
import GHC.Generics (Generic)
import Bitcoin.Block.Common
import Bitcoin.Crypto
import Bitcoin.Data
import Bitcoin.Transaction.Genesis
import Bitcoin.Util


-- | Short version of the block hash. Uses the good end of the hash (the part
Expand Down Expand Up @@ -323,7 +314,7 @@ connectBlocks _ _ [] = return $ Right []
connectBlocks net t bhs@(bh : _) =
runExceptT $ do
unless (chained bhs) $
throwError "Blocks to connect do not form a chain"
throwE "Blocks to connect do not form a chain"
par <-
maybeToExceptT
"Could not get parent block"
Expand All @@ -347,13 +338,13 @@ connectBlocks net t bhs@(bh : _) =
case skM of
Just sk -> return sk
Nothing ->
throwError $
throwE $
"BUG: Could not get skip for block "
++ show (headerHash $ nodeHeader par)
| otherwise = do
let sn = ls !! fromIntegral (nodeHeight par - sh)
when (nodeHeight sn /= sh) $
throwError "BUG: Node height not right in skip"
throwE "BUG: Node height not right in skip"
return sn
where
sh = skipHeight (nodeHeight par + 1)
Expand Down Expand Up @@ -394,7 +385,7 @@ connectBlock net t bh =
case skM of
Just sk -> return sk
Nothing ->
throwError $
throwE $
"BUG: Could not get skip for block "
++ show (headerHash $ nodeHeader par)
bb <- lift getBestBlockHeader
Expand Down
4 changes: 1 addition & 3 deletions src/Bitcoin/Crypto/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Bitcoin.Crypto.Hash (
initTaggedHash,
) where

import Bitcoin.Util
import Control.DeepSeq
import Crypto.Hash (
Context,
Expand Down Expand Up @@ -57,7 +58,6 @@ import Data.String (IsString, fromString)
import Data.String.Conversions (cs)
import Data.Word (Word32)
import GHC.Generics (Generic)
import Bitcoin.Util
import Text.Read as R


Expand Down Expand Up @@ -280,8 +280,6 @@ join512 (a, b) =


-- | Initialize tagged hash specified in BIP340
--
-- @since 0.21.0
initTaggedHash ::
-- | Hash tag
ByteString ->
Expand Down
Loading