Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ Example Usage:
`npm install localbitcoins-node`

```javascript
var LBCClient = require(localbitcoins-node);
var LBCClient = require('localbitcoins-node');
var lbc = new LBCClient(api_key, api_secret);

var ad_id; //set to value when applicable
var params = {};


// Display user's info
lbc.api('myself', null, function(error, data) {
lbc.api('myself', ad_id, params, function(error, data) {
if(error) {
console.log(error);
}
Expand All @@ -27,7 +31,7 @@ lbc.api('myself', null, function(error, data) {

```

To-Do:
To-Do:
- Get different methods working with querystring parameters added to message

Credit:
Expand Down
86 changes: 72 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var nonce = (new Date).getTime();

function LBCClient(key, secret, otp) {
var self = this;

var config = {
url: 'https://localbitcoins.com/api',
key: key,
Expand All @@ -22,22 +22,22 @@ function LBCClient(key, secret, otp) {
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
function api(method, params, callback) {
function api(method, ad_id, params, callback) {
var methods = {
onlineAds: ['buy-bitcoins-online'],
public: ['countrycodes'],
private: ['ad-get', 'ad-get/ad_id', 'myself', 'ads',
'dashboard', 'dashboard/released', 'dashboard/canceled', 'dashboard/closed',
'dashboard', 'dashboard/released', 'dashboard/canceled', 'dashboard/closed',
'dashboard/released/buyer', 'dashboard/canceled/buyer', 'dashboard/closed/buyer',
'dashboard/released/seller', 'dashboard/canceled/seller', 'dashboard/closed/seller',
'wallet-send'
'wallet-send', 'wallet'
]
};
if(methods.public.indexOf(method) !== -1) {
return publicMethod(method, params, callback);
return publicMethod(method, params, ad_id, callback);
}
else if(methods.private.indexOf(method) !== -1) {
return privateMethod(method, params, callback);
return privateMethod(method, params, ad_id, callback);
}
else {
throw new Error(method + ' is not a valid API method.');
Expand All @@ -51,10 +51,16 @@ function LBCClient(key, secret, otp) {
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
function publicMethod(method, params, callback) {
function publicMethod(method, params, ad_id, callback) {
params = params || {};

var path = '/' + method;
var path;
if (ad_id) {
path = '/' + method + '/' + ad_id;
} else {
path = '/' + method;
}

var url = config.url + path;

return rawRequest(url, {}, params, callback);
Expand All @@ -67,10 +73,17 @@ function LBCClient(key, secret, otp) {
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
function privateMethod(method, params, callback) {
function privateMethod(method, params, ad_id, callback) {
params = params || {};

var path = '/' + method;
var path;

if (ad_id) {
path = '/' + method + '/' + ad_id;
} else {
path = '/' + method;
}

var url = config.url + path;

var signature = getMessageSignature(path, params, nonce);
Expand All @@ -82,7 +95,7 @@ function LBCClient(key, secret, otp) {
'Apiauth-Signature': signature
};

return rawRequest(url, headers, params, callback);
return rawRequest(url, headers, params, method, callback);
}

/**
Expand All @@ -108,15 +121,59 @@ function LBCClient(key, secret, otp) {
* @param {Function} callback A callback function to call when the request is complete
* @return {Object} The request object
*/
function rawRequest(url, headers, params, callback) {
function rawRequest(url, headers, params, method, callback) {

var gets = ['ad-get', 'dashboard', 'dashboard/released', 'dashboard/canceled',
'dashboard/closed', 'dashboard/released/buyer', 'dashboard/canceled/buyer',
'dashboard/closed/buyer', 'dashboard/released/seller', 'dashboard/canceled/seller',
'dashboard/closed/seller', 'wallet'];
var posts = [ 'ad-get/ad_id', 'myself', 'ads',
'wallet-send', 'wallet-balance', 'wallet-addr'];

if (posts.indexOf(method) !== -1) {

var options = {
url: url + '/',
headers: headers,
form: params,
};

var req = request.post(options, function(error, response, body) {
if(typeof callback === 'function') {
var data;

if(error) {
callback.call(self, new Error('Error in server response: ' + JSON.stringify(error)), null);
return;
}

try {
data = JSON.parse(body);
}
catch(e) {
callback.call(self, new Error('Could not understand response from server: ' + body), null);
return;
}

if(data.error && data.error.length) {
callback.call(self, data.error, null);
}
else {
callback.call(self, null, data);
}
}
});

return req;

} else {

var options = {
url: url + '/',
headers: headers,
form: params,
};

var req = request.post(options, function(error, response, body) {
var req = request.get(options, function(error, response, body) {
if(typeof callback === 'function') {
var data;

Expand Down Expand Up @@ -144,6 +201,7 @@ function LBCClient(key, secret, otp) {

return req;
}
}

self.api = api;
self.publicMethod = publicMethod;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "localbitcoins-node",
"version": "0.0.3",
"version": "0.1.1",
"description": "LocalBitcoins API wrapper for NodeJS",
"keywords": [
"localbitcoins",
Expand All @@ -9,7 +9,7 @@
"bitcoin"
],
"author": "Anthony Mayfield (https://github.com/mrmayfield)",
"contributors": [],
"contributors": ["Vlad Nistor (https://github.com/vnistor)"],
"license": "MIT",
"dependencies": {
"querystring": ">=0.2.0",
Expand All @@ -19,7 +19,7 @@
"type": "git",
"url": "https://github.com/mrmayfield/localbitcoins-node"
},
"main": "localbitcoins.js",
"main": "index.js",
"engines": {
"node": ">=0.10"
},
Expand Down