Skip to content

Commit

Permalink
More extensions (#99)
Browse files Browse the repository at this point in the history
* Kotlin extensions Batch 1

* Kotlin extensions Batch 2

* Create ProfileEntity

* Rename passphrase in test
  • Loading branch information
micbakos-rdx authored Apr 22, 2024
1 parent 9d0f058 commit 5ccf175
Show file tree
Hide file tree
Showing 78 changed files with 1,398 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.radixdlt.sargon.accountsGetAccountById
import com.radixdlt.sargon.accountsGetElements
import com.radixdlt.sargon.newAccounts
import com.radixdlt.sargon.newAccountsByAppending
import com.radixdlt.sargon.newAccountsByUpdatingOrAppending
import com.radixdlt.sargon.newAccountsByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newAccountsRemovedById
import com.radixdlt.sargon.newAccountsRemovedElement

Expand All @@ -28,6 +30,12 @@ val Accounts.size: Int
fun Accounts.append(account: Account): Accounts =
newAccountsByAppending(account = account, to = this)

fun Accounts.updateOrInsert(account: Account, index: Int): Accounts =
newAccountsByUpdatingOrInsertingAtIndex(account = account, to = this, index = index.toULong())

fun Accounts.updateOrAppend(account: Account): Accounts =
newAccountsByUpdatingOrAppending(account = account, to = this)

fun Accounts.removeByAddress(address: AccountAddress): Accounts =
newAccountsRemovedById(idOfAccount = address, from = this)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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)

@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
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import com.radixdlt.sargon.AuthorizedDapp
import com.radixdlt.sargon.AuthorizedDapps
import com.radixdlt.sargon.authorizedDappsGetAuthorizedDappById
import com.radixdlt.sargon.authorizedDappsGetElements
import com.radixdlt.sargon.newAccountsByUpdatingOrAppending
import com.radixdlt.sargon.newAuthorizedDapps
import com.radixdlt.sargon.newAuthorizedDappsByAppending
import com.radixdlt.sargon.newAuthorizedDappsByUpdatingOrAppending
import com.radixdlt.sargon.newAuthorizedDappsByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newAuthorizedDappsRemovedById
import com.radixdlt.sargon.newAuthorizedDappsRemovedElement

Expand All @@ -22,14 +25,25 @@ operator fun AuthorizedDapps.invoke() = authorizedDappsGetElements(authorizedDap

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

operator fun AuthorizedDapps.contains(element: AuthorizedDapp) = invoke().contains(element = element)
operator fun AuthorizedDapps.contains(element: AuthorizedDapp) =
invoke().contains(element = element)

val AuthorizedDapps.size: Int
get() = invoke().size

fun AuthorizedDapps.append(authorizedDApp: AuthorizedDapp): AuthorizedDapps =
newAuthorizedDappsByAppending(authorizedDapp = authorizedDApp, to = this)

fun AuthorizedDapps.updateOrInsert(authorizedDApp: AuthorizedDapp, index: Int): AuthorizedDapps =
newAuthorizedDappsByUpdatingOrInsertingAtIndex(
authorizedDapp = authorizedDApp,
to = this,
index = index.toULong()
)

fun AuthorizedDapps.updateOrAppend(authorizedDApp: AuthorizedDapp): AuthorizedDapps =
newAuthorizedDappsByUpdatingOrAppending(authorizedDapp = authorizedDApp, to = this)

fun AuthorizedDapps.removeByAddress(address: AccountAddress): AuthorizedDapps =
newAuthorizedDappsRemovedById(idOfAuthorizedDapp = address, from = this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import com.radixdlt.sargon.hash
import com.radixdlt.sargon.newBagOfBytesFrom
import kotlin.random.Random

internal fun bagOfBytes(fromString: String) = fromString
.toByteArray(charset = Charsets.UTF_8)
.toBagOfBytes()

internal val BagOfBytes.string: String
get() = toUByteArray().toByteArray().toString(charset = Charsets.UTF_8)

fun bagOfBytesOf(byteArray: ByteArray) = newBagOfBytesFrom(bytes = byteArray)

fun String.hexToBagOfBytes(): BagOfBytes {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.DepositRule
import com.radixdlt.sargon.depositRuleToJsonString
import com.radixdlt.sargon.newDepositRuleFromJsonString

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

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

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

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.HdPathComponent
import com.radixdlt.sargon.HdPath
import com.radixdlt.sargon.NetworkId
import com.radixdlt.sargon.bip44LikePathGetAddressIndex
import com.radixdlt.sargon.bip44LikePathToString
import com.radixdlt.sargon.cap26PathToString
import com.radixdlt.sargon.defaultGetIdPath
import com.radixdlt.sargon.derivationPathToHdPath
import com.radixdlt.sargon.newAccountPath
import com.radixdlt.sargon.newBip44LikePathFromIndex
import com.radixdlt.sargon.newBip44LikePathFromString
import com.radixdlt.sargon.newCap26PathFromString
import com.radixdlt.sargon.newIdentityPath

typealias HDPathValue = UInt

@Throws(SargonException::class)
fun DerivationPath.Cap26.Companion.init(cap26Path: String): DerivationPath.Cap26 =
DerivationPath.Cap26(newCap26PathFromString(string = cap26Path))

fun DerivationPath.Cap26.Companion.account(
networkId: NetworkId,
keyKind: Cap26KeyKind,
index: HDPathValue
): DerivationPath.Cap26 = DerivationPath.Cap26(Cap26Path.Account(newAccountPath(
networkId = networkId,
keyKind = keyKind,
index = index
)))

fun DerivationPath.Cap26.Companion.identity(
networkId: NetworkId,
keyKind: Cap26KeyKind,
index: HDPathValue
): DerivationPath.Cap26 = DerivationPath.Cap26(Cap26Path.Identity(newIdentityPath(
networkId = networkId,
keyKind = keyKind,
index = index
)))

val DerivationPath.Cap26.string: String
get() = cap26PathToString(path = value)

@Throws(SargonException::class)
fun DerivationPath.Bip44Like.Companion.init(bip44LikePath: String): DerivationPath.Bip44Like =
DerivationPath.Bip44Like(newBip44LikePathFromString(string = bip44LikePath))

fun DerivationPath.Bip44Like.Companion.init(index: HdPathComponent): DerivationPath.Bip44Like =
DerivationPath.Bip44Like(newBip44LikePathFromIndex(index = index.value))
fun DerivationPath.Bip44Like.Companion.init(index: HDPathValue): DerivationPath.Bip44Like =
DerivationPath.Bip44Like(newBip44LikePathFromIndex(index = index))

val DerivationPath.Bip44Like.addressIndex: HDPathValue
get() = bip44LikePathGetAddressIndex(path = value)

val DerivationPath.Bip44Like.string: String
get() = bip44LikePathToString(path = value)
Expand All @@ -37,5 +68,12 @@ val DerivationPath.string: String
is DerivationPath.Cap26 -> string
}

val DerivationPath.hdPath: HdPath
get() = derivationPathToHdPath(path = this)

val DerivationPath.nonHardenedIndex: HDPathValue
get() = hdPath.components.last() // safe, we disallow empty paths.
.nonHardenedValue

fun Bip44LikePath.asGeneral() = DerivationPath.Bip44Like(this)
fun Cap26Path.asGeneral() = DerivationPath.Cap26(this)
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AuthorizedDapp
import com.radixdlt.sargon.AuthorizedDapps
import com.radixdlt.sargon.EntityFlag
import com.radixdlt.sargon.EntityFlags
import com.radixdlt.sargon.entityFlagsElementCount
import com.radixdlt.sargon.entityFlagsGetElements
import com.radixdlt.sargon.entityFlagsGetEntityFlagById
import com.radixdlt.sargon.newAuthorizedDappsByUpdatingOrAppending
import com.radixdlt.sargon.newAuthorizedDappsByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newEntityFlags
import com.radixdlt.sargon.newEntityFlagsByAppending
import com.radixdlt.sargon.newEntityFlagsByUpdatingOrAppending
import com.radixdlt.sargon.newEntityFlagsByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newEntityFlagsRemovedElement

@Throws(SargonException::class)
Expand All @@ -29,6 +35,16 @@ val EntityFlags.size: Int
fun EntityFlags.append(entityFlag: EntityFlag): EntityFlags =
newEntityFlagsByAppending(entityFlag = entityFlag, to = this)

fun EntityFlags.updateOrInsert(entityFlag: EntityFlag, index: Int): EntityFlags =
newEntityFlagsByUpdatingOrInsertingAtIndex(
entityFlag = entityFlag,
to = this,
index = index.toULong()
)

fun EntityFlags.updateOrAppend(entityFlag: EntityFlag): EntityFlags =
newEntityFlagsByUpdatingOrAppending(entityFlag = entityFlag, to = this)

fun EntityFlags.remove(entityFlag: EntityFlag): EntityFlags =
newEntityFlagsRemovedElement(entityFlag = entityFlag, from = this)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
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
import com.radixdlt.sargon.factorSourceIDFromAddressToJsonBytes
import com.radixdlt.sargon.factorSourceIDFromHashToJsonBytes
import com.radixdlt.sargon.newFactorSourceIDFromAddressFromJsonBytes
import com.radixdlt.sargon.newFactorSourceIDFromHashFromJsonBytes

fun FactorSourceIdFromHash.asGeneral() = FactorSourceId.Hash(value = this)

fun FactorSourceIdFromAddress.asGeneral() = FactorSourceId.Address(value = this)
fun FactorSourceIdFromAddress.asGeneral() = FactorSourceId.Address(
value = this
)

@Throws(SargonException::class)
fun FactorSourceId.Address.Companion.deserializeFromJsonBytes(
jsonBytes: BagOfBytes
): FactorSourceId.Address =
newFactorSourceIDFromAddressFromJsonBytes(jsonBytes = jsonBytes).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

@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
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.DeviceFactorSource
import com.radixdlt.sargon.EntityFlag
import com.radixdlt.sargon.EntityFlags
import com.radixdlt.sargon.FactorSource
import com.radixdlt.sargon.FactorSourceId
import com.radixdlt.sargon.FactorSources
import com.radixdlt.sargon.LedgerHardwareWalletFactorSource
import com.radixdlt.sargon.factorSourcesElementCount
import com.radixdlt.sargon.factorSourcesGetElements
import com.radixdlt.sargon.factorSourcesGetFactorSourceById
import com.radixdlt.sargon.newEntityFlagsByUpdatingOrAppending
import com.radixdlt.sargon.newEntityFlagsByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newFactorSources
import com.radixdlt.sargon.newFactorSourcesByAppending
import com.radixdlt.sargon.newFactorSourcesByUpdatingOrAppending
import com.radixdlt.sargon.newFactorSourcesByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newFactorSourcesRemovedById
import com.radixdlt.sargon.newFactorSourcesRemovedElement

Expand All @@ -33,6 +39,16 @@ val FactorSources.size: Int
fun FactorSources.append(factorSource: FactorSource): FactorSources =
newFactorSourcesByAppending(factorSource = factorSource, to = this)

fun FactorSources.updateOrInsert(factorSource: FactorSource, index: Int): FactorSources =
newFactorSourcesByUpdatingOrInsertingAtIndex(
factorSource = factorSource,
to = this,
index = index.toULong()
)

fun FactorSources.updateOrAppend(factorSource: FactorSource): FactorSources =
newFactorSourcesByUpdatingOrAppending(factorSource = factorSource, to = this)

/**
* FactorSources is NonEmpty, so this throws if the resulting collection would be empty when
* removing the element would result in an empty copy.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Gateway
import com.radixdlt.sargon.NetworkId
import com.radixdlt.sargon.gatewayIsWellknown
import com.radixdlt.sargon.gatewayMainnet
import com.radixdlt.sargon.gatewayStokenet
import com.radixdlt.sargon.gatewayToString
import com.radixdlt.sargon.gatewayWellknownGateways
import com.radixdlt.sargon.newGatewayForNetworkId
import com.radixdlt.sargon.newGatewayWithUrlOnNetwork

val Gateway.Companion.mainnet: Gateway
get() = gatewayMainnet()

val Gateway.Companion.stokenet: Gateway
get() = gatewayStokenet()
get() = gatewayStokenet()

val Gateway.Companion.wellKnown: List<Gateway>
get() = gatewayWellknownGateways()

@Throws(SargonException::class)
fun Gateway.Companion.init(url: String, networkId: NetworkId): Gateway =
newGatewayWithUrlOnNetwork(url = url, networkId = networkId)

fun Gateway.Companion.forNetwork(networkId: NetworkId): Gateway =
newGatewayForNetworkId(networkId = networkId)

/**
* Returns the [Gateway]'s url as [String]
*/
val Gateway.string: String
get() = gatewayToString(gateway = this)

val Gateway.isWellKnown: Boolean
get() = gatewayIsWellknown(gateway = this)
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Gateway
import com.radixdlt.sargon.Gateways
import com.radixdlt.sargon.gatewaysGetAllElements
import com.radixdlt.sargon.newGateways
import com.radixdlt.sargon.newGatewaysChangingCurrent
import com.radixdlt.sargon.newGatewaysDefault

/**
* Constructs [Gateways] with [current] set as active Gateway.
*/
fun Gateways.Companion.init(current: Gateway) = newGateways(current = current)

/**
* The default configuration for each new user:
* Current: mainnet
* Other: [ stokenet ]
*/
val Gateways.Companion.default
get() = newGatewaysDefault()

val Gateways.all: List<Gateway>
get() = gatewaysGetAllElements(gateways = this)

@Throws(SargonException::class)
fun Gateways.changeCurrent(newCurrent: Gateway) = newGatewaysChangingCurrent(
to = newCurrent,
gateways = 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.Exactly32Bytes
import com.radixdlt.sargon.Hash
import com.radixdlt.sargon.hashGetBytes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.HdPathComponent
import com.radixdlt.sargon.hdPathComponentGetNonHardenedValue

val HdPathComponent.nonHardenedValue: HDPathValue
get() = hdPathComponentGetNonHardenedValue(component = this)
Loading

0 comments on commit 5ccf175

Please sign in to comment.