7 log "github.com/sirupsen/logrus"
8 "github.com/tendermint/tmlibs/db"
10 "github.com/bytom/account"
11 "github.com/bytom/asset"
12 "github.com/bytom/blockchain/query"
13 "github.com/bytom/blockchain/signers"
14 "github.com/bytom/common"
15 "github.com/bytom/consensus"
16 "github.com/bytom/consensus/segwit"
17 "github.com/bytom/crypto/sha3pool"
18 "github.com/bytom/protocol/bc"
19 "github.com/bytom/protocol/bc/types"
20 "github.com/bytom/protocol/vm/vmutil"
23 // annotateTxs adds asset data to transactions
24 func annotateTxsAsset(w *Wallet, txs []*query.AnnotatedTx) {
25 for i, tx := range txs {
26 for j, input := range tx.Inputs {
27 alias, definition := w.getAliasDefinition(input.AssetID)
28 txs[i].Inputs[j].AssetAlias, txs[i].Inputs[j].AssetDefinition = alias, &definition
30 for k, output := range tx.Outputs {
31 alias, definition := w.getAliasDefinition(output.AssetID)
32 txs[i].Outputs[k].AssetAlias, txs[i].Outputs[k].AssetDefinition = alias, &definition
37 func (w *Wallet) getExternalDefinition(assetID *bc.AssetID) json.RawMessage {
38 definitionByte := w.DB.Get(asset.CalcExtAssetKey(assetID))
39 if definitionByte == nil {
43 definitionMap := make(map[string]interface{})
44 if err := json.Unmarshal(definitionByte, &definitionMap); err != nil {
48 saveAlias := assetID.String()
49 storeBatch := w.DB.NewBatch()
51 externalAsset := &asset.Asset{
54 DefinitionMap: definitionMap,
55 RawDefinitionByte: definitionByte,
56 Signer: &signers.Signer{Type: "external"},
59 if rawAsset, err := json.Marshal(externalAsset); err == nil {
60 log.WithFields(log.Fields{"assetID": assetID.String(), "alias": saveAlias}).Info("index external asset")
61 storeBatch.Set(asset.Key(assetID), rawAsset)
63 storeBatch.Set(asset.AliasKey(saveAlias), []byte(assetID.String()))
69 func (w *Wallet) getAliasDefinition(assetID bc.AssetID) (string, json.RawMessage) {
71 if assetID.String() == consensus.BTMAssetID.String() {
72 alias := consensus.BTMAlias
73 definition := []byte(asset.DefaultNativeAsset.RawDefinitionByte)
75 return alias, definition
78 //local asset and saved external asset
79 if localAsset, err := w.AssetReg.FindByID(nil, &assetID); err == nil {
80 alias := *localAsset.Alias
81 definition := []byte(localAsset.RawDefinitionByte)
82 return alias, definition
86 if definition := w.getExternalDefinition(&assetID); definition != nil {
87 return assetID.String(), definition
93 // annotateTxs adds account data to transactions
94 func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB db.DB) {
95 for i, tx := range txs {
96 for j, input := range tx.Inputs {
97 //issue asset tx input SpentOutputID is nil
98 if input.SpentOutputID == nil {
101 localAccount, err := getAccountFromUTXO(*input.SpentOutputID, walletDB)
102 if localAccount == nil || err != nil {
105 txs[i].Inputs[j].AccountAlias = localAccount.Alias
106 txs[i].Inputs[j].AccountID = localAccount.ID
108 for j, output := range tx.Outputs {
109 localAccount, err := getAccountFromACP(output.ControlProgram, walletDB)
110 if localAccount == nil || err != nil {
113 txs[i].Outputs[j].AccountAlias = localAccount.Alias
114 txs[i].Outputs[j].AccountID = localAccount.ID
119 func getAccountFromUTXO(outputID bc.Hash, walletDB db.DB) (*account.Account, error) {
120 accountUTXO := account.UTXO{}
121 localAccount := account.Account{}
123 accountUTXOValue := walletDB.Get(account.StandardUTXOKey(outputID))
124 if accountUTXOValue == nil {
125 return nil, fmt.Errorf("failed get account utxo:%x ", outputID)
128 if err := json.Unmarshal(accountUTXOValue, &accountUTXO); err != nil {
132 accountValue := walletDB.Get(account.Key(accountUTXO.AccountID))
133 if accountValue == nil {
134 return nil, fmt.Errorf("failed get account:%s ", accountUTXO.AccountID)
136 if err := json.Unmarshal(accountValue, &localAccount); err != nil {
140 return &localAccount, nil
143 func getAccountFromACP(program []byte, walletDB db.DB) (*account.Account, error) {
145 accountCP := account.CtrlProgram{}
146 localAccount := account.Account{}
148 sha3pool.Sum256(hash[:], program)
150 rawProgram := walletDB.Get(account.CPKey(hash))
151 if rawProgram == nil {
152 return nil, fmt.Errorf("failed get account control program:%x ", hash)
155 if err := json.Unmarshal(rawProgram, &accountCP); err != nil {
159 accountValue := walletDB.Get(account.Key(accountCP.AccountID))
160 if accountValue == nil {
161 return nil, fmt.Errorf("failed get account:%s ", accountCP.AccountID)
164 if err := json.Unmarshal(accountValue, &localAccount); err != nil {
168 return &localAccount, nil
171 var emptyJSONObject = json.RawMessage(`{}`)
173 func isValidJSON(b []byte) bool {
175 err := json.Unmarshal(b, &v)
179 func (w *Wallet) buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statusFail bool, indexInBlock int) *query.AnnotatedTx {
180 tx := &query.AnnotatedTx{
182 Timestamp: b.Timestamp,
184 BlockHeight: b.Height,
185 Position: uint32(indexInBlock),
186 BlockTransactionsCount: uint32(len(b.Transactions)),
187 Inputs: make([]*query.AnnotatedInput, 0, len(orig.Inputs)),
188 Outputs: make([]*query.AnnotatedOutput, 0, len(orig.Outputs)),
189 StatusFail: statusFail,
191 for i := range orig.Inputs {
192 tx.Inputs = append(tx.Inputs, w.BuildAnnotatedInput(orig, uint32(i)))
194 for i := range orig.Outputs {
195 tx.Outputs = append(tx.Outputs, w.BuildAnnotatedOutput(orig, i))
200 // BuildAnnotatedInput build the annotated input.
201 func (w *Wallet) BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
203 in := &query.AnnotatedInput{
204 AssetDefinition: &emptyJSONObject,
206 if orig.InputType() != types.CoinbaseInputType {
207 in.AssetID = orig.AssetID()
208 in.Amount = orig.Amount()
211 id := tx.Tx.InputIDs[i]
213 switch e := e.(type) {
216 in.ControlProgram = orig.ControlProgram()
217 in.Address = w.getAddressFromControlProgram(in.ControlProgram)
218 in.SpentOutputID = e.SpentOutputId
221 in.IssuanceProgram = orig.IssuanceProgram()
224 in.Arbitrary = e.Arbitrary
229 func (w *Wallet) getAddressFromControlProgram(prog []byte) string {
230 if segwit.IsP2WPKHScript(prog) {
231 if pubHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
232 return buildP2PKHAddress(pubHash)
234 } else if segwit.IsP2WSHScript(prog) {
235 if scriptHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
236 return buildP2SHAddress(scriptHash)
243 func buildP2PKHAddress(pubHash []byte) string {
244 address, err := common.NewAddressWitnessPubKeyHash(pubHash, consensus.ActiveNetParams)
249 return address.EncodeAddress()
252 func buildP2SHAddress(scriptHash []byte) string {
253 address, err := common.NewAddressWitnessScriptHash(scriptHash, consensus.ActiveNetParams)
258 return address.EncodeAddress()
261 // BuildAnnotatedOutput build the annotated output.
262 func (w *Wallet) BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
263 orig := tx.Outputs[idx]
264 outid := tx.OutputID(idx)
265 out := &query.AnnotatedOutput{
268 AssetID: *orig.AssetId,
269 AssetDefinition: &emptyJSONObject,
271 ControlProgram: orig.ControlProgram,
272 Address: w.getAddressFromControlProgram(orig.ControlProgram),
275 if vmutil.IsUnspendable(out.ControlProgram) {