github.com/aergoio/aergo@v1.3.1/types/account.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/hex"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/anaskhan96/base58check"
    10  )
    11  
    12  const AddressLength = 33
    13  const NameLength = 12
    14  const EncodedAddressLength = 52
    15  
    16  //NewAccount alloc new account object
    17  func NewAccount(addr []byte) *Account {
    18  	return &Account{
    19  		Address: addr,
    20  	}
    21  }
    22  
    23  //ToAddress return byte array of given base58check encoded address string
    24  func ToAddress(addr string) []byte {
    25  	ret, err := DecodeAddress(addr)
    26  	if err != nil {
    27  		return nil
    28  	}
    29  	return ret
    30  }
    31  
    32  //ToString return base58check encoded string of address
    33  func (a *Account) ToString() string {
    34  	return EncodeAddress(a.Address)
    35  }
    36  
    37  //NewAccountList alloc new account list
    38  func NewAccountList(accounts []*Account) *AccountList {
    39  	return &AccountList{
    40  		Accounts: accounts,
    41  	}
    42  }
    43  
    44  type Address = []byte
    45  
    46  const AddressVersion = 0x42
    47  const PrivKeyVersion = 0xAA
    48  
    49  func EncodeAddress(addr Address) string {
    50  	if len(addr) != AddressLength {
    51  		return string(addr)
    52  	}
    53  	encoded, _ := base58check.Encode(fmt.Sprintf("%x", AddressVersion), hex.EncodeToString(addr))
    54  	return encoded
    55  }
    56  
    57  const allowed = "abcdefghijklmnopqrstuvwxyz1234567890."
    58  
    59  func DecodeAddress(encodedAddr string) (Address, error) {
    60  	if len(encodedAddr) <= NameLength || strings.Contains(encodedAddr, ".") {
    61  		name := encodedAddr
    62  		for _, char := range string(name) {
    63  			if !strings.Contains(allowed, strings.ToLower(string(char))) {
    64  				return nil, fmt.Errorf("not allowed character in %s", string(name))
    65  			}
    66  		}
    67  		return []byte(name), nil
    68  	}
    69  	decodedString, err := base58check.Decode(encodedAddr)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	decodedBytes, err := hex.DecodeString(decodedString)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	var decoded []byte
    78  	version := decodedBytes[0]
    79  	switch version {
    80  	case AddressVersion:
    81  		decoded = decodedBytes[1:]
    82  		if len(decoded) != AddressLength {
    83  			return nil, errors.New("Invalid address length")
    84  		}
    85  	default:
    86  		return nil, errors.New("Invalid address version")
    87  	}
    88  	return decoded, nil
    89  }
    90  
    91  func EncodePrivKey(key []byte) string {
    92  	encoded, _ := base58check.Encode(fmt.Sprintf("%x", PrivKeyVersion), hex.EncodeToString(key))
    93  	return encoded
    94  }
    95  
    96  func DecodePrivKey(encodedKey string) ([]byte, error) {
    97  	decodedString, err := base58check.Decode(encodedKey)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	decodedBytes, err := hex.DecodeString(decodedString)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	version := decodedBytes[0]
   106  	if version != PrivKeyVersion {
   107  		return nil, errors.New("Invalid private key version")
   108  	}
   109  	decoded := decodedBytes[1:]
   110  	return decoded, nil
   111  }