OSDN Git Service

update gm
[bytom/bytom.git] / protocol / bc / asset_gm.go
1 // +build gm
2
3 package bc
4
5 import (
6         "encoding/binary"
7         "errors"
8         "io"
9
10         "github.com/tjfoc/gmsm/sm3"
11
12         "github.com/bytom/bytom/encoding/blockchain"
13 )
14
15 // NewAssetID convert byte array to aseet id
16 func NewAssetID(b [32]byte) (a AssetID) {
17         return 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]),
22         }
23 }
24
25 // Byte32 return the byte array representation
26 func (a AssetID) Byte32() (b32 [32]byte) { return Hash(a).Byte32() }
27
28 // MarshalText satisfies the TextMarshaler interface.
29 func (a AssetID) MarshalText() ([]byte, error) { return Hash(a).MarshalText() }
30
31 // UnmarshalText satisfies the TextUnmarshaler interface.
32 func (a *AssetID) UnmarshalText(b []byte) error { return (*Hash)(a).UnmarshalText(b) }
33
34 // UnmarshalJSON satisfies the json.Unmarshaler interface.
35 func (a *AssetID) UnmarshalJSON(b []byte) error { return (*Hash)(a).UnmarshalJSON(b) }
36
37 // Bytes returns the byte representation.
38 func (a AssetID) Bytes() []byte { return Hash(a).Bytes() }
39
40 // WriteTo satisfies the io.WriterTo interface.
41 func (a AssetID) WriteTo(w io.Writer) (int64, error) { return Hash(a).WriteTo(w) }
42
43 // ReadFrom satisfies the io.ReaderFrom interface.
44 func (a *AssetID) ReadFrom(r io.Reader) (int64, error) { return (*Hash)(a).ReadFrom(r) }
45
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() }
48
49 // ComputeAssetID calculate the asset id from AssetDefinition
50 func (ad *AssetDefinition) ComputeAssetID() (assetID AssetID) {
51         h := sm3.New()
52         writeForHash(h, *ad) // error is impossible
53         var b32 [32]byte
54         copy(b32[:], h.Sum(nil))
55         return NewAssetID(b32)
56 }
57
58 // ComputeAssetID implement the assetID calculate logic
59 func ComputeAssetID(prog []byte, vmVersion uint64, data *Hash) AssetID {
60         def := &AssetDefinition{
61                 IssuanceProgram: &Program{
62                         VmVersion: vmVersion,
63                         Code:      prog,
64                 },
65                 Data: data,
66         }
67
68         return def.ComputeAssetID()
69 }
70
71 // ReadFrom read the AssetAmount from the bytes
72 func (a *AssetAmount) ReadFrom(r *blockchain.Reader) (err error) {
73         var assetID AssetID
74         if _, err = assetID.ReadFrom(r); err != nil {
75                 return err
76         }
77
78         a.AssetId = &assetID
79         a.Amount, err = blockchain.ReadVarint63(r)
80         return err
81 }
82
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)
86         if err != nil {
87                 return n, err
88         }
89
90         n2, err := blockchain.WriteVarint63(w, a.Amount)
91         return n + int64(n2), err
92 }
93
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")
98         }
99
100         if a.AssetId == nil || other.AssetId == nil {
101                 return false, errors.New("empty asset id")
102         }
103
104         return a.Amount == other.Amount && *a.AssetId == *other.AssetId, nil
105 }