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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocking: Expose the new BlockHeader input in the published TypeScript declaration

The newly documented Uint8Array construction path is not exposed by the package's TypeScript API: index.d.ts exports typings/block/BlockHeader.d.ts, which still declares only constructor(). Compiling an import from the package followed by new BlockHeader(new Uint8Array(80)) produces TS2554: 'Expected 0 arguments, but got 1.' Although the constructor declaration was already incomplete, accepting this newly supported input is necessary to expose this PR's feature to TypeScript consumers. Add a constructor signature accepting Uint8Array and a test-d assertion exercising it; the existing type test only imports BlockHeader and does not detect this gap.

source: ['claude']

* @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) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocking: Preserve Buffer detection when widening the input check

Replacing Buffer.isBuffer with instanceof Uint8Array rejects browser Buffers created in another realm, such as an iframe. The configured browser Buffer polyfill recognizes these values with Buffer.isBuffer, but they fail the receiving realm's instanceof check. They consequently enter the generic object branch, which leaves this.buf undefined, and the first read throws. Using separately evaluated copies of the installed browser Buffer polyfill, the base successfully read 67305985 from the supplied bytes while this head threw a TypeError in readUInt32LE. Preserve the original Buffer check alongside Uint8Array detection and add a cross-realm regression test.

Suggested change
if (buf instanceof Uint8Array) {
if (Buffer.isBuffer(buf) || buf instanceof Uint8Array) {

source: ['claude']

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