Skip to content

Commit

Permalink
Format address according to its protocol version; (#14) (#15)
Browse files Browse the repository at this point in the history
* Format address according to its protocol version;

* Adding tests for addresses;

* Adding test

* Fix protocol 3

* Added invalid address test for protocol 1

* Adding invalid address test

* adding test for decompressLEB128

* fix test cases and script

* update to v0.6.0

* fix unit tests

Co-authored-by: Juan Leni <juan.leni@zondax.ch>

Co-authored-by: Lola Dam <rllola80@gmail.com>
  • Loading branch information
jleni and rllola committed Jan 14, 2020
1 parent 39a7a81 commit 4b571f4
Show file tree
Hide file tree
Showing 11 changed files with 779 additions and 127 deletions.
2 changes: 1 addition & 1 deletion src/ledger
71 changes: 68 additions & 3 deletions tests/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <iostream>
#include <hexutils.h>
#include <lib/crypto.h>
#include <bignum.h>

using ::testing::TestWithParam;
using ::testing::Values;
Expand Down Expand Up @@ -48,10 +49,10 @@ TEST(CRYPTO, fillAddress) {
char *addr = (char *) (buffer + 33);

EXPECT_THAT(std::string(pk),
::testing::Eq("03CD4569C4FE16556D74DFD1372A2F3AE7B6C43121C7C2902F9AE935B80A7C254B"));
::testing::Eq("03CD4569C4FE16556D74DFD1372A2F3AE7B6C43121C7C2902F9AE935B80A7C254B"));

EXPECT_THAT(std::string(addr),
::testing::Eq("f1z2uf3vzdjgpozbg3ihfnwkhx3dmm6mopkfhqoyy"));
::testing::Eq("f1z2uf3vzdjgpozbg3ihfnwkhx3dmm6mop6d4vlii"));

std::cout << pk << std::endl;
std::cout << addr << std::endl;
Expand All @@ -77,8 +78,72 @@ TEST(CRYPTO, fillAddressTestMnemonic) {
::testing::Eq("8D16D62802CA55326EC52BF76A8543B90E2ABA5BCF6CD195C0D6FC1EF38FA1B300"));

EXPECT_THAT(std::string(addr),
::testing::Eq("f1f5qrfe6fzjstz52ljd47ger4besc7j2g5hfv4lq"));
::testing::Eq("f1f5qrfe6fzjstz52ljd47ger4besc7j2gzz3e4ea"));

std::cout << pk << std::endl;
std::cout << addr << std::endl;
}

TEST(CRYPTO, extractBitsFromLEB128_small) {
uint8_t input[] = {0x81, 0x01};
uint64_t output;

auto ret = decompressLEB128(input, &output);

EXPECT_THAT(ret, ::testing::Eq(1));
EXPECT_THAT(output, ::testing::Eq(0x81));

char bufferUI[300];
uint64_to_str(bufferUI, sizeof(bufferUI), output);

auto expected = std::string("129");
EXPECT_THAT(std::string(bufferUI), testing::Eq(expected)) << "decimal output not matching";
}

TEST(CRYPTO, extractBitsFromLEB128_1byte) {
uint8_t input[] = {0xc1, 0x0d};
uint64_t output;

auto ret = decompressLEB128(input, &output);

EXPECT_THAT(ret, ::testing::Eq(1));
EXPECT_THAT(output, ::testing::Eq(1729));

char bufferUI[300];
uint64_to_str(bufferUI, sizeof(bufferUI), output);

auto expected = std::string("1729");
EXPECT_THAT(std::string(bufferUI), testing::Eq(expected)) << "decimal output not matching";
}

TEST(CRYPTO, extractBitsFromLEB128_big) {
uint8_t input[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01};
uint64_t output;

auto ret = decompressLEB128(input, &output);

EXPECT_THAT(ret, ::testing::Eq(1));
EXPECT_THAT(output, ::testing::Eq(18446744073709551615u));

char bufferUI[300];
uint64_to_str(bufferUI, sizeof(bufferUI), output);

auto expected = std::string("18446744073709551615");
EXPECT_THAT(std::string(bufferUI), testing::Eq(expected)) << "decimal output not matching";
}

TEST(CRYPTO, extractBitsFromLEB128_tooBig) {
uint8_t input[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02};
uint64_t output;

auto ret = decompressLEB128(input, &output);

EXPECT_THAT(ret, ::testing::Eq(0));
EXPECT_THAT(output, ::testing::Eq(0));

char bufferUI[300];
uint64_to_str(bufferUI, sizeof(bufferUI), output);

auto expected = std::string("0");
EXPECT_THAT(std::string(bufferUI), testing::Eq(expected)) << "decimal output not matching";
}
277 changes: 275 additions & 2 deletions tests/manual_testvectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
},
{
"description": "Basic test case",
"to": "01FD1D0F4DFCD7E99AFCB99A8326B7DC459D32C628",
"from": "01B882619D46558F3D9E316D11B48DCF211327025A",
"to": "f17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
"from": "f1xcbgdhkgkwht3hrrnui3jdopeejsoas2rujnkdi",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
Expand All @@ -24,5 +24,278 @@
"encoded_tx": "h1UB/R0PTfzX6Zr8uZqDJrfcRZ0yxihVAbiCYZ1GVY89njFtEbSNzyETJwJaAUMBhqBCCcRCYagA",
"valid": true,
"encoded_tx_hex": "875501fd1d0f4dfcd7e99afcb99a8326b7dc459d32c6285501b882619d46558f3d9e316d11b48dcf211327025a01430186a04209c44261a800"
},
{
"description": "Using Protocol 0 addresses",
"to": "f00",
"from": "f0150",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h0IAAEMAlgEBQwGGoEIJxEJhqAA=",
"valid": true,
"encoded_tx_hex": "874200004300960101430186a04209c44261a800"
},
{
"description": "Using Protocol 0 addresses 2",
"to": "f01024",
"from": "f01729",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h0MAgAhDAMENAUMBhqBCCcRCYagA",
"valid": true,
"encoded_tx_hex": "87430080084300c10d01430186a04209c44261a800"
},
{
"description": "Using Protocol 0 addresses 3",
"to": "f018446744073709551615",
"from": "f00",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h0sA////////////AUIAAAFDAYagQgnEQmGoAA==",
"valid": true,
"encoded_tx_hex": "874b00ffffffffffffffffff0142000001430186a04209c44261a800"
},
{
"description": "Using Protocol 1 addresses",
"to": "f17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
"from": "f1xcbgdhkgkwht3hrrnui3jdopeejsoatkzmoltqy",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1UB/R0PTfzX6Zr8uZqDJrfcRZ0yxihVAbiCYZ1GVY89njFtEbSNzyETJwJqAUMBhqBCCcRCYagA",
"valid": true,
"encoded_tx_hex": "875501fd1d0f4dfcd7e99afcb99a8326b7dc459d32c6285501b882619d46558f3d9e316d11b48dcf211327026a01430186a04209c44261a800"
},
{
"description": "Using Protocol 1 addresses 2",
"to": "f1xtwapqc6nh4si2hcwpr3656iotzmlwumogqbuaa",
"from": "f1wbxhu3ypkuo6eyp6hjx6davuelxaxrvwb2kuwva",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1UBvOwHwF5p+SRo4rPjv3fIdPLF2oxVAbBuem8PVR3iYf46b+GCtCLuC8a2AUMBhqBCCcRCYagA",
"valid": true,
"encoded_tx_hex": "875501bcec07c05e69f92468e2b3e3bf77c874f2c5da8c5501b06e7a6f0f551de261fe3a6fe182b422ee0bc6b601430186a04209c44261a800"
},
{
"description": "Using Protocol 1 addresses 3",
"to": "f12fiakbhe2gwd5cnmrenekasyn6v5tnaxaqizq6a",
"from": "f17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1UB0VAFBOTRrD6JrIkaRQJYb6vZtBdVAf0dD0381+ma/Lmagya33EWdMsYoAUMBhqBCCcRCYagA",
"valid": true,
"encoded_tx_hex": "875501d1500504e4d1ac3e89ac891a4502586fabd9b4175501fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62801430186a04209c44261a800"
},
{
"description": "Using Protocol 2 addresses",
"to": "f24vg6ut43yw2h2jqydgbg2xq7x6f4kub3bg6as6i",
"from": "f25nml2cfbljvn4goqtclhifepvfnicv6g7mfmmvq",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1UC5U3qT5vFtH0mGBmCbV4fv4vFUDtVAutYvQihWmreGdCYlnQUj6lagVfGAUMBhqBCCcRCYagA",
"valid": true,
"encoded_tx_hex": "875502e54dea4f9bc5b47d261819826d5e1fbf8bc5503b5502eb58bd08a15a6ade19d0989674148fa95a8157c601430186a04209c44261a800"
},
{
"description": "Using Protocol 2 addresses 2",
"to": "f2nuqrg7vuysaue2pistjjnt3fadsdzvyuatqtfei",
"from": "f24dd4ox4c2vpf5vk5wkadgyyn6qtuvgcpxxon64a",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1UCbSETfrTEgUJp6JTSls9lAOQ81xRVAuDHx1+C1V5e1V2ygDNjDfQnSphPAUMBhqBCCcRCYagA",
"valid": true,
"encoded_tx_hex": "8755026d21137eb4c4814269e894d296cf6500e43cd7145502e0c7c75f82d55e5ed55db28033630df4274a984f01430186a04209c44261a800"
},
{
"description": "Using Protocol 2 addresses 3",
"to": "f2gfvuyh7v2sx3patm5k23wdzmhyhtmqctasbr23y",
"from": "f24vg6ut43yw2h2jqydgbg2xq7x6f4kub3bg6as6i",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1UCMWtMH/XUr7eCbOq1uw8sPg82QFNVAuVN6k+bxbR9JhgZgm1eH7+LxVA7AUMBhqBCCcRCYagA",
"valid": true,
"encoded_tx_hex": "875502316b4c1ff5d4afb7826ceab5bb0f2c3e0f3640535502e54dea4f9bc5b47d261819826d5e1fbf8bc5503b01430186a04209c44261a800"
},
{
"description": "Using Protocol 3 addresses",
"to": "f3vvmn62lofvhjd2ugzca6sof2j2ubwok6cj4xxbfzz4yuxfkgobpihhd2thlanmsh3w2ptld2gqkn2jvlss4a",
"from": "f3wmuu6crofhqmm3v4enos73okk2l366ck6yc4owxwbdtkmpk42ohkqxfitcpa57pjdcftql4tojda2poeruwa",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1gxA61Y32luLU6R6obIgek4uk6oGzleEnl7hLnPMUuVRnBeg5x6mdYGskfdtPmsejQU3VgxA7MpTwouKeDGbrwjXS/tylaXv3hK9gXHWvYI5qY9XNOOqFyomJ4O/ekYizgvk3JGDQFDAYagQgnEQmGoAA==",
"valid": true,
"encoded_tx_hex": "87583103ad58df696e2d4e91ea86c881e938ba4ea81b395e12797b84b9cf314b9546705e839c7a99d606b247ddb4f9ac7a3414dd583103b3294f0a2e29e0c66ebc235d2fedca5697bf784af605c75af608e6a63d5cd38ea85ca8989e0efde9188b382f9372460d01430186a04209c44261a800"
},
{
"description": "Using Protocol 3 addresses 2",
"to": "f3s2q2hzhkpiknjgmf4zq3ejab2rh62qbndueslmsdzervrhapxr7dftie4kpnpdiv2n6tvkr743ndhrsw6d3a",
"from": "f3q22fijmmlckhl56rn5nkyamkph3mcfu5ed6dheq53c244hfmnq2i7efdma3cj5voxenwiummf2ajlsbxc65a",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1gxA5aho+TqehTUmYXmYbIkAdRP7UAtHQklskPJI1icD7x+Ms0E4p7XjRXTfTqqP+baM1gxA4a0VCWMWJR199FvWqwBinn2wRadIPwzkh3Ytc4crGw0j5CjYDYk9q65G2RRjC6AlQFDAYagQgnEQmGoAA==",
"valid": true,
"encoded_tx_hex": "8758310396a1a3e4ea7a14d49985e661b22401d44fed402d1d0925b243c923589c0fbc7e32cd04e29ed78d15d37d3aaa3fe6da3358310386b454258c589475f7d16f5aac018a79f6c1169d20fc33921dd8b5ce1cac6c348f90a3603624f6aeb91b64518c2e809501430186a04209c44261a800"
},
{
"description": "Using Protocol 3 addresses 3",
"to": "f3u5zgwa4ael3vuocgc5mfgygo4yuqocrntuuhcklf4xzg5tcaqwbyfabxetwtj4tsam3pbhnwghyhijr5mixa",
"from": "f3vvmn62lofvhjd2ugzca6sof2j2ubwok6cj4xxbfzz4yuxfkgobpihhd2thlanmsh3w2ptld2gqkn2jvlss4a",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1gxA6dyawOAIvdaOEYXWFNgzuYpBwotnShxKWXl8m7MQIWDgoA3JO008nIDNvCdtjHwdFgxA61Y32luLU6R6obIgek4uk6oGzleEnl7hLnPMUuVRnBeg5x6mdYGskfdtPmsejQU3QFDAYagQgnEQmGoAA==",
"valid": true,
"encoded_tx_hex": "87583103a7726b038022f75a384617585360cee629070a2d9d28712965e5f26ecc40858382803724ed34f2720336f09db631f074583103ad58df696e2d4e91ea86c881e938ba4ea81b395e12797b84b9cf314b9546705e839c7a99d606b247ddb4f9ac7a3414dd01430186a04209c44261a800"
},
{
"description": "Using Protocol 3 addresses 3",
"to": "f3u5zgwa4ael3vuocgc5mfgygo4yuqocrntuuhcklf4xzg5tcaqwbyfabxetwtj4tsam3pbhnwghyhijr5mixa",
"from": "f3vvmn62lofvhjd2ugzca6sof2j2ubwok6cj4xxbfzz4yuxfkgobpihhd2thlanmsh3w2ptld2gqkn2jvlss4a",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1gxA6dyawOAIvdaOEYXWFNgzuYpBwotnShxKWXl8m7MQIWDgoA3JO008nIDNvCdtjHwdFgxA61Y32luLU6R6obIgek4uk6oGzleEnl7hLnPMUuVRnBeg5x6mdYGskfdtPmsejQU3QFDAYagQgnEQmGoAA==",
"valid": true,
"encoded_tx_hex": "87583103a7726b038022f75a384617585360cee629070a2d9d28712965e5f26ecc40858382803724ed34f2720336f09db631f074583103ad58df696e2d4e91ea86c881e938ba4ea81b395e12797b84b9cf314b9546705e839c7a99d606b247ddb4f9ac7a3414dd01430186a04209c44261a800"
},
{
"description": "Address protocol 1 Invalid payload length of 21 bytes",
"to": "f1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaae6zk5dy",
"from": "f1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaae6zk5dy",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1YBAAAAAAAAAAAAAAAAAAAAAAAAAAABVgEAAAAAAAAAAAAAAAAAAAAAAAAAAAEBQwGGoEIJxEJhqAA=",
"valid": false,
"encoded_tx_hex": "875601000000000000000000000000000000000000000001560100000000000000000000000000000000000000000101430186a04209c44261a800"
},
{
"description": "Address protocol 1 Invalid payload length of 19 bytes",
"to": "f1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabh2wd2q",
"from": "f1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabh2wd2q",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1QBAAAAAAAAAAAAAAAAAAAAAAAAAFQBAAAAAAAAAAAAAAAAAAAAAAAAAAFDAYagQgnEQmGoAA==",
"valid": false,
"encoded_tx_hex": "8754010000000000000000000000000000000000000054010000000000000000000000000000000000000001430186a04209c44261a800"
},
{
"description": "Address protocol 0 Invalid payload length of 21 bytes",
"to": "f00",
"from": "f00",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAVgAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQwGGoEIJxEJhqAA=",
"valid": false,
"encoded_tx_hex": "875600000000000000000000000000000000000000000000560000000000000000000000000000000000000000000001430186a04209c44261a800"
},
{
"description": "Address protocol 2 Invalid payload length of 21 bytes",
"to": "f2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3sien4",
"from": "f2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3sien4",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1YCAAAAAAAAAAAAAAAAAAAAAAAAAAAAVgIAAAAAAAAAAAAAAAAAAAAAAAAAAAABQwGGoEIJxEJhqAA=",
"valid": false,
"encoded_tx_hex": "875602000000000000000000000000000000000000000000560200000000000000000000000000000000000000000001430186a04209c44261a800"
},
{
"description": "Address protocol 2 Invalid payload length of 19 bytes",
"to": "f2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab5nwlky",
"from": "f2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab5nwlky",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1QCAAAAAAAAAAAAAAAAAAAAAAAAAFQCAAAAAAAAAAAAAAAAAAAAAAAAAAFDAYagQgnEQmGoAA==",
"valid": false,
"encoded_tx_hex": "8754020000000000000000000000000000000000000054020000000000000000000000000000000000000001430186a04209c44261a800"
},
{
"description": "Address protocol 3 Invalid payload length of 47 bytes",
"to": "f3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaamqs4am4",
"from": "f3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaamqs4am4",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1gwAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWDADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQwGGoEIJxEJhqAA=",
"valid": false,
"encoded_tx_hex": "875830030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000583003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001430186a04209c44261a800"
},
{
"description": "Address protocol 3 Invalid payload length of 49 bytes",
"to": "f3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaddp6aeu",
"from": "f3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaddp6aeu",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1gyAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABYMgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUMBhqBCCcRCYagA",
"valid": false,
"encoded_tx_hex": "87583203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005832030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001430186a04209c44261a800"
},
{
"description": "Address with unknown protocol",
"to": "faaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaryyc34i",
"from": "faaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaryyc34i",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": "25000",
"method": 0,
"encoded_tx": "h1UEAAAAAAAAAAAAAAAAAAAAAAAAAABVBAAAAAAAAAAAAAAAAAAAAAAAAAAAAUMBhqBCCcRCYagA",
"valid": false,
"encoded_tx_hex": "87550400000000000000000000000000000000000000005504000000000000000000000000000000000000000001430186a04209c44261a800"
}
]
Loading

0 comments on commit 4b571f4

Please sign in to comment.