x-address-codec
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -9,39 +9,8 @@ 'use strict'; | ||
function seqEqual(arr1, arr2) { | ||
if (arr1.length !== arr2.length) { | ||
return false; | ||
} | ||
var _require = require('./utils'); | ||
for (var i = 0; i < arr1.length; i++) { | ||
if (arr1[i] !== arr2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
var seqEqual = _require.seqEqual; | ||
var toArray = _require.toArray; | ||
var isSet = _require.isSet; | ||
// Concatenates args and/or contents of sequence conforming args to an Array. | ||
function toArray() { | ||
var args = arguments; | ||
if (args.length === 1 && Array.isArray(args[0])) { | ||
return args[0]; | ||
} | ||
var ret = []; | ||
for (var i = 0; i < args.length; i++) { | ||
var arg = args[i]; | ||
if (arg.length !== undefined) { | ||
for (var j = 0; j < arg.length; j++) { | ||
ret.push(arg[j]); | ||
} | ||
} else { | ||
ret.push(arg); | ||
} | ||
} | ||
return ret; | ||
} | ||
function isSet(o) { | ||
return o !== null && o !== undefined; | ||
} | ||
/* --------------------------------- ENCODER -------------------------------- */ | ||
@@ -66,30 +35,2 @@ | ||
_createClass(AddressCodec, [{ | ||
key: 'encodeRaw', | ||
value: function encodeRaw(bytes) { | ||
return this.codec.encode(bytes); | ||
} | ||
}, { | ||
key: 'decodeRaw', | ||
value: function decodeRaw(string) { | ||
return this.codec.decode(string); | ||
} | ||
}, { | ||
key: 'verifyCheckSum', | ||
value: function verifyCheckSum(bytes) { | ||
var computed = sha256(sha256(bytes.slice(0, -4))).slice(0, 4); | ||
var checksum = bytes.slice(-4); | ||
return seqEqual(computed, checksum); | ||
} | ||
}, { | ||
key: 'encodeVersioned', | ||
value: function encodeVersioned(bytes, version) { | ||
return this.encodeChecked(toArray(version, bytes)); | ||
} | ||
}, { | ||
key: 'encodeChecked', | ||
value: function encodeChecked(buffer) { | ||
var check = sha256(sha256(buffer)).slice(0, 4); | ||
return this.encodeRaw(toArray(buffer, check)); | ||
} | ||
}, { | ||
key: 'encode', | ||
@@ -112,2 +53,18 @@ value: function encode(bytes) { | ||
}, { | ||
key: 'encodeRaw', | ||
value: function encodeRaw(bytes) { | ||
return this.codec.encode(bytes); | ||
} | ||
}, { | ||
key: 'decodeRaw', | ||
value: function decodeRaw(string) { | ||
return this.codec.decode(string); | ||
} | ||
}, { | ||
key: 'encodeChecked', | ||
value: function encodeChecked(buffer) { | ||
var check = sha256(sha256(buffer)).slice(0, 4); | ||
return this.encodeRaw(toArray(buffer, check)); | ||
} | ||
}, { | ||
key: 'decodeChecked', | ||
@@ -125,2 +82,7 @@ value: function decodeChecked(encoded) { | ||
}, { | ||
key: 'encodeVersioned', | ||
value: function encodeVersioned(bytes, version) { | ||
return this.encodeChecked(toArray(version, bytes)); | ||
} | ||
}, { | ||
key: 'decodeVersioned', | ||
@@ -131,25 +93,2 @@ value: function decodeVersioned(string, version) { | ||
}, { | ||
key: 'findPrefix', | ||
/** | ||
* @param {Number} payloadLength - number of bytes encoded not incl checksum | ||
* @param {String} desiredPrefix - desired prefix when base58 encoded with | ||
* checksum | ||
* @return {Array} version | ||
*/ | ||
value: function findPrefix(payloadLength, desiredPrefix) { | ||
if (this.base !== 58) { | ||
throw new Error('Only works for base58'); | ||
} | ||
var totalLength = payloadLength + 4; // for checksum | ||
var chars = Math.log(Math.pow(256, totalLength)) / Math.log(this.base); | ||
// (x, x.8] -> x+1, (x.8, x+1) -> x+2 | ||
var requiredChars = Math.ceil(chars + 0.2); | ||
var padding = this.alphabet[Math.floor(this.alphabet.length / 2) - 1]; | ||
var template = desiredPrefix + new Array(requiredChars + 1).join(padding); | ||
var bytes = this.decodeRaw(template); | ||
var version = bytes.slice(0, -totalLength); | ||
return version; | ||
} | ||
}, { | ||
key: 'decodeMultiVersioned', | ||
@@ -168,3 +107,3 @@ | ||
*/ | ||
value: function decodeMultiVersioned(encoded, possibleVersions, expectedLength, names) { | ||
value: function decodeMultiVersioned(encoded, possibleVersions, expectedLength, types) { | ||
var withoutSum = this.decodeChecked(encoded); | ||
@@ -188,4 +127,4 @@ var ret = { version: null, bytes: null }; | ||
ret.versionIx = i; | ||
if (names) { | ||
ret.type = names[i]; | ||
if (types) { | ||
ret.type = types[i]; | ||
} | ||
@@ -201,2 +140,32 @@ return false; | ||
} | ||
}, { | ||
key: 'verifyCheckSum', | ||
value: function verifyCheckSum(bytes) { | ||
var computed = sha256(sha256(bytes.slice(0, -4))).slice(0, 4); | ||
var checksum = bytes.slice(-4); | ||
return seqEqual(computed, checksum); | ||
} | ||
}, { | ||
key: 'findPrefix', | ||
/** | ||
* @param {Number} payloadLength - number of bytes encoded not incl checksum | ||
* @param {String} desiredPrefix - desired prefix when base58 encoded with | ||
* checksum | ||
* @return {Array} version | ||
*/ | ||
value: function findPrefix(payloadLength, desiredPrefix) { | ||
if (this.base !== 58) { | ||
throw new Error('Only works for base58'); | ||
} | ||
var totalLength = payloadLength + 4; // for checksum | ||
var chars = Math.log(Math.pow(256, totalLength)) / Math.log(this.base); | ||
// (x, x.8] -> x+1, (x.8, x+1) -> x+2 | ||
var requiredChars = Math.ceil(chars + 0.2); | ||
var padding = this.alphabet[Math.floor(this.alphabet.length / 2) - 1]; | ||
var template = desiredPrefix + new Array(requiredChars + 1).join(padding); | ||
var bytes = this.decodeRaw(template); | ||
var version = bytes.slice(0, -totalLength); | ||
return version; | ||
} | ||
}]); | ||
@@ -203,0 +172,0 @@ |
@@ -21,3 +21,5 @@ 'use strict'; | ||
add('decode'); | ||
if (!args.versions) add('encode'); | ||
if (!args.versions) { | ||
add('encode'); | ||
} | ||
} | ||
@@ -24,0 +26,0 @@ for (var k in codecMethods) { |
{ | ||
"name": "x-address-codec", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "crypto currency address codec", | ||
@@ -5,0 +5,0 @@ "files": [ |
10832
5
244