Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tordwessman/r2web3
A Web3 library inteded to be used for microcontrollers
https://github.com/tordwessman/r2web3
Last synced: about 2 months ago
JSON representation
A Web3 library inteded to be used for microcontrollers
- Host: GitHub
- URL: https://github.com/tordwessman/r2web3
- Owner: TordWessman
- License: mit
- Created: 2023-09-22T11:49:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-12T08:15:51.000Z (9 months ago)
- Last Synced: 2024-04-12T15:28:04.146Z (9 months ago)
- Language: C
- Homepage:
- Size: 344 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# r2web3
A Web3 library written in C++ intended to be used for microcontrollers, but should work on any platform.
Has at this point only been tested in Linux (g++) and ESP8266 and ESP32 boards (Arduino IDE).This project is under development. Here's a list of a few thing that's on the roadmap:
* Unit tests
* ABI encoding (existing, but incomplete)
* ABI decoding (existing, but incomplete)
* EIP-155 support## Installation
```
$ cd ~/Arduino/libraries
$ git clone https://github.com/TordWessman/r2web3.git
```
Remember to restart Arduino IDE## Examples
Here's a blob of example code.```
#ifdef ESP8266
#include
#endif
#ifdef ESP32
#include
#endif
#includeusing namespace blockchain;
#define SSID ""
#define WIFI_PASSWORD ""
#define PRIVATE_KEY "" // Account private key
#define CONTRACT_ADDRESS "" // Address to a deployed contract// Use CA Certificate for https.
// The one below is valid for json-rpc.evm.testnet.shimmer.network.
// Use root certificate unless accessing a local endpoint (e.g. Ganache)
const char cert [] PROGMEM = R"CERT(
-----BEGIN CERTIFICATE-----
MIIDzTCCArWgAwIBAgIQCjeHZF5ftIwiTv0b7RQMPDANBgkqhkiG9w0BAQsFADBa
MQswCQYDVQQGEwJJRTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJl
clRydXN0MSIwIAYDVQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTI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9yP+kHyCREU3qzuWFloUwOxkgAyXVjBYdwRVKD05WdRerw
6DEdfgkfCv4+3ao8XnTSrLE=
-----END CERTIFICATE-----
)CERT";Account account(PRIVATE_KEY);
Address contractAddress(CONTRACT_ADDRESS);
ESPNetwork networkFacade(cert); // Use the ESP network stack.
Chain chain("https://json-rpc.evm.testnet.shimmer.network", &networkFacade); // Use ShimmerEVM testnet.void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);WiFi.begin(SSID, WIFI_PASSWORD);
int connectionAttempts = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connecting to blockchain...");
if(chain.Start()) {
Serial.println("...done");
} else {
Serial.println("...failed");
}
}/// Example of a transfer to a specific address
void makeTransfer() {
Address sendToAddress("");
BigNumber sendAmount = BigNumber(random(1000,10000));
uint32_t gasLimit = 6721975;
chain.Send(&account, sendToAddress, sendAmount, gasLimit);
}/// Check balance
void getBalance() {Result balanceResult = chain.GetBalance(account.GetAddress());
if (balanceResult.HasValue()) {
// Retrieve tha balance and convert it into a human-friendly decimal string.
char *balanceDecimalString = balanceResult.Value().GenerateDecimalString();
Serial.print("Balance: "); Serial.println(balanceDecimalString);
delete []balanceDecimalString;
}// Retrieve the balance of an ERC-20 contract
Result contractBalanceResult = chain.GetBalance(account.GetAddress(), contractAddress);
}/// View call
void viewCall() {
ContractCall call("add", {ENC(10u, "uint32"), ENC({ENC(20u), ENC(30u)}, "uint32[]")}); // Equivalent to method signature 'add(uint32,uint32[])'
Result result = chain.ViewCall(account.GetAddress(), contractAddress, &call);
if (result.HasValue()) {
Serial.print("Result: "); Serial.println(result.Value().Result());
} else {
Serial.print("Failure: "); Serial.println(result.ErrorCode());
}
}/// (State changing) contract call
void mutateCall() {
ContractCall contractCall("setFoo", {ENC((uint32_t)random(1000,10000))}); // Equivalent to method signature 'setFoo(uint256y)'
BigNumber gasPrice(1234567); // chain.EstimateGas could be used instead.
BigNumber sendAmount = BigNumber(0u);
uint32_t gasLimit = 6721975;
Result contractCallResponse = chain.Send(&account, contractAddress, sendAmount, 6721975, &gasPrice, &contractCall);
}void loop() {
makeTransfer();
viewCall();
mutateCall();
getBalance();
delay(1000 * 60 * 60);
}
```## Secure connection (HTTPS)
When accessing a HTTP over TLS, one must specify the root certificate for the endpoint when instantiating the `ESPNetwork`. No certificates are provided and developers has to provide these for the RPC-URL:s they intend to use.Please note that `Restart()` method must be called once the WiFi connection has been established and _if_ the device clock has lost synchronization (e g after hybernation).
There are several ways to retrieve the certificate, and here's one:
```
$ openssl s_client -showcerts -verify 5 -connect :443 < /dev/null
```