-
Notifications
You must be signed in to change notification settings - Fork 486
Add Zano alias resolution to send flow #9393
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
2 commits
Select commit
Hold shift + click to select a range
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
63 changes: 63 additions & 0 deletions
63
walletkit/src/main/java/io/horizontalsystems/walletkit/core/address/ZanoAliasResolver.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,63 @@ | ||
| package io.horizontalsystems.walletkit.core.address | ||
|
|
||
| import com.google.gson.JsonObject | ||
| import com.google.gson.JsonParser | ||
| import io.horizontalsystems.walletkit.core.managers.ZanoNodeManager | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okhttp3.RequestBody.Companion.toRequestBody | ||
| import java.io.IOException | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| class ZanoAliasResolver(private val zanoNodeManager: ZanoNodeManager) { | ||
|
|
||
| private val httpClient: OkHttpClient = OkHttpClient.Builder() | ||
| .connectTimeout(10, TimeUnit.SECONDS) | ||
| .readTimeout(10, TimeUnit.SECONDS) | ||
| .build() | ||
|
|
||
| fun resolve(alias: String): String? { | ||
| val baseUrl = zanoNodeManager.currentNode.host.trimEnd('/') | ||
|
|
||
| val requestBody = JsonObject().apply { | ||
| addProperty("id", 0) | ||
| addProperty("jsonrpc", "2.0") | ||
| addProperty("method", "get_alias_details") | ||
| add("params", JsonObject().apply { | ||
| addProperty("alias", alias) | ||
| }) | ||
| } | ||
|
|
||
| val request = Request.Builder() | ||
| .url("$baseUrl/json_rpc") | ||
| .post(requestBody.toString().toRequestBody("application/json".toMediaType())) | ||
| .addHeader("Content-Type", "application/json") | ||
| .build() | ||
|
|
||
| httpClient.newCall(request).execute().use { response -> | ||
| val responseBody = response.body?.string() | ||
|
|
||
| if (!response.isSuccessful || responseBody == null) { | ||
| throw IOException("Unexpected response: ${response.code}") | ||
| } | ||
|
|
||
| return parseAliasAddress(responseBody) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| fun parseAliasAddress(body: String): String? = try { | ||
| val json = JsonParser.parseString(body).asJsonObject | ||
| val result = if (json.has("error")) null else json.getAsJsonObject("result") | ||
|
|
||
| if (result?.get("status")?.asString == "OK") { | ||
| result.getAsJsonObject("alias_details")?.get("address")?.asString?.takeIf { it.isNotEmpty() } | ||
| } else { | ||
| null | ||
| } | ||
| } catch (e: Exception) { | ||
| null | ||
| } | ||
| } | ||
| } |
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
97 changes: 97 additions & 0 deletions
97
walletkit/src/test/java/io/horizontalsystems/walletkit/modules/address/ZanoAliasTest.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,97 @@ | ||
| package io.horizontalsystems.walletkit.modules.address | ||
|
|
||
| import io.horizontalsystems.walletkit.core.address.ZanoAliasResolver | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Test | ||
|
|
||
| class ZanoAliasTest { | ||
|
|
||
| @Test | ||
| fun normalize_validAliases() { | ||
| assertEquals("zano", AddressHandlerZanoAlias.normalize("@zano")) | ||
| assertEquals("gigabyted", AddressHandlerZanoAlias.normalize("gigabyted")) | ||
| assertEquals("gigabyted", AddressHandlerZanoAlias.normalize("@gigabyted")) | ||
| assertEquals("alias123", AddressHandlerZanoAlias.normalize("alias123")) | ||
| assertEquals("a", AddressHandlerZanoAlias.normalize("@a")) | ||
| assertEquals("a".repeat(25), AddressHandlerZanoAlias.normalize("a".repeat(25))) | ||
| // dots and hyphens are part of the on-chain alias charset | ||
| assertEquals("-axel-", AddressHandlerZanoAlias.normalize("-axel-")) | ||
| assertEquals("---007", AddressHandlerZanoAlias.normalize("@---007")) | ||
| assertEquals("some.name", AddressHandlerZanoAlias.normalize("some.name")) | ||
| } | ||
|
|
||
| @Test | ||
| fun normalize_invalidAliases() { | ||
| assertNull(AddressHandlerZanoAlias.normalize("a")) | ||
| assertNull(AddressHandlerZanoAlias.normalize("")) | ||
| assertNull(AddressHandlerZanoAlias.normalize("@")) | ||
| assertNull(AddressHandlerZanoAlias.normalize("MyName")) | ||
| assertNull(AddressHandlerZanoAlias.normalize("with space")) | ||
| assertNull(AddressHandlerZanoAlias.normalize("under_score")) | ||
| assertNull(AddressHandlerZanoAlias.normalize("a".repeat(26))) | ||
| assertNull(AddressHandlerZanoAlias.normalize("@@double")) | ||
| assertNull( | ||
| AddressHandlerZanoAlias.normalize( | ||
| "ZxDGngsbdEvaPWoYmV995cKddBqYn1A963Wu2xRJUotE65J9FzitMtLAeYKwQewEGYVLsoc1MqRKghhGCmFEpcPo2BMnzYeCJ" | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun parseAliasAddress_ok() { | ||
| val body = """ | ||
| { | ||
| "id": 0, | ||
| "jsonrpc": "2.0", | ||
| "result": { | ||
| "alias_details": { | ||
| "address": "ZxDGngsbdEvaPWoYmV995cKddBqYn1A963Wu2xRJUotE65J9FzitMtLAeYKwQewEGYVLsoc1MqRKghhGCmFEpcPo2BMnzYeCJ", | ||
| "comment": "the one and only!", | ||
| "tracking_key": "" | ||
| }, | ||
| "status": "OK" | ||
| } | ||
| } | ||
| """.trimIndent() | ||
|
|
||
| assertEquals( | ||
| "ZxDGngsbdEvaPWoYmV995cKddBqYn1A963Wu2xRJUotE65J9FzitMtLAeYKwQewEGYVLsoc1MqRKghhGCmFEpcPo2BMnzYeCJ", | ||
| ZanoAliasResolver.parseAliasAddress(body) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun parseAliasAddress_error() { | ||
| val body = """{"id":0,"jsonrpc":"2.0","error":{"code":-1,"message":"Unknown alias"}}""" | ||
| assertNull(ZanoAliasResolver.parseAliasAddress(body)) | ||
| } | ||
|
|
||
| @Test | ||
| fun parseAliasAddress_notFound() { | ||
| // actual shape returned by public nodes for an unknown alias | ||
| val body = """ | ||
| {"id":0,"jsonrpc":"2.0","result":{"alias_details":{"address":"","comment":"","tracking_key":""},"status":"NOT_FOUND"}} | ||
| """.trimIndent() | ||
| assertNull(ZanoAliasResolver.parseAliasAddress(body)) | ||
| } | ||
|
|
||
| @Test | ||
| fun parseAliasAddress_okWithEmptyAddress() { | ||
| val body = """{"id":0,"jsonrpc":"2.0","result":{"alias_details":{"address":""},"status":"OK"}}""" | ||
| assertNull(ZanoAliasResolver.parseAliasAddress(body)) | ||
| } | ||
|
|
||
| @Test | ||
| fun parseAliasAddress_missingAliasDetails() { | ||
| val body = """{"id":0,"jsonrpc":"2.0","result":{"status":"OK"}}""" | ||
| assertNull(ZanoAliasResolver.parseAliasAddress(body)) | ||
| } | ||
|
|
||
| @Test | ||
| fun parseAliasAddress_malformedJson() { | ||
| assertNull(ZanoAliasResolver.parseAliasAddress("not a json")) | ||
| assertNull(ZanoAliasResolver.parseAliasAddress("")) | ||
| assertNull(ZanoAliasResolver.parseAliasAddress("[]")) | ||
| } | ||
| } |
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.
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Data race on non-thread-safe map.
Since
isSupportedmakes a blocking network call that doesn't cooperatively check for coroutine cancellation, typing quickly can spawn multiple coroutines that block simultaneously. When these network calls complete, they will write to this map concurrently from different threads onDispatchers.Default, which can cause aConcurrentModificationExceptionor corrupt the map.Use a
ConcurrentHashMapto ensure thread safety.🛠️ Proposed fix to ensure thread safety
📝 Committable suggestion
🤖 Prompt for AI Agents