-
Notifications
You must be signed in to change notification settings - Fork 19
fix(sdk): centralize UTXO address brand validation #1892
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e02dff
fix(sdk): centralize UTXO address brand validation
rcoderdev 31ba073
fix(sdk): preserve shielded Zcash address validation
rcoderdev ce5949d
fix(sdk): harden UTXO address format validation
rcoderdev 12bfd4c
test(sdk): align CashAddr casing expectations
rcoderdev 7654b50
fix(sdk): align uppercase UTXO classification
rcoderdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@vultisig/sdk': patch | ||
| --- | ||
|
|
||
| Add a canonical UTXO address-brand validator and enforce it in the transaction decoder. |
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,96 @@ | ||
| import { bech32, bech32m } from '@scure/base' | ||
| import bs58check from 'bs58check' | ||
|
|
||
| import { isValidCashAddr } from '../../utils/cashaddr' | ||
|
|
||
| export type UtxoChainName = 'Bitcoin' | 'Litecoin' | 'Dogecoin' | 'Dash' | 'Bitcoin-Cash' | 'Zcash' | ||
|
|
||
| const BASE58_VERSION_BYTES: Partial<Record<UtxoChainName, ReadonlySet<number>>> = { | ||
| Bitcoin: new Set([0x00, 0x05]), | ||
| Litecoin: new Set([0x30, 0x32]), | ||
| Dogecoin: new Set([0x1e, 0x16]), | ||
| Dash: new Set([0x4c, 0x10]), | ||
| } | ||
|
|
||
| const SEGWIT_HRP: Partial<Record<UtxoChainName, string>> = { | ||
| Bitcoin: 'bc', | ||
| Litecoin: 'ltc', | ||
| } | ||
|
|
||
| function hasExpectedSegwitBrand(address: string, expectedHrp: string): boolean { | ||
| for (const codec of [bech32, bech32m]) { | ||
| try { | ||
| const { prefix, words } = codec.decode(address as `${string}1${string}`) | ||
| const version = words[0] | ||
| if (prefix !== expectedHrp || version === undefined || version > 16) continue | ||
|
|
||
| const program = codec.fromWords(words.slice(1)) | ||
| if (program.length < 2 || program.length > 40) continue | ||
| if (version === 0) return codec === bech32 && (program.length === 20 || program.length === 32) | ||
| return codec === bech32m | ||
| } catch { | ||
| // Try the other checksum variant, then the non-SegWit formats below. | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| function hasExpectedZcashSaplingBrand(address: string): boolean { | ||
| try { | ||
| const decoded = bech32.decode(address as `${string}1${string}`) | ||
| return decoded.prefix === 'zs' && bech32.fromWords(decoded.words).length === 43 | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function hasExpectedBase58Version(address: string, chain: UtxoChainName): boolean { | ||
| let decoded: Uint8Array | ||
| try { | ||
| decoded = bs58check.decode(address) | ||
| } catch { | ||
| return false | ||
| } | ||
|
|
||
| if (chain === 'Zcash') { | ||
| return decoded.length === 22 && decoded[0] === 0x1c && (decoded[1] === 0xb8 || decoded[1] === 0xbd) | ||
| } | ||
|
|
||
| const versions = BASE58_VERSION_BYTES[chain] | ||
| return decoded.length === 21 && decoded[0] !== undefined && versions?.has(decoded[0]) === true | ||
| } | ||
|
|
||
| /** | ||
| * Return whether a checksummed UTXO address carries the brand for `chain`. | ||
| * | ||
| * This validates chain identity, not whether every branded script type is | ||
| * spendable by the current transaction builder. The policy is intentionally | ||
| * mainnet-only: | ||
| * - Bitcoin / Litecoin: Bech32/Bech32m HRP or exact Base58Check version byte | ||
| * - Dogecoin / Dash: exact Base58Check version byte | ||
| * - Bitcoin Cash: checksummed mainnet CashAddr | ||
| * - Zcash: transparent t1/t3 Base58Check versions or Sapling zs Bech32 HRP | ||
| * | ||
| * Ambiguous legacy Bitcoin Cash and Litecoin P2SH encodings are excluded so a | ||
| * Bitcoin-looking address is never accepted for another chain by accident. | ||
| */ | ||
| export function isUtxoAddressBrandValid(address: string, chain: UtxoChainName): boolean { | ||
| const normalized = address.trim() | ||
| if (normalized === '') return false | ||
|
|
||
| if (chain === 'Bitcoin-Cash') return isValidCashAddr(normalized) | ||
| if (chain === 'Zcash' && hasExpectedZcashSaplingBrand(normalized)) return true | ||
|
|
||
| const expectedHrp = SEGWIT_HRP[chain] | ||
| if (expectedHrp && hasExpectedSegwitBrand(normalized, expectedHrp)) return true | ||
|
|
||
| return hasExpectedBase58Version(normalized, chain) | ||
| } | ||
|
|
||
| /** Fail closed when an address is malformed or belongs to another UTXO chain. */ | ||
| export function assertUtxoAddressBrand(address: string, chain: UtxoChainName): void { | ||
| if (!isUtxoAddressBrandValid(address, chain)) { | ||
| throw new Error(`UTXO address brand mismatch: expected a valid ${chain} address`) | ||
| } | ||
| } |
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
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.