Comparing version 1.2.0 to 1.3.0
const {chanNumber} = require('./lib'); | ||
const {decodeChanId} = require('./lib'); | ||
const {encodeChanId} = require('./lib'); | ||
const {rawChanId} = require('./lib'); | ||
module.exports = {chanNumber, decodeChanId, rawChanId}; | ||
module.exports = {chanNumber, decodeChanId, encodeChanId, rawChanId}; | ||
const BN = require('bn.js'); | ||
const decBase = 10; | ||
const {decBase} = require('./constants'); | ||
const rawChanId = require('./raw_chan_id'); | ||
@@ -8,3 +9,4 @@ /** Channel id in numeric format | ||
{ | ||
id: <Channel Id Hex String> | ||
[channel]: <Channel Components String> | ||
[id]: <Channel Id Hex String> | ||
} | ||
@@ -17,9 +19,11 @@ | ||
*/ | ||
module.exports = ({id}) => { | ||
if (!id) { | ||
throw new Error('ExpectedChannelIdToConvertToNumber'); | ||
module.exports = ({channel, id}) => { | ||
if (!channel && !id) { | ||
throw new Error('ExpectedChannelIdOrComponentsToConvertToNumber'); | ||
} | ||
return {number: new BN(Buffer.from(id, 'hex')).toString(decBase)}; | ||
const rawId = id || rawChanId({channel}).id; | ||
return {number: new BN(Buffer.from(rawId, 'hex')).toString(decBase)}; | ||
}; | ||
{ | ||
"blockIndexByteLen": 4, | ||
"blockIndexOffset": 1, | ||
"chanDelimiter": "x", | ||
"decBase": 10, | ||
"endian": "be", | ||
"heightByteIndex": 0, | ||
"heightByteLen": 4, | ||
@@ -10,4 +12,4 @@ "heightByteOffset": 1, | ||
"outputIndexByteLen": 2, | ||
"rawChanIdLen": 8 | ||
"rawChanIdByteLen": 8 | ||
} | ||
const {blockIndexByteLen} = require('./constants'); | ||
const {blockIndexOffset} = require('./constants'); | ||
const {chanDelimiter} = require('./constants'); | ||
const componentsFromBuffer = require('./components_from_buffer'); | ||
@@ -17,2 +18,3 @@ const {decBase} = require('./constants'); | ||
{ | ||
[channel]: <Channel Components String> | ||
[id]: <Channel Id Hex String> | ||
@@ -32,3 +34,13 @@ [number]: <Channel Id Number Format String> | ||
*/ | ||
module.exports = ({id, number}) => { | ||
module.exports = ({channel, id, number}) => { | ||
if (!!channel) { | ||
const [height, blockIndex, outputindex] = channel.split(chanDelimiter); | ||
return { | ||
block_height: parseInt(height, decBase), | ||
block_index: parseInt(blockIndex, decBase), | ||
output_index: parseInt(outputindex, decBase), | ||
}; | ||
} | ||
if (!id && !number) { | ||
@@ -35,0 +47,0 @@ throw new Error('ExpectedShortChannelIdToDecode'); |
const chanNumber = require('./chan_number'); | ||
const decodeChanId = require('./decode_chan_id'); | ||
const encodeChanId = require('./encode_chan_id'); | ||
const rawChanId = require('./raw_chan_id'); | ||
module.exports = {chanNumber, decodeChanId, rawChanId}; | ||
module.exports = {chanNumber, decodeChanId, encodeChanId, rawChanId}; | ||
const BN = require('bn.js'); | ||
const {chanDelimiter} = require('./constants'); | ||
const {decBase} = require('./constants'); | ||
const {endian} = require('./constants'); | ||
const {rawChanIdLen} = require('./constants'); | ||
const encodeChanId = require('./encode_chan_id'); | ||
const {rawChanIdByteLen} = require('./constants'); | ||
@@ -10,3 +12,4 @@ /** Raw channel id | ||
{ | ||
number: <Channel Id In Number Format String> | ||
[channel]: <Channel Components String> | ||
[number]: <Channel Id In Number Format String> | ||
} | ||
@@ -22,11 +25,25 @@ | ||
*/ | ||
module.exports = ({number}) => { | ||
if (!number) { | ||
module.exports = ({channel, number}) => { | ||
if (!channel && !number) { | ||
throw new Error('ExpectedChannelIdInNumericFormat'); | ||
} | ||
const id = new BN(number, decBase); | ||
if (!!number) { | ||
const rawId = new BN(number, decBase); | ||
return {id: id.toArrayLike(Buffer, endian, rawChanIdLen).toString('hex')}; | ||
return { | ||
id: rawId.toArrayLike(Buffer, endian, rawChanIdByteLen).toString('hex'), | ||
}; | ||
} else { | ||
const [height, blockIndex, outputindex] = channel.split(chanDelimiter); | ||
const {id} = encodeChanId({ | ||
block_height: parseInt(height, decBase), | ||
block_index: parseInt(blockIndex, decBase), | ||
output_index: parseInt(outputindex, decBase), | ||
}); | ||
return {id}; | ||
} | ||
}; | ||
@@ -31,3 +31,3 @@ { | ||
}, | ||
"version": "1.2.0" | ||
"version": "1.3.0" | ||
} |
114
README.md
@@ -5,3 +5,115 @@ # BOLT07 | ||
Utilities for working with Lightning Network BOLT 07 | ||
Utilities for working with Lightning Network [BOLT 07](https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md) | ||
## Examples | ||
Sample code for working with bolt07 utility functions: | ||
### Formats | ||
- Channel: `x` delimited format designed to be readable. | ||
- Id: The raw channel id format specified by bolt07. | ||
- Number: Interpreting the raw channel id format as a uint64. | ||
### Chan Number | ||
const {chanNumber} = require('bolt07'); | ||
const channel = '1440743x38x0'; | ||
const id = '15fbe70000260000'; | ||
try { | ||
const fromChannel = chanNumber({channel}).number; | ||
// fromChannel === '1584113681139367936' | ||
} catch (err) { | ||
// valid channel, no error | ||
} | ||
try { | ||
const fromId = chanNumber({id}).number; | ||
// fromId === '1584113681139367936' | ||
} catch (err) { | ||
// valid id, no error | ||
} | ||
### Decode Chan Id | ||
Returns components of a channel id or channel number. | ||
const {decodeChanId} = require('bolt07'); | ||
const channel = '1440743x38x0'; | ||
const id = '15fbe70000260000'; | ||
const number = '1584113681139367936'; | ||
try { | ||
const fromChannel = decodeChanId({channel}); | ||
// fromChannel.block_height === 1440743 | ||
// fromChannel.block_index === 38 | ||
// fromChannel.output_index === 0 | ||
} catch (err) { | ||
// valid channel, no error | ||
} | ||
try { | ||
const fromId = decodeChanId({id}); | ||
// fromId.block_height === 1440743 | ||
// fromId.block_index === 38 | ||
// fromId.output_index === 0 | ||
} catch (err) { | ||
// valid id, no error | ||
} | ||
try { | ||
const fromNumber = decodeChanId({channel}); | ||
// fromNumber.block_height === 1440743 | ||
// fromNumber.block_index === 38 | ||
// fromNumber.output_index === 0 | ||
} catch (err) { | ||
// valid number, no error | ||
} | ||
### Encode Chan Id | ||
Returns channel id when components are specified | ||
const {encodeChanId} = require('bolt07'); | ||
try { | ||
const encoded = encodeChanId({ | ||
block_height: 1440743, | ||
block_index: 38, | ||
output_index: 0, | ||
}); | ||
// encoded.channel === '1440743x38x0' | ||
// encoded.id === '15fbe70000260000' | ||
// encoded.number === '1584113681139367936' | ||
} catch (err) { | ||
// valid components, no error | ||
} | ||
### Raw Chan Id | ||
Returns a raw channel id in numeric format. | ||
const {rawChanId} = require('bolt07'); | ||
const channel = '1440743x38x0'; | ||
const number = '1584113681139367936'; | ||
try { | ||
const idFromNumber = rawChanId({number}).id; | ||
// idFromNumber === '15fbe70000260000' | ||
} catch (err) { | ||
// valid number, no error | ||
} | ||
try { | ||
const idFromChannel = rawChanId({channel}).id; | ||
// idFromChannel === '15fbe70000260000' | ||
} catch (err) { | ||
// valid channel, no error | ||
} | ||
@@ -12,2 +12,7 @@ const {test} = require('tap'); | ||
{ | ||
args: {channel: '1440743x38x0'}, | ||
description: 'Standard testnet channel', | ||
expected: {number: '1584113681139367936'}, | ||
}, | ||
{ | ||
args: {id: '0832300008200001'}, | ||
@@ -17,2 +22,7 @@ description: 'Standard bitcoin channel id', | ||
}, | ||
{ | ||
args: {channel: '537136x2080x1'}, | ||
description: 'Standard bitcoin channel', | ||
expected: {number: '590587277833404417'}, | ||
}, | ||
]; | ||
@@ -19,0 +29,0 @@ |
@@ -7,9 +7,5 @@ const {test} = require('tap'); | ||
{ | ||
args: {number: '1584113681139367936'}, | ||
description: 'Standard testnet channel id', | ||
expected: { | ||
block_height: 1440743, | ||
block_index: 38, | ||
output_index: 0, | ||
}, | ||
args: {channel: '1440743x38x0'}, | ||
description: 'Standard testnet channel', | ||
expected: {block_height: 1440743, block_index: 38, output_index: 0}, | ||
}, | ||
@@ -19,26 +15,24 @@ { | ||
description: 'Standard testnet channel id', | ||
expected: { | ||
block_height: 1440743, | ||
block_index: 38, | ||
output_index: 0, | ||
}, | ||
expected: {block_height: 1440743, block_index: 38, output_index: 0}, | ||
}, | ||
{ | ||
args: {number: '590587277833404417'}, | ||
description: 'Standard bitcoin channel id', | ||
expected: { | ||
block_height: 537136, | ||
block_index: 2080, | ||
output_index: 1, | ||
}, | ||
args: {number: '1584113681139367936'}, | ||
description: 'Standard testnet channel id', | ||
expected: {block_height: 1440743, block_index: 38, output_index: 0}, | ||
}, | ||
{ | ||
args: {channel: '537136x2080x1'}, | ||
description: 'Standard bitcoin channel', | ||
expected: {block_height: 537136, block_index: 2080, output_index: 1}, | ||
}, | ||
{ | ||
args: {id: '0832300008200001'}, | ||
description: 'Standard bitcoin channel id', | ||
expected: { | ||
block_height: 537136, | ||
block_index: 2080, | ||
output_index: 1, | ||
}, | ||
expected: {block_height: 537136, block_index: 2080, output_index: 1}, | ||
}, | ||
{ | ||
args: {number: '590587277833404417'}, | ||
description: 'Standard bitcoin channel id', | ||
expected: {block_height: 537136, block_index: 2080, output_index: 1}, | ||
}, | ||
]; | ||
@@ -45,0 +39,0 @@ |
@@ -7,2 +7,7 @@ const {test} = require('tap'); | ||
{ | ||
args: {channel: '1440743x38x0'}, | ||
description: 'Standard testnet channel', | ||
expected: {id: '15fbe70000260000'}, | ||
}, | ||
{ | ||
args: {number: '1584113681139367936'}, | ||
@@ -13,2 +18,7 @@ description: 'Standard testnet channel id', | ||
{ | ||
args: {channel: '537136x2080x1'}, | ||
description: 'Standard bitcoin channel id', | ||
expected: {id: '0832300008200001'}, | ||
}, | ||
{ | ||
args: {number: '590587277833404417'}, | ||
@@ -15,0 +25,0 @@ description: 'Standard bitcoin channel id', |
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
17158
16
372
119