* 3.7

* Update new rules

* RegExp

* formatBalance cleanups

* Revert

* Formatting

* fill

* format test fix

* cleanups

* Cleanups

* Expand bip39 tests

* Cleanups

* Cleanups
This commit is contained in:
Jaco Greeff
2020-11-01 14:44:14 +01:00
committed by GitHub
parent 4a0fa7f615
commit 3230663ae1
37 changed files with 345 additions and 2434 deletions
+26 -15
View File
@@ -17,6 +17,25 @@ const LOOKUP = BASE32_ALPHABET
return lookup;
}, {});
/** @internal */
function decode (output: Uint8Array, input: string, offset: number): [Uint8Array, number, number] {
let bits = 0;
let buffer = 0;
let written = 0;
for (let i = offset; i < input.length; i++) {
buffer = (buffer << BITS_PER_CHAR) | LOOKUP[input[i]];
bits += BITS_PER_CHAR;
if (bits >= 8) {
bits -= 8;
output[written++] = 0xff & (buffer >> bits);
}
}
return [output, bits, buffer];
}
/**
* @name base32Decode
* @summary Delookup a base32 value.
@@ -26,22 +45,14 @@ const LOOKUP = BASE32_ALPHABET
export default function base32Decode (value: string, ipfsCompat = false): Uint8Array {
validate(value, ipfsCompat);
const out = new Uint8Array(((value.length - (ipfsCompat ? 1 : 0)) * BITS_PER_CHAR / 8) | 0);
let bits = 0;
let buffer = 0;
let written = 0;
for (let i = (ipfsCompat ? 1 : 0); i < value.length; ++i) {
buffer = (buffer << BITS_PER_CHAR) | LOOKUP[value[i]];
bits += BITS_PER_CHAR;
if (bits >= 8) {
bits -= 8;
out[written++] = 0xff & (buffer >> bits);
}
}
const offset = (ipfsCompat ? 1 : 0);
const [output, bits, buffer] = decode(
new Uint8Array(((value.length - offset) * BITS_PER_CHAR / 8) | 0),
value,
offset
);
assert(!(bits >= BITS_PER_CHAR || 0xff & (buffer << (8 - bits))), 'Unexpected end of data');
return out;
return output;
}