Skip to content

Commit

Permalink
Merge pull request #16 from susumuota/refactor-crypto
Browse files Browse the repository at this point in the history
refactor: add one-webcrypto to handle crypto package properly.
  • Loading branch information
susumuota committed Apr 23, 2023
2 parents cbd9e1b + 729fe19 commit 449274c
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 489 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node-version: [18.15.0]
node-version: [18.16.0]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node-version: [18.15.0]
node-version: [18.16.0]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 120,
"printWidth": 119,
"tabWidth": 2,
"useTabs": false,
"semi": true,
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# nostrain: Nostr client library with no strain

[![npm](https://img.shields.io/npm/v/nostrain?color=blue)](https://www.npmjs.com/package/nostrain)
[![npm bundle size](https://img.shields.io/bundlephobia/min/nostrain)](https://github.com/susumuota/nostrain)
[![GitHub](https://img.shields.io/github/license/susumuota/nostrain)](https://github.com/susumuota/nostrain/blob/main/LICENSE)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/susumuota/nostrain/build.yaml)](https://github.com/susumuota/nostrain/actions/workflows/build.yaml)
[![GitHub last commit](https://img.shields.io/github/last-commit/susumuota/nostrain)](https://github.com/susumuota/nostrain/commits)
Expand Down
824 changes: 353 additions & 471 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nostrain",
"version": "0.9.3",
"version": "0.9.4",
"description": "Nostr client library with no strain.",
"author": "Susumu OTA",
"license": "MIT",
Expand Down Expand Up @@ -46,17 +46,18 @@
"@types/node": "^18.15.11",
"concurrently": "^8.0.1",
"node-fetch": "^3.3.1",
"prettier": "2.8.7",
"prettier": "^2.8.8",
"typescript": "^5.0.3",
"vite": "^4.2.0",
"vitest": "^0.29.8",
"vitest": "^0.30.1",
"websocket-polyfill": "^0.0.3"
},
"dependencies": {
"@noble/curves": "^0.9.1",
"@noble/curves": "^1.0.0",
"@noble/hashes": "^1.3.0",
"@scure/base": "^1.1.1",
"@scure/bip32": "^1.2.0",
"@scure/bip39": "^1.2.0"
"@scure/bip39": "^1.2.0",
"one-webcrypto": "^1.0.3"
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export * as nip05 from './nip05';
export * as nip06 from './nip06';
export * as nip10 from './nip10';
export * as nip19 from './nip19';
// export * as nip21 from './nip21'; // TODO
export * as nip26 from './nip26';
// export * as nip27 from './nip27'; // TODO
export * as nip39 from './nip39';
// export * as nip42 from './nip42'; // TODO
// export * as nip57 from './nip57'; // TODO

export * as fj from './fakejson';
Expand Down
2 changes: 0 additions & 2 deletions src/nip04.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import { test, expect } from 'vitest';

globalThis.crypto = require('crypto'); // TODO: how to pass without this?

import { nip04, getPublicKey, generatePrivateKey } from '../dist/nostrain';

test('encrypt and decrypt message', async () => {
Expand Down
9 changes: 5 additions & 4 deletions src/nip04.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { secp256k1 } from '@noble/curves/secp256k1';
import { randomBytes } from '@noble/hashes/utils';
import { base64 } from '@scure/base';
import { webcrypto } from 'one-webcrypto';

import { utf8Decoder, utf8Encoder } from './utils';

Expand All @@ -14,8 +15,8 @@ const encrypt = async (privkey: string, pubkey: string, text: string) => {
const normalizedKey = getNormalizedX(key);
const iv = Uint8Array.from(randomBytes(16));
const plainText = utf8Encoder.encode(text);
const cryptoKey = await crypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['encrypt']);
const cipherText = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, cryptoKey, plainText);
const cryptoKey = await webcrypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['encrypt']);
const cipherText = await webcrypto.subtle.encrypt({ name: 'AES-CBC', iv }, cryptoKey, plainText);
const ctb64 = base64.encode(new Uint8Array(cipherText));
const ivb64 = base64.encode(new Uint8Array(iv.buffer));
return `${ctb64}?iv=${ivb64}`;
Expand All @@ -26,10 +27,10 @@ const decrypt = async (privkey: string, pubkey: string, data: string) => {
if (!ctb64 || !ivb64) throw new Error('invalid data');
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey);
const normalizedKey = getNormalizedX(key);
const cryptoKey = await crypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['decrypt']);
const cryptoKey = await webcrypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['decrypt']);
const cipherText = base64.decode(ctb64);
const iv = base64.decode(ivb64);
const plainText = await crypto.subtle.decrypt({ name: 'AES-CBC', iv }, cryptoKey, cipherText);
const plainText = await webcrypto.subtle.decrypt({ name: 'AES-CBC', iv }, cryptoKey, cipherText);
return utf8Decoder.decode(plainText);
};

Expand Down
2 changes: 0 additions & 2 deletions src/relay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import { test, expect, beforeAll, afterAll } from 'vitest';

globalThis.crypto = require('crypto'); // TODO: how to pass without this?

import 'websocket-polyfill';
import { relayInit, generatePrivateKey, getPublicKey, getEventHash, signEvent } from '../dist/nostrain';

Expand Down
4 changes: 3 additions & 1 deletion src/relay.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2023 Susumu OTA <1632335+susumuota@users.noreply.github.com>
// SPDX-License-Identifier: MIT

import { webcrypto } from 'one-webcrypto';

import type { Event } from './event';
import type { Filter } from './filter';

Expand Down Expand Up @@ -202,7 +204,7 @@ class Sub {
// send 'REQ' and handle 'EVENT' or 'EOSE' until unsub is called
sub(filters: Filter[]) {
if (!this.#relay.connected()) throw new Error('not connected');
this.#id = crypto.randomUUID();
this.#id = webcrypto.randomUUID();
this.#filters = filters;
this.#messageListener = ((ev: globalThis.MessageEvent) => {
try {
Expand Down
2 changes: 2 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default defineConfig({
'@scure/bip32',
'@scure/bip39',
'@scure/bip39/wordlists/english.js',
'one-webcrypto',
],
output: {
globals: {
Expand All @@ -30,6 +31,7 @@ export default defineConfig({
'@scure/bip32': 'ScureBip32',
'@scure/bip39': 'ScureBip39',
'@scure/bip39/wordlists/english.js': 'ScureBip39English',
'one-webcrypto': 'OneWebcrypto',
},
},
},
Expand Down

0 comments on commit 449274c

Please sign in to comment.