From cda36bfcb3cfb405a43e0a06172a8a8be414d3dd Mon Sep 17 00:00:00 2001 From: nanov Date: Wed, 6 Nov 2019 17:16:15 +0200 Subject: [PATCH] wip: implement BigNumber wrapper --- lib/BigNumber.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/BigNumber.js diff --git a/lib/BigNumber.js b/lib/BigNumber.js new file mode 100644 index 0000000..f53ce28 --- /dev/null +++ b/lib/BigNumber.js @@ -0,0 +1,20 @@ +'use strict'; + +const { jsbn: { BigInteger } } = require('node-forge'); + +class BigNumber { + constructor(value = '') { + this._n = new BigInteger(value); + } + + toBEBuffer(length = undefined) { + const arr = this._n.toByteArray(); + if (length && arr.length > length) + throw new Error('Number out of range.'); + while (arr.length < length) + arr.unshift(0); + return Buffer.from(arr); + } +} + +module.exports = BigNumber;