@investorphem/stx-utils 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +73 -38
  2. package/index.js +39 -18
  3. package/package.json +33 -6
package/README.md CHANGED
@@ -5,85 +5,120 @@
5
5
  [![License](https://img.shields.io/npm/l/@investorphem/stx-utils.svg?style=flat-square)](LICENSE)
6
6
  [![Build Status](https://github.com/investorphem/stx-utils/actions/workflows/publish.yml/badge.svg)](https://github.com/investorphem/stx-utils/actions/workflows/publish.yml)
7
7
  [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
8
- [![Contributors](https://img.shields.io/github/contributors/investorphem/stx-utils.svg)](https://github.com/<your-username>/stx-utils/graphs/contributors)
8
+ [![Contributors](https://img.shields.io/github/contributors/investorphem/stx-utils.svg)](https://github.com/investorphem/stx-utils/graphs/contributors)
9
9
 
10
10
  ---
11
11
 
12
- ## Description
12
+ ## 🚀 Description
13
13
 
14
- `@investorphem/stx-utils` is a JavaScript utility library for interacting with the Stacks blockchain. It provides functions to send STX, read contract state, validate addresses, and convert STX units.
14
+ `@investorphem/stx-utils` is a modern JavaScript utility library for interacting with the Stacks blockchain. It provides essential, type-safe functions to send STX programmatically, validate wallet addresses, and cleanly convert STX units for frontend interfaces.
15
15
 
16
- ## Features
16
+ ## Features
17
17
 
18
- * Send STX tokens programmatically
19
- * Read-only contract calls
20
- * Validate Stacks addresses
21
- * Convert between STX and micro-STX
22
- * Check account balances
23
- * Fully automated GitHub Actions publishing workflow
18
+ * 💸 **Send STX tokens** programmatically via backend or Node.js scripts
19
+ * **Validate Stacks addresses** securely without crashing your app
20
+ * 🔄 **Convert STX units** between STX and micro-STX with BigInt support
21
+ * 🛡️ **Null-Safe:** Safely handles empty or malformed inputs
22
+ * **Modern ESM:** Native ES Module support (`import`/`export`)
23
+ * 🤖 Fully automated GitHub Actions publishing workflow
24
24
 
25
- ## Installation
25
+ ## 📦 Installation
26
26
 
27
27
  ```bash
28
28
  npm install @investorphem/stx-utils
29
- npm install @stacks/transactions @stacks/network @stacks/crypto
30
29
  ```
31
30
 
32
- ## Usage
31
+ *Note: This package requires `@stacks/network` and `@stacks/transactions` as peer dependencies. Ensure they are installed in your project.*
33
32
 
34
- ```js
35
- const { stxToMicro, microToStx, isValidAddress, sendSTX } = require('@investorphem/stx-utils');
33
+ ## 🧠 Usage (ES Modules)
34
+
35
+ Since version 1.0.2, this package uses standard ES Modules.
36
+
37
+ ```javascript
38
+ import { stxToMicro, microToStx, isValidAddress, sendSTX } from '@investorphem/stx-utils';
36
39
 
37
40
  console.log(stxToMicro(1.5)); // 1500000n
38
41
  console.log(microToStx(1500000n)); // 1.5
39
- console.log(isValidAddress('ST3J2GVMMM2R07ZFBJDWTYEYAR8FZH5WKDTFJ9AHA')); // true
40
-
41
- // Sending STX (async)
42
- (async () => {
43
- const result = await sendSTX('<private-key>', 'ST3J2GVMMM2R07ZFBJDWTYEYAR8FZH5WKDTFJ9AHA', 0.1, 'testnet');
44
- console.log(result);
45
- })();
42
+ console.log(isValidAddress('SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR')); // true
43
+
44
+ // Sending STX programmatically (async)
45
+ async function executeTransfer() {
46
+ try {
47
+ const result = await sendSTX(
48
+ '<your-private-key>',
49
+ 'SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR',
50
+ 0.1,
51
+ 'mainnet' // Defaults to 'testnet' if omitted
52
+ );
53
+ console.log("Transaction broadcasted:", result);
54
+ } catch (error) {
55
+ console.error("Transfer failed:", error);
56
+ }
57
+ }
58
+
59
+ executeTransfer();
46
60
  ```
47
61
 
48
- ## API
62
+ ## 📚 API
49
63
 
50
64
  ### `stxToMicro(amount)`
51
65
 
52
- Converts STX to micro-STX.
66
+ Converts standard STX to micro-STX (1 STX = 1,000,000 micro-STX).
67
+
68
+ **Returns:** *BigInt*
69
+
70
+ ---
53
71
 
54
72
  ### `microToStx(amount)`
55
73
 
56
- Converts micro-STX to STX.
74
+ Converts micro-STX back to standard STX for UI display.
75
+
76
+ **Returns:** *Number*
77
+
78
+ ---
57
79
 
58
80
  ### `isValidAddress(address)`
59
81
 
60
- Validates a Stacks address.
82
+ Validates if a string is a properly formatted Stacks address. Safely catches underlying library errors.
83
+
84
+ **Returns:** *boolean*
85
+
86
+ ---
61
87
 
62
88
  ### `sendSTX(senderKey, recipient, amount, network)`
63
89
 
64
- Sends STX tokens from a private key to a recipient.
90
+ Broadcasts an STX token transfer to the network using a private key. Designed for Node.js / backend environments.
65
91
 
66
92
  **Parameters:**
67
-
68
- * `senderKey` *(string)* – private key of sender
69
- * `recipient` *(string)* – STX address of recipient
70
- * `amount` *(number)* – STX amount
71
- * `network` *(string)* – 'testnet' or 'mainnet' (default: testnet)
93
+ * `senderKey` *(string)* – Private key of the sender
94
+ * `recipient` *(string)* – STX address of the recipient
95
+ * `amount` *(number)* – STX amount to send (automatically converted to micro-STX)
96
+ * `network` *(string)* – `'testnet'` or `'mainnet'` (default: `'testnet'`)
72
97
 
73
98
  **Returns:**
99
+ * Promise resolving to the Stacks transaction broadcast result.
100
+
101
+ ---
102
+
103
+ ## ⚙️ Automated Releases
74
104
 
75
- * Promise resolving to transaction result
105
+ This project uses an automated release script. To publish a new version:
76
106
 
77
- ## Contributing
107
+ 1. Commit your changes: `git commit -m "update stx utilities"`
108
+ 2. Run the release command: `npm run release`
78
109
 
79
- Contributions are welcome! Fork the repo, make improvements, and submit a pull request. Ensure code follows [StandardJS style](https://standardjs.com).
110
+ This will automatically bump the patch version, create a git tag, and push to GitHub, triggering the automated NPM publish action.
80
111
 
81
- ## License
112
+ ---
113
+
114
+ ## 🛠️ Contributing
115
+
116
+ Contributions are welcome! Please fork the repo, make improvements, and submit a pull request. Ensure code follows [StandardJS style](https://standardjs.com).
117
+
118
+ ## 📄 License
82
119
 
83
120
  MIT License
84
121
 
85
122
  ---
86
123
 
87
124
  *Maintained by Oluwafemi Olagoke*
88
-
89
- ---
package/index.js CHANGED
@@ -1,33 +1,54 @@
1
- const { StacksTestnet, StacksMainnet } = require('@stacks/network');
2
- const { broadcastTransaction, makeSTXTokenTransfer, validateStacksAddress } = require('@stacks/transactions');
1
+ import { StacksTestnet, StacksMainnet } from '@stacks/network';
2
+ import {
3
+ broadcastTransaction,
4
+ makeSTXTokenTransfer,
5
+ validateStacksAddress
6
+ } from '@stacks/transactions';
3
7
 
4
- function stxToMicro(amount) {
5
- return BigInt(Math.round(amount * 1e6));
8
+ // Convert STX to micro-STX (1 STX = 1,000,000 micro-STX)
9
+ export function stxToMicro(amount) {
10
+ if (amount === undefined || amount === null) return 0n;
11
+ // Use Math.round to prevent floating-point precision errors before converting to BigInt
12
+ return BigInt(Math.round(Number(amount) * 1e6));
6
13
  }
7
14
 
8
- function microToStx(amount) {
15
+ // Convert micro-STX to STX
16
+ export function microToStx(amount) {
17
+ if (amount === undefined || amount === null) return 0;
18
+ // Safely cast to Number (handles both BigInt and string inputs from Stacks API)
9
19
  return Number(amount) / 1e6;
10
20
  }
11
21
 
12
- function isValidAddress(address) {
13
- return validateStacksAddress(address);
22
+ // Validate a Stacks address
23
+ export function isValidAddress(address) {
24
+ if (!address || typeof address !== 'string') return false;
25
+ try {
26
+ return validateStacksAddress(address);
27
+ } catch {
28
+ return false; // Prevent app crashes if the library throws on a badly malformed string
29
+ }
14
30
  }
15
31
 
16
- async function sendSTX(senderKey, recipient, amount, network='testnet') {
32
+ // Send STX using a private key (Designed for backend/Node.js usage)
33
+ export async function sendSTX(senderKey, recipient, amount, network = 'testnet') {
34
+ if (!senderKey || !recipient || !amount) {
35
+ throw new Error("Missing required parameters (senderKey, recipient, amount) for sendSTX");
36
+ }
37
+
17
38
  const net = network === 'mainnet' ? new StacksMainnet() : new StacksTestnet();
39
+
18
40
  const txOptions = {
19
41
  recipient,
20
- amount: stxToMicro(amount),
42
+ amount: stxToMicro(amount), // Converts STX to micro-STX automatically
21
43
  senderKey,
22
44
  network: net,
23
45
  };
24
- const tx = await makeSTXTokenTransfer(txOptions);
25
- return await broadcastTransaction(tx, net);
26
- }
27
46
 
28
- module.exports = {
29
- stxToMicro,
30
- microToStx,
31
- isValidAddress,
32
- sendSTX
33
- };
47
+ try {
48
+ const tx = await makeSTXTokenTransfer(txOptions);
49
+ return await broadcastTransaction(tx, net);
50
+ } catch (error) {
51
+ console.error("Failed to broadcast transaction:", error.message);
52
+ throw error;
53
+ }
54
+ }
package/package.json CHANGED
@@ -1,15 +1,42 @@
1
1
  {
2
2
  "name": "@investorphem/stx-utils",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "JavaScript utility library for interacting with the Stacks blockchain.",
5
5
  "main": "index.js",
6
- "license": "MIT",
6
+ "type": "module",
7
7
  "scripts": {
8
- "test": "echo \"No tests yet\" && exit 0"
8
+ "test": "echo \"No tests yet\" && exit 0",
9
+ "release": "npm version patch && git push --follow-tags"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/investorphem/stx-utils.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/investorphem/stx-utils/issues"
9
17
  },
18
+ "homepage": "https://github.com/investorphem/stx-utils",
19
+ "funding": {
20
+ "type": "github",
21
+ "url": "https://github.com/sponsors/investorphem"
22
+ },
23
+ "keywords": [
24
+ "stacks",
25
+ "stx",
26
+ "utils",
27
+ "blockchain",
28
+ "crypto",
29
+ "investorphem",
30
+ "clarity",
31
+ "web3"
32
+ ],
10
33
  "author": "Oluwafemi Olagoke",
34
+ "license": "MIT",
11
35
  "dependencies": {
12
- "@stacks/transactions": "^3.0.0",
13
- "@stacks/network": "^3.0.0"
36
+ "@stacks/network": "^6.13.0",
37
+ "@stacks/transactions": "^6.13.0"
38
+ },
39
+ "publishConfig": {
40
+ "access": "public"
14
41
  }
15
- }
42
+ }