Skip to content

Commit

Permalink
[resolves trustwallet#260] fix retrieving raw tx
Browse files Browse the repository at this point in the history
  • Loading branch information
Defuera committed May 31, 2019
1 parent cb871d3 commit b5c9816
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
25 changes: 18 additions & 7 deletions src/Aeternity/Signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
using namespace TW;
using namespace Aeternity;

// SignEncodeTx
/// SignEncodeTx
std::string Aeternity::Signer::sign(const TW::PrivateKey &privateKey, Transaction &transaction) {
std::string txPrefix = "tx_";
auto encodedTx = transaction.encode();
auto trimPrefix = encodedTx.substr(txPrefix.size(), encodedTx.size() - 1);
auto txRaw = TW::Base64::decode(trimPrefix); // parse_hex(trimPrefix);
auto txRaw = parseRawTransaction(encodedTx);

/// append networkId and txRaw
auto data = Data();
Expand All @@ -28,11 +26,12 @@ std::string Aeternity::Signer::sign(const TW::PrivateKey &privateKey, Transactio

/// sign
/// ed25519.Sign(account.SigningKey, data)
auto sigRaw = privateKey.sign(data, TWCurveED25519); //todo is different, then go sdk -/
auto sigRaw = privateKey.sign(data, TWCurveED25519); // todo is different, then go sdk -/
auto isValid = privateKey.getPublicKey(TWPublicKeyTypeED25519).verify(sigRaw, data); //todo should it be true?

/// encode the message using rlp
auto objectTagSignedTransaction = 11;
auto rlpMessageVersion = 1; // todo same as Tx
uint8_t objectTagSignedTransaction = 11;
uint8_t rlpMessageVersion = 1; // todo same as in Transaction

auto rlpTxRaw = Data();
append(rlpTxRaw, Ethereum::RLP::encode(objectTagSignedTransaction));
Expand Down Expand Up @@ -67,3 +66,15 @@ std::string TW::Aeternity::Signer::encode(const std::string prefix,

return prefix + TW::Base64::encode(data);
}

Data TW::Aeternity::Signer::parseRawTransaction(std::string transaction) {
std::string txPrefix = "tx_"; // todo get from Tx class

auto trimPrefix = transaction.substr(txPrefix.size(), transaction.size() - 1);
auto txRaw = TW::Base64::decode(trimPrefix);
/// trimChecksum
auto start = txRaw.begin();
auto end = txRaw.begin() + 97;
Data newVec(start, end); // todo this hardcoded 4 should be matched with encode tx hardcoded 4.
return newVec;
}
9 changes: 4 additions & 5 deletions src/Aeternity/Signer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.


#include "Transaction.h"
#include <PrivateKey.h>

static std::string networkId = "ae_mainnet"; //todo can I hardcore this one?
static std::string networkId = "ae_mainnet"; // todo can I hardcode this one?

namespace TW::Aeternity {

class Signer {
public:

/// Signs the given transaction.
static std::string sign(const PrivateKey &privateKey, Transaction &transaction);

static std::string encode(std::string basicString, const Data& vector);
static std::string encode(std::string basicString, const Data &vector);

static Data parseRawTransaction(std::string transaction);

private:
};

} // namespace TW::Aeternity

5 changes: 2 additions & 3 deletions tests/Aeternity/SignerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

using namespace TW;
using namespace TW::Aeternity;
//auto address = Aeternity::Address("0x4646464646464646464646464646464646464646464646464646464646464646");

TEST(AeternitySigner, Sign) {
std::string sender_id = "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi";
Expand All @@ -25,10 +24,10 @@ TEST(AeternitySigner, Sign) {
uint64_t nonce = 49;

auto transaction = Transaction(sender_id, recipient_id, amount, fee, payload, ttl, nonce);
auto privateKey = PrivateKey(parse_hex("46464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646"));
auto privateKey = PrivateKey(parse_hex("4646464646464646464646464646464646464646464646464646464646464646"));

auto signature = Signer::sign(privateKey, transaction);

EXPECT_EQ(signature, "sg_DjHWizZn8aA5Mnu5A9y7WvK1xYyZQeNmoZSVy11faj9uTN1dJvjTXoYrFqZVdz6YJGHnvEyYoyaUhWPX2RGRVZLG7DnEf");
EXPECT_EQ(signature, "sg_CwTqP+LAnMNKoxDkq4FuOQ8LVte4EjYuOrCnbWtCREUw+WMqapVGiL9V6CO9F93gcIt9YNehmYdmcB3XJ9pSAYnrNz4=");
}

0 comments on commit b5c9816

Please sign in to comment.