OSDN Git Service

feat: serializeAssetDef & checkValidJSON
authorHAOYUatHZ <haoyu@protonmail.com>
Tue, 21 May 2019 06:27:45 +0000 (14:27 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Tue, 21 May 2019 06:27:45 +0000 (14:27 +0800)
account/builder.go
asset/annotate.go
asset/asset.go
encoding/json/json.go
main.go [deleted file]

index 71cc5d4..0a969e1 100644 (file)
@@ -5,11 +5,13 @@ import (
        // TODO: stdjson?
        "encoding/json"
 
+       "github.com/vapor/asset"
        "github.com/vapor/blockchain/signers"
        "github.com/vapor/blockchain/txbuilder"
        "github.com/vapor/common"
        "github.com/vapor/consensus"
        "github.com/vapor/crypto/ed25519/chainkd"
+       chainjson "github.com/vapor/encoding/json"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
@@ -33,8 +35,9 @@ func (m *Manager) DecodeCrossInAction(data []byte) (txbuilder.Action, error) {
 
 type crossInAction struct {
        bc.AssetAmount
-       SourceID  string `json:"source_id"` // AnnotatedUTXO
-       SourcePos uint64 `json:"source_pos"`
+       SourceID        string                 `json:"source_id"` // AnnotatedUTXO
+       SourcePos       uint64                 `json:"source_pos"`
+       AssetDefinition map[string]interface{} `json:"asset_definition"`
 }
 
 // type AnnotatedInput struct {
@@ -65,9 +68,19 @@ func (a *crossInAction) Build(ctx context.Context, b *txbuilder.TemplateBuilder)
                return txbuilder.MissingFieldsError(missing...)
        }
 
-       sourceID := testutil.MustDecodeHash(a.SourceID)
+       // handle asset definition
+       rawDefinition, err := asset.SerializeAssetDef(a.AssetDefinition)
+       if err != nil {
+               return asset.ErrSerializing
+       }
+       if !chainjson.IsValidJSON(rawDefinition) {
+               return errors.New("asset definition is not in valid json format")
+       }
+       // TODO: check duplicate
+
        // in :=  types.NewCrossChainInput(arguments [][]byte, sourceID bc.Hash, assetID bc.AssetID, amount, sourcePos uint64, controlProgram, assetDefinition []byte)
-       in := types.NewCrossChainInput(nil, sourceID, *a.AssetId, a.Amount, a.SourcePos, nil, nil)
+       sourceID := testutil.MustDecodeHash(a.SourceID)
+       in := types.NewCrossChainInput(nil, sourceID, *a.AssetId, a.Amount, a.SourcePos, nil, rawDefinition)
        return b.AddInput(in, nil)
 }
 
index 4156202..723e0bb 100644 (file)
@@ -4,21 +4,16 @@ import (
        "encoding/json"
 
        "github.com/vapor/blockchain/query"
+       chainjson "github.com/vapor/encoding/json"
 )
 
-func isValidJSON(b []byte) bool {
-       var v interface{}
-       err := json.Unmarshal(b, &v)
-       return err == nil
-}
-
 //Annotated annotate the asset
 func Annotated(a *Asset) (*query.AnnotatedAsset, error) {
        jsonDefinition := json.RawMessage(`{}`)
 
        // a.RawDefinitionByte is the asset definition as it appears on the
        // blockchain, so it's untrusted and may not be valid json.
-       if isValidJSON(a.RawDefinitionByte) {
+       if chainjson.IsValidJSON(a.RawDefinitionByte) {
                jsonDefinition = json.RawMessage(a.RawDefinitionByte)
        }
 
index 12ae60b..a20a545 100644 (file)
@@ -33,7 +33,7 @@ var (
 func initNativeAsset() {
        alias := consensus.BTMAlias
 
-       definitionBytes, _ := serializeAssetDef(consensus.BTMDefinitionMap)
+       definitionBytes, _ := SerializeAssetDef(consensus.BTMDefinitionMap)
        DefaultNativeAsset = &Asset{
                AssetID:           *consensus.BTMAssetID,
                Alias:             &alias,
@@ -275,12 +275,12 @@ func (reg *Registry) ListAssets(id string) ([]*Asset, error) {
        return assets, nil
 }
 
-// serializeAssetDef produces a canonical byte representation of an asset
+// SerializeAssetDef produces a canonical byte representation of an asset
 // definition. Currently, this is implemented using pretty-printed JSON.
 // As is the standard for Go's map[string] serialization, object keys will
 // appear in lexicographic order. Although this is mostly meant for machine
 // consumption, the JSON is pretty-printed for easy reading.
-func serializeAssetDef(def map[string]interface{}) ([]byte, error) {
+func SerializeAssetDef(def map[string]interface{}) ([]byte, error) {
        if def == nil {
                def = make(map[string]interface{}, 0)
        }
index 0ad7b6a..02b8078 100644 (file)
@@ -5,6 +5,12 @@ import (
        "encoding/json"
 )
 
+func IsValidJSON(b []byte) bool {
+       var v interface{}
+       err := json.Unmarshal(b, &v)
+       return err == nil
+}
+
 type HexBytes []byte
 
 func (h HexBytes) MarshalText() ([]byte, error) {
diff --git a/main.go b/main.go
deleted file mode 100644 (file)
index bf6e7ed..0000000
--- a/main.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package main
-
-import (
-       "fmt"
-       "github.com/vapor/protocol/bc"
-       "github.com/vapor/testutil"
-)
-
-func main() {
-       fmt.Println((&bc.Hash{V0: 1, V1: 2, V2: 3, V3: 5}).String())
-       h := testutil.MustDecodeHash("0000000000000001000000000000000200000000000000030000000000000005")
-
-       fmt.Println(h)
-}