Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cjs and esm modules #19

Merged
merged 4 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading