Skip to content

Commit

Permalink
Unpad memos
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Dec 18, 2023
1 parent 5f86f62 commit 0fd2096
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
20 changes: 16 additions & 4 deletions src/libspark/coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ Coin::Coin(
MintCoinRecipientData r;
r.d = address.get_d();
r.k = k;
r.memo = std::string(padded_memo.begin(), padded_memo.end());
r.memo_length = (uint8_t) memo.size();
r.padded_memo = std::string(padded_memo.begin(), padded_memo.end());
CDataStream r_stream(SER_NETWORK, PROTOCOL_VERSION);
r_stream << r;
this->r_ = AEAD::encrypt(address.get_Q1()*SparkUtils::hash_k(k), "Mint coin data", r_stream);
Expand All @@ -73,7 +74,8 @@ Coin::Coin(
r.v = v;
r.d = address.get_d();
r.k = k;
r.memo = std::string(padded_memo.begin(), padded_memo.end());
r.memo_length = (uint8_t) memo.size();
r.padded_memo = std::string(padded_memo.begin(), padded_memo.end());
CDataStream r_stream(SER_NETWORK, PROTOCOL_VERSION);
r_stream << r;
this->r_ = AEAD::encrypt(address.get_Q1()*SparkUtils::hash_k(k), "Spend coin data", r_stream);
Expand Down Expand Up @@ -131,10 +133,15 @@ IdentifiedCoinData Coin::identify(const IncomingViewKey& incoming_view_key) {
throw std::runtime_error("Unable to identify coin");
}

// Check that the memo length is valid
if (r.memo_length > this->params->get_memo_bytes()) {
throw std::runtime_error("Unable to identify coin");
}

data.d = r.d;
data.v = this->v;
data.k = r.k;
data.memo = r.memo;
data.memo = std::string(r.padded_memo.begin(), r.padded_memo.begin() + r.memo_length);
} else {
SpendCoinRecipientData r;

Expand All @@ -146,10 +153,15 @@ IdentifiedCoinData Coin::identify(const IncomingViewKey& incoming_view_key) {
throw std::runtime_error("Unable to identify coin");
}

// Check that the memo length is valid
if (r.memo_length > this->params->get_memo_bytes()) {
throw std::runtime_error("Unable to identify coin");
}

data.d = r.d;
data.v = r.v;
data.k = r.k;
data.memo = r.memo;
data.memo = std::string(r.padded_memo.begin(), r.padded_memo.begin() + r.memo_length);
}

// Validate the coin
Expand Down
12 changes: 8 additions & 4 deletions src/libspark/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ struct RecoveredCoinData {
struct MintCoinRecipientData {
std::vector<unsigned char> d; // encrypted diversifier
Scalar k; // nonce
std::string memo; // memo
uint8_t memo_length; // memo length
std::string padded_memo; // padded memo

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(d);
READWRITE(k);
READWRITE(memo);
READWRITE(memo_length);
READWRITE(padded_memo);
}
};

Expand All @@ -50,7 +52,8 @@ struct SpendCoinRecipientData {
uint64_t v; // value
std::vector<unsigned char> d; // encrypted diversifier
Scalar k; // nonce
std::string memo; // memo
uint8_t memo_length; // memo length
std::string padded_memo; // padded memo

ADD_SERIALIZE_METHODS;

Expand All @@ -59,7 +62,8 @@ struct SpendCoinRecipientData {
READWRITE(v);
READWRITE(d);
READWRITE(k);
READWRITE(memo);
READWRITE(memo_length);
READWRITE(padded_memo);
}
};

Expand Down
7 changes: 3 additions & 4 deletions src/libspark/test/coin_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ BOOST_AUTO_TEST_CASE(mint_identify_recover)
BOOST_CHECK_EQUAL_COLLECTIONS(i_data.d.begin(), i_data.d.end(), address.get_d().begin(), address.get_d().end());
BOOST_CHECK_EQUAL(i_data.v, v);
BOOST_CHECK_EQUAL(i_data.k, k);
BOOST_CHECK_EQUAL(strcmp(memo.c_str(), i_data.memo.c_str()), 0); // compare strings in a lexicographical manner, as we pad the memo in the coin
BOOST_CHECK_EQUAL(i_data.memo.size(), params->get_memo_bytes()); // check that it is padded
BOOST_CHECK_EQUAL(i_data.memo, memo);

// Recover coin
RecoveredCoinData r_data = coin.recover(full_view_key, i_data);
BOOST_CHECK_EQUAL(
Expand Down Expand Up @@ -105,8 +105,7 @@ BOOST_AUTO_TEST_CASE(spend_identify_recover)
BOOST_CHECK_EQUAL_COLLECTIONS(i_data.d.begin(), i_data.d.end(), address.get_d().begin(), address.get_d().end());
BOOST_CHECK_EQUAL(i_data.v, v);
BOOST_CHECK_EQUAL(i_data.k, k);
BOOST_CHECK_EQUAL(strcmp(memo.c_str(), i_data.memo.c_str()), 0); // compare strings in a lexicographical manner, as we pad the memo in the coin
BOOST_CHECK_EQUAL(i_data.memo.size(), params->get_memo_bytes()); // check that it is padded
BOOST_CHECK_EQUAL(i_data.memo, memo);

// Recover coin
RecoveredCoinData r_data = coin.recover(full_view_key, i_data);
Expand Down

0 comments on commit 0fd2096

Please sign in to comment.