Skip to content

Commit

Permalink
Deterministic identity, other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
roshanr95 committed Dec 6, 2020
1 parent 5e65b62 commit 3694f44
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
4 changes: 2 additions & 2 deletions integrations/near/include/marlin/near/NearTransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void NearTransport<DelegateType>::did_recv(
core::Buffer message(buf, buf_size);
buf = nullptr;
message.cover_unsafe(4);
delegate->did_recv_message(*this, std::move(message));
delegate->did_recv(*this, std::move(message));
state = State::Idle;
bytes_remaining = 0;
buf_size = 0;
Expand All @@ -162,4 +162,4 @@ void NearTransport<DelegateType>::did_recv(
} // near
} // marlin

#endif // MARLIN_NEAR_NEARTRANSPORT_HPP
#endif // MARLIN_NEAR_NEARTRANSPORT_HPP
60 changes: 39 additions & 21 deletions integrations/near/include/marlin/near/OnRampNear.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <marlin/near/NearTransport.hpp>
#include <cryptopp/blake2.h>
#include <libbase58.h>
#include <boost/filesystem.hpp>

using namespace marlin::near;
using namespace marlin::core;
Expand All @@ -19,32 +20,45 @@ class OnRampNear {
public:
DefaultMulticastClient<OnRampNear> multicastClient;
uint8_t static_sk[crypto_sign_SECRETKEYBYTES], static_pk[crypto_sign_PUBLICKEYBYTES]; // should be moved to DefaultMulticastClient and add a function there to sign a message.
NearTransport <OnRampNear> *nearTransport = nullptr;
NearTransportFactory <OnRampNear, OnRampNear> f;
NearTransport<OnRampNear> *nearTransport = nullptr;
NearTransportFactory<OnRampNear, OnRampNear> f;

void handle_handshake(core::Buffer &&message);
void handle_transaction(core::Buffer &&message);
void handle_block(core::Buffer &&message);

OnRampNear(DefaultMulticastClientOptions clop): multicastClient(clop) {
multicastClient.delegate = this;
memcpy(static_sk, clop.static_sk, crypto_sign_SECRETKEYBYTES);
memcpy(static_pk, clop.static_pk, crypto_sign_PUBLICKEYBYTES);

if(sodium_init() == -1) {
throw;
}
if(boost::filesystem::exists("./.marlin/keys/near_gateway")) {
std::ifstream sk("./.marlin/keys/near_gateway", std::ios::binary);
if(!sk.read((char *)static_sk, crypto_sign_SECRETKEYBYTES)) {
throw;
}
crypto_sign_ed25519_sk_to_pk(static_pk, static_sk);
} else {
crypto_sign_keypair(static_pk, static_sk);

boost::filesystem::create_directories("./.marlin/keys/");
std::ofstream sk("./.marlin/keys/near_gateway", std::ios::binary);

sk.write((char *)static_sk, crypto_sign_SECRETKEYBYTES);
}

char b58[65];
size_t sz = 65;
SPDLOG_DEBUG(
"PrivateKey: {} \n PublicKey: {}",
spdlog::to_hex(clop.static_sk, clop.static_sk + crypto_sign_SECRETKEYBYTES),
spdlog::to_hex(clop.static_pk, clop.static_pk + 32)
spdlog::to_hex(static_sk, static_sk + crypto_sign_SECRETKEYBYTES),
spdlog::to_hex(static_pk, static_pk + crypto_sign_PUBLICKEYBYTES)
);

if(b58enc(b58, &sz, clop.static_pk, 32)) {
if(b58enc(b58, &sz, static_pk, 32)) {
SPDLOG_INFO(
"{}",
"Node identity: {}",
b58
);
} else {
Expand All @@ -55,25 +69,29 @@ class OnRampNear {
f.listen(*this);
}

void did_recv_message(NearTransport <OnRampNear> &, Buffer &&message) {
SPDLOG_INFO(
"msgSize: {}; Message received from NearTransport: {}",
void did_recv(NearTransport <OnRampNear> &, Buffer &&message) {
SPDLOG_DEBUG(
"Message received from Near: {} bytes: {}",
message.size(),
spdlog::to_hex(message.data(), message.data() + message.size())
);
SPDLOG_INFO(
"Message received from Near: {} bytes",
message.size()
);
if(message.data()[0] == 0x10) {
handle_handshake(std::move(message));
} else if(message.data()[0] == 0xc) {
handle_transaction(std::move(message));
} else if(message.data()[0] == 0xb) {
handle_block(std::move(message));
} else if(message.data()[0] == 0xd) {
SPDLOG_INFO(
SPDLOG_DEBUG(
"This is a RoutedMessage"
);
} else {
SPDLOG_INFO(
"Something else: {}",
SPDLOG_WARN(
"Unhandled: {}",
message.data()[0]
);
}
Expand All @@ -88,16 +106,16 @@ class OnRampNear {
uint64_t
) {
SPDLOG_DEBUG(
"OnRampNear:: did_recv_message, forwarding message: {}",
"OnRampNear:: did_recv, forwarding message: {}",
spdlog::to_hex(bytes.data(), bytes.size())
);
if(nearTransport != nullptr) {
nearTransport->send(std::move(bytes));
}
}

void did_send_message(NearTransport<OnRampNear> &, Buffer &&message) {
SPDLOG_INFO(
void did_send_message(NearTransport<OnRampNear> &, Buffer &&message [[maybe_unused]]) {
SPDLOG_DEBUG(
"Transport: Did send message: {} bytes",
// transport.src_addr.to_string(),
// transport.dst_addr.to_string(),
Expand Down Expand Up @@ -153,7 +171,7 @@ void OnRampNear::handle_transaction(core::Buffer &&message) {
}

void OnRampNear::handle_block(core::Buffer &&message) {
SPDLOG_INFO("Handling block");
SPDLOG_DEBUG("Handling block");
message.uncover_unsafe(4);
CryptoPP::BLAKE2b blake2b((uint)8);
blake2b.Update((uint8_t *)message.data(), message.size());
Expand All @@ -168,7 +186,7 @@ void OnRampNear::handle_block(core::Buffer &&message) {
}

void OnRampNear::handle_handshake(core::Buffer &&message) {
SPDLOG_INFO("Replying");
SPDLOG_DEBUG("Replying");
uint8_t *buf = message.data();
uint32_t buf_size = message.size();
std::swap_ranges(buf + 9, buf + 42, buf + 42);
Expand All @@ -192,10 +210,10 @@ void OnRampNear::handle_handshake(core::Buffer &&message) {
uint8_t *near_node_signature = buf + buf_size - crypto_sign_BYTES;

if(crypto_sign_verify_detached(near_node_signature, hashed_message, 32, buf + near_key_offset + 1) != 0) {
SPDLOG_INFO("Signature verification failed");
SPDLOG_ERROR("Signature verification failed");
return;
} else {
SPDLOG_INFO("Signature verified successfully");
SPDLOG_DEBUG("Signature verified successfully");
}

uint8_t *mySignature = buf + buf_size - 64;
Expand Down

0 comments on commit 3694f44

Please sign in to comment.