Skip to content

Commit

Permalink
integrate xcrypto and xsubstrate
Browse files Browse the repository at this point in the history
  • Loading branch information
mrZizik committed Oct 4, 2023
1 parent 377a95e commit 4089aeb
Show file tree
Hide file tree
Showing 80 changed files with 371 additions and 307 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ dependencies {

implementation timberDep

implementation xsubstrateDep

implementation daggerDep
kapt daggerKaptDep
implementation hiltWorkManagerDep
Expand Down
2 changes: 1 addition & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

# Keep sora sdk classes
-keep class jp.co.soramitsu.sora.sdk.** { *; }
-keep class jp.co.soramitsu.shared_utils.** { *; }
-keep class jp.co.soramitsu.xsubstrate.** { *; }
-keep class net.jpountz.** { *; }
-keep class com.sun.jna.** { *; }

Expand Down
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ buildscript {
accompanist : '0.30.1',
xserializationjson : '1.5.0',
ktor : '2.3.1',
xbackup : '0.0.2-20231002.173502-3'
xbackup : '1.0.0',
xcrypto : '1.0.1',
xsubstrate : '1.0.1'
]

ext {
Expand Down Expand Up @@ -147,6 +149,8 @@ buildscript {
web3jDep = "org.web3j:core:$versions.web3j"

xbackupDep = "jp.co.soramitsu:xbackup:$versions.xbackup"
xsubstrateDep = "jp.co.soramitsu:xsubstrate:$versions.xsubstrate"
xcryptoDep = "jp.co.soramitsu:x-crypto:$versions.xcrypto"

permissionsAccompanistDep = "com.google.accompanist:accompanist-permissions:$versions.accompanist"
permissionsRuntimeDep = "com.guolindev.permissionx:permissionx:1.7.1"
Expand Down Expand Up @@ -238,7 +242,6 @@ apply from: 'secrets.gradle'

allprojects {
repositories {
mavenLocal()
maven { url "https://nexus.iroha.tech/repository/maven-soramitsu/" }
google()
mavenCentral()
Expand Down
3 changes: 2 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ dependencies {
implementation navigationUiDep

implementation xbackupDep
implementation sharedUtilsDep
implementation xsubstrateDep
implementation xcryptoDep

implementation gsonDep

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import com.caverock.androidsvg.SVG
import javax.inject.Singleton
import jdenticon.Jdenticon
import jp.co.soramitsu.common.resourses.ResourceManager
import jp.co.soramitsu.shared_utils.extensions.toHexString
import jp.co.soramitsu.shared_utils.ss58.SS58Encoder.toAccountId
import jp.co.soramitsu.xcrypto.util.toHexString
import jp.co.soramitsu.xsubstrate.ss58.SS58Encoder.toAccountId

@Singleton
class AccountAvatarGenerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ import jp.co.soramitsu.common.util.TextFormatter
import jp.co.soramitsu.common.util.json_decoder.JsonAccountsEncoder
import jp.co.soramitsu.common.vibration.DeviceVibrator
import jp.co.soramitsu.crypto.ed25519.Ed25519Sha3
import jp.co.soramitsu.shared_utils.encrypt.json.JsonSeedEncoder
import jp.co.soramitsu.xbackup.BackupService
import jp.co.soramitsu.xnetworking.networkclient.SoramitsuHttpClientProvider
import jp.co.soramitsu.xnetworking.networkclient.SoramitsuHttpClientProviderImpl
Expand All @@ -91,6 +90,7 @@ import jp.co.soramitsu.xnetworking.sorawallet.mainconfig.SoraRemoteConfigBuilder
import jp.co.soramitsu.xnetworking.sorawallet.mainconfig.SoraRemoteConfigProvider
import jp.co.soramitsu.xnetworking.sorawallet.tokenwhitelist.SoraTokensWhitelistManager
import jp.co.soramitsu.xnetworking.txhistory.client.sorawallet.SubQueryClientForSoraWalletFactory
import jp.co.soramitsu.xsubstrate.encrypt.json.JsonSeedEncoder

@InstallIn(SingletonComponent::class)
@Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import jp.co.soramitsu.common.util.CryptoAssistant
import jp.co.soramitsu.shared_utils.encrypt.EncryptionType
import jp.co.soramitsu.shared_utils.encrypt.MultiChainEncryption
import jp.co.soramitsu.shared_utils.encrypt.json.JsonSeedEncoder
import jp.co.soramitsu.shared_utils.encrypt.keypair.Keypair
import jp.co.soramitsu.xcrypto.encryption.Keypair
import jp.co.soramitsu.xsubstrate.encrypt.EncryptionType
import jp.co.soramitsu.xsubstrate.encrypt.MultiChainEncryption
import jp.co.soramitsu.xsubstrate.encrypt.json.JsonSeedEncoder
import org.spongycastle.util.encoders.Base64

class JsonAccountsEncoder(
Expand Down
4 changes: 3 additions & 1 deletion demeter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ dependencies {
implementation coroutineDep
implementation coroutineAndroidDep

implementation xsubstrateDep

implementation composeRuntimeDep

implementation roomDep
implementation roomKtxDep
//kapt roomKaptDep

implementation sharedUtilsDep
// implementation sharedUtilsDep
implementation xNetworkingDep

testImplementation project(":test_shared")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ import jp.co.soramitsu.common_wallet.data.AssetLocalToAssetMapper
import jp.co.soramitsu.core_db.AppDatabase
import jp.co.soramitsu.demeter.domain.DemeterFarmingPool
import jp.co.soramitsu.feature_blockexplorer_api.data.SoraConfigManager
import jp.co.soramitsu.shared_utils.runtime.definitions.types.composite.Struct
import jp.co.soramitsu.shared_utils.runtime.definitions.types.fromHex
import jp.co.soramitsu.shared_utils.runtime.metadata.module
import jp.co.soramitsu.shared_utils.runtime.metadata.storage
import jp.co.soramitsu.shared_utils.runtime.metadata.storageKey
import jp.co.soramitsu.shared_utils.ss58.SS58Encoder.toAccountId
import jp.co.soramitsu.sora.substrate.runtime.Pallete
import jp.co.soramitsu.sora.substrate.runtime.RuntimeManager
import jp.co.soramitsu.sora.substrate.runtime.Storage
import jp.co.soramitsu.sora.substrate.runtime.mapToToken
import jp.co.soramitsu.sora.substrate.substrate.SubstrateCalls
import jp.co.soramitsu.xsubstrate.runtime.definitions.types.composite.Struct
import jp.co.soramitsu.xsubstrate.runtime.definitions.types.fromHex
import jp.co.soramitsu.xsubstrate.runtime.metadata.module
import jp.co.soramitsu.xsubstrate.runtime.metadata.storage
import jp.co.soramitsu.xsubstrate.runtime.metadata.storageKey
import jp.co.soramitsu.xsubstrate.ss58.SS58Encoder.toAccountId

interface DemeterFarmingRepository {
suspend fun getFarmedPools(soraAccountAddress: String): List<DemeterFarmingPool>?
Expand Down
4 changes: 3 additions & 1 deletion feature_account_api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ dependencies {

implementation project(":common")
implementation project(":sorasubstrate")
implementation sharedUtilsDep
// implementation sharedUtilsDep

implementation xsubstrateDep

implementation coroutineAndroidDep
implementation coroutineDep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package jp.co.soramitsu.feature_account_api.domain.interfaces

import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair

interface CredentialsDatasource {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ package jp.co.soramitsu.feature_account_api.domain.interfaces

import jp.co.soramitsu.common.account.IrohaData
import jp.co.soramitsu.common.account.SoraAccount
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair

interface CredentialsRepository {

Expand Down
5 changes: 4 additions & 1 deletion feature_account_impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ dependencies {
implementation project(":feature_blockexplorer_api")
implementation project(":core_db")
implementation project(":sorasubstrate")
implementation sharedUtilsDep
// implementation sharedUtilsDep

implementation xcryptoDep
implementation xsubstrateDep

implementation gsonDep

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ import jp.co.soramitsu.common.util.json_decoder.JsonAccountsEncoder
import jp.co.soramitsu.feature_account_api.domain.interfaces.CredentialsDatasource
import jp.co.soramitsu.feature_account_api.domain.interfaces.CredentialsRepository
import jp.co.soramitsu.feature_blockexplorer_api.data.SoraConfigManager
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.SubstrateKeypairFactory
import jp.co.soramitsu.shared_utils.encrypt.mnemonic.Mnemonic
import jp.co.soramitsu.shared_utils.encrypt.mnemonic.MnemonicCreator
import jp.co.soramitsu.shared_utils.encrypt.seed.substrate.SubstrateSeedFactory
import jp.co.soramitsu.shared_utils.extensions.fromHex
import jp.co.soramitsu.shared_utils.extensions.toHexString
import jp.co.soramitsu.sora.substrate.runtime.RuntimeManager
import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider
import jp.co.soramitsu.sora.substrate.substrate.deriveSeed32
import jp.co.soramitsu.xcrypto.util.fromHex
import jp.co.soramitsu.xcrypto.util.toHexString
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.SubstrateKeypairFactory
import jp.co.soramitsu.xsubstrate.encrypt.mnemonic.Mnemonic
import jp.co.soramitsu.xsubstrate.encrypt.mnemonic.MnemonicCreator
import jp.co.soramitsu.xsubstrate.encrypt.seed.substrate.SubstrateSeedFactory

class CredentialsRepositoryImpl constructor(
private val credentialsPrefs: CredentialsDatasource,
Expand All @@ -82,7 +81,7 @@ class CredentialsRepositoryImpl constructor(
}

private suspend fun generateEntropyAndKeysFromMnemonic(mnemonic: Mnemonic): String {
val derivationResult = SubstrateSeedFactory.deriveSeed32(mnemonic.words, null)
val derivationResult = SubstrateSeedFactory.deriveSeed(mnemonic.words, null)
val keyPair = SubstrateKeypairFactory.generate(
SubstrateOptionsProvider.encryptionType,
derivationResult.seed,
Expand Down Expand Up @@ -219,7 +218,11 @@ class CredentialsRepositoryImpl constructor(
it.substrateAddress
)

return jsonSeedEncoder.generate(account = exportAccountData, password = password, genesisHash = soraConfigManager.getGenesis())
return jsonSeedEncoder.generate(
account = exportAccountData,
password = password,
genesisHash = soraConfigManager.getGenesis()
)
}
} else {
val accountsList = accounts.map {
Expand All @@ -239,7 +242,7 @@ class CredentialsRepositoryImpl constructor(
}

override fun convertPassphraseToSeed(mnemonic: String): String {
val derivationResult = SubstrateSeedFactory.deriveSeed32(MnemonicCreator.fromWords(mnemonic).words, null)
val derivationResult = SubstrateSeedFactory.deriveSeed(MnemonicCreator.fromWords(mnemonic).words, null)
return derivationResult.seed.toHexString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ package jp.co.soramitsu.feature_account_impl.data.repository.datasource
import jp.co.soramitsu.common.data.EncryptedPreferences
import jp.co.soramitsu.common.data.SoraPreferences
import jp.co.soramitsu.feature_account_api.domain.interfaces.CredentialsDatasource
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.shared_utils.extensions.fromHex
import jp.co.soramitsu.shared_utils.extensions.toHexString
import jp.co.soramitsu.xcrypto.util.fromHex
import jp.co.soramitsu.xcrypto.util.toHexString
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair

class PrefsCredentialsDatasource constructor(
private val encryptedPreferences: EncryptedPreferences,
Expand Down Expand Up @@ -68,7 +68,11 @@ class PrefsCredentialsDatasource constructor(
val publicKeyBytes = encryptedPreferences.getDecryptedString(PREFS_PUBLIC_KEY + suffixAddress).fromHex()
val nonce = encryptedPreferences.getDecryptedString(PREFS_KEY_NONCE + suffixAddress).fromHex()

return if (privateKeyBytes.isEmpty() || publicKeyBytes.isEmpty()) null else Sr25519Keypair(privateKeyBytes, publicKeyBytes, nonce)
return if (privateKeyBytes.isEmpty() || publicKeyBytes.isEmpty()) null else Sr25519Keypair(
privateKeyBytes,
publicKeyBytes,
nonce
)
}

override suspend fun saveMnemonic(mnemonic: String, suffixAddress: String) {
Expand All @@ -88,8 +92,9 @@ class PrefsCredentialsDatasource constructor(
}

override suspend fun clearAllDataForAddress(suffixAddress: String) {
val fields = listOf(PREFS_ADDRESS, PREFS_PRIVATE_KEY, PREFS_PUBLIC_KEY, PREFS_KEY_NONCE, PREFS_MNEMONIC, PREFS_SEED)
.map { it + suffixAddress }
val fields =
listOf(PREFS_ADDRESS, PREFS_PRIVATE_KEY, PREFS_PUBLIC_KEY, PREFS_KEY_NONCE, PREFS_MNEMONIC, PREFS_SEED)
.map { it + suffixAddress }
encryptedPreferences.clear(fields)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ import jp.co.soramitsu.common.util.CryptoAssistant
import jp.co.soramitsu.common.util.json_decoder.JsonAccountsEncoder
import jp.co.soramitsu.feature_account_api.domain.interfaces.CredentialsDatasource
import jp.co.soramitsu.feature_blockexplorer_api.data.SoraConfigManager
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.SubstrateKeypairFactory
import jp.co.soramitsu.shared_utils.encrypt.mnemonic.Mnemonic
import jp.co.soramitsu.shared_utils.encrypt.mnemonic.MnemonicCreator
import jp.co.soramitsu.shared_utils.encrypt.seed.SeedFactory
import jp.co.soramitsu.shared_utils.encrypt.seed.substrate.SubstrateSeedFactory
//import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair
//import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.SubstrateKeypairFactory
//import jp.co.soramitsu.xsubstrate.encrypt.mnemonic.Mnemonic
//import jp.co.soramitsu.xsubstrate.encrypt.mnemonic.MnemonicCreator
//import jp.co.soramitsu.xsubstrate.encrypt.seed.SeedFactory
//import jp.co.soramitsu.xsubstrate.encrypt.seed.substrate.SubstrateSeedFactory
import jp.co.soramitsu.sora.substrate.runtime.RuntimeManager
import jp.co.soramitsu.sora.substrate.substrate.deriveSeed32
import jp.co.soramitsu.test_shared.MainCoroutineRule
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.SubstrateKeypairFactory
import jp.co.soramitsu.xsubstrate.encrypt.mnemonic.Mnemonic
import jp.co.soramitsu.xsubstrate.encrypt.mnemonic.MnemonicCreator
import jp.co.soramitsu.xsubstrate.encrypt.seed.SeedFactory
import jp.co.soramitsu.xsubstrate.encrypt.seed.substrate.SubstrateSeedFactory
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -102,9 +108,9 @@ class CredentialsRepositoryTest {
mockkObject(MnemonicCreator)
mockkObject(SubstrateKeypairFactory)
mockkObject(FirebaseWrapper)
mockkStatic(SubstrateSeedFactory::deriveSeed32)
mockkStatic(SubstrateSeedFactory::deriveSeed)
val derivationResult = mockk<SeedFactory.Result>()
every { SubstrateSeedFactory.deriveSeed32(any(), any()) } returns derivationResult
every { SubstrateSeedFactory.deriveSeed(any(), any()) } returns derivationResult
every { SubstrateKeypairFactory.generate(any(), any()) } returns keypair
every { SubstrateKeypairFactory.generate(any(), any(), any()) } returns keypair
every { FirebaseWrapper.log("Keys were created") } just runs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ package jp.co.soramitsu.feature_account_impl.data.repository.datasource
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import jp.co.soramitsu.common.data.EncryptedPreferences
import jp.co.soramitsu.common.data.SoraPreferences
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.shared_utils.extensions.toHexString
import jp.co.soramitsu.test_shared.MainCoroutineRule
import jp.co.soramitsu.xcrypto.util.toHexString
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertArrayEquals
Expand Down Expand Up @@ -175,7 +175,14 @@ class PrefsCredentialsDatasourceTest {
@Test
fun `clear all data for address called`() = runTest {
val address = "address"
val keys = listOf("prefs_address_pureaddress", "prefs_priv_keyaddress", "prefs_pub_keyaddress", "prefs_key_nonceaddress", "prefs_mnemonicaddress", "prefs_seedaddress")
val keys = listOf(
"prefs_address_pureaddress",
"prefs_priv_keyaddress",
"prefs_pub_keyaddress",
"prefs_key_nonceaddress",
"prefs_mnemonicaddress",
"prefs_seedaddress"
)

prefsCredentialsDatasource.clearAllDataForAddress(address)

Expand Down
3 changes: 2 additions & 1 deletion feature_assets_api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ dependencies {
implementation project(":common")
implementation project(':common_wallet')
implementation project(":sorasubstrate")
implementation sharedUtilsDep

implementation xsubstrateDep

implementation coroutineAndroidDep
implementation coroutineDep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import jp.co.soramitsu.common.account.SoraAccount
import jp.co.soramitsu.common.domain.Asset
import jp.co.soramitsu.common.domain.Token
import jp.co.soramitsu.common_wallet.data.XorAssetBalance
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.sora.substrate.models.ExtrinsicSubmitStatus
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair
import kotlinx.coroutines.flow.Flow

interface AssetsRepository {
Expand All @@ -60,7 +60,7 @@ interface AssetsRepository {

suspend fun getAsset(assetId: String, address: String): Asset?

suspend fun getAssetsActive(address: String,): List<Asset>
suspend fun getAssetsActive(address: String): List<Asset>

suspend fun getAssetsWhitelist(address: String): List<Asset>

Expand Down
3 changes: 2 additions & 1 deletion feature_assets_impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ dependencies {
implementation project(":feature_main_api")
implementation project(":feature_blockexplorer_api")

implementation sharedUtilsDep
implementation xsubstrateDep
implementation xcryptoDep

implementation androidDep
implementation materialDep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ import jp.co.soramitsu.core_db.model.AssetTokenWithFiatLocal
import jp.co.soramitsu.core_db.model.TokenLocal
import jp.co.soramitsu.feature_assets_api.data.AssetsRepository
import jp.co.soramitsu.feature_blockexplorer_api.data.SoraConfigManager
import jp.co.soramitsu.shared_utils.encrypt.keypair.substrate.Sr25519Keypair
import jp.co.soramitsu.sora.substrate.models.ExtrinsicSubmitStatus
import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider
import jp.co.soramitsu.sora.substrate.substrate.ExtrinsicManager
import jp.co.soramitsu.sora.substrate.substrate.SubstrateCalls
import jp.co.soramitsu.sora.substrate.substrate.faucetTransfer
import jp.co.soramitsu.sora.substrate.substrate.transfer
import jp.co.soramitsu.xsubstrate.encrypt.keypair.substrate.Sr25519Keypair
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
Expand Down
Loading

0 comments on commit 4089aeb

Please sign in to comment.