Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #267 from isocolsky/feat/tx-notes
Browse files Browse the repository at this point in the history
Additional endpoints for tx notes
  • Loading branch information
matiu committed Jun 1, 2016
2 parents d4c97ff + 602d67c commit 320cf1b
Show file tree
Hide file tree
Showing 3 changed files with 338 additions and 60 deletions.
84 changes: 81 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ API._decryptMessage = function(message, encryptingKey) {
}
};

API.prototype._processTxNotes = function(notes) {
var self = this;

if (!notes) return;

var encryptingKey = self.credentials.sharedEncryptingKey;
_.each([].concat(notes), function(note) {
note.encryptedBody = note.body;
note.body = API._decryptMessage(note.body, encryptingKey);
note.encryptedEditedByName = note.editedByName;
note.editedByName = API._decryptMessage(note.editedByName, encryptingKey);
});
};

/**
* Decrypt text fields in transaction proposals
* @private
Expand Down Expand Up @@ -218,6 +232,7 @@ API.prototype._processTxps = function(txps) {
txp.hasUnconfirmedInputs = _.any(txp.inputs, function(input) {
return input.confirmations == 0;
});
self._processTxNotes(txp.note);
});
};

Expand Down Expand Up @@ -2217,8 +2232,8 @@ API.prototype.startScan = function(opts, cb) {
});
};

/*
Adds access to the current copayer
/**
* Adds access to the current copayer
* @param {Object} opts
* @param {bool} opts.generateNewKey Optional: generate a new key for the new access
* @param {string} opts.restrictions
Expand Down Expand Up @@ -2257,6 +2272,69 @@ API.prototype.addAccess = function(opts, cb) {
});
};

/**
* Get a note associated with the specified txid
* @param {Object} opts
* @param {string} opts.txid - The txid to associate this note with
*/
API.prototype.getTxNote = function(opts, cb) {
$.checkState(this.credentials);

var self = this;

opts = opts || {};
self._doGetRequest('/v1/txnotes/' + opts.txid + '/', function(err, note) {
if (err) return cb(err);
self._processTxNotes(note);
return cb(null, note);
});
};

/**
* Edit a note associated with the specified txid
* @param {Object} opts
* @param {string} opts.txid - The txid to associate this note with
* @param {string} opts.body - The contents of the note
*/
API.prototype.editTxNote = function(opts, cb) {
$.checkState(this.credentials);

opts = opts || {};
if (opts.body) {
opts.body = API._encryptMessage(opts.body, this.credentials.sharedEncryptingKey);
}
this._doPutRequest('/v1/txnotes/' + opts.txid + '/', opts, function(err, res) {
return cb(err);
});
};

/**
* Get all notes edited after the specified date
* @param {Object} opts
* @param {string} opts.minTs - The starting timestamp
*/
API.prototype.getTxNotes = function(opts, cb) {
$.checkState(this.credentials);

var self = this;

opts = opts || {};
var args = [];
if (_.isNumber(opts.minTs)) {
args.push('minTs=' + opts.minTs);
}
var qs = '';
if (args.length > 0) {
qs = '?' + args.join('&');
}

self._doGetRequest('/v1/txnotes/' + qs, function(err, notes) {
if (err) return cb(err);
self._processTxNotes(notes);
return cb(null, notes);
});
};

/**
* Returns exchange rate for the specified currency & timestamp.
* @param {Object} opts
Expand All @@ -2266,7 +2344,7 @@ API.prototype.addAccess = function(opts, cb) {
* @returns {Object} rates - The exchange rate.
*/
API.prototype.getFiatRate = function(opts, cb) {
$.checkState(this.credentials && this.credentials.isComplete());
$.checkState(this.credentials);
$.checkArgument(cb);

var self = this;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"sjcl": "^1.0.2"
},
"devDependencies": {
"bitcore-wallet-service": "~1.8.0",
"bitcore-wallet-service": "~1.8.2",
"browserify": "^9.0.3",
"chai": "^1.9.1",
"coveralls": "^2.11.2",
Expand Down
Loading

0 comments on commit 320cf1b

Please sign in to comment.