# Universe Wallet API

## Universe Wallet API <a href="#unisat-wallet-api" id="unisat-wallet-api"></a>

Welcome to Universe Wallet's Developer Documentation. This documentation is for learning to develop applications for Universe Wallet.

## Getting Started <a href="#getting-started" id="getting-started"></a>

To develop for Universe Wallet, install Universe Wallet on your development machine. [Download here](https://bitcoinuniverse.io).

Once Universe Wallet is installed and running, you should find that new browser tabs have a `window.tapwallet` object available in the developer console. This is how your website will interact with Universe Wallet.

### Browser Detection <a href="#browser-detection" id="browser-detection"></a>

To verify if the browser is running Universe Wallet, copy and paste the code snippet below in the developer console of your web browser:

Copy

```
if (typeof window.tapwallet !== 'undefined') {
  console.log('Tap Wallet is installed!');
}
```

You can review the full API for the `window.tapwallet` object here.

### Connecting to Universe Wallet <a href="#connecting-to-unisat-wallet" id="connecting-to-unisat-wallet"></a>

"Connecting" or "logging in" to Universe Wallet effectively means "to access the user's Bitcoin account(s)".

You should **only** initiate a connection request in response to direct user action, such as clicking a button. You should **always** disable the "connect" button while the connection request is pending. You should **never** initiate a connection request on page load.

We recommend that you provide a button to allow the user to connect Tap Wallet to your dapp. Clicking this button should call the following method:

Copy

```
tapwallet.requestAccounts()
```

## Methods <a href="#methods" id="methods"></a>

### requestAccounts <a href="#requestaccounts" id="requestaccounts"></a>

Copy

```
tapwallet.requestAccounts()
```

Connect the current account.

**Parameters**

none

**Returns**

`Promise` returns `string[]` : Address of current account.

**Example**

Copy

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

### getAccounts <a href="#getaccounts" id="getaccounts"></a>

Copy

```
tapwallet.getAccounts()
```

Get address of current account

**Parameters**

none

**Returns**

* `Promise` - `string`: address of current account

**Example**

Copy

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

### getNetwork <a href="#getnetwork" id="getnetwork"></a>

Copy

```
tapwallet.getNetwork()
```

get network

**Parameters**

none

**Returns**

* `Promise` - `string`: the network. `livenet` and `testnet`

**Example**

Copy

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

> 0
```

### switchNetwork <a href="#switchnetwork" id="switchnetwork"></a>

Copy

```
tapwallet.switchNetwork(network)
```

switch network

**Parameters**

* `network` - `string`: the network. `livenet` and `testnet`

**Returns**

none

**Example**

Copy

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

> 0
```

### getPublicKey <a href="#getpublickey" id="getpublickey"></a>

Copy

```
tapwallet.getPublicKey()
```

Get publicKey of current account.

**Parameters**

none

**Returns**

* `Promise` - `string`: publicKey

**Example**

Copy

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

### getBalance <a href="#getbalance" id="getbalance"></a>

Copy

```
tapwallet.getBalance()
```

Get BTC balance

**Parameters**

none

**Returns**

* `Promise` - `Object`:
  * `confirmed` - `number` : the confirmed satoshis
  * `unconfirmed` - `number` : the unconfirmed satoshis
  * `total` - `number` : the total satoshis

**Example**

Copy

```
try {
  let res = await window.tapwallet.getBalance();
  console.log(res)
} catch (e) {
  console.log(e);
}

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

### getInscriptions <a href="#getinscriptions" id="getinscriptions"></a>

Copy

```
tapwallet.getInscriptions(cursor,size)
```

List inscriptions of current account

**Parameters**

none

**Returns**

* `Promise` - `Object`:
  * `total` - `number` : the total count
  * `list` - `Object[]` :
  * `inscriptionId` - `string` : the id of inscription.
  * `inscriptionNumber` - `string` : the number of inscription.
  * `address` - `string` : the address of inscription.
  * `outputValue` - `string` : the output value of inscription.
  * `content` - `string` : the content url of inscription.
  * `contentLength` - `string` : the content length of inscription.
  * `contentType` - `number` : the content type of inscription.
  * `preview` - `number` : the preview link
  * `timestamp` - `number` : the blocktime of inscription.
  * `offset` - `number` : the offset of inscription.
  * `genesisTransaction` - `string` : the txid of genesis transaction
  * `location` - `string` : the txid and vout of current location

**Example**

Copy

```
try {
  let res = await window.tapwallet.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
    }
  ]
}
```

### sendBitcoin <a href="#sendbitcoin" id="sendbitcoin"></a>

Copy

```
tapwallet.sendBitcoin(toAddress, satoshis, options)
```

Send BTC

**Parameters**

* `toAddress` - `string`: the address to send
* `satoshis` - `number`: the satoshis to send
* `options` - `object`: (optional)
  * `feeRate` - `number`: the network fee rate

**Returns**

* `Promise` - `string`: txid

**Example**

Copy

```
try {
  let txid = await window.tapwallet.sendBitcoin("tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz",1000);
  console.log(txid)
} catch (e) {
  console.log(e);
}
```

### sendInscription <a href="#sendinscription" id="sendinscription"></a>

Copy

```
tapwallet.sendInscription(address, inscriptionId, options)
```

Send Inscription

**Parameters**

* `address` - `string`: the receiver address.
* `inscriptionId` - `string`: the id of Inscription
* `options` - `Object`: (optional)
  * `feeRate` - `number`: the network fee rate

**Returns**

* `Promise` - `Object`:
  * `txid` - `string` : the txid

**Example**

Copy

```
try {
  let {txid} = await window.tapwallet.sendInscription("tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny","e9b86a063d78cc8a1ed17d291703bcc95bcd521e087ab0c7f1621c9c607def1ai0",{feeRate:15});
  console.log("send Inscription 204 to tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny",{txid})
} catch (e) {
  console.log(e);
}
```

### inscribeTransfer <a href="#inscribetransfer" id="inscribetransfer"></a>

Copy

```
tapwallet.inscribeTransfer(ticker, amount)
```

Inscribe BRC-20 TRANSFER Inscription

**Parameters**

* `ticker` - `string`: BRC-20 ticker
* `amount` - `string`: the amount to inscribe

**Returns**

* `Promise` - `void`:

**Example**

Copy

```

window.tapwallet.inscribeTransfer("ordi","100"); 
```

### signMessage <a href="#signmessage" id="signmessage"></a>

Copy

```
tapwallet.signMessage(msg[, type])
```

sign message

**Parameters**

* `msg` - `string`: a string to sign
* `type` - `string`: (Optional) "ecdsa" | "bip322-simple". default is "ecdsa"

**Returns**

* `Promise` - `string`: the signature.

**Example**

Copy

```
// sign by ecdsa
try {
  let res = await window.tapwallet.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.tapwallet.signMessage("abcdefghijk123456789","bip322-simple");
  console.log(res)
} catch (e) {
  console.log(e);
}

> AkcwRAIgeHUcjr0jODaR7GMM8cenWnIj0MYdGmmrpGyMoryNSkgCICzVXWrLIKKp5cFtaCTErY7FGNXTFe6kuEofl4G+Vi5wASECaIeVi8xMtvjATqSSYPDRDjEsQbr0hSUpU7FHJNtVKqw=

```

### pushTx <a href="#pushtx" id="pushtx"></a>

Copy

```
tapwallet.pushTx(options)
```

Push Transaction

**Parameters**

* `options` - `Object`:
  * `rawtx` - `string`: rawtx to push

**Returns**

* `Promise` - `string`: txid

**Example**

Copy

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

### signPsbt <a href="#signpsbt" id="signpsbt"></a>

Copy

```
tapwallet.signPsbt(psbtHex[, options])
```

Sign PSBT

This method will traverse all inputs that match the current address to sign.

**Parameters**

* `psbtHex` - `string`: the hex string of psbt to sign
* options
  * `autoFinalized` - `boolean`: whether finalize psbt after signing, default is true
  * `toSignInputs` - `array`:
    * `index` - `number`: which input to sign
    * `address` - `string`: (at least specify either an address or a publicKey) Which corresponding private key to use for signing
    * `publicKey` - `string`: (at least specify either an address or a publicKey) Which corresponding private key to use for signing
    * `sighashTypes` - `number[]`: (optionals) sighashTypes
    * `disableTweakSigner` - `boolean` :(optionals) When signing and unlocking Taproot addresses, the `tweakSigner` is used by default for signature generation. Enabling this allows for signing with the original private key.

**Returns**

* `Promise` - `string`: the hex string of signed psbt

**Example**

Copy

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

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

### signPsbts <a href="#signpsbts" id="signpsbts"></a>

Copy

```
tapwallet.signPsbts(psbtHexs[, options])
```

Sign Multiple PSBTs at once

This method will traverse all inputs that match the current address to sign.

**Parameters**

* `psbtHexs` - `string[]`: the hex strings of psbt to sign
* `options` - `object`\[]: the options of signing psbt
  * `autoFinalized` - `boolean`: whether finalize psbt after signing, default is true
  * `toSignInputs` - `array`:
    * `index` - `number`: which input to sign
    * `address` - `string`: (at least specify either an address or a publicKey) Which corresponding private key to use for signing
    * `publicKey` - `string`: (at least specify either an address or a publicKey) Which corresponding private key to use for signing
    * `sighashTypes` - `number[]`: (optionals) sighashTypes

**Returns**

* `Promise` - `string[]`: the hex strings of signed psbt

**Example**

Copy

```
try {
  let res = await window.tapwallet.signPsbts(["70736274ff01007d...","70736274ff01007d..."]);
  console.log(res)
} catch (e) {
  console.log(e);
}
```

### pushPsbt <a href="#pushpsbt" id="pushpsbt"></a>

Copy

```
tapwallet.pushPsbt(psbtHex)
```

Push transaction

**Parameters**

* `psbtHex` - `string`: the hex string of psbt to push

**Returns**

* `Promise` - `string`: txid

**Example**

Copy

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

## Events <a href="#events" id="events"></a>

### accountsChanged <a href="#accountschanged" id="accountschanged"></a>

Copy

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

The `accountsChanged` will be emitted whenever the user's exposed account address changes.

### networkChanged <a href="#networkchanged" id="networkchanged"></a>

Copy

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

The `networkChanged` will be emitted whenever the user's network changes.
