49 lines
1.0 KiB
JavaScript
49 lines
1.0 KiB
JavaScript
window.nostr = {
|
|
_requests: {},
|
|
_pubkey: null,
|
|
|
|
async getPublicKey() {
|
|
if (this._pubkey) return this._pubkey
|
|
this._pubkey = await this._call('getPublicKey', {})
|
|
return this._pubkey
|
|
},
|
|
|
|
async signEvent(event) {
|
|
return this._call('signEvent', {event})
|
|
},
|
|
|
|
_call(type, params) {
|
|
return new Promise((resolve, reject) => {
|
|
let id = Math.random().toString().slice(4)
|
|
this._requests[id] = {resolve, reject}
|
|
window.postMessage(
|
|
{
|
|
id,
|
|
ext: 'nos2x',
|
|
type,
|
|
params
|
|
},
|
|
'*'
|
|
)
|
|
})
|
|
}
|
|
}
|
|
|
|
window.addEventListener('message', message => {
|
|
if (
|
|
!message.data ||
|
|
!message.data.response ||
|
|
message.data.ext !== 'nos2x' ||
|
|
!window.nostr._requests[message.data.id]
|
|
)
|
|
return
|
|
|
|
if (message.data.response.error) {
|
|
window.nostr._requests[message.data.id].reject(
|
|
new Error(`nos2x: ${message.data.response.error}`)
|
|
)
|
|
} else {
|
|
window.nostr._requests[message.data.id].resolve(message.data.response)
|
|
}
|
|
})
|