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

refactor: input calc helper methods #5363

Merged
merged 1 commit into from
May 13, 2024
Merged

Conversation

kyranjamie
Copy link
Collaborator

@kyranjamie kyranjamie commented May 13, 2024

Try out Leather build 28b8d41Extension build, Test report, Storybook, Chromatic

Removes a confusing API choice of the helpers to get an input's address or amount being spent. Reads directly from the input.

Also turns a type literal to singular, cc discussion with @fbwoolf

Summary by CodeRabbit

  • Refactor
    • Enhanced clarity in type naming conventions for payment types in the Bitcoin utilities.
  • Bug Fixes
    • Simplified the input parsing in the PSBT signer feature by removing an unnecessary parameter.
  • Refactor
    • Streamlined the signing configuration in the Bitcoin utilities by adjusting the getAssumedZeroIndexSigningConfig function.

Copy link

coderabbitai bot commented May 13, 2024

Walkthrough

The updates involve refining Bitcoin-related utility modules by renaming types, adjusting import statements for clarity, and removing unnecessary function parameters in hooks. These changes aim to enhance code readability and maintainability, reflecting a commitment to code quality.

Changes

File Path Change Summary
.../hooks/use-parsed-inputs.tsx Removed the index parameter in the getBitcoinInputAddress function call.
src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts Removed the inputIndex parameter in the getInputPaymentType function call.
src/shared/crypto/bitcoin/bitcoin.utils.ts Renamed types and adjusted import statements for clarity and consistency within the utility module.
src/shared/crypto/bitcoin/signer-config.ts Updated getAssumedZeroIndexSigningConfig to remove input.index from getInputPaymentType call.

🐰✨
Changes in the code, tidy and neat,
Refining the lines where the bytes meet.
A rabbit hopped through, with a coder's cheer,
Tweaking the types, making all clear.
Now hop away to the next task, my dear!
🌟🐇


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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: 2

Out of diff range and nitpick comments (1)
src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx (1)

Line range hint 63-63: Fix incorrect number of arguments in function call.

- value: isDefined(input.index) ? getBitcoinInputValue(input.index, input) : 0,
+ value: isDefined(input.index) ? getBitcoinInputValue(input) : 0,

The getBitcoinInputValue function expects a single btc.TransactionInput argument, but was incorrectly called with two arguments. This fix aligns with the function's expected signature.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 45c9968 and dc10547.
Files selected for processing (2)
  • src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx (1 hunks)
  • src/shared/crypto/bitcoin/bitcoin.utils.ts (4 hunks)
Additional Context Used
GitHub Check Runs (2)
typecheck failure (4)

src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx: [failure] 63-63:
Expected 1 arguments, but got 2.


src/shared/crypto/bitcoin/bitcoin.utils.ts: [failure] 271-271:
Expected 2 arguments, but got 3.

test-unit failure (5)

src/shared/crypto/bitcoin/bitcoin.utils.ts: [failure] 272-272: src/shared/crypto/bitcoin/signer-config.spec.ts > getAssumedZeroIndexSigningConfig > for a given transaction with p2wpkh
Error: Input address cannot be empty
❯ Module.getInputPaymentType src/shared/crypto/bitcoin/bitcoin.utils.ts:272:29
❯ src/shared/crypto/bitcoin/signer-config.ts:36:29
❯ Object.forAccountIndex src/shared/crypto/bitcoin/signer-config.ts:32:22
❯ src/shared/crypto/bitcoin/signer-config.spec.ts:40:8


src/shared/crypto/bitcoin/bitcoin.utils.ts: [failure] 272-272: src/shared/crypto/bitcoin/signer-config.spec.ts > getAssumedZeroIndexSigningConfig > for a given transaction with p2tr
Error: Input address cannot be empty
❯ Module.getInputPaymentType src/shared/crypto/bitcoin/bitcoin.utils.ts:272:29
❯ src/shared/crypto/bitcoin/signer-config.ts:36:29
❯ Object.forAccountIndex src/shared/crypto/bitcoin/signer-config.ts:32:22
❯ src/shared/crypto/bitcoin/signer-config.spec.ts:60:8


src/shared/crypto/bitcoin/bitcoin.utils.ts: [failure] 272-272: src/shared/crypto/bitcoin/signer-config.spec.ts > getAssumedZeroIndexSigningConfig > it only returns config for inputs given, if passed
Error: Input address cannot be empty
❯ Module.getInputPaymentType src/shared/crypto/bitcoin/bitcoin.utils.ts:272:29
❯ src/shared/crypto/bitcoin/signer-config.ts:36:29
❯ Object.forAccountIndex src/shared/crypto/bitcoin/signer-config.ts:32:22
❯ src/shared/crypto/bitcoin/signer-config.spec.ts:79:8

Additional comments not posted (2)
src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx (1)

44-44: Refactor to simplify address retrieval logic.

The change to directly use getBitcoinInputAddress without the index parameter simplifies the logic and reduces potential errors by directly associating the address with the input object itself.

src/shared/crypto/bitcoin/bitcoin.utils.ts (1)

1-1: Update import alias to reflect singular form.

Changing the import alias from PaymentTypes to PaymentType aligns with the decision to use singular form for type names, enhancing consistency across the codebase.

src/shared/crypto/bitcoin/bitcoin.utils.ts Show resolved Hide resolved
@@ -263,9 +267,9 @@
index: number,
input: btc.TransactionInput,
network: BitcoinNetworkModes
): PaymentTypes {
): PaymentType {
const address = getBitcoinInputAddress(index, input, getBtcSignerLibNetworkConfigByMode(network));
if (address === '') throw new Error('Input address cannot be empty');
Copy link

Choose a reason for hiding this comment

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

Handle cases where input address is empty.

- if (address === '') throw new Error('Input address cannot be empty');
+ if (address === '') {
+   logger.error('Input address is empty, unable to determine payment type.');
+   return 'unknown'; // or handle as appropriate for your use case
+ }

This modification adds error handling for cases where the input address is empty, preventing runtime errors and allowing the application to continue operating, possibly with a default or fallback behavior.


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.

Suggested change
if (address === '') throw new Error('Input address cannot be empty');
if (address === '') {
logger.error('Input address is empty, unable to determine payment type.');
return 'unknown'; // or handle as appropriate for your use case
}

@kyranjamie kyranjamie force-pushed the refactor/input-calc-fns branch from dc10547 to 28b8d41 Compare May 13, 2024 11:53
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: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between dc10547 and 28b8d41.
Files selected for processing (4)
  • src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx (2 hunks)
  • src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts (1 hunks)
  • src/shared/crypto/bitcoin/bitcoin.utils.ts (4 hunks)
  • src/shared/crypto/bitcoin/signer-config.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts
Files skipped from review as they are similar to previous changes (2)
  • src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx
  • src/shared/crypto/bitcoin/bitcoin.utils.ts

@@ -33,7 +33,7 @@ export function getAssumedZeroIndexSigningConfig({
const input = tx.getInput(inputIndex);

if (isUndefined(input.index)) throw new Error('Input must have an index for payment type');
const paymentType = getInputPaymentType(input.index, input, network);
const paymentType = getInputPaymentType(input, network);
Copy link

Choose a reason for hiding this comment

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

Consider throwing an exception for non-segwit input types instead of just logging an error. This would enforce stronger guarantees about the function's behavior and ensure that calling functions can handle this case appropriately.

Copy link
Contributor

@pete-watters pete-watters left a comment

Choose a reason for hiding this comment

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

Thanks for updating

@kyranjamie kyranjamie added this pull request to the merge queue May 13, 2024
Merged via the queue into dev with commit 16a9e6e May 13, 2024
28 checks passed
@kyranjamie kyranjamie deleted the refactor/input-calc-fns branch May 13, 2024 14:27
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.

3 participants