Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: utils #133

Merged
merged 3 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/script_desktop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ cd ../examples/cmake_example
chmod +x ./build.sh
./build.sh

# run Gtest
# # run Gtest
cd ../../build
./test/Ark-Cpp-Crypto-tests
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- refactored utils implementation ([#133])
- refactored Identities implementation. ([#120])

## [0.6.0-arduino] - 2019-07-16
Expand Down Expand Up @@ -118,3 +119,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[#69]: https://github.com/ArkEcosystem/cpp-crypto/pull/69
[#70]: https://github.com/ArkEcosystem/cpp-crypto/pull/70
[#120]: https://github.com/ArkEcosystem/cpp-crypto/pull/120
[#133]: https://github.com/ArkEcosystem/cpp-crypto/pull/133
2 changes: 1 addition & 1 deletion docs/INSTALL_ARDUINO.MD
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void setup()


const auto text = "Computer science is no more about computers than astronomy is about telescopes.";
Ark::Crypto::Utils::Message message;
Ark::Crypto::Message message;
message.sign(text, passphrase);
Serial.println(BytesToHex(message.signature).c_str());
}
Expand Down
8 changes: 4 additions & 4 deletions docs/cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const auto MyBridgechainTransaction = Builder::buildTransfer(
const auto text = "Computer science is no more about computers than astronomy is about telescopes.";
const auto passphrase = "viable weasel wage promote praise inflict jaguar tackle color unusual exclude direct";

Ark::Crypto::Utils::Message message;
Ark::Crypto::Message message;
message.sign(text, passphrase);
```

Expand All @@ -119,7 +119,7 @@ message.sign(text, passphrase);
```cpp
const auto text = "Computer science is no more about computers than astronomy is about telescopes.";
const auto passphrase = "bullet parade snow bacon mutual deposit brass floor staff list concert ask";
Ark::Crypto::Utils::Message message;
Ark::Crypto::Message message;
message.sign(text, passphrase);
```

Expand All @@ -130,7 +130,7 @@ const auto text = "Computer science is no more about computers than astronomy is
PublicKey publicKey = PublicKey::fromHex("0275776018638e5c40f1b922901e96cac2caa734585ef302b4a2801ee9a338a456");
std::vector<uint8_t> signature = HexToBytes("3044022021704f2adb2e4a10a3ddc1d7d64552b8061c05f6d12a168c69091c75581d611402200edf37689d2786fc690af9f0f6fa1f629c95695039f648a6d455484302402e93");

Ark::Crypto::Utils::Message message(
Ark::Crypto::Message message(
text,
publicKey,
signature
Expand Down Expand Up @@ -260,7 +260,7 @@ void setup()


const auto text = "Computer science is no more about computers than astronomy is about telescopes.";
Ark::Crypto::Utils::Message message;
Ark::Crypto::Message message;
message.sign(text, passphrase);
Serial.println(BytesToHex(message.signature).c_str());
}
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/ESP32/ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void checkCrypto() {
*/
const auto text = "Hello World";
const auto passphrase5 = "this is a top secret passphrase";
Ark::Crypto::Utils::Message message;
Ark::Crypto::Message message;
message.sign(text, passphrase5);
Serial.print("\nSignature from Signed Message: ");
Serial.println(BytesToHex(message.signature).c_str());
Expand Down
29 changes: 20 additions & 9 deletions examples/cmake_example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
#include "arkCrypto.h"
/**
* This file is part of Ark Cpp Crypto.
*
* (c) Ark Ecosystem <info@ark.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
**/

#include <arkCrypto.h>

#include <iostream>
#include <string>
#include <vector>
#include <cstdint>

#include "utils/hex.hpp"

int main(int argc, char* argv[]) {
const auto text = "Computer science is no more about computers than astronomy is about telescopes.";
const auto passphrase = "viable weasel wage promote praise inflict jaguar tackle color unusual exclude direct";

// Message - sign
Ark::Crypto::Utils::Message message;
Ark::Crypto::Message message;
message.sign(text, passphrase);

std::cout << "Message\n";
Expand All @@ -23,9 +34,9 @@ int main(int argc, char* argv[]) {
auto publicKey = PublicKey::fromHex("0275776018638e5c40f1b922901e96cac2caa734585ef302b4a2801ee9a338a456");
auto signature = HexToBytes("3044022021704f2adb2e4a10a3ddc1d7d64552b8061c05f6d12a168c69091c75581d611402200edf37689d2786fc690af9f0f6fa1f629c95695039f648a6d455484302402e93");

message = Ark::Crypto::Utils::Message(
message = Ark::Crypto::Message(
text,
publicKey,
publicKey.toBytes(),
signature
);

Expand All @@ -44,16 +55,16 @@ int main(int argc, char* argv[]) {
std::cout << '\n';

// Address - from publickey
publicKey = PublicKey("029fdf41a7d69d8efc7b236c21b9509a23d862ea4ed8b13a56e31eee58dbfd97b4");
address = Address::fromPublicKey(publicKey, networkVersion);
publicKey = PublicKey::fromHex("029fdf41a7d69d8efc7b236c21b9509a23d862ea4ed8b13a56e31eee58dbfd97b4");
address = Address::fromPublicKey(publicKey.toBytes().data(), networkVersion);
std::cout << "Address from public key\n";
std::cout << "\tPublic Key: " << publicKey.toString() << '\n';
std::cout << "\tAddress: " << address.toString() << '\n';
std::cout << '\n';

// Address - from privatekey
PrivateKey privateKey("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021");
address = Address::fromPrivateKey(privateKey, networkVersion);
PrivateKey privateKey = PrivateKey::fromHex("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021");
address = Address::fromPrivateKey(privateKey.toBytes().data(), networkVersion);
std::cout << "Address from private key\n";
std::cout << "\tPrivate Key: " << privateKey.toString() << '\n';
std::cout << "\tAddress: " << address.toString() << '\n';
Expand All @@ -75,7 +86,7 @@ int main(int argc, char* argv[]) {
std::cout << '\n';

// Private Key - object from hex
privateKey = PrivateKey::fromHex("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021");
privateKey = std::move(PrivateKey::fromHex("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021"));
std::cout << "Private Key from hex\n";
std::cout << "\tHex: 950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021\n";
std::cout << "\tPrivate Key: " << privateKey.toString() << '\n';
Expand Down
18 changes: 14 additions & 4 deletions examples/platformio_example/src/main.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#include "arkCrypto.h"
/**
* This file is part of Ark Cpp Crypto.
*
* (c) Ark Ecosystem <info@ark.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
**/

#include <arkCrypto.h>
#include <Arduino.h>

#include <string>
#include <vector>
#include <cstdint>

#include <arduino.h>
#include "utils/hex.hpp"

void setup() {
Serial.begin(115200);
Expand All @@ -18,7 +28,7 @@ void loop() {
const auto passphrase = "this is a top secret passphrase";

// Message - sign
Ark::Crypto::Utils::Message message;
Ark::Crypto::Message message;
message.sign(text, passphrase);

Serial.println("Message");
Expand All @@ -31,7 +41,7 @@ void loop() {
auto publicKey = PublicKey::fromHex("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
auto signature = HexToBytes("304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8");

message = Ark::Crypto::Utils::Message(
message = Ark::Crypto::Message(
text,
publicKey,
signature
Expand Down
12 changes: 12 additions & 0 deletions extras/ARDUINO_IDE.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ EXTRAS_LIB_DIR=${EXTRAS_BACKUP_DIR}/lib
SRC_COMMON_DIR=${SRC_DIR}/common
INCLUDE_COMMON_DIR=${INCLUDE_CPP_CRYPTO_DIR}/common

SRC_CRYPTO_DIR=${SRC_DIR}/crypto
INCLUDE_CRYPTO_DIR=${INCLUDE_CPP_CRYPTO_DIR}/crypto

SRC_DEFAULTS_DIR=${SRC_DIR}/defaults
INCLUDE_DEFAULTS_DIR=${INCLUDE_CPP_CRYPTO_DIR}/defaults

Expand Down Expand Up @@ -101,6 +104,10 @@ if [[ -d ${INCLUDE_DIR} ]]; then
mv ${INCLUDE_COMMON_DIR}/fee_policy.hpp ${SRC_COMMON_DIR}
mv ${INCLUDE_COMMON_DIR}/network.hpp ${SRC_COMMON_DIR}

echo -e "Moving 'crypto' headers.\n"
mv ${INCLUDE_CRYPTO_DIR}/message.hpp ${SRC_CRYPTO_DIR}
mv ${INCLUDE_CRYPTO_DIR}/slot.hpp ${SRC_CRYPTO_DIR}

echo -e "Moving 'defaults' headers.\n"
mv ${INCLUDE_DEFAULTS_DIR}/fee_policies.hpp ${SRC_DEFAULTS_DIR}
mv ${INCLUDE_DEFAULTS_DIR}/static_fees.hpp ${SRC_DEFAULTS_DIR}
Expand Down Expand Up @@ -160,6 +167,7 @@ else
mkdir ${INCLUDE_DIR}
mkdir ${INCLUDE_CPP_CRYPTO_DIR}
mkdir ${INCLUDE_COMMON_DIR}
mkdir ${INCLUDE_CRYPTO_DIR}
mkdir ${INCLUDE_DEFAULTS_DIR}
mkdir ${INCLUDE_IDENTITIES_DIR}
mkdir ${INCLUDE_INTERFACES_DIR}
Expand All @@ -174,6 +182,10 @@ else
mv ${SRC_COMMON_DIR}/fee_policy.hpp ${INCLUDE_COMMON_DIR}
mv ${SRC_COMMON_DIR}/network.hpp ${INCLUDE_COMMON_DIR}

echo -e "Moving 'crypto' headers.\n"
mv ${SRC_CRYPTO_DIR}/message.hpp ${INCLUDE_CRYPTO_DIR}
mv ${SRC_CRYPTO_DIR}/slot.hpp ${INCLUDE_CRYPTO_DIR}

echo -e "Moving 'defaults' headers.\n"
mv ${SRC_DEFAULTS_DIR}/fee_policies.hpp ${INCLUDE_DEFAULTS_DIR}
mv ${SRC_DEFAULTS_DIR}/static_fees.hpp ${INCLUDE_DEFAULTS_DIR}
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ set(COMMON_SRC
common/network.cpp
crypto/curve.cpp
crypto/hash.cpp
crypto/message.cpp
crypto/slot.cpp
defaults/static_fees.cpp
identities/address.cpp
identities/keys.cpp
Expand All @@ -44,8 +46,6 @@ set(COMMON_SRC
transactions/deserializer.cpp
transactions/serializer.cpp
transactions/transaction.cpp
utils/message.cpp
utils/slot.cpp
utils/base58.cpp

)
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/curve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <vector>

#include "interfaces/identities.hpp"
#include "helpers/crypto_helpers.h"
#include "utils/crypto_helpers.h"
#include "utils/hex.hpp"

#include "rfc6979/rfc6979.h" // Nonce function
Expand Down
107 changes: 107 additions & 0 deletions src/crypto/message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* This file is part of Ark Cpp Crypto.
*
* (c) Ark Ecosystem <info@ark.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
**/

#include "crypto/message.hpp"

#include <map>
#include <string>
#include <vector>

#include "interfaces/identities.hpp"
#include "crypto/curve.hpp"
#include "crypto/hash.hpp"
#include "identities/keys.hpp"
#include "utils/hex.hpp"
#include "utils/json.h"

namespace Ark {
namespace Crypto {

namespace {
constexpr const char* MESSAGE_KEY = "message";
constexpr const char* PUBLICKEY_KEY = "publickey";
constexpr const char* SIGNATURE_KEY = "signature";
constexpr size_t MAGIC_JSON_SIZE = 120U;
} // namespace

// Create an empty Message object for building and signing.
Message::Message() : publicKey({}), signature(Curve::Ecdsa::MAX_SIG_LEN) {}

// Create a Signed Message object for verification.
Message::Message(std::string message,
const PublicKeyBytes& publicKeyBytes,
std::vector<uint8_t> signature)
: message(std::move(message)),
publicKey(publicKeyBytes),
signature(std::move(signature)) {};

bool Message::sign(const std::string& message, const std::string& passphrase) {
this->message = message;

const auto keys = identities::Keys::fromPassphrase(passphrase.c_str());
this->publicKey = keys.publicKey;

const auto messageBytes =
reinterpret_cast<const unsigned char *>(message.c_str());
const auto hash = Hash::sha256(messageBytes, this->message.size());

std::vector<uint8_t> buffer(Curve::Ecdsa::MAX_SIG_LEN);
Curve::Ecdsa::sign(hash.data(), keys.privateKey.data(), buffer);

buffer.resize(buffer[1] + 2);
this->signature = std::move(buffer);

return true;
}

// Verify a Signed Message object.
bool Message::verify() const {
const auto messageBytes =
reinterpret_cast<const unsigned char *>(this->message.c_str());
const auto hash = Hash::sha256(messageBytes, this->message.size());

return Curve::Ecdsa::verify(hash.data(),
this->publicKey.data(),
this->signature);
}

// Create a string map of the Signed Message objects.
std::map<std::string, std::string>
Message::toArray() const {
return {
{ MESSAGE_KEY, this->message },
{ PUBLICKEY_KEY, BytesToHex(this->publicKey.begin(), this->publicKey.end()) },
{ SIGNATURE_KEY, BytesToHex(this->signature.begin(), this->signature.end()) }
};
}

// Create a Json'ified string of the Signed Message.
std::string Message::toJson() const {
std::map<std::string, std::string> messageArray = this->toArray();

const size_t docLength = this->message.length() +
(PUBLICKEY_COMPRESSED_BYTE_LEN + 1) + // + `/0`
(Curve::Ecdsa::MAX_SIG_LEN + 1); // + `/0`

const size_t docCapacity = JSON_OBJECT_SIZE(3) + docLength + MAGIC_JSON_SIZE;
DynamicJsonDocument doc(docCapacity);

doc[MESSAGE_KEY] = messageArray[MESSAGE_KEY];
doc[PUBLICKEY_KEY] = messageArray[PUBLICKEY_KEY];
doc[SIGNATURE_KEY] = messageArray[SIGNATURE_KEY];

std::string jsonStr;
jsonStr.reserve(docCapacity);
serializeJson(doc, &jsonStr[0], docCapacity);

return jsonStr;
}

} // namespace Crypto
} // namespace Ark
Loading