10 "github.com/tjfoc/gmsm/sm3"
12 "github.com/bytom/bytom/encoding/blockchain"
15 // NewAssetID convert byte array to aseet id
16 func NewAssetID(b [32]byte) (a AssetID) {
18 V0: binary.BigEndian.Uint64(b[0:8]),
19 V1: binary.BigEndian.Uint64(b[8:16]),
20 V2: binary.BigEndian.Uint64(b[16:24]),
21 V3: binary.BigEndian.Uint64(b[24:32]),
25 // Byte32 return the byte array representation
26 func (a AssetID) Byte32() (b32 [32]byte) { return Hash(a).Byte32() }
28 // MarshalText satisfies the TextMarshaler interface.
29 func (a AssetID) MarshalText() ([]byte, error) { return Hash(a).MarshalText() }
31 // UnmarshalText satisfies the TextUnmarshaler interface.
32 func (a *AssetID) UnmarshalText(b []byte) error { return (*Hash)(a).UnmarshalText(b) }
34 // UnmarshalJSON satisfies the json.Unmarshaler interface.
35 func (a *AssetID) UnmarshalJSON(b []byte) error { return (*Hash)(a).UnmarshalJSON(b) }
37 // Bytes returns the byte representation.
38 func (a AssetID) Bytes() []byte { return Hash(a).Bytes() }
40 // WriteTo satisfies the io.WriterTo interface.
41 func (a AssetID) WriteTo(w io.Writer) (int64, error) { return Hash(a).WriteTo(w) }
43 // ReadFrom satisfies the io.ReaderFrom interface.
44 func (a *AssetID) ReadFrom(r io.Reader) (int64, error) { return (*Hash)(a).ReadFrom(r) }
46 // IsZero tells whether a Asset pointer is nil or points to an all-zero hash.
47 func (a *AssetID) IsZero() bool { return (*Hash)(a).IsZero() }
49 // ComputeAssetID calculate the asset id from AssetDefinition
50 func (ad *AssetDefinition) ComputeAssetID() (assetID AssetID) {
52 writeForHash(h, *ad) // error is impossible
54 copy(b32[:], h.Sum(nil))
55 return NewAssetID(b32)
58 // ComputeAssetID implement the assetID calculate logic
59 func ComputeAssetID(prog []byte, vmVersion uint64, data *Hash) AssetID {
60 def := &AssetDefinition{
61 IssuanceProgram: &Program{
68 return def.ComputeAssetID()
71 // ReadFrom read the AssetAmount from the bytes
72 func (a *AssetAmount) ReadFrom(r *blockchain.Reader) (err error) {
74 if _, err = assetID.ReadFrom(r); err != nil {
79 a.Amount, err = blockchain.ReadVarint63(r)
83 // WriteTo convert struct to byte and write to io
84 func (a AssetAmount) WriteTo(w io.Writer) (int64, error) {
85 n, err := a.AssetId.WriteTo(w)
90 n2, err := blockchain.WriteVarint63(w, a.Amount)
91 return n + int64(n2), err
94 // Equal check does two AssetAmount have same assetID and amount
95 func (a *AssetAmount) Equal(other *AssetAmount) (eq bool, err error) {
96 if a == nil || other == nil {
97 return false, errors.New("empty asset amount")
100 if a.AssetId == nil || other.AssetId == nil {
101 return false, errors.New("empty asset id")
104 return a.Amount == other.Amount && *a.AssetId == *other.AssetId, nil