The OKX injected provider API is a JavaScript API that OKX injects into websites visited by our users. Your DApp can use this API to request users' accounts, read data from blockchains users are connected to, and help users sign messages and transactions.
Dapps can access the injected object through the following methods:
window.okxwallet.nostr
try {
const publicKey = await window.okxwallet.nostr.getPublicKey();
} catch (error) {
console.log(error);
}
window.okxwallet.nostr.getPublicKey(): Promise<string>
Description
Returns the public key of the currently connected account.
Return value
publicKey
- string: the public key of the currently connected account.try {
const publicKey = await window.okxwallet.nostr.getPublicKey();
} catch (error) {
console.log(error);
}
window.okxwallet.nostr.signEvent(event: Event): Promise<SignedEvent>
Description
Signing the Event.
Parameters
event
- object
created_at
- number: event creation timekind
- number: event typetags
- string[][]: event tagscontent
- string: event contentReturn value
event
- SignedEvent, In addition to all the properties that include the event parameter, it also includes the following properties:
id
- string: idpubkey
- string: the public keysig
- string: the signatureconst event = {
content: "hello",
kind: 4,
"tags": [
[
"p",
"693d3f45b81c1f3557383fb955f3a8cb2c194c44ffba1e2f4566e678773b44f8"
],
[
"r",
"json"
],
[
"a",
"b4f4e689fca78ebcaeec72162628ba61c51a62e1420b9b8ca8cb63d9a7e26219"
]
],
"created_at": 1700726837,
}
const signedEvent = await window.okxwallet.nostr.signEvent(event)
console.log(signedEvent.id)
console.log(signedEvent.pubkey)
console.log(signedEvent.sig)
window.okxwallet.nostr.nip04.encrypt(pubkey: string, message: string): Promise<string>
Description
Encrypt the message according to the NIP-04 specification.
Return value
encryptMsg
- string: the encrypting resultconst pubkey = '693d3f45b81c1f3557383fb955f3a8cb2c194c44ffba1e2f4566e678773b44f8'
const msg = 'hello world'
const encryptMsg = await window.okxwallet.nostr.nip04.encrypt(pubkey, msg);
console.log(encryptMsg)
window.okxwallet.nostr.nip04.decrypt(pubkey: string, message: string): Promise<string>
Description
Decrypt the message according to the NIP-04 specification.
Return value
decryptMsg
- string: the decrypting resultconst pubkey = '693d3f45b81c1f3557383fb955f3a8cb2c194c44ffba1e2f4566e678773b44f8'
const msg = 'VVPplRPF0w4dNZkuiQ==?iv=Nrb7gcph/9eKuqyuDx0yKQ=='
const decryptMsg = await window.okxwallet.nostr.nip04.decrypt(pubkey, msg);
console.log(decryptMsg)
window.okxwallet.nostr.on(event:string, callback: Function): Promise<void>
window.okxwallet.nostr.off(event:string, callback: Function): Promise<void>
Description
Add event listener, currently supported events are:
accountChanged
: this event is triggered when the user switches accounts.window.okxwallet.nostr.on('accountChanged', async () => {
const publicKey = await window.okxwallet.nostr.getPublicKey();
console.log(publicKey)
})