-
Notifications
You must be signed in to change notification settings - Fork 8
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
Adapt wallet to the changes on HDPathComponent
#1225
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,17 +19,20 @@ import com.radixdlt.sargon.Account | |
import com.radixdlt.sargon.DerivationPathScheme | ||
import com.radixdlt.sargon.DisplayName | ||
import com.radixdlt.sargon.FactorSource | ||
import com.radixdlt.sargon.HdPathComponent | ||
import com.radixdlt.sargon.HierarchicalDeterministicPublicKey | ||
import com.radixdlt.sargon.KeySpace | ||
import com.radixdlt.sargon.NetworkId | ||
import com.radixdlt.sargon.OnLedgerSettings | ||
import com.radixdlt.sargon.Profile | ||
import com.radixdlt.sargon.PublicKey | ||
import com.radixdlt.sargon.ThirdPartyDeposits | ||
import com.radixdlt.sargon.extensions.HDPathValue | ||
import com.radixdlt.sargon.extensions.accountRecoveryScanned | ||
import com.radixdlt.sargon.extensions.asGeneral | ||
import com.radixdlt.sargon.extensions.derivePublicKey | ||
import com.radixdlt.sargon.extensions.id | ||
import com.radixdlt.sargon.extensions.indexInGlobalKeySpace | ||
import com.radixdlt.sargon.extensions.indexInLocalKeySpace | ||
import com.radixdlt.sargon.extensions.init | ||
import com.radixdlt.sargon.extensions.kind | ||
import com.radixdlt.sargon.extensions.string | ||
|
@@ -68,15 +71,18 @@ class DeriveAccountsViewModel @Inject constructor( | |
|
||
private lateinit var input: ToReDeriveAccounts | ||
private var profile: Profile? = null | ||
private var nextDerivationPathOffset: UInt = 0u | ||
private var nextDerivationPathOffset: HdPathComponent = HdPathComponent.init( | ||
localKeySpace = 0u, | ||
keySpace = KeySpace.Unsecurified(isHardened = true) | ||
) | ||
private var reDerivePublicKeyJob: Job? = null | ||
|
||
init { | ||
reDerivePublicKeyJob = viewModelScope.launch { | ||
profile = getProfileUseCase.finishedOnboardingProfile() | ||
|
||
input = accessFactorSourcesIOHandler.getInput() as ToReDeriveAccounts | ||
nextDerivationPathOffset = input.nextDerivationPathOffset | ||
nextDerivationPathOffset = input.nextDerivationPathIndex | ||
// if it is with given mnemonic it means it is an account recovery scan from onboarding, | ||
// thus profile is not initialized yet | ||
if (input is ToReDeriveAccounts.WithGivenMnemonic) { | ||
|
@@ -163,7 +169,7 @@ class DeriveAccountsViewModel @Inject constructor( | |
} | ||
withContext(ioDispatcher) { | ||
val networkId = profile?.currentGateway?.network?.id.orDefault() | ||
val indicesToScan: Set<HDPathValue> = computeIndicesToScan( | ||
val indicesToScan = computeIndicesToScan( | ||
derivationPathScheme = if (input.isForLegacyOlympia) DerivationPathScheme.BIP44_OLYMPIA else DerivationPathScheme.CAP26, | ||
forNetworkId = networkId, | ||
factorSource = input.factorSource | ||
|
@@ -180,7 +186,7 @@ class DeriveAccountsViewModel @Inject constructor( | |
accessFactorSourcesIOHandler.setOutput( | ||
output = AccessFactorSourcesOutput.DerivedAccountsWithNextDerivationPath( | ||
derivedAccounts = derivedAccounts, | ||
nextDerivationPathOffset = indicesToScan.last() + 1u | ||
nextDerivationPathIndex = HdPathComponent.init(indicesToScan.last().indexInGlobalKeySpace + 1u) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Next value is derived from the previous one + 1.
micbakos-rdx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
) | ||
} | ||
|
@@ -206,15 +212,15 @@ class DeriveAccountsViewModel @Inject constructor( | |
accessFactorSourcesIOHandler.setOutput( | ||
output = AccessFactorSourcesOutput.DerivedAccountsWithNextDerivationPath( | ||
derivedAccounts = derivedAccounts, | ||
nextDerivationPathOffset = indicesToScan.last() + 1u | ||
nextDerivationPathIndex = HdPathComponent.init(indicesToScan.last().indexInGlobalKeySpace + 1u) | ||
micbakos-rdx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun reDerivePublicKeysWithGivenMnemonic( | ||
accountIndices: Set<HDPathValue>, | ||
accountIndices: LinkedHashSet<HdPathComponent>, | ||
forNetworkId: NetworkId | ||
): List<HierarchicalDeterministicPublicKey> { | ||
val mnemonicWithPassphrase = (input as ToReDeriveAccounts.WithGivenMnemonic).mnemonicWithPassphrase | ||
|
@@ -228,7 +234,7 @@ class DeriveAccountsViewModel @Inject constructor( | |
} | ||
|
||
private suspend fun reDerivePublicKeysWithGivenAccountIndices( | ||
accountIndices: Set<HDPathValue>, | ||
accountIndices: LinkedHashSet<HdPathComponent>, | ||
forNetworkId: NetworkId | ||
): Result<List<HierarchicalDeterministicPublicKey>> { | ||
val derivationPaths = publicKeyProvider.getDerivationPathsForIndices( | ||
|
@@ -297,23 +303,28 @@ class DeriveAccountsViewModel @Inject constructor( | |
derivationPathScheme: DerivationPathScheme, | ||
forNetworkId: NetworkId, | ||
factorSource: FactorSource | ||
): Set<HDPathValue> { | ||
): LinkedHashSet<HdPathComponent> { | ||
val network = profile?.networks?.asIdentifiable()?.getBy(forNetworkId) | ||
val usedIndices = network?.accounts | ||
?.filter { account -> | ||
account.factorSourceId == factorSource.id && account.derivationPathScheme == derivationPathScheme | ||
} | ||
?.map { account -> | ||
account.derivationPathEntityIndex | ||
account.derivationPathEntityIndex.indexInLocalKeySpace | ||
} | ||
?.toSet().orEmpty() | ||
|
||
val indicesToScan = mutableSetOf<HDPathValue>() | ||
val startIndex = nextDerivationPathOffset | ||
val indicesToScan = LinkedHashSet<HdPathComponent>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Later in code we use the |
||
val startIndex = nextDerivationPathOffset.indexInLocalKeySpace | ||
var currentIndex = startIndex | ||
do { | ||
if (currentIndex !in usedIndices) { | ||
indicesToScan.add(currentIndex) | ||
indicesToScan.add( | ||
HdPathComponent.init( | ||
localKeySpace = currentIndex, | ||
keySpace = KeySpace.Unsecurified(isHardened = true) | ||
) | ||
) | ||
} | ||
currentIndex++ | ||
} while (indicesToScan.size < ACCOUNTS_PER_SCAN) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
package rdx.works.core.sargon | ||
|
||
import com.radixdlt.sargon.AccountPath | ||
import com.radixdlt.sargon.Bip44LikePath | ||
import com.radixdlt.sargon.Cap26KeyKind | ||
import com.radixdlt.sargon.Cap26Path | ||
import com.radixdlt.sargon.DerivationPath | ||
import com.radixdlt.sargon.GetIdPath | ||
import com.radixdlt.sargon.IdentityPath | ||
import com.radixdlt.sargon.NetworkId | ||
import com.radixdlt.sargon.extensions.account | ||
import com.radixdlt.sargon.extensions.addressIndex | ||
import com.radixdlt.sargon.extensions.asGeneral | ||
import com.radixdlt.sargon.extensions.default | ||
import com.radixdlt.sargon.extensions.identity | ||
import com.radixdlt.sargon.extensions.asHardened | ||
import com.radixdlt.sargon.extensions.init | ||
|
||
fun DerivationPath.Companion.getIdPath() = DerivationPath.Cap26(Cap26Path.GetId(GetIdPath.default())) | ||
|
||
fun Cap26Path.Account.toAuthSigningDerivationPath(): DerivationPath = DerivationPath.Cap26.account( | ||
networkId = value.networkId, | ||
fun AccountPath.toAuthSigningDerivationPath(): DerivationPath = AccountPath.init( | ||
networkId = networkId, | ||
keyKind = Cap26KeyKind.AUTHENTICATION_SIGNING, | ||
index = value.index | ||
) | ||
index = index | ||
).asGeneral() | ||
|
||
fun Cap26Path.Identity.toAuthSigningDerivationPath(): DerivationPath = DerivationPath.Cap26.identity( | ||
networkId = value.networkId, | ||
fun IdentityPath.toAuthSigningDerivationPath(): DerivationPath = IdentityPath.init( | ||
networkId = networkId, | ||
keyKind = Cap26KeyKind.AUTHENTICATION_SIGNING, | ||
index = value.index | ||
) | ||
index = index | ||
).asGeneral() | ||
|
||
fun Bip44LikePath.toAccountAuthSigningDerivationPath(networkId: NetworkId): DerivationPath = DerivationPath.Cap26.account( | ||
fun Bip44LikePath.toAccountAuthSigningDerivationPath(networkId: NetworkId): DerivationPath = AccountPath.init( | ||
networkId = networkId, | ||
keyKind = Cap26KeyKind.AUTHENTICATION_SIGNING, | ||
index = asGeneral().addressIndex | ||
) | ||
index = index.asHardened() | ||
).asGeneral() | ||
|
||
fun Bip44LikePath.toIdentityAuthSigningDerivationPath(networkId: NetworkId): DerivationPath = DerivationPath.Cap26.identity( | ||
fun Bip44LikePath.toIdentityAuthSigningDerivationPath(networkId: NetworkId): DerivationPath = IdentityPath.init( | ||
networkId = networkId, | ||
keyKind = Cap26KeyKind.AUTHENTICATION_SIGNING, | ||
index = asGeneral().addressIndex | ||
) | ||
index = index.asHardened() | ||
).asGeneral() |
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.
Wrapped the value with
HdPathComponent
. A simpleUInt
has no meaning with no context. We need to know if this index is calculated in the local key space or in the global.