Skip to content

Instantly share code, notes, and snippets.

@matthewhudson
Last active March 20, 2018 11:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewhudson/7999278 to your computer and use it in GitHub Desktop.
Save matthewhudson/7999278 to your computer and use it in GitHub Desktop.
Parse Bitcoin URL in JavaScript
/* Parse bitcoin URL query keys. */
function parseBitcoinURL(url) {
var r = /^bitcoin:([a-zA-Z0-9]{27,34})(?:\?(.*))?$/;
var match = r.exec(url);
if (!match) return null;
var parsed = { url: url }
if (match[2]) {
var queries = match[2].split('&');
for (var i = 0; i < queries.length; i++) {
var query = queries[i].split('=');
if (query.length == 2) {
parsed[query[0]] = decodeURIComponent(query[1].replace(/\+/g, '%20'));
}
}
}
parsed.address = match[1];
return parsed;
}
@jprichardson
Copy link

Cool. Why not just do:

parsed[query[0]] = decodeURIComponent(query[1]);

instead of:

parsed[query[0]] = decodeURIComponent(query[1].replace(/\+/g, '%20'));

? I mean... is there a realistic scenario with query params that have +? BIP 21 doesn't mention anything about converting them to spaces. So I must be missing something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment