github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/mobile/common.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:43</date> 10 //</624342653491744768> 11 12 13 //包含公用包中的所有包装器。 14 15 package geth 16 17 import ( 18 "encoding/hex" 19 "errors" 20 "fmt" 21 "strings" 22 23 "github.com/ethereum/go-ethereum/common" 24 ) 25 26 //hash表示任意数据的32字节keccak256哈希。 27 type Hash struct { 28 hash common.Hash 29 } 30 31 //NewHashFromBytes将字节切片转换为哈希值。 32 func NewHashFromBytes(binary []byte) (hash *Hash, _ error) { 33 h := new(Hash) 34 if err := h.SetBytes(common.CopyBytes(binary)); err != nil { 35 return nil, err 36 } 37 return h, nil 38 } 39 40 //NewHashFromHex将十六进制字符串转换为哈希值。 41 func NewHashFromHex(hex string) (hash *Hash, _ error) { 42 h := new(Hash) 43 if err := h.SetHex(hex); err != nil { 44 return nil, err 45 } 46 return h, nil 47 } 48 49 //setbytes将指定的字节切片设置为哈希值。 50 func (h *Hash) SetBytes(hash []byte) error { 51 if length := len(hash); length != common.HashLength { 52 return fmt.Errorf("invalid hash length: %v != %v", length, common.HashLength) 53 } 54 copy(h.hash[:], hash) 55 return nil 56 } 57 58 //GetBytes检索哈希的字节表示形式。 59 func (h *Hash) GetBytes() []byte { 60 return h.hash[:] 61 } 62 63 //sethex将指定的十六进制字符串设置为哈希值。 64 func (h *Hash) SetHex(hash string) error { 65 hash = strings.ToLower(hash) 66 if len(hash) >= 2 && hash[:2] == "0x" { 67 hash = hash[2:] 68 } 69 if length := len(hash); length != 2*common.HashLength { 70 return fmt.Errorf("invalid hash hex length: %v != %v", length, 2*common.HashLength) 71 } 72 bin, err := hex.DecodeString(hash) 73 if err != nil { 74 return err 75 } 76 copy(h.hash[:], bin) 77 return nil 78 } 79 80 //gethex检索哈希的十六进制字符串表示形式。 81 func (h *Hash) GetHex() string { 82 return h.hash.Hex() 83 } 84 85 //哈希表示哈希的一部分。 86 type Hashes struct{ hashes []common.Hash } 87 88 //newhashes创建未初始化哈希的切片。 89 func NewHashes(size int) *Hashes { 90 return &Hashes{ 91 hashes: make([]common.Hash, size), 92 } 93 } 94 95 //newhashempty创建哈希值的空切片。 96 func NewHashesEmpty() *Hashes { 97 return NewHashes(0) 98 } 99 100 //SIZE返回切片中的哈希数。 101 func (h *Hashes) Size() int { 102 return len(h.hashes) 103 } 104 105 //get返回切片中给定索引处的哈希。 106 func (h *Hashes) Get(index int) (hash *Hash, _ error) { 107 if index < 0 || index >= len(h.hashes) { 108 return nil, errors.New("index out of bounds") 109 } 110 return &Hash{h.hashes[index]}, nil 111 } 112 113 //set在切片中的给定索引处设置哈希。 114 func (h *Hashes) Set(index int, hash *Hash) error { 115 if index < 0 || index >= len(h.hashes) { 116 return errors.New("index out of bounds") 117 } 118 h.hashes[index] = hash.hash 119 return nil 120 } 121 122 //append在切片的末尾添加一个新的哈希元素。 123 func (h *Hashes) Append(hash *Hash) { 124 h.hashes = append(h.hashes, hash.hash) 125 } 126 127 //地址表示以太坊帐户的20字节地址。 128 type Address struct { 129 address common.Address 130 } 131 132 //newAddressFromBytes将字节切片转换为哈希值。 133 func NewAddressFromBytes(binary []byte) (address *Address, _ error) { 134 a := new(Address) 135 if err := a.SetBytes(common.CopyBytes(binary)); err != nil { 136 return nil, err 137 } 138 return a, nil 139 } 140 141 //newAddressFromHex将十六进制字符串转换为地址值。 142 func NewAddressFromHex(hex string) (address *Address, _ error) { 143 a := new(Address) 144 if err := a.SetHex(hex); err != nil { 145 return nil, err 146 } 147 return a, nil 148 } 149 150 //setbytes将指定的字节片设置为地址值。 151 func (a *Address) SetBytes(address []byte) error { 152 if length := len(address); length != common.AddressLength { 153 return fmt.Errorf("invalid address length: %v != %v", length, common.AddressLength) 154 } 155 copy(a.address[:], address) 156 return nil 157 } 158 159 //GetBytes检索地址的字节表示形式。 160 func (a *Address) GetBytes() []byte { 161 return a.address[:] 162 } 163 164 //sethex将指定的十六进制字符串设置为地址值。 165 func (a *Address) SetHex(address string) error { 166 address = strings.ToLower(address) 167 if len(address) >= 2 && address[:2] == "0x" { 168 address = address[2:] 169 } 170 if length := len(address); length != 2*common.AddressLength { 171 return fmt.Errorf("invalid address hex length: %v != %v", length, 2*common.AddressLength) 172 } 173 bin, err := hex.DecodeString(address) 174 if err != nil { 175 return err 176 } 177 copy(a.address[:], bin) 178 return nil 179 } 180 181 //gethex检索地址的十六进制字符串表示形式。 182 func (a *Address) GetHex() string { 183 return a.address.Hex() 184 } 185 186 //地址表示一个地址片。 187 type Addresses struct{ addresses []common.Address } 188 189 //newaddresses创建一个未初始化的地址切片。 190 func NewAddresses(size int) *Addresses { 191 return &Addresses{ 192 addresses: make([]common.Address, size), 193 } 194 } 195 196 //newAddressEmpty创建地址值的空切片。 197 func NewAddressesEmpty() *Addresses { 198 return NewAddresses(0) 199 } 200 201 //SIZE返回切片中的地址数。 202 func (a *Addresses) Size() int { 203 return len(a.addresses) 204 } 205 206 //GET从切片返回给定索引处的地址。 207 func (a *Addresses) Get(index int) (address *Address, _ error) { 208 if index < 0 || index >= len(a.addresses) { 209 return nil, errors.New("index out of bounds") 210 } 211 return &Address{a.addresses[index]}, nil 212 } 213 214 //set设置切片中给定索引的地址。 215 func (a *Addresses) Set(index int, address *Address) error { 216 if index < 0 || index >= len(a.addresses) { 217 return errors.New("index out of bounds") 218 } 219 a.addresses[index] = address.address 220 return nil 221 } 222 223 //append将新的地址元素添加到切片的末尾。 224 func (a *Addresses) Append(address *Address) { 225 a.addresses = append(a.addresses, address.address) 226 } 227