use a public BunkerPointer property on BunkerSigner class.

This commit is contained in:
fiatjaf
2024-02-14 12:29:47 -03:00
parent ce059d4608
commit 3aab7121f7

View File

@@ -74,7 +74,6 @@ export type BunkerSignerParams = {
export class BunkerSigner { export class BunkerSigner {
private pool: AbstractSimplePool private pool: AbstractSimplePool
private subCloser: SubCloser private subCloser: SubCloser
private relays: string[]
private isOpen: boolean private isOpen: boolean
private serial: number private serial: number
private idPrefix: string private idPrefix: string
@@ -85,8 +84,7 @@ export class BunkerSigner {
} }
} }
private secretKey: Uint8Array private secretKey: Uint8Array
private connectionSecret: string public bp: BunkerPointer
public remotePubkey: string
/** /**
* Creates a new instance of the Nip46 class. * Creates a new instance of the Nip46 class.
@@ -101,9 +99,7 @@ export class BunkerSigner {
this.pool = params.pool || new SimplePool() this.pool = params.pool || new SimplePool()
this.secretKey = clientSecretKey this.secretKey = clientSecretKey
this.relays = bp.relays this.bp = bp
this.remotePubkey = bp.pubkey
this.connectionSecret = bp.secret || ''
this.isOpen = false this.isOpen = false
this.idPrefix = Math.random().toString(36).substring(7) this.idPrefix = Math.random().toString(36).substring(7)
this.serial = 0 this.serial = 0
@@ -112,7 +108,7 @@ export class BunkerSigner {
const listeners = this.listeners const listeners = this.listeners
this.subCloser = this.pool.subscribeMany( this.subCloser = this.pool.subscribeMany(
this.relays, this.bp.relays,
[{ kinds: [NostrConnect, NostrConnectAdmin], '#p': [getPublicKey(this.secretKey)] }], [{ kinds: [NostrConnect, NostrConnectAdmin], '#p': [getPublicKey(this.secretKey)] }],
{ {
async onevent(event: NostrEvent) { async onevent(event: NostrEvent) {
@@ -154,17 +150,13 @@ export class BunkerSigner {
this.serial++ this.serial++
const id = `${this.idPrefix}-${this.serial}` const id = `${this.idPrefix}-${this.serial}`
const encryptedContent = await encrypt( const encryptedContent = await encrypt(this.secretKey, this.bp.pubkey, JSON.stringify({ id, method, params }))
this.secretKey,
this.remotePubkey,
JSON.stringify({ id, method, params }),
)
// the request event // the request event
const verifiedEvent: VerifiedEvent = finalizeEvent( const verifiedEvent: VerifiedEvent = finalizeEvent(
{ {
kind: method === 'create_account' ? NostrConnectAdmin : NostrConnect, kind: method === 'create_account' ? NostrConnectAdmin : NostrConnect,
tags: [['p', this.remotePubkey]], tags: [['p', this.bp.pubkey]],
content: encryptedContent, content: encryptedContent,
created_at: Math.floor(Date.now() / 1000), created_at: Math.floor(Date.now() / 1000),
}, },
@@ -175,7 +167,7 @@ export class BunkerSigner {
this.listeners[id] = { resolve, reject } this.listeners[id] = { resolve, reject }
// publish the event // publish the event
await Promise.any(this.pool.publish(this.relays, verifiedEvent)) await Promise.any(this.pool.publish(this.bp.relays, verifiedEvent))
} catch (err) { } catch (err) {
reject(err) reject(err)
} }
@@ -195,7 +187,7 @@ export class BunkerSigner {
* Calls the "connect" method on the bunker. * Calls the "connect" method on the bunker.
*/ */
async connect(): Promise<void> { async connect(): Promise<void> {
await this.sendRequest('connect', [getPublicKey(this.secretKey), this.connectionSecret]) await this.sendRequest('connect', [getPublicKey(this.secretKey), this.bp.secret || ''])
} }
/** /**
@@ -203,7 +195,7 @@ export class BunkerSigner {
* but instead we just returns the public key we already know. * but instead we just returns the public key we already know.
*/ */
async getPublicKey(): Promise<string> { async getPublicKey(): Promise<string> {
return this.remotePubkey return this.bp.pubkey
} }
/** /**
@@ -221,7 +213,7 @@ export class BunkerSigner {
async signEvent(event: UnsignedEvent): Promise<VerifiedEvent> { async signEvent(event: UnsignedEvent): Promise<VerifiedEvent> {
let resp = await this.sendRequest('sign_event', [JSON.stringify(event)]) let resp = await this.sendRequest('sign_event', [JSON.stringify(event)])
let signed: NostrEvent = JSON.parse(resp) let signed: NostrEvent = JSON.parse(resp)
if (signed.pubkey === this.remotePubkey && verifyEvent(signed)) { if (signed.pubkey === this.bp.pubkey && verifyEvent(signed)) {
return signed return signed
} else { } else {
throw new Error(`event returned from bunker is improperly signed: ${JSON.stringify(signed)}`) throw new Error(`event returned from bunker is improperly signed: ${JSON.stringify(signed)}`)
@@ -275,7 +267,7 @@ export async function createAccount(
// once we get the newly created pubkey back, we hijack this signer instance // once we get the newly created pubkey back, we hijack this signer instance
// and turn it into the main instance for this newly created pubkey // and turn it into the main instance for this newly created pubkey
rpc.remotePubkey = pubkey rpc.bp.pubkey = pubkey
await rpc.connect() await rpc.connect()
return rpc return rpc