让单片机接入以太坊区块链:Web3E开发框架简介
时间: 2020-01-16来源:OSCHINA
前景提要
【围观】麒麟芯片遭打压成绝版,华为亿元投入又砸向了哪里?>>>
Web3E,即Web3 for Embedded,是一个面向Arduino嵌入设备的全功能Web3开发框架,开发语言为C/C++。Web3E可以帮助嵌入设备开发者快速实现能够接入以太坊区块链的物联网/IoT设备,为物联网开发者打开了一扇新的大门。
1、Web3E简介
Web3E主要在ESP32上进行测试,ESP8266也可以正常工作。Web3E还包含了一个快速开发DApp注入器,可以很方便地将你的嵌入设备转换为以太坊DApp。

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(¶m); 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(¶m); 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开发框架 — 汇智网

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行