Skip to content

Commit

Permalink
fix(tx-builder): ensure that TX RLP have the same length as schema
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 22, 2022
1 parent 0560575 commit c042d73
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/tx/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ function unpackRawTx<Tx extends TxSchema>(
binary: Uint8Array | NestedUint8Array,
schema: TxField[],
): RawTxObject<Tx> {
if (binary.length !== schema.length) {
throw new ArgumentError('Transaction RLP length', schema.length, binary.length);
}
return schema
.reduce<any>(
(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export class WalletError extends BaseError {
* @category exception
*/
export class ArgumentError extends BaseError {
constructor(argumentName: string, requirement: string, argumentValue: any) {
super(`${argumentName} should be ${requirement}, got ${String(argumentValue)} instead`);
constructor(argumentName: string, requirement: unknown, argumentValue: unknown) {
super(`${argumentName} should be ${requirement}, got ${argumentValue} instead`);
this.name = 'ArgumentError';
}
}
Expand Down
17 changes: 12 additions & 5 deletions test/unit/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
toBytes,
buildTx, unpackTx,
NAME_BID_RANGES, Tag,
SchemaNotFoundError,
SchemaNotFoundError, ArgumentError,
} from '../../src';
import { Encoding, Encoded } from '../../src/utils/encoder';

Expand Down Expand Up @@ -110,10 +110,17 @@ describe('Tx', () => {
).to.throw('identifier should be prefixed with one of ak_, ok_, ct_, ch_, got ba_AQIq9Y55kw== instead'));
});

it('Deserialize tx: invalid tx VSN', () => {
const tx = encode(rlpEncode([10, 99]), Encoding.Transaction);
expect(() => unpackTx(tx))
.to.throw(SchemaNotFoundError, `Transaction deserialization not implemented for tag ${10} version ${99}`);
describe('unpackTx', () => {
it('throws error if invalid tx VSN', () => {
const tx = encode(rlpEncode([10, 99]), Encoding.Transaction);
expect(() => unpackTx(tx))
.to.throw(SchemaNotFoundError, 'Transaction deserialization not implemented for tag 10 version 99');
});

it('fails to unpack tx with more RLP items than in schema', () => {
expect(() => unpackTx('tx_+GIMAaEB4TK48d23oE5jt/qWR5pUu8UlpTGn8bwM5JISGQMGf7ChAeEyuPHdt6BOY7f6lkeaVLvFJaUxp/G8DOSSEhkDBn+wiBvBbWdOyAAAhg92HvYQAAABhHRlc3SEdGVzdK2Ldck='))
.to.throw(ArgumentError, 'Transaction RLP length should be 9, got 10 instead');
});
});

it('Serialize tx: invalid tx VSN', () => {
Expand Down

0 comments on commit c042d73

Please sign in to comment.