-
Notifications
You must be signed in to change notification settings - Fork 12
feat: minimal sweep WIF QR-code implementation #53
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
psychemist
wants to merge
5
commits into
bitcoindevkit:variant/2.0
Choose a base branch
from
psychemist:feat/wif-sweep-qrcode
base: variant/2.0
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dd08686
feat: add wifparser logic
psychemist 6d22688
feat: implement sweep wallet logic
psychemist d277e2b
feat: implement qrcode scannnner; send screen ui
psychemist 6bb127e
test: add test suites
psychemist 6ed9e52
chore: add dependencies
psychemist 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
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
81 changes: 81 additions & 0 deletions
81
app/src/main/java/org/bitcoindevkit/devkitwallet/domain/utils/WifParser.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,81 @@ | ||
| /* | ||
| * Copyright 2021-2026 thunderbiscuit and contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the ./LICENSE file. | ||
| */ | ||
|
|
||
| package org.bitcoindevkit.devkitwallet.domain.utils | ||
|
|
||
| import android.util.Log | ||
| import org.bitcoindevkit.devkitwallet.domain.DwLogger | ||
| import org.bitcoindevkit.devkitwallet.domain.DwLogger.LogLevel.INFO | ||
| import org.bitcoindevkit.devkitwallet.domain.DwLogger.LogLevel.WARN | ||
|
|
||
| private const val TAG = "WifParser" | ||
|
|
||
| /** | ||
| * Utility for detecting and extracting WIF-encoded private keys from various input formats. | ||
| */ | ||
| object WifParser { | ||
| private val WIF_FIRST_CHARS = setOf('5', 'K', 'L', '9', 'c') | ||
|
|
||
| private val BASE58_CHARSET: Set<Char> = | ||
| "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toSet() | ||
|
|
||
| private val WIF_PARAM_KEYS = setOf("wif", "privkey", "private_key", "privatekey") | ||
|
|
||
| fun extract(value: String): String? { | ||
| val trimmed = value.trim() | ||
| val candidates = mutableListOf<String>() | ||
|
|
||
| candidates.add(trimmed) | ||
|
|
||
| if (trimmed.lowercase().startsWith("wif:")) { | ||
| candidates.add(trimmed.drop(4).trim()) | ||
| } | ||
|
|
||
| if (trimmed.lowercase().startsWith("bitcoin:")) { | ||
| try { | ||
| val queryPart = trimmed.substringAfter("?", "") | ||
| if (queryPart.isNotEmpty()) { | ||
| queryPart.split("&").forEach { param -> | ||
| val kv = param.split("=", limit = 2) | ||
| if (kv.size == 2 && kv[0].lowercase() in WIF_PARAM_KEYS) { | ||
| candidates.add(kv[1].trim()) | ||
| } | ||
| } | ||
| } | ||
| } catch (e: Throwable) { | ||
| Log.i(TAG, "WIF Parsing error: ${e.message}", e) | ||
| } | ||
| } | ||
|
|
||
| val result = candidates.firstOrNull { isLikelyWif(it) } | ||
| if (result != null) { | ||
| DwLogger.log( | ||
| INFO, | ||
| "WifParser: extracted WIF (length=${result.length}) from input (length=${trimmed.length})" | ||
| ) | ||
| } else if (candidates.size > 1) { | ||
| DwLogger.log( | ||
| WARN, | ||
| "WifParser: no valid WIF found in ${candidates.size} candidates from input (length=${trimmed.length})" | ||
| ) | ||
| candidates.forEach { c -> | ||
| DwLogger.log( | ||
| WARN, | ||
| "WifParser: candidate length=${c.length}, first='${c.firstOrNull()}', likelyWif=${isLikelyWif(c)}" | ||
| ) | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| fun isLikelyWif(value: String): Boolean { | ||
| if (value.length != 51 && value.length != 52) return false | ||
|
|
||
| val first = value.firstOrNull() ?: return false | ||
| if (first !in WIF_FIRST_CHARS) return false | ||
|
|
||
| return value.all { it in BASE58_CHARSET } | ||
| } | ||
| } |
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.
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.
Currently, errors are rightfully thrown when we pass an uncompressed key because uncompressed will only work for legacy pkh script. Maybe we want to determine if the key is compressed or uncompressed and that will help us determine what to put as the candidates. pkh for uncompressed then the rest if compressed.
Maybe a helper function in WifParser can help tell us if
isCompressedhere.