# 钱包API

欢迎来到 Wizz Wallet 开发者文档！本文档旨在帮助你学习如何在DApp中接入 **Wizz Wallet** 。

## 入门

在接入 Wizz Wallet 前，首先需要在你的终端上安装 Wizz Wallet。[点击此处下载](https://wizzwallet.io/)

安装并运行 Wizz Wallet 后，你会发现新打开的浏览器标签页在开发者控制台中可访问 `window.wizz` 对象。

## 示例

* Github：[点击访问](https://github.com/WizzWallet/wizzwallet-provider-demo)
* 在线演示：[点击访问](https://wizzwallet-provider-demo.vercel.app/)

### **浏览器检测**

要验证浏览器是否运行了 Wizz Wallet，请将下面的代码复制粘贴到你的网页浏览器的开发者控制台中：

```jsx
if (typeof window.wizz !== 'undefined') {
    console.log('Wizz Wallet is installed!');
}
```

你可以在这里查看 `window.wizz` 对象的完整 API。

### **连接 Wizz Wallet**

“连接”或“登录”到 Wizz Wallet 实际上意味着“访问用户的比特币账户”。

你应该**只**在响应用户操作（如点击按钮）时发起连接请求。在连接请求进行中时，你应该**始终**禁用“连接”按钮。**不要**在页面加载时发起连接请求。

我们建议你添加一个按钮，允许用户将 Wizz Wallet 连接到你的 dapp。点击此按钮以调用以下方法：

```jsx
wizz.requestAccounts()
```

可以从这里获取 `Provider Api`的定义

```sh
pnpm add @wizz-btc/provider
```

## 方法

### **getVersion**

获取插件当前的版本。 (仅插件支持)

```jsx
const version = await wizz.getVersion();
console.log({ version });
```

**参数**

无

**返回**

`Promise` 返回 `string`：当前插件版本。

### **requestAccounts**

```jsx
wizz.requestAccounts()
```

连接当前账户。

**参数**

无

**返回**

`Promise` 返回 `string[]`：当前账户的地址。

**示例**

```jsx
try {
  let accounts = await window.wizz.requestAccounts();
  console.log('connect success', accounts);
} catch (e) {
  console.log('connect failed');
}
> connect success ['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz']
```

### isBiHelixAddress

查看当前钱包地址是否支持 BiHelix 网络。

```typescript
wizz.isBiHelixAddress();
```

#### 返回

**`Promise<boolean>`** - 查看当前钱包地址是否支持 BiHelix 网络。

#### 示例

```typescript
wizz.isBiHelixAddress().then((v) => {
  console.log('isBiHelixAddress:', v);
}).catch((e) => {
  console.error('error:', e);
});
```

### requestCPFP

创建 `CPFP` 加速交易请求（仅插件支持，且要求版本至少为 2.9.1）。

```typescript
wizz.requestCPFP('52c965f7...d854800');
```

#### 参数

**`txid`** - **`string`** - 要加速的交易 ID。

#### 返回

**`Promise<string>`** - 加速的交易 ID。

#### 示例

```typescript
wizz.requestCPFP('52c965f7...d854800').then((txid) => {
  console.log('CPFP txid:', txid);
}).catch((e) => {
  console.error('CPFP error:', e);
});
```

### requestMint

```jsx
wizz.requestMint()
```

发送请求以铸造各种类型的数字资产。(仅插件支持)

**参数**

* **`options`** - **`RequestMintParams`**: 铸造请求的选项，根据被铸造的资产类型而有所不同。这是一个对象，可以根据 **`type`** 字段具有不同的属性。

  * **`type`** - **`string`**: 指定铸造操作的类型。可能的值有 **`'mint_arc20'`**, **`'mint_nft'`**, **`'mint_realm'`**, **`'mint_container'`**, 和 **`'mint_dmitem'`**。

  对于 **`'mint_arc20'`** 类型:

  * **`atomicalId`** - **`string`** (可选): 要铸造的 ARC20 的原子 ID。
  * **`arc20`** - **`string`** (可选): 要铸造的ARC20的名字，和atomical id二选一

  对于 **`'mint_nft'`** 类型:

  * **`fileBytes`** - **`Uint8Array`** (可选): 文件内容的字节。
  * **`fileName`** - **`string`** (可选): 文件的名称。
  * **`contentType`** - **`string`** (可选): 文件的 MIME 类型。
  * **`bitworkc`** - **`string`** (可选): 提交交易的挖矿难度，必须是带有单个可选的 *点* 的十六进制，与 1 到 15 之间的数字分隔，不超过 10 个十六进制字符。例如：0123 或 3456.12。长度在 4-10 位数字之间。
  * **`bitworkr`** - **`string`** (可选): 揭示交易的挖矿难度，格式同上。
  * **`satsIn`** - **`number`** (可选): nft 中的 sats 数量。

  对于 **`'mint_dmitem'`** 类型:

  * **`atomicalId`** - **`string`** (可选): 要铸造的容器的 ID。
  * **`dmitem`** - **`Record<string,any>:`** dmitem 的 json 文件
  * **`satsIn`** - **`number`** (可选): dmitem 中的 sats 数量。

  对于 **`'mint_realm'`** 类型:

  * **`bitworkc`** - **`string`** (可选): 提交交易的挖矿难度，格式同上。
  * **`satsIn`** - **`number`** (可选): realm 中的 sats 数量。

  对于 **`'mint_subrealm'`** 类型:

  * **`satsIn`** - **`number`** (可选): subrealm 中的 sats 数量。
  * **`realm`** - **`string`** (可选): 父realm的名称，如果为unicode字符，需要先转为ASCII字符
  * **`subrealm`** - **`string`** (可选): 待mint的subrealm名称

  对于 **`'mint_container'`** 类型:

  * **`bitworkc`** - **`string`** (可选): 提交交易的挖矿难度，格式同上。
  * **`satsIn`** - **`number`** (可选): container 中的 sats 数量。

**返回**

* `Promise` - `void`:

**示例**

```javascript
// mint Arc20
wizz.requestMint({
  type: 'mint_arc20',
  arc20: 'atom' // 可选
}).catch(error => {
  console.error('Error minting ARC20:', error);
})
```

```javascript
//mint Realm
wizz.requestMint({
  type: 'mint_realm',
  realm: 'sats1573', // 可选
  bitworkc: '12345.6', // 可选
  satsIn: 888 // 可选
}).catch(error => {
  console.error('Error minting Realm:', error);
})
```

```jsx
// mintSubrealm
wizz.requestMint({
  type: 'mint_subrealm', 
  realm: 'hello', // 可选
  subrealm: '123456', // 可选
  satsIn: 789 // 可选
}).catch(error => {
  console.error('Error minting Subrealm:', error);
})
```

```jsx
//mintContainer
wizz.requestMint({
  type: 'mint_container',
  container: 'sats1573', // 可选
  satsIn: 666 // 可选
}).catch(error => {
  console.error('Error minting Container:', error);
})
```

```jsx
//mintDmitem
wizz.requestMint({
  type: 'mint_dmitem',
  collection: 'xxxx', // 可选
  collectionId: 'xxx', // 可选
  atomicalId: '00000b94ff2b25b8f51118aa65dd22dece0acd28609ae90bc564a276c33d5af2i0', // 可选
  dmitem: {
    'mainHash': '26af0bf67e433afe6c5637d07aaa9b17701efbe52d01e00e586635bcbf25dd36',
    'data': {
      'args': {
        'request_dmitem': '878',
        'main': 'image.svg',
        'i': true,
        'proof': [
          {
            'p': false,
            'd': '13576b4c9ae48ae86adf82c21f9876531825db8e5727a10eaf4b90e2e3de8bbe',
          },
          {
            'p': true,
            'd': '0b0276c54bf8c0d790df490d91f26ca94463edf9937a6660be27f4c3339a0dc3',
          },
          {
            'p': true,
            'd': '44b1c7be4aa863ea5e4fbefd5bd994d8dcf6878b387b46989ac9dadd71af4411',
          },
          {
            'p': true,
            'd': '0be312c85b53777b274be59457c0895bb37d9accee8333e2836e027c4473e6c1',
          },
          {
            'p': true,
            'd': '89e5a2d475b4e7d05adfc1d14583b18dd34a7a21f39aa4d9141939238653e0d9',
          },
          {
            'p': false,
            'd': 'fca8254d88845c95a050449e255d7a06a576f747e61a17cba3dcc27bfd963c93',
          },
          {
            'p': false,
            'd': '94cc56fcfa9f494a286e72bbb40c1823a3f07a0f74546542df369513c819d540',
          },
          {
            'p': true,
            'd': '2a57764d24fd91bdccd0bc6b899dfa99be652acc921ceacc745fcb8e3c57952d',
          },
          {
            'p': false,
            'd': '4a9a30fea16b07b2232282afc1c95e52354c6d5c1d0589dc6be9a87650a43d7f',
          },
          {
            'p': false,
            'd': 'e07e43e2b753905a6ef99eaa3f656b342c0b781d344cb80ecce6a5e1dddf4757',
          },
        ],
        'parent_container': '00000b94ff2b25b8f51118aa65dd22dece0acd28609ae90bc564a276c33d5af2i0',
        'bitworkc': '8888.8',
        'bitworkr': '8888.8',
      },
      'image.svg': {
        '$b': '3c7376672077696474683d2232303022206865696768743d223230302220786d6c6e733d22687474703a2f2f7777772e77332e6f72672f323030302f737667223e0a20203c726563742077696474683d223130302522206865696768743d2231303025222066696c6c3d2223303030303030222f3e0a20203c7465787420783d223530252220793d223435252220666f6e742d66616d696c793d22417269616c2220666f6e742d73697a653d223530222066696c6c3d2277686974652220646f6d696e616e742d626173656c696e653d226d6964646c652220746578742d616e63686f723d226d6964646c65223ee681903c2f746578743e0a20203c7465787420783d223530252220793d223735252220666f6e742d66616d696c793d22417269616c2220666f6e742d73697a653d223230222066696c6c3d2277686974652220646f6d696e616e742d626173656c696e653d226d6964646c652220746578742d616e63686f723d226d6964646c65223e6536383139303c2f746578743e0a3c2f7376673e',
      },
    },
    'targetVector': '878:any:any:image.svg:26af0bf67e433afe6c5637d07aaa9b17701efbe52d01e00e586635bcbf25dd36',
    'targethash': '26af0bf67e433afe6c5637d07aaa9b17701efbe52d01e00e586635bcbf25dd36',
  },
  satsIn: 666 // 可选
}).catch(error => {
  console.error('Error minting DMItem:', error);
})
```

```jsx
//mintNFT
wizz.requestMint({
  type: 'mint_nft',
  fileBytes: new Uint8Array([...]), // 可选
  fileName: 'item-988.json', // 可选
  contentType: 'application/json', // 可选
  bitworkc: '8888.8', // 可选
  bitworkr: '8888.8', // 可选
  satsIn: 555 // 可选
}).catch(error => {
  console.error('Error minting NFT:', error);
})

```

### sendBitcoin

发送BTC交易。 (要求插件版本>=2.6.0)

```javascript
const txid = await window.wizz.sendBitcoin('bc1p...', 1000, { feeRate: 100 });
console.log({ txid });
```

**参数**

1. `address` 接收地址
2. `amount` 发送的BTC数量
3. `options` 可选参数：
   * `feeRate` 使用的费率

**返回**

`Promise` - `string`: 交易的txid

### sendARC20

发送ARC20交易。 (要求插件版本>=2.6.0) (仅插件支持)

```javascript
const txid = await window.wizz.sendARC20('bc1p...', 'atom', 1000, { feeRate: 100 });
console.log({ txid });
```

**参数**

1. `address` 接收地址
2. `arc20` 待发送ARC20名称
3. `amount` 发送的ARC20数量
4. `options` 可选参数：
   * `feeRate` 使用的费率

**返回**

`Promise` - `string`：交易的txid

### sendAtomicals

发送Atomcials交易  (要求插件版本>=2.6.0) (仅插件支持)

```javascript
const txid = await window.wizz.sendAtomicals('bc1p...', ['O9adf....', '80ea...'], { feeRate: 100 });
console.log({ txid });
```

**参数**

1. `address` 接收地址
2. `atomicalIds` 待发送Atomicals ID列表
3. `options` 可选参数：
   * `feeRate` 使用的费率

**返回**

`Promise` - `string`：交易的txid

### getBalance

获取当前连接账号的余额  (要求插件版本>=2.6.0)

```jsx
const { confirmed, unconfirmed, total } = await window.wizz.getBalance();
console.log({ confirmed, unconfirmed, total });
```

**参数**

无

**返回**

`Promise` - `{ confirmed: number, unconfirmed: number, total: number }`: 当前账户的余额

### getAccounts

```jsx
wizz.getAccounts();
```

获取当前账户的地址

**参数**

无

**返回**

`Promise` - `string[]`：当前账户的地址

**示例**

```jsx
try {
  let res = await window.wizz.getAccounts();
  console.log(res)
} catch (e) {
  console.log(e);
}
> ["tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz"]

```

### getNetwork

```jsx
wizz.getNetwork()
```

获取网络

**参数**

无

**返回**

`Promise` - `string`：`livenet` 和 `testnet` 网络

**示例**

```jsx
try {
  let res = await window.wizz.getNetwork();
  console.log(res)
} catch (e) {
  console.log(e);
}

> 0

```

### switchNetwork

```jsx
wizz.switchNetwork(network)
```

切换网络

**参数**

`network` - `string`: `livenet` 和 `testnet` 网络

**返回**

无

**示例**

```jsx
try {
  let res = await window.wizz.switchNetwork("livenet");
  console.log(res)
} catch (e) {
  console.log(e);
}

> livenet

```

### getPublicKey

```jsx
wizz.getPublicKey()
```

获取当前账户的公钥。

**参数**

无

**返回**

`Promise` - `string`: 公钥

**示例**

```jsx
try {
  let res = await window.wizz.getPublicKey();
  console.log(res)
} catch (e) {
  console.log(e);
}
> 03cbaedc26f03fd3ba02fc936f338e980c9e2172c5e23128877ed46827e935296f

```

### getInscriptions

```jsx
wizz.getInscriptions(cursor, size)
```

列出当前账户的铭文

**参数**

无

**返回**

`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` : 创世交易的 txid

`location` - `string` : 当前位置的 txid 和 vout

**示例**

```jsx
try {
  let res = await window.wizz.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: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f

1832fb1ff560037531',
      location: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531:0:0',
      output: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531:0',
      offset: 0
    }
  ]
}

```

### signMessage

```jsx
wizz.signMessage(msg[, type])
```

签名消息

**参数**

`msg` - `string`: 要签名的字符串

`type` - `string`: (可选) "ecdsa" | "bip322-simple"。默认为 "ecdsa"

**返回**

`Promise` - `string`: 签名。

**示例**

```jsx
// 使用 ecdsa 签名
try {
  let res = await window.wizz.signMessage("abcdefghijk123456789");
  console.log(res)
} catch (e) {
  console.log(e);
}

> G+LrYa7T5dUMDgQduAErw+i6ebK4GqTXYVWIDM+snYk7Yc6LdPitmaqM6j+iJOeID1CsMXOJFpVopvPiHBdulkE=

// 使用 bip322-simple 签名
try {
  let res = await window.wizz.signMessage("abcdefghijk123456789","bip322-simple");
  console.log(res)
} catch (e) {
  console.log(e);
}

> AkcwRAIgeHUcjr0jODaR7GMM8cenWnIj0MYdGmmrpGyMoryNSkgCICzVXWrLIKKp5cFtaCTErY7FGNXTFe6kuEofl4G+Vi5wASECaIeVi8xMtvjATqSSYPDRDjEsQbr0hSUpU7FHJNtVKqw=

```

### pushTx

```jsx
wizz.pushTx(options)

```

推送交易

**参数**

`options` - `Object`:`rawtx` - `string`: 要推送的原始交易

**返回**

`Promise` - `string`: 交易 ID

**示例**

```jsx
try {
  let txid = await window.wizz.pushTx({
    rawtx:"0200000000010135bd7d..."
  });
  console.log(txid)
} catch (e) {
  console.log(e);
}

```

### signPsbt

```jsx
wizz.signPsbt(psbtHex[, options])
```

签名 PSBT

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

**参数**

`psbtHex` - `string`: 要签名的 PSBT 的十六进制字符串

`options` `autoFinalized` - `boolean`: 签名后是否自动完成 PSBT，默认为 true

`toSignInputs` - `array`:

`index` - `number`: 要签名的输入

`address` - `string`: (至少指定地址或公钥中的一个) 用于签名的私钥对应的地址

`publicKey` - `string`: (至少指定地址或公钥中的一个) 用于签名的私钥对应的公钥

`sighashTypes` - `number[]`: (可选) 签名哈希类型

`disableTweakSigner` - `boolean` :(可选)

在签名和解锁 Taproot 地址时，默认使用 `tweakSigner` 生成签名。启用此选项允许使用原始私钥进行签名。

**返回**

`Promise` - `string`: 签名后的 PSBT 的十六进制字符串

**示例**

```jsx
try {
  let res = await window.wizz.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);
}

wizz.signPsbt("xxxxxxxx",{toSignInputs:[{index:0,publicKey:"xxxxxx",disableTweakSigner:true}],autoFinalized:false})

```

### pushPsbt

```jsx
wizz.pushPsbt(psbtHex)
```

推送交易

**参数**

`psbtHex` - `string`: 要推送的 PSBT 的十六进制字符串

**返回**

`Promise` - `string`: 交易 ID

**示例**

```jsx
try {
  let res = await window.wizz.pushPsbt("70736274ff01007d....");
  console.log(res)
} catch (e) {
  console.log(e);
}
```

## **事件**

### **accountsChanged**

```jsx
wizz.on('accountsChanged', handler: (accounts: Array<string>) => void);
wizz.removeListener('accountsChanged', handler: (accounts: Array<string>) => void);
```

当用户公开的账户地址发生变化时，将触发 `accountsChanged` 事件。

### **networkChanged**

```jsx
wizz.on('networkChanged', handler: (network: string) => void);
wizz.removeListener('networkChanged', handler: (network: string) => void);
```

当用户的网络发生变化时，将触发 `networkChanged` 事件。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wizz.cash/wizz-wallet/zh/kai-fa-zhe/qian-bao-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
