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

Feat/add owallet #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

toandq2009
Copy link

@toandq2009 toandq2009 commented Sep 27, 2024

  • Add icon OWallet
  • Add method connect OWallet

Summary by CodeRabbit

  • New Features

    • Introduced a new wallet icon component (owallet) for enhanced visual representation.
    • Added a method to connect to the OWallet, streamlining user wallet interactions.
    • Implemented address validation for connected OWallet to ensure accuracy.
  • Improvements

    • Expanded the wallet icon options with the addition of the new owallet entry.

Copy link

coderabbitai bot commented Sep 27, 2024

Walkthrough

The changes introduce a new Vue component for an OWallet icon, enhance wallet connection functionality with a new method to connect to the OWallet, and implement a validation function for the connected OWallet address. Additionally, a new entry for the OWallet icon is added to the shared icon library. These modifications collectively expand the wallet features and improve the integration of the OWallet within the application.

Changes

Files Change Summary
layer/icons/wallet-new/owallet.vue Introduced a new Vue component for the OWallet icon with TypeScript logic and an SVG graphic.
layer/store/wallet.ts Added a method connectOWallet to facilitate connection and address validation for the OWallet.
layer/wallet/cosmos.ts Introduced confirmCorrectOWalletAddress to validate the OWallet address against a provided Injective address.
packages/ui-shared/playground/icons.json Added new entry "wallet/owallet" to the wallet icons list.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant WalletStore
    participant OWalletBase

    User->>WalletStore: connectOWallet()
    WalletStore->>WalletStore: connectWallet(Wallet.OWallet)
    WalletStore->>WalletStore: retrieve Injective addresses
    WalletStore->>WalletStore: confirm session
    WalletStore->>OWalletBase: create instance
    OWalletBase->>WalletStore: get public key
    WalletStore->>WalletStore: validate address
    WalletStore->>User: update wallet state
Loading

🐇 "In the meadow where wallets play,
A new friend hops in today.
OWallet shines with a gleaming hue,
Connecting hearts, both old and new.
With paths of code and icons bright,
We celebrate this joyful sight!" 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (4)
packages/ui-shared/playground/icons.json (1)

142-142: LGTM! Consider alphabetical ordering.

The addition of "wallet/owallet" is consistent with the existing naming convention for wallet icons and aligns with the PR objective. Good job!

For improved readability and easier maintenance, consider ordering the wallet icons alphabetically. In this case, "wallet/owallet" could be placed between "wallet/ninji" and "wallet/phantom".

layer/icons/wallet-new/owallet.vue (1)

5-20: SVG implementation looks good. Consider adding accessibility attributes.

The SVG implementation for the OWallet icon is well-structured and visually appealing. The use of opacity and gradients enhances the icon's appearance.

To improve accessibility, consider adding the following attributes to the <svg> element:

-  <svg width="166" height="165" viewBox="0 0 166 165" fill="none" xmlns="http://www.w3.org/2000/svg">
+  <svg width="166" height="165" viewBox="0 0 166 165" fill="none" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="OWallet icon">

This change will help screen readers identify and describe the icon to users with visual impairments.

layer/icons/wallet/owallet.vue (1)

5-21: Enhance accessibility by adding ARIA attributes to the SVG.

The current SVG lacks accessibility attributes, which can make it difficult for screen readers to interpret the icon.

Add appropriate ARIA attributes to improve accessibility:

 <template>
-  <svg width="166" height="165" viewBox="0 0 166 165" fill="none" xmlns="http://www.w3.org/2000/svg">
+  <svg width="166" height="165" viewBox="0 0 166 165" fill="none" xmlns="http://www.w3.org/2000/svg" role="img" aria-labelledby="owalletIconTitle">
+    <title id="owalletIconTitle">OWallet Icon</title>
     <!-- ... SVG content ... -->
   </svg>
 </template>

These changes will help screen readers understand the purpose of the icon, improving the overall accessibility of your application.

layer/store/wallet.ts (1)

438-460: LGTM: New connectOWallet method added

The new connectOWallet method follows the established pattern for wallet connections in this file and correctly integrates OWallet support. However, consider the following suggestions for improvement:

  1. Add error handling to manage potential failures during the connection process.
  2. Include comments explaining the purpose and flow of the method for better maintainability.

Consider adding error handling and comments:

 async connectOWallet() {
   const walletStore = useSharedWalletStore()

+  try {
+    // Connect to OWallet
     await walletStore.connectWallet(Wallet.OWallet)

+    // Retrieve Injective addresses and select the first one
     const injectiveAddresses = await getAddresses()
     const [injectiveAddress] = injectiveAddresses
     const session = await walletStrategy.getSessionOrConfirm()

+    // Validate the Injective address
     await confirmCorrectOWalletAddress(injectiveAddress)

+    // Update the store with the new wallet information
     walletStore.$patch({
       injectiveAddress,
       addresses: injectiveAddresses,
       address: getEthereumAddress(injectiveAddress),
       addressConfirmation: await walletStrategy.getSessionOrConfirm(
         injectiveAddress
       ),
       session
     })

+    // Finalize the connection process
     await walletStore.onConnect()
+  } catch (error) {
+    console.error('Failed to connect OWallet:', error)
+    // Handle the error appropriately (e.g., show an error message to the user)
+  }
 },
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 3230261 and 34824fa.

⛔ Files ignored due to path filters (1)
  • layer/yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (5)
  • layer/icons/wallet-new/owallet.vue (1 hunks)
  • layer/icons/wallet/owallet.vue (1 hunks)
  • layer/store/wallet.ts (2 hunks)
  • layer/wallet/cosmos.ts (2 hunks)
  • packages/ui-shared/playground/icons.json (2 hunks)
🔇 Additional comments (5)
layer/icons/wallet-new/owallet.vue (1)

1-20: Overall, the OWallet icon component is well-implemented with room for minor improvements.

The component successfully creates an SVG icon for OWallet, which aligns with the PR objective of adding an icon for OWallet. The implementation is generally good, with a clean structure and visually appealing design.

To further enhance this component:

  1. Improve the UUID generation in the script section for better uniqueness and unpredictability.
  2. Add accessibility attributes to the SVG element in the template section.

These improvements will make the component more robust and accessible while maintaining its core functionality.

layer/wallet/cosmos.ts (2)

9-9: LGTM: Import statement for OWalletBase

The import statement for OWalletBase is correctly added and is consistent with other wallet-related imports in the file. This import is necessary for the new confirmCorrectOWalletAddress function.


Line range hint 1-56: Summary: OWallet support successfully implemented

The changes in this file successfully implement support for OWallet, which aligns with the PR objectives to integrate OWallet support in the Injective UI. The new confirmCorrectOWalletAddress function mirrors the existing Keplr wallet validation, maintaining consistency in the codebase.

While the implementation is generally good, consider the suggested improvements for error handling and async operation consistency to enhance robustness and maintainability.

Overall, these changes effectively contribute to the goal of supporting OWallet in the application.

layer/store/wallet.ts (2)

23-23: LGTM: Import addition for OWallet support

The addition of confirmCorrectOWalletAddress import is consistent with the existing code style and supports the new OWallet connection functionality.


Line range hint 1-1043: Overall assessment: OWallet integration looks good

The changes to add OWallet support in this file are well-implemented and consistent with the existing code patterns. The new connectOWallet method and the related import have been added correctly. With the suggested improvements for error handling and comments, this implementation will be robust and maintainable.

Comment on lines +1 to +3
<script lang="ts" setup>
const uuid = `${Math.random()}`
</script>
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider using a more robust UUID generation method.

The current implementation uses Math.random() to generate a UUID, which doesn't guarantee uniqueness or unpredictability. This could potentially lead to conflicts if multiple instances of the component are rendered.

Consider using a dedicated UUID library or a more robust method for generating UUIDs. Here's an example using the uuid package:

 <script lang="ts" setup>
-const uuid = `${Math.random()}`
+import { v4 as uuidv4 } from 'uuid';
+const uuid = uuidv4();
 </script>

Make sure to install the uuid package:

npm install uuid
npm install --save-dev @types/uuid

This approach ensures a more reliable and standardized UUID generation.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<script lang="ts" setup>
const uuid = `${Math.random()}`
</script>
<script lang="ts" setup>
import { v4 as uuidv4 } from 'uuid';
const uuid = uuidv4();
</script>

Comment on lines +1 to +3
<script lang="ts" setup>
const uuid = `${Math.random()}`
</script>
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider using a more robust UUID generation method.

The current implementation uses Math.random() to generate a UUID, which doesn't guarantee uniqueness or unpredictability. This could potentially lead to conflicts if multiple instances of the component are rendered.

Consider using a dedicated UUID library or a more robust method for generating UUIDs. Here's an example using the uuid package:

 <script lang="ts" setup>
-const uuid = `${Math.random()}`
+import { v4 as uuidv4 } from 'uuid';
+const uuid = uuidv4();
 </script>

Make sure to install the uuid package:

npm install uuid
npm install --save-dev @types/uuid

This approach will provide a more reliable and standardized way of generating UUIDs.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<script lang="ts" setup>
const uuid = `${Math.random()}`
</script>
<script lang="ts" setup>
import { v4 as uuidv4 } from 'uuid';
const uuid = uuidv4();
</script>

Comment on lines +5 to +21
<template>
<svg width="166" height="165" viewBox="0 0 166 165" fill="none" xmlns="http://www.w3.org/2000/svg">
<path opacity="0.51" d="M145.129 56.7787H94.4052C91.9231 56.7787 89.8652 54.7633 89.8652 52.2388V30.5363C89.8652 28.0542 91.8806 25.9963 94.4052 25.9963H147.399C148.672 25.9963 149.669 26.9934 149.669 28.2663V52.2812C149.627 54.7633 147.611 56.7787 145.129 56.7787Z" fill="#AD83FF"/>
<path opacity="0.5" d="M132.442 46.2768H85.5575C84.1574 46.2768 83.0754 45.1312 83.0754 43.7947V17.9977C83.0754 16.5976 84.221 15.5156 85.5575 15.5156H132.484C133.884 15.5156 134.966 16.6612 134.966 17.9977V43.7947C134.987 45.1948 133.842 46.2768 132.442 46.2768Z" fill="#AD83FF"/>
<path opacity="0.9" d="M163.352 68.5518H108.343C107.028 68.5518 105.946 67.5122 105.946 66.1545V39.4666C105.946 38.1513 106.985 37.0693 108.343 37.0693H163.352C164.668 37.0693 165.75 38.1088 165.75 39.4666V66.1333C165.771 67.4486 164.668 68.5518 163.352 68.5518Z" fill="#AD83FF"/>
<path d="M145.32 129.311C130.809 149.698 106.985 163 80.0638 163C35.8526 163 0 127.147 0 82.9358C0 40.6552 32.7977 6.01179 74.3358 3.08418C74.6752 3.08418 74.9935 3.04175 75.3329 3.04175C77.9635 3.12661 80.0638 5.09956 80.0638 7.53924V36.4123C54.3305 36.4123 33.4978 57.2662 33.4978 82.9782C33.4978 108.69 54.3517 129.544 80.0638 129.544C105.776 129.544 126.63 108.69 126.63 82.9782C126.63 82.9782 152.66 93.734 145.32 129.311Z" fill="#925AFF"/>
<path d="M159.916 89.1109C159.513 85.6529 156.415 82.9799 152.618 82.9799H85.474C82.5039 82.9799 80.0855 80.5826 80.0855 77.5914V7.54086C80.0855 5.10119 77.9852 3.12823 75.3546 3.04338C75.6516 3.04338 75.9698 3.00094 76.2668 3.00094C79.6824 2.95851 81.5068 4.35868 81.9948 4.80418L82.0372 4.84661L82.1221 4.93147L154.23 77.0398C160.34 83.192 159.916 89.1109 159.916 89.1109Z" fill="#AD83FF"/>
<path opacity="0.91" d="M159.873 89.0451C159.831 89.9362 159.746 90.806 159.661 91.6333C158.155 105.571 153.063 118.449 145.32 129.31C152.66 93.7123 126.63 82.9778 126.63 82.9778H152.597C156.415 82.9778 159.513 85.6508 159.873 89.0451Z" fill="url(#paint0_linear_4242_32575)"/>
<defs>
<linearGradient id="paint0_linear_4242_32575" x1="143.252" y1="131.138" x2="143.252" y2="83.1802" gradientUnits="userSpaceOnUse">
<stop offset="8.52446e-08" stop-color="#925AFF"/>
<stop offset="1" stop-color="#713BBB"/>
</linearGradient>
</defs>
</svg>

</template>
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider extracting the SVG to a separate file.

While inline SVGs offer easy manipulation, they can impact performance if the component is frequently used. For a more optimized approach, consider extracting the SVG to a separate file.

  1. Create a new file owallet-icon.svg in the appropriate assets directory.
  2. Move the SVG content to this new file.
  3. Import and use the SVG file in your component:
 <script lang="ts" setup>
 const uuid = `${Math.random()}`
+import OWalletIcon from '@/assets/icons/owallet-icon.svg'
 </script>

 <template>
-  <svg width="166" height="165" viewBox="0 0 166 165" fill="none" xmlns="http://www.w3.org/2000/svg">
-    <!-- ... SVG content ... -->
-  </svg>
+  <img :src="OWalletIcon" alt="OWallet Icon" width="166" height="165" />
 </template>

This approach can improve performance, especially if the icon is used in multiple places.

Committable suggestion was skipped due to low confidence.

Comment on lines +35 to +56
export const confirmCorrectOWalletAddress = async (injectiveAddress: string) => {
// We only perform this check for OWallet addresses
if (walletStrategy.getWallet() !== Wallet.OWallet) {
return
}

const owallet = new OWalletBase(CHAIN_ID)
const key = await owallet.getKey()
const publicKey = PublicKey.fromBase64(
Buffer.from(key.pubKey).toString('base64')
)

const { address: derivedAddress } = publicKey.toAddress()

if (derivedAddress !== injectiveAddress) {
throw new CosmosWalletException(
new Error(
'Connected OWallet address is wrong. Please update Injective on OWallet.'
)
)
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling and provide more informative error messages

The confirmCorrectOWalletAddress function is a good addition and mirrors the existing confirmCorrectKeplrAddress function. However, there are a few suggestions for improvement:

  1. Add await to the owallet.getKey() call on line 42, as it's an asynchronous operation.
  2. Implement error handling for potential exceptions from owallet.getKey() or publicKey.toAddress().
  3. Enhance the error message to include the mismatched addresses for easier debugging.

Consider refactoring the function as follows:

export const confirmCorrectOWalletAddress = async (injectiveAddress: string) => {
  if (walletStrategy.getWallet() !== Wallet.OWallet) {
    return
  }

  try {
    const owallet = new OWalletBase(CHAIN_ID)
    const key = await owallet.getKey()
    const publicKey = PublicKey.fromBase64(
      Buffer.from(key.pubKey).toString('base64')
    )

    const { address: derivedAddress } = publicKey.toAddress()

    if (derivedAddress !== injectiveAddress) {
      throw new CosmosWalletException(
        new Error(
          `Connected OWallet address is incorrect. Expected: ${injectiveAddress}, Got: ${derivedAddress}`
        )
      )
    }
  } catch (error) {
    if (error instanceof CosmosWalletException) {
      throw error
    }
    throw new CosmosWalletException(
      new Error(`Failed to validate OWallet address: ${error.message}`)
    )
  }
}

This refactored version:

  • Correctly awaits the asynchronous owallet.getKey() call.
  • Adds error handling for potential exceptions.
  • Provides a more informative error message that includes both the expected and actual addresses.

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.

1 participant