连接插件钱包
Provider API

Provider API#

什么是 Injected provider API?#

欧易 Injected providers API 是一个 JavaScript API,欧易将其注入用户访问的网站。您的 DApp 可以使用此 API 请求用户帐户,从用户连接的区块链读取数据,帮助用户签署消息和交易。

获取注入的对象#

Dapp 可以通过如下方式访问注入的对象:

  • window.okxwallet.nostr

连接钱包的简单示例#

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

获取公钥#

window.okxwallet.nostr.getPublicKey(): Promise<string>

描述

返回当前连接的帐户的公钥。

返回值

  • publicKey - string: 当前连接的帐户的公钥。
try {
  const publicKey = await window.okxwallet.nostr.getPublicKey();
} catch (error) {
  console.log(error);
}

签名 Event#

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

描述

对 Event 进行签名。

入参

  • event - object
    • created_at - number: 事件创建时间
    • kind - number: 事件类型
    • tags - string[][]: 事件标签
    • content - string: 事件内容

返回值

  • event - SignedEvent:除了包含 event 入参的所有属性外,还包含如下属性
    • id - string: 唯一标识
    • pubkey - string: 公钥
    • sig - string: 签名
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)

对消息进行加密#

window.okxwallet.nostr.nip04.encrypt(pubkey: string, message: string): Promise<string>

描述

根据 NIP-04 规范对消息进行加密

返回值

  • encryptMsg - string: 加密的结果
const 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>

描述

根据 NIP-04 规范对消息进行解密

返回值

  • decryptMsg - string: 解密的结果
const 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>

描述

添加事件监听,目前支持的事件有:

  • accountChanged:当用户切换账户时会触发该事件
window.okxwallet.nostr.on('accountChanged', async () => {
    const publicKey = await window.okxwallet.nostr.getPublicKey();
    console.log(publicKey)
})