bip-321 0.0.3 → 0.0.5
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/LICENSE +21 -0
- package/README.md +77 -12
- package/bun.lock +467 -11
- package/eslint.config.js +94 -0
- package/example.ts +3 -3
- package/index.test.ts +247 -41
- package/index.ts +189 -23
- package/package.json +8 -4
- package/oxlintrc.json +0 -23
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Nitesh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ A TypeScript/JavaScript library for parsing BIP-321 Bitcoin URI scheme. This lib
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- ✅ **Complete BIP-321 compliance** - Implements the full BIP-321 specification
|
|
8
|
-
- ✅ **Multiple payment methods** - Supports on-chain, Lightning (BOLT11), BOLT12 offers,
|
|
8
|
+
- ✅ **Multiple payment methods** - Supports on-chain, Lightning (BOLT11), BOLT12 offers, silent payments, and Ark
|
|
9
9
|
- ✅ **Network detection** - Automatically detects mainnet, testnet, regtest, and signet networks
|
|
10
10
|
- ✅ **Address validation** - Validates Bitcoin addresses (P2PKH, P2SH, Segwit v0, Taproot)
|
|
11
11
|
- ✅ **Lightning invoice validation** - Validates BOLT11 Lightning invoices
|
|
@@ -21,7 +21,7 @@ result.paymentMethods; // PaymentMethod[]
|
|
|
21
21
|
result.errors; // string[]
|
|
22
22
|
|
|
23
23
|
result.paymentMethods.forEach((method: PaymentMethod) => {
|
|
24
|
-
method.type; // "onchain" | "lightning" | "
|
|
24
|
+
method.type; // "onchain" | "lightning" | "offer" | "silent-payment" | "ark"
|
|
25
25
|
method.network; // "mainnet" | "testnet" | "regtest" | "signet" | undefined
|
|
26
26
|
method.valid; // boolean
|
|
27
27
|
});
|
|
@@ -105,6 +105,49 @@ console.log(result.paymentMethods[0].type); // "lightning"
|
|
|
105
105
|
console.log(result.paymentMethods[0].network); // "mainnet"
|
|
106
106
|
```
|
|
107
107
|
|
|
108
|
+
### Ark Payment
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
// Mainnet Ark address
|
|
112
|
+
const result = parseBIP321(
|
|
113
|
+
"bitcoin:?ark=ark1pwh9vsmezqqpjy9akejayl2vvcse6he97rn40g84xrlvrlnhayuuyefrp9nse2yspqqjl5wpy"
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
console.log(result.paymentMethods[0].type); // "ark"
|
|
117
|
+
console.log(result.paymentMethods[0].network); // "mainnet"
|
|
118
|
+
|
|
119
|
+
// Testnet Ark address
|
|
120
|
+
const testnetResult = parseBIP321(
|
|
121
|
+
"bitcoin:?ark=tark1pm6sr0fpzqqpnzzwxf209kju4qavs4gtumxk30yv2u5ncrvtp72z34axcvrydtdqpqq5838km"
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
console.log(testnetResult.paymentMethods[0].network); // "testnet"
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Network Validation
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
// Ensure all payment methods are mainnet
|
|
131
|
+
const result = parseBIP321(
|
|
132
|
+
"bitcoin:bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq?lightning=lnbc...",
|
|
133
|
+
"mainnet"
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
if (result.valid) {
|
|
137
|
+
// All payment methods are guaranteed to be mainnet
|
|
138
|
+
console.log("All payment methods are mainnet");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Reject testnet addresses when expecting mainnet
|
|
142
|
+
const invalid = parseBIP321(
|
|
143
|
+
"bitcoin:tb1qghfhmd4zh7ncpmxl3qzhmq566jk8ckq4gafnmg",
|
|
144
|
+
"mainnet"
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
console.log(invalid.valid); // false
|
|
148
|
+
console.log(invalid.errors); // ["Payment method network mismatch..."]
|
|
149
|
+
```
|
|
150
|
+
|
|
108
151
|
### Multiple Payment Methods
|
|
109
152
|
|
|
110
153
|
```typescript
|
|
@@ -112,7 +155,7 @@ const result = parseBIP321(
|
|
|
112
155
|
"bitcoin:?lightning=lnbc...&lno=lno1bogusoffer&sp=sp1qsilentpayment"
|
|
113
156
|
);
|
|
114
157
|
|
|
115
|
-
// Returns 3 payment methods: lightning,
|
|
158
|
+
// Returns 3 payment methods: lightning, offer (BOLT12), and silent-payment
|
|
116
159
|
console.log(`Total payment methods: ${result.paymentMethods.length}`);
|
|
117
160
|
```
|
|
118
161
|
|
|
@@ -142,12 +185,28 @@ if (!result.valid) {
|
|
|
142
185
|
|
|
143
186
|
## API Reference
|
|
144
187
|
|
|
145
|
-
### `parseBIP321(uri: string): BIP321ParseResult`
|
|
188
|
+
### `parseBIP321(uri: string, expectedNetwork?: "mainnet" | "testnet" | "regtest" | "signet"): BIP321ParseResult`
|
|
146
189
|
|
|
147
190
|
Parses a BIP-321 URI and returns detailed information about the payment request.
|
|
148
191
|
|
|
149
192
|
**Parameters:**
|
|
150
193
|
- `uri` - The Bitcoin URI string to parse
|
|
194
|
+
- `expectedNetwork` (optional) - Expected network for all payment methods. If specified, all payment methods must match this network or the URI will be marked invalid.
|
|
195
|
+
</text>
|
|
196
|
+
|
|
197
|
+
<old_text line=240>
|
|
198
|
+
## Validation Rules
|
|
199
|
+
|
|
200
|
+
The parser enforces BIP-321 validation rules:
|
|
201
|
+
|
|
202
|
+
1. ✅ URI must start with `bitcoin:` (case-insensitive)
|
|
203
|
+
2. ✅ Address in URI path must be valid or empty
|
|
204
|
+
3. ✅ `amount` must be decimal BTC (no commas)
|
|
205
|
+
4. ✅ `label`, `message`, and `amount` cannot appear multiple times
|
|
206
|
+
5. ✅ `pop` and `req-pop` cannot both be present
|
|
207
|
+
6. ✅ Required parameters (`req-*`) must be understood or URI is invalid
|
|
208
|
+
7. ✅ Network-specific parameters (`bc`, `tb`, etc.) must match address network
|
|
209
|
+
8. ✅ `pop` URI scheme must not be forbidden (http, https, file, javascript, mailto)
|
|
151
210
|
|
|
152
211
|
**Returns:** `BIP321ParseResult` object containing:
|
|
153
212
|
|
|
@@ -181,7 +240,7 @@ interface BIP321ParseResult {
|
|
|
181
240
|
|
|
182
241
|
```typescript
|
|
183
242
|
interface PaymentMethod {
|
|
184
|
-
type: "onchain" | "lightning" | "
|
|
243
|
+
type: "onchain" | "lightning" | "offer" | "silent-payment" | "ark";
|
|
185
244
|
value: string; // The actual address/invoice value
|
|
186
245
|
network?: "mainnet" | "testnet" | "regtest" | "signet";
|
|
187
246
|
valid: boolean; // Whether this payment method is valid
|
|
@@ -231,8 +290,9 @@ console.log(summary);
|
|
|
231
290
|
|--------|--------------|-------------|
|
|
232
291
|
| On-chain | `address` or `bc`/`tb`/`bcrt`/`tbs` | Bitcoin addresses (P2PKH, P2SH, Segwit, Taproot) |
|
|
233
292
|
| Lightning | `lightning` | BOLT11 Lightning invoices |
|
|
234
|
-
| BOLT12 | `lno` | Lightning BOLT12 offers |
|
|
293
|
+
| BOLT12 Offer | `lno` | Lightning BOLT12 offers |
|
|
235
294
|
| Silent Payments | `sp` | BIP352 Silent Payment addresses |
|
|
295
|
+
| Ark | `ark` | Ark addresses (mainnet: `ark1...`, testnet: `tark1...`) |
|
|
236
296
|
|
|
237
297
|
## Network Detection
|
|
238
298
|
|
|
@@ -249,6 +309,10 @@ The library automatically detects the network from:
|
|
|
249
309
|
- **Regtest**: `lnbcrt...`
|
|
250
310
|
- **Signet**: `lntbs...`
|
|
251
311
|
|
|
312
|
+
### Ark Addresses
|
|
313
|
+
- **Mainnet**: `ark1...`
|
|
314
|
+
- **Testnet**: `tark1...`
|
|
315
|
+
|
|
252
316
|
## Validation Rules
|
|
253
317
|
|
|
254
318
|
The parser enforces BIP-321 validation rules:
|
|
@@ -261,6 +325,7 @@ The parser enforces BIP-321 validation rules:
|
|
|
261
325
|
6. ✅ Required parameters (`req-*`) must be understood or URI is invalid
|
|
262
326
|
7. ✅ Network-specific parameters (`bc`, `tb`, etc.) must match address network
|
|
263
327
|
8. ✅ `pop` URI scheme must not be forbidden (http, https, file, javascript, mailto)
|
|
328
|
+
9. ✅ If `expectedNetwork` is specified, all payment methods must match that network
|
|
264
329
|
|
|
265
330
|
## Browser Usage
|
|
266
331
|
|
|
@@ -307,15 +372,15 @@ function parseQRCode(data: string) {
|
|
|
307
372
|
}
|
|
308
373
|
```
|
|
309
374
|
|
|
310
|
-
## Contributing
|
|
311
|
-
|
|
312
|
-
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
313
|
-
|
|
314
375
|
## License
|
|
315
376
|
|
|
316
|
-
|
|
377
|
+
MIT
|
|
317
378
|
|
|
318
379
|
## Related
|
|
319
380
|
|
|
320
|
-
- [BIP-321 Specification](https://
|
|
381
|
+
- [BIP-321 Specification](https://github.com/bitcoin/bips/blob/master/bip-0321.mediawiki)
|
|
321
382
|
- [BIP-21 (Original)](https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki)
|
|
383
|
+
- [BIP-352 Silent Payments](https://github.com/bitcoin/bips/blob/master/bip-0352.mediawiki)
|
|
384
|
+
- [BOAT-0001 Ark Address Format](https://github.com/ark-protocol/boats/blob/master/boat-0001.md)
|
|
385
|
+
- [BOLT11 Lightning Invoices](https://github.com/lightning/bolts/blob/master/11-payment-encoding.md)
|
|
386
|
+
- [BOLT12 Lightning Offers](https://github.com/lightning/bolts/blob/master/12-offer-encoding.md)
|