@investorphem/stx-utils 1.0.2 → 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.
- package/README.md +73 -38
- package/index.js +39 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,85 +5,120 @@
|
|
|
5
5
|
[](LICENSE)
|
|
6
6
|
[](https://github.com/investorphem/stx-utils/actions/workflows/publish.yml)
|
|
7
7
|
[](https://standardjs.com)
|
|
8
|
-
[](https://github.com
|
|
8
|
+
[](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
|
|
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
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
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
|
-
|
|
31
|
+
*Note: This package requires `@stacks/network` and `@stacks/transactions` as peer dependencies. Ensure they are installed in your project.*
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
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('
|
|
40
|
-
|
|
41
|
-
// Sending STX (async)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
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
|
-
* `
|
|
69
|
-
* `
|
|
70
|
-
* `
|
|
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
|
-
|
|
105
|
+
This project uses an automated release script. To publish a new version:
|
|
76
106
|
|
|
77
|
-
|
|
107
|
+
1. Commit your changes: `git commit -m "update stx utilities"`
|
|
108
|
+
2. Run the release command: `npm run release`
|
|
78
109
|
|
|
79
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2
|
-
|
|
1
|
+
import { StacksTestnet, StacksMainnet } from '@stacks/network';
|
|
2
|
+
import {
|
|
3
|
+
broadcastTransaction,
|
|
4
|
+
makeSTXTokenTransfer,
|
|
5
|
+
validateStacksAddress
|
|
6
|
+
} from '@stacks/transactions';
|
|
3
7
|
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
13
|
-
|
|
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
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
}
|