Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function readBytes(memory: Uint8Array, offset: number, length: number) {
if(!Number.isSafeInteger(offset + length)) {
throw new DecodingError({
kind: "ReadErrorBytes" as const,
start: offset,
length
});
}
// grab `length` bytes no matter what, here fill this array
var bytes = new Uint8Array(length);
bytes.fill(0); //fill it wil zeroes to start
//if the start is beyond the end of memory, just return those 0s
if (offset >= memory.length) {
return bytes;
}
// if we're reading past the end of memory, truncate the length to read
export function readStack(stack: Uint8Array[], from: number, to: number): Uint8Array {
if(from < 0 || to >= stack.length) {
throw new DecodingError(
{
kind: "ReadErrorStack",
from,
to
}
);
}
//unforunately, Uint8Arrays don't support concat; if they did the rest of
//this would be one line. Or similarly if they worked with lodash's flatten,
//but they don't support that either. But neither of those are the case, so
//we'll have to concatenate a bit more manually.
let words = stack.slice(from, to + 1);
let result = new Uint8Array(words.length * CodecUtils.EVM.WORD_SIZE);
//shouldn't we total up the lengths? yeah, but each one should have a
//length of 32, so unless somehting's gone wrong we can just multiply
for(let index = 0; index < words.length; index++) {
switch(CodecUtils.Definition.typeClass(definition))
{
case "rational":
let numericalValue: BN = CodecUtils.Definition.rationalValue(definition);
return CodecUtils.Conversion.toBytes(numericalValue, CodecUtils.EVM.WORD_SIZE);
//you may be wondering, why do we not just use definition.value here,
//like we do below? answer: because if this isn't a literal, that may not
//exist
case "stringliteral":
return CodecUtils.Conversion.toBytes(definition.hexValue);
default:
//unfortunately, other types of constants are just too complicated to
//handle right now. sorry.
debug("unsupported constant definition type");
throw new DecodingError(
{
kind: "UnsupportedConstantError",
definition
}
);
}
}