Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 31, 2024
1 parent 39cda86 commit ef46c8e
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions yarn-project/foundation/src/abi/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,30 @@ class AbiDecoder {
switch (abiType.kind) {
case 'field':
return this.getNextField().toBigInt();
case 'integer':
case 'integer': {
const nextField = this.getNextField();

if (abiType.sign === 'signed') {
throw new Error('Unsupported type: signed integer');
// We get the last (width / 8) bytes where width = bits of type (i64, i32 etc)
const buf = nextField.toBuffer().subarray(-(abiType.width / 8));

// We then parse the buffer using 2's complement

// If our most significant bit is high
if (0x80 & buf.subarray(0, 1).readUInt8()) {
// We flip the bits
for (let i = 0; i < buf.length; i++) {
buf[i] = ~buf[i];
}

// Add one, then negate it
return -(BigInt(buf.toString('hex')) + 1n);
}

return BigInt(buf.toString('hex'));
}
return this.getNextField().toBigInt();
return nextField.toBigInt();
}
case 'boolean':
return !this.getNextField().isZero();
case 'array': {
Expand Down

0 comments on commit ef46c8e

Please sign in to comment.