2015-10-12 17:02:59 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2015 Cryptonomex, Inc., and contributors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-07-21 21:09:44 +00:00
|
|
|
#include "Operations.hpp"
|
2015-07-29 21:56:37 +00:00
|
|
|
#include "Wallet.hpp"
|
|
|
|
|
|
|
|
|
|
#include <graphene/utilities/key_conversion.hpp>
|
2015-07-21 21:09:44 +00:00
|
|
|
|
|
|
|
|
#include <fc/smart_ref_impl.hpp>
|
|
|
|
|
|
2015-07-22 21:38:44 +00:00
|
|
|
TransferOperation* OperationBuilder::transfer(ObjectId sender, ObjectId receiver, qint64 amount,
|
2015-07-21 21:09:44 +00:00
|
|
|
ObjectId amountType, QString memo, ObjectId feeType)
|
|
|
|
|
{
|
2015-07-27 20:01:16 +00:00
|
|
|
try {
|
|
|
|
|
TransferOperation* op = new TransferOperation;
|
|
|
|
|
op->setSender(sender);
|
|
|
|
|
op->setReceiver(receiver);
|
|
|
|
|
op->setAmount(amount);
|
|
|
|
|
op->setAmountType(amountType);
|
|
|
|
|
op->setMemo(memo);
|
|
|
|
|
op->setFeeType(feeType);
|
|
|
|
|
auto feeParameters = model.global_properties().parameters.current_fees->get<graphene::chain::transfer_operation>();
|
|
|
|
|
op->setFee(op->operation().calculate_fee(feeParameters).value);
|
|
|
|
|
return op;
|
|
|
|
|
} catch (const fc::exception& e) {
|
|
|
|
|
qDebug() << e.to_detail_string().c_str();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString TransferOperation::memo() const {
|
|
|
|
|
if (!m_op.memo)
|
|
|
|
|
return QString::null;
|
2015-07-31 15:16:19 +00:00
|
|
|
if (memoIsEncrypted())
|
|
|
|
|
return tr("Encrypted Memo");
|
2015-07-27 20:01:16 +00:00
|
|
|
QString memo = QString::fromStdString(m_op.memo->get_message({}, {}));
|
2015-08-06 02:54:03 +00:00
|
|
|
while (memo.endsWith(QChar('\0')))
|
2015-07-27 20:01:16 +00:00
|
|
|
memo.chop(1);
|
|
|
|
|
return memo;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 15:16:19 +00:00
|
|
|
bool TransferOperation::memoIsEncrypted() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_op.memo)
|
|
|
|
|
return false;
|
|
|
|
|
if (m_op.memo->message.empty())
|
|
|
|
|
return false;
|
|
|
|
|
if (m_op.memo->from == public_key_type() && m_op.memo->to == public_key_type())
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 21:56:37 +00:00
|
|
|
bool TransferOperation::canEncryptMemo(Wallet* wallet, ChainDataModel* model) const
|
|
|
|
|
{
|
|
|
|
|
if (!m_op.memo) return false;
|
|
|
|
|
auto pub = model->getAccount(sender())->memoKey();
|
|
|
|
|
if (!wallet->hasPrivateKey(pub)) return false;
|
|
|
|
|
return graphene::utilities::wif_to_key(wallet->getPrivateKey(pub).toStdString()).valid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TransferOperation::canDecryptMemo(Wallet* wallet, ChainDataModel* model) const
|
|
|
|
|
{
|
|
|
|
|
if (!m_op.memo) return false;
|
|
|
|
|
auto pub = model->getAccount(receiver())->memoKey();
|
|
|
|
|
if (!wallet->hasPrivateKey(pub)) return false;
|
|
|
|
|
return graphene::utilities::wif_to_key(wallet->getPrivateKey(pub).toStdString()).valid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString TransferOperation::decryptedMemo(Wallet* wallet, ChainDataModel* model) const
|
|
|
|
|
{
|
|
|
|
|
fc::ecc::private_key privateKey;
|
|
|
|
|
fc::ecc::public_key publicKey;
|
|
|
|
|
|
|
|
|
|
if (canEncryptMemo(wallet, model)) {
|
|
|
|
|
privateKey = *graphene::utilities::wif_to_key(wallet->getPrivateKey(model->getAccount(sender())->memoKey()).toStdString());
|
|
|
|
|
publicKey = m_op.memo->to;
|
|
|
|
|
} else if (canDecryptMemo(wallet, model)) {
|
|
|
|
|
privateKey = *graphene::utilities::wif_to_key(wallet->getPrivateKey(model->getAccount(receiver())->memoKey()).toStdString());
|
|
|
|
|
publicKey = m_op.memo->from;
|
|
|
|
|
} else return QString::null;
|
|
|
|
|
|
|
|
|
|
return QString::fromStdString(m_op.memo->get_message(privateKey, publicKey));
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-27 20:01:16 +00:00
|
|
|
void TransferOperation::setMemo(QString memo) {
|
|
|
|
|
if (memo == this->memo())
|
|
|
|
|
return;
|
|
|
|
|
if (!m_op.memo)
|
|
|
|
|
m_op.memo = graphene::chain::memo_data();
|
|
|
|
|
while (memo.size() % 32)
|
|
|
|
|
memo.append('\0');
|
|
|
|
|
m_op.memo->set_message({}, {}, memo.toStdString());
|
|
|
|
|
Q_EMIT memoChanged();
|
2015-07-21 21:09:44 +00:00
|
|
|
}
|
2015-07-29 21:56:37 +00:00
|
|
|
|
|
|
|
|
void TransferOperation::encryptMemo(Wallet* wallet, ChainDataModel* model)
|
|
|
|
|
{
|
|
|
|
|
if (!canEncryptMemo(wallet, model)) return;
|
|
|
|
|
auto privateKey = graphene::utilities::wif_to_key(wallet->getPrivateKey(model->getAccount(sender())->memoKey()).toStdString());
|
|
|
|
|
if (!privateKey) return;
|
|
|
|
|
m_op.memo->set_message(*privateKey, public_key_type(model->getAccount(receiver())->memoKey().toStdString()), memo().toStdString());
|
|
|
|
|
Q_EMIT memoChanged();
|
|
|
|
|
}
|