2

I have a local EOS network and I am trying to make my application work through a web page. I am using eosjs version 16.0.9. I downloaded it and ran npm install. I am able to run the following code ok both using node and from a web page:

const Eos = require('eosjs')
const fs = require("fs")

const config = {
   chainId:    'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f',
   keyProvider: ['myprivateky'],
   httpEndpoint: 'http://127.0.0.1:7777',
   expireInSeconds: 60,
   broadcast: true,
   verbose: false, // API activity
   sign: true
}

const eos = Eos(config)

const eosinfo = eos.getInfo((error, info) => {
console.log(error, info);})

// Promise
eos.getBlock(1).then(result => console.log(result)).catch(error => console.error(error)

The above code works fine. I am able to see the output both on my terminal and javascript console. When I try to call an action from a smart contract (using the syntax from Readme.md that came with EOSJS) I am running into some errors. This is the code I added:

const wasm = fs.readFileSync('/Users/Jyothi/Documents/contracts/ktc/ktc.wasm')
const abi = fs.readFileSync('/Users/Jyothi/Documents/contracts/ktc/ktc.abi')

eos.setcode('ktc', 0, 0, wasm) 

I get the following error when I run it with node:

(node:40092) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: keyProvider function is needed for private key lookup at /Users/Jyothi/Sites/node_modules/eosjs/lib/index.js:443:17 at at process._tickCallback (internal/process/next_tick.js:188:7) (node:40092) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:40092) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Any idea what I am missing here? I clearly need to send the keys but where and how? Also, are there any tutorials/examples on how to install EOSJS and integrate it with front end? I looked around but wasn't very successful. I am very new to node.js and so trying to figure this out

3
  • your answer in your question only! yes it might be a rude comment and sorry for that...as you passing your private key in config constant the same way you can do that in your second code like const eos = Eos(config) the way you done in previous code and then call setcode....it will be success....and yes if you are new to nodejs it's really good that you are trying to learn some cool stuff...happy coding :-)
    – Mr.Y
    Commented Nov 30, 2018 at 5:08
  • eosjs doc is here: eosio.github.io/eosjs
    – Tong Shen
    Commented Nov 30, 2018 at 16:04
  • Is this solved? I am having the same problem...
    – user2683
    Commented Dec 2, 2018 at 13:37

1 Answer 1

2

The Root Cause Analysis

The error you got came from

if(missingKeys.length !== 0) {
  assert(typeof keyProvider === 'function',
    'keyProvider function is needed for private key lookup')

  // const pubkeys = missingKeys.map(key => ecc.PublicKey(key).toStringLegacy())
  keyProvider({pubkeys: missingKeys})
    .forEach(pvt => { pvts.push(pvt) })
}

From the source code, I would assume the key you put in config.keyProvider is not the key for account 'ktc' in your server http://127.0.0.1:7777.

Suggested Solution

What I would suggest you to debug:

  1. make sure account 'ktc' exists in your local server and created with the public key corresponding to your private key.

    If you confirmed step 1 and the error still not solved:

  2. try passing a function in config.keyProvider like:

    (params)=>{
       console.log('missingKeys:', params.pubkeys);
       return ['your private key']
    }
    

    Then you can have the public key it is requiring.

    Append the corresponding private key in the config.keyProvider array should solve the issue.

IMO, this should at least lead you to the correct path.

1
  • Thank you so much for such a detailed answer. I did some troubleshooting based on your suggestion and I realized that in config.keyPorivder I was passing the public key instead of the private key. I changed it to private key and it works now :)
    – Jyothi
    Commented Dec 2, 2018 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.