Simple example of calling Cardano-Rust code from a C program

I randomly picked a couple of functions from the IOHK github to show the steps involved. Once you can call a function from C, you will be able to call it from many other languages and platforms.

Despite many attempts to format the post for this website, I gave up. You can see it all here.

Next: Android/Java


taking a look … thnks

Yep, I had a look at the rust-cardano, but it does not seem to me to have many exported functions, for some real use cases for mobile apps.
For example, no any mention about some SPV like functionalities whihc would mean that you do need to download the whole blockhain to the mobile. At the moment you just can achieve this functionality by using some trusted centralised server to obtain wallet related data, which is far from the perfect solution.

Anyway, I also played a bit w/ it especially /w the test.c in cardano-c test dir, and it allows to return some char** (see details below), but returning complex data structures (rust’s Structs etc.) are the hard parts.

--- test.c	2018-10-19 08:41:36.054502118 +1000
+++ ../../../../test/cargo/cardano-c/test/test.c	2018-10-19 08:43:17.794502118 +1000
@@ -16,12 +16,14 @@
 	cardano_account *account = cardano_account_create(wallet, alias, 0);
 	if (!account) goto error;

-	cardano_account_generate_addresses(account, 0, 0, 1, &address);
-	printf("address generated: %s\n", address);
-	printf("address is valid: %s\n", cardano_address_is_valid(address) ? "NO" : "YES");
+	int addr_count = 3;
+	addr_count = cardano_account_generate_addresses(account, 0, 0, addr_count, &address);

+	for (int i = 0; i < addr_count; i++) {
+		printf("address generated(%d): %s\n", i, *(&address+i));
+		printf("address is valid: %s\n", cardano_address_is_valid(*(&address+i)) ? "NO" : "YES");
+	}

1 Like

Yea, and that’s just the Rust-to-C part, you are going to have to complete the “bridge” to whatever language X you finally want to get to, assuming you don’t want to stop at C. i.e. the C-to-X part.

I suspect the Rust-to-C part is the easier of the two, as the languages are both low-level pointer based.

Today I am going to try and see if I can get the functions to be called from an app on Android/Java.

I don’t even know if there are standards for this kind of code - is it smarter to create and dispose of the structures/memory in Rust, or in C - both would work.

Right, downloading the blockchain to a mobile is just No. There will always be an element of centralisation when not hosting the BC locally. But if they open up Yoroi and let any client connect to the backend (and they run multiple backends) then I think that’s a good solution. Maybe even some other people run backends to really decentralise it - I’d be happy to run one.

That’s not an optimal solution as that/those servers can be easily DDoS-ed.
But, to achieve some SPV like solution, Cardano needs PoPoS, but I have not heard anything abt it since January. The solution must rely on the network/protocol and not on some backend servers.

1 Like