Skip to content
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
4 changes: 2 additions & 2 deletions lib/block/blockheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Instantiate a BlockHeader from a Buffer, JSON object, or Object with
* the properties of the BlockHeader
*
* @param {BlockHeader.fromObjectParams|Buffer} arg - A Buffer, JSON string, or Object
* @param {BlockHeader.fromObjectParams|Buffer|Uint8Array} arg - A Buffer, Uint8Array, JSON string, or Object

Check warning on line 15 in lib/block/blockheader.js

View workflow job for this annotation

GitHub Actions / Run Dashcore lib tests

This line has a length of 109. Maximum allowed is 100
* @returns {BlockHeader} - An instance of block header
* @constructor
*/
Expand Down Expand Up @@ -40,7 +40,7 @@
};

/**
* @param {BlockHeader.fromObjectParams|Buffer} arg - A Buffer, JSON string or Object
* @param {BlockHeader.fromObjectParams|Buffer|Uint8Array} arg - A Buffer, Uint8Array, JSON string or Object

Check warning on line 43 in lib/block/blockheader.js

View workflow job for this annotation

GitHub Actions / Run Dashcore lib tests

This line has a length of 108. Maximum allowed is 100
* @returns {Object} - An object representing block header data
* @throws {TypeError} - If the argument was not recognized
* @private
Expand Down
4 changes: 2 additions & 2 deletions lib/encoding/bufferreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var BufferReader = function BufferReader(buf) {
if (_.isUndefined(buf)) {
return;
}
if (Buffer.isBuffer(buf)) {
if (buf instanceof Uint8Array) {
this.set({
buf: buf,
buf: Buffer.isBuffer(buf) ? buf : Buffer.from(buf),
});
} else if (_.isString(buf)) {
this.set({
Expand Down
18 changes: 18 additions & 0 deletions test/block/blockheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ describe('BlockHeader', function () {
.toString('hex')
.should.equal(bhhex);
});

it('should accept Uint8Array', function () {
var bytes = new Uint8Array(bhbuf);
var fromBuffer = BlockHeader.fromBuffer(bhbuf);
var fromBytes = new BlockHeader(bytes);

fromBytes.version.should.equal(fromBuffer.version);
fromBytes.prevHash.toString('hex').should.equal(
fromBuffer.prevHash.toString('hex')
);
fromBytes.merkleRoot.toString('hex').should.equal(
fromBuffer.merkleRoot.toString('hex')
);
fromBytes.time.should.equal(fromBuffer.time);
fromBytes.bits.should.equal(fromBuffer.bits);
fromBytes.nonce.should.equal(fromBuffer.nonce);
fromBytes.toBuffer().toString('hex').should.equal(bhhex);
});
});

describe('#fromBufferReader', function () {
Expand Down
12 changes: 12 additions & 0 deletions test/encoding/bufferreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ describe('BufferReader', function () {
should.exist(br);
Buffer.isBuffer(br.buf).should.equal(true);
});

it('should accept Uint8Array', function () {
var bytes = new Uint8Array([1, 2, 0x34, 0x12, 0xdd, 0xcc, 0xbb, 0xaa]);
var br = new BufferReader(bytes);
should.exist(br);
Buffer.isBuffer(br.buf).should.equal(true);
br.readUInt8().should.equal(1);
br.readUInt8().should.equal(2);
br.readUInt16LE().should.equal(0x1234);
br.readUInt32LE().should.equal(0xaabbccdd);
});

it('should fail for invalid object', function () {
var fail = function () {
return new BufferReader(5);
Expand Down
Loading