feat: connection initiated by the client (#364)
This commit is contained in:
parent
6bfae35f6a
commit
74986b1c6e
6 changed files with 741 additions and 2 deletions
73
src/providers/NostrProvider/nostrConnection.signer.ts
Normal file
73
src/providers/NostrProvider/nostrConnection.signer.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import { ISigner, TDraftEvent } from '@/types'
|
||||
import { bytesToHex } from '@noble/hashes/utils'
|
||||
import { BunkerSigner as NBunkerSigner } from './nip46'
|
||||
|
||||
export class NostrConnectionSigner implements ISigner {
|
||||
signer: NBunkerSigner | null = null
|
||||
private clientSecretKey: Uint8Array
|
||||
private pubkey: string | null = null
|
||||
private connectionString: string
|
||||
private bunkerString: string | null = null
|
||||
private readonly CONNECTION_TIMEOUT = 300_000 // 300 seconds
|
||||
|
||||
constructor(clientSecretKey: Uint8Array, connectionString: string) {
|
||||
this.clientSecretKey = clientSecretKey
|
||||
this.connectionString = connectionString
|
||||
}
|
||||
|
||||
async login() {
|
||||
if (this.pubkey) {
|
||||
return {
|
||||
bunkerString: this.bunkerString,
|
||||
pubkey: this.pubkey
|
||||
}
|
||||
}
|
||||
|
||||
this.signer = new NBunkerSigner(this.clientSecretKey, this.connectionString, {
|
||||
onauth: (url) => {
|
||||
window.open(url, '_blank')
|
||||
}
|
||||
})
|
||||
this.bunkerString = await this.signer.connect(this.CONNECTION_TIMEOUT)
|
||||
this.pubkey = await this.signer.getPublicKey()
|
||||
return {
|
||||
bunkerString: this.bunkerString,
|
||||
pubkey: this.pubkey
|
||||
}
|
||||
}
|
||||
|
||||
async getPublicKey() {
|
||||
if (!this.signer) {
|
||||
throw new Error('Not logged in')
|
||||
}
|
||||
if (!this.pubkey) {
|
||||
this.pubkey = await this.signer.getPublicKey()
|
||||
}
|
||||
return this.pubkey
|
||||
}
|
||||
|
||||
async signEvent(draftEvent: TDraftEvent) {
|
||||
if (!this.signer) {
|
||||
throw new Error('Not logged in')
|
||||
}
|
||||
return this.signer.signEvent(draftEvent)
|
||||
}
|
||||
|
||||
async nip04Encrypt(pubkey: string, plainText: string) {
|
||||
if (!this.signer) {
|
||||
throw new Error('Not logged in')
|
||||
}
|
||||
return await this.signer.nip04Encrypt(pubkey, plainText)
|
||||
}
|
||||
|
||||
async nip04Decrypt(pubkey: string, cipherText: string) {
|
||||
if (!this.signer) {
|
||||
throw new Error('Not logged in')
|
||||
}
|
||||
return await this.signer.nip04Decrypt(pubkey, cipherText)
|
||||
}
|
||||
|
||||
getClientSecretKey() {
|
||||
return bytesToHex(this.clientSecretKey)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue