txo_parser 0.0.1 → 0.0.2
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 +33 -1
- package/index.js +34 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,6 +34,20 @@ console.log(parsed)
|
|
|
34
34
|
// amount: 0.75
|
|
35
35
|
// }
|
|
36
36
|
|
|
37
|
+
// Parse legacy format
|
|
38
|
+
const legacyUri =
|
|
39
|
+
'txo:btc:4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0:0 0.75 Kx9'
|
|
40
|
+
const parsedLegacy = parseTxoUri(legacyUri)
|
|
41
|
+
console.log(parsedLegacy)
|
|
42
|
+
// Output:
|
|
43
|
+
// {
|
|
44
|
+
// network: 'btc',
|
|
45
|
+
// txid: '4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0',
|
|
46
|
+
// output: 0,
|
|
47
|
+
// amount: 0.75,
|
|
48
|
+
// privkey: 'Kx9'
|
|
49
|
+
// }
|
|
50
|
+
|
|
37
51
|
// Check if a URI is valid
|
|
38
52
|
console.log(isValidTxoUri(uri)) // true
|
|
39
53
|
|
|
@@ -58,6 +72,9 @@ The package includes a command-line tool that allows you to parse TXO URIs direc
|
|
|
58
72
|
# Parse a TXO URI and output the full JSON
|
|
59
73
|
txo-parser "txo:btc:4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0:0?amount=0.75"
|
|
60
74
|
|
|
75
|
+
# Parse legacy format
|
|
76
|
+
txo-parser "txo:btc:4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0:0 0.75 Kx9"
|
|
77
|
+
|
|
61
78
|
# Extract just the txid
|
|
62
79
|
txo-parser "txo:btc:4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0:0" --txid
|
|
63
80
|
|
|
@@ -74,6 +91,21 @@ If you haven't installed the package globally, you can use `npx`:
|
|
|
74
91
|
npx txo-parser "txo:btc:4e9c1ef9ba5fa3b0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0:0"
|
|
75
92
|
```
|
|
76
93
|
|
|
94
|
+
## Supported Formats
|
|
95
|
+
|
|
96
|
+
The parser supports two formats:
|
|
97
|
+
|
|
98
|
+
1. **Standard Format** (as per specification):
|
|
99
|
+
`txo:<network>:<txid>:<output>?key=value&key=value...`
|
|
100
|
+
|
|
101
|
+
2. **Legacy Format**:
|
|
102
|
+
`txo:<network>:<txid>:<output> [amount] [privkey]`
|
|
103
|
+
|
|
104
|
+
The legacy format supports up to two space-separated parameters after the basic structure:
|
|
105
|
+
|
|
106
|
+
- First parameter is interpreted as the amount
|
|
107
|
+
- Second parameter is interpreted as the private key
|
|
108
|
+
|
|
77
109
|
## API
|
|
78
110
|
|
|
79
111
|
### `parseTxoUri(uri)`
|
|
@@ -99,7 +131,7 @@ Formats a JSON object into a TXO URI string.
|
|
|
99
131
|
|
|
100
132
|
- **Parameters:**
|
|
101
133
|
- `data` (object): The data to format (must include network, txid, and output)
|
|
102
|
-
- **Returns:** Formatted TXO URI string
|
|
134
|
+
- **Returns:** Formatted TXO URI string (always in standard format)
|
|
103
135
|
- **Throws:** Error if required fields are missing or invalid
|
|
104
136
|
|
|
105
137
|
## Testing
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Implementation of the TXO URI Specification (v0.1)
|
|
4
4
|
*
|
|
5
5
|
* Format: txo:<network>:<txid>:<output>?key=value&key=value...
|
|
6
|
+
* Legacy Format: txo:<network>:<txid>:<output> [amount] [privkey]
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -16,8 +17,22 @@ export function parseTxoUri (uri) {
|
|
|
16
17
|
throw new Error('Invalid URI: URI must be a non-empty string');
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
//
|
|
20
|
-
const
|
|
20
|
+
// Check if this is using the legacy space-separated format
|
|
21
|
+
const isLegacyFormat = !uri.includes('?') && uri.split(' ').length > 1;
|
|
22
|
+
|
|
23
|
+
// Parse the basic structure first
|
|
24
|
+
let basicParts;
|
|
25
|
+
let extraParts = [];
|
|
26
|
+
|
|
27
|
+
if (isLegacyFormat) {
|
|
28
|
+
const allParts = uri.split(' ');
|
|
29
|
+
basicParts = allParts[0].split(':');
|
|
30
|
+
extraParts = allParts.slice(1);
|
|
31
|
+
} else {
|
|
32
|
+
basicParts = uri.split(':');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const [scheme, network, txid, outputAndRest] = basicParts;
|
|
21
36
|
|
|
22
37
|
// Validate scheme
|
|
23
38
|
if (scheme !== 'txo') {
|
|
@@ -41,7 +56,7 @@ export function parseTxoUri (uri) {
|
|
|
41
56
|
|
|
42
57
|
// Split the output and query string
|
|
43
58
|
let output, queryString;
|
|
44
|
-
if (outputAndRest.includes('?')) {
|
|
59
|
+
if (!isLegacyFormat && outputAndRest.includes('?')) {
|
|
45
60
|
[output, queryString] = outputAndRest.split('?');
|
|
46
61
|
} else {
|
|
47
62
|
output = outputAndRest;
|
|
@@ -80,6 +95,22 @@ export function parseTxoUri (uri) {
|
|
|
80
95
|
});
|
|
81
96
|
}
|
|
82
97
|
|
|
98
|
+
// Handle legacy format with space-separated parameters
|
|
99
|
+
if (isLegacyFormat && extraParts.length > 0) {
|
|
100
|
+
// First extra part is amount
|
|
101
|
+
if (extraParts[0]) {
|
|
102
|
+
const amount = parseFloat(extraParts[0]);
|
|
103
|
+
if (!isNaN(amount)) {
|
|
104
|
+
queryParams.amount = amount;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Second extra part is privkey
|
|
109
|
+
if (extraParts.length > 1 && extraParts[1]) {
|
|
110
|
+
queryParams.privkey = extraParts[1];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
83
114
|
// Build the result object
|
|
84
115
|
const result = {
|
|
85
116
|
network,
|