Skip to content

Commit 96ea044

Browse files
authored
Merge pull request #19 from Nesopie/feat/hybrid
feat: add cjs and esm modules
2 parents b42ceb2 + 609410e commit 96ea044

File tree

7 files changed

+6801
-8633
lines changed

7 files changed

+6801
-8633
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
.nyc_output
3+
coverage

index.cjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki
2+
// bitcoin:<address>[?amount=<amount>][?label=<label>][?message=<message>]
3+
4+
const querystring = require('query-string').default
5+
6+
function decode (uri, urnScheme) {
7+
urnScheme = urnScheme || 'bitcoin'
8+
9+
const urnSchemeActual = uri.slice(0, urnScheme.length).toLowerCase()
10+
if (urnSchemeActual !== urnScheme ||
11+
uri.charAt(urnScheme.length) !== ':'
12+
) throw new Error('Invalid BIP21 URI: ' + uri)
13+
14+
const split = uri.indexOf('?')
15+
const address = uri.slice(urnScheme.length + 1, split === -1 ? undefined : split)
16+
const query = split === -1 ? '' : uri.slice(split + 1)
17+
const options = querystring.parse(query)
18+
19+
if (options.amount) {
20+
options.amount = Number(options.amount)
21+
if (!isFinite(options.amount)) throw new Error('Invalid amount')
22+
if (options.amount < 0) throw new Error('Invalid amount')
23+
}
24+
25+
return { address, options }
26+
}
27+
28+
function encode (address, options, urnScheme) {
29+
options = options || {}
30+
const scheme = urnScheme || 'bitcoin'
31+
const query = querystring.stringify(options)
32+
33+
if (options.amount) {
34+
if (!isFinite(options.amount)) throw new TypeError('Invalid amount')
35+
if (options.amount < 0) throw new TypeError('Invalid amount')
36+
}
37+
38+
return scheme + ':' + address + (query ? '?' : '') + query
39+
}
40+
41+
module.exports = {
42+
decode,
43+
encode
44+
}

index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export type Bip21Options = {
2+
amount?: number;
3+
label?: string;
4+
message?: string;
5+
}
6+
export function decode(uri: string, urnScheme?: string): {
7+
address: string;
8+
options: Bip21Options
9+
};
10+
export function encode(address: string, options?: Bip21Options, urnScheme?: string): string;

index.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki
22
// bitcoin:<address>[?amount=<amount>][?label=<label>][?message=<message>]
33

4-
const qs = require('qs')
4+
import querystring from 'query-string'
55

6-
function decode (uri, urnScheme) {
6+
export function decode (uri, urnScheme) {
77
urnScheme = urnScheme || 'bitcoin'
88
const urnSchemeActual = uri.slice(0, urnScheme.length).toLowerCase()
99
if (urnSchemeActual !== urnScheme ||
@@ -13,7 +13,7 @@ function decode (uri, urnScheme) {
1313
const split = uri.indexOf('?')
1414
const address = uri.slice(urnScheme.length + 1, split === -1 ? undefined : split)
1515
const query = split === -1 ? '' : uri.slice(split + 1)
16-
const options = qs.parse(query)
16+
const options = querystring.parse(query)
1717

1818
if (options.amount) {
1919
options.amount = Number(options.amount)
@@ -24,10 +24,10 @@ function decode (uri, urnScheme) {
2424
return { address, options }
2525
}
2626

27-
function encode (address, options, urnScheme) {
27+
export function encode (address, options, urnScheme) {
2828
options = options || {}
2929
const scheme = urnScheme || 'bitcoin'
30-
const query = qs.stringify(options)
30+
const query = querystring.stringify(options)
3131

3232
if (options.amount) {
3333
if (!isFinite(options.amount)) throw new TypeError('Invalid amount')
@@ -36,8 +36,3 @@ function encode (address, options, urnScheme) {
3636

3737
return scheme + ':' + address + (query ? '?' : '') + query
3838
}
39-
40-
module.exports = {
41-
decode,
42-
encode
43-
}

0 commit comments

Comments
 (0)