-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve ABI Coders
decode
validation (#1426)
* feat: add bytes size valiadtion for overflow/undeflow and division issues * chore: changeset * feat: validate array and vector max size * chore: linting * feat: add legnth checks to vecs and arrays * feat: missing test coverage for enum and b256 coder: * test: mock calc fee logic in transaction summary test * chore: refactor * chore: further refactor * chore: rebuild * chore: rebuild * chore: remove redundant import Co-authored-by: Anderson Arboleya <[email protected]> * chore: remove redundant import Co-authored-by: Anderson Arboleya <[email protected]> * chore: linting * chore: update changeset Co-authored-by: Nedim Salkić <[email protected]> * refactor: remove throw error function from abstract coder --------- Co-authored-by: Anderson Arboleya <[email protected]> Co-authored-by: Nedim Salkić <[email protected]>
- Loading branch information
1 parent
ad7ee46
commit 78e5e40
Showing
34 changed files
with
586 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fuel-ts/abi-coder": minor | ||
--- | ||
|
||
Improve decode validation of ABI Coders |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,45 @@ | ||
import { ErrorCode } from '@fuel-ts/errors'; | ||
import { ErrorCode, FuelError } from '@fuel-ts/errors'; | ||
import { bn, toHex } from '@fuel-ts/math'; | ||
import { getBytesCopy } from 'ethers'; | ||
|
||
import { WORD_SIZE } from '../constants'; | ||
|
||
import { Coder } from './abstract-coder'; | ||
|
||
export class B256Coder extends Coder<string, string> { | ||
constructor() { | ||
super('b256', 'b256', 32); | ||
super('b256', 'b256', WORD_SIZE * 4); | ||
} | ||
|
||
encode(value: string): Uint8Array { | ||
let encodedValue; | ||
try { | ||
encodedValue = getBytesCopy(value); | ||
} catch (error) { | ||
this.throwError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
} | ||
if (encodedValue.length !== 32) { | ||
this.throwError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
if (encodedValue.length !== this.encodedLength) { | ||
throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
} | ||
return encodedValue; | ||
} | ||
|
||
decode(data: Uint8Array, offset: number): [string, number] { | ||
let bytes = data.slice(offset, offset + 32); | ||
if (data.length < this.encodedLength) { | ||
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid b256 data size.`); | ||
} | ||
|
||
let bytes = data.slice(offset, offset + this.encodedLength); | ||
|
||
const decoded = bn(bytes); | ||
if (decoded.isZero()) { | ||
bytes = new Uint8Array(32); | ||
} | ||
if (bytes.length !== 32) { | ||
this.throwError(ErrorCode.DECODE_ERROR, `'Invalid size for b256'.`); | ||
|
||
if (bytes.length !== this.encodedLength) { | ||
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid b256 byte data size.`); | ||
} | ||
|
||
return [toHex(bytes, 32), offset + 32]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,45 @@ | ||
import { ErrorCode } from '@fuel-ts/errors'; | ||
import { ErrorCode, FuelError } from '@fuel-ts/errors'; | ||
import { bn, toHex } from '@fuel-ts/math'; | ||
import { getBytesCopy } from 'ethers'; | ||
|
||
import { WORD_SIZE } from '../constants'; | ||
|
||
import { Coder } from './abstract-coder'; | ||
|
||
export class B512Coder extends Coder<string, string> { | ||
constructor() { | ||
super('b512', 'struct B512', 64); | ||
super('b512', 'struct B512', WORD_SIZE * 8); | ||
} | ||
|
||
encode(value: string): Uint8Array { | ||
let encodedValue; | ||
try { | ||
encodedValue = getBytesCopy(value); | ||
} catch (error) { | ||
this.throwError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
} | ||
if (encodedValue.length !== 64) { | ||
this.throwError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
if (encodedValue.length !== this.encodedLength) { | ||
throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`); | ||
} | ||
return encodedValue; | ||
} | ||
|
||
decode(data: Uint8Array, offset: number): [string, number] { | ||
let bytes = data.slice(offset, offset + 64); | ||
if (data.length < this.encodedLength) { | ||
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid b512 data size.`); | ||
} | ||
|
||
let bytes = data.slice(offset, offset + this.encodedLength); | ||
|
||
const decoded = bn(bytes); | ||
if (decoded.isZero()) { | ||
bytes = new Uint8Array(64); | ||
} | ||
if (bytes.length !== 64) { | ||
this.throwError(ErrorCode.DECODE_ERROR, `Invalid size for b512.`); | ||
|
||
if (bytes.length !== this.encodedLength) { | ||
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid b512 byte data size.`); | ||
} | ||
return [toHex(bytes, 64), offset + 64]; | ||
|
||
return [toHex(bytes, this.encodedLength), offset + this.encodedLength]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.