Skip to content

Commit

Permalink
fix+test: DataView requires subarray offset
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Aug 17, 2024
1 parent 02930df commit 34b6334
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 4 additions & 4 deletions public/dashjoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var DashJoin = ('object' === typeof module && exports) || {};
(function (window, DashJoin) {
'use strict';

let DashP2P = window.DashP2P || require('dashp2p');
let DashP2P = window.DashP2P || require('./dashp2p.js');
let DashTx = window.DashTx || require('dashtx');

const DV_LITTLE_ENDIAN = true;
Expand Down Expand Up @@ -139,7 +139,7 @@ var DashJoin = ('object' === typeof module && exports) || {};
);
}

let dv = new DataView(payload.buffer);
let dv = new DataView(payload.buffer, payload.byteOffset);
let offset = 0;

dv.setUint32(offset, denomMask, DV_LITTLE_ENDIAN);
Expand Down Expand Up @@ -259,7 +259,7 @@ var DashJoin = ('object' === typeof module && exports) || {};
let msg = `developer error: 'dsq' must be ${Sizes.DSQ} bytes, but received ${bytes.length}`;
throw new Error(msg);
}
let dv = new DataView(bytes.buffer);
let dv = new DataView(bytes.buffer, bytes.byteOffset);

let offset = 0;

Expand Down Expand Up @@ -359,7 +359,7 @@ var DashJoin = ('object' === typeof module && exports) || {};
let msg = `developer error: a 'dssu' message is 16 bytes, but got ${bytes.length}`;
throw new Error(msg);
}
let dv = new DataView(bytes.buffer);
let dv = new DataView(bytes.buffer, bytes.byteOffset);
let offset = 0;

let session_id = dv.getUint32(offset, DV_LITTLE_ENDIAN);
Expand Down
4 changes: 2 additions & 2 deletions public/dashp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ var DashP2P = ('object' === typeof module && exports) || {};
let msg = `developer error: header should be ${Sizes.HEADER}+ bytes (optional payload), not ${bytes.length}`;
throw new Error(msg);
}
let dv = new DataView(bytes.buffer);
let dv = new DataView(bytes.buffer, bytes.byteOffset);

let index = 0;

Expand Down Expand Up @@ -1186,7 +1186,7 @@ var DashP2P = ('object' === typeof module && exports) || {};
let padded = new Uint8Array((bytes.length + 9 + 63) & ~63);
padded.set(bytes);
padded[bytes.length] = 0x80;
let dv = new DataView(padded.buffer);
let dv = new DataView(padded.buffer, padded.byteOffset);
dv.setUint32(padded.length - 4, bytes.length << 3, false);

let w = new Uint32Array(64);
Expand Down
13 changes: 10 additions & 3 deletions tests/dsa.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

let Packer = require('../packer.js');
let DashJoin = require('../public/dashjoin.js');
// TODO copy .utils.bytesToHex rather than depend on it
let DashKeys = require('dashkeys');

Expand Down Expand Up @@ -30,7 +30,7 @@ let collateralTxHex =
function test() {
let expectedHex = `${regtest}${command}${payloadSize}${checksum}${denomMask}${collateralTxHex}`;
let collateralTx = DashKeys.utils.hexToBytes(collateralTxHex);
let message = Packer.packAllow({ network, denomination, collateralTx });
let message = DashJoin.packers.dsa({ network, denomination, collateralTx });
let messageHex = DashKeys.utils.bytesToHex(message);
if (expectedHex.length !== messageHex.length) {
let length = expectedHex.length / 2;
Expand All @@ -39,13 +39,20 @@ function test() {
);
}
if (expectedHex !== messageHex) {
console.log();
console.log(`EXPECTED: (${expectedHex.length})`);
console.log(expectedHex);
console.log();
console.log(`ACTUAL: (${messageHex.length})`);
console.log(messageHex);
console.log();
throw new Error(
'bytes of dsa (allow / join request) messages do not match',
);
}

console.info(
`PASS: Packer.packAllow({ network, denomination, collateralTx }) matches`,
`PASS: DashJoin.packers.dsa({ network, denomination, collateralTx }) matches`,
);
}

Expand Down

0 comments on commit 34b6334

Please sign in to comment.