Skip to content

Commit

Permalink
Kotlin batch (#103)
Browse files Browse the repository at this point in the history
* WIP Batch 1

* WIP Batch 2

* Rename methods and provide encrypt/decrypt for profile
  • Loading branch information
micbakos-rdx authored Apr 24, 2024
1 parent 11a069e commit 5498570
Show file tree
Hide file tree
Showing 55 changed files with 986 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AppPreferences
import com.radixdlt.sargon.newAppPreferencesDefault

fun AppPreferences.Companion.default() = newAppPreferencesDefault()
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AssetException
import com.radixdlt.sargon.AssetsExceptionList
import com.radixdlt.sargon.ResourceAddress
import com.radixdlt.sargon.assetsExceptionListElementCount
import com.radixdlt.sargon.assetsExceptionListGetAssetExceptionById
import com.radixdlt.sargon.assetsExceptionListGetElements
import com.radixdlt.sargon.newAssetsExceptionList
import com.radixdlt.sargon.newAssetsExceptionListByAppending
import com.radixdlt.sargon.newAssetsExceptionListByUpdatingOrAppending
import com.radixdlt.sargon.newAssetsExceptionListByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newAssetsExceptionListRemovedById
import com.radixdlt.sargon.newAssetsExceptionListRemovedElement

fun AssetsExceptionList.Companion.init(vararg assetException: AssetException): AssetsExceptionList =
init(assetsExceptions = assetException.asList())

fun AssetsExceptionList.Companion.init(assetsExceptions: List<AssetException>): AssetsExceptionList =
newAssetsExceptionList(assetsExceptionList = assetsExceptions)

operator fun AssetsExceptionList.invoke() =
assetsExceptionListGetElements(assetsExceptionList = this)

operator fun AssetsExceptionList.get(index: Int) = invoke().get(index = index)

operator fun AssetsExceptionList.contains(element: AssetException) =
invoke().contains(element = element)

val AssetsExceptionList.size: Int
get() = assetsExceptionListElementCount(assetsExceptionList = this).toInt()

fun AssetsExceptionList.append(assetException: AssetException): AssetsExceptionList =
newAssetsExceptionListByAppending(assetException = assetException, to = this)

fun AssetsExceptionList.updateOrInsert(
assetException: AssetException,
index: Int
): AssetsExceptionList = newAssetsExceptionListByUpdatingOrInsertingAtIndex(
assetException = assetException,
to = this,
index = index.toULong()
)

fun AssetsExceptionList.updateOrAppend(assetException: AssetException): AssetsExceptionList =
newAssetsExceptionListByUpdatingOrAppending(assetException = assetException, to = this)

fun AssetsExceptionList.removeByAddress(address: ResourceAddress): AssetsExceptionList =
newAssetsExceptionListRemovedById(idOfAssetException = address, from = this)

fun AssetsExceptionList.remove(assetException: AssetException): AssetsExceptionList =
newAssetsExceptionListRemovedElement(assetException = assetException, from = this)

fun AssetsExceptionList.getBy(address: ResourceAddress): AssetException? =
assetsExceptionListGetAssetExceptionById(assetsExceptionList = this, id = address)
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AuthorizedDapp
import com.radixdlt.sargon.BagOfBytes
import com.radixdlt.sargon.authorizedDappToJsonBytes
import com.radixdlt.sargon.newAuthorizedDappFromJsonBytes

@Throws(SargonException::class)
fun AuthorizedDapp.Companion.deserializeFromJsonBytes(jsonBytes: BagOfBytes) =
newAuthorizedDappFromJsonBytes(jsonBytes = jsonBytes)
fun AuthorizedDapp.Companion.fromJson(jsonString: String) =
newAuthorizedDappFromJsonBytes(jsonBytes = bagOfBytes(fromString = jsonString))

@Throws(SargonException::class)
fun AuthorizedDapp.Companion.deserializeFromJsonString(jsonString: String) =
deserializeFromJsonBytes(jsonBytes = bagOfBytes(fromString = jsonString))

fun AuthorizedDapp.serializedJsonBytes(): BagOfBytes = authorizedDappToJsonBytes(authorizedDapp = this)
fun AuthorizedDapp.serializedJsonString(): String = serializedJsonBytes().string
fun AuthorizedDapp.toJson(): String = authorizedDappToJsonBytes(authorizedDapp = this).string
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Bip39Language
import com.radixdlt.sargon.Bip39Word
import com.radixdlt.sargon.bip39LanguageWordlist

val Bip39Language.wordList: List<Bip39Word>
get() = bip39LanguageWordlist(language = this)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Bip39WordCount
import com.radixdlt.sargon.CommonException

@Throws(SargonException::class)
fun Bip39WordCount.Companion.init(wordCount: Int) = Bip39WordCount.entries.find {
it.value == wordCount.toUByte()
} ?: throw CommonException.InvalidBip39WordCount(badValue = wordCount.toULong())
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.radixdlt.sargon.depositRuleToJsonString
import com.radixdlt.sargon.newDepositRuleFromJsonString

@Throws(SargonException::class)
fun DepositRule.Companion.deserializeFromJsonString(jsonString: String): DepositRule =
fun DepositRule.Companion.fromJson(jsonString: String): DepositRule =
newDepositRuleFromJsonString(jsonString = jsonString)

fun DepositRule.serializedJsonString(): String = depositRuleToJsonString(depositRule = this)
fun DepositRule.toJson(): String = depositRuleToJsonString(depositRule = this)

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.DepositorsAllowList
import com.radixdlt.sargon.ResourceOrNonFungible
import com.radixdlt.sargon.depositorsAllowListElementCount
import com.radixdlt.sargon.depositorsAllowListGetElements
import com.radixdlt.sargon.depositorsAllowListGetResourceOrNonFungibleById
import com.radixdlt.sargon.newDepositorsAllowList
import com.radixdlt.sargon.newDepositorsAllowListByAppending
import com.radixdlt.sargon.newDepositorsAllowListByUpdatingOrAppending
import com.radixdlt.sargon.newDepositorsAllowListByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newDepositorsAllowListRemovedElement

fun DepositorsAllowList.Companion.init(vararg resourceOrNonFungible: ResourceOrNonFungible): DepositorsAllowList =
init(depositorsAllowList = resourceOrNonFungible.asList())

fun DepositorsAllowList.Companion.init(depositorsAllowList: List<ResourceOrNonFungible>): DepositorsAllowList =
newDepositorsAllowList(depositorsAllowList = depositorsAllowList)

operator fun DepositorsAllowList.invoke() =
depositorsAllowListGetElements(depositorsAllowList = this)

operator fun DepositorsAllowList.get(index: Int) = invoke().get(index = index)

operator fun DepositorsAllowList.contains(element: ResourceOrNonFungible) =
invoke().contains(element = element)

val DepositorsAllowList.size: Int
get() = depositorsAllowListElementCount(depositorsAllowList = this).toInt()

fun DepositorsAllowList.append(resourceOrNonFungible: ResourceOrNonFungible): DepositorsAllowList =
newDepositorsAllowListByAppending(resourceOrNonFungible = resourceOrNonFungible, to = this)

fun DepositorsAllowList.updateOrInsert(
resourceOrNonFungible: ResourceOrNonFungible,
index: Int
): DepositorsAllowList = newDepositorsAllowListByUpdatingOrInsertingAtIndex(
resourceOrNonFungible = resourceOrNonFungible,
to = this,
index = index.toULong()
)

fun DepositorsAllowList.updateOrAppend(
resourceOrNonFungible: ResourceOrNonFungible
): DepositorsAllowList = newDepositorsAllowListByUpdatingOrAppending(
resourceOrNonFungible = resourceOrNonFungible,
to = this
)

fun DepositorsAllowList.remove(
resourceOrNonFungible: ResourceOrNonFungible
): DepositorsAllowList = newDepositorsAllowListRemovedElement(
resourceOrNonFungible = resourceOrNonFungible,
from = this
)

fun DepositorsAllowList.getBy(
resourceOrNonFungible: ResourceOrNonFungible
): ResourceOrNonFungible? = depositorsAllowListGetResourceOrNonFungibleById(
depositorsAllowList = this,
id = resourceOrNonFungible
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import com.radixdlt.sargon.FactorSource
import com.radixdlt.sargon.FactorSourceId
import com.radixdlt.sargon.FactorSourceKind
import com.radixdlt.sargon.LedgerHardwareWalletFactorSource
import com.radixdlt.sargon.MnemonicWithPassphrase
import com.radixdlt.sargon.WalletClientModel
import com.radixdlt.sargon.deviceFactorSourceIsMainBdfs
import com.radixdlt.sargon.factorSourceSupportsBabylon
import com.radixdlt.sargon.factorSourceSupportsOlympia
import com.radixdlt.sargon.newDeviceFactorSourceBabylon
import com.radixdlt.sargon.newDeviceFactorSourceOlympia

val FactorSource.id: FactorSourceId
get() = when (this) {
Expand All @@ -18,6 +25,31 @@ val FactorSource.kind: FactorSourceKind
is FactorSource.Ledger -> value.kind
}

fun FactorSource.Device.Companion.olympia(
mnemonicWithPassphrase: MnemonicWithPassphrase
) = newDeviceFactorSourceOlympia(
mnemonicWithPassphrase = mnemonicWithPassphrase,
walletClientModel = WalletClientModel.ANDROID
).asGeneral()

fun FactorSource.Device.Companion.babylon(
isMain: Boolean,
mnemonicWithPassphrase: MnemonicWithPassphrase
) = newDeviceFactorSourceBabylon(
isMain = isMain,
mnemonicWithPassphrase = mnemonicWithPassphrase,
walletClientModel = WalletClientModel.ANDROID
).asGeneral()

val FactorSource.Device.isMain: Boolean
get() = deviceFactorSourceIsMainBdfs(deviceFactorSource = value)

val FactorSource.supportsOlympia: Boolean
get() = factorSourceSupportsOlympia(factorSource = this)

val FactorSource.supportsBabylon: Boolean
get() = factorSourceSupportsBabylon(factorSource = this)

val DeviceFactorSource.kind: FactorSourceKind
get() = FactorSourceKind.DEVICE

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.BagOfBytes
import com.radixdlt.sargon.FactorSourceId
import com.radixdlt.sargon.FactorSourceIdFromAddress
import com.radixdlt.sargon.FactorSourceIdFromHash
Expand All @@ -16,30 +15,21 @@ fun FactorSourceIdFromAddress.asGeneral() = FactorSourceId.Address(
)

@Throws(SargonException::class)
fun FactorSourceId.Address.Companion.deserializeFromJsonBytes(
jsonBytes: BagOfBytes
): FactorSourceId.Address =
newFactorSourceIDFromAddressFromJsonBytes(jsonBytes = jsonBytes).asGeneral()
fun FactorSourceId.Address.Companion.fromJson(
jsonString: String
): FactorSourceId.Address = newFactorSourceIDFromAddressFromJsonBytes(
jsonBytes = bagOfBytes(fromString = jsonString)
).asGeneral()

@Throws(SargonException::class)
fun FactorSourceId.Address.Companion.deserializeFromJsonString(jsonString: String): FactorSourceId.Address =
deserializeFromJsonBytes(jsonBytes = bagOfBytes(fromString = jsonString))

fun FactorSourceId.Address.serializedJsonBytes(): BagOfBytes =
factorSourceIDFromAddressToJsonBytes(factorSourceIDFromAddress = value)

fun FactorSourceId.Address.serializedJsonString(): String = serializedJsonBytes().string
fun FactorSourceId.Address.toJson(): String =
factorSourceIDFromAddressToJsonBytes(factorSourceIDFromAddress = value).string

@Throws(SargonException::class)
fun FactorSourceId.Hash.Companion.deserializeFromJsonBytes(
jsonBytes: BagOfBytes
): FactorSourceId.Hash = newFactorSourceIDFromHashFromJsonBytes(jsonBytes = jsonBytes).asGeneral()

@Throws(SargonException::class)
fun FactorSourceId.Hash.Companion.deserializeFromJsonString(jsonString: String): FactorSourceId.Hash =
deserializeFromJsonBytes(jsonBytes = bagOfBytes(fromString = jsonString))

fun FactorSourceId.Hash.serializedJsonBytes(): BagOfBytes =
factorSourceIDFromHashToJsonBytes(factorSourceIDFromHash = value)

fun FactorSourceId.Hash.serializedJsonString(): String = serializedJsonBytes().string
fun FactorSourceId.Hash.Companion.fromJson(
jsonString: String
): FactorSourceId.Hash = newFactorSourceIDFromHashFromJsonBytes(
jsonBytes = bagOfBytes(fromString = jsonString)
).asGeneral()

fun FactorSourceId.Hash.toJson(): String =
factorSourceIDFromHashToJsonBytes(factorSourceIDFromHash = value).string
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.FactorSourceKind
import com.radixdlt.sargon.factorSourceKindToString
import com.radixdlt.sargon.newFactorSourceKindFromString

@Throws(SargonException::class)
fun FactorSourceKind.Companion.init(string: String) = newFactorSourceKindFromString(string = string)

val FactorSourceKind.string: String
get() = factorSourceKindToString(kind = this)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package com.radixdlt.sargon.extensions
import com.radixdlt.sargon.Exactly32Bytes
import com.radixdlt.sargon.Hash
import com.radixdlt.sargon.hashGetBytes
import com.radixdlt.sargon.newHashFromBytes
import com.radixdlt.sargon.newHashFromString

@Throws(SargonException::class)
fun Hash.Companion.init(string: String) = newHashFromString(string = string)

fun Hash.Companion.init(bytes: Exactly32Bytes) = newHashFromBytes(bytes = bytes)

val Hash.bytes: Exactly32Bytes
get() = hashGetBytes(hash = this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.BagOfBytes
import com.radixdlt.sargon.DeviceInfo
import com.radixdlt.sargon.Header
import com.radixdlt.sargon.headerToJsonBytes
Expand All @@ -11,13 +10,7 @@ fun Header.Companion.init(creatingDevice: DeviceInfo): Header =
newHeaderWithCreatingDevice(creatingDevice = creatingDevice)

@Throws(SargonException::class)
fun Header.Companion.deserializeFromJsonBytes(jsonBytes: BagOfBytes) =
newHeaderFromJsonBytes(jsonBytes = jsonBytes)
fun Header.Companion.fromJson(jsonString: String) =
newHeaderFromJsonBytes(jsonBytes = bagOfBytes(fromString = jsonString))

@Throws(SargonException::class)
fun Header.Companion.deserializeFromJsonString(jsonString: String) =
deserializeFromJsonBytes(jsonBytes = bagOfBytes(fromString = jsonString))

fun Header.serializedJsonBytes(): BagOfBytes = headerToJsonBytes(header = this)

fun Header.serializedJsonString(): String = serializedJsonBytes().string
fun Header.toJson(): String = headerToJsonBytes(header = this).string
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.LedgerHardwareWalletModel
import com.radixdlt.sargon.ledgerHwWalletModelToString
import com.radixdlt.sargon.newLedgerHwWalletModelFromString

@Throws(SargonException::class)
fun LedgerHardwareWalletModel.Companion.init(string: String) =
newLedgerHwWalletModelFromString(string = string)

val LedgerHardwareWalletModel.string: String
get() = ledgerHwWalletModelToString(model = this)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.BagOfBytes
import com.radixdlt.sargon.DerivationPath
import com.radixdlt.sargon.Hash
import com.radixdlt.sargon.HierarchicalDeterministicPublicKey
Expand All @@ -13,17 +12,11 @@ import com.radixdlt.sargon.mnemonicWithPassphraseValidatePublicKeys
import com.radixdlt.sargon.newMnemonicWithPassphraseFromJsonBytes

@Throws(SargonException::class)
fun MnemonicWithPassphrase.Companion.deserializeFromJsonBytes(jsonBytes: BagOfBytes) =
newMnemonicWithPassphraseFromJsonBytes(jsonBytes = jsonBytes)
fun MnemonicWithPassphrase.Companion.fromJson(fromJson: String) =
newMnemonicWithPassphraseFromJsonBytes(bagOfBytes(fromString = fromJson))

@Throws(SargonException::class)
fun MnemonicWithPassphrase.Companion.deserializeFromJsonString(fromJson: String) =
deserializeFromJsonBytes(bagOfBytes(fromString = fromJson))

fun MnemonicWithPassphrase.serializedJsonBytes(): BagOfBytes =
mnemonicWithPassphraseToJsonBytes(mnemonicWithPassphrase = this)

fun MnemonicWithPassphrase.serializedJsonString(): String = serializedJsonBytes().string
fun MnemonicWithPassphrase.toJson(): String =
mnemonicWithPassphraseToJsonBytes(mnemonicWithPassphrase = this).string

fun MnemonicWithPassphrase.validate(hdPublicKeys: List<HierarchicalDeterministicPublicKey>): Boolean =
mnemonicWithPassphraseValidatePublicKeys(mnemonicWithPassphrase = this, hdKeys = hdPublicKeys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import com.radixdlt.sargon.newPersonaDataEntryEmailAddressFromJsonString
import com.radixdlt.sargon.personaDataEntryEmailAddressToJsonString

@Throws(SargonException::class)
fun PersonaDataEntryEmailAddress.Companion.deserializeFromJsonString(
fun PersonaDataEntryEmailAddress.Companion.fromJson(
jsonString: String
): PersonaDataEntryEmailAddress =
newPersonaDataEntryEmailAddressFromJsonString(jsonString = jsonString)

fun PersonaDataEntryEmailAddress.serializedJsonString(): String =
fun PersonaDataEntryEmailAddress.toJson(): String =
personaDataEntryEmailAddressToJsonString(personaDataEntryEmailAddress = this)
Loading

0 comments on commit 5498570

Please sign in to comment.