Compare commits

...

4 Commits
master ... dev

Author SHA1 Message Date
Nicolas Dorier
1ebc542be6
Merge pull request #32 from johnbailon/update_readme
Docs: Fix instructions on generating private key
2019-10-06 23:20:11 +09:00
John Bailon
f9f3624305 Docs: Fix instructions on generating private key 2019-10-06 22:12:04 +08:00
Daniel
78e56ee75e Docs: Make easier to understand, use ES6 Javascript 2018-10-18 15:36:50 +02:00
Daniel
1017448362 Keymanagment: new Buffer() is deprecated, use new Buffer.from() instead
Also, you only generate your initial keypair once, so every time you use the key in your app, it is not derived, from the previously keypair var, but e.g. from environment variables
2018-10-18 13:58:29 +02:00

View File

@ -5,58 +5,68 @@
npm install https://github.com/tanjalo/node-btcpay
```
## Pairing
## Private key generation
* Generate and save private key:
```js
let btcpay = require('btcpay')
var keypair = btcpay.crypto.generate_keypair()
```
* Create client:
```js
var client = new btcpay.BTCPayClient('https://btcpayserverhostname', keypair)
const btcpay = require('btcpay')
const privatekey = btcpay.crypto.generate_keypair().getPrivate('hex')
console.log(`PRIVATEKEY: ${privatekey}`)
```
Store the value of "priv" in a save place, e.g. environment variables
## Pairing
After generating your private key, you have to pair your client with your BTCPay store:
* On BTCPay Server > Stores > Settings > Access Tokens > Create a new token, (leave PublicKey blank) > Request pairing
* Copy pairing code:
* Pair client to server and save returned token:
```js
client.pair_client(<pairing-code>).then(res => console.log(res))
>>> { merchant: '6gi59fB1LKxHuyY29m8tR6tRysWppk9TnuoM7wT77Las' }
const btcpay = require('btcpay')
const keypair = btcpay.crypto.load_keypair(new Buffer.from(<PRIVATEKEY>, 'hex'))
const client = new btcpay.BTCPayClient(<BTCPAYURL>, btcpay.crypto.load_keypair(Buffer.from(<PRIVATEKEY>, 'hex')))
// Pair client to server
client
.pair_client(<PAIRINGCODE>)
.then(res => console.log(res))
.catch(err => console.log(err))
>>> { merchant: 'XXXXXX' }
```
* Recreate client:
Store the value of "merchant" in a save place, e.g. environment variables
## Recreating a client
After pairing your client to the store, you can recreate the client as needed and use it in your code
```js
var client = new btcpay.BTCPayClient('https://btcpayserverhostname', keypair, {merchant: '6gi59fB1LKxHuyY29m8tR6tRysWppk9TnuoM7wT77Las'})
const btcpay = require('btcpay')
const keypair = btcpay.crypto.load_keypair(new Buffer.from(<PRIVATEKEY>, 'hex'))
// Recreate client
const client = new btcpay.BTCPayClient(<BCTPAYURL>, keypair, {merchant: <MERCHANT>})
```
## Creating a client
### Get rates
Fetches current rates from BitcoinAverage (using your BTCPayServer)
```js
var client = new btcpay.BTCPayClient('https://btcpayserverhostname', keypair, {merchant: '6gi59fB1LKxHuyY29m8tR6tRysWppk9TnuoM7wT77Las'})
client.get_rates('BTC_USD')
.then(rates => console.log(rates))
.catch(err => console.log(err))
```
## Get rates
### Create invoice
See [BitPay Invoice API documentation](https://bitpay.com/api#resource-Invoices)
```js
client.get_rates('BTC_USD').then(rates => console.log(rates))
client.create_invoice({price: 20, currency: 'USD'})
.then(invoice => console.log(invoice.url))
.catch(err => console.log(err))
```
The first argument accept comma-separated list of currency pair.
## Create invoice
See BitPay API documentation: https://bitpay.com/api#resource-Invoices
### Get invoice
```js
client.create_invoice({"price": 20, "currency": "USD"}).then(invoice => console.log(invoice.url))
```
## Get invoice
```js
client.get_invoice(<invoice-id>).then(invoice => console.log(invoice.status))
```
## Key Management
```js
var privateKey = keypair.getPrivate().toString('hex')
var keypair = btcpay.crypto.load_keypair(new Buffer(privateKey, "hex"))
client.get_invoice(<invoice-id>)
.then(invoice => console.log(invoice.status))
.catch(err => console.log(err))
```