Skip to content

Commit f4e6b51

Browse files
committed
fix: update module resolution and test mocks
- 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
1 parent d8daac3 commit f4e6b51

File tree

7 files changed

+63
-87
lines changed

7 files changed

+63
-87
lines changed

__mocks__/@noble/secp256k1.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
console.log("Loading secp256k1 mock");
2+
3+
let utilsValue = {
4+
hmacSha256: (key: Uint8Array, ...messages: Uint8Array[]): Uint8Array => {
5+
// Mock implementation of HMAC SHA256
6+
const result = new Uint8Array(32); // Dummy result
7+
result.fill(1); // Fill with dummy data
8+
return result;
9+
},
10+
hmacSha256Sync: (key: Uint8Array, ...messages: Uint8Array[]): Uint8Array => {
11+
// Mock implementation of synchronous HMAC SHA256
12+
const result = new Uint8Array(32); // Dummy result
13+
result.fill(1); // Fill with dummy data
14+
return result;
15+
}
16+
};
17+
18+
Object.defineProperty(exports, 'utils', {
19+
get: () => utilsValue,
20+
set: (value) => {
21+
utilsValue = { ...utilsValue, ...value };
22+
},
23+
configurable: true,
24+
enumerable: true
25+
});
26+
27+
class Signature {
28+
constructor(private bytes: Uint8Array) {
29+
console.log("Creating Signature instance");
30+
}
31+
32+
toCompactRawBytes(): Uint8Array {
33+
console.log("Calling toCompactRawBytes");
34+
return this.bytes;
35+
}
36+
}
37+
38+
// Keep track of signed message hashes for verification
39+
const signedHashes = new Map<string, boolean>();
40+
41+
export const sign = async (messageHash: Uint8Array, privateKey: Uint8Array): Promise<Signature> => {
42+
console.log("Calling mock sign function");
43+
const signatureBytes = new Uint8Array(64);
44+
signatureBytes.fill(1);
45+
// Store the message hash for verification
46+
const hashHex = Array.from(messageHash).map(b => b.toString(16).padStart(2, '0')).join('');
47+
signedHashes.set(hashHex, true);
48+
return new Signature(signatureBytes);
49+
};
50+
51+
export const verify = async (signature: Uint8Array, messageHash: Uint8Array, publicKey: Uint8Array): Promise<boolean> => {
52+
const hashHex = Array.from(messageHash).map(b => b.toString(16).padStart(2, '0')).join('');
53+
return signedHashes.has(hashHex);
54+
};

mocks/mock-secp256k1.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"homepage": "https://github.com/humanjavaenterprises/nostr-nsec-seedphrase#readme",
4747
"dependencies": {
4848
"@noble/hashes": "^1.3.3",
49-
"@noble/secp256k1": "^2.0.0",
49+
"@noble/secp256k1": "^2.1.0",
5050
"bip39": "^3.1.0",
5151
"nostr-tools": "^2.1.4",
5252
"pino": "^8.17.2"

src/__tests__/index.test.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,10 @@ import {
1313
verifyEvent,
1414
configureHMAC,
1515
fromHex,
16-
} from "../index";
17-
18-
vi.mock("@noble/secp256k1", async () => {
19-
const utils = {
20-
hmacSha256: (_key: Uint8Array, ..._messages: Uint8Array[]): Uint8Array => {
21-
const result = new Uint8Array(32);
22-
result.fill(1);
23-
return result;
24-
},
25-
hmacSha256Sync: (
26-
_key: Uint8Array,
27-
..._messages: Uint8Array[]
28-
): Uint8Array => {
29-
const result = new Uint8Array(32);
30-
result.fill(1);
31-
return result;
32-
},
33-
};
34-
35-
return {
36-
utils,
37-
sign: async (message: Uint8Array) => ({
38-
toCompactRawBytes: () => message, // Return the message as the signature for testing
39-
}),
40-
verify: async (signature: Uint8Array, message: Uint8Array) => {
41-
// Compare the signature with the message for testing
42-
// In our mock, signature should match message for valid verification
43-
if (signature.length !== message.length) return false;
44-
return signature.every((byte, i) => byte === message[i]);
45-
},
46-
};
47-
});
16+
} from "../index.js";
17+
18+
// Mock is automatically picked up from src/__mocks__/@noble/secp256k1.ts
19+
vi.mock("@noble/secp256k1");
4820

4921
describe("nostr-nsec-seedphrase", () => {
5022
beforeAll(() => {

src/__tests__/mocks/secp256k1.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
"resolveJsonModule": true
2222
},
2323
"include": ["src/**/*"],
24-
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
24+
"exclude": ["node_modules", "dist"]
2525
}

0 commit comments

Comments
 (0)