fix(globe-wallet): validate asset code against UserAssets in spend limits and canonicalize storage keys - #101
Closed
s6pa1rta3n-lab wants to merge 1 commit into
Conversation
…mits and canonicalize storage keys
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #88
Root cause
In
contracts/globe-wallet/src/lib.rs,GlobeWallet::set_spend_limitandGlobeWallet::record_spendtook an arbitraryasset_code: Stringargument and used it directly as part of storage keys (DataKey::SpendLimit(user, asset_code)andDataKey::DailySpent(user, asset_code)). Unlikeadd_asset(which case-normalizes asset codes viacodes_match_case_insensitive),set_spend_limit,get_spend_limit, andrecord_spendnever checked whetherasset_codebelonged toUserAssets(user)or case-normalized it.As a result:
"USDC"vs"usdc") mapped to separate storage keys, allowing spends to bypass configured daily limits.UserAssets.What changed and why
Helper
resolve_registered_asset_code:resolve_registered_asset_code(env, user, asset_code) -> Result<String, WalletError>.DataKey::UserAssets(user)usingSelf::codes_match_case_insensitiveto find the matching registered asset.asset.codefor use as storage keys, orErr(WalletError::AssetNotFound)if no match is found.Validation and Canonical Storage Keys in Spend Limit Operations:
set_spend_limit: Validatesasset_codeagainstUserAssets(user)(returningWalletError::AssetNotFoundif unregistered) and uses the canonical registered code forDataKey::SpendLimit,DataKey::DailySpent, and event emission.get_spend_limit: Resolvesasset_codecase-insensitively againstUserAssets(user). Returns0(unlimited / default) if unregistered or unset, and retrieves the configured limit under the canonical key if registered.record_spend: Validatesasset_codeagainstUserAssets(user)(returningWalletError::AssetNotFoundif unregistered) and records daily spend against the canonical storage key.Consistency in
remove_asset:remove_assetto also usecodes_match_case_insensitivewhen locating the asset to remove, ensuring consistent case-insensitive asset identification across all wallet operations.Test Suite Updates and Additions:
test_case_variant_asset_code_hits_same_spend_limit_bucketproving that case variants (e.g.,"USDC"and"usdc") share the exact same daily spend limit bucket.test_set_spend_limit_rejects_unregistered_assetandtest_record_spend_rejects_unregistered_assetproving calls for unregistered assets are rejected withWalletError::AssetNotFound.test_set_spend_limit_with_case_variant_configures_canonical_bucketproving limits configured under a case-variant string correctly apply to the canonical asset bucket.add_assetprior to configuring limits or recording spends.Definition of done — addressed item by item
set_spend_limit/record_spendvalidateasset_codeagainst the caller'sUserAssets, using the same canonicalizationadd_assetuses:resolve_registered_asset_codeconsultsUserAssets(user)viacodes_match_case_insensitiveand returnsWalletError::AssetNotFoundif not registered."USDC"/"usdc"land in the same bucket: Bothset_spend_limit,get_spend_limit, andrecord_spendresolve to the canonical registered code stored inUserAssetsbefore reading/writingDataKey::SpendLimitandDataKey::DailySpent.test_case_variant_asset_code_hits_same_spend_limit_bucketandtest_set_spend_limit_with_case_variant_configures_canonical_bucket.record_spend/set_spend_limitreject anasset_codethat doesn't correspond to any registered asset: Addedtest_set_spend_limit_rejects_unregistered_assetandtest_record_spend_rejects_unregistered_asset.cargo test --workspaceoutput pasted: Pasted in Evidence section below.Evidence this actually runs
Tests
test_case_variant_asset_code_hits_same_spend_limit_bucketasserting that spending against"usdc"and"USDC"accumulates toward the same configured daily limit and rejects spends when the aggregate exceeds the limit.test_set_spend_limit_rejects_unregistered_assetassertingset_spend_limitfails withWalletError::AssetNotFoundwhen called for an unregistered asset code.test_record_spend_rejects_unregistered_assetassertingrecord_spendfails withWalletError::AssetNotFoundwhen called for an unregistered asset code.test_set_spend_limit_with_case_variant_configures_canonical_bucketasserting setting limits with lowercase strings correctly configures the canonical asset bucket.Regression check
All existing contract features and workflows continue to pass without regression:
record_spendUserAssets,SpendLimit, andDailySpentChecklist
console.log/TODO/debug codePayout Routing
0xF46C9F6d70C50BF81ef3588AB523a90a594a2F89GCL6OXAMLD75BMTINA6EMRUDWK5THQUSHMYNLSNBCJAPZJHNYJTUNIBC