Skip to content

Commit

Permalink
nip46 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Nov 23, 2024
1 parent 79ff8f1 commit bed2f8d
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 33 deletions.
7 changes: 7 additions & 0 deletions ndk-cache-dexie/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @nostr-dev-kit/ndk-cache-dexie

## 2.5.8

### Patch Changes

- Updated dependencies
- @nostr-dev-kit/ndk@2.10.7

## 2.5.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk-cache-dexie/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-cache-dexie",
"version": "2.5.7",
"version": "2.5.8",
"description": "NDK Dexie Cache Adapter",
"license": "MIT",
"docs": "typedoc",
Expand Down
7 changes: 7 additions & 0 deletions ndk-cache-nostr/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @nostr-dev-kit/ndk-cache-nostr

## 0.1.7

### Patch Changes

- Updated dependencies
- @nostr-dev-kit/ndk@2.10.7

## 0.1.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk-cache-nostr/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-cache-nostr",
"version": "0.1.6",
"version": "0.1.7",
"description": "NDK cache adapter that uses a local nostr relay.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
7 changes: 7 additions & 0 deletions ndk-cache-redis/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @nostr-dev-kit/ndk-cache-redis

## 2.1.24

### Patch Changes

- Updated dependencies
- @nostr-dev-kit/ndk@2.10.7

## 2.1.23

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk-cache-redis/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-cache-redis",
"version": "2.1.23",
"version": "2.1.24",
"description": "NDK cache adapter for redis.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
8 changes: 8 additions & 0 deletions ndk-mobile/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @nostr-dev-kit/ndk-mobile

## 0.2.1

### Patch Changes

- Updated dependencies
- @nostr-dev-kit/ndk@2.10.7
- @nostr-dev-kit/ndk-wallet@0.3.15

## 0.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk-mobile/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-mobile",
"version": "0.2.0",
"version": "0.2.1",
"description": "NDK Mobile",
"private": false,
"license": "MIT",
Expand Down
7 changes: 7 additions & 0 deletions ndk-svelte-components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @nostr-dev-kit/ndk-svelte-components

## 2.3.5

### Patch Changes

- Updated dependencies
- @nostr-dev-kit/ndk@2.10.7

## 2.3.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk-svelte-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-svelte-components",
"version": "2.3.4",
"version": "2.3.5",
"description": "",
"license": "MIT",
"type": "module",
Expand Down
7 changes: 7 additions & 0 deletions ndk-svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @nostr-dev-kit/ndk-svelte

## 2.3.2

### Patch Changes

- Updated dependencies
- @nostr-dev-kit/ndk@2.10.7

## 2.3.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk-svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-svelte",
"version": "2.3.1",
"version": "2.3.2",
"description": "This package provides convenience functionalities to make usage of NDK with Svelte nicer.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
7 changes: 7 additions & 0 deletions ndk-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @nostr-dev-kit/ndk-cache-redis

## 0.3.15

### Patch Changes

- Updated dependencies
- @nostr-dev-kit/ndk@2.10.7

## 0.3.14

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk-wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-wallet",
"version": "0.3.14",
"version": "0.3.15",
"description": "NDK Wallet",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
6 changes: 6 additions & 0 deletions ndk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @nostr-dev-kit/ndk

## 2.10.7

### Patch Changes

- auto-auth to nip46 relays, and set the user pubkey after getting it from remote

## 2.10.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk",
"version": "2.10.6",
"version": "2.10.7",
"description": "NDK - Nostr Development Kit",
"homepage": "https://ndk.fyi",
"documentation": "https://github.com/nostr-dev-kit/ndk/blob/master/docs/modules.md",
Expand Down
17 changes: 12 additions & 5 deletions ndk/src/relay/sets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NDKEvent } from "../../events/index.js";
import type { NDK } from "../../ndk/index.js";
import { normalizeRelayUrl } from "../../utils/normalize-url.js";
import { NDKRelay, NDKRelayStatus } from "../index.js";
import { NDKPool } from "../pool/index.js";

export { calculateRelaySetFromEvent } from "./calculate.js";

Expand Down Expand Up @@ -48,10 +49,12 @@ export class NDKRelaySet {
readonly relays: Set<NDKRelay>;
private debug: debug.Debugger;
private ndk: NDK;
private pool: NDKPool;

public constructor(relays: Set<NDKRelay>, ndk: NDK) {
public constructor(relays: Set<NDKRelay>, ndk: NDK, pool?: NDKPool) {
this.relays = relays;
this.ndk = ndk;
this.pool = pool ?? ndk.pool;
this.debug = ndk.debug.extend("relayset");
}

Expand All @@ -77,10 +80,14 @@ export class NDKRelaySet {
* @param connect - whether to connect to the relay immediately if it was already in the pool but not connected
* @returns NDKRelaySet
*/
static fromRelayUrls(relayUrls: string[], ndk: NDK, connect = true): NDKRelaySet {
static fromRelayUrls(relayUrls: string[], ndk: NDK, connect = true, pool?: NDKPool): NDKRelaySet {
pool = pool ?? ndk.pool;

if (!pool) throw new Error("No pool provided");

const relays = new Set<NDKRelay>();
for (const url of relayUrls) {
const relay = ndk.pool.relays.get(normalizeRelayUrl(url));
const relay = pool.relays.get(normalizeRelayUrl(url));
if (relay) {
if (relay.status < NDKRelayStatus.CONNECTED && connect) {
relay.connect();
Expand All @@ -93,7 +100,7 @@ export class NDKRelaySet {
ndk?.relayAuthDefaultPolicy,
ndk
);
ndk.pool.useTemporaryRelay(
pool.useTemporaryRelay(
temporaryRelay,
undefined,
"requested from fromRelayUrls " + relayUrls
Expand All @@ -102,7 +109,7 @@ export class NDKRelaySet {
}
}

return new NDKRelaySet(new Set(relays), ndk);
return new NDKRelaySet(new Set(relays), ndk, pool);
}

/**
Expand Down
10 changes: 1 addition & 9 deletions ndk/src/signers/nip46/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export class NDKNip46Signer extends EventEmitter implements NDKSigner {
(response: NDKRpcResponse) => {
if (response.result === "ack") {
this.getPublicKey().then((pubkey) => {
this.userPubkey = pubkey;
this._user = new NDKUser({ pubkey });
resolve(this._user);
});
Expand Down Expand Up @@ -239,8 +240,6 @@ export class NDKNip46Signer extends EventEmitter implements NDKSigner {
value: string,
method: "nip04" | "nip44"
): Promise<string> {
this.debug("asking for encryption");

const promise = new Promise<string>((resolve, reject) => {
if (!this.bunkerPubkey) throw new Error("Bunker pubkey not set");

Expand All @@ -267,8 +266,6 @@ export class NDKNip46Signer extends EventEmitter implements NDKSigner {
value: string,
method: "nip04" | "nip44"
): Promise<string> {
this.debug("asking for decryption");

const promise = new Promise<string>((resolve, reject) => {
if (!this.bunkerPubkey) throw new Error("Bunker pubkey not set");

Expand All @@ -291,8 +288,6 @@ export class NDKNip46Signer extends EventEmitter implements NDKSigner {
}

public async sign(event: NostrEvent): Promise<string> {
this.debug("asking for a signature");

const promise = new Promise<string>((resolve, reject) => {
if (!this.bunkerPubkey) throw new Error("Bunker pubkey not set");

Expand All @@ -302,7 +297,6 @@ export class NDKNip46Signer extends EventEmitter implements NDKSigner {
[JSON.stringify(event)],
24133,
(response: NDKRpcResponse) => {
this.debug("got a response", response);
if (!response.error) {
const json = JSON.parse(response.result);
resolve(json.sig);
Expand All @@ -329,7 +323,6 @@ export class NDKNip46Signer extends EventEmitter implements NDKSigner {
email?: string
): Promise<Hexpubkey> {
await this.startListening();
this.debug("asking to create an account");
const req: string[] = [];

if (username) req.push(username);
Expand All @@ -345,7 +338,6 @@ export class NDKNip46Signer extends EventEmitter implements NDKSigner {
req,
NDKKind.NostrConnect,
(response: NDKRpcResponse) => {
this.debug("got a response", response);
if (!response.error) {
const pubkey = response.result;
resolve(pubkey);
Expand Down
49 changes: 38 additions & 11 deletions ndk/src/signers/nip46/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
type NDKFilter,
type NDKSubscription,
} from "../../subscription";
import { NDKRelayAuthPolicies } from "../../relay/auth-policies";
import { NDKPool } from "../../relay/pool";

export interface NDKRpcRequest {
id: string;
Expand All @@ -33,12 +35,31 @@ export class NDKNostrRpc extends EventEmitter {
private relaySet: NDKRelaySet | undefined;
private debug: debug.Debugger;
public encryptionType: "nip04" | "nip44" = "nip04";
private pool: NDKPool | undefined;

public constructor(ndk: NDK, signer: NDKSigner, debug: debug.Debugger, relayUrls?: string[]) {
super();
this.ndk = ndk;
this.signer = signer;
this.relaySet = relayUrls ? NDKRelaySet.fromRelayUrls(relayUrls, ndk) : undefined;

// if we have relays, we create a separate pool for it
if (relayUrls) {
this.pool = new NDKPool(
relayUrls,
Array.from(ndk.pool.blacklistRelayUrls),
ndk,
debug.extend("rpc-pool")
);

this.relaySet = new NDKRelaySet(new Set(), ndk, this.pool);
for (const url of relayUrls) {
const relay = this.pool.getRelay(url, false, false);
relay.authPolicy = NDKRelayAuthPolicies.signIn({ ndk, signer, debug });
this.relaySet.addRelay(relay);
relay.connect();
}
}

this.debug = debug.extend("rpc");
}

Expand All @@ -52,13 +73,12 @@ export class NDKNostrRpc extends EventEmitter {
closeOnEose: false,
groupable: false,
cacheUsage: NDKSubscriptionCacheUsage.ONLY_RELAY,
pool: this.pool,
},
this.relaySet,
false
);

this.debug("starting subscription");

sub.on("event", async (event: NDKEvent) => {
try {
const parsedEvent = await this.parseEvent(event);
Expand All @@ -85,19 +105,26 @@ export class NDKNostrRpc extends EventEmitter {
// support both nip04 and nip44 encryption
if (this.encryptionType === 'nip44' && event.content.includes('?iv=')) {
this.encryptionType = 'nip04';
console.log('switching to nip04 encryption')
} else if (this.encryptionType === 'nip04' && !event.content.includes('?iv=')) {
this.encryptionType = 'nip44';
console.log('switching to nip44 encryption')
}

const remoteUser = this.ndk.getUser({ pubkey: event.pubkey });
remoteUser.ndk = this.ndk;
const decryptedContent = await this.signer.decrypt(
remoteUser,
event.content,
this.encryptionType
);
let decryptedContent: string;

try {
decryptedContent = await this.signer.decrypt(
remoteUser,
event.content,
this.encryptionType
);
} catch (e) {
const otherEncryptionType = this.encryptionType === 'nip04' ? 'nip44' : 'nip04';
decryptedContent = await this.signer.decrypt(remoteUser, event.content, otherEncryptionType);
this.encryptionType = otherEncryptionType;
}

const parsedContent = JSON.parse(decryptedContent);
const { id, method, params, result, error } = parsedContent;

Expand Down Expand Up @@ -128,7 +155,7 @@ export class NDKNostrRpc extends EventEmitter {
tags: [["p", remotePubkey]],
pubkey: localUser.pubkey,
} as NostrEvent);

event.content = await this.signer.encrypt(remoteUser, event.content, this.encryptionType);
await event.sign(this.signer);
await event.publish(this.relaySet);
Expand Down

0 comments on commit bed2f8d

Please sign in to comment.