Skip to content
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

integrate xcrypto and xsubstrate #214

Merged
merged 9 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ dependencies {

implementation timberDep

implementation xsubstrateDep

implementation daggerDep
kapt daggerKaptDep
implementation hiltWorkManagerDep
Expand Down
21 changes: 20 additions & 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 Expand Up @@ -176,6 +176,25 @@

-dontwarn com.google.android.gms.**

# Please add these rules to your existing keep rules in order to suppress warnings.
# This is generated automatically by the Android Gradle plugin.
-dontwarn com.sun.jna.platform.win32.Kernel32
-dontwarn com.sun.jna.platform.win32.Win32Exception
-dontwarn com.sun.jna.platform.win32.WinDef$LPVOID
-dontwarn com.sun.jna.platform.win32.WinNT$HANDLE
-dontwarn edu.umd.cs.findbugs.annotations.SuppressFBWarnings
-dontwarn io.mockk.proxy.jvm.dispatcher.JvmMockKDispatcher
-dontwarn io.mockk.proxy.jvm.dispatcher.JvmMockKWeakMap
-dontwarn java.lang.instrument.ClassDefinition
-dontwarn java.lang.instrument.IllegalClassFormatException
-dontwarn java.lang.instrument.UnmodifiableClassException
-dontwarn org.junit.jupiter.api.extension.AfterAllCallback
-dontwarn org.junit.jupiter.api.extension.ParameterContext
-dontwarn org.junit.jupiter.api.extension.ParameterResolver
-dontwarn org.junit.jupiter.api.extension.TestInstancePostProcessor
-dontwarn org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher
-dontwarn org.opentest4j.AssertionFailedError

-keep class com.google.** { *;}
-keep interface com.google.** { *;}
-dontwarn com.google.**
Expand Down
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ buildscript {
accompanist : '0.30.1',
xserializationjson : '1.5.1',
ktor : '2.3.1',
xbackup : '1.0.0'
xbackup : '1.0.0',
xcrypto : '1.0.2',
xsubstrate : '1.0.1'
]

ext {
Expand Down Expand Up @@ -146,6 +148,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 @@ -237,7 +241,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 @@ -150,7 +150,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.basic.networkclient.SoramitsuHttpClientProvider
import jp.co.soramitsu.xnetworking.basic.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.sorawallet.txhistory.client.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
5 changes: 3 additions & 2 deletions demeter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ dependencies {
implementation coroutineDep
implementation coroutineAndroidDep

implementation xsubstrateDep
implementation xNetworkingDep

implementation composeRuntimeDep

implementation roomDep
implementation roomKtxDep

implementation sharedUtilsDep

testImplementation project(":test_shared")
testImplementation project(":test_data")
}
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 @@ -36,23 +36,21 @@ import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.runs
import jp.co.soramitsu.common.account.SoraAccount
import jp.co.soramitsu.common.logger.FirebaseWrapper
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.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 +100,9 @@ class CredentialsRepositoryTest {
mockkObject(MnemonicCreator)
mockkObject(SubstrateKeypairFactory)
mockkObject(FirebaseWrapper)
mockkStatic(SubstrateSeedFactory::deriveSeed32)
mockkObject(SubstrateSeedFactory)
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 @@ -64,7 +64,8 @@ dependencies {
implementation project(':common_wallet')
implementation project(":sorasubstrate")
implementation project(":network")
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
Loading