starknet.dart icon indicating copy to clipboard operation
starknet.dart copied to clipboard

feat: Investigate and Explore Support for New Wallet API from JSON RPC Specification

Open redDwarf03 opened this issue 7 months ago • 3 comments

This PR suggests to manage issue #490

Notes on Starknet Wallet API Implementation for starknet.dart

1. Objective

The goal was to study the Starknet Wallet API specification (based on wallet_rpc.json) and propose an implementation as a distinct Dart package (wallet_provider), usable within the starknet.dart ecosystem. This package should allow Dart applications to interact with a compatible Starknet wallet.

2. Implementation Approach

  • Dedicated Package: A new package, wallet_provider, was created by duplicating then modifying starknet_provider to isolate the logic specific to the Wallet API.
  • Data Models: Data structures (requests, responses) defined in wallet_rpc.json were implemented in Dart using the freezed package to generate immutable and serializable classes (files in lib/src/model/).
  • Provider Class: A WalletProvider class (lib/src/provider.dart) was created. It exposes typed methods corresponding to the Wallet API RPC calls (wallet_supportedWalletApi, wallet_addInvokeTransaction, etc.).
  • RPC Client: A simple JsonRpcClient was implemented in provider.dart to handle sending JSON-RPC POST requests and basic deserialization of responses and errors.
  • Dependencies: The package depends on package:starknet for fundamental types (Felt) and some shared complex types (EntryPointsByType). It also uses package:http for network calls.
  • Cleanup: Models, providers, and tests related to the standard Starknet JSON-RPC API were removed from the wallet_provider package to focus it solely on the Wallet API.

3. Discoveries and Key Points

  • Wallet API vs. Standard RPC API Distinction: The Wallet API is a distinct set of JSON-RPC methods that does not replace the standard RPC API but complements it for interactions requiring user wallet action or information.
  • Model Generation: The OpenRPC specification (wallet_rpc.json) is structured enough to allow for the generation (here, manually assisted) of type-safe Dart data models using freezed.
  • Dependency on External Types: The implementation heavily relies on types defined in package:starknet (Felt, EntryPointsByType, etc.). Version compatibility and the clarity of exports from this package are crucial.
  • RPC Parameter Ambiguity: The exact expected structure for parameters (params in the JSON-RPC request) is not always explicitly defined in the spec as being a positional list or a single object. The current implementation makes assumptions based on the order and apparent structure in wallet_rpc.json, but this might require adjustments based on actual wallet implementations.
  • signTypedData Complexity: Signing typed data requires careful construction of the TypedData object, especially converting all message and domain values into their corresponding Felt type (short string encoding, integer/hex conversion, etc.) to match the types defined in the types structure.

4. Challenges Encountered

  • Identifying Required Types (ContractClass): There was initial confusion about the origin and correct definition of ContractClass, requiring determination of whether to use a local definition (from the old starknet_provider) or the one from the starknet package. Ultimately, a local definition based on the schema was created.

5. Recommendations

  • Spec Clarification: If possible, encourage clarification in the Wallet API specification regarding the expected structure of params for each method (positional list vs. object).
  • TypedData Utilities: Consider adding utility functions (potentially in package:starknet or wallet_provider) to facilitate the correct construction of TypedData objects, especially for String -> Felt conversion according to EIP-712/Starknet rules.

Summary by CodeRabbit

  • New Features

    • Introduced the wallet_provider Dart package for interacting with Starknet wallets via the Wallet API.
    • Provides a strongly typed Dart client supporting wallet actions such as account access, transaction signing and submission, network management, and asset watching.
    • Includes comprehensive data models for transactions, assets, permissions, errors, and typed data.
    • Offers example usage and detailed documentation.
  • Documentation

    • Added README, changelog, implementation notes, and license files for clear guidance and transparency.
  • Tests

    • Added unit and integration tests to verify provider functionality and ensure robust error handling.
  • Chores

    • Added configuration files for analysis, build, tool versions, and package management.

redDwarf03 avatar Apr 29 '25 14:04 redDwarf03

To view this pull requests documentation preview, visit the following URL:

docs.page/focustree/starknet.dart~515

Documentation is deployed and generated using docs.page.

docs-page[bot] avatar Apr 29 '25 14:04 docs-page[bot]

"""

Walkthrough

This change introduces a new Dart package, wallet_provider, designed for interacting with Starknet wallets via the Starknet Wallet API JSON-RPC specification. The package includes all necessary configuration files, core provider/client implementations, comprehensive data models for wallet operations, and extensive generated code for immutability and serialization using the freezed package. Documentation and example usage are provided, along with initial tests for provider methods. The package is structured for strong type safety, clear separation from standard Starknet JSON-RPC APIs, and follows Dart's recommended practices for dependencies and code generation.

Changes

File(s) / Path(s) Change Summary
.gitignore, .tool-versions, CHANGELOG.md, LICENSE, README.md, analysis_options.yaml, build.yaml, dart_test.yaml, pubspec.yaml Added standard configuration, documentation, and metadata files for the Dart package, including versioning, linting, build options, license, and changelog.
Implementation_notes.md Added design and development notes explaining the package's architecture, use of code generation, API distinctions, and recommendations for future improvements.
example/wallet_provider_example.dart Added a comprehensive example demonstrating how to use the WalletProvider to interact with a Starknet wallet, including account requests, transaction submission, and typed data signing.
test/melos_test.dart, test/provider_test.dart Added initial test files: one placeholder to satisfy tool requirements, and one that tests provider methods against a Starknet node, checking for correct error handling and expected responses.
lib/wallet_provider.dart, lib/src/index.dart, lib/src/model/index.dart Introduced main library entry points and consolidated exports for the provider and all data models, allowing for organized and simple imports.
lib/src/provider.dart Implemented the WalletProvider class and a supporting JsonRpcClient, providing typed, asynchronous methods for all wallet API operations, with error handling and resource management.
lib/src/model/account_deployment_data.dart, add_declare_transaction_result.dart, add_invoke_transaction_result.dart, api_version.dart, asset.dart, contract_class.dart, declare_txn_wallet.dart, deployment_version.dart, invoke_call.dart, permission.dart, starknet_chain.dart, typed_data.dart, wallet_error.dart Defined all core data models for the wallet API, including account deployment, transaction results, asset and contract representations, permissions, chain info, typed data for signing, and structured wallet error handling.
lib/src/model/.freezed.dart, lib/src/model/.g.dart Generated code for all models using the freezed and json_serializable packages, providing immutability, value equality, copy-with, and JSON serialization/deserialization for all data structures.

Sequence Diagram(s)

sequenceDiagram
    participant App as Dapp/Client
    participant Provider as WalletProvider
    participant RPC as JsonRpcClient
    participant Wallet as Starknet Wallet (via HTTP)

    App->>Provider: Call wallet method (e.g., requestAccounts)
    Provider->>RPC: call(method, params)
    RPC->>Wallet: HTTP POST (JSON-RPC)
    Wallet-->>RPC: JSON-RPC response
    RPC-->>Provider: Parse result or throw WalletError
    Provider-->>App: Return typed result or error

Possibly related issues

  • focustree/starknet.dart#490: This PR implements a full Dart package wallet_provider directly addressing the objectives of the issue by providing a strongly typed client for the Starknet Wallet API, including models, provider classes, and example usage.

Suggested reviewers

  • ptisserand

Poem

In the warren of code, a wallet appears,
With models and types, it conquers our fears.
JSON-RPC hopping, requests sent with care,
Dart and Freezed magic are floating in air.
Now Starknet is open, with safety and pride—
The rabbit approves, with a binky and glide!
🐇✨ """


📜 Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eab5e28f3497f7b3934def7a3c81f0a246cbd466 and bba33de7f229d8d8784e96eb7090c826b23a9daf.

📒 Files selected for processing (1)
  • packages/wallet_provider/lib/src/model/wallet_error.dart (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/wallet_provider/lib/src/model/wallet_error.dart

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Apr 29 '25 14:04 coderabbitai[bot]

i did some updates from coderabbit recommandations @ptisserand i let you for the moment review this PR. I will work on some updates if necessary but for the moment i think this PR could be challenged

redDwarf03 avatar May 05 '25 16:05 redDwarf03

Hello @ptisserand . Do you know if this PR will be reviewed? Thx

redDwarf03 avatar Jun 18 '25 07:06 redDwarf03

Hello @ptisserand . Do you know if this PR will be reviewed? Thx

Hi @redDwarf03 , yes I will review it this week-end

ptisserand avatar Jun 20 '25 09:06 ptisserand

"Also for typed data, for future version we should think how we could use the same model for the different packages." -> Agreed. That would be good for maintenance and consistency of the data model.

Are you expecting anything else from this PR?

redDwarf03 avatar Jun 23 '25 19:06 redDwarf03

Are you expecting anything else from this PR?

Only change for enum error and it will good to be merged. Thanks

ptisserand avatar Jun 25 '25 05:06 ptisserand

@ptisserand thx ! please find a question in a PM on your x's profile :)

redDwarf03 avatar Jun 26 '25 16:06 redDwarf03