Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raymens committed Feb 19, 2024
1 parent 3987cbc commit f858262
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
30 changes: 21 additions & 9 deletions mobile/src/utils/__tests__/authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import { getSignatureHeaders } from "../axios";
import { Buffer } from "buffer";

ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
//ed.etc.sha512Async = (...m) => Promise.resolve(ed.etc.sha512Sync(...m));

jest.useFakeTimers().setSystemTime(new Date("2020-01-01T00:00:00Z"));
ed.etc.sha512Async = (...m) => Promise.resolve(ed.etc.sha512Sync(...m));

describe("signMessage noble", () => {
it("message is deterministic", async () => {
it("signature and key generation is deterministic", () => {
//var privKey = ed.utils.randomPrivateKey();

const privKey =
Expand All @@ -22,17 +20,24 @@ describe("signMessage noble", () => {
const privateKey = Buffer.from(privKey, "hex");
const privateKeyHex = privateKey.toString("hex");
const privateKeyB64 = privateKey.toString("base64");

const hexResult = ed.getPublicKey(privateKey);
const pubKeyB64 = Buffer.from(hexResult).toString("base64");

expect(privateKeyHex).toEqual(
"6e2dd227481f9d65e92f7be424876015eb54e59826206098acbc442fd371dab4"
);
expect(privateKeyB64).toEqual(
"bi3SJ0gfnWXpL3vkJIdgFetU5ZgmIGCYrLxEL9Nx2rQ="
);
expect(pubKeyB64).toEqual("gwZ+LyQ+VLaIsWeSq3QFh+WaZHNgl07pXul++BsezoY=");
});

const hexResult = ed.getPublicKey(privateKey);
const pubKeyB64 = Buffer.from(hexResult).toString("base64");
it("signature is deterministic", () => {
const privKey =
"6e2dd227481f9d65e92f7be424876015eb54e59826206098acbc442fd371dab4";

expect(pubKeyB64).toEqual("gwZ+LyQ+VLaIsWeSq3QFh+WaZHNgl07pXul++BsezoY=");
const privateKey = Buffer.from(privKey, "hex");

const message = "HELLO";
const messageBytes = Buffer.from(message, "utf-8");
Expand All @@ -45,15 +50,22 @@ describe("signMessage noble", () => {
});

describe("send signature to server", () => {

it("message is deterministic", async () => {
const date = new Date("2020-01-01T00:00:00Z");

const privKey = "bi3SJ0gfnWXpL3vkJIdgFetU5ZgmIGCYrLxEL9Nx2rQ=";

const data = { message: "HELLO" };
const sigData = getSignatureHeaders(data, privKey);

// we supply the date directly to the function
// trying to mock it using fakeTimers and setSystemTime did not work
// it was unable to resolve the hook (whatever hook that might be) at the end of the call
const sigData = getSignatureHeaders(date, data, privKey);

expect(sigData.signature).toEqual(
"ksnz8fzvQerq3uTgYYisKqLu/tZJWcYQYPW4UAl62FREqm6T9PDGiAIjwiePL6SC4jE7X59r8llhUQqgQKQ1DQ=="
);
expect(sigData.timestamp).toEqual("1577836800");
}, 10000);
});
});
8 changes: 5 additions & 3 deletions mobile/src/utils/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function requestInterceptor(config: InternalAxiosRequestConfig) {
config.headers["Authorization"] = `Bearer ${accessToken}`;

if (pubKeyFromStore !== null && privKeyFromStore != null) {
const sigData = getSignatureHeaders(config.data, privKeyFromStore);
const sigData = getSignatureHeaders(new Date(), config.data, privKeyFromStore);
config.headers["x-public-key"] = pubKeyFromStore;
config.headers["x-timestamp"] = sigData.timestamp;
config.headers["x-signature"] = sigData.signature;
Expand All @@ -71,9 +71,11 @@ async function requestInterceptor(config: InternalAxiosRequestConfig) {
return config;
}

// the date is supplied as a parameter to allow for testing
// there were various issues with trying to mock it directly
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getSignatureHeaders(data: any, privKeyFromStore: string) {
const timestampInSeconds = Math.floor(Date.now() / 1000).toString(); // Convert current time to Unix timestamp in seconds
export function getSignatureHeaders(date: Date, data: any, privKeyFromStore: string) {
const timestampInSeconds = Math.floor(date.getTime() / 1000).toString(); // Convert current time to Unix timestamp in seconds
const dataToSign = data
? timestampInSeconds + JSON.stringify(data)
: timestampInSeconds;
Expand Down

0 comments on commit f858262

Please sign in to comment.