Skip to content

Commit

Permalink
Fix converting seckey
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowCait committed Dec 29, 2024
1 parent bec029c commit 35c525f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
env:
NOSTR_RELAYS: ${{ vars.NOSTR_RELAYS_TEST }}
NOSTR_PRIVATE_KEY: ${{ secrets.NOSTR_PRIVATE_KEY_TEST }}
NOSTR_SECKEY: ${{ secrets.NOSTR_SECKEY_TEST }}

e2e-test:
runs-on: ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion nostr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const WebSocket = require('ws');
const { nip19, getPublicKey, finalizeEvent } = require('nostr-tools');
const { useWebSocketImplementation } = require('nostr-tools/pool');
const { hexToBytes } = require('@noble/hashes/utils');

useWebSocketImplementation(WebSocket);

Expand All @@ -11,7 +12,7 @@ useWebSocketImplementation(WebSocket);
* @param {string[][]} tags
*/
module.exports.createEvent = (privateKey, kind, content, tags) => {
const seckey = privateKey.startsWith('nsec') ? nip19.decode(privateKey).data : Uint8Array.from(Buffer.from(privateKey));
const seckey = privateKey.startsWith('nsec') ? nip19.decode(privateKey).data : hexToBytes(privateKey);

const createdAt = Math.round(Date.now() / 1000);
let event = {
Expand Down
18 changes: 17 additions & 1 deletion nostr.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { createEvent, publishEvent } = require('./nostr');
require('dotenv').config();

test('createEvent', async () => {
test('createEvent with nsec', async () => {
const privateKey = process.env.NOSTR_PRIVATE_KEY;
const content = 'test';
const kind = 1;
Expand All @@ -17,6 +17,22 @@ test('createEvent', async () => {
expect(event.content).toBe(content);
});

test('createEvent with seckey', async () => {
const privateKey = process.env.NOSTR_SECKEY;
const content = 'test';
const kind = 1;
const tags = [];
const event = createEvent(privateKey, kind, content, tags);
expect(event).toHaveProperty('id');
expect(event).toHaveProperty('pubkey');
expect(event).toHaveProperty('created_at');
expect(event).toHaveProperty('kind');
expect(event).toHaveProperty('tags');
expect(event).toHaveProperty('content');
expect(event).toHaveProperty('sig');
expect(event.content).toBe(content);
});

test('publishEvent', async () => {
const relays = process.env.NOSTR_RELAYS.split("\n").map(x => x.trim()).filter(x => x.startsWith('wss://'));
const privateKey = process.env.NOSTR_PRIVATE_KEY;
Expand Down
47 changes: 46 additions & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"homepage": "https://github.com/snow-actions/nostr#readme",
"dependencies": {
"@actions/core": "^1.10.0",
"@noble/hashes": "^1.6.1",
"js-yaml": "^4.1.0",
"nostr-tools": "^2.10.4",
"ws": "^8.16.0"
Expand Down

0 comments on commit 35c525f

Please sign in to comment.