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

Web3Provider should be initializable without account #694

Open
peterjah opened this issue Oct 29, 2024 · 1 comment
Open

Web3Provider should be initializable without account #694

peterjah opened this issue Oct 29, 2024 · 1 comment
Assignees

Comments

@peterjah
Copy link
Collaborator

to use all json rpc endpoint that does not require signaure

@thomas-senechal
Copy link
Member

We could make a ReadOnlyWeb3Provider that would implement Provider and could be "upgraded" to a Web3Provider.
Every methods that require an account would throw an error explaining that the class should be upgraded to use it

export class ReadOnlyProvider implements Provider {
  constructor(public client: PublicAPI) {}
  /**
   * Upgrades the provider to a Web3Provider by connecting an account
   * @param account - Account to connect to the provider
   * @returns The created Web3Provider
   */
  connectAccount(account: Account): Web3Provider {
    return new Web3Provider(this.client, account)
  }

  private readonly _providerName: string = 'Massa web3 ReadOnlyProvider'
  private readonly _accountRequiredError = `This method is not available in ReadOnlyProvider. Please use the "connectAccount" method to upgrade the class to a "Web3Provider".`

  get address(): string {
    throw new Error(this._accountRequiredError)
  }
  get accountName(): string {
    throw new Error(this._accountRequiredError)
  }
  get providerName(): string {
    return this._providerName
  }
...

And Web3Provider could extends ReadOnlyWeb3Provider so we avoid duplicating code as they will both implement the same function except the ones requiring an account which will be different

@Ben-Rey Ben-Rey linked a pull request Dec 26, 2024 that will close this issue
@Ben-Rey Ben-Rey self-assigned this Dec 30, 2024
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.

3 participants