github.com/chain5j/chain5j-pkg@v1.0.7/crypto/icap/hash_iban.go (about)

     1  // Package icap
     2  //
     3  // @author: xwc1125
     4  package icap
     5  
     6  import (
     7  	"github.com/chain5j/chain5j-pkg/codec/json"
     8  	"github.com/chain5j/chain5j-pkg/crypto/base/base36"
     9  	"github.com/chain5j/chain5j-pkg/types"
    10  )
    11  
    12  // THash 交易Hash
    13  type THash struct {
    14  	ChainName string     `json:"chainName" mapstructure:"chainName"` // 链名称
    15  	Index     uint64     `json:"index" mapstructure:"index"`         // dbindex
    16  	Hash      types.Hash `json:"hash" mapstructure:"hash"`           // 交易hash值
    17  }
    18  
    19  // THashFromIban 字符串转对象
    20  func THashFromIban(tHash string) (THash, error) {
    21  	iBanInfo := NewIBanInfo(4, 0, 64, tHash)
    22  	customer, err := ParseICAP(*iBanInfo)
    23  	if err != nil {
    24  		return THash{}, err
    25  	}
    26  	hash := types.BytesToHash(customer.Customer())
    27  	return THash{
    28  		ChainName: customer.Currency(),
    29  		Index:     0,
    30  		Hash:      hash,
    31  	}, nil
    32  }
    33  
    34  // String tHash的值
    35  func (h THash) String() string {
    36  	indexBase36 := base36.Encode(uint64(h.Index))
    37  	customer := NewCustomer(
    38  		h.ChainName,
    39  		LeftJoin(indexBase36, 4),
    40  		60,
    41  		h.Hash.Bytes())
    42  	iBanInfo, _ := ToICAP(*customer)
    43  	return iBanInfo.Iban()
    44  }
    45  
    46  func (h THash) MarshalJSON() ([]byte, error) {
    47  	return json.Marshal(h.String())
    48  }
    49  
    50  func (h THash) Bytes() []byte {
    51  	bytes, _ := json.Marshal(h.String())
    52  	return bytes
    53  }
    54  
    55  func (h THash) Nil() bool {
    56  	if h.ChainName == "" || h.Hash.Nil() {
    57  		return true
    58  	}
    59  	return false
    60  }