github.com/chain5j/chain5j-pkg@v1.0.7/types/address_domain.go (about) 1 // Package types 2 // 3 // @author: xwc1125 4 package types 5 6 import ( 7 "database/sql/driver" 8 "encoding/json" 9 "errors" 10 "fmt" 11 "io" 12 "math/big" 13 "strings" 14 15 "github.com/chain5j/chain5j-pkg/codec/rlp" 16 "github.com/chain5j/chain5j-pkg/util/hexutil" 17 ) 18 19 var ( 20 EmptyDomainAddress = DomainAddress{ 21 Addr: EmptyAddress, 22 } 23 ) 24 25 type DomainAddress struct { 26 Addr Address `json:"addr"` // address 27 DomainAddr string `json:"domainAddr"` // domainAddr 28 } 29 30 func NewDomainAddress(domain string, addr Address) DomainAddress { 31 return DomainAddress{ 32 Addr: addr, 33 DomainAddr: domain, 34 } 35 } 36 37 // DomainToAddress domainAddr转Address。addr格式:xxx@chain5j.com或0xxxxxxxx 38 func DomainToAddress(s string) Address { 39 if strings.Contains(s, "@") { 40 split := strings.Split(s, "@") 41 if hexutil.IsHex(split[0]) { 42 return BytesToAddress(hexutil.MustDecode(split[0])) 43 } 44 return StringToAddress(split[0]) 45 } 46 return BytesToAddress(hexutil.MustDecode(s)) 47 } 48 49 // FromDomainAddress domainAddress 转换为DomainAddress对象。addr格式:xxx@chain5j.com:0xxxxxxxx 50 func FromDomainAddress(addr string) DomainAddress { 51 if strings.Contains(addr, ":") { 52 split := strings.Split(addr, ":") 53 if len(split) != 2 && len(split) != 2 { 54 return EmptyDomainAddress 55 } 56 var a DomainAddress 57 a.DomainAddr = split[0] 58 if len(split) == 2 { 59 a.Addr = BytesToAddress(hexutil.MustDecode(split[1])) 60 } 61 return a 62 } 63 return EmptyDomainAddress 64 } 65 66 func (a DomainAddress) Len() int { 67 return len(a.DomainAddr) 68 } 69 70 func (a DomainAddress) Bytes() []byte { 71 return []byte(a.String()) 72 } 73 74 func (a DomainAddress) FromBytes(b []byte) (Addr, error) { 75 addr := string(b) 76 return a.FromStr(addr) 77 } 78 79 func (a DomainAddress) String() string { 80 return a.DomainAddr + ":" + a.Addr.Hex() 81 } 82 83 func (a DomainAddress) FromStr(addr string) (Addr, error) { 84 if strings.Contains(addr, ":") { 85 split := strings.Split(addr, ":") 86 if len(split) != 2 && len(split) != 2 { 87 return EmptyDomainAddress, errors.New("invalid bytes") 88 } 89 a.DomainAddr = split[0] 90 if len(split) == 2 { 91 a.Addr = BytesToAddress(hexutil.MustDecode(split[1])) 92 } 93 return a, nil 94 } 95 return EmptyDomainAddress, errors.New("invalid bytes") 96 } 97 98 func (a DomainAddress) Nil() bool { 99 if a == EmptyDomainAddress { 100 return true 101 } 102 if a.DomainAddr == "" && a.Addr == EmptyAddress { 103 return true 104 } 105 return false 106 } 107 108 func (a DomainAddress) Validate(addr string) bool { 109 return !a.Nil() 110 } 111 112 func (a DomainAddress) Hash() Hash { 113 return BytesToHash(a.Bytes()) 114 } 115 116 func (a DomainAddress) Format(s fmt.State, c rune) { 117 fmt.Fprintf(s, "%"+string(c), a.Bytes()) 118 } 119 120 func (a DomainAddress) Big() *big.Int { 121 return new(big.Int).SetBytes(a.Bytes()) 122 } 123 124 func (a *DomainAddress) UnmarshalJSON(data []byte) error { 125 type erased DomainAddress 126 e := erased{} 127 err := json.Unmarshal(data, &e) 128 if err != nil { 129 return err 130 } 131 132 a.Addr = e.Addr 133 a.DomainAddr = e.DomainAddr 134 return nil 135 } 136 137 func (a DomainAddress) Value() (driver.Value, error) { 138 return a.Bytes(), nil 139 } 140 141 func (a *DomainAddress) EncodeRLP(w io.Writer) error { 142 return rlp.Encode(w, a.Bytes()) 143 } 144 145 func (a *DomainAddress) DecodeRLP(s *rlp.Stream) error { 146 var bytes []byte 147 err := s.Decode(&bytes) 148 if err != nil { 149 return err 150 } 151 addr := string(bytes) 152 domainAddress := FromDomainAddress(addr) 153 a.Addr = domainAddress.Addr 154 a.DomainAddr = domainAddress.DomainAddr 155 return nil 156 }