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