Provider API (Fractal Bitcoin)#
什么是 Injected provider API (Fractal Bitcoin) ?#
OKX Injected Providers API (Fractal Bitcoin) 基于 JavaScript 模型,由 OKX 嵌入用户访问网站中。
DApp 项目可以通过调用此 API 请求用户账户信息,从用户所连接的区块链中读取数据,并协助用户进行消息和交易的签署。
connect#
描述
连接钱包
okxwallet.fractalBitcoin.connect()
参数
无
返回值
- Promise - object
- address - string:当前账户的地址
- publicKey - string:当前账户的公钥
示例
const result = await okxwallet.fractalBitcoin.connect()
// example
{
address: 'bc1pwqye6x35g2n6xpwalywhpsvsu39k3l6086cvdgqazlw9mz2meansz9knaq',
publicKey: '4a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497',
compressedPublicKey:'034a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497',
}
requestAccounts#
okxwallet.fractalBitcoin.requestAccounts()
描述
请求连接当前账户
参数
无
返回值
- Promise - string[]:当前账户的地址
示例
try {
let accounts = await okxwallet.fractalBitcoin.requestAccounts();
console.log('connect success', accounts);
} catch (e) {
console.log('connect failed');
}
// example
['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'];
getAccounts#
okxwallet.fractalBitcoin.getAccounts()
描述
获取当前账户地址
参数
无
返回值
- Promise - string[]:当前账户地址
示例
try {
let res = await okxwallet.fractalBitcoin.getAccounts();
console.log(res);
} catch (e) {
console.log(e);
}
// example
['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'];
getPublicKey#
okxwallet.fractalBitcoin.getPublicKey()
描述
获取当前账户的公钥
参数
无
返回值
- Promise - string:公钥
示例
try {
let res = await okxwallet.fractalBitcoin.getPublicKey();
console.log(res)
} catch (e) {
console.log(e);
}
// example
03cbaedc26f03fd3ba02fc936f338e980c9e2172c5e23128877ed46827e935296f
getBalance#
okxwallet.fractalBitcoin.getBalance()
描述
获取 BTC 余额
参数
无
返回值
- Promise - object:
- confirmed - number:已确认的聪数量
- unconfirmed - number:未经确认的聪数量
- total - number:总聪量
示例
try {
let res = await okxwallet.fractalBitcoin.getBalance();
console.log(res)
} catch (e) {
console.log(e);
}
// example
{
"confirmed":0,
"unconfirmed":100000,
"total":100000
}
signMessage#
okxwallet.fractalBitcoin.signMessage(signStr[, type])
描述
签名消息
参数
- signStr - string:需要签名的数据
- type - string: (可选) "ecdsa" | "bip322-simple",默认值是 "ecdsa"。(请注意:版本低于 6.51.0 的应用仅支持“ecdsa”签名算法,而版本为 6.51.0 或更高的应用可支持所有签名算法类型。)
返回值
- Promise - string:签名结果
示例
const signStr = 'need sign string';
const result = await window.okxwallet.fractalBitcoin.signMessage(signStr, 'ecdsa')
// example
INg2ZeG8b6GsiYLiWeQQpvmfFHqCt3zC6ocdlN9ZRQLhSFZdGhgYWF8ipar1wqJtYufxzSYiZm5kdlAcnxgZWQU=
signPsbt#
okxwallet.fractalBitcoin.signPsbt(psbtHex[, options])
描述
签名 psbt,该方法将遍历所有与当前地址匹配的输入进行签名
参数
- psbtHex - string:要签名的 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 来生成签名,启用此选项允许使用原始私钥进行签名
返回值
- Promise - string:已签名 psbt 的十六进制字符串
示例
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,});
signPsbts#
okxwallet.fractalBitcoin.signPsbts(psbtHexs[, options])
描述
签署多个 psbt,该方法将遍历所有与当前地址匹配的输入进行签名
参数
- psbtHexs - string[]:要签名的 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
返回值
- Promise - string[]:已签名 psbt 的十六进制字符串
示例
try {
let res = await okxwallet.fractalBitcoin.signPsbts([
'70736274ff01007d...',
'70736274ff01007d...',
]);
console.log(res);
} catch (e) {
console.log(e);
}
pushPsbt#
okxwallet.fractalBitcoin.pushPsbt(psbtHex)
描述
广播 psbt 交易
参数
- psbtHex - string:要推送的 psbt 的十六进制字符串
返回值
- Promise - string:交易哈希
示例
try {
let res = await okxwallet.fractalBitcoin.pushPsbt('70736274ff01007d....');
console.log(res);
} catch (e) {
console.log(e);
}
pushTx#
okxwallet.fractalBitcoin.pushTx(rawTx)
描述
推送交易
参数
- rawTx - string:上链的原始交易
返回值
- Promise - string:交易哈希
示例
try {
let txid = await okxwallet.fractalBitcoin.pushTx('0200000000010135bd7d...');
console.log(txid);
} catch (e) {
console.log(e);
}