fix: Add transaction encoding helper functions
This commit is contained in:
committed by
Michael Vines
parent
02ca644cb1
commit
ac6e503b35
32
web3.js/src/util/encoding.js
Normal file
32
web3.js/src/util/encoding.js
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user