github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/bc/asset.go (about) 1 package bc 2 3 import ( 4 "encoding/binary" 5 "errors" 6 "io" 7 8 "github.com/bytom/bytom/crypto/sha3pool" 9 "github.com/bytom/bytom/encoding/blockchain" 10 ) 11 12 // NewAssetID convert byte array to aseet id 13 func NewAssetID(b [32]byte) (a AssetID) { 14 return AssetID{ 15 V0: binary.BigEndian.Uint64(b[0:8]), 16 V1: binary.BigEndian.Uint64(b[8:16]), 17 V2: binary.BigEndian.Uint64(b[16:24]), 18 V3: binary.BigEndian.Uint64(b[24:32]), 19 } 20 } 21 22 // Byte32 return the byte array representation 23 func (a AssetID) Byte32() (b32 [32]byte) { return Hash(a).Byte32() } 24 25 // MarshalText satisfies the TextMarshaler interface. 26 func (a AssetID) MarshalText() ([]byte, error) { return Hash(a).MarshalText() } 27 28 // UnmarshalText satisfies the TextUnmarshaler interface. 29 func (a *AssetID) UnmarshalText(b []byte) error { return (*Hash)(a).UnmarshalText(b) } 30 31 // UnmarshalJSON satisfies the json.Unmarshaler interface. 32 func (a *AssetID) UnmarshalJSON(b []byte) error { return (*Hash)(a).UnmarshalJSON(b) } 33 34 // Bytes returns the byte representation. 35 func (a AssetID) Bytes() []byte { return Hash(a).Bytes() } 36 37 // WriteTo satisfies the io.WriterTo interface. 38 func (a AssetID) WriteTo(w io.Writer) (int64, error) { return Hash(a).WriteTo(w) } 39 40 // ReadFrom satisfies the io.ReaderFrom interface. 41 func (a *AssetID) ReadFrom(r io.Reader) (int64, error) { return (*Hash)(a).ReadFrom(r) } 42 43 // IsZero tells whether a Asset pointer is nil or points to an all-zero hash. 44 func (a *AssetID) IsZero() bool { return (*Hash)(a).IsZero() } 45 46 // ComputeAssetID calculate the asset id from AssetDefinition 47 func (ad *AssetDefinition) ComputeAssetID() (assetID AssetID) { 48 h := sha3pool.Get256() 49 defer sha3pool.Put256(h) 50 writeForHash(h, *ad) // error is impossible 51 var b [32]byte 52 h.Read(b[:]) // error is impossible 53 return NewAssetID(b) 54 } 55 56 // ComputeAssetID implement the assetID calculate logic 57 func ComputeAssetID(prog []byte, vmVersion uint64, data *Hash) AssetID { 58 def := &AssetDefinition{ 59 IssuanceProgram: &Program{ 60 VmVersion: vmVersion, 61 Code: prog, 62 }, 63 Data: data, 64 } 65 66 return def.ComputeAssetID() 67 } 68 69 // ReadFrom read the AssetAmount from the bytes 70 func (a *AssetAmount) ReadFrom(r *blockchain.Reader) (err error) { 71 var assetID AssetID 72 if _, err = assetID.ReadFrom(r); err != nil { 73 return err 74 } 75 76 a.AssetId = &assetID 77 a.Amount, err = blockchain.ReadVarint63(r) 78 return err 79 } 80 81 // WriteTo convert struct to byte and write to io 82 func (a AssetAmount) WriteTo(w io.Writer) (int64, error) { 83 n, err := a.AssetId.WriteTo(w) 84 if err != nil { 85 return n, err 86 } 87 88 n2, err := blockchain.WriteVarint63(w, a.Amount) 89 return n + int64(n2), err 90 } 91 92 // Equal check does two AssetAmount have same assetID and amount 93 func (a *AssetAmount) Equal(other *AssetAmount) (eq bool, err error) { 94 if a == nil || other == nil { 95 return false, errors.New("empty asset amount") 96 } 97 98 if a.AssetId == nil || other.AssetId == nil { 99 return false, errors.New("empty asset id") 100 } 101 102 return a.Amount == other.Amount && *a.AssetId == *other.AssetId, nil 103 }