basic functionality, untested.
This commit is contained in:
@@ -1,4 +1,45 @@
|
||||
window.nostr = {
|
||||
getPublicKey() {},
|
||||
signEvent(event) {}
|
||||
_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.ext !== 'nos2x' ||
|
||||
!window.nostr._requests[message.data.id]
|
||||
)
|
||||
return
|
||||
|
||||
if (message.data.response.error) {
|
||||
window.nostr._requests[message.data.id].reject(message.data.response.error)
|
||||
} else {
|
||||
window.nostr._requests[message.data.id].resolve(message.data.response)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user