Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bug): toTxMethod sanitizing #183

Merged
merged 11 commits into from
Feb 22, 2022
Merged

fix(bug): toTxMethod sanitizing #183

merged 11 commits into from
Feb 22, 2022

Conversation

TarikGul
Copy link
Member

@TarikGul TarikGul commented Feb 21, 2022

closes: #180
rel: #126

This fixes a bug introduced in #178 where an increased effort to sanitize numbers via toTxMethod was given, this makes sure we check the type of the primitive value as well as !Number.isNaN to ensure any integer that makes it past the first initial sanitization is converted to a string.

I also added a test suite for toTxMethod (long over due).
I can also confirm with the given test the older logic was failing and is now passing.

@@ -43,7 +43,7 @@ export function toTxMethod(registry: TypeRegistry, method: Call): TxMethod {

// Sanity check to check that `jsonArg` is a number, and if it is
// to change it to a string
if (!Number.isNaN(jsonArg)) {
if (!Number.isNaN(jsonArg) && typeof jsonArg === 'number') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line looks a wee bit sketchy to me.
I assume that the first part of the ternary is guaranteed to return a number, i.e. if codec is an AbstractInt I assume that the conversion to string is fine and need no further checking.
If that is true, then this line is here to guard against the second part, (codec as BN).toJSON()?

But then why not do this checking here in that else branch?

if codec instanceof AbstractInt {
  codec.toString(RADIX_PARAM)
} else {
  (codec as BN).toJSON();
  // check here
}

Not very familiar with BN but it seems odd that a .toJSON() call could return something that isn't a number. What exactly could codec contain that makes this necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dvdplm BN.toJSON() just returns a string representation of the number.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dvdplm I think the concern here is 100% valid, and I will clean up the code to make it more readable, and cleaner.

The issue with (codec as BN).toJSON() is that it is aactually technically incorrect as the codec arg will not always be guaranteed to be a BN, but may be an object with the raw type of:

{"_enum":{"Id":"AccountId","Index":"Compact<AccountIndex>","Raw":"Bytes","Address32":"H256","Address20":"H160"}}

as well as any other valid codec type.

or it could also very well be a BN.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up and fixed.

Copy link
Contributor

@0xslipk 0xslipk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just a small comment

packages/txwrapper-core/src/core/method/toTxMethod.ts Outdated Show resolved Hide resolved
@TarikGul TarikGul merged commit c7f8e12 into main Feb 22, 2022
@TarikGul TarikGul deleted the tarik-toTxMethod branch February 22, 2022 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

toTxMethod returning "[object Object]" value for the dest field in method.args.dest
3 participants