Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 048efb3

Browse files
committed
fix: replace node buffers with unit8arrays
This module now accepts Uint8Arrays as well as node Buffers and returns Uint8Arrays. Internally it converts non-Buffers into Buffers because the ethereum libs require that. BREAKING CHANGES: - `util.serialize` returns a `Uint8Array` - `util.cid` returns `CID`s with a breaking API change - see multiformats/js-cid#117 for changes
1 parent 8dc22d8 commit 048efb3

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

eth-block/test/resolver.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const EthBlockHeader = require('ethereumjs-block/header')
99
const multihash = require('multihashes')
1010
const multicodec = require('multicodec')
1111
const { Buffer } = require('buffer')
12+
const uint8ArrayToString = require('uint8arrays/to-string')
1213

1314
const ipldEthBlock = require('../index')
1415
const resolver = ipldEthBlock.resolver
@@ -51,7 +52,7 @@ describe('IPLD format resolver (local)', () => {
5152
const reconstructedCid = new CID(encodedCid)
5253
expect(cid.version).to.equal(reconstructedCid.version)
5354
expect(cid.codec).to.equal(reconstructedCid.codec)
54-
expect(cid.multihash.toString('hex')).to.equal(reconstructedCid.multihash.toString('hex'))
55+
expect(uint8ArrayToString(cid.multihash, 'base16')).to.equal(uint8ArrayToString(reconstructedCid.multihash, 'base16'))
5556
})
5657

5758
describe('resolver.resolve', () => {

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,20 @@
2222
"license": "MIT",
2323
"dependencies": {
2424
"buffer": "^5.6.0",
25-
"cids": "^0.8.3",
25+
"cids": "^1.0.0",
2626
"ethereumjs-account": "^3.0.0",
2727
"ethereumjs-block": "^2.2.1",
2828
"ethereumjs-tx": "^2.1.1",
2929
"merkle-patricia-tree": "^3.0.0",
30-
"multicodec": "^1.0.0",
31-
"multihashes": "^1.0.1",
32-
"multihashing-async": "^1.0.0",
30+
"multicodec": "^2.0.0",
31+
"multihashes": "^3.0.1",
32+
"multihashing-async": "^2.0.0",
3333
"rlp": "^2.2.4"
3434
},
3535
"devDependencies": {
3636
"aegir": "^25.0.0",
37-
"chai": "^4.2.0",
38-
"dirty-chai": "^2.0.1",
39-
"promisify-es6": "^1.0.3"
37+
"promisify-es6": "^1.0.3",
38+
"uint8arrays": "^1.0.0"
4039
},
4140
"contributors": [
4241
"kumavis <[email protected]>",

util/createResolver.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22
const CID = require('cids')
33
const multicodec = require('multicodec')
4-
const { Buffer } = require('buffer')
54
const createUtil = require('../util/createUtil')
65

76
const createResolver = (codec, deserialize) => {
@@ -13,7 +12,7 @@ const createResolver = (codec, deserialize) => {
1312
* Returns the value or a link and the partial mising path. This way the
1413
* IPLD Resolver can fetch the link and continue to resolve.
1514
*
16-
* @param {Buffer} binaryBlob - Binary representation of a Ethereum block
15+
* @param {Uint8Array} binaryBlob - Binary representation of a Ethereum block
1716
* @param {string} [path='/'] - Path that should be resolved
1817
* @returns {Object} result - Result of the path it it was resolved successfully
1918
* @returns {*} result.value - Value the path resolves to
@@ -48,7 +47,7 @@ const createResolver = (codec, deserialize) => {
4847

4948
const _traverse = function * (node, path) {
5049
// Traverse only objects and arrays
51-
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
50+
if (node instanceof Uint8Array || CID.isCID(node) || typeof node === 'string' ||
5251
node === null) {
5352
return
5453
}

util/createTrieResolver.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const rlp = require('rlp')
33
const EthTrieNode = require('merkle-patricia-tree/trieNode')
44
const cidFromHash = require('./cidFromHash')
55
const createResolver = require('./createResolver')
6-
const createUtil = require('./createUtil')
76

87
// A `nibble` is an array of nested keys. So for example `[2, 1, 3]` would
98
// mean an item with value `"foo"` would be in an object like this:

util/createUtil.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@ const createUtil = (codec, deserialize) => {
99
/**
1010
* Deserialize Ethereum block into the internal representation.
1111
*
12-
* @param {Buffer} serialized - Binary representation of a Ethereum block.
12+
* @param {Uint8Array|Array<Uint8Array>} serialized - Binary representation of a Ethereum block.
1313
* @returns {Object}
1414
*/
15-
deserialize,
15+
deserialize: (serialized) => {
16+
if (Array.isArray(serialized)) {
17+
if (!Buffer.isBuffer(serialized[0])) {
18+
serialized = serialized.map(s => Buffer.from(s))
19+
}
20+
} else if (!Buffer.isBuffer(serialized)) {
21+
serialized = Buffer.from(serialized)
22+
}
23+
24+
return deserialize(serialized)
25+
},
1626
/**
1727
* Serialize internal representation into a binary Ethereum block.
1828
*
1929
* @param {Object} deserialized - Internal representation of a Bitcoin block
20-
* @returns {Buffer}
30+
* @returns {Uint8Array}
2131
*/
22-
serialize: (deserialized) => deserialized._ethObj.serialize(),
32+
serialize: (deserialized) => Uint8Array.from(deserialized._ethObj.serialize()),
2333
/**
2434
* Calculate the CID of the binary blob.
2535
*

0 commit comments

Comments
 (0)