Waffle icon indicating copy to clipboard operation
Waffle copied to clipboard

Error: Expected private key to be an Uint8Array

Open D4nte opened this issue 3 years ago • 3 comments

I am guessing I am missing something obvious...

I just want to test some usage of Web3 provider via ethers.js. No smart contract at this stage (maybe waffle is not the best for this use case?).

I am getting the following error:

Error: Expected private key to be an Uint8Array

    at assert (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/secp256k1/lib/index.js:18:20)
    at isUint8Array (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/secp256k1/lib/index.js:22:3)
    at Object.privateKeyVerify (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/secp256k1/lib/index.js:66:7)
    at Object.privateKeyVerify (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/ethereum-cryptography/src/shims/hdkey-secp256k1v3.ts:4:20)
    at HDKey.set (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/ethereum-cryptography/vendor/hdkey-without-crypto.js:46:26)
    at Function.Object.<anonymous>.HDKey.fromMasterSeed (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/ethereum-cryptography/vendor/hdkey-without-crypto.js:194:20)
    at Function.Object.<anonymous>.EthereumHDKey.fromMasterSeed (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/ethereumjs-wallet/hdkey.js:20:26)
    at new StateManager (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/ganache-core/lib/statemanager.js:58:23)
    at new GethApiDouble (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/ganache-core/lib/subproviders/geth_api_double.js:21:33)
    at new Provider (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/ganache-core/lib/provider.js:25:25)
    at Object.provider (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/ganache-core/public-exports.js:13:12)
    at new MockProvider (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/@ethereum-waffle/provider/dist/cjs/MockProvider.js:10:39)
    at Object.<anonymous> (/home/froyer/src/status-im/js-waku/examples/eth-dm/src/crypto.spec.ts:12:20)
    at Promise.then.completed (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-circus/build/utils.js:276:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-circus/build/utils.js:216:10)
    at _callCircusTest (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-circus/build/run.js:212:40)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at _runTest (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-circus/build/run.js:149:3)
    at _runTestsForDescribeBlock (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-circus/build/run.js:63:9)
    at run (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-circus/build/run.js:25:3)
    at runAndTransformResultsToJestFormat (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:176:21)
    at jestAdapter (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:109:19)
    at runTestInternal (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-runner/build/runTest.js:380:16)
    at runTest (/home/froyer/src/status-im/js-waku/examples/eth-dm/node_modules/jest-runner/build/runTest.js:472:34)

I am just signing a message using Web3:

import {
  createEthDmPublicationMessage,
  generateEthDmKeyPair,
  verifyEthDmPublicKey
} from './crypto';
import { MockProvider } from "ethereum-waffle";
import { waffleJest } from '@ethereum-waffle/jest';

expect.extend(waffleJest);

test('Signature of Eth-DM key is verifiable', async () => {
  console.log("get wallet")
  const [wallet] = new MockProvider().getWallets();
  console.log("Generate Keys")
  const ethDmKeys = await generateEthDmKeyPair(wallet);

  console.log("Create EthDm message")
  const ethDmMsg = await createEthDmPublicationMessage(wallet, ethDmKeys.publicKey);

  console.log("Verify EthDm message")
  const res = verifyEthDmPublicKey(ethDmMsg)

  expect(res).toBe(true);
});

You can find the code here: 264c623 (#204)

D4nte avatar Jun 10 '21 06:06 D4nte

any update on this?

jontonsoup avatar Aug 16 '21 19:08 jontonsoup

any update on this?

Are you facing the same issue? I haven't tried since the dependency update.

D4nte avatar Aug 17 '21 03:08 D4nte

I had the same issue. Turns out this is a bug in jest - in more particular jest-environment-jsdom@26 and above.

let t = Buffer.alloc(0)
console.log(t instanceof Uint8Array)

prints true in jest-environment-jsdom@25 prints false in jest-environment-jsdom@26

WORKAROUND

  1. Add jest-environment-jsdom.js containing the following:
// Stolen from https://github.com/facebook/jest/issues/7780#issuecomment-615890410
// and https://github.com/firebase/firebase-js-sdk/issues/3096#issuecomment-827741103
// Overcomes error from jest internals...: https://github.com/facebook/jest/issues/7780
"use strict";

const JSDOMEnvironment = require("jest-environment-jsdom");

class MyEnvironment extends JSDOMEnvironment {
  constructor(config) {
    super(
      Object.assign({}, config, {
        globals: Object.assign({}, config.globals, {
          Uint32Array: Uint32Array,
          Uint8Array: Uint8Array,
          ArrayBuffer: ArrayBuffer
        })
      })
    );
  }

  async setup() {}

  async teardown() {}
}

module.exports = MyEnvironment;
  1. Add our new custom environment to config
  • add to either package.json or jest.config.js:
jest: {
  testEnvironment: './test/jest-environment-jsdom.js'`  
}
  • CRA (create-react-app), you will need to use the CLI param to override the default test environment
"scripts": {
  "test": "react-scripts test --env=./test/jest-environment-jsdom.js" 
}

rolandsaven avatar Nov 26 '21 18:11 rolandsaven