@omisego/omg-js-util
Advanced tools
Comparing version
{ | ||
"name": "@omisego/omg-js-util", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "OMG util module", | ||
@@ -26,5 +26,6 @@ "main": "src/index.js", | ||
"js-sha3": "^0.8.0", | ||
"number-to-bn": "^1.7.0", | ||
"rlp": "^2.2.2" | ||
}, | ||
"gitHead": "09e36e667d11af23eb8b449a0a54f71b428340dc" | ||
"gitHead": "5e88014b838c9d132d06bf9cef6e0b92b3f2ed3d" | ||
} |
@@ -19,3 +19,4 @@ /* | ||
transaction: require('./transaction'), | ||
sign: require('./sign') | ||
sign: require('./sign'), | ||
hexToBytes: require('./hexToBytes') | ||
} |
@@ -19,3 +19,3 @@ /* | ||
const InvalidArgumentError = require('./InvalidArgumentError') | ||
const web3Utils = require('web3-utils') | ||
const numberToBN = require('number-to-bn') | ||
const rlp = require('rlp') | ||
@@ -30,3 +30,3 @@ | ||
const BLOCK_OFFSET = web3Utils.toBN(1000000000) | ||
const BLOCK_OFFSET = numberToBN(1000000000) | ||
const TX_OFFSET = 10000 | ||
@@ -158,3 +158,3 @@ | ||
inputArr.sort((a, b) => { | ||
const diff = web3Utils.toBN(a.amount).sub(web3Utils.toBN(b.amount)) | ||
const diff = numberToBN(a.amount).sub(numberToBN(b.amount)) | ||
return Number(diff.toString()) | ||
@@ -166,6 +166,6 @@ }) | ||
// Get the total value of the inputs | ||
const totalInputValue = inputArr.reduce((acc, curr) => acc.add(web3Utils.toBN(curr.amount.toString())), web3Utils.toBN(0)) | ||
const totalInputValue = inputArr.reduce((acc, curr) => acc.add(numberToBN(curr.amount.toString())), numberToBN(0)) | ||
// Check there is enough in the inputs to cover the amount | ||
if (totalInputValue.lt(web3Utils.toBN(toAmount))) { | ||
if (totalInputValue.lt(numberToBN(toAmount))) { | ||
throw new Error(`Insufficient funds for ${toAmount}`) | ||
@@ -177,6 +177,6 @@ } | ||
currency, | ||
amount: Number(web3Utils.toBN(toAmount)) | ||
amount: Number(numberToBN(toAmount)) | ||
}] | ||
if (totalInputValue.gt(web3Utils.toBN(toAmount))) { | ||
if (totalInputValue.gt(numberToBN(toAmount))) { | ||
// If necessary add a 'change' output | ||
@@ -186,3 +186,3 @@ outputArr.push({ | ||
currency, | ||
amount: Number(totalInputValue.sub(web3Utils.toBN(toAmount)).toString()) | ||
amount: Number(totalInputValue.sub(numberToBN(toAmount)).toString()) | ||
}) | ||
@@ -200,4 +200,4 @@ } | ||
encodeUtxoPos: function (utxo) { | ||
const blk = web3Utils.toBN(utxo.blknum).mul(BLOCK_OFFSET) | ||
const tx = web3Utils.toBN(utxo.txindex).muln(TX_OFFSET) | ||
const blk = numberToBN(utxo.blknum).mul(BLOCK_OFFSET) | ||
const tx = numberToBN(utxo.txindex).muln(TX_OFFSET) | ||
return blk.add(tx).addn(utxo.oindex) | ||
@@ -207,3 +207,3 @@ }, | ||
decodeUtxoPos: function (utxoPos) { | ||
const bn = web3Utils.toBN(utxoPos) | ||
const bn = numberToBN(utxoPos) | ||
const blknum = bn.div(BLOCK_OFFSET).toNumber() | ||
@@ -210,0 +210,0 @@ const txindex = bn.mod(BLOCK_OFFSET).divn(TX_OFFSET).toNumber() |
const transaction = require('../src/transaction') | ||
const Web3Utils = require('web3-utils') | ||
const numberToBN = require('number-to-bn') | ||
const assert = require('chai').assert | ||
@@ -26,3 +26,3 @@ | ||
assert.equal(txBody.outputs[1].owner, fromAddress) | ||
const expectedChange = Web3Utils.toBN(fromUtxos[0].amount).sub(Web3Utils.toBN(toAmount)) | ||
const expectedChange = numberToBN(fromUtxos[0].amount).sub(numberToBN(toAmount)) | ||
assert.equal(txBody.outputs[1].amount.toString(), expectedChange.toString()) | ||
@@ -58,3 +58,3 @@ }) | ||
assert.equal(txBody.outputs[1].owner, fromAddress) | ||
const expectedChange = Web3Utils.toBN(fromUtxos[0].amount).add(Web3Utils.toBN(fromUtxos[1].amount)).sub(Web3Utils.toBN(toAmount)) | ||
const expectedChange = numberToBN(fromUtxos[0].amount).add(numberToBN(fromUtxos[1].amount)).sub(numberToBN(toAmount)) | ||
assert.equal(txBody.outputs[1].amount.toString(), expectedChange.toString()) | ||
@@ -61,0 +61,0 @@ }) |
41578
0.71%12
9.09%802
1.52%5
25%+ Added
+ Added
+ Added