tbcapi

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2024 License: MIT Imports: 6 Imported by: 0

README ΒΆ

πŸ“‘ Hemi Tiny Bitcoin Daemon RPC

Last updated: May 16th, 2024

This document provides details on the RPC protocol and available commands for the Hemi Tiny Bitcoin Daemon (tbcd).

Table of Contents

βš™οΈ Implementations

βš™οΈ tbcd Daemon

The tbcd daemon runs an RPC server that listens on the address provided by the TBC_ADDRESS environment variable. You can run the tbcd daemon with the RPC server enabled with the following command:

TBC_ADDRESS=localhost:8082 /path/to/tbcd
πŸ‘‰ RPC Client

hemictl serves as a reference implementation of an RPC client tailored for interacting with the tbcd daemon.


πŸ“š Resources

For developers looking to integrate or extend functionality, view the raw Go types used in TBC's RPC commands: View tbcapi.go.


πŸ“‘ Protocol

The RPC protocol is WebSocket-based and follows a standard request/response model. For more detailed information, refer to the protocol documentation.

🚫 Errors

If an error occurs during a request, the response payload will include an error value containing the following details:

Field Description
timestamp The time at which the error occurred, in Unix seconds.
trace A unique string for tracing errors between server and client (internal errors only).
message The error message. For internal server errors, this will read internal error.
πŸ—„οΈ Serialized Types
Block Header

A serialized block header contains the following data:

Field Description
version The version of the block.
prev_hash The hash of the previous block header in the blockchain, in reverse byte order and encoded as a hexadecimal string.
merkle_root The hash derived from the hashes of all transactions in the block, in reverse byte order and encoded as a hexadecimal string.
timestamp The time the miner began hashing the header, represented in Unix seconds.
bits The difficulty target for the block.
nonce The nonce used to create the hash that is less than or equal to the target threshold.
Address

Represents an encoded Bitcoin address, supporting these types:

  • P2PKH
  • P2SH
  • P2WPKH
  • P2WSH
  • P2TR
UTXO

A serialized UTXO contains the following data:

Field Description
tx_id The transaction ID, in reverse byte order and encoded as a hexadecimal string.
value The value of the UTXO.
out_index The output index of the UTXO.
Transaction

A serialized transaction contains the following data:

Field Description
version The transaction version.
lock_time The block height or timestamp after which the transaction becomes final.
tx_in An array of transaction inputs.
tx_out An array of transaction outputs.
Transaction Input

A serialized transaction input contains the following data:

Field Description
outpoint The outpoint for the previous transaction output.
signature_script The signature script for the transaction.
witness An array of the transaction witnesses, encoded as hexadecimal strings.
sequence The transaction sequence number.
Transaction Output

A serialized transaction output contains the following data:

Field Description
value The value of the transaction output in satoshis.
pk_script The pubkey script of the transaction output, encoded as a hexadecimal string.
Outpoint

A serialized outpoint contains the following data:

Field Description
hash The ID of the transaction holding the output to be spent, in reverse byte order and encoded as a hexadecimal string.
index The index of the specific output to spend from the transaction.

πŸ‘‰ Block Headers by Height

Retrieve the block headers by height.

πŸ—‚ Raw Data
Type command value
Request tbcapi-block-headers-by-height-raw-request
Response tbcapi-block-headers-by-height-raw-response
πŸ“€ Request
Payload
  • height: The height at which block headers should be retrieved.
Example Request

Retrieve block headers at height 43111:

{
  "header": {
    "command": "tbcapi-block-headers-by-height-raw-request",
    "id": "68656d69"
  },
  "payload": {
    "height": 43111
  }
}
πŸ“₯ Response
Payload
  • block_headers: An array of raw block headers encoded as hexadecimal strings.
Example Response

Response for a request with id 68656d69 and height 43111:

{
  "header": {
    "command": "tbcapi-block-headers-by-height-raw-response",
    "id": "68656d69"
  },
  "payload": {
    "block_headers": [
      "02000000cf31d5156c8ab752b91874d1072d4673b83ee3ed718d3cb4f461c410000000000ca43abadf59bee614186d30da42f56932dc2a53e6d920169b8577207f7b11fcfec3d750c0ff3f1c4f428f6a"
    ]
  }
}
πŸ—‚ Serialized Data
Type command value
Request tbcapi-block-headers-by-height-request
Response tbcapi-block-headers-by-height-response
πŸ“€ Request
Payload
  • height: The height at which block headers should be retrieved.
Example Request

Retrieve block headers at height 43111:

{
  "header": {
    "command": "tbcapi-block-headers-by-height-request",
    "id": "68656d69"
  },
  "payload": {
    "height": 43111
  }
}
πŸ“₯ Response
Payload
Example Response

Response for a request with id 68656d69 and height 43111:

{
  "header": {
    "command": "tbcapi-block-headers-by-height-response",
    "id": "68656d69"
  },
  "payload": {
    "block_headers": [
      {
        "version": 2,
        "prev_hash": "0000000010c461f4b43c8d71ede33eb873462d07d17418b952b78a6c15d531cf",
        "merkle_root": "fc117b7f2077859b1620d9e6532adc3269f542da306d1814e6be59dfba3aa40c",
        "timestamp": 1356317694,
        "bits": "1c3fffc0",
        "nonce": 1787773519
      }
    ]
  }
}

πŸ‘‰ Best Block Headers

Retrieve the best block headers.

πŸ—‚ Raw Data
Type command value
Request tbcapi-block-headers-best-raw-request
Response tbcapi-block-headers-best-raw-response
πŸ“€ Request
Example Request

Retrieve the best block headers:

{
  "header": {
    "command": "tbcapi-block-headers-best-raw-request",
    "id": "68656d69"
  }
}
πŸ“₯ Response
Payload
  • height: The best-known height.
  • block_headers: An array of the best-known block headers encoded as hexadecimal strings.
Example Response

Response for a request with id 68656d69 and best height 2182000:

{
  "header": {
    "command": "tbcapi-block-headers-best-raw-response",
    "id": "68656d69"
  },
  "payload": {
    "height": 2182000,
    "block_headers": [
      "0420002075089ac1ab1cab70cf6e6b774a86703a8d7127c0ebed1d3dfa2c00000000000086105509ec4a79457a400451290ad2a019fec4c76b47512623f1bb17a0ced76f38d82662bef4001b07d86700"
    ]
  }
}
πŸ—‚ Serialized Data
Type command value
Request tbcapi-block-headers-best-request
Response tbcapi-block-headers-best-response
πŸ“€ Request
Example Request

Retrieve the best block headers:

{
  "header": {
    "command": "tbcapi-block-headers-best-request",
    "id": "68656d69"
  }
}
πŸ“₯ Response
Payload
  • height: The best-known height.
  • block_headers: An array of best-known block headers.
Example Response

Response for a request with id 68656d69 and height 2587400:

{
  "header": {
    "command": "tbcapi-block-headers-best-response",
    "id": "68656d69"
  },
  "payload": {
    "height": 2587400,
    "block_headers": [
      {
        "version": 536887296,
        "prev_hash": "000000000000002bbbbec8f126dc76a82109d898383bca5013a2386c8675ce34",
        "merkle_root": "b9d74efdafb5436330b47478b2df28251057da5a9bc11c5509410950253d4f0e",
        "timestamp": 1713461092,
        "bits": "192e17d5",
        "nonce": 3365605040
      }
    ]
  }
}

πŸ‘‰ Balance by Address

Retrieve the balance for an address.

πŸ—‚ Raw Data
Type command value
Request tbcapi-balance-by-address-request
Response tbcapi-balance-by-address-response
πŸ“€ Request
Payload
  • address: The address for which the balance should be retrieved.
Example Request

Retrieve the balance for the address myqzZmRvoXmrhsrM5STiMGtNRxCFArHWRd:

{
  "header": {
    "command": "tbcapi-balance-by-address-request",
    "id": "68656d69"
  },
  "payload": {
    "address": "mhAfMWDjd8YV3RoWcpHSzqjkWi6q5Bfixa"
  }
}
πŸ“₯ Response
Payload
  • balance: The known balance of the address, in satoshis.
Example Response

Response for a request with id 68656d69, if the address's balance is 0:

{
  "header": {
    "command": "tbcapi-balance-by-address-response",
    "id": "68656d69"
  },
  "payload": {
    "balance": 0
  }
}

πŸ‘‰ UTXOs by Address

Retrieve UTXOs by address.

πŸ—‚ Raw Data
Type command value
Request tbcapi-utxos-by-address-raw-request
Response tbcapi-utxos-by-address-raw-response
πŸ“€ Request
Payload
  • address: The address to retrieve the UTXOs for.
  • start: The start index for the UTXOs that should be included in the response (or the number of UTXOs that should be skipped).
  • count: The maximum number of UTXOs that should be included in the response.
Example Request

Retrieve five UTXOs for the address mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk:

{
  "header": {
    "command": "tbcapi-utxos-by-address-raw-request",
    "id": "68656d69"
  },
  "payload": {
    "address": "mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk",
    "start": 0,
    "count": 5
  }
}
πŸ“₯ Response
Payload
  • utxos: An array of known UTXOs for the address, encoded as hexadecimal strings, or null if there are * no UTXOs* for the address.
Example Response

Response for a request with id 68656d69, requesting 5 UTXOs for the address mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk:

{
  "header": {
    "command": "tbcapi-utxos-by-address-raw-response",
    "id": "68656d69"
  },
  "payload": {
    "utxos": [
      "0073700282db1dcc4853dc64e5da5c8595d1204ea7d036b04ea6b8ba41093a770000000000000cab00000002",
      "0073700282db1dcc4853dc64e5da5c8595d1204ea7d036b04ea6b8ba41093a770000000000000cab00000002",
      "0073700282db1dcc4853dc64e5da5c8595d1204ea7d036b04ea6b8ba41093a770000000000000cab00000002",
      "0073700282db1dcc4853dc64e5da5c8595d1204ea7d036b04ea6b8ba41093a770000000000000cab00000002",
      "0073700282db1dcc4853dc64e5da5c8595d1204ea7d036b04ea6b8ba41093a770000000000000cab00000002"
    ]
  }
}
πŸ—‚ Serialized Data
Type command value
Request tbcapi-utxos-by-address-request
Response tbcapi-utxos-by-address-response
πŸ“€ Request
Payload
  • address: The address to retrieve the UTXOs for.
  • start: The start index for the UTXOs that should be included in the response (or the number of UTXOs that should be skipped).
  • count: The maximum number of UTXOs that should be included in the response.
Example Request

Retrieve 5 UTXOs for the address mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk:

{
  "header": {
    "command": "tbcapi-utxos-by-address-request",
    "id": "68656d69"
  },
  "payload": {
    "address": "mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk",
    "start": 0,
    "count": 5
  }
}
πŸ“₯ Response
Payload
  • utxos: An array of known UTXOs. The maximum number of items in this array can be changed with * count* in the request.
Example Response

Response for a request with id 68656d69, showing 5 UTXOs for the address:

{
  "header": {
    "command": "tbcapi-utxos-by-address-response",
    "id": "68656d69"
  },
  "payload": {
    "utxos": [
      {
        "tx_id": "0012a33f3c301c90427d81f256d8a4848dcbfc289e8325725e7657e9a643d6fd",
        "value": 2026,
        "out_index": 1
      },
      {
        "tx_id": "0066c9f87d012e75e390adb490794a746fefe05eb16d220515788f33d5b6b336",
        "value": 10000,
        "out_index": 1
      },
      {
        "tx_id": "0066c9f87d012e75e390adb490794a746fefe05eb16d220515788f33d5b6b336",
        "value": 10000,
        "out_index": 2
      },
      {
        "tx_id": "0073700282db1dcc4853dc64e5da5c8595d1204ea7d036b04ea6b8ba41093a77",
        "value": 3243,
        "out_index": 1
      },
      {
        "tx_id": "0073700282db1dcc4853dc64e5da5c8595d1204ea7d036b04ea6b8ba41093a77",
        "value": 3243,
        "out_index": 2
      }
    ]
  }
}

πŸ‘‰ Transaction by ID

πŸ—‚ Raw Data
Type command value
Request tbcapi-tx-by-id-raw-request
Response ttbcapi-tx-by-id-raw-response
πŸ“€ Request
Payload
  • tx_id: The ID of the transaction to retrieve, encoded as a hexadecimal string.

An example request to retrieve the transaction 0584ad53bf1938702b952026f7c986ab5d07ee7295c0ad3241c932a5483158ac:

{
  "header": {
    "command": "tbcapi-tx-by-id-raw-request",
    "id": "68656d69"
  },
  "payload": {
    "tx_id": "0584ad53bf1938702b952026f7c986ab5d07ee7295c0ad3241c932a5483158ac"
  }
}
πŸ“₯ Response
Payload
  • tx: The transaction (encoded as a hexadecimal string).
Example Response

An example response for a request with id 68656d69, requesting the transaction 0584ad53bf1938702b952026f7c986ab5d07ee7295c0ad3241c932a5483158ac:

{
  "header": {
    "command": "tbcapi-tx-by-id-raw-response",
    "id": "68656d69"
  },
  "payload": {
    "tx": "02000000019554a7eb8bc903ea957c87964ab04a58d177692f15d7271cccb95258202f14b5bd00000000fdffffff014a010000000000002251208ec88237b5978e75e93feaeeb1343ff86ae2f2c348a903c9c9c4ad081926773500000000"
  }
}
πŸ—‚ Serialized Data
Type command value
Request tbcapi-tx-by-id-request
Response tbcapi-tx-by-id-response
πŸ“€ Request
Payload
  • tx_id: The ID of the transaction to retrieve, in reverse byte order and encoded as a hexadecimal string.
Example Request

An example request to retrieve the transaction 0584ad53bf1938702b952026f7c986ab5d07ee7295c0ad3241c932a5483158ac:

{
  "header": {
    "command": "tbcapi-tx-by-id-request",
    "id": "68656d69"
  },
  "payload": {
    "tx_id": "0584ad53bf1938702b952026f7c986ab5d07ee7295c0ad3241c932a5483158ac"
  }
}
πŸ“₯ Response
Payload
  • tx: The requested transaction, if found, otherwise null.
Example Response

An example response for a request with id 68656d69, requesting the transaction 0584ad53bf1938702b952026f7c986ab5d07ee7295c0ad3241c932a5483158ac:

{
  "header": {
    "command": "tbcapi-tx-by-id-response",
    "id": "68656d69"
  },
  "payload": {
    "tx": {
      "version": 2,
      "lock_time": 0,
      "tx_in": [
        {
          "outpoint": {
            "hash": "9554a7eb8bc903ea957c87964ab04a58d177692f15d7271cccb95258202f14b5",
            "index": 189
          },
          "signature_script": "",
          "tx_witness": null,
          "sequence": 4294967293
        }
      ],
      "tx_out": [
        {
          "value": 330,
          "pk_script": "51208ec88237b5978e75e93feaeeb1343ff86ae2f2c348a903c9c9c4ad0819267735"
        }
      ]
    }
  }
}

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	APIVersion = 1

	CmdPingRequest  = "tbcapi-ping-request"
	CmdPingResponse = "tbcapi-ping-response"

	CmdBlockHeadersByHeightRawRequest  = "tbcapi-block-headers-by-height-raw-request"
	CmdBlockHeadersByHeightRawResponse = "tbcapi-block-headers-by-height-raw-response"

	CmdBlockHeadersByHeightRequest  = "tbcapi-block-headers-by-height-request"
	CmdBlockHeadersByHeightResponse = "tbcapi-block-headers-by-height-response"

	CmdBlockHeadersBestRawRequest  = "tbcapi-block-headers-best-raw-request"
	CmdBlockHeadersBestRawResponse = "tbcapi-block-headers-best-raw-response"

	CmdBlockHeadersBestRequest  = "tbcapi-block-headers-best-request"
	CmdBlockHeadersBestResponse = "tbcapi-block-headers-best-response"

	CmdBalanceByAddressRequest  = "tbcapi-balance-by-address-request"
	CmdBalanceByAddressResponse = "tbcapi-balance-by-address-response"

	CmdUtxosByAddressRawRequest  = "tbcapi-utxos-by-address-raw-request"
	CmdUtxosByAddressRawResponse = "tbcapi-utxos-by-address-raw-response"

	CmdUtxosByAddressRequest  = "tbcapi-utxos-by-address-request"
	CmdUtxosByAddressResponse = "tbcapi-utxos-by-address-response"

	CmdTxByIdRawRequest  = "tbcapi-tx-by-id-raw-request"
	CmdTxByIdRawResponse = "tbcapi-tx-by-id-raw-response"

	CmdTxByIdRequest  = "tbcapi-tx-by-id-request"
	CmdTxByIdResponse = "tbcapi-tx-by-id-response"
)

Variables ΒΆ

View Source
var (
	APIVersionRoute = fmt.Sprintf("v%d", APIVersion)
	RouteWebsocket  = fmt.Sprintf("/%s/ws", APIVersionRoute)

	DefaultListen = "localhost:8082"
	DefaultURL    = fmt.Sprintf("ws://%s/%s", DefaultListen, RouteWebsocket)
)

Functions ΒΆ

func APICommands ΒΆ

func APICommands() map[protocol.Command]reflect.Type

func Call ΒΆ

func Call(ctx context.Context, c *protocol.Conn, payload any) (protocol.Command, string, any, error)

Call is a blocking call. One should use ReadConn when using Call or else the completion will end up in the Read instead of being completed as expected.

func Read ΒΆ

Read is the low level primitive of a protocol Read. One should generally not use this function and use ReadConn instead.

func ReadConn ΒΆ

func ReadConn(ctx context.Context, c *protocol.Conn) (protocol.Command, string, any, error)

ReadConn reads from Conn and performs callbacks. One should use ReadConn over Read when mixing Write, WriteConn and Call.

func Write ΒΆ

func Write(ctx context.Context, c protocol.APIConn, id string, payload any) error

Write is the low level primitive of a protocol Write. One should generally not use this function and use WriteConn and Call instead.

func WriteConn ΒΆ

func WriteConn(ctx context.Context, c *protocol.Conn, id string, payload any) error

WriteConn writes to Conn. It is equivalent to Write but exists for symmetry reasons.

Types ΒΆ

type BalanceByAddressRequest ΒΆ

type BalanceByAddressRequest struct {
	Address string `json:"address"`
}

type BalanceByAddressResponse ΒΆ

type BalanceByAddressResponse struct {
	Balance uint64          `json:"balance"`
	Error   *protocol.Error `json:"error,omitempty"`
}

type BlockHeader ΒΆ

type BlockHeader struct {
	Version    int32         `json:"version"`
	PrevHash   api.ByteSlice `json:"prev_hash"`   // reverse order
	MerkleRoot api.ByteSlice `json:"merkle_root"` // reverse order
	Timestamp  int64         `json:"timestamp"`
	Bits       string        `json:"bits"`
	Nonce      uint32        `json:"nonce"`
}

type BlockHeadersBestRawRequest ΒΆ

type BlockHeadersBestRawRequest struct{}

type BlockHeadersBestRawResponse ΒΆ

type BlockHeadersBestRawResponse struct {
	Height       uint64          `json:"height"`
	BlockHeaders []api.ByteSlice `json:"block_headers"`
	Error        *protocol.Error `json:"error,omitempty"`
}

type BlockHeadersBestRequest ΒΆ

type BlockHeadersBestRequest struct{}

type BlockHeadersBestResponse ΒΆ

type BlockHeadersBestResponse struct {
	Height       uint64          `json:"height"`
	BlockHeaders []*BlockHeader  `json:"block_headers"`
	Error        *protocol.Error `json:"error,omitempty"`
}

type BlockHeadersByHeightRawRequest ΒΆ

type BlockHeadersByHeightRawRequest struct {
	Height uint32 `json:"height"`
}

type BlockHeadersByHeightRawResponse ΒΆ

type BlockHeadersByHeightRawResponse struct {
	BlockHeaders []api.ByteSlice `json:"block_headers"`
	Error        *protocol.Error `json:"error,omitempty"`
}

type BlockHeadersByHeightRequest ΒΆ

type BlockHeadersByHeightRequest struct {
	Height uint32 `json:"height"`
}

type BlockHeadersByHeightResponse ΒΆ

type BlockHeadersByHeightResponse struct {
	BlockHeaders []*BlockHeader  `json:"block_headers"`
	Error        *protocol.Error `json:"error,omitempty"`
}

type OutPoint ΒΆ

type OutPoint struct {
	Hash  api.ByteSlice `json:"hash"` // reverse order
	Index uint32        `json:"index"`
}

type PingRequest ΒΆ

type PingRequest protocol.PingRequest

type PingResponse ΒΆ

type PingResponse protocol.PingResponse

type Tx ΒΆ

type Tx struct {
	Version  int32    `json:"version"`
	LockTime uint32   `json:"lock_time"`
	TxIn     []*TxIn  `json:"tx_in"`
	TxOut    []*TxOut `json:"tx_out"`
}

type TxByIdRawRequest ΒΆ

type TxByIdRawRequest struct {
	TxId api.ByteSlice `json:"tx_id"` // natural order
}

type TxByIdRawResponse ΒΆ

type TxByIdRawResponse struct {
	Tx    api.ByteSlice   `json:"tx"`
	Error *protocol.Error `json:"error,omitempty"`
}

type TxByIdRequest ΒΆ

type TxByIdRequest struct {
	TxId api.ByteSlice `json:"tx_id"` // reverse order
}

type TxByIdResponse ΒΆ

type TxByIdResponse struct {
	Tx    *Tx             `json:"tx"`
	Error *protocol.Error `json:"error,omitempty"`
}

type TxIn ΒΆ

type TxIn struct {
	PreviousOutPoint OutPoint      `json:"outpoint"`
	SignatureScript  api.ByteSlice `json:"signature_script"`
	Witness          TxWitness     `json:"tx_witness"`
	Sequence         uint32        `json:"sequence"`
}

type TxOut ΒΆ

type TxOut struct {
	Value    int64         `json:"value"`
	PkScript api.ByteSlice `json:"pk_script"`
}

type TxWitness ΒΆ

type TxWitness []api.ByteSlice

type Utxo ΒΆ

type Utxo struct {
	TxId     api.ByteSlice `json:"tx_id"` // reverse order
	Value    uint64        `json:"value"`
	OutIndex uint32        `json:"out_index"`
}

type UtxosByAddressRawRequest ΒΆ

type UtxosByAddressRawRequest struct {
	Address string `json:"address"`
	Start   uint   `json:"start"`
	Count   uint   `json:"count"`
}

type UtxosByAddressRawResponse ΒΆ

type UtxosByAddressRawResponse struct {
	Utxos []api.ByteSlice `json:"utxos"`
	Error *protocol.Error `json:"error,omitempty"`
}

type UtxosByAddressRequest ΒΆ

type UtxosByAddressRequest struct {
	Address string `json:"address"`
	Start   uint   `json:"start"`
	Count   uint   `json:"count"`
}

type UtxosByAddressResponse ΒΆ

type UtxosByAddressResponse struct {
	Utxos []*Utxo         `json:"utxos"`
	Error *protocol.Error `json:"error,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL