diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b244f1c..b925cca 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/nostr.js b/nostr.js index c8a7732..3b6eda6 100644 --- a/nostr.js +++ b/nostr.js @@ -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); @@ -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 = { diff --git a/nostr.test.js b/nostr.test.js index fb096e1..6dd1490 100644 --- a/nostr.test.js +++ b/nostr.test.js @@ -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; @@ -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; diff --git a/package-lock.json b/package-lock.json index ee28e1b..8613a7f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "MIT", "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" @@ -1265,7 +1266,7 @@ "url": "https://paulmillr.com/funding/" } }, - "node_modules/@noble/hashes": { + "node_modules/@noble/curves/node_modules/@noble/hashes": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.1.tgz", "integrity": "sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==", @@ -1276,6 +1277,17 @@ "url": "https://paulmillr.com/funding/" } }, + "node_modules/@noble/hashes": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.6.1.tgz", + "integrity": "sha512-pq5D8h10hHBjyqX+cfBm0i8JUXJ0UhczFc4r74zbuT9XgewFo2E3J1cOaGtdZynILNmQ685YWGzGE1Zv6io50w==", + "engines": { + "node": "^14.21.3 || >=16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -1335,6 +1347,17 @@ "url": "https://paulmillr.com/funding/" } }, + "node_modules/@scure/bip32/node_modules/@noble/hashes": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.3.tgz", + "integrity": "sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==", + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, "node_modules/@scure/bip39": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/@scure/bip39/-/bip39-1.2.1.tgz", @@ -1347,6 +1370,17 @@ "url": "https://paulmillr.com/funding/" } }, + "node_modules/@scure/bip39/node_modules/@noble/hashes": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.3.tgz", + "integrity": "sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==", + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, "node_modules/@sinclair/typebox": { "version": "0.27.8", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", @@ -3739,6 +3773,17 @@ "url": "https://paulmillr.com/funding/" } }, + "node_modules/nostr-tools/node_modules/@noble/hashes": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.1.tgz", + "integrity": "sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==", + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, "node_modules/nostr-wasm": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/nostr-wasm/-/nostr-wasm-0.1.0.tgz", diff --git a/package.json b/package.json index 6505580..0fdff33 100644 --- a/package.json +++ b/package.json @@ -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"