Add encryption of SaplingNotePlaintext and SaplingOutgoingPlaintext classes.
This is part of #3061 to add Sapling note functionality.
This commit is contained in:
@@ -137,3 +137,68 @@ ZCNoteEncryption::Ciphertext SproutNotePlaintext::encrypt(ZCNoteEncryption& encr
|
||||
|
||||
return encryptor.encrypt(pk_enc, pt);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Construct and populate SaplingNotePlaintext for a given note and memo.
|
||||
SaplingNotePlaintext::SaplingNotePlaintext(
|
||||
const SaplingNote& note,
|
||||
std::array<unsigned char, ZC_MEMO_SIZE> memo) : BaseNotePlaintext(note, memo)
|
||||
{
|
||||
d = note.d;
|
||||
rcm = note.r;
|
||||
}
|
||||
|
||||
|
||||
boost::optional<SaplingNote> SaplingNotePlaintext::note(const SaplingIncomingViewingKey& ivk) const
|
||||
{
|
||||
auto addr = ivk.address( d );
|
||||
if (addr) {
|
||||
return SaplingNote(addr.get(), value_);
|
||||
} else {
|
||||
return boost::none;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
boost::optional<SaplingNotePlaintextEncryptionResult> SaplingNotePlaintext::encrypt(const uint256& pk_d) const
|
||||
{
|
||||
// Get the encryptor
|
||||
auto sne = SaplingNoteEncryption::FromDiversifier(d);
|
||||
if (!sne) {
|
||||
return boost::none;
|
||||
}
|
||||
auto enc = sne.get();
|
||||
|
||||
// Create the plaintext
|
||||
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
||||
ss << (*this);
|
||||
SaplingEncPlaintext pt;
|
||||
assert(pt.size() == ss.size());
|
||||
memcpy(&pt[0], &ss[0], pt.size());
|
||||
|
||||
// Encrypt the plaintext
|
||||
auto encciphertext = enc.encrypt_to_recipient(pk_d, pt);
|
||||
if (!encciphertext) {
|
||||
return boost::none;
|
||||
}
|
||||
return SaplingNotePlaintextEncryptionResult(encciphertext.get(), enc);
|
||||
}
|
||||
|
||||
|
||||
SaplingOutCiphertext SaplingOutgoingPlaintext::encrypt(
|
||||
const uint256& ovk,
|
||||
const uint256& cv,
|
||||
const uint256& cm,
|
||||
SaplingNoteEncryption& enc
|
||||
) const
|
||||
{
|
||||
// Create the plaintext
|
||||
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
||||
ss << (*this);
|
||||
SaplingOutPlaintext pt;
|
||||
assert(pt.size() == ss.size());
|
||||
memcpy(&pt[0], &ss[0], pt.size());
|
||||
|
||||
return enc.encrypt_to_ourselves(ovk, cv, cm, pt);
|
||||
}
|
||||
|
||||
@@ -116,6 +116,68 @@ public:
|
||||
) const;
|
||||
};
|
||||
|
||||
typedef std::pair<std::reference_wrapper<const SaplingEncCiphertext>, std::reference_wrapper<const SaplingNoteEncryption>> SaplingNotePlaintextEncryptionResult;
|
||||
|
||||
class SaplingNotePlaintext : public BaseNotePlaintext {
|
||||
public:
|
||||
diversifier_t d;
|
||||
uint256 rcm;
|
||||
|
||||
SaplingNotePlaintext() {}
|
||||
|
||||
SaplingNotePlaintext(const SaplingNote& note, std::array<unsigned char, ZC_MEMO_SIZE> memo);
|
||||
|
||||
boost::optional<SaplingNote> note(const SaplingIncomingViewingKey& ivk) const;
|
||||
|
||||
virtual ~SaplingNotePlaintext() {}
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||
unsigned char leadingByte = 0x01;
|
||||
READWRITE(leadingByte);
|
||||
|
||||
if (leadingByte != 0x01) {
|
||||
throw std::ios_base::failure("lead byte of SaplingNotePlaintext is not recognized");
|
||||
}
|
||||
|
||||
READWRITE(d); // 11 bytes
|
||||
READWRITE(value_); // 8 bytes
|
||||
READWRITE(rcm); // 32 bytes
|
||||
READWRITE(memo_); // 512 bytes
|
||||
}
|
||||
|
||||
boost::optional<SaplingNotePlaintextEncryptionResult> encrypt(const uint256& pk_d) const;
|
||||
};
|
||||
|
||||
class SaplingOutgoingPlaintext
|
||||
{
|
||||
public:
|
||||
uint256 pk_d;
|
||||
uint256 esk;
|
||||
|
||||
SaplingOutgoingPlaintext() {};
|
||||
|
||||
SaplingOutgoingPlaintext(uint256 pk_d, uint256 esk) : pk_d(pk_d), esk(esk) {}
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||
READWRITE(pk_d); // 8 bytes
|
||||
READWRITE(esk); // 8 bytes
|
||||
}
|
||||
|
||||
SaplingOutCiphertext encrypt(
|
||||
const uint256& ovk,
|
||||
const uint256& cv,
|
||||
const uint256& cm,
|
||||
SaplingNoteEncryption& enc
|
||||
) const;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif // ZC_NOTE_H_
|
||||
|
||||
Reference in New Issue
Block a user