OSDN Git Service

add log (#373)
[bytom/vapor.git] / blockchain / txbuilder / actions.go
index 99f35bc..f5b40bc 100644 (file)
@@ -5,9 +5,10 @@ import (
        stdjson "encoding/json"
        "errors"
 
-       "github.com/vapor/config"
+       "golang.org/x/crypto/sha3"
 
        "github.com/vapor/common"
+       cfg "github.com/vapor/config"
        "github.com/vapor/consensus"
        "github.com/vapor/encoding/json"
        "github.com/vapor/protocol/bc"
@@ -167,7 +168,7 @@ func (a *crossOutAction) Build(ctx context.Context, b *TemplateBuilder) error {
                return MissingFieldsError(missing...)
        }
 
-       address, err := common.DecodeAddress(a.Address, &consensus.MainNetParams)
+       address, err := common.DecodeAddress(a.Address, consensus.BytomMainNetParams(&consensus.ActiveNetParams))
        if err != nil {
                return err
        }
@@ -263,7 +264,9 @@ type crossInAction struct {
        bc.AssetAmount
        SourceID          bc.Hash       `json:"source_id"`
        SourcePos         uint64        `json:"source_pos"`
+       VMVersion         uint64        `json:"vm_version"`
        RawDefinitionByte json.HexBytes `json:"raw_definition_byte"`
+       IssuanceProgram   json.HexBytes `json:"issuance_program"`
 }
 
 func (a *crossInAction) Build(ctx context.Context, builder *TemplateBuilder) error {
@@ -277,19 +280,35 @@ func (a *crossInAction) Build(ctx context.Context, builder *TemplateBuilder) err
        if a.Amount == 0 {
                missing = append(missing, "amount")
        }
+
        if len(missing) > 0 {
                return MissingFieldsError(missing...)
        }
 
+       if err := a.checkAssetID(); err != nil {
+               return err
+       }
+
        // arguments will be set when materializeWitnesses
-       fedProg := config.FederationProgrom(config.CommonConfig)
-       txin := types.NewCrossChainInput(nil, a.SourceID, *a.AssetId, a.Amount, a.SourcePos, fedProg, a.RawDefinitionByte)
+       txin := types.NewCrossChainInput(nil, a.SourceID, *a.AssetId, a.Amount, a.SourcePos, a.VMVersion, a.RawDefinitionByte, a.IssuanceProgram)
        tplIn := &SigningInstruction{}
-       fed := config.CommonConfig.Federation
-       tplIn.AddRawWitnessKeys(fed.Xpubs, nil, fed.Quorum)
+       fed := cfg.CommonConfig.Federation
+       tplIn.AddRawWitnessKeys(fed.Xpubs, cfg.FedAddressPath, fed.Quorum)
+       tplIn.AddDataWitness(cfg.FederationPMultiSigScript(cfg.CommonConfig))
        return builder.AddInput(txin, tplIn)
 }
 
 func (a *crossInAction) ActionType() string {
        return "cross_chain_in"
 }
+
+func (c *crossInAction) checkAssetID() error {
+       defHash := bc.NewHash(sha3.Sum256(c.RawDefinitionByte))
+       assetID := bc.ComputeAssetID(c.IssuanceProgram, c.VMVersion, &defHash)
+
+       if *c.AssetId != *consensus.BTMAssetID && assetID != *c.AssetAmount.AssetId {
+               return errors.New("incorrect asset_idincorrect asset_id")
+       }
+
+       return nil
+}