-
Notifications
You must be signed in to change notification settings - Fork 59
feat(sdk): once-per-identity token distribution in the mobile example apps #4829
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
6 commits
Select commit
Hold shift + click to select a range
4d9a55a
feat(sdk): once-per-identity token distribution in the mobile example…
QuantumExplorer 452c2e8
fix(sdk): once-per-identity claims on Android default to the eligible…
QuantumExplorer 5708e55
fix(sdk): once-per-identity claims on iOS default to the eligible kind
QuantumExplorer c5dbccf
fix(sdk): recognise the already-claimed code 40722 as a number of its…
QuantumExplorer 58e63b5
fix(sdk): offer only the claim kinds the identity is eligible for on …
QuantumExplorer f29028b
fix(sdk): offer only the claim kinds the identity is eligible for on iOS
QuantumExplorer 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
69 changes: 69 additions & 0 deletions
69
...app/src/main/java/org/dashfoundation/example/services/tokens/OncePerIdentityClaimStore.kt
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,69 @@ | ||
| package org.dashfoundation.example.services.tokens | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.core.booleanPreferencesKey | ||
| import androidx.datastore.preferences.core.edit | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.catch | ||
| import kotlinx.coroutines.flow.map | ||
| import org.dashfoundation.example.util.toHex | ||
|
|
||
| /** | ||
| * Device-local memory of the once-per-identity distributions an identity | ||
| * already claimed (protocol version 14). | ||
| * | ||
| * Platform has no "has this identity claimed" query yet, and a second claim | ||
| * is a paid rejection (`TokenOncePerIdentityDistributionAlreadyClaimedError`, | ||
| * code 40722), so the app remembers what it learned: a claim it submitted | ||
| * that succeeded, or one that came back as already claimed. A fresh install | ||
| * starts empty and learns the state from the first rejection. | ||
| */ | ||
| class OncePerIdentityClaimStore( | ||
| private val dataStore: DataStore<Preferences>, | ||
| ) { | ||
| fun observe(networkRaw: Int, tokenId: ByteArray, identityId: ByteArray): Flow<Boolean> { | ||
| val key = preferenceKey(networkRaw, tokenId, identityId) | ||
| return dataStore.data | ||
| .map { preferences -> preferences[key] == true } | ||
| .catch { emit(false) } | ||
| } | ||
|
|
||
| suspend fun markClaimed(networkRaw: Int, tokenId: ByteArray, identityId: ByteArray) { | ||
| dataStore.edit { preferences -> | ||
| preferences[preferenceKey(networkRaw, tokenId, identityId)] = true | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| /** Consensus code of `TokenOncePerIdentityDistributionAlreadyClaimedError`. */ | ||
| const val ALREADY_CLAIMED_ERROR_CODE = 40722 | ||
|
|
||
| /** | ||
| * The code as a number of its own. Error texts carry amounts and | ||
| * millisecond timestamps, and a claim time such as 1758140722000 | ||
| * contains the digits, so a plain substring match would take an | ||
| * unrelated failure for a spent claim and hide the kind for good. | ||
| */ | ||
| private val ALREADY_CLAIMED_CODE_PATTERN = | ||
| Regex("(?<![0-9])$ALREADY_CLAIMED_ERROR_CODE(?![0-9])") | ||
|
|
||
| /** | ||
| * True when [error] is the already-claimed rejection. The native | ||
| * layer surfaces consensus errors as text, so match the code and the | ||
| * message rs-dpp renders for it. | ||
| */ | ||
| fun isAlreadyClaimed(error: Throwable): Boolean { | ||
| val message = generateSequence(error) { it.cause } | ||
| .mapNotNull { it.message } | ||
| .joinToString(" ") | ||
| return ALREADY_CLAIMED_CODE_PATTERN.containsMatchIn(message) || | ||
| message.contains("already claimed the once-per-identity distribution") | ||
| } | ||
|
|
||
| private fun preferenceKey(networkRaw: Int, tokenId: ByteArray, identityId: ByteArray) = | ||
| booleanPreferencesKey( | ||
| "once_per_identity_claimed.$networkRaw.${tokenId.toHex()}.${identityId.toHex()}", | ||
| ) | ||
| } | ||
| } | ||
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.
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.