Skip to content

Commit

Permalink
Merge pull request #19 from Nesopie/feat/hybrid
Browse files Browse the repository at this point in the history
feat: add cjs and esm modules
  • Loading branch information
junderw authored Jul 20, 2024
2 parents b42ceb2 + 609410e commit 96ea044
Show file tree
Hide file tree
Showing 7 changed files with 6,801 additions and 8,633 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.nyc_output
coverage
44 changes: 44 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki
// bitcoin:<address>[?amount=<amount>][?label=<label>][?message=<message>]

const querystring = require('query-string').default

function decode (uri, urnScheme) {
urnScheme = urnScheme || 'bitcoin'

const urnSchemeActual = uri.slice(0, urnScheme.length).toLowerCase()
if (urnSchemeActual !== urnScheme ||
uri.charAt(urnScheme.length) !== ':'
) throw new Error('Invalid BIP21 URI: ' + uri)

const split = uri.indexOf('?')
const address = uri.slice(urnScheme.length + 1, split === -1 ? undefined : split)
const query = split === -1 ? '' : uri.slice(split + 1)
const options = querystring.parse(query)

if (options.amount) {
options.amount = Number(options.amount)
if (!isFinite(options.amount)) throw new Error('Invalid amount')
if (options.amount < 0) throw new Error('Invalid amount')
}

return { address, options }
}

function encode (address, options, urnScheme) {
options = options || {}
const scheme = urnScheme || 'bitcoin'
const query = querystring.stringify(options)

if (options.amount) {
if (!isFinite(options.amount)) throw new TypeError('Invalid amount')
if (options.amount < 0) throw new TypeError('Invalid amount')
}

return scheme + ':' + address + (query ? '?' : '') + query
}

module.exports = {
decode,
encode
}
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type Bip21Options = {
amount?: number;
label?: string;
message?: string;
}
export function decode(uri: string, urnScheme?: string): {
address: string;
options: Bip21Options
};
export function encode(address: string, options?: Bip21Options, urnScheme?: string): string;
15 changes: 5 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki
// bitcoin:<address>[?amount=<amount>][?label=<label>][?message=<message>]

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

function decode (uri, urnScheme) {
export function decode (uri, urnScheme) {
urnScheme = urnScheme || 'bitcoin'
const urnSchemeActual = uri.slice(0, urnScheme.length).toLowerCase()
if (urnSchemeActual !== urnScheme ||
Expand All @@ -13,7 +13,7 @@ function decode (uri, urnScheme) {
const split = uri.indexOf('?')
const address = uri.slice(urnScheme.length + 1, split === -1 ? undefined : split)
const query = split === -1 ? '' : uri.slice(split + 1)
const options = qs.parse(query)
const options = querystring.parse(query)

if (options.amount) {
options.amount = Number(options.amount)
Expand All @@ -24,10 +24,10 @@ function decode (uri, urnScheme) {
return { address, options }
}

function encode (address, options, urnScheme) {
export function encode (address, options, urnScheme) {
options = options || {}
const scheme = urnScheme || 'bitcoin'
const query = qs.stringify(options)
const query = querystring.stringify(options)

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

return scheme + ':' + address + (query ? '?' : '') + query
}

module.exports = {
decode,
encode
}
Loading

0 comments on commit 96ea044

Please sign in to comment.