GoSat Wallet API

GoSat Wallet API

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

Getting Started

To develop for GoSat Wallet, install GoSat Wallet on your development machine.

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

Browser Detection

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

if (typeof window.gosat !== 'undefined') {
  console.log('GoSat Wallet is installed!');
}

You can review the full API for the window.gosat object here.

Connecting to GoSat Wallet

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

You should only initiate a connection request in response to a 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 GoSat Wallet to your DAPP. Clicking this button should call the following method:

gosat.requestAccounts()

Methods

requestAccounts

gosat.requestAccounts()

Connect the current account.

Parameters

none

Returns

Promise returns string[] : Address of current account.

Example

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

getAccounts

gosat.getAccounts()

Get address of current account

Parameters

none

Returns

  • Promise - string: address of current account

Example

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

getNetwork

gosat.getNetwork()

get network

Parameters

none

Returns

  • Promise - string: the network. livenet and testnet

Example

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

> 0

switchNetwork

gosat.switchNetwork(network)

switch network

Parameters

  • network - string: the network. livenet and testnet

Returns

none

Example

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

> 0

getPublicKey

gosat.getPublicKey()

Get publicKey of the current account.

Parameters

none

Returns

  • Promise - string: publicKey

Example

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

getBalance

gosat.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

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

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

getInscriptions

gosat.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

try {
  let res = await window.gosat.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

gosat.sendBitcoin(toAddress, satoshis, options)

Send BTC

Parameters

  • toAddress - string: the address to send

  • satoshis - number: the satoshis to send

  • options - object:

    • feeRate - number: the network fee rate

Returns

  • Promise - string: txid

Example

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

sendInscription

gosat.sendInscription(address, inscriptionId, options)

Send Inscription

Parameters

  • options - Object:

    • id - string: the id of Inscription.

    • address - string: the receiver address.

Returns

  • Promise - Object:

    • txid - string : the txid

Example

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

signMessage

gosat.signMessage(msg[, address])

sign message

Parameters

  • msg - string: a string to sign

Returns

  • Promise - string: the signature.

Example

try {
  let res = await window.gosat.signMessage("abcdefghijk123456789");
  console.log(res)
} catch (e) {
  console.log(e);
}

> IMEo3yzgqJKlmc38IqlP3YjadVOnXmVR6fqeDhtVdiyHUbitYlO2CFUHUgGtM1/cjWsWoGVhTv6pyvj9L/kNT5A=

pushTx

gosat.pushTx(options)

Push Transaction

Parameters

  • options - Object:

    • rawtx - string: rawtx to push

Returns

  • Promise - string: txid

Example

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

signPsbt

gosat.signPsbt(psbtHex)

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

Returns

  • Promise - string: the hex string of signed psbt

Example

try {
  let res = await window.gosat.signPsbt("70736274ff01007d....");
  console.log(res)
} catch (e) {
  console.log(e);
}

pushPsbt

gosat.pushPsbt(psbtHex)

Push transaction

Parameters

  • psbtHex - string: the hex string of psbt to push

Returns

  • Promise - string: txid

Example

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

Events

accountsChanged

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

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

networkChanged

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

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

Last updated