From 3931e73da4dc670fb91ff953462a231745f9e08f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 6 Aug 2014 12:49:48 -0700 Subject: [PATCH] paypro: debugging endianness issue in bignum (browser vs node). --- js/models/core/Wallet.js | 4 ++++ test/test.PayPro.js | 44 +++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 2558067e2..e89dc437e 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -1122,6 +1122,7 @@ Wallet.prototype.createPaymentTxSync = function(options, merchantData, unspent) merchantData.pr.pd.outputs.forEach(function(output) { var amount = output.amount; + // big endian var v = new Buffer(8); v[0] = (amount.high >> 24) & 0xff; v[1] = (amount.high >> 16) & 0xff; @@ -1268,6 +1269,7 @@ Wallet.prototype.verifyPaymentRequest = function(ntxid) { for (var i = 0; i < outputs.length; i++) { var output = outputs[i]; var amount = output.get('amount'); + // big endian var v = new Buffer(8); v[0] = (amount.high >> 24) & 0xff; v[1] = (amount.high >> 16) & 0xff; @@ -1297,6 +1299,7 @@ Wallet.prototype.verifyPaymentRequest = function(ntxid) { }; // Expected value + // little endian var ev = new Buffer(8); ev[0] = (amount.low >> 0) & 0xff; ev[1] = (amount.low >> 8) & 0xff; @@ -1342,6 +1345,7 @@ Wallet.prototype.verifyPaymentRequest = function(ntxid) { var ro = txp.merchant.pr.pd.outputs[i]; // Actual value + // little endian var av = new Buffer(8); av[0] = (ro.amount.low >> 0) & 0xff; av[1] = (ro.amount.low >> 8) & 0xff; diff --git a/test/test.PayPro.js b/test/test.PayPro.js index cd17d7056..309107a4b 100644 --- a/test/test.PayPro.js +++ b/test/test.PayPro.js @@ -245,15 +245,16 @@ describe('PayPro (in Wallet) model', function() { output.get('script').buffer)) }; + // little endian var v = new Buffer(8); - v[0] = (amount.high >> 24) & 0xff; - v[1] = (amount.high >> 16) & 0xff; - v[2] = (amount.high >> 8) & 0xff; - v[3] = (amount.high >> 0) & 0xff; - v[4] = (amount.low >> 24) & 0xff; - v[5] = (amount.low >> 16) & 0xff; - v[6] = (amount.low >> 8) & 0xff; - v[7] = (amount.low >> 0) & 0xff; + v[0] = (amount.low >> 0) & 0xff; + v[1] = (amount.low >> 8) & 0xff; + v[2] = (amount.low >> 16) & 0xff; + v[3] = (amount.low >> 24) & 0xff; + v[4] = (amount.high >> 0) & 0xff; + v[5] = (amount.high >> 8) & 0xff; + v[6] = (amount.high >> 16) & 0xff; + v[7] = (amount.high >> 24) & 0xff; var s = script.buffer.slice(script.offset, script.limit); var net = network === 'main' ? 'livenet' : 'testnet'; @@ -262,10 +263,16 @@ describe('PayPro (in Wallet) model', function() { outs.push({ address: addr[0].toString(), amountSatStr: bitcore.Bignum.fromBuffer(v, { + // XXX for some reason, endian is ALWAYS 'big' + // in node (in the browser it behaves correctly) endian: 'little', size: 1 }).toString(10) }); + + console.log('Output 1:'); + console.log('Buffer: ' + v.toString('hex')); + console.log(outs[outs.length - 1]); }); var b = new bitcore.TransactionBuilder(opts) @@ -483,15 +490,16 @@ describe('PayPro (in Wallet) model', function() { output.get('script').buffer)) }; + // little endian var v = new Buffer(8); - v[0] = (amount.high >> 24) & 0xff; - v[1] = (amount.high >> 16) & 0xff; - v[2] = (amount.high >> 8) & 0xff; - v[3] = (amount.high >> 0) & 0xff; - v[4] = (amount.low >> 24) & 0xff; - v[5] = (amount.low >> 16) & 0xff; - v[6] = (amount.low >> 8) & 0xff; - v[7] = (amount.low >> 0) & 0xff; + v[0] = (amount.low >> 0) & 0xff; + v[1] = (amount.low >> 8) & 0xff; + v[2] = (amount.low >> 16) & 0xff; + v[3] = (amount.low >> 24) & 0xff; + v[4] = (amount.high >> 0) & 0xff; + v[5] = (amount.high >> 8) & 0xff; + v[6] = (amount.high >> 16) & 0xff; + v[7] = (amount.high >> 24) & 0xff; var s = script.buffer.slice(script.offset, script.limit); var net = network === 'main' ? 'livenet' : 'testnet'; @@ -504,6 +512,10 @@ describe('PayPro (in Wallet) model', function() { size: 1 }).toString(10) }); + + console.log('Output 2:'); + console.log('Buffer: ' + v.toString('hex')); + console.log(outs[outs.length - 1]); }); var b = new bitcore.TransactionBuilder(opts)