Skip to content

Commit fd0687e

Browse files
spokodevbeaugunderson
authored andcommitted
fix: pad Address6#toByteArray to a full 16 bytes
`bigInt().toString(16)` drops high-order zero nibbles, so `toByteArray()` and `toUnsignedByteArray()` returned a short array for any address with leading zero bytes. `::1` yielded `[1]` and `::` yielded `[0]` instead of the documented 16-byte representation, which breaks `Buffer.from(...)` for fixed-width network and storage use. Pad the hex string to `BITS / 4` nibbles (16 bytes), matching the existing `binaryZeroPad()` approach and the `Address4#toArray` sibling, which already returns a fixed-width array.
1 parent c697eaa commit fd0687e

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

‎src/ipv6.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,10 +1122,7 @@ export class Address6 {
11221122
* @returns {Array}
11231123
*/
11241124
toByteArray(): number[] {
1125-
const valueWithoutPadding = this.bigInt().toString(16);
1126-
const leadingPad = '0'.repeat(valueWithoutPadding.length % 2);
1127-
1128-
const value = `${leadingPad}${valueWithoutPadding}`;
1125+
const value = this.bigInt().toString(16).padStart(constants6.BITS / 4, '0');
11291126

11301127
const bytes = [];
11311128
for (let i = 0, length = value.length; i < length; i += 2) {

‎test/address-test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function addressIs(addressString: string, descriptors: string[]) {
9090
it('converts to a byte array and back', () => {
9191
const byteArray = address6.toByteArray();
9292

93-
byteArray.length.should.be.at.most(16);
93+
byteArray.length.should.equal(16);
9494

9595
const converted = Address6.fromByteArray(byteArray);
9696

@@ -100,7 +100,7 @@ function addressIs(addressString: string, descriptors: string[]) {
100100
it('converts to an unsigned byte array and back', () => {
101101
const byteArray = address6.toUnsignedByteArray();
102102

103-
byteArray.length.should.be.at.most(16);
103+
byteArray.length.should.equal(16);
104104

105105
const converted = Address6.fromUnsignedByteArray(byteArray);
106106

‎test/functionality-v6-test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,4 +1515,26 @@ describe('v6', () => {
15151515
should.equal(new Address6('2001::/16').isInSubnet(new Address6('fc00::/7')), false);
15161516
});
15171517
});
1518+
1519+
describe('toByteArray', () => {
1520+
it('always returns 16 bytes, including for addresses with leading zero bytes', () => {
1521+
// RFC 4291 section 2: an IPv6 address is a 128-bit (16-octet) identifier,
1522+
// so its byte array must always be 16 bytes long.
1523+
new Address6('::').toByteArray().length.should.equal(16);
1524+
new Address6('::1').toByteArray().length.should.equal(16);
1525+
new Address6('::ffff:192.168.0.1').toByteArray().length.should.equal(16);
1526+
new Address6('64:ff9b::c000:221').toByteArray().length.should.equal(16);
1527+
new Address6('a:b:c:d:e:f:0:1').toByteArray().length.should.equal(16);
1528+
new Address6('::1').toUnsignedByteArray().length.should.equal(16);
1529+
});
1530+
1531+
it('preserves the high-order zero bytes', () => {
1532+
new Address6('::1')
1533+
.toByteArray()
1534+
.should.deep.equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
1535+
new Address6('::ffff:192.168.0.1')
1536+
.toByteArray()
1537+
.should.deep.equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 192, 168, 0, 1]);
1538+
});
1539+
});
15181540
});

0 commit comments

Comments
 (0)