From 8e5ca9f41e444a958e4650bc29c21f580fb8e897 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 23 Jul 2016 04:44:26 -0700 Subject: [PATCH] bip70: details.getData. --- lib/bcoin/bip70/index.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/bcoin/bip70/index.js b/lib/bcoin/bip70/index.js index f83a9232..555cc449 100644 --- a/lib/bcoin/bip70/index.js +++ b/lib/bcoin/bip70/index.js @@ -317,6 +317,43 @@ PaymentDetails.fromOptions = function fromOptions(options) { return new PaymentDetails().fromOptions(options); }; +PaymentDetails.prototype.setData = function setData(data, enc) { + if (data == null || Buffer.isBuffer(data)) { + this.merchantData = data; + return; + } + + if (enc === 'json') { + this.merchantData = new Buffer(JSON.stringify(data), 'utf8'); + return; + } + + assert(typeof data === 'string'); + this.merchantData = new Buffer(data, enc); +}; + +PaymentDetails.prototype.getData = function getData(enc) { + var data = this.merchantData; + + if (!data) + return; + + if (!enc) + return data; + + if (enc === 'json') { + data = data.toString('utf8'); + try { + data = JSON.parse(data); + } catch (e) { + return; + } + return data; + } + + return data.toString(enc); +}; + PaymentDetails.prototype.fromRaw = function fromRaw(data) { var p = new ProtoReader(data); var op, output; @@ -430,6 +467,9 @@ Payment.fromOptions = function fromOptions(options) { return new Payment().fromOptions(options); }; +Payment.prototype.setData = PaymentDetails.prototype.setData; +Payment.prototype.getData = PaymentDetails.prototype.getData; + Payment.prototype.fromRaw = function fromRaw(data) { var p = new ProtoReader(data); var tx, op, output;