2023-07-13 10:35:34 +00:00
|
|
|
import { chan, closed, sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
|
2024-01-01 17:28:10 +00:00
|
|
|
import { PrivateKey, PublicKey } from "../../libs/nostr.ts/key.ts";
|
2023-06-30 14:05:57 +00:00
|
|
|
import {
|
|
|
|
InMemoryAccountContext,
|
|
|
|
NostrAccountContext,
|
|
|
|
NostrEvent,
|
2023-07-21 08:50:38 +00:00
|
|
|
NostrKind,
|
2023-06-30 14:05:57 +00:00
|
|
|
UnsignedNostrEvent,
|
2024-01-01 17:28:10 +00:00
|
|
|
} from "../../libs/nostr.ts/nostr.ts";
|
2024-03-15 13:44:17 +00:00
|
|
|
import { LocalPrivateKeyController } from "./sign-in.ts";
|
2023-06-30 14:05:57 +00:00
|
|
|
|
2023-07-09 07:06:13 +00:00
|
|
|
type NIP07 = {
|
2023-06-30 14:05:57 +00:00
|
|
|
getPublicKey(): Promise<string>;
|
2024-03-15 17:23:54 +00:00
|
|
|
signEvent<T extends NostrKind>(event: UnsignedNostrEvent<T>): Promise<NostrEvent<T>>;
|
|
|
|
getRelays(): { [url: string]: { read: boolean; write: boolean } };
|
2023-07-09 07:06:13 +00:00
|
|
|
nip04: {
|
|
|
|
encrypt: (pubkey: string, plaintext: string) => Promise<string | Error>;
|
|
|
|
decrypt: (pubkey: string, ciphertext: string) => Promise<string | Error>;
|
|
|
|
};
|
2024-03-15 17:23:54 +00:00
|
|
|
nip44: {
|
|
|
|
encrypt: (pubkey: string, plaintext: string) => Promise<string | Error>;
|
|
|
|
decrypt: (pubkey: string, ciphertext: string) => Promise<string | Error>;
|
|
|
|
};
|
2023-06-30 14:05:57 +00:00
|
|
|
};
|
|
|
|
|
2023-07-09 07:06:13 +00:00
|
|
|
export class Nip7ExtensionContext implements NostrAccountContext {
|
|
|
|
static async New(): Promise<Nip7ExtensionContext | Error | undefined> {
|
2024-03-15 17:23:54 +00:00
|
|
|
// wait for nip-07 extension init
|
|
|
|
await sleep(20);
|
|
|
|
let ext;
|
|
|
|
if ("nostr" in window) {
|
|
|
|
ext = window.nostr as NIP07;
|
|
|
|
} else {
|
2023-06-30 14:05:57 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2024-03-15 17:23:54 +00:00
|
|
|
|
2023-06-30 14:05:57 +00:00
|
|
|
let pubkey: string | undefined;
|
|
|
|
try {
|
|
|
|
pubkey = await ext.getPublicKey();
|
|
|
|
} catch (e) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
const pub = PublicKey.FromHex(pubkey);
|
|
|
|
if (pub instanceof Error) {
|
|
|
|
return pub;
|
|
|
|
}
|
2023-07-09 07:06:13 +00:00
|
|
|
return new Nip7ExtensionContext(ext, pub);
|
2023-06-30 14:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private constructor(
|
2024-03-15 17:23:54 +00:00
|
|
|
private nip07: NIP07,
|
2023-06-30 14:05:57 +00:00
|
|
|
public publicKey: PublicKey,
|
|
|
|
) {
|
2024-03-15 17:23:54 +00:00
|
|
|
console.log(nip07);
|
2023-06-30 14:05:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-15 17:23:54 +00:00
|
|
|
async signEvent<T extends NostrKind = NostrKind>(event: UnsignedNostrEvent<T>) {
|
|
|
|
return this.nip07.signEvent(event);
|
2023-06-30 14:05:57 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 07:06:13 +00:00
|
|
|
encrypt = async (pubkey: string, plaintext: string) => {
|
2024-03-15 17:23:54 +00:00
|
|
|
if (!("nip44" in this.nip07)) {
|
|
|
|
return new Error(
|
|
|
|
"This NIP-07 extension does not implement NIP-44, please use a NIP-44 compatible one",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return this.nip07.nip44.encrypt(pubkey, plaintext);
|
|
|
|
} catch (e) {
|
|
|
|
return e as Error;
|
2023-07-09 07:06:13 +00:00
|
|
|
}
|
|
|
|
};
|
2024-03-15 17:23:54 +00:00
|
|
|
|
2023-07-09 07:06:13 +00:00
|
|
|
decrypt = async (pubkey: string, ciphertext: string) => {
|
2023-10-21 05:04:04 +00:00
|
|
|
try {
|
2024-03-15 17:23:54 +00:00
|
|
|
if (ciphertext.includes("?iv")) {
|
|
|
|
return await this.nip07.nip04.decrypt(pubkey, ciphertext);
|
|
|
|
} else {
|
|
|
|
if (!("nip44" in this.nip07)) {
|
|
|
|
return new Error(
|
|
|
|
"This NIP-07 extension does not implement NIP-44, please use a NIP-44 compatible one",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return await this.nip07.nip44.decrypt(pubkey, ciphertext);
|
|
|
|
}
|
2023-10-21 05:04:04 +00:00
|
|
|
} catch (e) {
|
|
|
|
return e as Error;
|
2023-06-30 14:05:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-15 13:44:17 +00:00
|
|
|
export async function GetLocalStorageAccountContext() {
|
|
|
|
const priKey = await LocalPrivateKeyController.getKey("blowater");
|
2024-01-19 07:21:43 +00:00
|
|
|
if (priKey instanceof Error) {
|
|
|
|
return priKey;
|
|
|
|
} else if (priKey == undefined) {
|
2023-06-30 14:05:57 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2024-01-19 07:21:43 +00:00
|
|
|
return InMemoryAccountContext.New(priKey);
|
2023-06-30 14:05:57 +00:00
|
|
|
}
|