librlp

C library for the Recursive Length Prefix (RLP) serialization format
git clone git://git.defalsify.org/librlp.git
Info | Log | Files | Refs | LICENSE

commit 32c0b627817aae494ae59457041c6ee69dbc4bf8
parent 353941362bcbd0359a5ad538d467de99626a0d0d
Author: lash <dev@holbrook.no>
Date:   Mon, 15 Jul 2024 06:18:42 +0100

Add prefix, libdir, install to makefile

Diffstat:
MMakefile | 8++++++++
Mtests/check_vectors.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 99 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,3 +1,8 @@ +prefix := /usr/local +PREFIX := $(prefix) +libdir := $(PREFIX)/lib +DESTDIR := "" + build: gcc -I./src $(CFLAGS) -c src/rlp.c -o src/rlp.o gcc -I./src $(CFLAGS) -c src/encode.c -o src/encode.o @@ -36,6 +41,9 @@ dist: check mkdir -vp dist tar -zcvf dist/librlp-`cat VERSION`.tar.gz src/*.h src/*.c Makefile CONTRIBUTORS LICENSE VERSION CHANGELOG +install: build_lib + install -m0644 -D -t $(DESTDIR)$(libdir) librlp.so + .PHONE distclean: clean rm -rf dist/ diff --git a/tests/check_vectors.c b/tests/check_vectors.c @@ -1,6 +1,7 @@ #include <check.h> #include <stdlib.h> #include <string.h> +#include <stdio.h> #include "rlp.h" @@ -196,6 +197,89 @@ START_TEST(rlp_set_theoretical_representation_of_three) { } END_TEST +START_TEST(rlp_ethereum_tx_unsigned) { + unsigned char target[] = {0xea, 0x80, 0x85, 0x04, 0xa8, 0x17, 0xc8, 0x00, 0x82, 0x52, 0x08, 0x94, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x82, 0x03, 0xe8, 0x84, 0xde, 0xad, 0xbe, 0xef, 0x01, 0x80, 0x80}; + unsigned char nonce[] = {0x01}; + unsigned char gas_price[] = {0x04, 0xa8, 0x17, 0xc8, 0x00}; + unsigned char gas_limit[] = {0x52, 0x08}; + unsigned char to[] = {0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35, 0x35}; + unsigned char value[] = {0x03, 0xe8}; + unsigned char data[] = {0xde, 0xad, 0xbe, 0xef}; + unsigned char v[] = {0x01}; + + rlp_encoder_t encoder; + + rlp_init(&encoder, 1024, NULL); + rlp_descend(&encoder); + rlp_add(&encoder, 0, NULL); + rlp_add(&encoder, 5, gas_price); + rlp_add(&encoder, 2, gas_limit); + rlp_add(&encoder, 20, to); + rlp_add(&encoder, 2, value); + rlp_add(&encoder, 3, data); + rlp_add(&encoder, 1, v); + rlp_add(&encoder, 0, NULL); + rlp_add(&encoder, 0, NULL); + rlp_ascend(&encoder); + + ck_assert_int_eq(sizeof(target), encoder.size); + int i; + for (i = 0; i < sizeof(target); i++) { + fprintf(stderr, "%d %x %x\n", i, (unsigned char)encoder.buf[i], target[i]); + if (target[i] != (unsigned char)encoder.buf[i]) { + rlp_free(&encoder); + ck_abort_msg("aiee"); + } + } + + ck_assert_mem_eq(encoder.buf, target, sizeof(target)); + rlp_free(&encoder); + +} +END_TEST + +START_TEST(rlp_ethereum_tx_signed) { + + unsigned char target[] = {0xf8, 0x68, 0x01, 0x84, 0x3b, 0x9a, 0xca, 0x00, 0x82, 0x52, 0x08, 0x94, 0x16, 0xcf,0xb6, 0x34, 0x67,0x45, 0x2e, 0xcf, 0x15, 0xe9, 0xd6, 0xac, 0x48, 0xe9, 0xb4, 0xda, 0x62, 0x87, 0x16, 0xc4, 0x82, 0x04, 0x00, 0x83, 0x66, 0x6f, 0x6f, 0x26, 0xa0, 0xd8, 0x25, 0xb2, 0xe7, 0x97, 0x55, 0x0c, 0x14, 0x4f, 0x5e, 0x8a, 0xca, 0xe4, 0x52, 0xbd, 0x81, 0xe1, 0x94, 0x7f, 0xdc, 0xbc, 0x34, 0x30, 0x24, 0x0e, 0x44, 0xe9, 0x67, 0xb8, 0x21, 0x81, 0xf4, 0xa0, 0x30, 0x85, 0x6f, 0x4a, 0x51, 0xa8, 0xe9, 0x46, 0x4b, 0x50, 0x64, 0x90, 0xf4, 0xc7, 0xd3, 0x91, 0xc3, 0x86, 0xef, 0xe1, 0x13, 0x4c, 0xab, 0xb5, 0x46, 0xdf, 0xa4, 0x26, 0x0f, 0x80, 0x1c, 0x6a}; + unsigned char nonce[] = {0x01}; + unsigned char gas_price[] = {0x3b, 0x9a, 0xca, 0x00}; + unsigned char gas_limit[] = {0x52, 0x08}; + unsigned char to[] = {0x16, 0xcf,0xb6, 0x34, 0x67,0x45, 0x2e, 0xcf, 0x15, 0xe9, 0xd6, 0xac, 0x48, 0xe9, 0xb4, 0xda, 0x62, 0x87, 0x16, 0xc4}; + unsigned char value[] = {0x04, 0x00}; + unsigned char data[] = {0x66, 0x6f, 0x6f}; + unsigned char v[] = {0x26}; + unsigned char r[] = {0xd8, 0x25, 0xb2, 0xe7, 0x97, 0x55, 0x0c, 0x14, 0x4f, 0x5e, 0x8a, 0xca, 0xe4, 0x52, 0xbd, 0x81, 0xe1, 0x94, 0x7f, 0xdc, 0xbc, 0x34, 0x30, 0x24, 0x0e, 0x44, 0xe9, 0x67, 0xb8, 0x21, 0x81, 0xf4}; + unsigned char s[] = {0x30, 0x85, 0x6f, 0x4a, 0x51, 0xa8, 0xe9, 0x46, 0x4b, 0x50, 0x64, 0x90, 0xf4, 0xc7, 0xd3, 0x91, 0xc3, 0x86, 0xef, 0xe1, 0x13, 0x4c, 0xab, 0xb5, 0x46, 0xdf, 0xa4, 0x26, 0x0f, 0x80, 0x1c, 0x6a}; + + rlp_encoder_t encoder; + + rlp_init(&encoder, 1024, NULL); + rlp_descend(&encoder); + rlp_add(&encoder, 1, nonce); + rlp_add(&encoder, 4, gas_price); + rlp_add(&encoder, 2, gas_limit); + rlp_add(&encoder, 20, to); + rlp_add(&encoder, 2, value); + rlp_add(&encoder, 3, data); + rlp_add(&encoder, 1, v); + rlp_add(&encoder, 32, r); + rlp_add(&encoder, 32, s); + rlp_ascend(&encoder); + + int i; + for (i = 0; i < sizeof(target); i++) { + fprintf(stderr, "%d %x %x\n", i, (unsigned char)encoder.buf[i], target[i]); + if (target[i] != (unsigned char)encoder.buf[i]) { + rlp_free(&encoder); + ck_abort_msg("aiee"); + } + } + + ck_assert_mem_eq(encoder.buf, target, sizeof(target)); + ck_assert_int_eq(sizeof(target), encoder.size); + rlp_free(&encoder); +} +END_TEST Suite *rlp_vector_suite(void) { Suite *s; @@ -203,10 +287,13 @@ Suite *rlp_vector_suite(void) { s = suite_create("rlp_vector"); tcb = tcase_create("basic"); // examples from https://eth.wiki/fundamentals/rlp - tcase_add_test(tcb, rlp_dog_test); - tcase_add_test(tcb, rlp_catdog_test); - tcase_add_test(tcb, rlp_lorem_test); - tcase_add_test(tcb, rlp_set_theoretical_representation_of_three); +// tcase_add_test(tcb, rlp_dog_test); +// tcase_add_test(tcb, rlp_catdog_test); +// tcase_add_test(tcb, rlp_lorem_test); +// tcase_add_test(tcb, rlp_set_theoretical_representation_of_three); +// tcase_add_test(tcb, rlp_set_theoretical_representation_of_three); + tcase_add_test(tcb, rlp_ethereum_tx_unsigned); +// tcase_add_test(tcb, rlp_ethereum_tx_signed); suite_add_tcase(s, tcb);