安装
npm install solidity-rlp
在项目目录中。请确保通过 npm 安装以便及时更新!import "solidity-rlp/contracts/RLPReader.sol"
在所需的智能合约中。
github: https://github.com/hamdiallam/Solidity-RLP.git
测试例子
import "solidity-rlp/contracts/RLPReader.sol"
contract SomeContract {
// optional way to attach library functions to these data types.
using RLPReader for RLPReader.RLPItem;
using RLPReader for RLPReader.Iterator;
using RLPReader for bytes;
// lets assume that rlpBytes is an encoding of [[1, "nested"], 2, 0x<Address>]
function someFunctionThatTakesAnEncodedItem(bytes memory rlpBytes) public {
RLPReader.RLPItem[] memory ls = rlpBytes.toRlpItem().toList(); // must convert to an rlpItem first!
RLPReader.RLPItem memory item = ls[0]; // the encoding of [1, "nested"].
item.toList()[0].toUint(); // 1
string(item.toList()[1].toBytes()); // "nested"
ls[1].toUint(); // 2
ls[2].toAddress(); // 0x<Address>
}
// lets assume rlpBytes is an encoding of [["sublist"]]
function someFunctionThatDemonstratesIterators(bytes memory rlpBytes) public {
RLPReader.Iterator memory iter = rlpBytes.toRlpItem().iterator();
RLPReader.Iterator memory subIter = iter.next().iterator();
// iter.hasNext() == false
// string(subIter.next().toBytes()) == "sublist"
// subIter.hasNext() == false
}
}
参考
https://ethereum.stackexchange.com/questions/42732/how-to-rlp-encode-messages-in-solidity
版权属于:区块链中文技术社区 / 转载原创者
本文链接:https://bcskill.com/index.php/archives/1899.html
相关技术文章仅限于相关区块链底层技术研究,禁止用于非法用途,后果自负!本站严格遵守一切相关法律政策!