OKX Injected Providers API (Fractal Bitcoin) 基于 JavaScript 模型,由 OKX 嵌入用户访问网站中。
DApp 项目可以通过调用此 API 请求用户账户信息,从用户所连接的区块链中读取数据,并协助用户进行消息和交易的签署。
描述
连接钱包
okxwallet.fractalBitcoin.connect()
参数
无
返回值
示例
const result = await okxwallet.fractalBitcoin.connect()
// example
{
address: 'bc1pwqye6x35g2n6xpwalywhpsvsu39k3l6086cvdgqazlw9mz2meansz9knaq',
publicKey: '4a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497',
compressedPublicKey:'034a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497',
}
okxwallet.fractalBitcoin.requestAccounts()
描述
请求连接当前账户
参数
无
返回值
示例
try {
let accounts = await okxwallet.fractalBitcoin.requestAccounts();
console.log('connect success', accounts);
} catch (e) {
console.log('connect failed');
}
// example
['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'];
okxwallet.fractalBitcoin.getAccounts()
描述
获取当前账户地址
参数
无
返回值
示例
try {
let res = await okxwallet.fractalBitcoin.getAccounts();
console.log(res);
} catch (e) {
console.log(e);
}
// example
['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'];
okxwallet.fractalBitcoin.getPublicKey()
描述
获取当前账户的公钥
参数
无
返回值
示例
try {
let res = await okxwallet.fractalBitcoin.getPublicKey();
console.log(res)
} catch (e) {
console.log(e);
}
// example
03cbaedc26f03fd3ba02fc936f338e980c9e2172c5e23128877ed46827e935296f
okxwallet.fractalBitcoin.getBalance()
描述
获取 BTC 余额
参数
无
返回值
示例
try {
let res = await okxwallet.fractalBitcoin.getBalance();
console.log(res)
} catch (e) {
console.log(e);
}
// example
{
"confirmed":0,
"unconfirmed":100000,
"total":100000
}
okxwallet.fractalBitcoin.signMessage(signStr[, type])
描述
签名消息
参数
返回值
示例
const signStr = 'need sign string';
const result = await window.okxwallet.fractalBitcoin.signMessage(signStr, 'ecdsa')
// example
INg2ZeG8b6GsiYLiWeQQpvmfFHqCt3zC6ocdlN9ZRQLhSFZdGhgYWF8ipar1wqJtYufxzSYiZm5kdlAcnxgZWQU=
okxwallet.fractalBitcoin.signPsbt(psbtHex[, options])
描述
签名 psbt,该方法将遍历所有与当前地址匹配的输入进行签名
参数
示例:可参考下面的 txInput 和 publicKey
const txInputs: utxoInput[] = [];
txInputs.push({
txId: "1e0f92720ef34ab75eefc5d691b551fb2f783eac61503a69cdf63eb7305d2306",
vOut: 2,
amount: 341474,
address: "tb1q8h8....mjxzny",
privateKey: "0s79......ldjejke",
publicKey: "tb1q8h8....mjxzny",
bip32Derivation: [{"masterFingerprint": "a22e8e32","pubkey": "tb1q8h8....mjxzny","path": "m/49'/0'/0'/0/0",},],});
- options
- autoFinalized - boolean:签名后是否完成 psbt,默认为 true
- toSignInputs - array:
- index - number:要签名的输入
- address - string:用于签名的相应私钥所对应的地址
- publicKey - string:用于签名的相应私钥所对应的公钥
- sighashTypes - number[]: (可选) sighashTypes
- disableTweakSigner - boolean: (可选) 签名和解锁 Taproot 地址时, 默认使用 tweakSigner 来生成签名,启用此选项允许使用原始私钥进行签名
返回值
示例
try {let res = await okxwallet.fractalBitcoin.signPsbt('70736274ff01007d....', {
autoFinalized: false,
toSignInputs: [{
index: 0,
address: 'tb1q8h8....mjxzny',},{
index: 1,
publicKey: 'tb1q8h8....mjxzny',
sighashTypes: [1],},{
index: 2,
publicKey: '02062...8779693f',},],});console.log(res);} catch (e) {console.log(e);}
okxwallet.fractalBitcoin.signPsbt('xxxxxxxx', {
toSignInputs: [{ index: 0, publicKey: 'xxxxxx', disableTweakSigner: true }],
autoFinalized: false,});
okxwallet.fractalBitcoin.signPsbts(psbtHexs[, options])
描述
签署多个 psbt,该方法将遍历所有与当前地址匹配的输入进行签名
参数
示例:可参考下面的 txInput 和 publicKey
const txInputs: utxoInput[] = [];
txInputs.push({
txId: "1e0f92720ef34ab75eefc5d691b551fb2f783eac61503a69cdf63eb7305d2306",
vOut: 2,
amount: 341474,
address: "tb1q8h8....mjxzny",
privateKey: "0s79......ldjejke",
publicKey: "tb1q8h8....mjxzny",
bip32Derivation: [{"masterFingerprint": "a22e8e32","pubkey": "tb1q8h8....mjxzny","path": "m/49'/0'/0'/0/0",},],});
- options - object[]:签署 psbt 的选项
- autoFinalized - boolean:签名后是否完成 psbt,默认为 true
- toSignInputs - array:
- index - number:要签名的输入
- address - string:用于签名的相应私钥所对应的地址
- publicKey - string:用于签名的相应私钥所对应的公钥
- sighashTypes - number[]: (可选) sighashTypes
返回值
示例
try {
let res = await okxwallet.fractalBitcoin.signPsbts([
'70736274ff01007d...',
'70736274ff01007d...',
]);
console.log(res);
} catch (e) {
console.log(e);
}
okxwallet.fractalBitcoin.pushPsbt(psbtHex)
描述
广播 psbt 交易
参数
返回值
示例
try {
let res = await okxwallet.fractalBitcoin.pushPsbt('70736274ff01007d....');
console.log(res);
} catch (e) {
console.log(e);
}
okxwallet.fractalBitcoin.pushTx(rawTx)
描述
推送交易
参数
返回值
示例
try {
let txid = await okxwallet.fractalBitcoin.pushTx('0200000000010135bd7d...');
console.log(txid);
} catch (e) {
console.log(e);
}