/
annotated.go
266 lines (232 loc) · 7.69 KB
/
annotated.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package wallet
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/bytom/bytom-classic/account"
"github.com/bytom/bytom-classic/asset"
"github.com/bytom/bytom-classic/blockchain/query"
"github.com/bytom/bytom-classic/blockchain/signers"
"github.com/bytom/bytom-classic/common"
"github.com/bytom/bytom-classic/consensus"
"github.com/bytom/bytom-classic/consensus/segwit"
"github.com/bytom/bytom-classic/crypto/sha3pool"
dbm "github.com/bytom/bytom-classic/database/leveldb"
"github.com/bytom/bytom-classic/protocol/bc"
"github.com/bytom/bytom-classic/protocol/bc/types"
"github.com/bytom/bytom-classic/protocol/vm/vmutil"
)
// annotateTxs adds asset data to transactions
func annotateTxsAsset(w *Wallet, txs []*query.AnnotatedTx) {
for i, tx := range txs {
for j, input := range tx.Inputs {
alias, definition := w.getAliasDefinition(input.AssetID)
txs[i].Inputs[j].AssetAlias, txs[i].Inputs[j].AssetDefinition = alias, &definition
}
for k, output := range tx.Outputs {
alias, definition := w.getAliasDefinition(output.AssetID)
txs[i].Outputs[k].AssetAlias, txs[i].Outputs[k].AssetDefinition = alias, &definition
}
}
}
func (w *Wallet) getExternalDefinition(assetID *bc.AssetID) json.RawMessage {
definitionByte := w.DB.Get(asset.ExtAssetKey(assetID))
if definitionByte == nil {
return nil
}
definitionMap := make(map[string]interface{})
if err := json.Unmarshal(definitionByte, &definitionMap); err != nil {
return nil
}
alias := assetID.String()
externalAsset := &asset.Asset{
AssetID: *assetID,
Alias: &alias,
DefinitionMap: definitionMap,
RawDefinitionByte: definitionByte,
Signer: &signers.Signer{Type: "external"},
}
if err := w.AssetReg.SaveAsset(externalAsset, alias); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err, "assetID": alias}).Warning("fail on save external asset to internal asset DB")
}
return definitionByte
}
func (w *Wallet) getAliasDefinition(assetID bc.AssetID) (string, json.RawMessage) {
//btm
if assetID.String() == consensus.BTMAssetID.String() {
alias := consensus.BTMAlias
definition := []byte(asset.DefaultNativeAsset.RawDefinitionByte)
return alias, definition
}
//local asset and saved external asset
if localAsset, err := w.AssetReg.FindByID(nil, &assetID); err == nil {
alias := *localAsset.Alias
definition := []byte(localAsset.RawDefinitionByte)
return alias, definition
}
//external asset
if definition := w.getExternalDefinition(&assetID); definition != nil {
return assetID.String(), definition
}
return "", nil
}
// annotateTxs adds account data to transactions
func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB dbm.DB) {
for i, tx := range txs {
for j, input := range tx.Inputs {
//issue asset tx input SpentOutputID is nil
if input.SpentOutputID == nil {
continue
}
localAccount, err := getAccountFromACP(input.ControlProgram, walletDB)
if localAccount == nil || err != nil {
continue
}
txs[i].Inputs[j].AccountAlias = localAccount.Alias
txs[i].Inputs[j].AccountID = localAccount.ID
}
for j, output := range tx.Outputs {
localAccount, err := getAccountFromACP(output.ControlProgram, walletDB)
if localAccount == nil || err != nil {
continue
}
txs[i].Outputs[j].AccountAlias = localAccount.Alias
txs[i].Outputs[j].AccountID = localAccount.ID
}
}
}
func getAccountFromACP(program []byte, walletDB dbm.DB) (*account.Account, error) {
var hash common.Hash
accountCP := account.CtrlProgram{}
localAccount := account.Account{}
sha3pool.Sum256(hash[:], program)
rawProgram := walletDB.Get(account.ContractKey(hash))
if rawProgram == nil {
return nil, fmt.Errorf("failed get account control program:%x ", hash)
}
if err := json.Unmarshal(rawProgram, &accountCP); err != nil {
return nil, err
}
accountValue := walletDB.Get(account.Key(accountCP.AccountID))
if accountValue == nil {
return nil, fmt.Errorf("failed get account:%s ", accountCP.AccountID)
}
if err := json.Unmarshal(accountValue, &localAccount); err != nil {
return nil, err
}
return &localAccount, nil
}
var emptyJSONObject = json.RawMessage(`{}`)
func isValidJSON(b []byte) bool {
var v interface{}
err := json.Unmarshal(b, &v)
return err == nil
}
func (w *Wallet) buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statusFail bool, indexInBlock int) *query.AnnotatedTx {
tx := &query.AnnotatedTx{
ID: orig.ID,
Timestamp: b.Timestamp,
BlockID: b.Hash(),
BlockHeight: b.Height,
Position: uint32(indexInBlock),
BlockTransactionsCount: uint32(len(b.Transactions)),
Inputs: make([]*query.AnnotatedInput, 0, len(orig.Inputs)),
Outputs: make([]*query.AnnotatedOutput, 0, len(orig.Outputs)),
StatusFail: statusFail,
Size: orig.SerializedSize,
}
for i := range orig.Inputs {
tx.Inputs = append(tx.Inputs, w.BuildAnnotatedInput(orig, uint32(i)))
}
for i := range orig.Outputs {
tx.Outputs = append(tx.Outputs, w.BuildAnnotatedOutput(orig, i))
}
return tx
}
// BuildAnnotatedInput build the annotated input.
func (w *Wallet) BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
orig := tx.Inputs[i]
in := &query.AnnotatedInput{
AssetDefinition: &emptyJSONObject,
}
if orig.InputType() != types.CoinbaseInputType {
in.AssetID = orig.AssetID()
in.Amount = orig.Amount()
in.SignData = tx.SigHash(i)
}
id := tx.Tx.InputIDs[i]
in.InputID = id
e := tx.Entries[id]
switch e := e.(type) {
case *bc.Spend:
in.Type = "spend"
in.ControlProgram = orig.ControlProgram()
in.Address = w.getAddressFromControlProgram(in.ControlProgram)
in.SpentOutputID = e.SpentOutputId
arguments := orig.Arguments()
for _, arg := range arguments {
in.WitnessArguments = append(in.WitnessArguments, arg)
}
case *bc.Issuance:
in.Type = "issue"
in.IssuanceProgram = orig.IssuanceProgram()
arguments := orig.Arguments()
for _, arg := range arguments {
in.WitnessArguments = append(in.WitnessArguments, arg)
}
if assetDefinition := orig.AssetDefinition(); isValidJSON(assetDefinition) {
assetDefinition := json.RawMessage(assetDefinition)
in.AssetDefinition = &assetDefinition
}
case *bc.Coinbase:
in.Type = "coinbase"
in.Arbitrary = e.Arbitrary
}
return in
}
func (w *Wallet) getAddressFromControlProgram(prog []byte) string {
if segwit.IsP2WPKHScript(prog) {
if pubHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
return buildP2PKHAddress(pubHash)
}
} else if segwit.IsP2WSHScript(prog) {
if scriptHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
return buildP2SHAddress(scriptHash)
}
}
return ""
}
func buildP2PKHAddress(pubHash []byte) string {
address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
if err != nil {
return ""
}
return address.EncodeAddress()
}
func buildP2SHAddress(scriptHash []byte) string {
address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.ActiveNetParams)
if err != nil {
return ""
}
return address.EncodeAddress()
}
// BuildAnnotatedOutput build the annotated output.
func (w *Wallet) BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
orig := tx.Outputs[idx]
outid := tx.OutputID(idx)
out := &query.AnnotatedOutput{
OutputID: *outid,
Position: idx,
AssetID: *orig.AssetId,
AssetDefinition: &emptyJSONObject,
Amount: orig.Amount,
ControlProgram: orig.ControlProgram,
Address: w.getAddressFromControlProgram(orig.ControlProgram),
}
if vmutil.IsUnspendable(out.ControlProgram) {
out.Type = "retire"
} else {
out.Type = "control"
}
return out
}