github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/address.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  
    21  #include "common/address.h"
    22  #include "common/base58.h"
    23  namespace neb {
    24  address_t base58_to_address(const base58_address_t &addr) {
    25    return bytes::from_base58(addr);
    26  }
    27  base58_address_t address_to_base58(const address_t &addr) {
    28    return addr.to_base58();
    29  }
    30  
    31  bool is_valid_address(const address_t &addr) {
    32    if (addr.size() != NAS_ADDRESS_LEN)
    33      return false;
    34    if (addr[0] != NAS_ADDRESS_MAGIC_NUM)
    35      return false;
    36    if (addr[1] != NAS_ADDRESS_CONTRACT_MAGIC_NUM &&
    37        addr[1] != NAS_ADDRESS_ACCOUNT_MAGIC_NUM)
    38      return false;
    39    return true;
    40  }
    41  bool is_contract_address(const address_t &addr) {
    42    if (is_valid_address(addr) && addr[1] == NAS_ADDRESS_CONTRACT_MAGIC_NUM)
    43      return true;
    44    return false;
    45  }
    46  bool is_normal_address(const address_t &addr) {
    47    if (is_valid_address(addr) && addr[1] == NAS_ADDRESS_ACCOUNT_MAGIC_NUM)
    48      return true;
    49    return false;
    50  }
    51  } // namespace neb