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

     1  // Package icap
     2  //
     3  // @author: xwc1125
     4  package icap
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/chain5j/chain5j-pkg/types"
    10  )
    11  
    12  var (
    13  	AddressPrettyPrefix = 4
    14  	AddressPrettyLen    = 37
    15  	EmptyAddressPretty  = addressPretty{
    16  		Addr: types.EmptyAddress,
    17  	}
    18  )
    19  
    20  // addressPretty 优雅地址,base36类型
    21  type addressPretty struct {
    22  	ChainName string        `json:"chainName" mapstructure:"chainName"` // 链名称
    23  	Addr      types.Address `json:"addr" mapstructure:"addr"`           // 地址
    24  	ibanAddr  string        // ibanAddr地址
    25  }
    26  
    27  func NewAddressPretty(chainName string, address types.Address) addressPretty {
    28  	customer := NewCustomer(
    29  		chainName,
    30  		"",
    31  		AddressPrettyLen,
    32  		address.Bytes())
    33  	iBanInfo, err := ToICAP(*customer)
    34  	if err != nil {
    35  		return EmptyAddressPretty
    36  	}
    37  	iban := iBanInfo.Iban()
    38  
    39  	return addressPretty{
    40  		ChainName: chainName,
    41  		Addr:      address,
    42  		ibanAddr:  iban,
    43  	}
    44  }
    45  
    46  // AddressPrettyFromIban 字符串转对象
    47  func AddressPrettyFromIban(tAddr string) (addressPretty, error) {
    48  	iBanInfo := NewIBanInfo(AddressPrettyPrefix, 0, 31, tAddr)
    49  	customer, err := ParseICAP(*iBanInfo)
    50  	if err != nil {
    51  		return addressPretty{}, err
    52  	}
    53  	address := types.BytesToAddress(customer.Customer())
    54  	return addressPretty{
    55  		ChainName: customer.Currency(),
    56  		Addr:      address,
    57  		ibanAddr:  tAddr,
    58  	}, nil
    59  }
    60  
    61  func (a addressPretty) Len() int { return AddressPrettyLen }
    62  
    63  func (a addressPretty) Bytes() []byte { return []byte(a.String()) }
    64  
    65  func (a addressPretty) FromBytes(b []byte) (types.Addr, error) {
    66  	addr := string(b)
    67  	return a.FromStr(addr)
    68  }
    69  
    70  func (a addressPretty) String() string {
    71  	return a.ibanAddr
    72  }
    73  
    74  func (a addressPretty) FromStr(addr string) (types.Addr, error) {
    75  	return AddressPrettyFromIban(addr)
    76  }
    77  
    78  func (a addressPretty) Validate(addr string) bool {
    79  	_, err := AddressPrettyFromIban(addr)
    80  	if err != nil {
    81  		return false
    82  	}
    83  	return true
    84  }
    85  
    86  func (a addressPretty) Nil() bool {
    87  	return a.Addr == types.EmptyAddress
    88  }
    89  
    90  func (a addressPretty) Hash() types.Hash {
    91  	return types.BytesToHash(a.Bytes())
    92  }
    93  
    94  func (a addressPretty) Format(s fmt.State, c rune) {
    95  	fmt.Fprintf(s, "%"+string(c), a.Bytes())
    96  }
    97  
    98  // TerminalString String implements fmt.Stringer.
    99  func (a addressPretty) TerminalString() string {
   100  	return a.String()
   101  }
   102  
   103  func (a addressPretty) MarshalJSON() ([]byte, error) {
   104  	return []byte(a.String()), nil
   105  }
   106  
   107  func (a *addressPretty) UnmarshalJSON(input []byte) error {
   108  	addr := string(input)
   109  	pretty, err := AddressPrettyFromIban(addr)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	a.ChainName = pretty.ChainName
   114  	a.Addr = pretty.Addr
   115  	a.ibanAddr = pretty.ibanAddr
   116  	return nil
   117  }