/* * Copyright (c) 2015 Cryptonomex, Inc., and contributors. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Any modified source or binaries are used only with the BitShares network. * * 2. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * * 3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include #include #include #include #include namespace graphene { namespace chain { address::address(){} address::address( const std::string& base58str ) { std::string prefix( GRAPHENE_ADDRESS_PREFIX ); FC_ASSERT( is_valid( base58str, prefix ), "${str}", ("str",base58str) ); std::vector v = fc::from_base58( base58str.substr( prefix.size() ) ); memcpy( (char*)addr._hash, v.data(), std::min( v.size()-4, sizeof( addr ) ) ); } bool address::is_valid( const std::string& base58str, const std::string& prefix ) { const size_t prefix_len = prefix.size(); if( base58str.size() <= prefix_len ) return false; if( base58str.substr( 0, prefix_len ) != prefix ) return false; std::vector v; try { v = fc::from_base58( base58str.substr( prefix_len ) ); } catch( const fc::parse_error_exception& e ) { return false; } if( v.size() != sizeof( fc::ripemd160 ) + 4 ) return false; const fc::ripemd160 checksum = fc::ripemd160::hash( v.data(), v.size() - 4 ); if( memcmp( v.data() + 20, (char*)checksum._hash, 4 ) != 0 ) return false; return true; } address::address( const fc::ecc::public_key& pub ) { auto dat = pub.serialize(); addr = fc::ripemd160::hash( fc::sha512::hash( dat.data, sizeof( dat ) ) ); } address::address( const pts_address& ptsaddr ) { addr = fc::ripemd160::hash( (char*)&ptsaddr, sizeof( ptsaddr ) ); } address::address( const fc::ecc::public_key_data& pub ) { addr = fc::ripemd160::hash( fc::sha512::hash( pub.data, sizeof( pub ) ) ); } address::address( const graphene::chain::public_key_type& pub ) { addr = fc::ripemd160::hash( fc::sha512::hash( pub.key_data.data, sizeof( pub.key_data ) ) ); } address::operator std::string()const { fc::array bin_addr; memcpy( (char*)&bin_addr, (char*)&addr, sizeof( addr ) ); auto checksum = fc::ripemd160::hash( (char*)&addr, sizeof( addr ) ); memcpy( ((char*)&bin_addr)+20, (char*)&checksum._hash[0], 4 ); return GRAPHENE_ADDRESS_PREFIX + fc::to_base58( bin_addr.data, sizeof( bin_addr ) ); } } } // namespace graphene::chain namespace fc { void to_variant( const graphene::chain::address& var, variant& vo ) { vo = std::string(var); } void from_variant( const variant& var, graphene::chain::address& vo ) { vo = graphene::chain::address( var.as_string() ); } }