Skip to content

Commit

Permalink
fix: update module resolution and test mocks
Browse files Browse the repository at this point in the history
- Add .js extension to imports for node16/nodenext module resolution
- Move secp256k1 mock to root __mocks__ directory
- Fix mock implementation to properly handle message signing and verification
  • Loading branch information
vveerrgg committed Dec 16, 2024
1 parent d8daac3 commit f4e6b51
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 87 deletions.
54 changes: 54 additions & 0 deletions __mocks__/@noble/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
console.log("Loading secp256k1 mock");

let utilsValue = {
hmacSha256: (key: Uint8Array, ...messages: Uint8Array[]): Uint8Array => {
// Mock implementation of HMAC SHA256
const result = new Uint8Array(32); // Dummy result
result.fill(1); // Fill with dummy data
return result;
},
hmacSha256Sync: (key: Uint8Array, ...messages: Uint8Array[]): Uint8Array => {
// Mock implementation of synchronous HMAC SHA256
const result = new Uint8Array(32); // Dummy result
result.fill(1); // Fill with dummy data
return result;
}
};

Object.defineProperty(exports, 'utils', {
get: () => utilsValue,
set: (value) => {
utilsValue = { ...utilsValue, ...value };
},
configurable: true,
enumerable: true
});

class Signature {
constructor(private bytes: Uint8Array) {
console.log("Creating Signature instance");
}

toCompactRawBytes(): Uint8Array {
console.log("Calling toCompactRawBytes");
return this.bytes;
}
}

// Keep track of signed message hashes for verification
const signedHashes = new Map<string, boolean>();

export const sign = async (messageHash: Uint8Array, privateKey: Uint8Array): Promise<Signature> => {
console.log("Calling mock sign function");
const signatureBytes = new Uint8Array(64);
signatureBytes.fill(1);
// Store the message hash for verification
const hashHex = Array.from(messageHash).map(b => b.toString(16).padStart(2, '0')).join('');
signedHashes.set(hashHex, true);
return new Signature(signatureBytes);
};

export const verify = async (signature: Uint8Array, messageHash: Uint8Array, publicKey: Uint8Array): Promise<boolean> => {
const hashHex = Array.from(messageHash).map(b => b.toString(16).padStart(2, '0')).join('');
return signedHashes.has(hashHex);
};
14 changes: 0 additions & 14 deletions mocks/mock-secp256k1.ts

This file was deleted.

6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"homepage": "https://github.com/humanjavaenterprises/nostr-nsec-seedphrase#readme",
"dependencies": {
"@noble/hashes": "^1.3.3",
"@noble/secp256k1": "^2.0.0",
"@noble/secp256k1": "^2.1.0",
"bip39": "^3.1.0",
"nostr-tools": "^2.1.4",
"pino": "^8.17.2"
Expand Down
36 changes: 4 additions & 32 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,10 @@ import {
verifyEvent,
configureHMAC,
fromHex,
} from "../index";

vi.mock("@noble/secp256k1", async () => {
const utils = {
hmacSha256: (_key: Uint8Array, ..._messages: Uint8Array[]): Uint8Array => {
const result = new Uint8Array(32);
result.fill(1);
return result;
},
hmacSha256Sync: (
_key: Uint8Array,
..._messages: Uint8Array[]
): Uint8Array => {
const result = new Uint8Array(32);
result.fill(1);
return result;
},
};

return {
utils,
sign: async (message: Uint8Array) => ({
toCompactRawBytes: () => message, // Return the message as the signature for testing
}),
verify: async (signature: Uint8Array, message: Uint8Array) => {
// Compare the signature with the message for testing
// In our mock, signature should match message for valid verification
if (signature.length !== message.length) return false;
return signature.every((byte, i) => byte === message[i]);
},
};
});
} from "../index.js";

// Mock is automatically picked up from src/__mocks__/@noble/secp256k1.ts
vi.mock("@noble/secp256k1");

describe("nostr-nsec-seedphrase", () => {
beforeAll(() => {
Expand Down
36 changes: 0 additions & 36 deletions src/__tests__/mocks/secp256k1.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"resolveJsonModule": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
"exclude": ["node_modules", "dist"]
}

0 comments on commit f4e6b51

Please sign in to comment.