Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 307
fix(abstract-utxo): reject invalid chain and index values#9401
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
Open
Daan-25
wants to merge
1
commit into
BitGo:masterChoose a base branch
from
Daan-25:fix/fixed-script-invalid-index
base:master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Uh oh!
There was an error while loading. Please reload this page.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -72,12 +72,20 @@ export function generateAddressWithChainAndIndex( | ||
| */ | ||
| export function generateAddress(coinName: UtxoCoinName, params: GenerateFixedScriptAddressOptions): string { | ||
| let derivationIndex = 0; | ||
| if (_.isInteger(params.index) && (params.index as number) > 0) { | ||
| if (!_.isUndefined(params.index)) { | ||
| if (!_.isInteger(params.index) || (params.index as number) < 0) { | ||
| throw new InvalidAddressDerivationPropertyError(`address validation failure: invalid index (${params.index})`); | ||
| } | ||
| derivationIndex = params.index as number; | ||
| } | ||
Daan-25 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const { keychains, chain, segwit = false, bech32 = false } = params as GenerateFixedScriptAddressOptions; | ||
| if (!_.isUndefined(chain) && !_.isInteger(chain)) { | ||
| throw new InvalidAddressDerivationPropertyError(`address validation failure: invalid chain (${chain})`); | ||
| } | ||
| if (_.isNumber(chain) && _.isInteger(chain) && !fixedScriptWallet.ChainCode.is(chain)) { | ||
| throw new InvalidAddressDerivationPropertyError(`address validation failure: unrecognised chain code (${chain})`); | ||
| } | ||
| @@ -157,7 +165,13 @@ export function assertFixedScriptWalletAddress( | ||
| address: string; | ||
| } | ||
| ): void { | ||
| if ((_.isUndefined(chain) && _.isUndefined(index)) || !(_.isFinite(chain) && _.isFinite(index))) { | ||
| if ( | ||
| (_.isUndefined(chain) && _.isUndefined(index)) || | ||
| !(_.isFinite(chain) && _.isFinite(index)) || | ||
| !_.isInteger(chain) || | ||
| !_.isInteger(index) || | ||
| (index as number) < 0 | ||
| ) { | ||
| throw new InvalidAddressDerivationPropertyError( | ||
| `address validation failure: invalid chain (${chain}) or index (${index})` | ||
| ); | ||
105 changes: 105 additions & 0 deletions
105 modules/abstract-utxo/test/unit/address-index-edge.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import * as assert from 'assert'; | ||
| import { InvalidAddressDerivationPropertyError } from '@bitgo/sdk-core'; | ||
| import { assertFixedScriptWalletAddress, generateAddress } from '../../src'; | ||
| const keychains = [ | ||
| { | ||
| pub: 'xpub661MyMwAqRbcGiQhVk1J7cD1YodF9tc5Y1B8vpTjjB1pcB1J1m1QX8fMtYP2sYqFmW6J2ra69tNoARKjvTGo9cGUrbPbJdjwrSzGGzPzWWS', | ||
| }, | ||
| { | ||
| pub: 'xpub661MyMwAqRbcFzLXuganogQvd7MrefQQqCcJP2ZDumnCdQecf5cw1P1nD5qBz8SNS1yCLSC9VqpNUWnQU3V6qmnPt2r21oXhicQFzPA6Lby', | ||
| }, | ||
| { | ||
| pub: 'xpub661MyMwAqRbcFHpwWrzPB61U2CgBmdD21WNVM1JKUn9rEExkoGE4yafUVFbPSd78vdX8tWcEUQWaALFkU9fUbUM4Cc49DKEJSCYGRnbzCym', | ||
| }, | ||
| ]; | ||
| describe('fixedScript address index edge cases', function () { | ||
| const chain = 20; | ||
| for (const invalidIndex of [-1, 1.5]) { | ||
| it(`rejects invalid derivation index ${invalidIndex}`, function () { | ||
| assert.throws( | ||
| () => | ||
| generateAddress('btc', { | ||
| keychains, | ||
| chain, | ||
| index: invalidIndex, | ||
| }), | ||
| InvalidAddressDerivationPropertyError | ||
| ); | ||
| }); | ||
| it(`rejects index ${invalidIndex} during address validation`, function () { | ||
| const address0 = generateAddress('btc', { | ||
| keychains, | ||
| chain, | ||
| index: 0, | ||
| }); | ||
| assert.throws( | ||
| () => | ||
| assertFixedScriptWalletAddress('btc', { | ||
| chain, | ||
| index: invalidIndex, | ||
| keychains, | ||
| format: 'base58', | ||
| address: address0, | ||
| }), | ||
| InvalidAddressDerivationPropertyError | ||
| ); | ||
| }); | ||
| } | ||
| it('rejects a non-integer derivation chain', function () { | ||
| assert.throws( | ||
| () => | ||
| generateAddress('btc', { | ||
| keychains, | ||
| chain: 1.5, | ||
| index: 0, | ||
| }), | ||
| InvalidAddressDerivationPropertyError | ||
| ); | ||
| }); | ||
| it('rejects a non-integer chain during address validation', function () { | ||
| const address = generateAddress('btc', { | ||
| keychains, | ||
| chain: 0, | ||
| index: 0, | ||
| }); | ||
| assert.throws( | ||
| () => | ||
| assertFixedScriptWalletAddress('btc', { | ||
| chain: 1.5, | ||
| index: 0, | ||
| keychains, | ||
| format: 'base58', | ||
| address, | ||
| }), | ||
| InvalidAddressDerivationPropertyError | ||
| ); | ||
| }); | ||
| it('still accepts index 0', function () { | ||
| const address0 = generateAddress('btc', { | ||
| keychains, | ||
| chain, | ||
| index: 0, | ||
| }); | ||
| assert.doesNotThrow(() => | ||
| assertFixedScriptWalletAddress('btc', { | ||
| chain, | ||
| index: 0, | ||
| keychains, | ||
| format: 'base58', | ||
| address: address0, | ||
| }) | ||
| ); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Can we do this without casting?