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

Review usage of Number constructor for parsing to number #220

Closed
lenkan opened this issue Feb 15, 2024 · 1 comment · Fixed by #257
Closed

Review usage of Number constructor for parsing to number #220

lenkan opened this issue Feb 15, 2024 · 1 comment · Fixed by #257

Comments

@lenkan
Copy link
Collaborator

lenkan commented Feb 15, 2024

Currently, we parse strings to numbers using the Number constructor in some places. See comment #216 (comment). It looks like the eventing.ts usage needs replacing with parseInt(input, 16) to ensure we parse hex strings correctly. I don't think the tholder.ts needs changing to anything else, but it's worth having a look at it.

See current usage:

$ grep --line-number -A2 -B2  -E " Number\(" src/**/*.ts
src/keri/core/eventing.ts-68-    }
src/keri/core/eventing.ts-69-
src/keri/core/eventing.ts:70:    const sner = Number(sn);
src/keri/core/eventing.ts-71-    if (sner < 1) {
src/keri/core/eventing.ts-72-        throw new Error(`Invalid sn = 0x${sner.toString()} for rot or drt.`);
--
src/keri/core/tholder.ts-45-            let sith = this.thold.map((clause: Fraction[]) => {
src/keri/core/tholder.ts-46-                return clause.map((c) => {
src/keri/core/tholder.ts:47:                    if (0 < Number(c) && Number(c) < 1) {
src/keri/core/tholder.ts-48-                        return math.format(c, { fraction: 'ratio' });
src/keri/core/tholder.ts-49-                    } else {
--
src/keri/core/tholder.ts-201-            for (const w of clause) {
src/keri/core/tholder.ts-202-                if (sats[wio]) {
src/keri/core/tholder.ts:203:                    cw += Number(w);
src/keri/core/tholder.ts-204-                }
src/keri/core/tholder.ts-205-                wio += 1;
@lenkan
Copy link
Collaborator Author

lenkan commented Feb 20, 2024

Had a quick look at the usage here:

const sner = Number(sn);

As you can see from the type information:

The function only accepts sn that are numbers. I think the correct way to go about that fix is to do

if(typeof sn !== "number") {
   throw new Error(`Expected sn to be a number, got '${sn}'`);
}

Then skip the Number constructor and the sner variable completely.

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 a pull request may close this issue.

1 participant