Skip to content

Commit

Permalink
Merge pull-request #30
Browse files Browse the repository at this point in the history
  • Loading branch information
r-n-o committed Mar 29, 2024
2 parents 9688dc7 + 007b75e commit b6361ef
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions auth/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ <h2>Message log</h2>
* @returns {Uint8Array}
*/
var uint8arrayFromHexString = function(hexString) {
var hexRegex = /^[0-9A-Fa-f]+$/;
if (!hexString || hexString.length % 2 != 0 || !hexRegex.test(hexString)) {
throw new Error('cannot create uint8array from invalid hex string: "' + hexString + '"');
}
return new Uint8Array(hexString.match(/../g).map(h=>parseInt(h,16)));
}

Expand Down
17 changes: 17 additions & 0 deletions auth/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ describe("TKHQ", () => {

it("contains uint8arrayFromHexString", () => {
expect(TKHQ.uint8arrayFromHexString("627566666572").toString()).toEqual("98,117,102,102,101,114");

// Error case: bad value
expect(() => {
TKHQ.uint8arrayFromHexString({});
}).toThrow('cannot create uint8array from invalid hex string: "[object Object]"');
// Error case: empty string
expect(() => {
TKHQ.uint8arrayFromHexString("");
}).toThrow('cannot create uint8array from invalid hex string: ""');
// Error case: odd number of characters
expect(() => {
TKHQ.uint8arrayFromHexString("123");
}).toThrow('cannot create uint8array from invalid hex string: "123"');
// Error case: bad characters outside of hex range
expect(() => {
TKHQ.uint8arrayFromHexString("oops");
}).toThrow('cannot create uint8array from invalid hex string: "oops"');
})

it("contains bigIntToHex", () => {
Expand Down
9 changes: 7 additions & 2 deletions export/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,13 @@ <h2>Message log</h2>
* @param {string} hexString
* @returns {Uint8Array}
*/
const uint8arrayFromHexString = hexString =>
new Uint8Array(hexString.match(/../g).map(h=>parseInt(h,16)));
const uint8arrayFromHexString = function(hexString) {
var hexRegex = /^[0-9A-Fa-f]+$/;
if (!hexString || hexString.length % 2 != 0 || !hexRegex.test(hexString)) {
throw new Error('cannot create uint8array from invalid hex string: "' + hexString + '"');
}
return new Uint8Array(hexString.match(/../g).map(h=>parseInt(h,16)));
}

/**
* Takes a Uint8Array and returns a hex string
Expand Down
17 changes: 17 additions & 0 deletions export/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ describe("TKHQ", () => {

it("contains uint8arrayFromHexString", () => {
expect(TKHQ.uint8arrayFromHexString("627566666572").toString()).toEqual("98,117,102,102,101,114");

// Error case: bad value
expect(() => {
TKHQ.uint8arrayFromHexString({});
}).toThrow('cannot create uint8array from invalid hex string: "[object Object]"');
// Error case: empty string
expect(() => {
TKHQ.uint8arrayFromHexString("");
}).toThrow('cannot create uint8array from invalid hex string: ""');
// Error case: odd number of characters
expect(() => {
TKHQ.uint8arrayFromHexString("123");
}).toThrow('cannot create uint8array from invalid hex string: "123"');
// Error case: bad characters outside of hex range
expect(() => {
TKHQ.uint8arrayFromHexString("oops");
}).toThrow('cannot create uint8array from invalid hex string: "oops"');
})

it("logs messages and sends messages up", async () => {
Expand Down

0 comments on commit b6361ef

Please sign in to comment.