solidity-rlp
Advanced tools
Comparing version 2.0.0 to 2.0.1
# Changelog | ||
## 2.0.1 | ||
### Added | ||
- `iterator(RLPItem)` returns an iterator object over an rlp encoded list | ||
- `hasNext(Iterator)` returns a boolean if the iterator has an item to iterate on next | ||
- `next(Iterator) returns (RLPItem)` returns an RLPItem of the next item up for iteration | ||
## 2.0.0 | ||
@@ -4,0 +10,0 @@ ### Added |
{ | ||
"name": "solidity-rlp", | ||
"author": "Hamdi Allam", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "solidity rlp encoder/decoder", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -19,11 +19,14 @@ # RLP decoder/reader | ||
1. `isList(RLPItem) bool` : inidicator if the encoded data is a list | ||
1. `toList(RLPItem) RLPItem[]` : returns a list of RLPItems | ||
2. `toBytes(RLPItem) bytes` : returns the payload in bytes | ||
3. `toAddress(RLPItem) address` : returns the encoded address. Must be exactly 20 bytes. | ||
4. `toUint(RLPItem) uint` : returns the encoded uint. Enforced data is capped to 32 bytes. | ||
5. `toUintStrict(RLPItem) uint` : returns the encoded uint. Encoded data must be padded to 32 bytes. | ||
6. `toBoolean(RLPItem) bool`: returns the encoded boolean | ||
7. `toRlpBytes(RLPItem) bytes `: returns the raw rlp encoded byte form | ||
8. `rlpLen(RLPItem) uint` : returns the byte length of the rlp item | ||
9. `payloadLen(RLPItem) uint` : returns the byte length of the data payload | ||
2. `toList(RLPItem) RLPItem[]` : returns a list of RLPItems | ||
3. `iterator(RLPITem) Iterator` : returns an `Iterator` over the RLPItem. RLPItem must be an encoded list | ||
4. `toBytes(RLPItem) bytes` : returns the payload in bytes | ||
5. `toAddress(RLPItem) address` : returns the encoded address. Must be exactly 20 bytes. | ||
6. `toUint(RLPItem) uint` : returns the encoded uint. Enforced data is capped to 32 bytes. | ||
7. `toUintStrict(RLPItem) uint` : returns the encoded uint. Encoded data must be padded to 32 bytes. | ||
8. `toBoolean(RLPItem) bool`: returns the encoded boolean | ||
9. `toRlpBytes(RLPItem) bytes `: returns the raw rlp encoded byte form | ||
10. `rlpLen(RLPItem) uint` : returns the byte length of the rlp item | ||
11. `payloadLen(RLPItem) uint` : returns the byte length of the data payload | ||
12. `hasNext(Iterator) bool` : indicator if there is another item to iterate on | ||
13. `next(Iterator) RLPItem` : returns the next `RLPItem` in the iterator | ||
@@ -55,2 +58,12 @@ **Note**: The reader contract only provides only these conversion functions. All other solidity data types can be derived from | ||
} | ||
// lets assume rlpBytes is an encoding of [["sublist"]] | ||
func 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 | ||
} | ||
} | ||
@@ -57,0 +70,0 @@ ``` |
@@ -12,3 +12,3 @@ let rlp = require("rlp"); | ||
let catchError = function(promise) { | ||
let catchError = (promise) => { | ||
return promise.then(result => [null, result]) | ||
@@ -18,2 +18,22 @@ .catch(err => [err]); | ||
let toRLPHeader = (block) => { | ||
return rlp.encode([ | ||
block.parentHash, | ||
block.sha3Uncles, | ||
block.miner, | ||
block.stateRoot, | ||
block.transactionsRoot, | ||
block.receiptsRoot, | ||
block.logsBloom, | ||
new web3.utils.BN(block.difficulty), | ||
new web3.utils.BN(block.number), | ||
block.gasLimit, | ||
block.gasUsed, | ||
block.timestamp, | ||
block.extraData, | ||
block.mixHash, | ||
block.nonce, | ||
]); | ||
}; | ||
contract("RLPReader", async (accounts) => { | ||
@@ -186,7 +206,7 @@ before(async () => { | ||
let err; | ||
[err] = await catchError(helper.toBoolean(toHex(rlp.encode(256)))); | ||
[err] = await catchError(helper.toBoolean.call(toHex(rlp.encode(256)))); | ||
if (!err) { | ||
assert.fail(null, null, "converted a boolean larger than a byte"); | ||
} | ||
[err] = await catchError(helper.toBoolean(toHex(''))); | ||
[err] = await catchError(helper.toBoolean.call(toHex(''))); | ||
if (!err) { | ||
@@ -222,3 +242,3 @@ assert.fail(null, null, "converted a boolean of empty bytes"); | ||
[err] = await catchError(helper.toBytes(toHex(''))); | ||
[err] = await catchError(helper.toBytes.call(toHex(''))); | ||
if (!err) { | ||
@@ -228,2 +248,56 @@ assert.fail(null, null, "converted to bytes of empty bytes"); | ||
}); | ||
it("correctly creates an iterator", async () => { | ||
let err; | ||
let data = rlp.encode("foo"); | ||
[err] = await catchError(helper.toIterator.call(toHex(data))); | ||
if (!err) | ||
assert.fail(null, null, "constructed an iterator from something not a list"); | ||
data = rlp.encode([1, "isvalid", 2]); | ||
[err, _] = await catchError(helper.toIterator.call(toHex(data))); | ||
if (err) | ||
assert.fail(null, null, "could not construct iterator out of a valid list") | ||
data = rlp.encode([["yeah!"]]) | ||
let result = await helper.nestedIteration.call(toHex(data)); | ||
assert(result == "yeah!", "could not retrieve the string in the sublist") | ||
}); | ||
it("correctly iterates over an RLP list (e.g., an RLP encoded block header)", async () => { | ||
// Block 8000000 from the Ethereum main net with a couple of fields omitted | ||
const block = { | ||
parentHash: '0x487e074bba7f0749950d7e2f226307c8ac388cb0410cfe817931a5a44077e159', | ||
sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', | ||
stateRoot: '0x7b814195793c699d345339dd7a4225112ad91b9ba7f03787563a9e98ba692e52', | ||
transactionsRoot: '0xad6c9f611dfdd446855cb430b8392e20538db4f0349336063e710c6c483e9e43', | ||
receiptsRoot: '0xf022ddad6e90316614df496922cb73508a9abecfda2d3076a5f1129a01497869', | ||
difficulty: '2037888242889388', | ||
number: 8000000, | ||
extraData: '0x5050594520737061726b706f6f6c2d6574682d636e2d687a', | ||
gasLimit: 8002255, | ||
gasUsed: 7985243, | ||
timestamp: 1561100149, | ||
nonce: '0x00daa7b00156a516', | ||
hash: '0x4e454b49dc8a2e2a229e0ce911e9fd4d2aa647de4cf6e0df40cf71bff7283330', | ||
logsBloom: '0xc29754f51412a148104c6716000a3084218a2c2eb411080f0204cc2000182520544cd8896089451840a4c3d492209909825614420c21350104e0a81810b82018838f088200f3022616869299810060089f08291289c920ea25d1006460513529851001477aa905491218501179c40b01348430400ad167600e0141344140022135a01484482520131c40141583050710042168c050220010c1c443f2291b41688340084524418d0048b1328844438630c88000940524800c4001202a1540b00498350932001812960220043b200016c02cf06433548b5100429220aa00423421e25121330b410051204098d8406a600b3610403d208c8381c51bd15a9dc30004', | ||
miner: '0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c', | ||
mixHash: '0x8a24dc2c8fb497ff40a622173d9c7804a274de3da4b335b2ba0e3c53e3fae714', | ||
totalDifficulty: '10690776258913596267754', | ||
}; | ||
const rlpHeader = toRLPHeader(block); | ||
const result = await helper.toBlockHeader.call(rlpHeader, {}); | ||
assert(result.parentHash == block.parentHash, "parentHash not equal"); | ||
assert(result.sha3Uncles == block.sha3Uncles, "sha3Uncles not equal"); | ||
assert(result.stateRoot == block.stateRoot, "stateRoot not equal"); | ||
assert(result.transactionsRoot == block.transactionsRoot, "transactionsRoot not equal"); | ||
assert(result.receiptsRoot == block.receiptsRoot, "receiptsRoot not equal"); | ||
assert(result.difficulty == block.difficulty, "difficulty not equal"); | ||
assert(result.number == block.number, "number not equal"); | ||
assert(result.gasLimit == block.gasLimit, "gasLimit not equal"); | ||
assert(result.gasUsed == block.gasUsed, "gasUsed not equal"); | ||
assert(result.timestamp == block.timestamp, "timestamp not equal"); | ||
assert(result.nonce.toString() == web3.utils.toBN(block.nonce).toString(), "nonce not equal"); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47249
264
78