OSDN Git Service

update gm
authorLonelyPale <lonelypale@126.com>
Thu, 11 Nov 2021 08:08:38 +0000 (16:08 +0800)
committerLonelyPale <lonelypale@126.com>
Thu, 11 Nov 2021 08:08:38 +0000 (16:08 +0800)
protocol/bc/asset.go
protocol/bc/asset_gm.go [new file with mode: 0644]

index 55dd199..486fcd5 100644 (file)
@@ -1,3 +1,5 @@
+// +build !gm
+
 package bc
 
 import (
diff --git a/protocol/bc/asset_gm.go b/protocol/bc/asset_gm.go
new file mode 100644 (file)
index 0000000..70bed75
--- /dev/null
@@ -0,0 +1,105 @@
+// +build gm
+
+package bc
+
+import (
+       "encoding/binary"
+       "errors"
+       "io"
+
+       "github.com/tjfoc/gmsm/sm3"
+
+       "github.com/bytom/bytom/encoding/blockchain"
+)
+
+// NewAssetID convert byte array to aseet id
+func NewAssetID(b [32]byte) (a AssetID) {
+       return AssetID{
+               V0: binary.BigEndian.Uint64(b[0:8]),
+               V1: binary.BigEndian.Uint64(b[8:16]),
+               V2: binary.BigEndian.Uint64(b[16:24]),
+               V3: binary.BigEndian.Uint64(b[24:32]),
+       }
+}
+
+// Byte32 return the byte array representation
+func (a AssetID) Byte32() (b32 [32]byte) { return Hash(a).Byte32() }
+
+// MarshalText satisfies the TextMarshaler interface.
+func (a AssetID) MarshalText() ([]byte, error) { return Hash(a).MarshalText() }
+
+// UnmarshalText satisfies the TextUnmarshaler interface.
+func (a *AssetID) UnmarshalText(b []byte) error { return (*Hash)(a).UnmarshalText(b) }
+
+// UnmarshalJSON satisfies the json.Unmarshaler interface.
+func (a *AssetID) UnmarshalJSON(b []byte) error { return (*Hash)(a).UnmarshalJSON(b) }
+
+// Bytes returns the byte representation.
+func (a AssetID) Bytes() []byte { return Hash(a).Bytes() }
+
+// WriteTo satisfies the io.WriterTo interface.
+func (a AssetID) WriteTo(w io.Writer) (int64, error) { return Hash(a).WriteTo(w) }
+
+// ReadFrom satisfies the io.ReaderFrom interface.
+func (a *AssetID) ReadFrom(r io.Reader) (int64, error) { return (*Hash)(a).ReadFrom(r) }
+
+// IsZero tells whether a Asset pointer is nil or points to an all-zero hash.
+func (a *AssetID) IsZero() bool { return (*Hash)(a).IsZero() }
+
+// ComputeAssetID calculate the asset id from AssetDefinition
+func (ad *AssetDefinition) ComputeAssetID() (assetID AssetID) {
+       h := sm3.New()
+       writeForHash(h, *ad) // error is impossible
+       var b32 [32]byte
+       copy(b32[:], h.Sum(nil))
+       return NewAssetID(b32)
+}
+
+// ComputeAssetID implement the assetID calculate logic
+func ComputeAssetID(prog []byte, vmVersion uint64, data *Hash) AssetID {
+       def := &AssetDefinition{
+               IssuanceProgram: &Program{
+                       VmVersion: vmVersion,
+                       Code:      prog,
+               },
+               Data: data,
+       }
+
+       return def.ComputeAssetID()
+}
+
+// ReadFrom read the AssetAmount from the bytes
+func (a *AssetAmount) ReadFrom(r *blockchain.Reader) (err error) {
+       var assetID AssetID
+       if _, err = assetID.ReadFrom(r); err != nil {
+               return err
+       }
+
+       a.AssetId = &assetID
+       a.Amount, err = blockchain.ReadVarint63(r)
+       return err
+}
+
+// WriteTo convert struct to byte and write to io
+func (a AssetAmount) WriteTo(w io.Writer) (int64, error) {
+       n, err := a.AssetId.WriteTo(w)
+       if err != nil {
+               return n, err
+       }
+
+       n2, err := blockchain.WriteVarint63(w, a.Amount)
+       return n + int64(n2), err
+}
+
+// Equal check does two AssetAmount have same assetID and amount
+func (a *AssetAmount) Equal(other *AssetAmount) (eq bool, err error) {
+       if a == nil || other == nil {
+               return false, errors.New("empty asset amount")
+       }
+
+       if a.AssetId == nil || other.AssetId == nil {
+               return false, errors.New("empty asset id")
+       }
+
+       return a.Amount == other.Amount && *a.AssetId == *other.AssetId, nil
+}