bigchaindb / js-bigchaindb-driver

Official BigchainDB JavaScript driver for Node.js and the browser
https://docs.bigchaindb.com/projects/js-driver/en/latest/usage.html
Apache License 2.0
216 stars 92 forks source link

Crash due to Buffer required in dependency #264

Closed DavidEdwards closed 6 years ago

DavidEdwards commented 6 years ago

The problem

The driver is passing a Uint8Array from nacl into base-x. This is an unexpected datatype and is causing a crash.

/home/david/repo/bigchaindb-test/node_modules/base-x/index.js:29
    if (!Buffer.isBuffer(source)) throw new TypeError('Expected Buffer')

Where it is

The issue begins here in the driver code.

nacl is creating a Uint8Array here.

That is creating the Exception in base-x here.

Reproduce

Dependencies

{
  "dependencies": {
    "bigchaindb-driver": "^4.1.0"
  }
}
const base58 = require('bs58')
const nacl = require('tweetnacl')
const keyPair = nacl.sign.keyPair()
console.log("keyPair.publicKey", keyPair.publicKey);
console.log("keyPair.secretKey", keyPair.secretKey);

publicKey = base58.encode(keyPair.publicKey)
console.log("publicKey", publicKey);
privateKey = base58.encode(keyPair.secretKey.slice(0, 32))
console.log("privateKey", privateKey);

How to fix

If you wrap the Uint8Array in a Buffer, it will work without crashing.

const base58 = require('bs58')
const nacl = require('tweetnacl')
const keyPair = nacl.sign.keyPair()
console.log("keyPair.publicKey", keyPair.publicKey);
console.log("keyPair.secretKey", keyPair.secretKey);

publicKey = base58.encode(new Buffer(keyPair.publicKey))
console.log("publicKey", publicKey);
privateKey = base58.encode(new Buffer(keyPair.secretKey.slice(0, 32)))
console.log("privateKey", privateKey);

Output

keyPair.publicKey Uint8Array [
  210,
  154,
...snip...
  87,
  91 ]
keyPair.secretKey Uint8Array [
  119,
  13,
...snip...
  87,
  91 ]
publicKey FB7R29t5wQL5SaxtjMckc1BuKLxGZy56XnC6kvzX5enN
privateKey 91jXtHyiCemrZxu2XLtAT2iiMJuqo5EFAL1h3k