bitpay / node-bitpay-client

A Node.js module and command line client for interacting with BitPay's Cryptographically Secure API
102 stars 95 forks source link

Unspecified "error" event #72

Closed yogiben closed 8 years ago

yogiben commented 8 years ago

Followed tutorial

var bitpay  = require('bitpay');
var privkey = fs.readFileSync('path/to/private.key');
var client  = bitpay.createClient(privkey);

the app crashes with the following error message

TypeError: Uncaught, unspecified "error" event.
    at TypeError (<anonymous>)
    at emit (events.js:74:15)
    at /Users/gringo/dev/lab/node/node_modules/bitpay/lib/rest-client.js:82:26
    at Request._callback (/Users/gringo/dev/lab/node/node_modules/bitpay/lib/rest-client.js:173:40)
    at Request.self.callback (/Users/gringo/dev/lab/node/node_modules/bitpay/node_modules/request/request.js:198:22)
    at Request.emit (events.js:98:17)
    at Request.<anonymous> (/Users/gringo/dev/lab/node/node_modules/bitpay/node_modules/request/request.js:1063:14)
    at Request.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (/Users/gringo/dev/lab/node/node_modules/bitpay/node_modules/request/request.js:1009:12)
    at IncomingMessage.emit (events.js:117:20)
unusualbob commented 8 years ago

Have you tried catching the error on the client?

client.on('error', function(err) {
    // handle client errors here
    console.log(err);
});
yogiben commented 8 years ago

@unusualbob good idea.

I thought the error would be logged by

client.on('ready', function() {
    client.get('invoices', function(err, invoices) {
        console.log(err || invoices);
    });
});

the error is { error: 'Unauthorized sin' }.

yogiben commented 8 years ago

@unusualbob is there a list of error messages anywhere?

Googling unauthorized sin does not return so much

philosodad commented 8 years ago

unauthorized sin is a bitpay specific error that indicates there is a problem with your private key. How did you generate your private key?

ionux commented 8 years ago

@yogiben no we don't publish a list of our error messages. It's possible you haven't paired with your account, maybe?

philosodad commented 8 years ago

@yogiben what @ionux said seems like the most likely cause. If you did not go through the steps in the CLI tutorial to pair your key with your bitpay account (and if you don't have an account on either bitpay.com or test.bitpay.com) then your private key will not be authorized. The "SIN" is an identification number that is created from your public key, and must be associated with your account through the pairing process. The ruby client guide has a short section on authentication that you might find helpful.

yogiben commented 8 years ago

@philosodad @ionux

I followed the steps in this repo's readme:

bitpay keygen
bitpay pair

I pair the key (confirmed on my Bitpay dashboard) and am able to successfully return invoices with the example command

bitpay request -T merchant -R invoices -P '{"dateStart":"2014-01-01"}'

I initialize in my project

var bitpay  = require('bitpay');
var privkey = fs.readFileSync('path/to/private.key');
var client  = bitpay.createClient(privkey);

and log the error

client.on('error', function(err) {
    // handle client errors here
    console.log(err);
});

Logging the output of the command

{ domain: null,
  _events: {},
  _maxListeners: 10,
  defaults:
   { getTokens: true,
     signRequests: true,
     config:
      { apiHost: 'bitpay.com',
        apiPort: 443,
        forceSSL: true } },
  options:
   { getTokens: true,
     signRequests: true,
     config:
      { apiHost: 'bitpay.com',
        apiPort: 443,
        forceSSL: true } },
  privkey: <Buffer 12 34 ...>,
  tokens: {},
  apiHost: 'bitpay.com',
  apiPort: 443,
  facade: 'merchant' }

I'm gone through the setup multiple times.

The pairing should definitely have worked as I'm able to successfully make requests with the cli.

yogiben commented 8 years ago

@ionux who's currently maintaining this for bitpay?

ionux commented 8 years ago

@yogiben send an email to support@bitpay.com and a ticket will be routed to the correct engineer.

braydonf commented 8 years ago

https://github.com/bitpay/node-bitpay-client/pull/73 fixes an issue regarding utf-8 and has performance improvements.

However I think the problem here is that path/to/private.key is likely encrypted, and needs to be decrypted first, similar to: https://github.com/bitpay/node-bitpay-client/blob/master/lib/cli-utils.js#L13

yogiben commented 8 years ago

@ionux Bitpay support have been unhelpful so far. It's taken over a week since the first request and they haven't connected me with anyone technical yet.

This is a video of my interpretation of the walkthrough:

http://quick.as/zvypcqwrp

I'd be very curious to see someone set up a working example.

@braydonf I tried using the latest commit but got the same error.

unusualbob commented 8 years ago

I've opened a pull request with better documentation, can you take a look at that and let me know if it helps? You can view it here on my fork: https://github.com/unusualbob/node-bitpay-client/blob/betterReadme/README.md#module

yogiben commented 8 years ago

Thank you so much @unusualbob !

Finally got it to work.

I had previously tried using bitauth.decrypt after looking at source but had got the Non-base58 character error mentioned here.

Had to generate new keys and all was fine.