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

Commit

Permalink
fix: replace node buffers with uint8arrays (#69)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain authored Aug 4, 2020
1 parent 8dc22d8 commit a8fc4f5
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 37 deletions.
4 changes: 1 addition & 3 deletions eth-account-snapshot/test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { expect } = require('aegir/utils/chai')
const dagEthAccount = require('../index')
const resolver = dagEthAccount.resolver
const Account = require('ethereumjs-account').default
Expand Down
4 changes: 1 addition & 3 deletions eth-block-list/test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { expect } = require('aegir/utils/chai')
const CID = require('cids')
const multihash = require('multihashes')
const multicodec = require('multicodec')
Expand Down
7 changes: 3 additions & 4 deletions eth-block/test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { expect } = require('aegir/utils/chai')
const CID = require('cids')
const EthBlockHeader = require('ethereumjs-block/header')
const multihash = require('multihashes')
const multicodec = require('multicodec')
const { Buffer } = require('buffer')
const uint8ArrayToString = require('uint8arrays/to-string')

const ipldEthBlock = require('../index')
const resolver = ipldEthBlock.resolver
Expand Down Expand Up @@ -51,7 +50,7 @@ describe('IPLD format resolver (local)', () => {
const reconstructedCid = new CID(encodedCid)
expect(cid.version).to.equal(reconstructedCid.version)
expect(cid.codec).to.equal(reconstructedCid.codec)
expect(cid.multihash.toString('hex')).to.equal(reconstructedCid.multihash.toString('hex'))
expect(uint8ArrayToString(cid.multihash, 'base16')).to.equal(uint8ArrayToString(reconstructedCid.multihash, 'base16'))
})

describe('resolver.resolve', () => {
Expand Down
4 changes: 1 addition & 3 deletions eth-state-trie/test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { expect } = require('aegir/utils/chai')
const Account = require('ethereumjs-account').default
const Trie = require('merkle-patricia-tree')
const multicodec = require('multicodec')
Expand Down
4 changes: 1 addition & 3 deletions eth-storage-trie/test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { expect } = require('aegir/utils/chai')
const CID = require('cids')
const Trie = require('merkle-patricia-tree')
const multicodec = require('multicodec')
Expand Down
4 changes: 1 addition & 3 deletions eth-tx-trie/test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { expect } = require('aegir/utils/chai')
const CID = require('cids')
const EthBlock = require('ethereumjs-block')
const EthTx = require('ethereumjs-tx').Transaction
Expand Down
4 changes: 1 addition & 3 deletions eth-tx/test/resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const { expect } = require('aegir/utils/chai')
const Transaction = require('ethereumjs-tx').Transaction
const dagEthTx = require('../index')
const resolver = dagEthTx.resolver
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@
"license": "MIT",
"dependencies": {
"buffer": "^5.6.0",
"cids": "^0.8.3",
"cids": "^1.0.0",
"ethereumjs-account": "^3.0.0",
"ethereumjs-block": "^2.2.1",
"ethereumjs-tx": "^2.1.1",
"merkle-patricia-tree": "^3.0.0",
"multicodec": "^1.0.0",
"multihashes": "^1.0.1",
"multihashing-async": "^1.0.0",
"multicodec": "^2.0.0",
"multihashes": "^3.0.1",
"multihashing-async": "^2.0.0",
"rlp": "^2.2.4"
},
"devDependencies": {
"aegir": "^25.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"promisify-es6": "^1.0.3"
"promisify-es6": "^1.0.3",
"uint8arrays": "^1.0.0"
},
"contributors": [
"kumavis <aaron@kumavis.me>",
Expand Down
5 changes: 2 additions & 3 deletions util/createResolver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'
const CID = require('cids')
const multicodec = require('multicodec')
const { Buffer } = require('buffer')
const createUtil = require('../util/createUtil')

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

const _traverse = function * (node, path) {
// Traverse only objects and arrays
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
if (node instanceof Uint8Array || CID.isCID(node) || typeof node === 'string' ||
node === null) {
return
}
Expand Down
1 change: 0 additions & 1 deletion util/createTrieResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const rlp = require('rlp')
const EthTrieNode = require('merkle-patricia-tree/trieNode')
const cidFromHash = require('./cidFromHash')
const createResolver = require('./createResolver')
const createUtil = require('./createUtil')

// A `nibble` is an array of nested keys. So for example `[2, 1, 3]` would
// mean an item with value `"foo"` would be in an object like this:
Expand Down
18 changes: 14 additions & 4 deletions util/createUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ const createUtil = (codec, deserialize) => {
/**
* Deserialize Ethereum block into the internal representation.
*
* @param {Buffer} serialized - Binary representation of a Ethereum block.
* @param {Uint8Array|Array<Uint8Array>} serialized - Binary representation of a Ethereum block.
* @returns {Object}
*/
deserialize,
deserialize: (serialized) => {
if (Array.isArray(serialized)) {
if (!Buffer.isBuffer(serialized[0])) {
serialized = serialized.map(s => Buffer.from(s, s.byteOffset, s.byteLength))
}
} else if (!Buffer.isBuffer(serialized)) {
serialized = Buffer.from(serialized, serialized.byteOffset, serialized.byteLength)
}

return deserialize(serialized)
},
/**
* Serialize internal representation into a binary Ethereum block.
*
* @param {Object} deserialized - Internal representation of a Bitcoin block
* @returns {Buffer}
* @returns {Uint8Array}
*/
serialize: (deserialized) => deserialized._ethObj.serialize(),
serialize: (deserialized) => Uint8Array.from(deserialized._ethObj.serialize()),
/**
* Calculate the CID of the binary blob.
*
Expand Down

0 comments on commit a8fc4f5

Please sign in to comment.