Removes redundant convert functions
This commit is contained in:
parent
a8cf2fdd9e
commit
4716eb29bf
4 changed files with 16 additions and 128 deletions
src
|
@ -24,15 +24,6 @@ function hexToBytes(hex) {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a byte array representing a number with the given length
|
||||
*/
|
||||
function numToBytes(num, bytes) {
|
||||
if (bytes === undefined) bytes = 8
|
||||
if (bytes === 0) return []
|
||||
return [num % 256].concat(numToBytes(Math.floor(num / 256), bytes - 1))
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a byte array to the number that it represents
|
||||
*/
|
||||
|
@ -41,42 +32,6 @@ function bytesToNum(bytes) {
|
|||
return bytes[0] + 256 * bytesToNum(bytes.slice(1))
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn an integer into a "var_int".
|
||||
*
|
||||
* "var_int" is a variable length integer used by Bitcoin's binary format.
|
||||
*
|
||||
* Returns a byte array.
|
||||
*/
|
||||
function numToVarInt(num) {
|
||||
if (num < 253) return [num]
|
||||
if (num < 65536) return [253].concat(numToBytes(num, 2))
|
||||
if (num < 4294967296) return [254].concat(numToBytes(num, 4))
|
||||
return [255].concat(numToBytes(num, 8))
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn an VarInt into an integer
|
||||
*
|
||||
* "var_int" is a variable length integer used by Bitcoin's binary format.
|
||||
*
|
||||
* Returns { bytes: bytesUsed, number: theNumber }
|
||||
*/
|
||||
function varIntToNum(bytes) {
|
||||
var prefix = bytes[0]
|
||||
|
||||
var viBytes =
|
||||
prefix < 253 ? bytes.slice(0, 1)
|
||||
: prefix === 253 ? bytes.slice(1, 3)
|
||||
: prefix === 254 ? bytes.slice(1, 5)
|
||||
: bytes.slice(1, 9)
|
||||
|
||||
return {
|
||||
bytes: prefix < 253 ? viBytes : bytes.slice(0, viBytes.length + 1),
|
||||
number: bytesToNum(viBytes)
|
||||
}
|
||||
}
|
||||
|
||||
function bytesToWords(bytes) {
|
||||
assert(Array.isArray(bytes) || Buffer.isBuffer(bytes), 'Input must be a byte array')
|
||||
var words = []
|
||||
|
@ -110,10 +65,7 @@ module.exports = {
|
|||
lpad: lpad,
|
||||
bytesToHex: bytesToHex,
|
||||
hexToBytes: hexToBytes,
|
||||
numToBytes: numToBytes,
|
||||
bytesToNum: bytesToNum,
|
||||
numToVarInt: numToVarInt,
|
||||
varIntToNum: varIntToNum,
|
||||
bytesToWords: bytesToWords,
|
||||
wordsToBytes: wordsToBytes,
|
||||
bytesToWordArray: bytesToWordArray,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue