-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement android parsing for MnemonicWithPassphrase (#106)
- Loading branch information
1 parent
642215b
commit cdff3ce
Showing
5 changed files
with
149 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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
45 changes: 0 additions & 45 deletions
45
jvm/sargon-android/src/main/java/com/radixdlt/sargon/extensions/MnemonicWIthPassphrase.kt
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
jvm/sargon-android/src/main/java/com/radixdlt/sargon/extensions/MnemonicWithPassphrase.kt
This file contains 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,86 @@ | ||
package com.radixdlt.sargon.extensions | ||
|
||
import com.radixdlt.sargon.CommonException | ||
import com.radixdlt.sargon.DerivationPath | ||
import com.radixdlt.sargon.Hash | ||
import com.radixdlt.sargon.HierarchicalDeterministicPublicKey | ||
import com.radixdlt.sargon.Mnemonic | ||
import com.radixdlt.sargon.MnemonicWithPassphrase | ||
import com.radixdlt.sargon.SignatureWithPublicKey | ||
import com.radixdlt.sargon.extensions.AndroidMnemonicWithPassphrase.Companion.toAndroid | ||
import com.radixdlt.sargon.mnemonicWithPassphraseDerivePublicKeys | ||
import com.radixdlt.sargon.mnemonicWithPassphraseSign | ||
import com.radixdlt.sargon.mnemonicWithPassphraseValidatePublicKeys | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
|
||
@Throws(SargonException::class) | ||
fun MnemonicWithPassphrase.Companion.fromJson( | ||
fromJson: String | ||
) = runCatching { | ||
Json.decodeFromString<AndroidMnemonicWithPassphrase>(fromJson) | ||
}.map { | ||
it.toMnemonicWithPassphrase() | ||
}.onFailure { | ||
throw CommonException.FailedToDeserializeJsonToValue( | ||
jsonByteCount = fromJson.toByteArray(charset = Charsets.UTF_8).size.toULong(), | ||
typeName = "MnemonicWithPassphrase" | ||
) | ||
}.getOrThrow() | ||
|
||
fun MnemonicWithPassphrase.toJson(): String = Json.encodeToString(toAndroid()) | ||
|
||
fun MnemonicWithPassphrase.validate(hdPublicKeys: List<HierarchicalDeterministicPublicKey>): Boolean = | ||
mnemonicWithPassphraseValidatePublicKeys(mnemonicWithPassphrase = this, hdKeys = hdPublicKeys) | ||
|
||
fun MnemonicWithPassphrase.derivePublicKey( | ||
path: DerivationPath | ||
): HierarchicalDeterministicPublicKey = derivePublicKeys( | ||
paths = listOf(path) | ||
).first() | ||
|
||
|
||
fun MnemonicWithPassphrase.derivePublicKeys( | ||
paths: List<DerivationPath> | ||
): List<HierarchicalDeterministicPublicKey> = mnemonicWithPassphraseDerivePublicKeys( | ||
mnemonicWithPassphrase = this, | ||
derivationPaths = paths | ||
) | ||
|
||
fun MnemonicWithPassphrase.sign( | ||
hash: Hash, | ||
path: DerivationPath | ||
): SignatureWithPublicKey = mnemonicWithPassphraseSign( | ||
mnemonicWithPassphrase = this, | ||
derivationPath = path, | ||
hashToSign = hash | ||
) | ||
|
||
/** | ||
* Class needed for compatibility for Android Wallet version 1.*. | ||
* | ||
* Android and iOS use different schema and since Sargon Rust follows the iOS schema, the android | ||
* counterpart hides Sargon's implementation until the wallets migrate to version 2.*. | ||
*/ | ||
@Serializable | ||
private data class AndroidMnemonicWithPassphrase( | ||
@SerialName("mnemonic") | ||
val phrase: String, | ||
@SerialName("bip39Passphrase") | ||
val passphrase: String | ||
) { | ||
|
||
fun toMnemonicWithPassphrase() = MnemonicWithPassphrase( | ||
mnemonic = Mnemonic.init(phrase = phrase), | ||
passphrase = passphrase | ||
) | ||
|
||
companion object { | ||
fun MnemonicWithPassphrase.toAndroid() = AndroidMnemonicWithPassphrase( | ||
phrase = mnemonic.phrase, | ||
passphrase = passphrase | ||
) | ||
} | ||
} |
This file contains 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