Ganache-cli: Error defining secretKey in . provider()

Created on 2 Sep 2017  路  3Comments  路  Source: trufflesuite/ganache-cli


I get an error that the length of the private key is invalid when I specify a secretKey inside of an accounts object in the TestRPC.provider parameter.

Expected Behavior

When I specify a secretKey in the accounts object of the TestRPC.provider parameter, I expect the provider to be successfully created without an error. This is how I create the provider:

web3.setProvider(TestRPC.provider({
  accounts: [
    {
      balance: 1000000,
      secretKey: 'b8f487adfb4f48cf053e01051e16b3beb6b578c445ff44db1f0196c8ce335c92'
    }
  ]
}));

Current Behavior

When I run the above code in context, I get the following error:

at Object.exports.isBufferLength (/Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:41954:39)
at Object.publicKeyCreate (/Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:41746:14)
at Object.exports.privateToPublic (/Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:2802:20)
at StateManager.createAccount (/Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:72787:25)
at Array.map (<anonymous>)
at StateManager.initialize (/Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:72700:33)
at new GethApiDouble (/Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:72197:14)
at new Provider (/Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:27170:23)
at Object.provider (/Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:40558:12)
at Object.<anonymous> (/Users/username/myproject/contracts/deploy.js:11:26) // <--- This is the first line of the above quoted code block
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:598:3

Possible Solution

I tracked down the line /Users/username/myproject/node_modules/ethereumjs-testrpc/build/lib.node.js:41746:14 from the above error, and I found that it is here:

publicKeyCreate: function (privateKey, compressed) {
  assert.isBuffer(privateKey, messages.EC_PRIVATE_KEY_TYPE_INVALID)
  assert.isBufferLength(privateKey, 32, messages.EC_PRIVATE_KEY_LENGTH_INVALID) // <--- THIS LINE

  compressed = initCompressedValue(compressed, true)

  return secp256k1.publicKeyCreate(privateKey, compressed)
},

After looking more at the isBufferLength function, it seems that the commented line checks if the privateKey is 32 characters in length, however Ethereum private keys are 64 characters in length, hence that is why isBufferLength returns an error. It seems to me that this could be the issue.

Steps to Reproduce (for bugs)

Simply use the provider method with a 64 character secretKey specified in the accounts object of the parameter and you will reproduce the error.

Context

This bug makes it impossible to use the provider with a predefined private key which renders the package non-functional for my use case.

Your Environment

  • Version used: 4.1.1
  • Environment name and version (e.g. PHP 5.4 on nginx 1.9.1): Node 8.4.0
  • Operating System and version: MacOS

Most helpful comment

Prefix secret key with '0x'(hex)
secretKey: '0xb8f487adfb4f48cf053e01051e16b3beb6b578c445ff44db1f0196c8ce335c92'. Every two characters in hex is a byte of data so you will have exactly 32 byte length.

All 3 comments

Prefix secret key with '0x'(hex)
secretKey: '0xb8f487adfb4f48cf053e01051e16b3beb6b578c445ff44db1f0196c8ce335c92'. Every two characters in hex is a byte of data so you will have exactly 32 byte length.

@kenshyx Oh of course! I should've realized this! Thank you for clarifying!

No but really @leopoldjoy, thanks for asking this!!!! you made my life, at least, much easier :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

remote-gildor picture remote-gildor  路  4Comments

SCBuergel picture SCBuergel  路  5Comments

pradeeps picture pradeeps  路  6Comments

mariam-crissi picture mariam-crissi  路  6Comments

axic picture axic  路  5Comments