diff --git a/src/ledger b/src/ledger index c855a1c0..e68bcc72 160000 --- a/src/ledger +++ b/src/ledger @@ -1 +1 @@ -Subproject commit c855a1c02ada3c8844a0180ff7e680d2f6204f91 +Subproject commit e68bcc726e8b7c7d76f32f642369494ffcc3a2e2 diff --git a/tests/crypto.cpp b/tests/crypto.cpp index af76a067..180a3ee7 100644 --- a/tests/crypto.cpp +++ b/tests/crypto.cpp @@ -19,6 +19,7 @@ #include #include #include +#include using ::testing::TestWithParam; using ::testing::Values; @@ -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; @@ -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"; +} diff --git a/tests/manual_testvectors.json b/tests/manual_testvectors.json index a6db296f..8295d16a 100644 --- a/tests/manual_testvectors.json +++ b/tests/manual_testvectors.json @@ -14,8 +14,8 @@ }, { "description": "Basic test case", - "to": "01FD1D0F4DFCD7E99AFCB99A8326B7DC459D32C628", - "from": "01B882619D46558F3D9E316D11B48DCF211327025A", + "to": "f17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy", + "from": "f1xcbgdhkgkwht3hrrnui3jdopeejsoas2rujnkdi", "nonce": 1, "value": "100000", "gasprice": "2500", @@ -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" } ] \ No newline at end of file diff --git a/tests/tools/generate.js b/tests/tools/generate.js deleted file mode 100644 index 42af54bc..00000000 --- a/tests/tools/generate.js +++ /dev/null @@ -1,63 +0,0 @@ -const fs = require('fs'); -const cbor = require('cbor'); - -function bigintToArray(v) { - tmp = BigInt(v).toString(16); - // not sure why it is not padding and buffer does not like it - if (tmp.length % 2 === 1) tmp = "0" + tmp; - return Buffer.from(tmp, "hex"); -} - -function toCBOR(message) { - let answer = [] - - - // "to" field - answer.push(Buffer.from(message.to, 'hex')); - - // "from" field - answer.push(Buffer.from(message.from, 'hex')); - - // "nonce" field - answer.push(message.nonce); - - // "value" - buf = bigintToArray(message.value); - answer.push(buf); - - // "gasprice" - buf = bigintToArray(message.gasprice); - answer.push(buf); - - // "gaslimit" - buf = bigintToArray(message.gaslimit); - answer.push(buf); - - // "method" - answer.push(message.method); - - if (message.params) { - // "params" - answer.push(message.params); - } - - - return cbor.encode(answer); -} - -let rawData = fs.readFileSync('template.json'); -let jsonData = JSON.parse(rawData); - -newJsonData = []; -jsonData.forEach(tc => { - let cborBuf = toCBOR(tc); - - tc['encoded_tx'] = cborBuf.toString('base64'); - tc['encoded_tx_hex'] = cborBuf.toString('hex'); - newJsonData.push(tc); - -}); - -let rawdata = JSON.stringify(newJsonData, null, 4); - -fs.writeFileSync('../manual_testvectors.json', rawdata); \ No newline at end of file diff --git a/tests/tools/template.json b/tests/tools/template.json deleted file mode 100644 index 3c875e81..00000000 --- a/tests/tools/template.json +++ /dev/null @@ -1,26 +0,0 @@ -[ - { - "description": "TODO invalid test case ", - "to": "", - "from": "", - "nonce": 0, - "value" : "0", - "gasprice" : "0", - "gaslimit" : "0", - "method" : 0, - "encoded_tx": "", - "valid": false - }, - { - "description": "Basic test case", - "to": "01FD1D0F4DFCD7E99AFCB99A8326B7DC459D32C628", - "from": "01B882619D46558F3D9E316D11B48DCF211327025A", - "nonce": 1, - "value" : "100000", - "gasprice" : "2500", - "gaslimit" : "25000", - "method" : 0, - "encoded_tx": "", - "valid": true - } -] \ No newline at end of file diff --git a/tests/util/testcases.cpp b/tests/util/testcases.cpp index 78a2759f..c8998bf6 100644 --- a/tests/util/testcases.cpp +++ b/tests/util/testcases.cpp @@ -68,11 +68,27 @@ std::vector GenerateExpectedUIOutput(const Json::Value &j) { uint8_t dummy; - addTo(answer, "0 | To : {}", FormatAddress(j["to"].asString(), 0, &dummy)); - addTo(answer, "0 | To : {}", FormatAddress(j["to"].asString(), 1, &dummy)); + if (j["to"].asString().length() > 40) { + addTo(answer, "0 | To : {}", FormatAddress(j["to"].asString(), 0, &dummy)); + addTo(answer, "0 | To : {}", FormatAddress(j["to"].asString(), 1, &dummy)); + if (j["to"].asString().length() > 80) { + addTo(answer, "0 | To : {}", FormatAddress(j["to"].asString(), 2, &dummy)); + } + } else { + // To print protocol 0 addresses which seems to be always less than 20char + addTo(answer, "0 | To : {}", FormatAddress(j["to"].asString(), 0, &dummy)); + } - addTo(answer, "1 | From : {}", FormatAddress(j["from"].asString(), 0, &dummy)); - addTo(answer, "1 | From : {}", FormatAddress(j["from"].asString(), 1, &dummy)); + if (j["from"].asString().length() > 40) { + addTo(answer, "1 | From : {}", FormatAddress(j["from"].asString(), 0, &dummy)); + addTo(answer, "1 | From : {}", FormatAddress(j["from"].asString(), 1, &dummy)); + if (j["to"].asString().length() > 80) { + addTo(answer, "1 | From : {}", FormatAddress(j["from"].asString(), 2, &dummy)); + } + } else { + // To print protocol 0 addresses which seems to be always less than 20char + addTo(answer, "1 | From : {}", FormatAddress(j["from"].asString(), 0, &dummy)); + } addTo(answer, "2 | Nonce : {}", j["nonce"].asUInt64()); diff --git a/tests/tools/.gitignore b/tools/.gitignore similarity index 100% rename from tests/tools/.gitignore rename to tools/.gitignore diff --git a/tools/generate.js b/tools/generate.js new file mode 100644 index 00000000..ded83702 --- /dev/null +++ b/tools/generate.js @@ -0,0 +1,111 @@ +const fs = require('fs'); +const cbor = require('cbor'); +const blake2 = require('blake2'); +const base32Encode = require('base32-encode'); +const leb128 = require('@webassemblyjs/leb128'); + +function bigintToArray(v) { + tmp = BigInt(v).toString(16); + // not sure why it is not padding and buffer does not like it + if (tmp.length % 2 === 1) tmp = "0" + tmp; + return Buffer.from(tmp, "hex"); +} + +function toCBOR(message) { + let answer = []; + + + // "to" field + answer.push(Buffer.from(message.to, 'hex')); + + // "from" field + answer.push(Buffer.from(message.from, 'hex')); + + // "nonce" field + answer.push(message.nonce); + + // "value" + buf = bigintToArray(message.value); + answer.push(buf); + + // "gasprice" + buf = bigintToArray(message.gasprice); + answer.push(buf); + + // "gaslimit" + buf = bigintToArray(message.gaslimit); + answer.push(buf); + + // "method" + answer.push(message.method); + + if (message.params) { + // "params" + answer.push(message.params); + } + + + return cbor.encode(answer); +} + +function formatAddress(a) { + let formattedAddress = "f"; + addressBuffer = Buffer.from(a, 'hex'); + + if (addressBuffer.length < 1) { + // empty address + return ""; + } + + if (addressBuffer[0] === 0x00) { + formattedAddress += "0"; + let result = leb128.decodeUInt64(addressBuffer, 1).value; + + formattedAddress += result; + } else { + + switch (addressBuffer[0]) { + case 0x01: + formattedAddress += "1"; + break; + case 0x02: + formattedAddress += "2"; + break; + case 0x03: + formattedAddress += "3"; + break; + } + + let h = blake2.createHash('blake2b', {digestLength: 4}); + h.update(addressBuffer); + let cksm = h.digest(); + let b = Buffer.concat([addressBuffer.slice(1), cksm]); + let result = base32Encode(b, 'RFC3548', {padding: false}); + + formattedAddress += result.toLowerCase(); + + } + + return formattedAddress; +} + +let rawData = fs.readFileSync('template.json'); +let jsonData = JSON.parse(rawData); + +newJsonData = []; +jsonData.forEach(tc => { + let cborBuf = toCBOR(tc); + + // Format address + tc["to"] = formatAddress(tc["to"]); + tc["from"] = formatAddress(tc["from"]); + + tc['encoded_tx'] = cborBuf.toString('base64'); + tc['encoded_tx_hex'] = cborBuf.toString('hex'); + newJsonData.push(tc); + +}); + +let rawdata = JSON.stringify(newJsonData, null, 4); + +fs.writeFileSync('../tests/manual_testvectors.json', rawdata); diff --git a/tests/tools/package-lock.json b/tools/package-lock.json similarity index 54% rename from tests/tools/package-lock.json rename to tools/package-lock.json index f15e1261..6ab11e8e 100644 --- a/tests/tools/package-lock.json +++ b/tools/package-lock.json @@ -4,18 +4,18 @@ "lockfileVersion": 1, "requires": true, "dependencies": { - "base32": { - "version": "0.0.6", - "resolved": "https://registry.npmjs.org/base32/-/base32-0.0.6.tgz", - "integrity": "sha1-eQOLy1rsLY8ivMHChAKST1Cm0qw=", + "@webassemblyjs/leb128": { + "version": "1.8.5", + "resolved": "https://registry.npmjs.org/@webassemblyjs/leb128/-/leb128-1.8.5.tgz", + "integrity": "sha512-plYUuUwleLIziknvlP8VpTgO4kqNaH57Y3JnNa6DLpu/sGcP6hbVdfdX5aHAV716pQBKrfuU26BJK29qY37J7A==", "requires": { - "optimist": ">=0.1.0" + "@xtuc/long": "4.2.2" } }, - "base32-decode": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/base32-decode/-/base32-decode-1.0.0.tgz", - "integrity": "sha512-KNWUX/R7wKenwE/G/qFMzGScOgVntOmbE27vvc6GrniDGYb6a5+qWcuoXl8WIOQL7q0TpK7nZDm1Y04Yi3Yn5g==" + "@xtuc/long": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/@xtuc/long/-/long-4.2.2.tgz", + "integrity": "sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==" }, "base32-encode": { "version": "1.1.1", @@ -27,6 +27,14 @@ "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" }, + "blake2": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/blake2/-/blake2-4.0.0.tgz", + "integrity": "sha512-PIOc6RXAZYBYcdpyMzI6/SCU3BH8EbmA9vr0BAVyQv48CQTXDN6viHOTM+8KQue2IPsyHNpIR3UDisz8rZDPTA==", + "requires": { + "nan": "^2.14.0" + } + }, "cbor": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/cbor/-/cbor-5.0.1.tgz", @@ -36,29 +44,15 @@ "nofilter": "^1.0.3" } }, - "minimist": { - "version": "0.0.10", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", - "integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8=" + "nan": { + "version": "2.14.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz", + "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==" }, "nofilter": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/nofilter/-/nofilter-1.0.3.tgz", "integrity": "sha512-FlUlqwRK6reQCaFLAhMcF+6VkVG2caYjKQY3YsRDTl4/SEch595Qb3oLjJRDr8dkHAAOVj2pOx3VknfnSgkE5g==" - }, - "optimist": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz", - "integrity": "sha1-2j6nRob6IaGaERwybpDrFaAZZoY=", - "requires": { - "minimist": "~0.0.1", - "wordwrap": "~0.0.2" - } - }, - "wordwrap": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz", - "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc=" } } } diff --git a/tests/tools/package.json b/tools/package.json similarity index 51% rename from tests/tools/package.json rename to tools/package.json index fdf44ba4..d2207756 100644 --- a/tests/tools/package.json +++ b/tools/package.json @@ -2,7 +2,10 @@ "name": "tools", "version": "1.0.0", "dependencies": { - "cbor": "^5.0.1" + "base32-encode": "^1.1.1", + "blake2": "^4.0.0", + "cbor": "^5.0.1", + "@webassemblyjs/leb128": "^1.8.5" }, "scripts": { "generate": "node generate.js" diff --git a/tools/template.json b/tools/template.json new file mode 100644 index 00000000..bf4c1f01 --- /dev/null +++ b/tools/template.json @@ -0,0 +1,279 @@ +[ + { + "description": "TODO invalid test case ", + "to": "", + "from": "", + "nonce": 0, + "value" : "0", + "gasprice" : "0", + "gaslimit" : "0", + "method" : 0, + "encoded_tx": "", + "valid": false + }, + { + "description": "Basic test case", + "to": "01FD1D0F4DFCD7E99AFCB99A8326B7DC459D32C628", + "from": "01B882619D46558F3D9E316D11B48DCF211327025A", + "nonce": 1, + "value": "100000", + "gasprice": "2500", + "gaslimit": "25000", + "method": 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 0 addresses", + "to": "0000", + "from": "009601", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 0 addresses 2", + "to": "008008", + "from": "00c10d", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 0 addresses 3", + "to": "00ffffffffffffffffff01", + "from": "0000", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 1 addresses", + "to": "01fd1d0f4dfcd7e99afcb99a8326b7dc459d32c628", + "from": "01b882619d46558f3d9e316d11b48dcf211327026a", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 1 addresses 2", + "to": "01bcec07c05e69f92468e2b3e3bf77c874f2c5da8c", + "from": "01b06e7a6f0f551de261fe3a6fe182b422ee0bc6b6", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 1 addresses 3", + "to": "01d1500504e4d1ac3e89ac891a4502586fabd9b417", + "from": "01fd1d0f4dfcd7e99afcb99a8326b7dc459d32c628", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 2 addresses", + "to": "02e54dea4f9bc5b47d261819826d5e1fbf8bc5503b", + "from": "02eb58bd08a15a6ade19d0989674148fa95a8157c6", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 2 addresses 2", + "to": "026d21137eb4c4814269e894d296cf6500e43cd714", + "from": "02e0c7c75f82d55e5ed55db28033630df4274a984f", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 2 addresses 3", + "to": "02316b4c1ff5d4afb7826ceab5bb0f2c3e0f364053", + "from": "02e54dea4f9bc5b47d261819826d5e1fbf8bc5503b", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 3 addresses", + "to": "03ad58df696e2d4e91ea86c881e938ba4ea81b395e12797b84b9cf314b9546705e839c7a99d606b247ddb4f9ac7a3414dd", + "from": "03b3294f0a2e29e0c66ebc235d2fedca5697bf784af605c75af608e6a63d5cd38ea85ca8989e0efde9188b382f9372460d", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 3 addresses 2", + "to": "0396a1a3e4ea7a14d49985e661b22401d44fed402d1d0925b243c923589c0fbc7e32cd04e29ed78d15d37d3aaa3fe6da33", + "from": "0386b454258c589475f7d16f5aac018a79f6c1169d20fc33921dd8b5ce1cac6c348f90a3603624f6aeb91b64518c2e8095", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 3 addresses 3", + "to": "03a7726b038022f75a384617585360cee629070a2d9d28712965e5f26ecc40858382803724ed34f2720336f09db631f074", + "from": "03ad58df696e2d4e91ea86c881e938ba4ea81b395e12797b84b9cf314b9546705e839c7a99d606b247ddb4f9ac7a3414dd", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Using Protocol 3 addresses 3", + "to": "03a7726b038022f75a384617585360cee629070a2d9d28712965e5f26ecc40858382803724ed34f2720336f09db631f074", + "from": "03ad58df696e2d4e91ea86c881e938ba4ea81b395e12797b84b9cf314b9546705e839c7a99d606b247ddb4f9ac7a3414dd", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": true + }, + { + "description": "Address protocol 1 Invalid payload length of 21 bytes", + "to": "01000000000000000000000000000000000000000001", + "from": "01000000000000000000000000000000000000000001", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": false + }, + { + "description": "Address protocol 1 Invalid payload length of 19 bytes", + "to": "0100000000000000000000000000000000000000", + "from": "0100000000000000000000000000000000000000", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": false + }, + { + "description": "Address protocol 0 Invalid payload length of 21 bytes", + "to": "00000000000000000000000000000000000000000000", + "from": "00000000000000000000000000000000000000000000", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": false + }, + { + "description": "Address protocol 2 Invalid payload length of 21 bytes", + "to": "02000000000000000000000000000000000000000000", + "from": "02000000000000000000000000000000000000000000", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": false + }, + { + "description": "Address protocol 2 Invalid payload length of 19 bytes", + "to": "0200000000000000000000000000000000000000", + "from": "0200000000000000000000000000000000000000", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": false + }, + { + "description": "Address protocol 3 Invalid payload length of 47 bytes", + "to": "030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "from": "030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": false + }, + { + "description": "Address protocol 3 Invalid payload length of 49 bytes", + "to": "0300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "from": "0300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": false + }, + { + "description": "Address with unknown protocol", + "to": "040000000000000000000000000000000000000000", + "from": "040000000000000000000000000000000000000000", + "nonce": 1, + "value" : "100000", + "gasprice" : "2500", + "gaslimit" : "25000", + "method" : 0, + "encoded_tx": "", + "valid": false + } +] +