github.com/annchain/OG@v0.0.9/og_interface/address.go (about) 1 package og_interface 2 3 import ( 4 "fmt" 5 6 "github.com/annchain/commongo/hexutil" 7 "github.com/annchain/commongo/marshaller" 8 ) 9 10 const ( 11 FlagAddressNone byte = iota 12 FlagAddress20 13 ) 14 15 func AddressFromHex(s string) (Address, error) { 16 bts, err := hexutil.FromHex(s) 17 if err != nil { 18 return nil, err 19 } 20 21 switch len(bts) { 22 case Address20Length: 23 var addr Address20 24 addr.FromBytes(bts) 25 26 return &addr, nil 27 default: 28 return nil, fmt.Errorf("unknown address hex length: %d", len(bts)) 29 } 30 } 31 32 func AddressFromAddressKey(key AddressKey) (Address, error) { 33 return AddressFromHex(string(key)) 34 } 35 36 func MarshalAddress(addr Address) ([]byte, error) { 37 return addr.MarshalMsg() 38 } 39 40 func UnmarshalAddress(b []byte) (Address, []byte, error) { 41 var addr Address 42 43 if len(b) == 0 { 44 return nil, nil, marshaller.ErrorNotEnoughBytes(1, 0) 45 } 46 47 lead := b[0] 48 switch lead { 49 case FlagAddress20: 50 addr = &Address20{} 51 default: 52 return nil, nil, fmt.Errorf("unknown Address lead: %x", lead) 53 } 54 55 b, err := addr.UnmarshalMsg(b) 56 return addr, b, err 57 }