7 log "github.com/sirupsen/logrus"
9 "github.com/vapor/account"
10 "github.com/vapor/asset"
11 "github.com/vapor/blockchain/query"
12 "github.com/vapor/common"
13 "github.com/vapor/consensus"
14 "github.com/vapor/consensus/segwit"
15 "github.com/vapor/crypto/sha3pool"
16 dbm "github.com/vapor/database/leveldb"
17 "github.com/vapor/protocol/bc"
18 "github.com/vapor/protocol/bc/types"
19 "github.com/vapor/protocol/vm/vmutil"
22 // annotateTxs adds asset data to transactions
23 func annotateTxsAsset(w *Wallet, txs []*query.AnnotatedTx) {
24 for i, tx := range txs {
25 for j, input := range tx.Inputs {
26 alias, definition := w.getAliasDefinition(input.AssetID)
27 txs[i].Inputs[j].AssetAlias, txs[i].Inputs[j].AssetDefinition = alias, &definition
29 for k, output := range tx.Outputs {
30 alias, definition := w.getAliasDefinition(output.AssetID)
31 txs[i].Outputs[k].AssetAlias, txs[i].Outputs[k].AssetDefinition = alias, &definition
36 func (w *Wallet) getExternalDefinition(assetID *bc.AssetID) json.RawMessage {
37 definitionByte := w.DB.Get(asset.ExtAssetKey(assetID))
38 if definitionByte == nil {
42 definitionMap := make(map[string]interface{})
43 if err := json.Unmarshal(definitionByte, &definitionMap); err != nil {
47 alias := assetID.String()
48 externalAsset := &asset.Asset{
51 DefinitionMap: definitionMap,
52 RawDefinitionByte: definitionByte,
55 if err := w.AssetReg.SaveAsset(externalAsset, alias); err != nil {
56 log.WithFields(log.Fields{"module": logModule, "err": err, "assetID": alias}).Warning("fail on save external asset to internal asset DB")
61 func (w *Wallet) getAliasDefinition(assetID bc.AssetID) (string, json.RawMessage) {
63 if assetID.String() == consensus.BTMAssetID.String() {
64 alias := consensus.BTMAlias
65 definition := []byte(asset.DefaultNativeAsset.RawDefinitionByte)
67 return alias, definition
70 //local asset and saved external asset
71 if localAsset, err := w.AssetReg.FindByID(nil, &assetID); err == nil {
72 alias := *localAsset.Alias
73 definition := []byte(localAsset.RawDefinitionByte)
74 return alias, definition
78 if definition := w.getExternalDefinition(&assetID); definition != nil {
79 return assetID.String(), definition
85 // annotateTxs adds account data to transactions
86 func annotateTxsAccount(txs []*query.AnnotatedTx, walletDB dbm.DB) {
87 for i, tx := range txs {
88 for j, input := range tx.Inputs {
89 //issue asset tx input SpentOutputID is nil
90 if input.SpentOutputID == nil {
93 localAccount, err := getAccountFromACP(input.ControlProgram, walletDB)
94 if localAccount == nil || err != nil {
97 txs[i].Inputs[j].AccountAlias = localAccount.Alias
98 txs[i].Inputs[j].AccountID = localAccount.ID
100 for j, output := range tx.Outputs {
101 localAccount, err := getAccountFromACP(output.ControlProgram, walletDB)
102 if localAccount == nil || err != nil {
105 txs[i].Outputs[j].AccountAlias = localAccount.Alias
106 txs[i].Outputs[j].AccountID = localAccount.ID
111 func getAccountFromACP(program []byte, walletDB dbm.DB) (*account.Account, error) {
113 accountCP := account.CtrlProgram{}
114 localAccount := account.Account{}
116 sha3pool.Sum256(hash[:], program)
118 rawProgram := walletDB.Get(account.ContractKey(hash))
119 if rawProgram == nil {
120 return nil, fmt.Errorf("failed get account control program:%x ", hash)
123 if err := json.Unmarshal(rawProgram, &accountCP); err != nil {
127 accountValue := walletDB.Get(account.Key(accountCP.AccountID))
128 if accountValue == nil {
129 return nil, fmt.Errorf("failed get account:%s ", accountCP.AccountID)
132 if err := json.Unmarshal(accountValue, &localAccount); err != nil {
136 return &localAccount, nil
139 var emptyJSONObject = json.RawMessage(`{}`)
141 func isValidJSON(b []byte) bool {
143 err := json.Unmarshal(b, &v)
147 func (w *Wallet) buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statusFail bool, indexInBlock int) *query.AnnotatedTx {
148 tx := &query.AnnotatedTx{
150 Timestamp: b.Timestamp,
152 BlockHeight: b.Height,
153 Position: uint32(indexInBlock),
154 BlockTransactionsCount: uint32(len(b.Transactions)),
155 Inputs: make([]*query.AnnotatedInput, 0, len(orig.Inputs)),
156 Outputs: make([]*query.AnnotatedOutput, 0, len(orig.Outputs)),
157 StatusFail: statusFail,
158 Size: orig.SerializedSize,
160 for i := range orig.Inputs {
161 tx.Inputs = append(tx.Inputs, w.BuildAnnotatedInput(orig, uint32(i)))
163 for i := range orig.Outputs {
164 tx.Outputs = append(tx.Outputs, w.BuildAnnotatedOutput(orig, i))
169 // BuildAnnotatedInput build the annotated input.
170 func (w *Wallet) BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
172 in := &query.AnnotatedInput{
173 AssetDefinition: &emptyJSONObject,
175 if orig.InputType() != types.CoinbaseInputType {
176 in.AssetID = orig.AssetID()
177 in.Amount = orig.Amount()
180 id := tx.Tx.InputIDs[i]
183 switch e := e.(type) {
186 in.ControlProgram = orig.ControlProgram()
187 in.Address = w.getAddressFromControlProgram(in.ControlProgram)
188 in.SpentOutputID = e.SpentOutputId
189 arguments := orig.Arguments()
190 for _, arg := range arguments {
191 in.WitnessArguments = append(in.WitnessArguments, arg)
195 in.Arbitrary = e.Arbitrary
200 func (w *Wallet) getAddressFromControlProgram(prog []byte) string {
201 if segwit.IsP2WPKHScript(prog) {
202 if pubHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
203 return buildP2PKHAddress(pubHash)
205 } else if segwit.IsP2WSHScript(prog) {
206 if scriptHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
207 return buildP2SHAddress(scriptHash)
214 func buildP2PKHAddress(pubHash []byte) string {
215 address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
220 return address.EncodeAddress()
223 func buildP2SHAddress(scriptHash []byte) string {
224 address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.ActiveNetParams)
229 return address.EncodeAddress()
232 // BuildAnnotatedOutput build the annotated output.
233 func (w *Wallet) BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
234 orig := tx.Outputs[idx]
235 outid := tx.OutputID(idx)
236 out := &query.AnnotatedOutput{
239 AssetID: *orig.AssetAmount().AssetId,
240 AssetDefinition: &emptyJSONObject,
241 Amount: orig.AssetAmount().Amount,
242 ControlProgram: orig.ControlProgram(),
243 Address: w.getAddressFromControlProgram(orig.ControlProgram()),
246 if vmutil.IsUnspendable(out.ControlProgram) {