question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I’m trying to test this library to see how I could use it with CryptoJS alongside other stuff and it seems to not like it even when I convert the hashes into buffers

test('Simple', () => {
  const data = ['Lorem', 'Ipsum', 'Dolore', 'Sit', 'Amet'];
  const leaves = data.map(d => Buffer.from(SHA256(d).toString(), 'hex'));
  console.log(leaves);
  let tree = new MerkelTree(leaves, SHA256);
}

I get this error:

TypeError [ERR_INVALID_ARG_TYPE]: The "list[0]" argument must be one of type Array, Buffer, or Uint8Array. Received type object
    at Function.concat (buffer.js:479:13)
    at MerkleTree.createHashes (/home/maxie/Dropbox/Projects/SaCoin/node_modules/merkletreejs/index.js:57:23)
    at MerkleTree.createHashes (/home/maxie/Dropbox/Projects/SaCoin/node_modules/merkletreejs/index.js:86:10)
    at new MerkleTree (/home/maxie/Dropbox/Projects/SaCoin/node_modules/merkletreejs/index.js:37:10)
    at Object.<anonymous>.test (/home/maxie/Dropbox/Projects/SaCoin/tests/MerkelTree.test.js:9:14)
    at Object.asyncFn (/home/maxie/Dropbox/Projects/SaCoin/node_modules/jest-jasmine2/build/jasmine_async.js:108:37)
    at resolve (/home/maxie/Dropbox/Projects/SaCoin/node_modules/jest-jasmine2/build/queue_runner.js:56:12)
    at new Promise (<anonymous>)
    at mapper (/home/maxie/Dropbox/Projects/SaCoin/node_modules/jest-jasmine2/build/queue_runner.js:43:19)
    at promise.then (/home/maxie/Dropbox/Projects/SaCoin/node_modules/jest-jasmine2/build/queue_runner.js:87:41)

This error says that the leave inputs aren’t buffers but they are (confirmed by expect(leaves[0] instanceof Buffer).toBeTruthy(); passing) so I’m perplexed as to why that is happening?

I don’t want to use crypto because all the crypto functions in my project are based on CryptoJS and work perfectly well.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
miguelmotacommented, Jul 12, 2018

Although the CryptoJS constructor type is called Buffer, CryptoJS buffers are not actually native Node.js buffers which is what’s causing the error.

console.log(Buffer.isBuffer(SHA256('hello'))) // false

To get around this you can wrap the hashing function in order to return a Buffer.

const SHA256 = require('crypto-js/sha256')
const MerkleTree = require('merkletreejs')

const bufferify = f => x => Buffer.from(f(x.toString()).toString(), 'hex')

const hashFn = bufferify(SHA256)
const leaves = ['a', 'b', 'c'].map(hashFn)
const tree = new MerkleTree(leaves, hashFn)

console.log(tree)

We should however support CryptoJS in a future release

@Berkmann18 @DonleeIO

1reaction
miguelmotacommented, Aug 3, 2018

Latest release supports CryptoJS algos

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to use CryptoJS in javascript - node.js - Stack Overflow
Here's a sample on how to use CryptoJs in webclient: // INIT var myString = "blablabla Card game bla"; var myPassword = "myPassword"; ......
Read more >
CryptoJS - CryptoJS
CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns.
Read more >
CryptoJS Tutorial For Dummies | Davide Barranca
CryptoJS makes large use of Word Array - that is, arrays of 32-bits words (instances of the CryptoJS.lib.WordArray ); few useful functions:.
Read more >
brix/crypto-js: JavaScript library of crypto standards. - GitHub
JavaScript library of crypto standards. Contribute to brix/crypto-js development by creating an account on GitHub.
Read more >
crypto-js - npm
JavaScript library of crypto standards.. Latest version: 4.1.1, last published: a year ago. Start using crypto-js in your project by running ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found