{"id":20987430,"url":"https://github.com/stacks-archive/stacks-utils","last_synced_at":"2025-05-14T17:33:58.759Z","repository":{"id":38052592,"uuid":"159214928","full_name":"stacks-network/stacks-utils","owner":"stacks-network","description":"Utilities for the Stacks blockchain.","archived":false,"fork":false,"pushed_at":"2023-09-11T14:13:04.000Z","size":2191,"stargazers_count":6,"open_issues_count":31,"forks_count":6,"subscribers_count":19,"default_branch":"master","last_synced_at":"2024-04-14T11:50:54.840Z","etag":null,"topics":["blockchain","stacks"],"latest_commit_sha":null,"homepage":"https://stacks-utils.now.sh","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stacks-network.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-26T18:32:13.000Z","updated_at":"2023-11-12T21:05:59.000Z","dependencies_parsed_at":"2023-01-26T02:15:15.228Z","dependency_job_id":null,"html_url":"https://github.com/stacks-network/stacks-utils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacks-network%2Fstacks-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacks-network%2Fstacks-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacks-network%2Fstacks-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacks-network%2Fstacks-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stacks-network","download_url":"https://codeload.github.com/stacks-network/stacks-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225303944,"owners_count":17453037,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","stacks"],"created_at":"2024-11-19T06:16:56.358Z","updated_at":"2025-05-14T17:33:53.370Z","avatar_url":"https://github.com/stacks-network.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Stacks Utilities\n\n[![npm version](https://img.shields.io/bundlephobia/minzip/stacks-utils.svg)](https://npmjs.com/stacks-utils)\n[![npm version](https://img.shields.io/npm/dm/stacks-utils.svg)](https://npmjs.com/stacks-utils)\n[![npm version](https://img.shields.io/npm/v/stacks-utils.svg)](https://npmjs.com/stacks-utils)\n[![npm license](https://img.shields.io/npm/l/stacks-utils.svg)](https://npmjs.com/stacks-utils)\n\n## Getting started\n\n```\nnpm install stacks-utils\n# or\nyarn add stacks-utils\n```\n\n## Table of Contents\n\n- Addresses\n- Transactions\n- Hardware Wallets\n- Data Fetching\n- Units\n\n## Addresses\n\n#### Validate Stacks Address\n\n```jsx\nimport { validateStacksAddress } from \"stacks-utils\";\n\nconst isValid = validateStacksAddress(stacksAddress);\n```\n\n#### Stacks to Bitcoin\n\n```jsx\nimport { stacksAddressToBtcAddress } from \"stacks-utils\";\n\nconst btcAddress = stacksAddressToBtcAddress(stacksAddress);\n```\n\n#### Bitcoin to Stacks\n\n```jsx\nimport { btcAddressToStacksAddress } from \"stacks-utils\";\n\nconst stacksAddress = btcAddressToStacksAddress(btcAddress);\n```\n\n## Transactions\n\n#### Decode raw Bitcoin Transaction\n\n```jsx\nimport { decodeRawTx } from \"stacks-utils\";\n\nconst fetchFees = false; // if true, the BTC fees will be fetched and calculated\n\nasync () =\u003e {\n  const tx = await decodeRawTx(rawTx, fetchFees);\n  console.log(tx);\n};\n```\n\nThis will return an object as such:\n\n```jsx\nconst tx = {\n  sender, // sender STX address\n  senderBitcoinAddress, // sender BTC address\n  recipient, // recipient STX address\n  recipientBitcoinAddress, // recipient BTC address\n  opcode, // $\n  operation, // TOKEN_TRANSFER\n  consensusHash, // df1631913bbf485ce6a25f26bccfc8d3\n  tokenType, // \"STACKS\"\n  tokenAmount, // BigInteger\n  tokenAmountReadable, // 0.000001\n  memo, // Message\n  fees // BTC tx fees in satoshis (if fetchFees = true)\n};\n```\n\n### Decode an array of transactions\n\nThis is mostly to be used in conjunction with `fetchBtcAddressData`. This will take an array of BTC transactions (with a `hex` key in each object) and decode the raw transaction and combine the two.\n\n```jsx\nimport { decodeRawTxs } from \"stacks-utils\";\n\nconst fetchFees = false; // if true, the BTC fees will be fetched and calculated\n\n(async () =\u003e {\n    const txs = [...];\n    const transactions = await decodeRawTx(txs, fetchFees);\n    console.log(transactions)\n})\n```\n\n### Get readable operation type\n\nSee: [https://docs.blockstack.org/core/wire-format.html](https://docs.blockstack.org/core/wire-format.html)\n\n```jsx\nimport { getOperationType } from \"stacks-utils\";\n\nconst opcode = \"$\";\nconst operation = getOperationType(opcode); // TOKEN_TRANSFER\n```\n\n## Data Fetching\n\n### Fetch all data associated with a Stacks Address\n\n```jsx\nimport { fetchStacksAddressData } from \"stacks-utils\";\n\nconst data = await fetchStacksAddressData(stacksAddress);\n```\n\n### Fetch Stacks Address data from the Blockstack Explorer API\n\n```jsx\nimport { fetchStacksAddressDetails } from \"stacks-utils\";\n\nconst data = await fetchStacksAddressDetails(stacksAddress);\n```\n\n### Fetch all data associated with a BTC Address\n\n```jsx\nimport { fetchBtcAddressData } from \"stacks-utils\";\n\nconst data = await fetchBtcAddressData(btcAddress);\n```\n\n## Units\n\n### Microstacks to Stacks\n\n```jsx\nimport { microToStacks } from \"stacks-utils\";\n\nconst stacksAmount = microToStacks(1); // 0.000001\n```\n\n### Stacks to Microstacks\n\n```jsx\nimport { stacksToMicro } from \"stacks-utils\";\n\nconst microStacksAmount = stacksToMicro(0.000001); // 1\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstacks-archive%2Fstacks-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstacks-archive%2Fstacks-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstacks-archive%2Fstacks-utils/lists"}