OSDN Git Service

updated equity frontend. (#1177)
[bytom/bytom.git] / wallet / annotated.go
index 02c7d4b..aca87a1 100644 (file)
@@ -13,6 +13,7 @@ import (
        "github.com/bytom/blockchain/signers"
        "github.com/bytom/common"
        "github.com/bytom/consensus"
+       "github.com/bytom/consensus/segwit"
        "github.com/bytom/crypto/sha3pool"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/types"
@@ -34,7 +35,7 @@ func annotateTxsAsset(w *Wallet, txs []*query.AnnotatedTx) {
 }
 
 func (w *Wallet) getExternalDefinition(assetID *bc.AssetID) json.RawMessage {
-       definitionByte := w.DB.Get(asset.CalcExtAssetKey(assetID))
+       definitionByte := w.DB.Get(asset.ExtAssetKey(assetID))
        if definitionByte == nil {
                return nil
        }
@@ -44,17 +45,18 @@ func (w *Wallet) getExternalDefinition(assetID *bc.AssetID) json.RawMessage {
                return nil
        }
 
-       saveAlias := assetID.String()
-       storeBatch := w.DB.NewBatch()
-
-       externalAsset := &asset.Asset{AssetID: *assetID, Alias: &saveAlias, DefinitionMap: definitionMap, Signer: &signers.Signer{Type: "external"}}
-       if rawAsset, err := json.Marshal(externalAsset); err == nil {
-               log.WithFields(log.Fields{"assetID": assetID.String(), "alias": saveAlias}).Info("index external asset")
-               storeBatch.Set(asset.Key(assetID), rawAsset)
+       alias := assetID.String()
+       externalAsset := &asset.Asset{
+               AssetID:           *assetID,
+               Alias:             &alias,
+               DefinitionMap:     definitionMap,
+               RawDefinitionByte: definitionByte,
+               Signer:            &signers.Signer{Type: "external"},
        }
-       storeBatch.Set(asset.AliasKey(saveAlias), []byte(assetID.String()))
-       storeBatch.Write()
 
+       if err := w.AssetReg.SaveAsset(externalAsset, alias); err != nil {
+               log.WithFields(log.Fields{"err": err, "assetID": alias}).Warning("fail on save external asset to internal asset DB")
+       }
        return definitionByte
 }
 
@@ -139,7 +141,7 @@ func getAccountFromACP(program []byte, walletDB db.DB) (*account.Account, error)
 
        sha3pool.Sum256(hash[:], program)
 
-       rawProgram := walletDB.Get(account.CPKey(hash))
+       rawProgram := walletDB.Get(account.ContractKey(hash))
        if rawProgram == nil {
                return nil, fmt.Errorf("failed get account control program:%x ", hash)
        }
@@ -168,7 +170,7 @@ func isValidJSON(b []byte) bool {
        return err == nil
 }
 
-func buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statusFail bool, indexInBlock int) *query.AnnotatedTx {
+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,
@@ -179,18 +181,19 @@ func buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statusFail bool,
                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, BuildAnnotatedInput(orig, uint32(i)))
+               tx.Inputs = append(tx.Inputs, w.BuildAnnotatedInput(orig, uint32(i)))
        }
        for i := range orig.Outputs {
-               tx.Outputs = append(tx.Outputs, BuildAnnotatedOutput(orig, i))
+               tx.Outputs = append(tx.Outputs, w.BuildAnnotatedOutput(orig, i))
        }
        return tx
 }
 
 // BuildAnnotatedInput build the annotated input.
-func BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
+func (w *Wallet) BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
        orig := tx.Inputs[i]
        in := &query.AnnotatedInput{
                AssetDefinition: &emptyJSONObject,
@@ -201,11 +204,13 @@ func BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
        }
 
        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
        case *bc.Issuance:
                in.Type = "issue"
@@ -217,8 +222,40 @@ func BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
        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 BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
+func (w *Wallet) BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
        orig := tx.Outputs[idx]
        outid := tx.OutputID(idx)
        out := &query.AnnotatedOutput{
@@ -228,7 +265,9 @@ func BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
                AssetDefinition: &emptyJSONObject,
                Amount:          orig.Amount,
                ControlProgram:  orig.ControlProgram,
+               Address:         w.getAddressFromControlProgram(orig.ControlProgram),
        }
+
        if vmutil.IsUnspendable(out.ControlProgram) {
                out.Type = "retire"
        } else {