Comparing version 0.0.1 to 0.0.2
158
index.js
@@ -1,1 +0,157 @@ | ||
console.log('Hello World'); | ||
'use strict'; | ||
const request = require('request'); | ||
const signing = require('./lib/signing'); | ||
const verifications = require('./lib/verifications'); | ||
const bitwala = { | ||
client(params) { | ||
const token = params.token; | ||
const env = params.env; | ||
if (!token || !token._id || !token.secret || !env) { | ||
throw 'missing-initialization-parameters'; | ||
} | ||
if (env !== 'sandbox' && env !== 'production' && env !== 'dev') { | ||
throw 'incorrect-env'; | ||
} | ||
const instance = { | ||
_config: { | ||
token, | ||
env | ||
}, | ||
_getSignature(params) { | ||
return signing.getSignature(Object.assign(params, {secret: this._config.token.secret})); | ||
}, | ||
_getNonce() { | ||
return new Date().getTime(); | ||
}, | ||
_call(method, path, data, cb) { | ||
const endpoint = { | ||
dev: 'http://localhost:3000', | ||
sandbox: 'https://sandbox.bitwala.io', | ||
production: 'https://my.bitwala.io' | ||
}[this._config.env] + '/api/v1'; | ||
const nonce = this._getNonce(); | ||
const options = { | ||
url: endpoint + path, | ||
method: method, | ||
headers: { | ||
'Content-Type': 'application/vnd.api+json', | ||
'x-bitwala-token': this._config.token._id, | ||
'x-bitwala-signature': this._getSignature({method, nonce, url: '/api/v1' + path, data}), | ||
'x-bitwala-nonce': nonce | ||
} | ||
} | ||
if (data) { | ||
if (method === 'GET') { | ||
options.qs = data; | ||
} else if (method === 'POST') { | ||
options.body = JSON.stringify(data); | ||
} | ||
} | ||
if (cb) { | ||
request(options, function(err, res, body) { | ||
try { | ||
body = JSON.parse(body); | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (body.status === 'error') { | ||
return cb(body); | ||
} | ||
if (body.status === 'success') { | ||
return cb(null, body.data); | ||
} | ||
return cb(null, body.data); | ||
} catch (err) { | ||
console.error('Could not parse response'); | ||
return cb('Could not parse response'); | ||
} | ||
}); | ||
} else { | ||
return new Promise(function(resolve, reject) { | ||
request(options, function(err, res, body) { | ||
try { | ||
body = JSON.parse(body); | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (body.status === 'error') { | ||
return reject(body); | ||
} | ||
if (body.status === 'success') { | ||
return resolve(body.data); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
console.error('Could not parse response'); | ||
return reject('Could not parse response'); | ||
} | ||
}); | ||
}); | ||
} | ||
}, | ||
info(key, cb) { | ||
let path; | ||
if (typeof key === 'function') { | ||
path = '/info', | ||
cb = key; | ||
} else if (!key) { | ||
path = '/info'; | ||
} else if (key === 'inputs') { | ||
path = '/info/inputs'; | ||
} else if (key === 'outputs') { | ||
path = '/info/outputs'; | ||
} else if (key) { | ||
throw `not-supported`; | ||
} | ||
return instance._call('GET', path, null, cb); | ||
}, | ||
auth(cb) { | ||
return instance._call('GET', '/auth', null, cb); | ||
}, | ||
transactions: { | ||
get(query, cb) { | ||
return instance._call('GET', '/transactions', query, cb); | ||
}, | ||
create(transaction, cb) { | ||
return instance._call('POST', '/transactions', transaction, cb); | ||
}, | ||
refresh(query, cb) { | ||
return instance._call('POST', '/transactions/refresh', query, cb); | ||
}, | ||
cancel(query, cb) { | ||
return instance._call('POST', '/transactions/cancel', query, cb); | ||
} | ||
} | ||
} | ||
return instance; | ||
}, | ||
verifications | ||
} | ||
module.exports = bitwala; |
{ | ||
"name": "bitwala", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Node client for the Bitwala API (docs.bitwala.apiary.io)", | ||
@@ -23,3 +23,7 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/bitwala/api-node-client#readme" | ||
"homepage": "https://github.com/bitwala/api-node-client#readme", | ||
"dependencies": { | ||
"request": "^2.81.0", | ||
"querystring": "^0.2.0" | ||
} | ||
} |
158
README.md
@@ -1,2 +0,156 @@ | ||
# api-node-client | ||
Node client for the Bitwala API | ||
# API Node Client | ||
Node client for the [Bitwala](https://www.bitwala.io). | ||
For a detailed explanation of our API routes, please [check out the documentation](http://docs.bitwala.apiary.io) | ||
## Quick Start | ||
1. Go to https://my.bitwala.io/api-apps | ||
2. Create an app and store your token ID and secret | ||
3. `yarn add bitwala` | ||
4. | ||
```js | ||
import bitwala from 'bitwala'; | ||
const token = { | ||
_id: 'SuBnyc6d564P49Uuls', | ||
secret: 'LTdFE6hw725yh0aM4m4azACEZSpNrtl4aowFjCZetSAAPBaGN3Ecq7FgMSR5b3Va' | ||
}; | ||
const client = bitwala.client({ | ||
token: token, | ||
env: 'sandbox' | ||
}); | ||
client.info('inputs') | ||
.then(data => console.log(data)) | ||
.catch(err => console.error(err)); | ||
/* | ||
[ | ||
{ | ||
"collection": "BitcoinInvoices", | ||
"currency": "XBT" | ||
} | ||
] | ||
*/ | ||
``` | ||
## Environments | ||
The client can be initialised with the `sandbox` or `production` env. | ||
For a breakdown of the differences, see our [docs](http://docs.bitwala.apiary.io/#introduction/environments). | ||
## Methods | ||
| Endpoint | Method | Client | | ||
|-----------------|--------|-------------------| | ||
| `/info` | GET | `.info()` | | ||
| `/info/inputs` | GET | `.info('inputs')` | | ||
| `/info/outputs` | GET | `.info('outputs')` | | ||
| `/auth` | GET | `.auth()` | | ||
| `/transactions?page=1`| GET | `.transactions.get({page: 1})` | | ||
| `/transactions?transactionId=transactionId`| GET | `.transactions.get({transactionId: 'YRTvEJxAEm3MB1GI'})` | | ||
| `/transactions?ref=ref`| GET | `.transactions.get({ref: 'YRTvEJxAEm3MB1GI'})` | | ||
| `/transactions`| POST | `.transactions.create(yourTransactionObj})` | | ||
| `/transactions/refresh`| POST | `.transactions.refresh({transactionId: transactionId}})` | | ||
| `/transactions/refresh`| POST | `.transactions.refresh({transactionId: transactionId}})` | | ||
## Promises / Callbacks | ||
All api calls can be used with promises or callbacks. | ||
With callbacks | ||
```js | ||
client.info((e, data) => { | ||
if (e) { | ||
return console.error(e); | ||
} | ||
console.log(data); | ||
}) | ||
``` | ||
With promises | ||
```js | ||
client.info() | ||
.then(data => console.log(data)); | ||
``` | ||
With `await` and `async` (if you're using ES7) | ||
```js | ||
(async function () { | ||
let data; | ||
try { | ||
await data = client.info(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
console.log(data); | ||
})(); | ||
``` | ||
## Examples | ||
### Get a transaction by `ref` | ||
```js | ||
client.transactions.get({ref: 'My custom id'}); | ||
.then(r => console.log(r)) | ||
.catch(err => console.log(err)); | ||
``` | ||
### Create a transfer to Germany | ||
```js | ||
client.transactions.create({ | ||
ref: 'My custom id', | ||
webhookUrl: '...', // example: https://test.com/callbacks/bitwala | ||
inputs: [{ | ||
collection: 'BitcoinInvoices', | ||
doc: { | ||
currency: 'XBT', | ||
convertedCurrency: 'EUR', | ||
convertedAmount: 100 | ||
} | ||
}], | ||
outputs: [{ | ||
collection: 'BankTransfers', | ||
doc: { | ||
amount: 100, | ||
currency: 'EUR', | ||
reference: 'January Salary', | ||
bankAccount: { | ||
recipientType: 'INDIVIDUAL', | ||
firstName: 'Satoshi', | ||
lastName: 'Nakamoto', | ||
iban: 'DE89370400440532013000', | ||
currency: 'EUR' | ||
} | ||
} | ||
}] | ||
}).then(r => console.log(r)) | ||
.catch(err => console.log(err)); | ||
``` | ||
### Receive and verify webhooks | ||
```js | ||
import bitwala from 'bitwala'; | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
const token = { | ||
_id: 'SuBnyc6d564P49Uuls', | ||
secret: 'LTdFE6hw725yh0aM4m4azACEZSpNrtl4aowFjCZetSAAPBaGN3Ecq7FgMSR5b3Va' | ||
}; | ||
const router = express.Router(); | ||
router.use(bodyParser.json({ | ||
limit: '100kb', | ||
type: 'application/*+json' | ||
})); | ||
router.post('/callbacks/bitwala', function (req, res, next) { | ||
// !important: use body-parser | ||
const signatureOk = bitwala.verifications.verifyCallback(req, token.secret); | ||
... | ||
res.success(); | ||
}); | ||
... | ||
``` |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
19279
7
158
157
0
2
+ Addedquerystring@^0.2.0
+ Addedrequest@^2.81.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedquerystring@0.2.1(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)