fix: Add transaction encoding helper functions

This commit is contained in:
Tyera Eulberg
2019-01-31 02:15:02 -07:00
committed by Michael Vines
parent 02ca644cb1
commit ac6e503b35
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// @flow
export function decodeLength(bytes: Array<number>): number {
let len = 0;
let size = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
let elem = bytes.shift();
len |= (elem & 0x7f) << (size * 7);
size += 1;
if ((elem & 0x80) === 0) {
break;
}
}
return len;
}
export function encodeLength(bytes: Array<number>, len: number) {
let rem_len = len;
// eslint-disable-next-line no-constant-condition
while (true) {
let elem = rem_len & 0x7f;
rem_len >>= 7;
if (rem_len == 0) {
bytes.push(elem);
break;
} else {
elem |= 0x80;
bytes.push(elem);
}
}
}