diff --git a/RpcClient.js b/RpcClient.js index 631c089..2137bfb 100644 --- a/RpcClient.js +++ b/RpcClient.js @@ -35,7 +35,7 @@ function ClassSpec(b) { dumpPrivKey: '', encryptWallet: '', getAccount: '', - getAccountAddress: '', + getAccountAddress: 'str', getAddedNodeInfo: '', getAddressesByAccount: '', getBalance: 'str int', diff --git a/Transaction.js b/Transaction.js index b4580b3..7c83c5d 100644 --- a/Transaction.js +++ b/Transaction.js @@ -181,6 +181,19 @@ function spec(b) { return this.hash; }; + // convert encoded list of inputs to easy-to-use JS list-of-lists + Transaction.prototype.inputs = function inputs() { + var res = []; + for (var i = 0; i < this.ins.length; i++) { + var txin = this.ins[i]; + var outHash = txin.getOutpointHash(); + var outIndex = txin.getOutpointIndex(); + res.push([outHash, outIndex]); + } + + return res; + } + /** * Load and cache transaction inputs. * diff --git a/test/basic.js b/test/basic.js index cdf1e71..f34a2a7 100644 --- a/test/basic.js +++ b/test/basic.js @@ -5,6 +5,7 @@ var Address = require('../Address').class(); var PrivateKey = require('../PrivateKey').class(); var networks = require('../networks'); var KeyModule = require('../Key'); +var coinUtil = require('../util/util'); suite('basic'); @@ -110,8 +111,22 @@ function is_invalid(datum) assert.equal(valid, false); } +function test_value(datum) +{ + if (datum.length != 2) + throw new Error("Bad test"); + + var decimal = datum[0]; + var intStr = datum[1]; + + var bn = coinUtil.parseValue(decimal); + assert.notEqual(bn, undefined); + assert.equal(bn.toString(), intStr); +} + var dataValid = JSON.parse(fs.readFileSync('test/base58_keys_valid.json')); var dataInvalid = JSON.parse(fs.readFileSync('test/base58_keys_invalid.json')); +var dataValues = JSON.parse(fs.readFileSync('test/values.json')); test('valid', function() { dataValid.forEach(function(datum) { is_valid(datum); }); @@ -121,3 +136,7 @@ test('invalid', function() { dataInvalid.forEach(function(datum) { is_invalid(datum); }); }); +test('values', function() { + dataValues.forEach(function(datum) { test_value(datum); }); +}); + diff --git a/test/values.json b/test/values.json new file mode 100644 index 0000000..517acbc --- /dev/null +++ b/test/values.json @@ -0,0 +1,7 @@ +[ + [ "0", "0" ], + [ "1.0", "100000000" ], + [ "0.1", "10000000" ], + [ ".1", "10000000" ], + [ "0.0005", "50000" ] +] diff --git a/util/util.js b/util/util.js index fe170a1..974a93d 100644 --- a/util/util.js +++ b/util/util.js @@ -107,6 +107,49 @@ var formatValue = exports.formatValue = function (valueBuffer) { return integerPart+"."+decimalPart; }; +var reFullVal = /^\s*(\d+)\.(\d+)/; +var reFracVal = /^\s*\.(\d+)/; +var reWholeVal = /^\s*(\d+)/; + +function padFrac(frac) +{ + while (frac.length < 8) + frac = frac + '0'; + return frac; +} + +function parseFullValue(res) +{ + return bignum(res[1]).mul('100000000').add(padFrac(res[2])); +} + +function parseFracValue(res) +{ + return bignum(padFrac(res[1])); +} + +function parseWholeValue(res) +{ + return bignum(res[1]).mul('100000000'); +} + +exports.parseValue = function parseValue(valueStr) +{ + var res = valueStr.match(reFullVal); + if (res) + return parseFullValue(res); + + res = valueStr.match(reFracVal); + if (res) + return parseFracValue(res); + + res = valueStr.match(reWholeVal); + if (res) + return parseWholeValue(res); + + return undefined; +}; + var pubKeyHashToAddress = exports.pubKeyHashToAddress = function (pubKeyHash, addressVersion) { if (!pubKeyHash) return ""; @@ -323,15 +366,6 @@ var varStrBuf = exports.varStrBuf = function varStrBuf(s) { return Buffer.concat(varIntBuf(s.length), s); }; -var buf64 = exports.buf64 = function buf64(n) { - var lo = n & 0xffffffff; - var hi = (n >>> 32); - var buf = new Buffer(4 + 4); - buf.writeUInt32LE(lo, 0); - buf.writeUInt32LE(hi, 4); - return buf; -}; - // Initializations exports.NULL_HASH = new Buffer(32).fill(0); exports.EMPTY_BUFFER = new Buffer(0);