github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/asset/builder.go (about) 1 package asset 2 3 import ( 4 "context" 5 "crypto/rand" 6 "encoding/json" 7 "time" 8 9 log "github.com/sirupsen/logrus" 10 11 "github.com/bytom/bytom/blockchain/signers" 12 "github.com/bytom/bytom/blockchain/txbuilder" 13 "github.com/bytom/bytom/protocol/bc" 14 "github.com/bytom/bytom/protocol/bc/types" 15 ) 16 17 //NewIssueAction create a new asset issue action 18 func (reg *Registry) NewIssueAction(assetAmount bc.AssetAmount) txbuilder.Action { 19 return &issueAction{ 20 assets: reg, 21 AssetAmount: assetAmount, 22 } 23 } 24 25 //DecodeIssueAction unmarshal JSON-encoded data of asset issue action 26 func (reg *Registry) DecodeIssueAction(data []byte) (txbuilder.Action, error) { 27 a := &issueAction{assets: reg} 28 err := json.Unmarshal(data, a) 29 return a, err 30 } 31 32 type issueAction struct { 33 assets *Registry 34 bc.AssetAmount 35 Arguments []txbuilder.ContractArgument `json:"arguments"` 36 } 37 38 func (a *issueAction) Build(ctx context.Context, builder *txbuilder.TemplateBuilder) error { 39 if a.AssetId.IsZero() { 40 return txbuilder.MissingFieldsError("asset_id") 41 } 42 43 asset, err := a.assets.FindByID(ctx, a.AssetId) 44 if err != nil { 45 return err 46 } 47 48 var nonce [8]byte 49 if _, err = rand.Read(nonce[:]); err != nil { 50 return err 51 } 52 53 txin := types.NewIssuanceInput(nonce[:], a.Amount, asset.IssuanceProgram, nil, asset.RawDefinitionByte) 54 tplIn := &txbuilder.SigningInstruction{} 55 if asset.Signer != nil { 56 path := signers.GetBip0032Path(asset.Signer, signers.AssetKeySpace) 57 tplIn.AddRawWitnessKeys(asset.Signer.XPubs, path, asset.Signer.Quorum) 58 } else if a.Arguments != nil { 59 if err := txbuilder.AddContractArgs(tplIn, a.Arguments); err != nil { 60 return err 61 } 62 } 63 64 log.Info("Issue action build") 65 builder.RestrictMinTime(time.Now()) 66 return builder.AddInput(txin, tplIn) 67 } 68 69 func (a *issueAction) ActionType() string { 70 return "issue" 71 }