Source: block_explorer.js

  1. /**
  2. * This module provides functions for creating URLs for Blockstream's
  3. * [block explorer]{@link https://blockstream.info}.
  4. *
  5. * This module does NOT provide implementations of HTTP requests which
  6. * fetch data from these URLs.
  7. *
  8. * @module block_explorer
  9. */
  10. import {TESTNET} from "./networks";
  11. const BASE_URL_MAINNET = 'https://blockstream.info';
  12. const BASE_URL_TESTNET = 'https://blockstream.info/testnet';
  13. function blockExplorerBaseURL(network) {
  14. return (network === TESTNET ? BASE_URL_TESTNET : BASE_URL_MAINNET);
  15. }
  16. /**
  17. * Returns the block explorer URL for the given path and network.
  18. *
  19. * @param {string} path - the explorer path
  20. * @param {module:networks.NETWORKS} network - bitcoin network
  21. * @returns {string} the block explorer url
  22. * @example
  23. * import {MAINNET, TESTNET, blockExplorerURL} from "unchained-bitcoin";
  24. * const path = "/block/00000000000000000011341d69792271766e4683e29b3ea169eacc59bde10a57";
  25. * console.log(blockExplorerURL(path, MAINNET)) // https://blockstream.info/block/00000000000000000011341d69792271766e4683e29b3ea169eacc59bde10a57
  26. * console.log(blockExplorerURL(path, TESTNET)) // https://blockstream.info/block/testnet/00000000000000000011341d69792271766e4683e29b3ea169eacc59bde10a57
  27. */
  28. export function blockExplorerURL(path, network) {
  29. return `${blockExplorerBaseURL(network)}${path}`;
  30. }
  31. /**
  32. * Returns the block explorer API URL for the given path and network.
  33. *
  34. * @param {string} path - the API path
  35. * @param {module:networks.NETWORKS} network - bitcoin network
  36. * @returns {string} the full block explorer url
  37. * @example
  38. * import {MAINNET, TESTNET, blockExplorerAPIURL} from "unchained-bitcoin";
  39. * const path = "/tx/1814a10fb22e9551a17a94a1e68971e19b4f59eaf1689e0af85b97929b3b9ae0";
  40. * console.log(blockExplorerAPIURL(path, MAINNET)); // https://blockstream.info/api/tx/1814a10fb22e9551a17a94a1e68971e19b4f59eaf1689e0af85b97929b3b9ae0
  41. * console.log(blockExplorerAPIURL(path, TESTNET)); // https://blockstream.info/testnet/api/tx/1814a10fb22e9551a17a94a1e68971e19b4f59eaf1689e0af85b97929b3b9ae0
  42. */
  43. export function blockExplorerAPIURL(path, network) {
  44. return `${blockExplorerBaseURL(network)}/api${path}`;
  45. }
  46. /**
  47. * Return the block explorer URL for the given transaction ID and network.
  48. *
  49. * @param {string} txid - the transaction id
  50. * @param {module:networks.NETWORKS} network - bitcoin network
  51. * @returns {string} the full transaction URL
  52. * @example
  53. * import {MAINNET, TESTNET, blockExplorerTransactionURL} from "unchained-bitcoin";
  54. * const txid = "1814a10fb22e9551a17a94a1e68971e19b4f59eaf1689e0af85b97929b3b9ae0";
  55. * console.log(blockExplorerTransactionURL(txid, MAINNET)); // https://blockstream.info/tx/1814a10fb22e9551a17a94a1e68971e19b4f59eaf1689e0af85b97929b3b9ae0
  56. * console.log(blockExplorerTransactionURL(txid, TESTNET)); // https://blockstream.info/testnet/tx/1814a10fb22e9551a17a94a1e68971e19b4f59eaf1689e0af85b97929b3b9ae0
  57. */
  58. export function blockExplorerTransactionURL(txid, network) {
  59. return blockExplorerURL(`/tx/${txid}`, network);
  60. }
  61. /**
  62. * Return the block explorer URL for the given address and network.
  63. *
  64. * @param {string} address - the address
  65. * @param {module:networks.NETWORKS} network - bitcoin network
  66. * @returns {string} full URL for address lookup
  67. * @example
  68. * import {MAINNET, TESTNET, blockExplorerAddressURL} from "unchained-bitcoin";
  69. * const address = "39YqNoLULDpbjmeCTdGJ42DQhrQLzRcMdX";
  70. * console.log(blockExplorerAddressURL(address, MAINNET)); // https://blockstream.info/address/39YqNoLULDpbjmeCTdGJ42DQhrQLzRcMdX
  71. * console.log(blockExplorerAddressURL(address, TESTNET)); // https://blockstream.info/testnet/address/39YqNoLULDpbjmeCTdGJ42DQhrQLzRcMdX
  72. */
  73. export function blockExplorerAddressURL(address, network) {
  74. return blockExplorerURL(`/address/${address}`, network);
  75. }