-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Nesopie/feat/hybrid
feat: add cjs and esm modules
- Loading branch information
Showing
7 changed files
with
6,801 additions
and
8,633 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
.nyc_output | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.