Skip to content

Commit 3f20868

Browse files
sleepdefic1tfaustbrian
authored andcommitted
refactor: Use BIP66 Library (#88)
1 parent 453f87f commit 3f20868

13 files changed

Lines changed: 30 additions & 387 deletions

File tree

.circleci/script_arduino.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ mkdir -p ~/Arduino/libraries/cpp-crypto/
66
mv ~/project/* ~/Arduino/libraries/cpp-crypto
77

88
arduino-cli lib install "ArduinoJson@6.10.0"
9+
arduino-cli lib install "BIP66"
910

1011
arduino-cli compile --output temp.bin -b esp32:esp32:esp32 ~/Arduino/libraries/cpp-crypto/examples/arduino/ESP32/ESP32.ino --debug

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "src/lib/ArduinoJson"]
1111
path = src/lib/ArduinoJson
1212
url = https://github.com/bblanchon/ArduinoJson
13+
[submodule "src/lib/BIP66"]
14+
path = src/lib/BIP66
15+
url = https://github.com/sleepdefic1t/BIP66

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Changed
1111

12+
- changed to BIP66 lib for DER ser/des. ([#88])
1213
- updated vendorField to support 255 bytes in Core v2.4 ([#84])
1314
- updated ArduinoJson package to version v.6.10.0 ([#76])
1415
- updated tests to use Core fixtures ([#74])
1516
- improved Windows support ([#83])
1617

1718
### Fixed
1819

19-
- properly handle 0 ARKtoshi Transaction amounts.
20+
- properly handle 0 ARKtoshi Transaction amounts. ([#85])
2021

2122
## [0.3.1] - 2019-02-19
2223

extras/ARDUINO_IDE.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ if [[ -d ${INCLUDE_DIR} ]]; then
112112
mv ${INCLUDE_ENUMS_DIR}/types.h ${SRC_ENUMS_DIR}
113113

114114
echo -e "Moving 'helpers' headers.\n"
115-
mv ${INCLUDE_HELPERS_DIR}/encoding/der.h ${SRC_HELPERS_DIR}/encoding
116-
mv ${INCLUDE_HELPERS_DIR}/encoding/hex.h ${SRC_HELPERS_DIR}/encoding
115+
mkdir ${SRC_ENCODING_DIR}
116+
mv ${INCLUDE_HELPERS_DIR}/encoding/hex.h ${SRC_ENCODING_DIR}
117117

118118
## 'bip39' library is not supported in Arduino
119119
echo -e "Backing up and removing 'mnemonic.h'.\n"
@@ -192,6 +192,7 @@ else
192192

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

196197
echo -e "Moving 'identities' headers.\n"
197198
mv ${SRC_IDENTITIES_DIR}/address.h ${INCLUDE_IDENTITIES_DIR}

platformio.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ description = "A simple Cryptography Implementation in C++ for the ARK Blockchai
1313

1414
[common]
1515
lib_ldf_mode = off
16-
lib_deps = micro-ecc, bip39@^1.1, ArduinoJson@6.10.0
16+
lib_deps = micro-ecc, bip39@^1.1, ArduinoJson@6.10.0, BIP66
1717
build_flags = -I./src/ -I./src/lib -I./src/include/cpp-crypto
18-
src_filter = +<*> -<.git/> -<examples/> -<lib/ArduinoJson> -<lib/bip39> -<lib/uECC> -<CMakeFiles>
18+
src_filter = +<*> -<.git/> -<examples/> -<lib/ArduinoJson> -<lib/bip39> -<lib/uECC> -<CMakeFiles> -<lib/BIP66>
1919
upload_speed = 921600
2020

2121
[env:esp8266]

src/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ project(Ark-Cpp-Crypto-lib C CXX)
55

66
add_subdirectory(lib/bip39/src)
77

8+
set(BIP66_SRC
9+
${PROJECT_SOURCE_DIR}/lib/BIP66/src/bip66.cpp
10+
)
11+
812
set(BCL_SRC
913
lib/bcl/Base58Check.cpp
1014
lib/bcl/CurvePoint.cpp
@@ -26,7 +30,6 @@ set(COMMON_SRC
2630
configuration/fee.cpp
2731
configuration/network.cpp
2832
helpers/crypto.cpp
29-
helpers/encoding/der.cpp
3033
identities/address.cpp
3134
identities/mnemonic.cpp
3235
identities/privatekey.cpp
@@ -45,6 +48,7 @@ add_library(${PROJECT_NAME}
4548
STATIC
4649
${BCL_SRC}
4750
${uECC_SRC}
51+
${BIP66_SRC}
4852
${COMMON_SRC}
4953
)
5054

@@ -59,6 +63,7 @@ include_directories(${PROJECT_SOURCE_DIR}/lib/bcl)
5963
include_directories(${PROJECT_SOURCE_DIR}/lib/bip39)
6064
include_directories(${PROJECT_SOURCE_DIR}/lib/rfc6979)
6165
include_directories(${PROJECT_SOURCE_DIR}/lib/uECC)
66+
include_directories(${PROJECT_SOURCE_DIR}/lib/BIP66/src)
6267

6368
target_include_directories( ${PROJECT_NAME}
6469
PUBLIC ${cpp_crypto_build_include_dirs}

src/helpers/crypto.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include "bcl/Ecdsa.hpp"
55
#include "bcl/Sha256.hpp"
66
#include "bcl/Uint256.hpp"
7-
#include "helpers/encoding/der.h"
87
#include "helpers/crypto_helpers.h"
98
#include "rfc6979/rfc6979.h"
109
#include "uECC.h"
10+
#include "bip66.h"
1111

1212
void cryptoSign(Sha256Hash hash, Ark::Crypto::Identities::PrivateKey privateKey, std::vector<uint8_t>& signature) {
1313
Uint256 r;
@@ -19,13 +19,11 @@ void cryptoSign(Sha256Hash hash, Ark::Crypto::Identities::PrivateKey privateKey,
1919
auto ret = Ecdsa::sign(Uint256(privateKey.toBytes()), hash, Uint256(nonce32), r, s);
2020
assert(ret);
2121

22-
std::vector<uint8_t> r_der(PRIVATEKEY_SIZE);
23-
r.getBigEndianBytes(&r_der[0]);
22+
std::vector<uint8_t> rValue(PRIVATEKEY_SIZE), sValue(PRIVATEKEY_SIZE);
23+
r.getBigEndianBytes(&rValue[0]);
24+
s.getBigEndianBytes(&sValue[0]);
2425

25-
std::vector<uint8_t> s_der(PRIVATEKEY_SIZE);
26-
s.getBigEndianBytes(&s_der[0]);
27-
28-
encodeDER(toDER(r_der), toDER(s_der), signature);
26+
BIP66::encode(rValue, sValue, signature);
2927
}
3028

3129
bool cryptoVerify(Ark::Crypto::Identities::PublicKey publicKey, Sha256Hash hash, std::vector<uint8_t>& signature) {
@@ -52,12 +50,12 @@ bool cryptoVerify(Ark::Crypto::Identities::PublicKey publicKey, Sha256Hash hash,
5250
CurvePoint curvePoint(x, y);
5351

5452
/* Decode signature from DER into r & s buffers */
55-
std::vector<uint8_t> r; // create r-value buffer
56-
std::vector<uint8_t> s; // create s-value buffer
57-
decodeDER(signature, r, s);
53+
std::vector<uint8_t> rValue(PRIVATEKEY_SIZE), sValue(PRIVATEKEY_SIZE);
54+
55+
BIP66::decode(signature, rValue, sValue);
5856

59-
Uint256 r256(r.data()); // create Uint256/BigNumber from r-value buffer
60-
Uint256 s256(s.data()); // create Uint256/BigNumber from s-value buffer
57+
Uint256 r256(rValue.data()); // create Uint256/BigNumber from r-value buffer
58+
Uint256 s256(sValue.data()); // create Uint256/BigNumber from s-value buffer
6159

6260
/* Verify */
6361
return Ecdsa::verify(curvePoint, hash, r256, s256);

src/helpers/encoding/der.cpp

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/include/cpp-crypto/helpers/encoding/der.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/lib/BIP66

Submodule BIP66 added at 5f024b4

0 commit comments

Comments
 (0)