-
Notifications
You must be signed in to change notification settings - Fork 4
/
DetailedTx.hs
93 lines (82 loc) · 3.12 KB
/
DetailedTx.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE OverloadedStrings #-}
module DetailedTx where
import Data.Aeson hiding (decode')
import Data.Aeson.Types (Pair)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as T
import Network.Haskoin.Crypto (pubKeyAddr,addrToBase58,derivePubKey,toWif)
import Network.Haskoin.Transaction (txHash)
import Network.Haskoin.Internals (Tx(..), TxIn(..), TxOut(..)
,scriptSender, scriptRecipient
,XPrvKey(..),XPubKey(..)
,xPrvIsHard,xPrvChild,xPubIsHard,xPubChild
)
import Network.Haskoin.Util (eitherToMaybe,decode')
import Utils (putHex)
import PrettyScript (showDoc, prettyScript)
(.=$) :: T.Text -> String -> Pair
(.=$) x y = x .= y
newtype DetailedTx = DetailedTx { _unDetailedTx :: Tx }
newtype DetailedTxIn = DetailedTxIn { _unDetailedTxIn :: TxIn }
newtype DetailedTxOut = DetailedTxOut { _unDetailedTxOut :: TxOut }
newtype DetailedXPrvKey = DetailedXPrvKey { _unDetailedXPrvKey :: XPrvKey }
newtype DetailedXPubKey = DetailedXPubKey { _unDetailedXPubKey :: XPubKey }
instance ToJSON DetailedTx where
toJSON (DetailedTx tx) =
object
["hash" .= txHash tx
,"version" .= txVersion tx
,"locktime" .= txLockTime tx
,"inputs" .= map DetailedTxIn (txIn tx)
,"outputs" .= map DetailedTxOut (txOut tx)
]
instance ToJSON DetailedTxIn where
toJSON (DetailedTxIn i) =
object
["previous_output" .= prevOutput i
,"script" .= showDoc (prettyScript script)
,"sequence" .= txInSequence i
,"address" .= eitherToMaybe (scriptSender script)
]
where script = decode' $ scriptInput i
instance ToJSON DetailedTxOut where
toJSON (DetailedTxOut o) =
object
["value" .= outValue o
,"script" .= showDoc (prettyScript script)
,"address" .= eitherToMaybe (scriptRecipient script)
]
where script = decode' $ scriptOutput o
instance ToJSON DetailedXPrvKey where
toJSON (DetailedXPrvKey k) =
object
["type" .=$ "xprv"
,"depth" .= xPrvDepth k
,"parent" .= xPrvParent k
,"index" .= object ["value" .= xPrvIndex k
,(if xPrvIsHard k then "hard" else "soft") .= xPrvChild k
]
,"chain" .= xPrvChain k
,"prvkey" .= toWif (xPrvKey k)
,"pubkey" .=$ putHex pub
,"address" .= addrToBase58 addr
]
where pub = derivePubKey (xPrvKey k)
addr = pubKeyAddr pub
instance ToJSON DetailedXPubKey where
toJSON (DetailedXPubKey k) =
object
["type" .=$ "xpub"
,"depth" .= xPubDepth k
,"parent" .= xPubParent k
,"index" .= object ["value" .= xPubIndex k
,(if xPubIsHard k then "hard" else "soft") .= xPubChild k
]
,"chain" .= xPubChain k
,"pubkey" .=$ putHex pub
,"address" .= addrToBase58 addr
]
where pub = xPubKey k
addr = pubKeyAddr pub
txDetailedJSON :: Tx -> LBS.ByteString
txDetailedJSON = encode . toJSON . DetailedTx