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(identities): improve implementation #120

Merged
merged 14 commits into from Aug 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/stale.yml
@@ -0,0 +1 @@
_extends: .github
10 changes: 9 additions & 1 deletion CHANGELOG.md
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).


## [Unreleased]

### Changed

- refactored Identities implementation. ([#120])

## [0.6.0-arduino] - 2019-07-16

## [0.6.0] - 2019-07-16
Expand Down Expand Up @@ -96,7 +103,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed the way the Arduino IDE Script restores converted directories ([#49])
- Corrected PIO Builds to determine dependency versions explicitly ([#52])

[unreleased]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.2.0...develop
[unreleased]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.6.0...develop
[0.2.0]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.1.0..0.2.0
[#54]: https://github.com/ArkEcosystem/cpp-crypto/pull/54
[#55]: https://github.com/ArkEcosystem/cpp-crypto/pull/55
Expand All @@ -110,3 +117,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[#64]: https://github.com/ArkEcosystem/cpp-crypto/pull/64
[#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
9 changes: 4 additions & 5 deletions README.md
Expand Up @@ -11,6 +11,8 @@
[![Latest Version](https://badgen.now.sh/github/release/ArkEcosystem/cpp-crypto)](https://github.com/ArkEcosystem/cpp-crypto/releases)
[![License: MIT](https://badgen.now.sh/badge/license/MIT/green)](https://opensource.org/licenses/MIT)

> Lead Maintainer: [Simon Downey](https://github.com/sleepdefic1t)

## Documentation

You can find installation instructions and detailed instructions on how to use this package at the [dedicated documentation site](https://docs.ark.io/sdk/cryptography/usage.html).
Expand All @@ -21,11 +23,8 @@ If you discover a security vulnerability within this package, please send an e-m

## Credits

- [Simon Downey](https://github.com/sleepdeficit)
- [Chris Johnson](https://github.com/ciband)
- [supaiku0](https://github.com/supaiku0)
- [All Contributors](../../../../contributors)
This project exists thanks to all the people who [contribute](../../contributors).

## License

[MIT](LICENSE) © [ArkEcosystem](https://ark.io)
[MIT](LICENSE) © [ARK Ecosystem](https://ark.io)
10 changes: 8 additions & 2 deletions examples/arduino/ESP32/ESP32.ino
Expand Up @@ -28,6 +28,12 @@
#include <arkCrypto.h>
/**/

/**
* This is a small hex helper header included in ARK Cpp-Crypto
*/
#include "utils/hex.hpp"
/**/

/****************************************/

void checkCrypto() {
Expand Down Expand Up @@ -134,7 +140,7 @@ void checkCrypto() {
*/
const auto passphrase4 = "this is a top secret passphrase";
const uint8_t wifByte = 0xaa;
WIF wifFromPassphrase = WIF::fromPassphrase(passphrase4, wifByte);
Wif wifFromPassphrase = Wif::fromPassphrase(passphrase4, wifByte);
Serial.print("\nWIF from Passphrase: ");
Serial.println(wifFromPassphrase.toString().c_str()); // the 'WIF' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'.
/**/
Expand All @@ -154,7 +160,7 @@ void checkCrypto() {
Ark::Crypto::Utils::Message message;
message.sign(text, passphrase5);
Serial.print("\nSignature from Signed Message: ");
Serial.println(BytesToHex(message.signature).c_str()); // the 'message.signature' is a byte-array. Use 'BytesToHex()' to view the output. Arduino requires a 'c_str()' to 'print'.
Serial.println(BytesToHex(message.signature).c_str());
// Additionally, you can verify the message.
bool isValid = message.verify();
Serial.print("\nSigned Message Signature is Verified: ");
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/ESP8266/ESP8266.ino
Expand Up @@ -133,7 +133,7 @@ void checkCrypto() {
*/
const auto passphrase4 = "this is a top secret passphrase";
const uint8_t wifByte = 0xaa;
WIF wifFromPassphrase = WIF::fromPassphrase(passphrase4, wifByte);
Wif wifFromPassphrase = Wif::fromPassphrase(passphrase4, wifByte);
Serial.print("\nWIF from Passphrase: ");
Serial.println(wifFromPassphrase.toString().c_str()); // the 'WIF' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'.
/**/
Expand Down
2 changes: 1 addition & 1 deletion examples/cmake_example/main.cpp
Expand Up @@ -97,7 +97,7 @@ int main(int argc, char* argv[]) {

// WIF - from passphrase
const uint8_t wifByte = 0xaa;
auto wif = WIF::fromPassphrase(passphrase, wifByte);
auto wif = Wif::fromPassphrase(passphrase, wifByte);
std::cout << "WIF from passphrase\n";
std::cout << "\tPassphrase: " << passphrase << '\n';
std::cout << "\tWIF Byte: 0x" << std::hex << static_cast<uint16_t>(wifByte) << '\n';
Expand Down
2 changes: 1 addition & 1 deletion examples/platformio_example/src/main.ino
Expand Up @@ -105,7 +105,7 @@ void loop() {

// WIF - from passphrase
const uint8_t wifByte = 0xaa;
auto wif = WIF::fromPassphrase(passphrase, wifByte);
auto wif = Wif::fromPassphrase(passphrase, wifByte);
Serial.println("WIF from passphrase");
Serial.print("\tPassphrase: "); Serial.print(passphrase); Serial.println();
Serial.print("\tWIF Byte: "); Serial.print(static_cast<uint16_t>(wifByte)); Serial.println();
Expand Down
85 changes: 42 additions & 43 deletions extras/ARDUINO_IDE.sh
@@ -1,4 +1,5 @@
#!/usr/bin/env bash

# This file is part of Ark Cpp Crypto.
#
# (c) Ark Ecosystem <info@ark.io>
Expand Down Expand Up @@ -30,7 +31,7 @@ fi
EXTRAS_DIR=$(dirname $0)
PROJECT_ROOT=${EXTRAS_DIR}/../
INCLUDE_DIR=${EXTRAS_DIR}/../src/include
INCLUDE_CRYPTO_DIR=${INCLUDE_DIR}/cpp-crypto
INCLUDE_CPP_CRYPTO_DIR=${INCLUDE_DIR}/cpp-crypto
SRC_DIR=${EXTRAS_DIR}/../src

EXTRAS_BACKUP_DIR=${EXTRAS_DIR}/BACKUP
Expand All @@ -39,24 +40,21 @@ SRC_LIB_DIR=${SRC_DIR}/lib
EXTRAS_LIB_DIR=${EXTRAS_BACKUP_DIR}/lib

SRC_COMMON_DIR=${SRC_DIR}/common
INCLUDE_COMMON_DIR=${INCLUDE_CRYPTO_DIR}/common
INCLUDE_COMMON_DIR=${INCLUDE_CPP_CRYPTO_DIR}/common

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

SRC_HELPERS_DIR=${SRC_DIR}/helpers
INCLUDE_HELPERS_DIR=${INCLUDE_CRYPTO_DIR}/helpers

SRC_ENCODING_DIR=${SRC_HELPERS_DIR}/encoding
INCLUDE_ENCODING_DIR=${INCLUDE_HELPERS_DIR}/encoding
INCLUDE_DEFAULTS_DIR=${INCLUDE_CPP_CRYPTO_DIR}/defaults

SRC_IDENTITIES_DIR=${SRC_DIR}/identities
INCLUDE_IDENTITIES_DIR=${INCLUDE_CRYPTO_DIR}/identities
INCLUDE_IDENTITIES_DIR=${INCLUDE_CPP_CRYPTO_DIR}/identities

SRC_INTERFACES_DIR=${SRC_DIR}/interfaces
INCLUDE_INTERFACES_DIR=${INCLUDE_CPP_CRYPTO_DIR}/interfaces

INCLUDE_NETWORKS_DIR=${INCLUDE_CRYPTO_DIR}/networks
INCLUDE_NETWORKS_DIR=${INCLUDE_CPP_CRYPTO_DIR}/networks
SRC_NETWORKS_DIR=${SRC_DIR}/networks

INCLUDE_TRANSACTIONS_DIR=${INCLUDE_CRYPTO_DIR}/transactions
INCLUDE_TRANSACTIONS_DIR=${INCLUDE_CPP_CRYPTO_DIR}/transactions
SRC_TRANSACTIONS_DIR=${SRC_DIR}/transactions

if [[ $AUTO == '0' ]]; then
Expand Down Expand Up @@ -96,7 +94,7 @@ if [[ -d ${INCLUDE_DIR} ]]; then
# This will run if headers are in the 'include' directory tree.
echo -e "****************************************\n"
echo -e "Moving 'arkCrypto.h' to 'src' directory.\n"
mv ${INCLUDE_CRYPTO_DIR}/arkCrypto.h ${SRC_DIR}
mv ${INCLUDE_CPP_CRYPTO_DIR}/arkCrypto.h ${SRC_DIR}

echo -e "Moving 'common' headers.\n"
mv ${INCLUDE_COMMON_DIR}/configuration.hpp ${SRC_COMMON_DIR}
Expand All @@ -108,15 +106,16 @@ if [[ -d ${INCLUDE_DIR} ]]; then
mv ${INCLUDE_DEFAULTS_DIR}/static_fees.hpp ${SRC_DEFAULTS_DIR}
mv ${INCLUDE_DEFAULTS_DIR}/transaction_types.hpp ${SRC_DEFAULTS_DIR}

echo -e "Moving 'helpers' headers.\n"
mkdir ${SRC_ENCODING_DIR}
mv ${INCLUDE_HELPERS_DIR}/encoding/hex.h ${SRC_ENCODING_DIR}

echo -e "Moving 'identites' headers.\n"
mv ${INCLUDE_IDENTITIES_DIR}/address.h ${SRC_IDENTITIES_DIR}
mv ${INCLUDE_IDENTITIES_DIR}/privatekey.h ${SRC_IDENTITIES_DIR}
mv ${INCLUDE_IDENTITIES_DIR}/publickey.h ${SRC_IDENTITIES_DIR}
mv ${INCLUDE_IDENTITIES_DIR}/wif.h ${SRC_IDENTITIES_DIR}
mv ${INCLUDE_IDENTITIES_DIR}/address.hpp ${SRC_IDENTITIES_DIR}
mv ${INCLUDE_IDENTITIES_DIR}/keys.hpp ${SRC_IDENTITIES_DIR}
mv ${INCLUDE_IDENTITIES_DIR}/privatekey.hpp ${SRC_IDENTITIES_DIR}
mv ${INCLUDE_IDENTITIES_DIR}/publickey.hpp ${SRC_IDENTITIES_DIR}
mv ${INCLUDE_IDENTITIES_DIR}/wif.hpp ${SRC_IDENTITIES_DIR}

echo -e "Moving 'interfaces' headers.\n"
mkdir ${SRC_INTERFACES_DIR}
mv ${INCLUDE_INTERFACES_DIR}/identities.hpp ${SRC_INTERFACES_DIR}

echo -e "Moving 'networks' headers.\n"
mv ${INCLUDE_NETWORKS_DIR}/networks.hpp ${SRC_NETWORKS_DIR}
Expand All @@ -132,11 +131,12 @@ if [[ -d ${INCLUDE_DIR} ]]; then

echo -e "Backing up, moving, and removing dependencies from the 'src/lib' directory.\n"
mkdir ${EXTRAS_BACKUP_DIR}
mv ${SRC_LIB_DIR}/ArduinoJson ${EXTRAS_BACKUP_DIR}
mv ${SRC_LIB_DIR}/BIP66 ${EXTRAS_BACKUP_DIR}
mv ${SRC_LIB_DIR}/uECC ${EXTRAS_BACKUP_DIR}
mkdir ${EXTRAS_LIB_DIR}
mv ${SRC_LIB_DIR}/ArduinoJson ${EXTRAS_LIB_DIR}
mv ${SRC_LIB_DIR}/BIP66 ${EXTRAS_LIB_DIR}
mv ${SRC_LIB_DIR}/uECC ${EXTRAS_LIB_DIR}
mv ${SRC_LIB_DIR}/date ${EXTRAS_LIB_DIR}
mv ${SRC_LIB_DIR}/bcl ${SRC_DIR}
mv ${SRC_LIB_DIR}/date ${SRC_DIR}
mv ${SRC_LIB_DIR}/rfc6979 ${SRC_DIR}

echo -e "Moving Docs to the './extras' directory.\n"
Expand All @@ -158,17 +158,16 @@ else

echo -e "Creating the 'include' directory tree 🗂\n"
mkdir ${INCLUDE_DIR}
mkdir ${INCLUDE_CRYPTO_DIR}
mkdir ${INCLUDE_CPP_CRYPTO_DIR}
mkdir ${INCLUDE_COMMON_DIR}
mkdir ${INCLUDE_DEFAULTS_DIR}
mkdir ${INCLUDE_HELPERS_DIR}
mkdir ${INCLUDE_ENCODING_DIR}
mkdir ${INCLUDE_IDENTITIES_DIR}
mkdir ${INCLUDE_INTERFACES_DIR}
mkdir ${INCLUDE_NETWORKS_DIR}
mkdir ${INCLUDE_TRANSACTIONS_DIR}

echo -e "Moving 'arkCrypto.h' back to the 'include/cpp-crypto/' directory.\n"
mv ${SRC_DIR}/arkCrypto.h ${INCLUDE_CRYPTO_DIR}
mv ${SRC_DIR}/arkCrypto.h ${INCLUDE_CPP_CRYPTO_DIR}

echo -e "Moving 'common' headers.\n"
mv ${SRC_COMMON_DIR}/configuration.hpp ${INCLUDE_COMMON_DIR}
Expand All @@ -180,15 +179,15 @@ else
mv ${SRC_DEFAULTS_DIR}/static_fees.hpp ${INCLUDE_DEFAULTS_DIR}
mv ${SRC_DEFAULTS_DIR}/transaction_types.hpp ${INCLUDE_DEFAULTS_DIR}

echo -e "Moving 'helpers/encoding' headers.\n"
mv ${SRC_ENCODING_DIR}/hex.h ${INCLUDE_ENCODING_DIR}
rm ${SRC_ENCODING_DIR}

echo -e "Moving 'identities' headers.\n"
mv ${SRC_IDENTITIES_DIR}/address.h ${INCLUDE_IDENTITIES_DIR}
mv ${SRC_IDENTITIES_DIR}/privatekey.h ${INCLUDE_IDENTITIES_DIR}
mv ${SRC_IDENTITIES_DIR}/publickey.h ${INCLUDE_IDENTITIES_DIR}
mv ${SRC_IDENTITIES_DIR}/wif.h ${INCLUDE_IDENTITIES_DIR}
mv ${SRC_IDENTITIES_DIR}/address.hpp ${INCLUDE_IDENTITIES_DIR}
mv ${SRC_IDENTITIES_DIR}/privatekey.hpp ${INCLUDE_IDENTITIES_DIR}
mv ${SRC_IDENTITIES_DIR}/publickey.hpp ${INCLUDE_IDENTITIES_DIR}
mv ${SRC_IDENTITIES_DIR}/wif.hpp ${INCLUDE_IDENTITIES_DIR}

echo -e "Moving 'interfaces' headers.\n"
mv ${SRC_INTERFACES_DIR}/identities.hpp ${INCLUDE_INTERFACES_DIR}
rm -rf ${SRC_INTERFACES_DIR}

echo -e "Moving 'networks' headers.\n"
mv ${SRC_NETWORKS_DIR}/networks.hpp ${INCLUDE_NETWORKS_DIR}
Expand All @@ -204,12 +203,12 @@ else

echo -e "Restoring the 'lib' directory.\n"
mkdir ${SRC_LIB_DIR}
mv ${EXTRAS_BACKUP_DIR}/ArduinoJson ${SRC_LIB_DIR}
mv ${EXTRAS_BACKUP_DIR}/BIP66 ${SRC_LIB_DIR}
mv ${EXTRAS_BACKUP_DIR}/uECC ${SRC_LIB_DIR}
mv ${SRC_DIR}/bcl ${SRC_LIB_DIR}
mv ${SRC_DIR}/date ${SRC_LIB_DIR}
mv ${SRC_DIR}/rfc6979 ${SRC_LIB_DIR}
mv ${EXTRAS_LIB_DIR}/ArduinoJson ${SRC_LIB_DIR}
mv ${EXTRAS_LIB_DIR}/BIP66 ${SRC_LIB_DIR}
mv ${EXTRAS_LIB_DIR}/uECC ${SRC_LIB_DIR}
mv ${EXTRAS_LIB_DIR}/date ${SRC_LIB_DIR}
mv ${SRC_DIR}/bcl ${SRC_LIB_DIR}
mv ${SRC_DIR}/rfc6979 ${SRC_LIB_DIR}

echo -e "Moving Docs back to the project root directory.\n"
mv ${EXTRAS_DIR}/docs ${PROJECT_ROOT}
Expand Down
6 changes: 4 additions & 2 deletions keywords.txt
Expand Up @@ -10,7 +10,8 @@ Ark KEYWORD1
Crypto KEYWORD1
common KEYWORD1
defaults KEYWORD1
Identities KEYWORD1
identities KEYWORD1
interfaces KEYWORD1
managers KEYWORD1
Transactions KEYWORD1
Utils KEYWORD1
Expand All @@ -20,9 +21,10 @@ FeePolicy KEYWORD1
Network KEYWORD1

Address KEYWORD1
Keys KEYWORD1
PrivateKey KEYWORD1
PublicKey KEYWORD1
WIF KEYWORD1
Wif KEYWORD1

Networks KEYWORD1

Expand Down
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -27,9 +27,12 @@ set(uECC_SRC
set(COMMON_SRC
common/configuration.cpp
common/network.cpp
crypto/curve.cpp
crypto/hash.cpp
defaults/static_fees.cpp
helpers/crypto.cpp
identities/address.cpp
identities/keys.cpp
identities/privatekey.cpp
identities/publickey.cpp
identities/wif.cpp
Expand All @@ -44,6 +47,8 @@ set(COMMON_SRC
transactions/transaction.cpp
utils/message.cpp
utils/slot.cpp
utils/base58.cpp

)

add_library(${PROJECT_NAME} STATIC
Expand Down
46 changes: 46 additions & 0 deletions src/crypto/curve.cpp
@@ -0,0 +1,46 @@

/**
* 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/curve.hpp"

#include "interfaces/identities.hpp"

#include "uECC.h"

namespace Ark {
namespace Crypto {

PublicKeyBytes Curve::PublicKey::compute(const uint8_t* privateKeyBytes) {
const struct uECC_Curve_t* curve = uECC_secp256k1();

PublicKeyPoint uncompressed {};
if (uECC_compute_public_key(privateKeyBytes,
uncompressed.data(),
curve) == 0) { return {}; };

PublicKeyBytes compressed {};
uECC_compress(uncompressed.data(), compressed.data(), curve);

return compressed;
}

/**/

bool Curve::PublicKey::validate(const uint8_t* publicKeyBytes) {
const struct uECC_Curve_t* curve = uECC_secp256k1();

PublicKeyPoint uncompressed {};
uECC_decompress(publicKeyBytes, uncompressed.data(), curve);

return (uECC_valid_public_key(uncompressed.data(), curve) != 0);
}


} // namespace Crypto
} // namespace Ark
29 changes: 29 additions & 0 deletions src/crypto/curve.hpp
@@ -0,0 +1,29 @@

/**
* 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.
**/

#ifndef CRYPTO_CURVE_HPP
#define CRYPTO_CURVE_HPP

#include "interfaces/identities.hpp"

namespace Ark {
namespace Crypto {

struct Curve {
struct PublicKey {
static PublicKeyBytes compute(const uint8_t* privateKeyBytes);
static bool validate(const uint8_t* publicKeyBytes);
};
};

} // namespace Crypto
} // namespace Ark

#endif