mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
initial commit
This commit is contained in:
commit
c2da007f40
1610 changed files with 398047 additions and 0 deletions
14
keys/CMakeLists.txt
Normal file
14
keys/CMakeLists.txt
Normal file
|
@ -0,0 +1,14 @@
|
|||
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
|
||||
|
||||
set(KEYS_SOURCE
|
||||
keys.cpp
|
||||
encryptor.cpp
|
||||
keys.hpp
|
||||
encryptor.h
|
||||
encryptor.hpp
|
||||
)
|
||||
|
||||
add_library(keys STATIC ${KEYS_SOURCE})
|
||||
|
||||
target_include_directories(keys PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
|
||||
target_link_libraries(keys PUBLIC tdactor ton_crypto tl_api tl-utils )
|
205
keys/encryptor.cpp
Normal file
205
keys/encryptor.cpp
Normal file
|
@ -0,0 +1,205 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
*/
|
||||
#include "td/utils/crypto.h"
|
||||
#include "td/utils/overloaded.h"
|
||||
|
||||
#include "encryptor.h"
|
||||
#include "encryptor.hpp"
|
||||
#include "auto/tl/ton_api.hpp"
|
||||
|
||||
#include "common/status.h"
|
||||
#include "common/errorcode.h"
|
||||
#include "keys.hpp"
|
||||
|
||||
namespace ton {
|
||||
|
||||
td::Result<std::unique_ptr<Encryptor>> Encryptor::create(const ton_api::PublicKey *id) {
|
||||
td::Result<std::unique_ptr<Encryptor>> res;
|
||||
ton_api::downcast_call(
|
||||
*const_cast<ton_api::PublicKey *>(id),
|
||||
td::overloaded([&](const ton_api::pub_unenc &obj) { res = std::make_unique<EncryptorNone>(); },
|
||||
[&](const ton_api::pub_ed25519 &obj) { res = std::make_unique<EncryptorEd25519>(obj.key_); },
|
||||
[&](const ton_api::pub_overlay &obj) { res = std::make_unique<EncryptorOverlay>(); },
|
||||
[&](const ton_api::pub_aes &obj) { res = std::make_unique<EncryptorAES>(obj.key_); }));
|
||||
return res;
|
||||
}
|
||||
|
||||
td::Result<std::unique_ptr<Decryptor>> Decryptor::create(const ton_api::PrivateKey *id) {
|
||||
td::Result<std::unique_ptr<Decryptor>> res;
|
||||
ton_api::downcast_call(
|
||||
*const_cast<ton_api::PrivateKey *>(id),
|
||||
td::overloaded([&](const ton_api::pk_unenc &obj) { res = std::make_unique<DecryptorNone>(); },
|
||||
[&](const ton_api::pk_ed25519 &obj) { res = std::make_unique<DecryptorEd25519>(obj.key_); },
|
||||
[&](const ton_api::pk_overlay &obj) { res = std::make_unique<DecryptorFail>(); },
|
||||
[&](const ton_api::pk_aes &obj) { res = std::make_unique<DecryptorAES>(obj.key_); }));
|
||||
return res;
|
||||
}
|
||||
|
||||
td::Result<td::BufferSlice> EncryptorEd25519::encrypt(td::Slice data) {
|
||||
TRY_RESULT_PREFIX(pk, td::Ed25519::generate_private_key(), "failed to generate private key: ");
|
||||
TRY_RESULT_PREFIX(pubkey, pk.get_public_key(), "failed to get public key from private: ");
|
||||
auto pubkey_str = pubkey.as_octet_string();
|
||||
|
||||
td::BufferSlice msg(pubkey_str.size() + 32 + data.size());
|
||||
td::MutableSlice slice = msg.as_slice();
|
||||
slice.copy_from(pubkey_str);
|
||||
slice.remove_prefix(pubkey_str.size());
|
||||
|
||||
TRY_RESULT_PREFIX(shared_secret, td::Ed25519::compute_shared_secret(pub_, pk), "failed to compute shared secret: ");
|
||||
|
||||
td::MutableSlice digest = slice.substr(0, 32);
|
||||
slice.remove_prefix(32);
|
||||
td::sha256(data, digest);
|
||||
|
||||
td::SecureString key(32);
|
||||
{
|
||||
auto S = key.as_mutable_slice();
|
||||
S.copy_from(td::Slice(shared_secret).truncate(16));
|
||||
S.remove_prefix(16);
|
||||
S.copy_from(digest.copy().remove_prefix(16).truncate(16));
|
||||
}
|
||||
|
||||
td::SecureString iv(16);
|
||||
{
|
||||
auto S = iv.as_mutable_slice();
|
||||
S.copy_from(digest.copy().truncate(4));
|
||||
S.remove_prefix(4);
|
||||
S.copy_from(td::Slice(shared_secret).remove_prefix(20).truncate(12));
|
||||
}
|
||||
|
||||
td::AesCtrState ctr;
|
||||
ctr.init(key, iv);
|
||||
ctr.encrypt(data, slice);
|
||||
|
||||
return std::move(msg);
|
||||
}
|
||||
|
||||
td::Status EncryptorEd25519::check_signature(td::Slice message, td::Slice signature) {
|
||||
return td::status_prefix(pub_.verify_signature(message, signature), "bad signature: ");
|
||||
}
|
||||
|
||||
td::Result<td::BufferSlice> DecryptorEd25519::decrypt(td::Slice data) {
|
||||
if (data.size() < td::Ed25519::PublicKey::LENGTH + 32) {
|
||||
return td::Status::Error(ErrorCode::protoviolation, "message is too short");
|
||||
}
|
||||
|
||||
td::Slice pub = data.substr(0, td::Ed25519::PublicKey::LENGTH);
|
||||
data.remove_prefix(td::Ed25519::PublicKey::LENGTH);
|
||||
|
||||
td::Slice digest = data.substr(0, 32);
|
||||
data.remove_prefix(32);
|
||||
|
||||
TRY_RESULT_PREFIX(shared_secret,
|
||||
td::Ed25519::compute_shared_secret(td::Ed25519::PublicKey(td::SecureString(pub)), pk_),
|
||||
"failed to generate shared secret: ");
|
||||
|
||||
td::SecureString key(32);
|
||||
key.as_mutable_slice().copy_from(td::Slice(shared_secret).substr(0, 16));
|
||||
key.as_mutable_slice().substr(16).copy_from(digest.substr(16, 16));
|
||||
|
||||
td::SecureString iv(16);
|
||||
iv.as_mutable_slice().copy_from(digest.substr(0, 4));
|
||||
iv.as_mutable_slice().substr(4).copy_from(td::Slice(shared_secret).substr(20, 12));
|
||||
|
||||
td::BufferSlice res(data.size());
|
||||
|
||||
td::AesCtrState ctr;
|
||||
ctr.init(key, iv);
|
||||
ctr.encrypt(data, res.as_slice());
|
||||
|
||||
td::UInt256 real_digest;
|
||||
td::sha256(res.as_slice(), as_slice(real_digest));
|
||||
|
||||
if (as_slice(real_digest) != digest) {
|
||||
return td::Status::Error(ErrorCode::protoviolation, "sha256 mismatch after decryption");
|
||||
}
|
||||
|
||||
return std::move(res);
|
||||
}
|
||||
|
||||
td::Result<td::BufferSlice> DecryptorEd25519::sign(td::Slice data) {
|
||||
TRY_RESULT_PREFIX(signature, pk_.sign(data), "failed to sign: ");
|
||||
return td::BufferSlice(signature);
|
||||
}
|
||||
|
||||
td::Result<td::BufferSlice> EncryptorAES::encrypt(td::Slice data) {
|
||||
td::BufferSlice msg(32 + data.size());
|
||||
td::MutableSlice slice = msg.as_slice();
|
||||
|
||||
td::MutableSlice digest = slice.substr(0, 32);
|
||||
slice.remove_prefix(32);
|
||||
td::sha256(data, digest);
|
||||
|
||||
td::SecureString key(32);
|
||||
key.as_mutable_slice().copy_from(shared_secret_.as_slice().substr(0, 16));
|
||||
key.as_mutable_slice().substr(16).copy_from(digest.substr(16, 16));
|
||||
|
||||
td::SecureString iv(16);
|
||||
iv.as_mutable_slice().copy_from(digest.substr(0, 4));
|
||||
iv.as_mutable_slice().substr(4).copy_from(shared_secret_.as_slice().substr(20, 12));
|
||||
|
||||
td::AesCtrState ctr;
|
||||
ctr.init(key, iv);
|
||||
ctr.encrypt(data, slice);
|
||||
|
||||
return std::move(msg);
|
||||
}
|
||||
|
||||
td::Result<td::BufferSlice> DecryptorAES::decrypt(td::Slice data) {
|
||||
if (data.size() < 32) {
|
||||
return td::Status::Error(ErrorCode::protoviolation, "message is too short");
|
||||
}
|
||||
|
||||
td::Slice digest = data.substr(0, 32);
|
||||
data.remove_prefix(32);
|
||||
|
||||
td::SecureString key(32);
|
||||
key.as_mutable_slice().copy_from(shared_secret_.as_slice().substr(0, 16));
|
||||
key.as_mutable_slice().substr(16).copy_from(digest.substr(16, 16));
|
||||
|
||||
td::SecureString iv(16);
|
||||
iv.as_mutable_slice().copy_from(digest.substr(0, 4));
|
||||
iv.as_mutable_slice().substr(4).copy_from(shared_secret_.as_slice().substr(20, 12));
|
||||
|
||||
td::BufferSlice res(data.size());
|
||||
|
||||
td::AesCtrState ctr;
|
||||
ctr.init(key, iv);
|
||||
ctr.encrypt(data, res.as_slice());
|
||||
|
||||
td::UInt256 real_digest;
|
||||
td::sha256(res.as_slice(), as_slice(real_digest));
|
||||
|
||||
if (as_slice(real_digest) != digest) {
|
||||
return td::Status::Error(ErrorCode::protoviolation, "sha256 mismatch after decryption");
|
||||
}
|
||||
|
||||
return std::move(res);
|
||||
}
|
||||
|
||||
std::vector<td::Result<td::BufferSlice>> Decryptor::sign_batch(std::vector<td::Slice> data) {
|
||||
std::vector<td::Result<td::BufferSlice>> r;
|
||||
r.resize(data.size());
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
r[i] = sign(data[i]);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
} // namespace ton
|
109
keys/encryptor.h
Normal file
109
keys/encryptor.h
Normal file
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "td/actor/actor.h"
|
||||
#include "td/utils/buffer.h"
|
||||
#include "td/utils/Status.h"
|
||||
#include "td/actor/PromiseFuture.h"
|
||||
#include "auto/tl/ton_api.h"
|
||||
|
||||
namespace ton {
|
||||
|
||||
class Encryptor {
|
||||
public:
|
||||
virtual td::Result<td::BufferSlice> encrypt(td::Slice data) = 0;
|
||||
virtual td::Status check_signature(td::Slice message, td::Slice signature) = 0;
|
||||
virtual ~Encryptor() = default;
|
||||
static td::Result<std::unique_ptr<Encryptor>> create(const ton_api::PublicKey *id);
|
||||
};
|
||||
|
||||
class Decryptor {
|
||||
public:
|
||||
virtual td::Result<td::BufferSlice> decrypt(td::Slice data) = 0;
|
||||
virtual td::Result<td::BufferSlice> sign(td::Slice data) = 0;
|
||||
virtual std::vector<td::Result<td::BufferSlice>> sign_batch(std::vector<td::Slice> data);
|
||||
virtual ~Decryptor() = default;
|
||||
static td::Result<std::unique_ptr<Decryptor>> create(const ton_api::PrivateKey *id);
|
||||
};
|
||||
|
||||
class EncryptorAsync : public td::actor::Actor {
|
||||
private:
|
||||
std::unique_ptr<Encryptor> encryptor_;
|
||||
|
||||
public:
|
||||
EncryptorAsync(std::unique_ptr<Encryptor> encryptor) : encryptor_(std::move(encryptor)) {
|
||||
}
|
||||
void check_signature(td::BufferSlice data, td::BufferSlice signature, td::Promise<td::Unit> promise) {
|
||||
auto res = encryptor_->check_signature(data.as_slice(), signature.as_slice());
|
||||
if (res.is_ok()) {
|
||||
promise.set_value(td::Unit());
|
||||
} else {
|
||||
promise.set_error(res.move_as_error());
|
||||
}
|
||||
}
|
||||
void encrypt(td::BufferSlice data, td::Promise<td::BufferSlice> promise) {
|
||||
promise.set_result(encryptor_->encrypt(data.as_slice()));
|
||||
}
|
||||
template <class T>
|
||||
static td::Result<td::actor::ActorOwn<EncryptorAsync>> create(T &id) {
|
||||
TRY_RESULT(d, Encryptor::create(id));
|
||||
return td::actor::create_actor<EncryptorAsync>("encryptor", std::move(d));
|
||||
}
|
||||
template <class T>
|
||||
static td::Result<td::actor::ActorOwn<EncryptorAsync>> create(T *id) {
|
||||
TRY_RESULT(d, Encryptor::create(id));
|
||||
return td::actor::create_actor<EncryptorAsync>("encryptor", std::move(d));
|
||||
}
|
||||
};
|
||||
|
||||
class DecryptorAsync : public td::actor::Actor {
|
||||
private:
|
||||
std::unique_ptr<Decryptor> decryptor_;
|
||||
|
||||
public:
|
||||
DecryptorAsync(std::unique_ptr<Decryptor> decryptor) : decryptor_(std::move(decryptor)) {
|
||||
}
|
||||
auto decrypt(td::BufferSlice data) {
|
||||
return decryptor_->decrypt(data.as_slice());
|
||||
}
|
||||
auto sign(td::BufferSlice data) {
|
||||
return decryptor_->sign(data.as_slice());
|
||||
}
|
||||
auto sign_batch(std::vector<td::BufferSlice> data) {
|
||||
std::vector<td::Slice> v;
|
||||
v.resize(data.size());
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
v[i] = data[i].as_slice();
|
||||
}
|
||||
return decryptor_->sign_batch(v);
|
||||
}
|
||||
template <class T>
|
||||
static td::Result<td::actor::ActorOwn<DecryptorAsync>> create(T &id) {
|
||||
TRY_RESULT(d, Decryptor::create(id));
|
||||
return td::actor::create_actor<DecryptorAsync>("decryptor", std::move(d));
|
||||
}
|
||||
template <class T>
|
||||
static td::Result<td::actor::ActorOwn<DecryptorAsync>> create(T *id) {
|
||||
TRY_RESULT(d, Decryptor::create(id));
|
||||
return td::actor::create_actor<DecryptorAsync>("decryptor", std::move(d));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ton
|
154
keys/encryptor.hpp
Normal file
154
keys/encryptor.hpp
Normal file
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "encryptor.h"
|
||||
#include "crypto/Ed25519.h"
|
||||
#include "auto/tl/ton_api.h"
|
||||
#include "tl-utils/tl-utils.hpp"
|
||||
|
||||
namespace ton {
|
||||
|
||||
class EncryptorNone : public Encryptor {
|
||||
public:
|
||||
td::Result<td::BufferSlice> encrypt(td::Slice data) override {
|
||||
return td::BufferSlice(data);
|
||||
}
|
||||
td::Status check_signature(td::Slice message, td::Slice signature) override {
|
||||
return td::Status::OK();
|
||||
}
|
||||
|
||||
EncryptorNone() {
|
||||
}
|
||||
};
|
||||
|
||||
class DecryptorNone : public Decryptor {
|
||||
public:
|
||||
td::Result<td::BufferSlice> decrypt(td::Slice data) override {
|
||||
return td::BufferSlice(data);
|
||||
}
|
||||
td::Result<td::BufferSlice> sign(td::Slice data) override {
|
||||
return td::BufferSlice("");
|
||||
}
|
||||
DecryptorNone() {
|
||||
}
|
||||
};
|
||||
|
||||
class EncryptorFail : public Encryptor {
|
||||
public:
|
||||
td::Result<td::BufferSlice> encrypt(td::Slice data) override {
|
||||
return td::Status::Error("Fail encryptor");
|
||||
}
|
||||
td::Status check_signature(td::Slice message, td::Slice signature) override {
|
||||
return td::Status::Error("Fail encryptor");
|
||||
}
|
||||
|
||||
EncryptorFail() {
|
||||
}
|
||||
};
|
||||
|
||||
class DecryptorFail : public Decryptor {
|
||||
public:
|
||||
td::Result<td::BufferSlice> decrypt(td::Slice data) override {
|
||||
return td::Status::Error("Fail decryptor");
|
||||
}
|
||||
td::Result<td::BufferSlice> sign(td::Slice data) override {
|
||||
return td::Status::Error("Fail decryptor");
|
||||
}
|
||||
DecryptorFail() {
|
||||
}
|
||||
};
|
||||
|
||||
class EncryptorEd25519 : public Encryptor {
|
||||
private:
|
||||
td::Ed25519::PublicKey pub_;
|
||||
|
||||
public:
|
||||
td::Result<td::BufferSlice> encrypt(td::Slice data) override;
|
||||
td::Status check_signature(td::Slice message, td::Slice signature) override;
|
||||
|
||||
EncryptorEd25519(td::Bits256 key) : pub_(td::SecureString(as_slice(key))) {
|
||||
}
|
||||
};
|
||||
|
||||
class DecryptorEd25519 : public Decryptor {
|
||||
private:
|
||||
td::Ed25519::PrivateKey pk_;
|
||||
|
||||
public:
|
||||
td::Result<td::BufferSlice> decrypt(td::Slice data) override;
|
||||
td::Result<td::BufferSlice> sign(td::Slice data) override;
|
||||
DecryptorEd25519(td::Bits256 key) : pk_(td::SecureString(as_slice(key))) {
|
||||
}
|
||||
};
|
||||
|
||||
class EncryptorOverlay : public Encryptor {
|
||||
public:
|
||||
EncryptorOverlay() {
|
||||
}
|
||||
td::Result<td::BufferSlice> encrypt(td::Slice data) override {
|
||||
return td::Status::Error("overlay id can not be used for encryption");
|
||||
}
|
||||
td::Status check_signature(td::Slice message, td::Slice signature) override {
|
||||
auto R = fetch_tl_object<ton_api::dht_keyDescription>(message, true);
|
||||
if (R.is_error()) {
|
||||
return R.move_as_error();
|
||||
}
|
||||
if (signature.size() > 0) {
|
||||
return td::Status::Error("overlay signature must be empty");
|
||||
}
|
||||
auto G = R.move_as_ok();
|
||||
if (G->update_rule_->get_id() != ton_api::dht_updateRule_overlayNodes::ID) {
|
||||
return td::Status::Error("overlay update rule should be 'overlayNodes'");
|
||||
}
|
||||
if (G->signature_.size() > 0) {
|
||||
return td::Status::Error("overlay signature must be empty");
|
||||
}
|
||||
return td::Status::OK();
|
||||
}
|
||||
};
|
||||
|
||||
class EncryptorAES : public Encryptor {
|
||||
private:
|
||||
td::Bits256 shared_secret_;
|
||||
|
||||
public:
|
||||
td::Result<td::BufferSlice> encrypt(td::Slice data) override;
|
||||
td::Status check_signature(td::Slice message, td::Slice signature) override {
|
||||
return td::Status::Error("can no sign channel messages");
|
||||
}
|
||||
|
||||
EncryptorAES(td::Bits256 shared_secret) : shared_secret_(shared_secret) {
|
||||
}
|
||||
};
|
||||
|
||||
class DecryptorAES : public Decryptor {
|
||||
private:
|
||||
td::Bits256 shared_secret_;
|
||||
|
||||
public:
|
||||
td::Result<td::BufferSlice> decrypt(td::Slice data) override;
|
||||
td::Result<td::BufferSlice> sign(td::Slice data) override {
|
||||
return td::Status::Error("can no sign channel messages");
|
||||
}
|
||||
DecryptorAES(td::Bits256 shared_secret) : shared_secret_(shared_secret) {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ton
|
198
keys/keys.cpp
Normal file
198
keys/keys.cpp
Normal file
|
@ -0,0 +1,198 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
*/
|
||||
#include "keys.hpp"
|
||||
#include "auto/tl/ton_api.hpp"
|
||||
#include "td/utils/overloaded.h"
|
||||
#include "tl-utils/tl-utils.hpp"
|
||||
#include "encryptor.h"
|
||||
#include "crypto/Ed25519.h"
|
||||
|
||||
namespace ton {
|
||||
|
||||
PublicKeyHash::PublicKeyHash(const tl_object_ptr<ton_api::PublicKey> &value) {
|
||||
value_ = get_tl_object_sha_bits256(value);
|
||||
}
|
||||
|
||||
PublicKey::PublicKey(const tl_object_ptr<ton_api::PublicKey> &id) {
|
||||
ton_api::downcast_call(
|
||||
*const_cast<ton_api::PublicKey *>(id.get()),
|
||||
td::overloaded([&](const ton_api::pub_ed25519 &obj) { this->pub_key_ = pubkeys::Ed25519{obj}; },
|
||||
[&](const ton_api::pub_aes &obj) { this->pub_key_ = pubkeys::AES{obj}; },
|
||||
[&](const ton_api::pub_unenc &obj) { this->pub_key_ = pubkeys::Unenc{obj}; },
|
||||
[&](const ton_api::pub_overlay &obj) { this->pub_key_ = pubkeys::Overlay{obj}; }));
|
||||
}
|
||||
|
||||
PublicKeyHash PublicKey::compute_short_id() const {
|
||||
return PublicKeyHash{get_tl_object_sha_bits256(tl())};
|
||||
}
|
||||
|
||||
td::uint32 PublicKey::serialized_size() const {
|
||||
td::uint32 res = 0;
|
||||
pub_key_.visit([&](auto &obj) { res = obj.serialized_size(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
tl_object_ptr<ton_api::PublicKey> PublicKey::tl() const {
|
||||
tl_object_ptr<ton_api::PublicKey> res;
|
||||
pub_key_.visit([&](auto &obj) { res = obj.tl(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
td::BufferSlice PublicKey::export_as_slice() const {
|
||||
return serialize_tl_object(tl(), true);
|
||||
}
|
||||
|
||||
td::Result<PublicKey> PublicKey::import(td::Slice s) {
|
||||
TRY_RESULT(x, fetch_tl_object<ton_api::PublicKey>(s, true));
|
||||
return PublicKey{x};
|
||||
}
|
||||
|
||||
td::Result<std::unique_ptr<Encryptor>> PublicKey::create_encryptor() const {
|
||||
return Encryptor::create(tl().get());
|
||||
}
|
||||
|
||||
td::Result<td::actor::ActorOwn<EncryptorAsync>> PublicKey::create_encryptor_async() const {
|
||||
return EncryptorAsync::create(tl().get());
|
||||
}
|
||||
|
||||
bool PublicKey::empty() const {
|
||||
return pub_key_.get_offset() == pub_key_.offset<Empty>();
|
||||
}
|
||||
|
||||
tl_object_ptr<ton_api::PublicKey> privkeys::Ed25519::pub_tl() const {
|
||||
td::Ed25519::PrivateKey pkey(td::SecureString(as_slice(data_)));
|
||||
auto r_public_key = pkey.get_public_key();
|
||||
if (r_public_key.is_error()) {
|
||||
return nullptr;
|
||||
} else {
|
||||
auto public_key = r_public_key.ok().as_octet_string();
|
||||
td::Bits256 X;
|
||||
as_slice(X).copy_from(public_key);
|
||||
return create_tl_object<ton_api::pub_ed25519>(X);
|
||||
}
|
||||
}
|
||||
|
||||
pubkeys::Ed25519 privkeys::Ed25519::pub() const {
|
||||
td::Ed25519::PrivateKey pkey(td::SecureString(as_slice(data_)));
|
||||
return pubkeys::Ed25519{pkey.get_public_key().move_as_ok()};
|
||||
}
|
||||
|
||||
privkeys::Ed25519 privkeys::Ed25519::random() {
|
||||
while (true) {
|
||||
auto key = td::Ed25519::generate_private_key();
|
||||
if (key.is_error()) {
|
||||
LOG(WARNING) << "failed to generate private key: " << key.move_as_error();
|
||||
}
|
||||
return Ed25519{key.move_as_ok()};
|
||||
}
|
||||
}
|
||||
|
||||
privkeys::Ed25519::Ed25519(td::Ed25519::PrivateKey key) {
|
||||
auto s = key.as_octet_string();
|
||||
CHECK(s.length() == 32);
|
||||
data_.as_slice().copy_from(td::Slice(s));
|
||||
}
|
||||
|
||||
pubkeys::Ed25519::Ed25519(td::Ed25519::PublicKey key) {
|
||||
auto s = key.as_octet_string();
|
||||
CHECK(s.length() == 32);
|
||||
data_.as_slice().copy_from(td::Slice(s));
|
||||
}
|
||||
|
||||
PrivateKey::PrivateKey(const tl_object_ptr<ton_api::PrivateKey> &id) {
|
||||
ton_api::downcast_call(
|
||||
*const_cast<ton_api::PrivateKey *>(id.get()),
|
||||
td::overloaded([&](const ton_api::pk_ed25519 &obj) { this->priv_key_ = privkeys::Ed25519{obj}; },
|
||||
[&](const ton_api::pk_aes &obj) { this->priv_key_ = privkeys::AES{obj}; },
|
||||
[&](const ton_api::pk_unenc &obj) { this->priv_key_ = privkeys::Unenc{obj}; },
|
||||
[&](const ton_api::pk_overlay &obj) { this->priv_key_ = privkeys::Overlay{obj}; }));
|
||||
}
|
||||
|
||||
bool PrivateKey::empty() const {
|
||||
return priv_key_.get_offset() == priv_key_.offset<Empty>();
|
||||
}
|
||||
|
||||
PublicKey PrivateKey::compute_public_key() const {
|
||||
PublicKey res;
|
||||
priv_key_.visit([&](auto &obj) { res = obj.pub(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
PublicKeyHash PrivateKey::compute_short_id() const {
|
||||
tl_object_ptr<ton_api::PublicKey> res;
|
||||
priv_key_.visit([&](auto &obj) { res = obj.pub_tl(); });
|
||||
return PublicKeyHash{res};
|
||||
}
|
||||
|
||||
td::SecureString PrivateKey::export_as_slice() const {
|
||||
td::SecureString res;
|
||||
priv_key_.visit([&](auto &obj) { res = obj.export_as_slice(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
bool PrivateKey::exportable() const {
|
||||
bool res;
|
||||
priv_key_.visit([&](auto &obj) { res = obj.exportable(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
td::Result<PrivateKey> PrivateKey::import(td::Slice s) {
|
||||
if (s.size() < 4) {
|
||||
return td::Status::Error(ErrorCode::protoviolation, "too short key");
|
||||
}
|
||||
td::int32 id;
|
||||
td::MutableSlice{reinterpret_cast<char *>(&id), 4}.copy_from(s.copy().truncate(4));
|
||||
s.remove_prefix(4);
|
||||
switch (id) {
|
||||
case ton_api::pk_ed25519::ID: {
|
||||
auto R = privkeys::Ed25519::import(s);
|
||||
if (R.is_error()) {
|
||||
return R.move_as_error();
|
||||
} else {
|
||||
return R.move_as_ok();
|
||||
}
|
||||
} break;
|
||||
case ton_api::pk_aes::ID: {
|
||||
auto R = privkeys::AES::import(s);
|
||||
if (R.is_error()) {
|
||||
return R.move_as_error();
|
||||
} else {
|
||||
return R.move_as_ok();
|
||||
}
|
||||
} break;
|
||||
default:
|
||||
return td::Status::Error(ErrorCode::protoviolation, PSTRING() << "unknown magic " << id);
|
||||
}
|
||||
}
|
||||
|
||||
tl_object_ptr<ton_api::PrivateKey> PrivateKey::tl() const {
|
||||
tl_object_ptr<ton_api::PrivateKey> res;
|
||||
priv_key_.visit([&](auto &obj) { res = obj.tl(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
td::Result<std::unique_ptr<Decryptor>> PrivateKey::create_decryptor() const {
|
||||
return Decryptor::create(tl().get());
|
||||
}
|
||||
|
||||
td::Result<td::actor::ActorOwn<DecryptorAsync>> PrivateKey::create_decryptor_async() const {
|
||||
return DecryptorAsync::create(tl().get());
|
||||
}
|
||||
|
||||
} // namespace ton
|
477
keys/keys.hpp
Normal file
477
keys/keys.hpp
Normal file
|
@ -0,0 +1,477 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "td/utils/int_types.h"
|
||||
#include "td/utils/buffer.h"
|
||||
#include "auto/tl/ton_api.h"
|
||||
#include "td/utils/UInt.h"
|
||||
#include "td/utils/Variant.h"
|
||||
#include "td/actor/actor.h"
|
||||
#include "crypto/common/bitstring.h"
|
||||
#include "crypto/Ed25519.h"
|
||||
#include "common/errorcode.h"
|
||||
|
||||
namespace ton {
|
||||
|
||||
class Encryptor;
|
||||
class EncryptorAsync;
|
||||
class Decryptor;
|
||||
class DecryptorAsync;
|
||||
|
||||
class PublicKeyHash {
|
||||
public:
|
||||
explicit PublicKeyHash(td::Bits256 value) : value_(value) {
|
||||
}
|
||||
explicit PublicKeyHash(const tl_object_ptr<ton_api::PublicKey> &value);
|
||||
PublicKeyHash() {
|
||||
}
|
||||
static PublicKeyHash zero() {
|
||||
return PublicKeyHash{td::Bits256::zero()};
|
||||
}
|
||||
explicit PublicKeyHash(td::Slice data) {
|
||||
CHECK(data.size() == 32);
|
||||
value_.as_slice().copy_from(data);
|
||||
}
|
||||
|
||||
td::UInt256 uint256_value() const {
|
||||
td::UInt256 x;
|
||||
x.as_slice().copy_from(value_.as_slice());
|
||||
return x;
|
||||
}
|
||||
td::Bits256 bits256_value() const {
|
||||
return value_;
|
||||
}
|
||||
auto tl() const {
|
||||
return value_;
|
||||
}
|
||||
|
||||
bool operator<(const PublicKeyHash &with) const {
|
||||
return value_ < with.value_;
|
||||
}
|
||||
bool operator==(const PublicKeyHash &with) const {
|
||||
return value_ == with.value_;
|
||||
}
|
||||
bool operator!=(const PublicKeyHash &with) const {
|
||||
return value_ != with.value_;
|
||||
}
|
||||
td::Slice as_slice() const {
|
||||
return td::as_slice(value_);
|
||||
}
|
||||
bool is_zero() const {
|
||||
return value_.is_zero();
|
||||
}
|
||||
|
||||
private:
|
||||
td::Bits256 value_;
|
||||
};
|
||||
|
||||
namespace pubkeys {
|
||||
|
||||
class Ed25519 {
|
||||
private:
|
||||
td::Bits256 data_;
|
||||
|
||||
public:
|
||||
Ed25519(const ton_api::pub_ed25519 &obj) {
|
||||
data_ = obj.key_;
|
||||
}
|
||||
Ed25519(td::Bits256 id) : data_(id) {
|
||||
}
|
||||
Ed25519() {
|
||||
}
|
||||
Ed25519(td::Ed25519::PublicKey pk);
|
||||
td::Ed25519::PublicKey export_key() {
|
||||
return td::Ed25519::PublicKey{td::SecureString(data_.as_slice())};
|
||||
}
|
||||
|
||||
auto raw() const {
|
||||
return data_;
|
||||
}
|
||||
td::uint32 serialized_size() const {
|
||||
return 36;
|
||||
}
|
||||
tl_object_ptr<ton_api::pub_ed25519> tl() const {
|
||||
return create_tl_object<ton_api::pub_ed25519>(data_);
|
||||
}
|
||||
bool operator==(const Ed25519 &with) const {
|
||||
return data_ == with.data_;
|
||||
}
|
||||
bool operator!=(const Ed25519 &with) const {
|
||||
return data_ != with.data_;
|
||||
}
|
||||
};
|
||||
|
||||
class AES {
|
||||
private:
|
||||
td::Bits256 data_;
|
||||
|
||||
public:
|
||||
~AES() {
|
||||
data_.set_zero_s();
|
||||
}
|
||||
AES(const ton_api::pub_aes &obj) {
|
||||
data_ = obj.key_;
|
||||
}
|
||||
AES(td::Slice data) {
|
||||
CHECK(data.size() == 32);
|
||||
data_.as_slice().copy_from(data);
|
||||
}
|
||||
AES(td::Bits256 data) : data_(data) {
|
||||
}
|
||||
td::uint32 serialized_size() const {
|
||||
return 36;
|
||||
}
|
||||
tl_object_ptr<ton_api::pub_aes> tl() const {
|
||||
return create_tl_object<ton_api::pub_aes>(data_);
|
||||
}
|
||||
bool operator==(const AES &with) const {
|
||||
return data_ == with.data_;
|
||||
}
|
||||
bool operator!=(const AES &with) const {
|
||||
return data_ != with.data_;
|
||||
}
|
||||
};
|
||||
|
||||
class Unenc {
|
||||
private:
|
||||
td::SharedSlice data_;
|
||||
|
||||
public:
|
||||
Unenc(const ton_api::pub_unenc &obj) {
|
||||
data_ = td::SharedSlice{obj.data_.as_slice()};
|
||||
}
|
||||
Unenc(const Unenc &obj) {
|
||||
data_ = obj.data_.clone();
|
||||
}
|
||||
explicit Unenc(td::BufferSlice data) : data_(td::SharedSlice{data.as_slice()}) {
|
||||
}
|
||||
explicit Unenc(td::Slice data) : data_(td::SharedSlice{data}) {
|
||||
}
|
||||
explicit Unenc(td::SharedSlice data) : data_(std::move(data)) {
|
||||
}
|
||||
td::uint32 serialized_size() const {
|
||||
return static_cast<td::uint32>(data_.size()) + 8;
|
||||
}
|
||||
tl_object_ptr<ton_api::pub_unenc> tl() const {
|
||||
return create_tl_object<ton_api::pub_unenc>(data_.clone_as_buffer_slice());
|
||||
}
|
||||
bool operator==(const Unenc &with) const {
|
||||
return data_.as_slice() == with.data_.as_slice();
|
||||
}
|
||||
bool operator!=(const Unenc &with) const {
|
||||
return data_.as_slice() != with.data_.as_slice();
|
||||
}
|
||||
}; // namespace pubkeys
|
||||
|
||||
class Overlay {
|
||||
private:
|
||||
td::SharedSlice data_;
|
||||
|
||||
public:
|
||||
Overlay(const ton_api::pub_overlay &obj) {
|
||||
data_ = td::SharedSlice{obj.name_.as_slice()};
|
||||
}
|
||||
Overlay(const Overlay &obj) {
|
||||
data_ = obj.data_.clone();
|
||||
}
|
||||
explicit Overlay(td::BufferSlice data) : data_(td::SharedSlice{data.as_slice()}) {
|
||||
}
|
||||
explicit Overlay(td::Slice data) : data_(td::SharedSlice{data}) {
|
||||
}
|
||||
explicit Overlay(td::SharedSlice data) : data_(std::move(data)) {
|
||||
}
|
||||
td::uint32 serialized_size() const {
|
||||
return static_cast<td::uint32>(data_.size()) + 8;
|
||||
}
|
||||
tl_object_ptr<ton_api::pub_overlay> tl() const {
|
||||
return create_tl_object<ton_api::pub_overlay>(data_.clone_as_buffer_slice());
|
||||
}
|
||||
bool operator==(const Overlay &with) const {
|
||||
return data_.as_slice() == with.data_.as_slice();
|
||||
}
|
||||
bool operator!=(const Overlay &with) const {
|
||||
return data_.as_slice() != with.data_.as_slice();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace pubkeys
|
||||
|
||||
class PublicKey {
|
||||
private:
|
||||
class Empty {
|
||||
public:
|
||||
tl_object_ptr<ton_api::PublicKey> tl() const {
|
||||
UNREACHABLE();
|
||||
}
|
||||
td::uint32 serialized_size() const {
|
||||
UNREACHABLE();
|
||||
}
|
||||
bool operator==(const Empty &with) const {
|
||||
return false;
|
||||
}
|
||||
bool operator!=(const Empty &with) const {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
td::Variant<Empty, pubkeys::Ed25519, pubkeys::AES, pubkeys::Unenc, pubkeys::Overlay> pub_key_{Empty{}};
|
||||
|
||||
public:
|
||||
explicit PublicKey(const tl_object_ptr<ton_api::PublicKey> &id);
|
||||
PublicKey() {
|
||||
}
|
||||
PublicKey(pubkeys::Ed25519 pub) : pub_key_(std::move(pub)) {
|
||||
}
|
||||
PublicKey(pubkeys::AES pub) : pub_key_(std::move(pub)) {
|
||||
}
|
||||
PublicKey(pubkeys::Unenc pub) : pub_key_(std::move(pub)) {
|
||||
}
|
||||
PublicKey(pubkeys::Overlay pub) : pub_key_(std::move(pub)) {
|
||||
}
|
||||
|
||||
bool empty() const;
|
||||
|
||||
PublicKeyHash compute_short_id() const;
|
||||
td::uint32 serialized_size() const;
|
||||
tl_object_ptr<ton_api::PublicKey> tl() const;
|
||||
td::BufferSlice export_as_slice() const;
|
||||
static td::Result<PublicKey> import(td::Slice s);
|
||||
|
||||
pubkeys::Ed25519 ed25519_value() const {
|
||||
CHECK(pub_key_.get_offset() == pub_key_.offset<pubkeys::Ed25519>());
|
||||
return pub_key_.get<pubkeys::Ed25519>();
|
||||
}
|
||||
|
||||
td::Result<std::unique_ptr<Encryptor>> create_encryptor() const;
|
||||
td::Result<td::actor::ActorOwn<EncryptorAsync>> create_encryptor_async() const;
|
||||
|
||||
bool operator==(const PublicKey &with) const {
|
||||
return pub_key_ == with.pub_key_;
|
||||
}
|
||||
bool operator!=(const PublicKey &with) const {
|
||||
return !(pub_key_ == with.pub_key_);
|
||||
}
|
||||
};
|
||||
|
||||
namespace privkeys {
|
||||
|
||||
class Ed25519 {
|
||||
private:
|
||||
td::Bits256 data_;
|
||||
|
||||
public:
|
||||
~Ed25519() {
|
||||
data_.set_zero_s();
|
||||
}
|
||||
Ed25519(const ton_api::pk_ed25519 &obj) {
|
||||
data_ = obj.key_;
|
||||
}
|
||||
Ed25519(td::Bits256 id) : data_(id) {
|
||||
id.set_zero_s();
|
||||
}
|
||||
Ed25519(td::Slice data) {
|
||||
CHECK(data.size() == 32);
|
||||
data_.as_slice().copy_from(data);
|
||||
}
|
||||
Ed25519() {
|
||||
}
|
||||
Ed25519(td::Ed25519::PrivateKey pk);
|
||||
td::Ed25519::PrivateKey export_key() {
|
||||
return td::Ed25519::PrivateKey{td::SecureString(data_.as_slice())};
|
||||
}
|
||||
td::SecureString export_as_slice() const {
|
||||
td::SecureString s{36};
|
||||
auto id = ton_api::pk_ed25519::ID;
|
||||
s.as_mutable_slice().copy_from(td::Slice{reinterpret_cast<const td::uint8 *>(&id), 4});
|
||||
s.as_mutable_slice().remove_prefix(4).copy_from(data_.as_slice());
|
||||
return s;
|
||||
}
|
||||
bool exportable() const {
|
||||
return true;
|
||||
}
|
||||
static td::Result<Ed25519> import(td::Slice slice) {
|
||||
if (slice.size() != 32) {
|
||||
return td::Status::Error(ErrorCode::error, "bad length");
|
||||
}
|
||||
return Ed25519{slice};
|
||||
}
|
||||
tl_object_ptr<ton_api::pk_ed25519> tl() const {
|
||||
return create_tl_object<ton_api::pk_ed25519>(data_);
|
||||
}
|
||||
tl_object_ptr<ton_api::PublicKey> pub_tl() const;
|
||||
pubkeys::Ed25519 pub() const;
|
||||
static Ed25519 random();
|
||||
};
|
||||
|
||||
class AES {
|
||||
private:
|
||||
td::Bits256 data_;
|
||||
|
||||
public:
|
||||
~AES() {
|
||||
data_.set_zero_s();
|
||||
}
|
||||
AES(const ton_api::pk_aes &obj) {
|
||||
data_ = obj.key_;
|
||||
}
|
||||
AES(td::Slice data) {
|
||||
CHECK(data.size() == 32);
|
||||
data_.as_slice().copy_from(data);
|
||||
}
|
||||
td::SecureString export_as_slice() const {
|
||||
td::SecureString s{40};
|
||||
auto id = ton_api::pk_aes::ID;
|
||||
s.as_mutable_slice().copy_from(td::Slice{reinterpret_cast<const td::uint8 *>(&id), 4});
|
||||
s.as_mutable_slice().remove_prefix(4).copy_from(data_.as_slice());
|
||||
return s;
|
||||
}
|
||||
bool exportable() const {
|
||||
return true;
|
||||
}
|
||||
static td::Result<AES> import(td::Slice slice) {
|
||||
if (slice.size() != 32) {
|
||||
return td::Status::Error(ErrorCode::error, "bad length");
|
||||
}
|
||||
return AES{slice};
|
||||
}
|
||||
tl_object_ptr<ton_api::pk_aes> tl() const {
|
||||
return create_tl_object<ton_api::pk_aes>(data_);
|
||||
}
|
||||
tl_object_ptr<ton_api::PublicKey> pub_tl() const {
|
||||
return create_tl_object<ton_api::pub_aes>(data_);
|
||||
}
|
||||
pubkeys::AES pub() const {
|
||||
return pubkeys::AES{data_};
|
||||
}
|
||||
};
|
||||
|
||||
class Unenc {
|
||||
private:
|
||||
td::SharedSlice data_;
|
||||
|
||||
public:
|
||||
Unenc(const ton_api::pk_unenc &obj) {
|
||||
data_ = td::SharedSlice{obj.data_.as_slice()};
|
||||
}
|
||||
Unenc(const Unenc &obj) {
|
||||
data_ = obj.data_.clone();
|
||||
}
|
||||
explicit Unenc(td::BufferSlice data) : data_(td::SharedSlice{data.as_slice()}) {
|
||||
}
|
||||
explicit Unenc(td::Slice data) : data_(td::SharedSlice{data}) {
|
||||
}
|
||||
explicit Unenc(td::SharedSlice data) : data_(std::move(data)) {
|
||||
}
|
||||
td::SecureString export_as_slice() const {
|
||||
UNREACHABLE();
|
||||
}
|
||||
bool exportable() const {
|
||||
return false;
|
||||
}
|
||||
tl_object_ptr<ton_api::pk_unenc> tl() const {
|
||||
return create_tl_object<ton_api::pk_unenc>(data_.clone_as_buffer_slice());
|
||||
}
|
||||
tl_object_ptr<ton_api::PublicKey> pub_tl() const {
|
||||
return create_tl_object<ton_api::pub_unenc>(data_.clone_as_buffer_slice());
|
||||
}
|
||||
pubkeys::Unenc pub() const {
|
||||
return pubkeys::Unenc{data_.clone()};
|
||||
}
|
||||
};
|
||||
|
||||
class Overlay {
|
||||
private:
|
||||
td::SharedSlice data_;
|
||||
|
||||
public:
|
||||
Overlay(const ton_api::pk_overlay &obj) {
|
||||
data_ = td::SharedSlice{obj.name_.as_slice()};
|
||||
}
|
||||
Overlay(const Overlay &obj) {
|
||||
data_ = obj.data_.clone();
|
||||
}
|
||||
explicit Overlay(td::BufferSlice data) : data_(td::SharedSlice{data.as_slice()}) {
|
||||
}
|
||||
explicit Overlay(td::Slice data) : data_(td::SharedSlice{data}) {
|
||||
}
|
||||
explicit Overlay(td::SharedSlice data) : data_(std::move(data)) {
|
||||
}
|
||||
td::SecureString export_as_slice() const {
|
||||
UNREACHABLE();
|
||||
}
|
||||
bool exportable() const {
|
||||
return false;
|
||||
}
|
||||
tl_object_ptr<ton_api::pk_overlay> tl() const {
|
||||
return create_tl_object<ton_api::pk_overlay>(data_.clone_as_buffer_slice());
|
||||
}
|
||||
tl_object_ptr<ton_api::PublicKey> pub_tl() const {
|
||||
return create_tl_object<ton_api::pub_overlay>(data_.clone_as_buffer_slice());
|
||||
}
|
||||
pubkeys::Overlay pub() const {
|
||||
return pubkeys::Overlay{data_.clone()};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace privkeys
|
||||
|
||||
class PrivateKey {
|
||||
private:
|
||||
class Empty {
|
||||
public:
|
||||
td::SecureString export_as_slice() const {
|
||||
UNREACHABLE();
|
||||
}
|
||||
bool exportable() const {
|
||||
return false;
|
||||
}
|
||||
tl_object_ptr<ton_api::PrivateKey> tl() const {
|
||||
UNREACHABLE();
|
||||
}
|
||||
tl_object_ptr<ton_api::PublicKey> pub_tl() const {
|
||||
UNREACHABLE();
|
||||
}
|
||||
PublicKey pub() const {
|
||||
UNREACHABLE();
|
||||
}
|
||||
};
|
||||
td::Variant<Empty, privkeys::Ed25519, privkeys::AES, privkeys::Unenc, privkeys::Overlay> priv_key_{Empty{}};
|
||||
|
||||
public:
|
||||
explicit PrivateKey(const tl_object_ptr<ton_api::PrivateKey> &pk);
|
||||
template <class T>
|
||||
PrivateKey(T key) : priv_key_(std::move(key)) {
|
||||
}
|
||||
PrivateKey() {
|
||||
}
|
||||
|
||||
bool empty() const;
|
||||
|
||||
PublicKey compute_public_key() const;
|
||||
PublicKeyHash compute_short_id() const;
|
||||
td::SecureString export_as_slice() const;
|
||||
static td::Result<PrivateKey> import(td::Slice s);
|
||||
bool exportable() const;
|
||||
tl_object_ptr<ton_api::PrivateKey> tl() const;
|
||||
|
||||
td::Result<std::unique_ptr<Decryptor>> create_decryptor() const;
|
||||
td::Result<td::actor::ActorOwn<DecryptorAsync>> create_decryptor_async() const;
|
||||
};
|
||||
|
||||
} // namespace ton
|
Loading…
Add table
Add a link
Reference in a new issue