-
Notifications
You must be signed in to change notification settings - Fork 149
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
Conversation
WalkthroughThe 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
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this 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 singlebtc.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
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 theindex
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
toPaymentType
aligns with the decision to use singular form for type names, enhancing consistency across the codebase.
@@ -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'); |
There was a problem hiding this comment.
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.
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 | |
} |
dc10547
to
28b8d41
Compare
There was a problem hiding this 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
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updating
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
getAssumedZeroIndexSigningConfig
function.