Comparing version
39
index.js
@@ -1,11 +0,44 @@ | ||
// let Buffer = require('safe-buffer').Buffer | ||
// see https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki#compatibility | ||
function decode () { | ||
const SEQUENCE_FINAL = 0xffffffff | ||
const SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31) | ||
const SEQUENCE_LOCKTIME_GRANULARITY = 9 | ||
const SEQUENCE_LOCKTIME_MASK = 0x0000ffff | ||
const SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22) | ||
const BLOCKS_MAX = SEQUENCE_LOCKTIME_MASK | ||
const SECONDS_MOD = 1 << SEQUENCE_LOCKTIME_GRANULARITY | ||
const SECONDS_MAX = SEQUENCE_LOCKTIME_MASK << SEQUENCE_LOCKTIME_GRANULARITY | ||
function decode (sequence) { | ||
if (sequence & SEQUENCE_LOCKTIME_DISABLE_FLAG) return {} | ||
if (sequence & SEQUENCE_LOCKTIME_TYPE_FLAG) { | ||
return { | ||
seconds: (sequence & SEQUENCE_LOCKTIME_MASK) << SEQUENCE_LOCKTIME_GRANULARITY | ||
} | ||
} | ||
return { | ||
blocks: sequence & SEQUENCE_LOCKTIME_MASK | ||
} | ||
} | ||
function encode () { | ||
function encode ({ blocks, seconds }) { | ||
if (blocks !== undefined && seconds !== undefined) throw new TypeError('Cannot encode blocks AND seconds') | ||
if (blocks === undefined && seconds === undefined) return SEQUENCE_FINAL // neither? assume final | ||
if (seconds !== undefined) { | ||
if (typeof seconds !== 'number') throw new TypeError('Expected Number seconds') | ||
if (seconds > SECONDS_MAX) throw new TypeError('Expected Number seconds <= ' + SECONDS_MAX) | ||
if (seconds % SECONDS_MOD !== 0) throw new TypeError('Expected Number seconds as a multiple of ' + SECONDS_MOD) | ||
return SEQUENCE_LOCKTIME_TYPE_FLAG | (seconds >> SEQUENCE_LOCKTIME_GRANULARITY) | ||
} | ||
if (typeof blocks !== 'number') throw new TypeError('Expected Number blocks') | ||
if (blocks > SEQUENCE_LOCKTIME_MASK) throw new TypeError('Expected Number blocks <= ' + BLOCKS_MAX) | ||
return blocks | ||
} | ||
module.exports = { decode, encode } |
{ | ||
"name": "bip68", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Relative lock-time using consensus-enforced sequence numbers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -6,3 +6,3 @@ # bip68 | ||
Relative lock-time using consensus-enforced sequence numbers. | ||
A [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki) relative lock-time encoding library. | ||
@@ -9,0 +9,0 @@ |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
3856
70.85%34
466.67%0
-100%