You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
5.2 KiB
128 lines
5.2 KiB
//
|
|
// monero_address_utils.cpp
|
|
// Copyright (c) 2014-2018, MyMonero.com
|
|
//
|
|
// 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. Redistributions of source code must retain the above copyright notice, this list of
|
|
// conditions and the following disclaimer.
|
|
//
|
|
// 2. 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.
|
|
//
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
// used to endorse or promote products derived from this software without specific
|
|
// prior written permission.
|
|
//
|
|
// 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.
|
|
//
|
|
//
|
|
#import "index.hpp"
|
|
//
|
|
#include "cryptonote_basic_impl.h"
|
|
#include "string_tools.h"
|
|
#include "tools__ret_vals.hpp"
|
|
using namespace epee;
|
|
//
|
|
#include "monero_paymentID_utils.hpp"
|
|
//
|
|
// Accessors - Implementations
|
|
Monero_DecodedAddress_RetVals decodedAddress(string addressString, bool isTestnet)
|
|
{
|
|
Monero_DecodedAddress_RetVals *retVals = [Monero_DecodedAddress_RetVals new];
|
|
//
|
|
cryptonote::address_parse_info info;
|
|
bool didSucceed = cryptonote::get_account_address_from_str(info, isTestnet ? cryptonote::TESTNET : cryptonote::MAINNET, std::string(addressString.UTF8String));
|
|
if (didSucceed == false) {
|
|
retVals.errStr_orNil = NSLocalizedString(@"Invalid address", nil);
|
|
//
|
|
return retVals;
|
|
}
|
|
cryptonote::account_public_address address = info.address;
|
|
std::string pub_viewKey_hexString = string_tools::pod_to_hex(address.m_view_public_key);
|
|
std::string pub_spendKey_hexString = string_tools::pod_to_hex(address.m_spend_public_key);
|
|
//
|
|
NSString *pub_viewKey_NSString = [NSString stringWithUTF8String:pub_viewKey_hexString.c_str()];
|
|
NSString *pub_spendKey_NSString = [NSString stringWithUTF8String:pub_spendKey_hexString.c_str()];
|
|
NSString *paymentID_NSString_orNil = nil;
|
|
if (info.has_payment_id == true) {
|
|
crypto::hash8 payment_id = info.payment_id;
|
|
std::string payment_id_hexString = string_tools::pod_to_hex(payment_id);
|
|
paymentID_NSString_orNil = [NSString stringWithUTF8String:payment_id_hexString.c_str()];
|
|
}
|
|
{
|
|
retVals.pub_viewKey_NSString = pub_viewKey_NSString;
|
|
retVals.pub_spendKey_NSString = pub_spendKey_NSString;
|
|
retVals.paymentID_NSString_orNil = paymentID_NSString_orNil;
|
|
retVals.isSubaddress = info.is_subaddress;
|
|
}
|
|
return retVals;
|
|
}
|
|
+ (BOOL)isSubAddress:(NSString *)addressString isTestnet:(BOOL)isTestnet
|
|
{
|
|
Monero_DecodedAddress_RetVals *retVals = [self decodedAddress:addressString isTestnet:isTestnet];
|
|
//
|
|
return retVals.isSubaddress;
|
|
}
|
|
+ (BOOL)isIntegratedAddress:(NSString *)addressString isTestnet:(BOOL)isTestnet
|
|
{
|
|
Monero_DecodedAddress_RetVals *retVals = [self decodedAddress:addressString isTestnet:isTestnet];
|
|
//
|
|
return retVals.paymentID_NSString_orNil != nil;
|
|
}
|
|
|
|
+ (NSString *)new_integratedAddrFromStdAddr:(NSString *)std_address_NSString andShortPID:(NSString *)short_paymentID isTestnet:(BOOL)isTestnet
|
|
{
|
|
std::string payment_id__string = std::string(short_paymentID.UTF8String);
|
|
crypto::hash8 payment_id_short;
|
|
bool didParse = monero_paymentID_utils::parse_short_payment_id(payment_id__string, payment_id_short);
|
|
if (!didParse) {
|
|
return nil;
|
|
}
|
|
cryptonote::address_parse_info info;
|
|
bool didSucceed = cryptonote::get_account_address_from_str(info, isTestnet ? cryptonote::TESTNET : cryptonote::MAINNET, std::string(std_address_NSString.UTF8String));
|
|
if (didSucceed == false) {
|
|
return nil;
|
|
}
|
|
if (info.is_subaddress) {
|
|
NSString *msg = [NSString stringWithFormat:@"%@ must not be called with a subaddress", NSStringFromSelector(_cmd)];
|
|
NSAssert(false, msg);
|
|
[NSException raise:@"Illegal address value" format:@"%@", msg];
|
|
//
|
|
return nil;
|
|
}
|
|
if (info.has_payment_id != false) {
|
|
// could even throw / fatalError here
|
|
return nil; // that was not a std_address!
|
|
}
|
|
std::string int_address_string = cryptonote::get_account_integrated_address_as_str(
|
|
isTestnet ? cryptonote::TESTNET : cryptonote::MAINNET,
|
|
info.address,
|
|
payment_id_short
|
|
);
|
|
NSString *int_address_NSString = [NSString stringWithUTF8String:int_address_string.c_str()];
|
|
//
|
|
return int_address_NSString;
|
|
}
|
|
+ (NSString *)new_integratedAddrFromStdAddr:(NSString *)std_address_NSString andShortPID:(NSString *)short_paymentID // mainnet
|
|
{
|
|
return [self
|
|
new_integratedAddrFromStdAddr:std_address_NSString
|
|
andShortPID:short_paymentID
|
|
isTestnet:NO];
|
|
}
|
|
|
|
@end
|