Connect to Extension Wallet
Provider API

Provider API#

What is injected provider API?#

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.

The injected object#

Dapps can access the injected object through the following methods:

  • window.okxwallet.nostr

Simple example of connecting to a wallet#

try {
  const publicKey = await window.okxwallet.nostr.getPublicKey();
} catch (error) {
  console.log(error);
}

Get public key#

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);
}

Sign Event#

window.okxwallet.nostr.signEvent(event: Event): Promise<SignedEvent>

Description

Signing the Event.

Parameters

  • event - object
    • created_at - number: event creation time
    • kind - number: event type
    • tags - string[][]: event tags
    • content - string: event content

Return value

  • event - SignedEvent, In addition to all the properties that include the event parameter, it also includes the following properties:
    • id - string: id
    • pubkey - string: the public key
    • sig - string: the signature
const 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)

Encrypting the message#

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 result
const pubkey = '693d3f45b81c1f3557383fb955f3a8cb2c194c44ffba1e2f4566e678773b44f8'
const msg = 'hello world'
const encryptMsg = await window.okxwallet.nostr.nip04.encrypt(pubkey, msg);
console.log(encryptMsg)

Decrypting the message#

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 result
const pubkey = '693d3f45b81c1f3557383fb955f3a8cb2c194c44ffba1e2f4566e678773b44f8'
const msg = 'VVPplRPF0w4dNZkuiQ==?iv=Nrb7gcph/9eKuqyuDx0yKQ=='
const decryptMsg = await window.okxwallet.nostr.nip04.decrypt(pubkey, msg);
console.log(decryptMsg)

Add/Remove Event Listeners#

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)
})