Compare commits

..

7 Commits

Author SHA1 Message Date
Jonathan Underwood
2f7e81af48
Merge pull request #28 from Sword-Smith/patch-1
Add millisatoshis to documentation.
2019-11-01 01:07:37 +09:00
Thorkil Værge
4cc4c418c1
Add millisatoshis to documentation. 2019-10-31 12:59:18 +01:00
Jonathan Underwood
36cbbedfd6
Merge pull request #23 from bitcoinjs/dependabot/npm_and_yarn/lodash-4.17.13
Bump lodash from 4.17.11 to 4.17.13
2019-07-12 20:17:00 +09:00
dependabot[bot]
1bc481dbc2
Bump lodash from 4.17.11 to 4.17.13
Bumps [lodash](https://github.com/lodash/lodash) from 4.17.11 to 4.17.13.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](https://github.com/lodash/lodash/compare/4.17.11...4.17.13)

Signed-off-by: dependabot[bot] <support@github.com>
2019-07-12 10:12:10 +00:00
Jonathan Underwood
ce49dbb0f6
Merge pull request #22 from bitcoinjs/addNetworkParam
Add ability to decode any network type wanted
2019-07-07 11:18:29 +09:00
junderw
9033eba9d8
Add ability to decode any network type wanted 2019-07-07 11:14:03 +09:00
Jonathan Underwood
1462d1203e
Merge pull request #21 from bitcoinjs/fixType
Type can also be null
2019-07-07 11:05:00 +09:00
6 changed files with 38 additions and 12 deletions

View File

@ -22,6 +22,7 @@ var decoded = lightningPayReq.decode('lnbc20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zw
{
"coinType": "bitcoin",
"complete": true,
"millisatoshi": "2000000000",
"satoshis": 2000000,
"payeeNodeKey": "03e7156ae33b0a208d0744199163177e909e80176e55d97a2f221ede0f934dd9ad",
"paymentRequest": "lnbc20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqfppqw508d6qejxtdg4y5r3zarvary0c5xw7kepvrhrm9s57hejg0p662ur5j5cr03890fa7k2pypgttmh4897d3raaq85a293e9jpuqwl0rnfuwzam7yr8e690nd2ypcq9hlkdwdvycqa0qza8",
@ -53,6 +54,9 @@ var decoded = lightningPayReq.decode('lnbc20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zw
*/
```
### Warning
The `"satoshis"` field will only be set if the invoice is for a whole number of satoshis. If it is in a fractional number of satoshis, the `"millisatoshis"` field must be used. 1000 millisatoshis is 1 satoshi.
### Encoding
* MINIMUM NEED: `privateKey` and one `payment_hash` tag as well as one `description`
* (`timestamp` defaults to current time, `description` defaults to empty string,

8
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "bolt11",
"version": "1.2.5",
"version": "1.2.6",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -1480,9 +1480,9 @@
}
},
"lodash": {
"version": "4.17.11",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz",
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg=="
"version": "4.17.13",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.13.tgz",
"integrity": "sha512-vm3/XWXfWtRua0FkUyEHBZy8kCPjErNBT9fJx8Zvs+U6zjqPbTUOpkaoum3O5uiA8sm+yNMHXfYkTUHFoMxFNA=="
},
"loose-envify": {
"version": "1.4.0",

View File

@ -1,6 +1,6 @@
{
"name": "bolt11",
"version": "1.2.5",
"version": "1.2.6",
"description": "A library for encoding and decoding lightning network payment requests as defined in [BOLT #11](https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md).",
"main": "payreq.js",
"types": "payreq.d.ts",

6
payreq.d.ts vendored
View File

@ -12,6 +12,10 @@ type FallbackAddress = {
address: string;
addressHash: string;
};
type Network = {
[index: string]: any;
bech32: string;
};
// Start exports
export declare type TagData = string | number | RoutingInfo | FallbackAddress;
@ -36,7 +40,7 @@ export declare type PaymentRequestObject = {
}>;
};
export declare function encode(inputData: PaymentRequestObject, addDefaults?: boolean): PaymentRequestObject;
export declare function decode(paymentRequest: string): PaymentRequestObject;
export declare function decode(paymentRequest: string, network?: Network): PaymentRequestObject;
export declare function sign(inputPayReqObj: PaymentRequestObject, inputPrivateKey: string | Buffer): PaymentRequestObject;
export declare function satToHrp(satoshis: number | string): string;
export declare function millisatToHrp(millisatoshis: number | string): string;

View File

@ -720,7 +720,7 @@ function encode (inputData, addDefaults) {
// decode will only have extra comments that aren't covered in encode comments.
// also if anything is hard to read I'll comment.
function decode (paymentRequest) {
function decode (paymentRequest, network) {
if (typeof paymentRequest !== 'string') throw new Error('Lightning Payment Request must be string')
if (paymentRequest.slice(0, 2).toLowerCase() !== 'ln') throw new Error('Not a proper lightning payment request')
let decoded = bech32.decode(paymentRequest, Number.MAX_SAFE_INTEGER)
@ -755,12 +755,16 @@ function decode (paymentRequest) {
throw new Error('Not a proper lightning payment request')
}
let coinType = prefixMatches[1]
let coinNetwork
if (BECH32CODES[coinType]) {
coinType = BECH32CODES[coinType]
let bech32Prefix = prefixMatches[1]
let coinNetwork, coinType
if (BECH32CODES[bech32Prefix]) {
coinType = BECH32CODES[bech32Prefix]
coinNetwork = BITCOINJS_NETWORK_INFO[coinType]
} else {
} else if (network && network.bech32) {
coinType = 'unknown'
coinNetwork = network
}
if (!coinNetwork || coinNetwork.bech32 !== bech32Prefix) {
throw new Error('Unknown coin bech32 prefix')
}

View File

@ -237,3 +237,17 @@ tape(`can decode and encode payment request containing unknown tags`, (t) => {
t.end()
})
tape(`can decode unknown network payment request`, (t) => {
const network = { bech32: 'sb' }
let decoded = lnpayreq.decode(
'lnsb1u1pwslkj8pp52u27w39645j24a0zfxnwytshxserjchdqt8nz8uwv9fp8wasxrhsdq' +
'l2pkxz7tfdenjqum0w4hxggrgv4kxj7qcqzpgnvqq8t63nxmgha5945s633fdd3p5x9k889' +
'g6p02qsghx4vrgqgr3xzz3hgld8r84ellwgz3teexvqzwlxj7lgkhl8xh2p7dstq0fgsspa' +
'5ldq6',
network
)
t.ok(decoded.complete === true)
t.ok(decoded.coinType === 'unknown')
t.end()
})