Remove tsc-multi, re-install gts, apply autoformatting and linting

This commit is contained in:
Jon Staab
2024-12-17 10:59:27 -08:00
parent 0b86613161
commit f33e03740e
122 changed files with 2243 additions and 2178 deletions
+6 -6
View File
@@ -1,6 +1,6 @@
export * from './util'
export * from './nip59'
export * from './signers/nip01'
export * from './signers/nip07'
export * from './signers/nip46'
export * from './signers/nip55'
export * from "./util.js"
export * from "./nip59.js"
export * from "./signers/nip01.js"
export * from "./signers/nip07.js"
export * from "./signers/nip46.js"
export * from "./signers/nip55.js"
+39 -22
View File
@@ -1,6 +1,6 @@
import {UnwrappedEvent, SignedEvent, HashedEvent, StampedEvent, WRAP, SEAL} from '@welshman/util'
import {own, hash, decrypt, ISigner} from './util'
import {Nip01Signer} from './signers/nip01'
import {UnwrappedEvent, SignedEvent, HashedEvent, StampedEvent, WRAP, SEAL} from "@welshman/util"
import {own, hash, decrypt, ISigner} from "./util.js"
import {Nip01Signer} from "./signers/nip01.js"
export const seen = new Map<string, UnwrappedEvent | Error>()
@@ -11,24 +11,39 @@ export const getRumor = async (signer: ISigner, template: StampedEvent) =>
hash(own(template, await signer.getPubkey()))
export const getSeal = async (signer: ISigner, pubkey: string, rumor: HashedEvent) =>
signer.sign(hash({
kind: SEAL,
pubkey: await signer.getPubkey(),
content: await signer.nip44.encrypt(pubkey, JSON.stringify(rumor)),
created_at: now(5),
tags: [],
}))
signer.sign(
hash({
kind: SEAL,
pubkey: await signer.getPubkey(),
content: await signer.nip44.encrypt(pubkey, JSON.stringify(rumor)),
created_at: now(5),
tags: [],
}),
)
export const getWrap = async (wrapper: ISigner, pubkey: string, seal: SignedEvent, tags: string[][]) =>
wrapper.sign(hash({
kind: WRAP,
pubkey: await wrapper.getPubkey(),
content: await wrapper.nip44.encrypt(pubkey, JSON.stringify(seal)),
created_at: now(5),
tags: [...tags, ["p", pubkey]],
}))
export const getWrap = async (
wrapper: ISigner,
pubkey: string,
seal: SignedEvent,
tags: string[][],
) =>
wrapper.sign(
hash({
kind: WRAP,
pubkey: await wrapper.getPubkey(),
content: await wrapper.nip44.encrypt(pubkey, JSON.stringify(seal)),
created_at: now(5),
tags: [...tags, ["p", pubkey]],
}),
)
export const wrap = async (signer: ISigner, wrapper: ISigner, pubkey: string, template: StampedEvent, tags: string[][] = []) => {
export const wrap = async (
signer: ISigner,
wrapper: ISigner,
pubkey: string,
template: StampedEvent,
tags: string[][] = [],
) => {
const rumor = await getRumor(signer, template)
const seal = await getSeal(signer, pubkey, rumor)
const wrap = await getWrap(wrapper, pubkey, seal, tags)
@@ -69,7 +84,10 @@ export const unwrap = async (signer: ISigner, wrap: SignedEvent) => {
// wrapping a single user signer and omit the wrapper signer argument to wrap, while still
// making it possible to pass a wrapper signer if desired.
export class Nip59 {
constructor(private signer: ISigner, private wrapper?: ISigner) {}
constructor(
private signer: ISigner,
private wrapper?: ISigner,
) {}
static fromSigner = (signer: ISigner) => new Nip59(signer)
@@ -80,6 +98,5 @@ export class Nip59 {
wrap = (pubkey: string, template: StampedEvent, tags: string[][] = []) =>
wrap(this.signer, this.wrapper || Nip01Signer.ephemeral(), pubkey, template, tags)
unwrap = (event: SignedEvent) =>
unwrap(this.signer, event)
unwrap = (event: SignedEvent) => unwrap(this.signer, event)
}
+6 -10
View File
@@ -1,5 +1,5 @@
import {StampedEvent} from '@welshman/util'
import {nip04, nip44, own, hash, sign, getPubkey, ISigner, makeSecret} from "../util"
import {StampedEvent} from "@welshman/util"
import {nip04, nip44, own, hash, sign, getPubkey, ISigner, makeSecret} from "../util.js"
export class Nip01Signer implements ISigner {
#pubkey: string
@@ -17,16 +17,12 @@ export class Nip01Signer implements ISigner {
sign = async (event: StampedEvent) => sign(hash(own(event, this.#pubkey)), this.secret)
nip04 = {
encrypt: async (pubkey: string, message: string) =>
nip04.encrypt(pubkey, this.secret, message),
decrypt: async (pubkey: string, message: string) =>
nip04.decrypt(pubkey, this.secret, message),
encrypt: async (pubkey: string, message: string) => nip04.encrypt(pubkey, this.secret, message),
decrypt: async (pubkey: string, message: string) => nip04.decrypt(pubkey, this.secret, message),
}
nip44 = {
encrypt: async (pubkey: string, message: string) =>
nip44.encrypt(pubkey, this.secret, message),
decrypt: async (pubkey: string, message: string) =>
nip44.decrypt(pubkey, this.secret, message),
encrypt: async (pubkey: string, message: string) => nip44.encrypt(pubkey, this.secret, message),
decrypt: async (pubkey: string, message: string) => nip44.decrypt(pubkey, this.secret, message),
}
}
+6 -4
View File
@@ -1,5 +1,5 @@
import {StampedEvent} from '@welshman/util'
import {hash, own, Sign, ISigner, EncryptionImplementation} from '../util'
import {StampedEvent} from "@welshman/util"
import {hash, own, Sign, ISigner, EncryptionImplementation} from "../util.js"
export type Nip07 = {
signEvent: Sign
@@ -23,7 +23,10 @@ export class Nip07Signer implements ISigner {
})
// Recover from errors
this.#lock = promise.then(() => undefined, () => undefined)
this.#lock = promise.then(
() => undefined,
() => undefined,
)
return promise
}
@@ -50,4 +53,3 @@ export class Nip07Signer implements ISigner {
this.#then(ext => ext.nip44.decrypt(pubkey, message)),
}
}
+58 -30
View File
@@ -1,14 +1,29 @@
import {Emitter, throttle, makePromise, defer, sleep, tryCatch, randomId, equals} from "@welshman/lib"
import {createEvent, normalizeRelayUrl, TrustedEvent, StampedEvent, NOSTR_CONNECT} from "@welshman/util"
import {
Emitter,
throttle,
makePromise,
defer,
sleep,
tryCatch,
randomId,
equals,
} from "@welshman/lib"
import {
createEvent,
normalizeRelayUrl,
TrustedEvent,
StampedEvent,
NOSTR_CONNECT,
} from "@welshman/util"
import {subscribe, publish, Subscription, SubscriptionEvent} from "@welshman/net"
import {ISigner, decrypt, hash, own} from '../util'
import {Nip01Signer} from './nip01'
import {ISigner, EncryptionImplementation, decrypt, hash, own} from "../util.js"
import {Nip01Signer} from "./nip01.js"
export type Nip46Algorithm = "nip04" | "nip44"
export enum Nip46Event {
Send = 'send',
Receive = 'receive',
Send = "send",
Receive = "receive",
}
export type Nip46BrokerParams = {
@@ -82,7 +97,10 @@ const popupManager = (() => {
export class Nip46Receiver extends Emitter {
public sub?: Subscription
constructor(public signer: ISigner, public params: Nip46BrokerParams) {
constructor(
public signer: ISigner,
public params: Nip46BrokerParams,
) {
super()
}
@@ -125,7 +143,10 @@ export class Nip46Sender extends Emitter {
public processing = false
public queue: Nip46Request[] = []
constructor(public signer: ISigner, public params: Nip46BrokerParams) {
constructor(
public signer: ISigner,
public params: Nip46BrokerParams,
) {
super()
}
@@ -160,7 +181,7 @@ export class Nip46Sender extends Emitter {
try {
await this.send(request)
} catch (error: any) {
console.error(`nip46 error:`, error, request)
console.error("nip46 error:", error, request)
}
}
} finally {
@@ -182,7 +203,10 @@ export class Nip46Request {
id = randomId()
promise = defer<Nip46ResponseWithResult, Nip46ResponseWithError>()
constructor(readonly method: string, readonly params: string[]) {}
constructor(
readonly method: string,
readonly params: string[],
) {}
listen = async (receiver: Nip46Receiver) => {
await receiver.start()
@@ -251,7 +275,7 @@ export class Nip46Broker extends Emitter {
const sender = new Nip46Sender(this.signer, this.params)
sender.on(Nip46Event.Send, (data: any) => {
console.log('nip46 send:', data)
console.log("nip46 send:", data)
})
return sender
@@ -261,7 +285,7 @@ export class Nip46Broker extends Emitter {
const receiver = new Nip46Receiver(this.signer, this.params)
receiver.on(Nip46Event.Receive, (data: any) => {
console.log('nip46 receive:', data)
console.log("nip46 receive:", data)
})
return receiver
@@ -314,7 +338,7 @@ export class Nip46Broker extends Emitter {
const _url = new URL(url)
relays = _url.searchParams.getAll("relay") || []
signerPubkey = _url.hostname || _url.pathname.replace(/\//g, '')
signerPubkey = _url.hostname || _url.pathname.replace(/\//g, "")
connectSecret = _url.searchParams.get("secret") || ""
} catch {
// pass
@@ -329,22 +353,24 @@ export class Nip46Broker extends Emitter {
const params = new URLSearchParams({...meta, secret})
for (const relay of this.params.relays) {
params.append('relay', relay)
params.append("relay", relay)
}
return `nostrconnect://${clientPubkey}?${params.toString()}`
}
waitForNostrconnect = (url: string, abort?: AbortController) => {
const secret = new URL(url).searchParams.get('secret')
const secret = new URL(url).searchParams.get("secret")
return makePromise<Nip46ResponseWithResult, Nip46Response | undefined>((resolve, reject) => {
const onReceive = (response: Nip46Response) => {
if (["ack", secret].includes(response.result!)) {
this.setParams({signerPubkey: response.event.pubkey})
if (response.result === 'ack') {
console.warn("Bunker responded to nostrconnect with 'ack', which can lead to session hijacking")
if (response.result === "ack") {
console.warn(
"Bunker responded to nostrconnect with 'ack', which can lead to session hijacking",
)
}
resolve(response as Nip46ResponseWithResult)
@@ -362,7 +388,7 @@ export class Nip46Broker extends Emitter {
this.receiver.on(Nip46Event.Receive, onReceive)
this.receiver.start()
abort?.signal.addEventListener('abort', () => {
abort?.signal.addEventListener("abort", () => {
reject(undefined)
cleanup()
})
@@ -394,9 +420,21 @@ export class Nip46Broker extends Emitter {
}
export class Nip46Signer implements ISigner {
public pubkey?: string
pubkey?: string
nip04: EncryptionImplementation
nip44: EncryptionImplementation
constructor(public broker: Nip46Broker) {}
constructor(public broker: Nip46Broker) {
this.nip04 = {
encrypt: this.broker.nip04Encrypt,
decrypt: this.broker.nip04Decrypt,
}
this.nip44 = {
encrypt: this.broker.nip44Encrypt,
decrypt: this.broker.nip44Decrypt,
}
}
getPubkey = async () => {
if (!this.pubkey) {
@@ -408,14 +446,4 @@ export class Nip46Signer implements ISigner {
sign = async (template: StampedEvent) =>
this.broker.signEvent(hash(own(template, await this.getPubkey())))
nip04 = {
encrypt: this.broker.nip04Encrypt,
decrypt: this.broker.nip04Decrypt,
}
nip44 = {
encrypt: this.broker.nip44Encrypt,
decrypt: this.broker.nip44Decrypt,
}
}
+9 -8
View File
@@ -1,7 +1,7 @@
import {SignedEvent, StampedEvent} from "@welshman/util"
import {hash, own, ISigner} from "../util"
import {NostrSignerPlugin, AppInfo} from "nostr-signer-capacitor-plugin"
import {nip19} from "nostr-tools"
import {decode} from "nostr-tools/nip19"
import {SignedEvent, StampedEvent} from "@welshman/util"
import {hash, own, ISigner} from "../util.js"
export const getNip55 = async (): Promise<AppInfo[]> => {
const {apps} = await NostrSignerPlugin.getInstalledSignerApps()
@@ -21,10 +21,11 @@ export class Nip55Signer implements ISigner {
this.#initialize()
}
async #initialize() {
#initialize() {
if (!this.#packageNameSet) {
await this.#plugin.setPackageName({packageName: this.#packageName})
this.#packageNameSet = true
void this.#plugin.setPackageName({packageName: this.#packageName}).then(() => {
this.#packageNameSet = true
})
}
}
@@ -53,10 +54,10 @@ export class Nip55Signer implements ISigner {
try {
const {npub} = await signer.getPublicKey()
this.#npub = npub
const {data} = nip19.decode(npub)
const {data} = decode(npub)
this.#publicKey = data as string
} catch (error) {
throw new Error(`Failed to get public key`)
throw new Error("Failed to get public key")
}
}
return this.#publicKey
+14 -9
View File
@@ -1,8 +1,10 @@
import {schnorr} from '@noble/curves/secp256k1'
import {bytesToHex, hexToBytes} from '@noble/hashes/utils'
import {nip04 as nt04, nip44 as nt44, generateSecretKey, getPublicKey, getEventHash} from "nostr-tools"
import {cached, now} from '@welshman/lib'
import {SignedEvent, HashedEvent, EventTemplate, StampedEvent, OwnedEvent} from '@welshman/util'
import {schnorr} from "@noble/curves/secp256k1"
import {bytesToHex, hexToBytes} from "@noble/hashes/utils"
import * as nt04 from "nostr-tools/nip04"
import * as nt44 from "nostr-tools/nip44"
import {generateSecretKey, getPublicKey, getEventHash} from "nostr-tools/pure"
import {cached, now} from "@welshman/lib"
import {SignedEvent, HashedEvent, EventTemplate, StampedEvent, OwnedEvent} from "@welshman/util"
export const makeSecret = () => bytesToHex(generateSecretKey())
@@ -24,17 +26,20 @@ export const sign = (event: HashedEvent, secret: string) => ({...event, sig: get
export const nip04 = {
detect: (m: string) => m.includes("?iv="),
encrypt: (pubkey: string, secret: string, m: string) => nt04.encrypt(secret, pubkey, m),
decrypt: (pubkey: string, secret: string, m: string) => nt04.decrypt(secret, pubkey, m),
decrypt: (pubkey: string, secret: string, m: string) => nt04.decrypt(secret, pubkey, m),
}
export const nip44 = {
getSharedSecret: cached({
maxSize: 10000,
getKey: ([secret, pubkey]) => [secret, pubkey].join(":"),
getValue: ([secret, pubkey]: string[]) => nt44.v2.utils.getConversationKey(hexToBytes(secret), pubkey),
getValue: ([secret, pubkey]: string[]) =>
nt44.v2.utils.getConversationKey(hexToBytes(secret), pubkey),
}),
encrypt: (pubkey: string, secret: string, m: string) => nt44.v2.encrypt(m, nip44.getSharedSecret(secret, pubkey)!),
decrypt: (pubkey: string, secret: string, m: string) => nt44.v2.decrypt(m, nip44.getSharedSecret(secret, pubkey)!),
encrypt: (pubkey: string, secret: string, m: string) =>
nt44.v2.encrypt(m, nip44.getSharedSecret(secret, pubkey)!),
decrypt: (pubkey: string, secret: string, m: string) =>
nt44.v2.decrypt(m, nip44.getSharedSecret(secret, pubkey)!),
}
export type Sign = (event: StampedEvent) => Promise<SignedEvent>