@synonymdev/lightning-channel-id
Advanced tools
Comparing version 0.0.2 to 0.0.3
27
index.js
@@ -18,5 +18,4 @@ 'use strict' | ||
*/ | ||
function clnToLnd(chanId, marker){ | ||
if(typeof chanId !== "string") throw new Error("Channel id must be string") | ||
const chid = chanId.split(marker || ":") | ||
function fromCLN(chanId, marker){ | ||
const chid = chanId.split(marker) | ||
if(chid.length !== 3) throw new Error("Invalid channel id passed") | ||
@@ -27,3 +26,3 @@ const block = long.fromString(chid[0]).shiftLeft(40) | ||
const lnd = block.or(tx).or(output) | ||
const fmt = lndToCln(lnd.toString()) | ||
const fmt = fromLND(lnd.toString()) | ||
fmt.cln_format = chanId | ||
@@ -38,3 +37,3 @@ return fmt | ||
*/ | ||
function lndToCln(chanId){ | ||
function fromLND(chanId){ | ||
const block = long.fromString(chanId).shiftRight(40) | ||
@@ -53,5 +52,17 @@ const tx = long.fromString(chanId).shiftRight(16).and(0xFFFFFF) | ||
module.exports = { | ||
clnToLnd, | ||
lndToCln, | ||
function parseChannelId (chanId, marker){ | ||
if(typeof chanId !== "string") throw new Error("Invalid chan id") | ||
chanId = chanId.toLowerCase() | ||
if(chanId.includes("x")){ | ||
marker = "x" | ||
} | ||
if(chanId.includes(":")){ | ||
marker = ":" | ||
} | ||
if(!marker) return fromLND(chanId) | ||
return fromCLN(chanId, marker) | ||
} | ||
module.exports = parseChannelId |
{ | ||
"name": "@synonymdev/lightning-channel-id", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Convert lightning channel id formats", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,8 +8,10 @@ # lightning-channel-id-js | ||
npm install lightning-channel-js | ||
npm install @synonymdev/lightning-channel-id | ||
const {clnToLnd, lndToCln} = require("lightning-channel-js") | ||
const parseChannelId = require("lightning-channel-js") | ||
lndToCln("807896954914930688") | ||
clnToLnd("734778:1235:0") | ||
parseChannelId("807896954914930688") // long channel id format | ||
parseChannelId("734778:1235:0") // : seperated | ||
parseChannelId("734778x1235x0") // x seperated | ||
parseChannelId("734778$1235$0","$) // custom marker | ||
@@ -26,22 +28,1 @@ // All of the above should output same result | ||
``` | ||
### Functions | ||
### `lndToCln(channelId : String)` | ||
Converts from LND format to CLN format | ||
### `clnToLnd(channelId : String, marker: String)` | ||
Converts from CLN to LND format. | ||
Accepts an optional `marker` string, which is used incase your channel format has a different seperator | ||
``` | ||
clnToLnd("734778x1235x0","x") | ||
{ | ||
block: '734778', | ||
tx: '1235', | ||
output: '0', | ||
lnd_format: '807896954914930688', | ||
cln_format: '734778x1235x0' | ||
} | ||
``` |
/* eslint-env mocha */ | ||
'use strict' | ||
const assert = require('assert') | ||
const {clnToLnd, lndToCln} = require("../index") | ||
const parseChannelId = require("../index") | ||
@@ -9,3 +9,4 @@ describe('Channel format converter', () => { | ||
it("should convert CLN format to LND format",()=>{ | ||
const scid = clnToLnd("734778:1235:0") | ||
const scid = parseChannelId("734778:1235:0") | ||
console.log(scid) | ||
assert(scid.lnd_format === "807896954914930688") | ||
@@ -20,3 +21,3 @@ assert(scid.tx === "1235") | ||
const scid = clnToLnd("734778x1235x0","x") | ||
const scid = parseChannelId("734778x1235x0") | ||
assert(scid.lnd_format === "807896954914930688") | ||
@@ -30,3 +31,3 @@ assert(scid.tx === "1235") | ||
it("should convert LND format to CLN format",()=>{ | ||
const scid = lndToCln("770826920361984001") | ||
const scid = parseChannelId("770826920361984001") | ||
assert(scid.lnd_format === "770826920361984001") | ||
@@ -33,0 +34,0 @@ assert(scid.tx === "892") |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84
4934
27