OSDN Git Service

change BTMAssetID to human readable (#1216)
[bytom/bytom-spv.git] / protocol / bc / asset.go
index 876ad82..880df75 100644 (file)
@@ -1,31 +1,49 @@
 package bc
 
 import (
-       "database/sql/driver"
+       "encoding/binary"
        "errors"
        "io"
 
-       "github.com/blockchain/crypto/sha3pool"
-       "github.com/blockchain/encoding/blockchain"
+       "github.com/bytom/crypto/sha3pool"
+       "github.com/bytom/encoding/blockchain"
 )
 
-// AssetID is the Hash256 of the asset definition.
-
+// NewAssetID convert byte array to aseet id
 func NewAssetID(b [32]byte) (a AssetID) {
-       return AssetID(NewHash(b))
+       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]),
+       }
 }
 
-func (a AssetID) Byte32() (b32 [32]byte)               { return Hash(a).Byte32() }
-func (a AssetID) MarshalText() ([]byte, error)         { return Hash(a).MarshalText() }
-func (a *AssetID) UnmarshalText(b []byte) error        { return (*Hash)(a).UnmarshalText(b) }
-func (a *AssetID) UnmarshalJSON(b []byte) error        { return (*Hash)(a).UnmarshalJSON(b) }
-func (a AssetID) Bytes() []byte                        { return Hash(a).Bytes() }
-func (a AssetID) Value() (driver.Value, error)         { return Hash(a).Value() }
-func (a *AssetID) Scan(val interface{}) error          { return (*Hash)(a).Scan(val) }
-func (a AssetID) WriteTo(w io.Writer) (int64, error)   { return Hash(a).WriteTo(w) }
+// 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) }
-func (a *AssetID) IsZero() bool                        { return (*Hash)(a).IsZero() }
 
+// 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 := sha3pool.Get256()
        defer sha3pool.Put256(h)
@@ -35,9 +53,9 @@ func (ad *AssetDefinition) ComputeAssetID() (assetID AssetID) {
        return NewAssetID(b)
 }
 
-func ComputeAssetID(prog []byte, initialBlockID *Hash, vmVersion uint64, data *Hash) AssetID {
+// ComputeAssetID implement the assetID calculate logic
+func ComputeAssetID(prog []byte, vmVersion uint64, data *Hash) AssetID {
        def := &AssetDefinition{
-               InitialBlockId: initialBlockID,
                IssuanceProgram: &Program{
                        VmVersion: vmVersion,
                        Code:      prog,
@@ -47,10 +65,10 @@ func ComputeAssetID(prog []byte, initialBlockID *Hash, vmVersion uint64, data *H
        return def.ComputeAssetID()
 }
 
-func (a *AssetAmount) ReadFrom(r *blockchain.Reader) error {
+// ReadFrom read the AssetAmount from the bytes
+func (a *AssetAmount) ReadFrom(r *blockchain.Reader) (err error) {
        var assetID AssetID
-       _, err := assetID.ReadFrom(r)
-       if err != nil {
+       if _, err = assetID.ReadFrom(r); err != nil {
                return err
        }
        a.AssetId = &assetID
@@ -58,6 +76,7 @@ func (a *AssetAmount) ReadFrom(r *blockchain.Reader) error {
        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 {
@@ -67,6 +86,7 @@ func (a AssetAmount) WriteTo(w io.Writer) (int64, error) {
        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")