mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 00:58:51 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb085ffdf7 | ||
|
|
280d483ef4 | ||
|
|
54b55b98f1 | ||
|
|
84f9881812 |
@@ -1,18 +1,9 @@
|
|||||||
import { test, expect } from 'bun:test'
|
import { test, expect } from 'bun:test'
|
||||||
import crypto from 'node:crypto'
|
|
||||||
|
|
||||||
import { encrypt, decrypt } from './nip04.ts'
|
import { encrypt, decrypt } from './nip04.ts'
|
||||||
import { getPublicKey, generateSecretKey } from './pure.ts'
|
import { getPublicKey, generateSecretKey } from './pure.ts'
|
||||||
import { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
import { bytesToHex, hexToBytes } from '@noble/hashes/utils'
|
||||||
|
|
||||||
try {
|
|
||||||
// @ts-ignore
|
|
||||||
// eslint-disable-next-line no-undef
|
|
||||||
globalThis.crypto = crypto
|
|
||||||
} catch (err) {
|
|
||||||
/***/
|
|
||||||
}
|
|
||||||
|
|
||||||
test('encrypt and decrypt message', async () => {
|
test('encrypt and decrypt message', async () => {
|
||||||
let sk1 = generateSecretKey()
|
let sk1 = generateSecretKey()
|
||||||
let sk2 = generateSecretKey()
|
let sk2 = generateSecretKey()
|
||||||
|
|||||||
20
nip04.ts
20
nip04.ts
@@ -1,15 +1,10 @@
|
|||||||
import { bytesToHex, randomBytes } from '@noble/hashes/utils'
|
import { bytesToHex, randomBytes } from '@noble/hashes/utils'
|
||||||
import { secp256k1 } from '@noble/curves/secp256k1'
|
import { secp256k1 } from '@noble/curves/secp256k1'
|
||||||
|
import { cbc } from '@noble/ciphers/aes'
|
||||||
import { base64 } from '@scure/base'
|
import { base64 } from '@scure/base'
|
||||||
|
|
||||||
import { utf8Decoder, utf8Encoder } from './utils.ts'
|
import { utf8Decoder, utf8Encoder } from './utils.ts'
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
if (typeof crypto !== 'undefined' && !crypto.subtle && crypto.webcrypto) {
|
|
||||||
// @ts-ignore
|
|
||||||
crypto.subtle = crypto.webcrypto.subtle
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): Promise<string> {
|
export async function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): Promise<string> {
|
||||||
const privkey: string = secretKey instanceof Uint8Array ? bytesToHex(secretKey) : secretKey
|
const privkey: string = secretKey instanceof Uint8Array ? bytesToHex(secretKey) : secretKey
|
||||||
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||||
@@ -17,8 +12,9 @@ export async function encrypt(secretKey: string | Uint8Array, pubkey: string, te
|
|||||||
|
|
||||||
let iv = Uint8Array.from(randomBytes(16))
|
let iv = Uint8Array.from(randomBytes(16))
|
||||||
let plaintext = utf8Encoder.encode(text)
|
let plaintext = utf8Encoder.encode(text)
|
||||||
let cryptoKey = await crypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['encrypt'])
|
|
||||||
let ciphertext = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, cryptoKey, plaintext)
|
let ciphertext = cbc(normalizedKey, iv).encrypt(plaintext)
|
||||||
|
|
||||||
let ctb64 = base64.encode(new Uint8Array(ciphertext))
|
let ctb64 = base64.encode(new Uint8Array(ciphertext))
|
||||||
let ivb64 = base64.encode(new Uint8Array(iv.buffer))
|
let ivb64 = base64.encode(new Uint8Array(iv.buffer))
|
||||||
|
|
||||||
@@ -31,14 +27,12 @@ export async function decrypt(secretKey: string | Uint8Array, pubkey: string, da
|
|||||||
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||||
let normalizedKey = getNormalizedX(key)
|
let normalizedKey = getNormalizedX(key)
|
||||||
|
|
||||||
let cryptoKey = await crypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['decrypt'])
|
|
||||||
let ciphertext = base64.decode(ctb64)
|
|
||||||
let iv = base64.decode(ivb64)
|
let iv = base64.decode(ivb64)
|
||||||
|
let ciphertext = base64.decode(ctb64)
|
||||||
|
|
||||||
let plaintext = await crypto.subtle.decrypt({ name: 'AES-CBC', iv }, cryptoKey, ciphertext)
|
let plaintext = cbc(normalizedKey, iv).decrypt(ciphertext)
|
||||||
|
|
||||||
let text = utf8Decoder.decode(plaintext)
|
return utf8Decoder.decode(plaintext)
|
||||||
return text
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNormalizedX(key: Uint8Array): Uint8Array {
|
function getNormalizedX(key: Uint8Array): Uint8Array {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ describe('requesting relay as for NIP11', () => {
|
|||||||
test('testing a relay', async () => {
|
test('testing a relay', async () => {
|
||||||
const info = await fetchRelayInformation('wss://atlas.nostr.land')
|
const info = await fetchRelayInformation('wss://atlas.nostr.land')
|
||||||
expect(info.name).toEqual('nostr.land')
|
expect(info.name).toEqual('nostr.land')
|
||||||
expect(info.description).toEqual('nostr.land family of relays (us-or-01)')
|
expect(info.description).toContain('nostr.land family')
|
||||||
expect(info.fees).toBeTruthy()
|
expect(info.fees).toBeTruthy()
|
||||||
expect(info.supported_nips).toEqual([1, 2, 4, 9, 11, 12, 16, 20, 22, 28, 33, 40])
|
expect(info.supported_nips).toEqual([1, 2, 4, 9, 11, 12, 16, 20, 22, 28, 33, 40])
|
||||||
expect(info.software).toEqual('custom')
|
expect(info.software).toEqual('custom')
|
||||||
|
|||||||
4
nip44.ts
4
nip44.ts
@@ -1,5 +1,5 @@
|
|||||||
import { chacha20 } from '@noble/ciphers/chacha'
|
import { chacha20 } from '@noble/ciphers/chacha'
|
||||||
import { ensureBytes, equalBytes } from '@noble/ciphers/utils'
|
import { equalBytes } from '@noble/ciphers/utils'
|
||||||
import { secp256k1 } from '@noble/curves/secp256k1'
|
import { secp256k1 } from '@noble/curves/secp256k1'
|
||||||
import { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf'
|
import { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf'
|
||||||
import { hmac } from '@noble/hashes/hmac'
|
import { hmac } from '@noble/hashes/hmac'
|
||||||
@@ -23,8 +23,6 @@ const u = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getMessageKeys(conversationKey: Uint8Array, nonce: Uint8Array) {
|
getMessageKeys(conversationKey: Uint8Array, nonce: Uint8Array) {
|
||||||
ensureBytes(conversationKey, 32)
|
|
||||||
ensureBytes(nonce, 32)
|
|
||||||
const keys = hkdf_expand(sha256, conversationKey, nonce, 76)
|
const keys = hkdf_expand(sha256, conversationKey, nonce, 76)
|
||||||
return {
|
return {
|
||||||
chacha_key: keys.subarray(0, 32),
|
chacha_key: keys.subarray(0, 32),
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.2.1",
|
"version": "2.3.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -208,7 +208,7 @@
|
|||||||
},
|
},
|
||||||
"license": "Unlicense",
|
"license": "Unlicense",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/ciphers": "0.2.0",
|
"@noble/ciphers": "^0.5.1",
|
||||||
"@noble/curves": "1.2.0",
|
"@noble/curves": "1.2.0",
|
||||||
"@noble/hashes": "1.3.1",
|
"@noble/hashes": "1.3.1",
|
||||||
"@scure/base": "1.1.1",
|
"@scure/base": "1.1.1",
|
||||||
|
|||||||
Reference in New Issue
Block a user