github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/asset/annotate.go (about)

     1  package asset
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/bytom/bytom/blockchain/query"
     7  	chainjson "github.com/bytom/bytom/encoding/json"
     8  	"github.com/bytom/bytom/protocol/vm/vmutil"
     9  )
    10  
    11  func isValidJSON(b []byte) bool {
    12  	var v interface{}
    13  	err := json.Unmarshal(b, &v)
    14  	return err == nil
    15  }
    16  
    17  //Annotated annotate the asset
    18  func Annotated(a *Asset) (*query.AnnotatedAsset, error) {
    19  	jsonDefinition := json.RawMessage(`{}`)
    20  
    21  	// a.RawDefinitionByte is the asset definition as it appears on the
    22  	// blockchain, so it's untrusted and may not be valid json.
    23  	if isValidJSON(a.RawDefinitionByte) {
    24  		jsonDefinition = json.RawMessage(a.RawDefinitionByte)
    25  	}
    26  
    27  	annotatedAsset := &query.AnnotatedAsset{
    28  		ID:                a.AssetID,
    29  		Alias:             *a.Alias,
    30  		VMVersion:         a.VMVersion,
    31  		RawDefinitionByte: a.RawDefinitionByte,
    32  		Definition:        &jsonDefinition,
    33  		IssuanceProgram:   chainjson.HexBytes(a.IssuanceProgram),
    34  	}
    35  
    36  	annotatedAsset.LimitHeight = vmutil.GetIssuanceProgramRestrictHeight(a.IssuanceProgram)
    37  	if a.Signer != nil {
    38  		annotatedAsset.AnnotatedSigner = query.AnnotatedSigner{
    39  			Type:       a.Signer.Type,
    40  			XPubs:      a.Signer.XPubs,
    41  			Quorum:     a.Signer.Quorum,
    42  			KeyIndex:   a.Signer.KeyIndex,
    43  			DeriveRule: a.Signer.DeriveRule,
    44  		}
    45  	}
    46  	return annotatedAsset, nil
    47  }