Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ $ npm test
| block/blockheader.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| block/index.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| block/merkleblock.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/bn.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/ecdsa.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/hash.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/bn.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/ecdsa.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/hash.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/point.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/random.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/random.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| crypto/signature.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| encoding/base58.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| encoding/base58check.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
Expand Down
41 changes: 19 additions & 22 deletions src/crypto/bn.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@


const BN = require('bn.js');
const _ = require('lodash');
const $ = require('../util/preconditions');

const reversebuf = function (buf) {
const reversebuf = buf => {
const buf2 = Buffer.alloc(buf.length);
for (let i = 0; i < buf.length; i += 1) {
buf2[i] = buf[buf.length - 1 - i];
Expand All @@ -16,17 +14,17 @@ BN.Zero = new BN(0);
BN.One = new BN(1);
BN.Minus1 = new BN(-1);

BN.fromNumber = function (n) {
BN.fromNumber = function(n) {
$.checkArgument(_.isNumber(n));
return new BN(n);
};

BN.fromString = function (str, base) {
BN.fromString = function(str, base) {
$.checkArgument(_.isString(str));
return new BN(str, base);
};

BN.fromBuffer = function (buf, opts) {
BN.fromBuffer = function(buf, opts) {
if (typeof opts !== 'undefined' && opts.endian === 'little') {
buf = reversebuf(buf);
}
Expand All @@ -39,7 +37,7 @@ BN.fromBuffer = function (buf, opts) {
* Instantiate a BigNumber from a "signed magnitude buffer"
* (a buffer where the most significant bit represents the sign (0 = positive, -1 = negative))
*/
BN.fromSM = function (buf, opts) {
BN.fromSM = function(buf, opts) {
let ret;
if (buf.length === 0) {
return BN.fromBuffer(Buffer.from([0]));
Expand All @@ -63,14 +61,13 @@ BN.fromSM = function (buf, opts) {
return ret;
};


BN.prototype.toNumber = function () {
BN.prototype.toNumber = function() {
return parseInt(this.toString(10), 10);
};

BN.prototype.toBuffer = function (opts) {
let buf; let
hex;
BN.prototype.toBuffer = function(opts) {
let buf;
let hex;
if (opts && opts.size) {
hex = this.toString(16, 2);
const natlen = hex.length / 2;
Expand All @@ -93,7 +90,7 @@ BN.prototype.toBuffer = function (opts) {
return buf;
};

BN.prototype.toSMBigEndian = function () {
BN.prototype.toSMBigEndian = function() {
let buf;
if (this.cmp(BN.Zero) === -1) {
buf = this.neg().toBuffer();
Expand All @@ -109,13 +106,13 @@ BN.prototype.toSMBigEndian = function () {
}
}

if (buf.length === 1 & buf[0] === 0) {
if ((buf.length === 1) & (buf[0] === 0)) {
buf = Buffer.from([]);
}
return buf;
};

BN.prototype.toSM = function (opts) {
BN.prototype.toSM = function(opts) {
const endian = opts ? opts.endian : 'big';
let buf = this.toSMBigEndian();

Expand All @@ -133,7 +130,7 @@ BN.prototype.toSM = function (opts) {
* 4 bytes. We copy that behavior here. A third argument, `size`, is provided to
* extend the hard limit of 4 bytes, as some usages require more than 4 bytes.
*/
BN.fromScriptNumBuffer = function (buf, fRequireMinimal, size) {
BN.fromScriptNumBuffer = function(buf, fRequireMinimal, size) {
const nMaxNumSize = size || 4;
$.checkArgument(buf.length <= nMaxNumSize, new Error('script number overflow'));
if (fRequireMinimal && buf.length > 0) {
Expand Down Expand Up @@ -165,29 +162,29 @@ BN.fromScriptNumBuffer = function (buf, fRequireMinimal, size) {
* performing a numerical operation that results in an overflow to more than 4
* bytes).
*/
BN.prototype.toScriptNumBuffer = function () {
BN.prototype.toScriptNumBuffer = function() {
return this.toSM({
endian: 'little',
});
};

BN.prototype.gt = function (b) {
BN.prototype.gt = function(b) {
return this.cmp(b) > 0;
};

BN.prototype.gte = function (b) {
BN.prototype.gte = function(b) {
return this.cmp(b) >= 0;
};

BN.prototype.lt = function (b) {
BN.prototype.lt = function(b) {
return this.cmp(b) < 0;
};

BN.trim = function (buf, natlen) {
BN.trim = function(buf, natlen) {
return buf.slice(natlen - buf.length, buf.length);
};

BN.pad = function (buf, natlen, size) {
BN.pad = function(buf, natlen, size) {
const rbuf = Buffer.alloc(size);
for (let i = 0; i < buf.length; i += 1) {
rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i];
Expand Down
Loading