天天看點

Arduino以太坊開發架構Web3E安裝與使用

Web3E,即Web3 for Embedded,是一個面向Arduino嵌入裝置的全功能Web3開發架構,開發語言為C/C++。Web3E可以幫助嵌入裝置開發者快速實作能夠接入以太坊區塊鍊的物聯網/IoT裝置,為物聯網開發者打開了一扇新的大門。

1、Web3E簡介

Web3E主要在ESP32上進行測試,ESP8266也可以正常工作。Web3E還包含了一個快速開發DApp注入器,可以很友善地将你的嵌入裝置轉換為以太坊DApp。

Arduino以太坊開發架構Web3E安裝與使用

Web3E的開發始于一個簡單的需求:開發一個能夠在ESP32上運作的門禁DApp。這期間經曆了相當多的挫折,我們意識到需要一個方法來簡化物聯網嵌入裝置的DApp的開發,這就是開發Web3E的最初動機。

Web3E的主要特性包括:

  • 支援TokenScript接口
  • 開箱即用的以太坊DApp注入器,可以立刻将物聯網嵌入裝置轉化為支援ECDSA密碼學技術 的以太坊DApp
  • 經過優化精簡的密碼學算法實作
  • 交易系統已經充分優化,以太坊ERC20和ERC875合約都有實際使用

2、Web3E安裝

建議使用Platformio安裝Web3E,因為Web3E目前已經是Platformio開發庫的一份子了,是以不需要克隆原始的Web3E代碼庫。

使用Web3E很簡單,隻需要在Platformio中建立一個新項目,然後參考如下内容修改platformio.ini:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

; Serial Monitor options
monitor_speed = 115200

lib_deps =
  # Using a library name
  Web3E           

3、Web3E的示例及使用方法

在Web3E中預置了4個物聯網+以太坊的示例應用:

  • Simple DApp:展示如何建立可以運作在嵌入裝置上的DApp。闆載密碼學引擎 可以與使用者輸入充分互動,在ESP32上的公鑰恢複和簽名驗證可以毫秒級完成
  • 查詢錢包餘額:展示在嵌入裝置上如何查詢ERC20代币餘額以及非同質化通證(NFT)餘額
  • 交易廣播:展示在嵌入裝置上如何實作ERC20和ERC875代币的轉賬交易
  • 以太币轉賬:展示如何在嵌入裝置上實作以太币轉賬

例如,下面的代碼展示了如何使用Web3E讓物聯網嵌入裝置支援以太币轉賬:

// Setup Web3 and Contract with Private Key
...

Contract contract(&web3, "");
contract.SetPrivateKey(PRIVATE_KEY);
uint32_t nonceVal = (uint32_t)web3.EthGetTransactionCount(&address); //obtain the next nonce
uint256_t weiValue = Util::ConvertToWei(0.25, 18); //send 0.25 eth
unsigned long long gasPriceVal = 1000000000ULL;
uint32_t  gasLimitVal = 90000;
string emptyString = "";
string toAddress = "0xC067A53c91258ba513059919E03B81CF93f57Ac7";
string result = contract.SendTransaction(
                nonceVal, gasPriceVal, gasLimitVal, &toAddress, 
                &weiValue, &emptyString);           

下面的代碼使用Web3E在物聯網嵌入裝置上查詢指定的以太坊位址的以太币餘額:

//obtain balance in Wei
uint256_t balance = web3.EthGetBalance(&address); 
//get string balance as Eth (18 decimals)
string balanceStr = Util::ConvertWeiToEthString(&balance, 18);           

使用Web3E讓嵌入裝置支援ERC20代币的發送要複雜一點,但考慮到這是在用C/C++,也還能夠接受:

string contractAddr = "0x20fe562d797a42dcb3399062ae9546cd06f63280";
Contract contract(&web3, contractAddr.c_str());
contract.SetPrivateKey(<Your private key>);

//Get contract name
string param = contract.SetupContractData("name()", &addr);
string result = contract.ViewCall(&param);
string interpreted = Util::InterpretStringResult(web3.getString(&result).c_str());
Serial.println(interpreted.c_str());

//Get Contract decimals
param = contract.SetupContractData("decimals()", &addr);
result = contract.ViewCall(&param);
int decimals = web3.getInt(&result);
Serial.println(decimals);

unsigned long long gasPriceVal = 22000000000ULL;
uint32_t  gasLimitVal = 4300000;

//amount of erc20 token to send, note we use decimal value obtained earlier
uint256_t weiValue = Util::ConvertToWei(0.1, decimals);

//get nonce
uint32_t nonceVal = (uint32_t)web3.EthGetTransactionCount(&addr);
string toAddress = "0x007bee82bdd9e866b2bd114780a47f2261c684e3";
string valueStr = "0x00";

//Setup contract function call 
string p = contract.SetupContractData("transfer(address,uint256)", &toAddress, &weiValue); 

//push transaction to ethereum
result = contract.SendTransaction(nonceVal, gasPriceVal, gasLimitVal, &contractAddr, &valueStr, &p);
string transactionHash = web3.getString(&result);           

4、總結

讓嵌入裝置接入以太坊,是發揮智能合約能力的重要環節,因為機器天生容易按規矩辦事。Web3E提供了一個相對完整的解決方案,雖然目前僅适用于部分Arduino裝置,但這對于物聯網嵌入裝置開發者而言絕對是一個好工具。如果希望系統的學習以太坊開發知識,可以參考如下教程:

以太坊DApp開發入門

|

去中心化電商DApp實戰

原文連結:

Web3E物聯網嵌入裝置的以太坊DApp開發架構 — 彙智網

繼續閱讀