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.ExtAssetKey(assetID))
39 if definitionByte == nil {
43 definitionMap := make(map[string]interface{})
44 if err := json.Unmarshal(definitionByte, &definitionMap); err != nil {
48 alias := assetID.String()
49 externalAsset := &asset.Asset{
52 DefinitionMap: definitionMap,
53 RawDefinitionByte: definitionByte,
54 Signer: &signers.Signer{Type: "external"},
57 if err := w.AssetReg.SaveAsset(externalAsset, alias); err != nil {
58 log.WithFields(log.Fields{"err": err, "assetID": alias}).Warning("fail on save external asset to internal asset DB")
63 func (w *Wallet) getAliasDefinition(assetID bc.AssetID) (string, json.RawMessage) {
65 if assetID.String() == consensus.BTMAssetID.String() {
66 alias := consensus.BTMAlias
67 definition := []byte(asset.DefaultNativeAsset.RawDefinitionByte)
69 return alias, definition
72 //local asset and saved external asset
73 if localAsset, err := w.AssetReg.FindByID(nil, &assetID); err == nil {
74 alias := *localAsset.Alias
75 definition := []byte(localAsset.RawDefinitionByte)
76 return alias, definition
80 if definition := w.getExternalDefinition(&assetID); definition != nil {
81 return assetID.String(), definition
87 // annotateTxs adds account data to transactions
88 func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB db.DB) {
89 for i, tx := range txs {
90 for j, input := range tx.Inputs {
91 //issue asset tx input SpentOutputID is nil
92 if input.SpentOutputID == nil {
95 localAccount, err := getAccountFromUTXO(*input.SpentOutputID, walletDB)
96 if localAccount == nil || err != nil {
99 txs[i].Inputs[j].AccountAlias = localAccount.Alias
100 txs[i].Inputs[j].AccountID = localAccount.ID
102 for j, output := range tx.Outputs {
103 localAccount, err := getAccountFromACP(output.ControlProgram, walletDB)
104 if localAccount == nil || err != nil {
107 txs[i].Outputs[j].AccountAlias = localAccount.Alias
108 txs[i].Outputs[j].AccountID = localAccount.ID
113 func getAccountFromUTXO(outputID bc.Hash, walletDB db.DB) (*account.Account, error) {
114 accountUTXO := account.UTXO{}
115 localAccount := account.Account{}
117 accountUTXOValue := walletDB.Get(account.StandardUTXOKey(outputID))
118 if accountUTXOValue == nil {
119 return nil, fmt.Errorf("failed get account utxo:%x ", outputID)
122 if err := json.Unmarshal(accountUTXOValue, &accountUTXO); err != nil {
126 accountValue := walletDB.Get(account.Key(accountUTXO.AccountID))
127 if accountValue == nil {
128 return nil, fmt.Errorf("failed get account:%s ", accountUTXO.AccountID)
130 if err := json.Unmarshal(accountValue, &localAccount); err != nil {
134 return &localAccount, nil
137 func getAccountFromACP(program []byte, walletDB db.DB) (*account.Account, error) {
139 accountCP := account.CtrlProgram{}
140 localAccount := account.Account{}
142 sha3pool.Sum256(hash[:], program)
144 rawProgram := walletDB.Get(account.ContractKey(hash))
145 if rawProgram == nil {
146 return nil, fmt.Errorf("failed get account control program:%x ", hash)
149 if err := json.Unmarshal(rawProgram, &accountCP); err != nil {
153 accountValue := walletDB.Get(account.Key(accountCP.AccountID))
154 if accountValue == nil {
155 return nil, fmt.Errorf("failed get account:%s ", accountCP.AccountID)
158 if err := json.Unmarshal(accountValue, &localAccount); err != nil {
162 return &localAccount, nil
165 var emptyJSONObject = json.RawMessage(`{}`)
167 func isValidJSON(b []byte) bool {
169 err := json.Unmarshal(b, &v)
173 func (w *Wallet) buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statusFail bool, indexInBlock int) *query.AnnotatedTx {
174 tx := &query.AnnotatedTx{
176 Timestamp: b.Timestamp,
178 BlockHeight: b.Height,
179 Position: uint32(indexInBlock),
180 BlockTransactionsCount: uint32(len(b.Transactions)),
181 Inputs: make([]*query.AnnotatedInput, 0, len(orig.Inputs)),
182 Outputs: make([]*query.AnnotatedOutput, 0, len(orig.Outputs)),
183 StatusFail: statusFail,
184 Size: orig.SerializedSize,
186 for i := range orig.Inputs {
187 tx.Inputs = append(tx.Inputs, w.BuildAnnotatedInput(orig, uint32(i)))
189 for i := range orig.Outputs {
190 tx.Outputs = append(tx.Outputs, w.BuildAnnotatedOutput(orig, i))
195 // BuildAnnotatedInput build the annotated input.
196 func (w *Wallet) BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
198 in := &query.AnnotatedInput{
199 AssetDefinition: &emptyJSONObject,
201 if orig.InputType() != types.CoinbaseInputType {
202 in.AssetID = orig.AssetID()
203 in.Amount = orig.Amount()
206 id := tx.Tx.InputIDs[i]
208 switch e := e.(type) {
211 in.ControlProgram = orig.ControlProgram()
212 in.Address = w.getAddressFromControlProgram(in.ControlProgram)
213 in.SpentOutputID = e.SpentOutputId
216 in.IssuanceProgram = orig.IssuanceProgram()
219 in.Arbitrary = e.Arbitrary
224 func (w *Wallet) getAddressFromControlProgram(prog []byte) string {
225 if segwit.IsP2WPKHScript(prog) {
226 if pubHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
227 return buildP2PKHAddress(pubHash)
229 } else if segwit.IsP2WSHScript(prog) {
230 if scriptHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
231 return buildP2SHAddress(scriptHash)
238 func buildP2PKHAddress(pubHash []byte) string {
239 address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
244 return address.EncodeAddress()
247 func buildP2SHAddress(scriptHash []byte) string {
248 address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.ActiveNetParams)
253 return address.EncodeAddress()
256 // BuildAnnotatedOutput build the annotated output.
257 func (w *Wallet) BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
258 orig := tx.Outputs[idx]
259 outid := tx.OutputID(idx)
260 out := &query.AnnotatedOutput{
263 AssetID: *orig.AssetId,
264 AssetDefinition: &emptyJSONObject,
266 ControlProgram: orig.ControlProgram,
267 Address: w.getAddressFromControlProgram(orig.ControlProgram),
270 if vmutil.IsUnspendable(out.ControlProgram) {