-
Notifications
You must be signed in to change notification settings - Fork 41
/
decodetx.go
184 lines (154 loc) · 5.86 KB
/
decodetx.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package dcrlibwallet
import (
"fmt"
"decred.org/dcrwallet/v2/wallet"
"github.com/decred/dcrd/blockchain/stake/v4"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/txscript/v4/stdscript"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrdata/v7/txhelpers"
"github.com/planetdecred/dcrlibwallet/txhelper"
)
const BlockValid = 1 << 0
// DecodeTransaction uses `walletTx.Hex` to retrieve detailed information for a transaction.
func (w *Wallet) DecodeTransaction(walletTx *TxInfoFromWallet, netParams *chaincfg.Params) (*Transaction, error) {
msgTx, txFee, txSize, txFeeRate, err := txhelper.MsgTxFeeSizeRate(walletTx.Hex)
if err != nil {
return nil, err
}
inputs, totalWalletInput, totalWalletUnmixedInputs := w.decodeTxInputs(msgTx, walletTx.Inputs)
outputs, totalWalletOutput, totalWalletMixedOutputs, mixedOutputsCount := w.decodeTxOutputs(msgTx, netParams, walletTx.Outputs)
amount, direction := txhelper.TransactionAmountAndDirection(totalWalletInput, totalWalletOutput, int64(txFee))
ssGenVersion, lastBlockValid, voteBits, ticketSpentHash := voteInfo(msgTx)
// ticketSpentHash will be empty if this isn't a vote tx
if txhelpers.IsSSRtx(msgTx) {
ticketSpentHash = msgTx.TxIn[0].PreviousOutPoint.Hash.String()
// set first tx input as amount for revoked txs
amount = msgTx.TxIn[0].ValueIn
} else if stake.IsSStx(msgTx) {
// set first tx output as amount for ticket txs
amount = msgTx.TxOut[0].Value
}
isMixedTx, mixDenom, _ := txhelpers.IsMixTx(msgTx)
txType := txhelper.FormatTransactionType(wallet.TxTransactionType(msgTx))
if isMixedTx {
txType = txhelper.TxTypeMixed
mixChange := totalWalletOutput - totalWalletMixedOutputs
txFee = dcrutil.Amount(totalWalletUnmixedInputs - (totalWalletMixedOutputs + mixChange))
}
return &Transaction{
WalletID: walletTx.WalletID,
Hash: msgTx.TxHash().String(),
Type: txType,
Hex: walletTx.Hex,
Timestamp: walletTx.Timestamp,
BlockHeight: walletTx.BlockHeight,
MixDenomination: mixDenom,
MixCount: mixedOutputsCount,
Version: int32(msgTx.Version),
LockTime: int32(msgTx.LockTime),
Expiry: int32(msgTx.Expiry),
Fee: int64(txFee),
FeeRate: int64(txFeeRate),
Size: txSize,
Direction: direction,
Amount: amount,
Inputs: inputs,
Outputs: outputs,
VoteVersion: int32(ssGenVersion),
LastBlockValid: lastBlockValid,
VoteBits: voteBits,
TicketSpentHash: ticketSpentHash,
}, nil
}
func (wallet *Wallet) decodeTxInputs(mtx *wire.MsgTx, walletInputs []*WalletInput) (inputs []*TxInput, totalWalletInputs, totalWalletUnmixedInputs int64) {
inputs = make([]*TxInput, len(mtx.TxIn))
unmixedAccountNumber := wallet.ReadInt32ConfigValueForKey(AccountMixerUnmixedAccount, -1)
for i, txIn := range mtx.TxIn {
input := &TxInput{
PreviousTransactionHash: txIn.PreviousOutPoint.Hash.String(),
PreviousTransactionIndex: int32(txIn.PreviousOutPoint.Index),
PreviousOutpoint: txIn.PreviousOutPoint.String(),
Amount: txIn.ValueIn,
AccountNumber: -1, // correct account number is set below if this is a wallet output
}
// override account details if this is wallet input
for _, walletInput := range walletInputs {
if walletInput.Index == int32(i) {
input.AccountNumber = walletInput.AccountNumber
break
}
}
if input.AccountNumber != -1 {
totalWalletInputs += input.Amount
if input.AccountNumber == unmixedAccountNumber {
totalWalletUnmixedInputs += input.Amount
}
}
inputs[i] = input
}
return
}
func (wallet *Wallet) decodeTxOutputs(mtx *wire.MsgTx, netParams *chaincfg.Params,
walletOutputs []*WalletOutput) (outputs []*TxOutput, totalWalletOutput, totalWalletMixedOutputs int64, mixedOutputsCount int32) {
outputs = make([]*TxOutput, len(mtx.TxOut))
txType := txhelpers.DetermineTxType(mtx, true)
mixedAccountNumber := wallet.ReadInt32ConfigValueForKey(AccountMixerMixedAccount, -1)
for i, txOut := range mtx.TxOut {
// get address and script type for output
var address, scriptType string
if (txType == stake.TxTypeSStx) && (stake.IsStakeCommitmentTxOut(i)) {
addr, err := stake.AddrFromSStxPkScrCommitment(txOut.PkScript, netParams)
if err == nil {
address = addr.String()
}
scriptType = stdscript.STStakeSubmissionPubKeyHash.String()
} else {
// Ignore the error here since an error means the script
// couldn't parse and there is no additional information
// about it anyways.
scriptClass, addrs := stdscript.ExtractAddrs(txOut.Version, txOut.PkScript, netParams)
if len(addrs) > 0 {
address = addrs[0].String()
}
scriptType = scriptClass.String()
}
output := &TxOutput{
Index: int32(i),
Amount: txOut.Value,
Version: int32(txOut.Version),
ScriptType: scriptType,
Address: address, // correct address, account name and number set below if this is a wallet output
AccountNumber: -1,
}
// override address and account details if this is wallet output
for _, walletOutput := range walletOutputs {
if walletOutput.Index == output.Index {
output.Internal = walletOutput.Internal
output.Address = walletOutput.Address
output.AccountNumber = walletOutput.AccountNumber
break
}
}
if output.AccountNumber != -1 {
totalWalletOutput += output.Amount
if output.AccountNumber == mixedAccountNumber {
totalWalletMixedOutputs += output.Amount
mixedOutputsCount++
}
}
outputs[i] = output
}
return
}
func voteInfo(msgTx *wire.MsgTx) (ssGenVersion uint32, lastBlockValid bool, voteBits string, ticketSpentHash string) {
if stake.IsSSGen(msgTx, true) {
ssGenVersion = stake.SSGenVersion(msgTx)
bits := stake.SSGenVoteBits(msgTx)
voteBits = fmt.Sprintf("%#04x", bits)
lastBlockValid = bits&uint16(BlockValid) != 0
ticketSpentHash = msgTx.TxIn[1].PreviousOutPoint.Hash.String()
}
return
}