Updated on 2026-08-14
This commit is contained in:
parent
e0a8e25779
commit
aa9eda0758
6 changed files with 321 additions and 10 deletions
|
|
@ -0,0 +1,46 @@
|
|||
package com.tangem.lib.crypto.derivation
|
||||
|
||||
import com.tangem.blockchain.common.Blockchain
|
||||
import com.tangem.blockchain.common.isUTXO
|
||||
import com.tangem.crypto.hdWallet.DerivationPath
|
||||
|
||||
/**
|
||||
* Utility class to recognize the account node in a derivation path based on the blockchain type.
|
||||
* Derivation path schema: [ m / purpose' / coin_type' / account' / change / address_index ].
|
||||
*
|
||||
* @param blockchain the blockchain for which the account node is to be recognized
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
class AccountNodeRecognizer(blockchain: Blockchain) {
|
||||
|
||||
/** Index of the account node in the derivation path */
|
||||
val accountNodeIndex: Int = if (blockchain.isUTXO) {
|
||||
UTXO_BLOCKCHAIN_NODE_INDEX
|
||||
} else {
|
||||
NON_UTXO_BLOCKCHAIN_NODE_INDEX
|
||||
}
|
||||
|
||||
/** Recognizes the account node value from the given derivation path string [derivationPathValue] */
|
||||
fun recognize(derivationPathValue: String): Long? {
|
||||
return runCatching {
|
||||
recognize(derivationPath = DerivationPath(rawPath = derivationPathValue))
|
||||
}
|
||||
.getOrNull()
|
||||
}
|
||||
|
||||
/** Recognizes the account node value from the given [derivationPath] */
|
||||
fun recognize(derivationPath: DerivationPath): Long? {
|
||||
return runCatching {
|
||||
val accountNode = derivationPath.nodes.getOrNull(accountNodeIndex)
|
||||
|
||||
accountNode?.getIndex(includeHardened = false)
|
||||
}
|
||||
.getOrNull()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val UTXO_BLOCKCHAIN_NODE_INDEX = 2
|
||||
const val NON_UTXO_BLOCKCHAIN_NODE_INDEX = 4
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package com.tangem.lib.crypto.derivation
|
||||
|
||||
import com.tangem.blockchain.common.Blockchain
|
||||
import com.tangem.crypto.hdWallet.DerivationNode
|
||||
import com.tangem.crypto.hdWallet.DerivationPath
|
||||
import timber.log.Timber
|
||||
|
||||
/** Extension function to convert a [DerivationPath] into a [MutableDerivationPath] */
|
||||
fun DerivationPath.toMutable(): MutableDerivationPath = MutableDerivationPath(value = this)
|
||||
|
||||
/**
|
||||
* A mutable representation of a derivation path, allowing modifications to specific nodes
|
||||
*
|
||||
* @property value the initial derivationPath to be used
|
||||
*/
|
||||
class MutableDerivationPath internal constructor(val value: DerivationPath) {
|
||||
|
||||
/**
|
||||
* Replaces the account node in the derivation path with a new value
|
||||
*
|
||||
* @param value the new index to set for the account node
|
||||
* @param blockchain the blockchain used to determine the account node index
|
||||
*/
|
||||
fun replaceAccountNode(value: Long, blockchain: Blockchain): MutableDerivationPath {
|
||||
val mutableNodes = this@MutableDerivationPath.value.nodes.toMutableList()
|
||||
|
||||
val accountNodeIndex = AccountNodeRecognizer(blockchain).accountNodeIndex
|
||||
val accountNode = mutableNodes.getOrNull(accountNodeIndex)
|
||||
|
||||
if (accountNode != null) {
|
||||
mutableNodes[accountNodeIndex] = when (accountNode) {
|
||||
is DerivationNode.Hardened -> DerivationNode.Hardened(value)
|
||||
is DerivationNode.NonHardened -> DerivationNode.NonHardened(value)
|
||||
}
|
||||
} else {
|
||||
Timber.e("Account node not found in the derivation path: ${this@MutableDerivationPath.value}")
|
||||
}
|
||||
|
||||
return DerivationPath(path = mutableNodes).toMutable()
|
||||
}
|
||||
|
||||
/** Applies the changes and returns it as an immutable [DerivationPath] */
|
||||
fun apply(): DerivationPath = value
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue