Skip to content

BTC

在 Bitget Wallet App 及安装 Chrome Extension 的 Chrome 浏览器中运行 DApp 时,可获得全局对象 window.bitkeep.unisat 并进行后续的 API 调用。

js
const provider = window.bitkeep.unisat
const provider = window.bitkeep.unisat

Bitget Wallet 钱包仅在以下版本支持此功能

PlatformVersionDescription
Chrome Extension>=v2.2.2mainnet
App(IOS)>= 7.3.9mainnet
App(Android)>= 7.3.9mainnet

Note

部分方法可能会有异常,将在新版本中支持

PlatformVersionfunction
Chrome Extension>=v2.2.0getBalance, getInscriptions, pushTx
App(IOS)>= 8.8.0getBalance, getInscriptions, pushTx
App(Android)>= 8.8.0getBalance, getInscriptions, pushTx

请求链接到钱包

返回值:

  • Promise returns string[]:当前账户地址
js
try {
  let accounts = await provider.requestAccounts();
  console.log('connect success', accounts);
} catch (e) {
  console.log('connect failed');
}
> connect success ['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz']
try {
  let accounts = await provider.requestAccounts();
  console.log('connect success', accounts);
} catch (e) {
  console.log('connect failed');
}
> connect success ['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz']

获取当前账户地址

返回值:

  • Promise - string: 当前账户地址
js
try {
  let res = await provider.getAccounts();
  console.log(res)
} catch (e) {
  console.log(e);
}
> ["tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz"]
try {
  let res = await provider.getAccounts();
  console.log(res)
} catch (e) {
  console.log(e);
}
> ["tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz"]

获取当前网络

返回值:

  • Promise - string:网络 livenet | testnet | signet
js
try {
  let res = await provider.getNetwork();
  console.log(res)
} catch (e) {
  console.log(e);
}
> 'livenet'
try {
  let res = await provider.getNetwork();
  console.log(res)
} catch (e) {
  console.log(e);
}
> 'livenet'

切换网络, 请求钱包切换到指定网络

参数:

  • network - string:网络 livenet | testnet | signet

返回值:

  • 无数据
js
try {
  let res = await provider.switchNetwork("livenet");
  console.log(res)
} catch (e) {
  console.log(e);
}

> 'livenet'
try {
  let res = await provider.switchNetwork("livenet");
  console.log(res)
} catch (e) {
  console.log(e);
}

> 'livenet'

获取当前用户的公钥

返回:

  • Promise - string: 公钥
js
try {
  let res = await provider.getPublicKey();
  console.log(res)
} catch (e) {
  console.log(e);
}
> "03cbaedc26f03fd3ba02fc936f338e980c9e2172c5e23128877ed46827e935296f"
try {
  let res = await provider.getPublicKey();
  console.log(res)
} catch (e) {
  console.log(e);
}
> "03cbaedc26f03fd3ba02fc936f338e980c9e2172c5e23128877ed46827e935296f"

获取账户余额

返回:

  • Promise - Object
    • confirmed - number : 已确认的聪
    • unconfirmed - numbet: 还未确认的聪
    • total - number: 总的聪
js
try {
  let res = await provider.getBalance();
  console.log(res)
} catch (e) {
  console.log(e);
}

> {
    "confirmed":0,
    "unconfirmed":100000,
    "total":100000
  }
try {
  let res = await provider.getBalance();
  console.log(res)
} catch (e) {
  console.log(e);
}

> {
    "confirmed":0,
    "unconfirmed":100000,
    "total":100000
  }

获取当前账户的铭文

返回:

  • Promise - Object
    • total - number : 总数
    • list - Object[] :
    • inscriptionId - string : 铭文 id
    • inscriptionNumber - string : 铭文 数量
    • address - string : 铭文地址
    • outputValue - string : 铭文输出值
    • content - string : 铭文内容url
    • contentLength - string : 铭文内容长度
    • contentType - number : 铭文内容 类型
    • preview - number : 预览链接
    • timestamp - number : 铭文出块时间
    • offset - number : 铭文偏移量
    • genesisTransaction - string : 创世交易的id
    • location - string : 当前位置
js
try {
  let res = await provider.getInscriptions(0,10);
  console.log(res)
} catch (e) {
  console.log(e);
}

> {
  "total":10,
  "list":[
    {
      inscriptionId: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531i0',
      inscriptionNumber: 959941,
      address: 'bc1q8h8s4zd9y0lkrx334aqnj4ykqs220ss735a3gh',
      outputValue: 546,
      preview: 'https://ordinals.com/preview/6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531i0',
      content: 'https://ordinals.com/content/6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531i0',
      contentLength: 53,
      contentType: 'text/plain;charset=utf-8',
      timestamp: 1680865285,
      genesisTransaction: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531',
      location: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531:0:0',
      output: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531:0',
      offset: 0
    }
  ]
}
try {
  let res = await provider.getInscriptions(0,10);
  console.log(res)
} catch (e) {
  console.log(e);
}

> {
  "total":10,
  "list":[
    {
      inscriptionId: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531i0',
      inscriptionNumber: 959941,
      address: 'bc1q8h8s4zd9y0lkrx334aqnj4ykqs220ss735a3gh',
      outputValue: 546,
      preview: 'https://ordinals.com/preview/6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531i0',
      content: 'https://ordinals.com/content/6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531i0',
      contentLength: 53,
      contentType: 'text/plain;charset=utf-8',
      timestamp: 1680865285,
      genesisTransaction: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531',
      location: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531:0:0',
      output: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531:0',
      offset: 0
    }
  ]
}

发送 Bitcoin

参数

  • toAddress - to 地址
  • satoshis - 发送的聪数量
  • options - object: (可选)
    • feeRate - number: :网络资费率

返回

  • Promise - string: 交易 ID
js
try {
  let txid = await provider.sendBitcoin("tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz",1000);
  console.log(txid)
} catch (e) {
  console.log(e);
}
try {
  let txid = await provider.sendBitcoin("tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz",1000);
  console.log(txid)
} catch (e) {
  console.log(e);
}

发送铭文

参数

  • address - string: 接受者地址
  • inscriptionId - 铭文 id
  • options - Object: (可选)
  • feeRate - number:网络资费率

返回

  • Promise - Object:
    • txid - string : 交易ID
js
try {
  let { txid } = await provider.sendInscription("tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny","e9b86a063d78cc8a1ed17d291703bcc95bcd521e087ab0c7f1621c9c607def1ai0",{feeRate:15});
  console.log("send Inscription 204 to tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny",{txid})
} catch (e) {
  console.log(e);
}
try {
  let { txid } = await provider.sendInscription("tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny","e9b86a063d78cc8a1ed17d291703bcc95bcd521e087ab0c7f1621c9c607def1ai0",{feeRate:15});
  console.log("send Inscription 204 to tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny",{txid})
} catch (e) {
  console.log(e);
}

消息签名

签署消息

参数

  • msg - string: 要签名的字符串
  • type - string: (可选) "ecdsa" | "bip322-simple". default is "ecdsa": 要签名的可选参数

返回

  • Promise - string: 签名结果
js
// sign by ecdsa
try {
  let res = await provider.signMessage("abcdefghijk123456789");
  console.log(res)
} catch (e) {
  console.log(e);
}

> G+LrYa7T5dUMDgQduAErw+i6ebK4GqTXYVWIDM+snYk7Yc6LdPitmaqM6j+iJOeID1CsMXOJFpVopvPiHBdulkE=

// verify by ecdsa
import { verifyMessage } from "@unisat/wallet-utils";
const pubkey = "026887958bcc4cb6f8c04ea49260f0d10e312c41baf485252953b14724db552aac";
const message = "abcdefghijk123456789";
const signature = "G+LrYa7T5dUMDgQduAErw+i6ebK4GqTXYVWIDM+snYk7Yc6LdPitmaqM6j+iJOeID1CsMXOJFpVopvPiHBdulkE=";
const result = verifyMessage(pubkey,message,signature);
console.log(result);

> true

// sign by bip322-simple
try {
  let res = await window.unisat.signMessage("abcdefghijk123456789","bip322-simple");
  console.log(res)
} catch (e) {
  console.log(e);
}

> AkcwRAIgeHUcjr0jODaR7GMM8cenWnIj0MYdGmmrpGyMoryNSkgCICzVXWrLIKKp5cFtaCTErY7FGNXTFe6kuEofl4G+Vi5wASECaIeVi8xMtvjATqSSYPDRDjEsQbr0hSUpU7FHJNtVKqw=
// sign by ecdsa
try {
  let res = await provider.signMessage("abcdefghijk123456789");
  console.log(res)
} catch (e) {
  console.log(e);
}

> G+LrYa7T5dUMDgQduAErw+i6ebK4GqTXYVWIDM+snYk7Yc6LdPitmaqM6j+iJOeID1CsMXOJFpVopvPiHBdulkE=

// verify by ecdsa
import { verifyMessage } from "@unisat/wallet-utils";
const pubkey = "026887958bcc4cb6f8c04ea49260f0d10e312c41baf485252953b14724db552aac";
const message = "abcdefghijk123456789";
const signature = "G+LrYa7T5dUMDgQduAErw+i6ebK4GqTXYVWIDM+snYk7Yc6LdPitmaqM6j+iJOeID1CsMXOJFpVopvPiHBdulkE=";
const result = verifyMessage(pubkey,message,signature);
console.log(result);

> true

// sign by bip322-simple
try {
  let res = await window.unisat.signMessage("abcdefghijk123456789","bip322-simple");
  console.log(res)
} catch (e) {
  console.log(e);
}

> AkcwRAIgeHUcjr0jODaR7GMM8cenWnIj0MYdGmmrpGyMoryNSkgCICzVXWrLIKKp5cFtaCTErY7FGNXTFe6kuEofl4G+Vi5wASECaIeVi8xMtvjATqSSYPDRDjEsQbr0hSUpU7FHJNtVKqw=

广播交易

参数:

  • options - Object:
    • rawtx - string: tawtx 广播

返回

  • Promise - string: 交易ID
js
try {
  let txid = await provider.pushTx({
    rawtx:"0200000000010135bd7d..."
  });
  console.log(txid)
} catch (e) {
  console.log(e);
}
try {
  let txid = await provider.pushTx({
    rawtx:"0200000000010135bd7d..."
  });
  console.log(txid)
} catch (e) {
  console.log(e);
}

签署PSBT

该方法将遍历所有与当前地址匹配的输入进行签名。

参数

  • psbtHex - string: 要签名的psbt的十六进制字符串
  • options
    • autoFinalized - boolean: 签名后是否完成psbt,默认为true
    • toSignInputs - array:
      • index - number: 要签署哪个输入
      • address - string:(至少指定地址或公钥)用于签名的相应私钥
      • publicKey - string:(至少指定地址或公钥)用于签名的相应私钥
      • sighashTypes - number[]:(可选)sighashTypes
      • disableTweakSigner - boolean:(可选)在签名和解锁 Taproot 地址时,默认情况下使用tweakSigner 来生成签名。启用此选项允许使用原始私钥进行签名。

返回

  • Promise - string: 签名的psbt的十六进制字符串
js
try {
  let res = await provider.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);
}
try {
  let res = await provider.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);
}

一次签署多个 PSBT

该方法将遍历所有与当前地址匹配的输入进行签名。

参数

  • psbtHexs - string[]: 要签名的 psbt 的十六进制字符串
  • options - object[]: 签署 psbt 的选项
    • autoFinalized - boolean: 签名后是否完成psbt,默认为true
    • toSignInputs - array:
      • index - number: 要签署哪个输入
      • address - string:(至少指定地址或公钥)用于签名的相应私钥
      • publicKey - string:(至少指定地址或公钥)用于签名的相应私钥
      • sighashTypes - number[]:(可选)sighashTypes
      • disableTweakSigner - boolean:(可选)在签名和解锁 Taproot 地址时,默认情况下使用tweakSigner 来生成签名。启用此选项允许使用原始私钥进行签名。

返回

  • Promise - string[]: 签名的psbt的十六进制字符串
js
try {
  let res = await provider.signPsbts(["70736274ff01007d...","70736274ff01007d..."]);
  console.log(res)
} catch (e) {
  console.log(e);
}
try {
  let res = await provider.signPsbts(["70736274ff01007d...","70736274ff01007d..."]);
  console.log(res)
} catch (e) {
  console.log(e);
}

广播PSBT

参数:

  • psbtHex - string: 要广播的 psbt 的十六进制字符串

返回

  • Promise - string: 交易ID
js
try {
  let res = await provider.pushPsbt("70736274ff01007d....");
  console.log(res)
} catch (e) {
  console.log(e);
}
try {
  let res = await provider.pushPsbt("70736274ff01007d....");
  console.log(res)
} catch (e) {
  console.log(e);
}