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

Access to Private Field Issue in FixedNumber Object Operations #4131

Open
RauppRafael opened this issue Jun 9, 2023 · 5 comments
Open

Access to Private Field Issue in FixedNumber Object Operations #4131

RauppRafael opened this issue Jun 9, 2023 · 5 comments
Assignees
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6

Comments

@RauppRafael
Copy link

Ethers Version

6.4.2

Search Terms

FixedNumber

Describe the Problem

I've been using the Ethers library in a project and I've encountered an issue with the FixedNumber object, specifically with operations that access o.#val, a private field.

Here's a simplified snippet of the FixedNumber class and a method that's causing problems:

export class FixedNumber {
    ...
    
    #mul(o: FixedNumber, safeOp?: string): FixedNumber {
        this.#checkFormat(o);
        return this.#checkValue((this.#val * o.#val) / this.#tens, safeOp);
    }
    
    ...
}

When the #mul method is called, it leads to the following error:

Uncaught (in promise) TypeError: Cannot read from private field

The issue arises from attempts to access the private field o.#val. I have observed this behavior when using the FixedNumber object within a Pinia store (Nuxt3), but the underlying problem appears to be in the FixedNumber object itself, even though it doesn't occur on other parts of the code.

This behavior is unexpected as operations on a FixedNumber object should not be attempting to access private properties of the object.

Code Snippet

#mul(o: FixedNumber, safeOp?: string): FixedNumber {
        this.#checkFormat(o);
        return this.#checkValue((this.#val * o.#val) / this.#tens, safeOp);
    }

Contract ABI

No response

Errors

Uncaught (in promise) TypeError: Cannot read from private field
    at __accessCheck (chunk-ZR4A2MMM.js?v=91ea41d3:38:11)
    at __privateGet (chunk-ZR4A2MMM.js?v=91ea41d3:41:3)
    at _FixedNumber.mul_fn (fixednumber.ts:359:46)
    at _FixedNumber.mul (fixednumber.ts:373:51)

Environment

Browser (Chrome, Safari, etc)

Environment (Other)

Nuxt3 + pinia

@RauppRafael RauppRafael added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels Jun 9, 2023
@ricmoo
Copy link
Member

ricmoo commented Jun 9, 2023

You cannot access private members, in general. Can you provide some sample code that causes this?

An incorrectly implemented proxy can result in a wrapped object that doesn’t pass the original target along to the method as the this, but I’m not sure what else would cause this.

@RauppRafael
Copy link
Author

Hey Ric, thanks for the quick response, I couldn't reproduce the exact issue in a sandbox, but indeed there's something weird happening with the FixedNumber proxies generated by pinia, here's an example

https://codesandbox.io/s/ethers-f4fhtk?file=/src/my-store.ts

@ricmoo
Copy link
Member

ricmoo commented Jun 13, 2023

This looks like it may be a problem with Vue itself: vuejs/core#2981

@yuetloo
Copy link
Collaborator

yuetloo commented Jun 14, 2023

This seems to relate to the vue reactivity implementation. In cases where you don't need the deep reactive conversion of vue, you could use markRaw, but use it with caution.

https://vuejs.org/api/reactivity-advanced.html#markraw

@ricmoo
Copy link
Member

ricmoo commented Jun 14, 2023

One note is that the FixedNumber is immutable, so it is safe to assume its private members do not change (nor do its public members). :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

3 participants