Skip to content

Commit

Permalink
Added method validatePostback to validate postback checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
vividvilla committed Mar 12, 2017
1 parent df868ca commit 2f6f7e9
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,37 @@ var KiteConnect = function(api_key, options) {
);
};

/**
* Validate postback data checksum
* @method validatePostback
* @memberOf KiteConnect
* @instance
* @param {object} postback_data Postback data received. Must be an json object with required keys order_id, checksum and order_timestamp
* @param {string} api_secret Api secret of the app
* @returns {bool} Return true if checksum matches else false
* @throws Throws an error if the @postback_data or @api_secret is invalid
*/
self.validatePostback = function(postback_data, api_secret) {
if (!postback_data || !postback_data.checksum || !postback_data.order_id ||
!postback_data.order_timestamp || !api_secret) {
throw new Error("Invalid postback data or api_secret");
}

var inputString = postback_data.order_id + postback_data.order_timestamp + api_secret;
var checksum;
try {
checksum = crypto.createHash("sha256").update(inputString).digest("hex");
} catch (e) {
throw(e)
}

if (postback_data.checksum === checksum) {
return true;
} else {
return false;
}
}

function parseHistorical(jsonData) {
var results = [];
for(var i=0; i<jsonData.data.candles.length - 1; i++) {
Expand Down

0 comments on commit 2f6f7e9

Please sign in to comment.