Skip to content

Commit

Permalink
fix(wallet): support uppercase payment requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton authored and korhaliv committed Jan 9, 2019
1 parent 1f99e4f commit 440aa19
Showing 1 changed file with 18 additions and 37 deletions.
55 changes: 18 additions & 37 deletions app/lib/utils/crypto.js
@@ -1,5 +1,5 @@
import get from 'lodash.get'
import bitcoin from 'bitcoinjs-lib'
import bech32 from 'lib/utils/bech32'
import lightningRequestReq from 'bolt11'
import coininfo from 'coininfo'

Expand All @@ -15,6 +15,18 @@ export const networks = {
}
}

export const coinTypes = {
bitcoin: {
mainnet: 'bitcoin',
testnet: 'testnet',
regtest: 'regtest'
},
litecoin: {
mainnet: 'litecoin',
testnet: 'litecoin_testnet'
}
}

export const decodePayReq = (payReq, addDefaults = true) => {
const data = lightningRequestReq.decode(payReq)
const expiry = data.tags.find(t => t.tagName === 'expire_time')
Expand Down Expand Up @@ -107,45 +119,14 @@ export const isOnchain = (input, chain, network) => {
* @return {Boolean} boolean indicating wether the address is a lightning address.
*/
export const isLn = (input, chain = 'bitcoin', network = 'mainnet') => {
if (typeof input !== 'string') {
if (!input || typeof input !== 'string') {
return false
}

let prefix = 'ln'
// Prefixes come from SLIP-0173
// See https://github.com/satoshilabs/slips/blob/master/slip-0173.md
if (chain === 'bitcoin') {
switch (network) {
case 'mainnet':
prefix = 'lnbc'
break
case 'testnet':
prefix = 'lntb'
break
case 'regtest':
prefix = 'lnbcrt'
break
}
} else if (chain === 'litecoin') {
switch (network) {
case 'mainnet':
prefix = 'lnltc'
break
case 'testnet':
prefix = 'lntltc'
break
case 'regtest':
prefix = 'lnrltc'
break
}
}

if (!input.startsWith(prefix)) {
return false
}

try {
bech32.decode(input)
const decoded = lightningRequestReq.decode(input)
if (decoded.coinType !== get(coinTypes, `${chain}.${network}`)) {
throw new Error('Invalid coin type')
}
return true
} catch (e) {
return false
Expand Down

0 comments on commit 440aa19

Please sign in to comment.