Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Feb 11, 2024
1 parent cbda1f7 commit 1779c0f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ data class Cacao(
const val ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
const val RECAPS_PREFIX = "urn:recap:"
const val ATT_KEY = "att"
const val ACTION_TYPE_POSITION = 0
const val ACTION_POSITION = 1
const val ACTION_DELIMITER = "/"
}
}
}
Expand Down Expand Up @@ -100,7 +97,7 @@ fun Cacao.Payload.toCAIP222Message(chainName: String = "Ethereum"): String {

private fun Cacao.Payload.getActionsString(): String {
val map = decodeReCaps()
if (map.isEmpty()) return "The map is empty."
if (map.isEmpty()) throw Exception("Decoded ReCaps map is empty")
var result = ""
var index = 1

Expand All @@ -120,7 +117,7 @@ private fun Cacao.Payload.getActionsString(): String {
}

private fun Cacao.Payload.getActions(): List<String> {
return decodeReCaps().values.flatten()
return decodeReCaps().values.flatten().map { action -> action.substringAfter('/') }
}

private fun Cacao.Payload.decodeReCaps(): MutableMap<String, MutableList<String>> {
Expand All @@ -145,5 +142,5 @@ private fun Cacao.Payload.decodeReCaps(): MutableMap<String, MutableList<String>
}
}
reCapsMap.forEach { entry -> entry.value.sort() }
return reCapsMap
return reCapsMap.toSortedMap()
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ internal class MapperTest {
resources = listOf(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/",
"https://example.com/my-web2-claim.json",
encodedSignRecaps,
encodedNotifyRecaps
encodedNotifyRecaps,
encodedSignRecaps
)
)

Expand All @@ -152,8 +152,8 @@ internal class MapperTest {
"Resources:\n" +
"- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/\n" +
"- https://example.com/my-web2-claim.json\n" +
"- $encodedSignRecaps\n" +
"- $encodedNotifyRecaps"
"- $encodedNotifyRecaps\n" +
"- $encodedSignRecaps"

assertEquals(message, payload.toCAIP222Message(chainName))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ internal class ApproveSessionAuthenticateUseCase(
return@supervisorScope
}
//todo: expiry check
//todo: check for single chain

//todo: check for single chain - if not eip155 throw
val sessionAuthenticateParams: SignParams.SessionAuthenticateParams = jsonRpcHistoryEntry.params
val receiverPublicKey = PublicKey(sessionAuthenticateParams.requester.publicKey)
val receiverMetadata = sessionAuthenticateParams.requester.metadata
Expand All @@ -84,7 +83,7 @@ internal class ApproveSessionAuthenticateUseCase(

val accounts = cacaos.map { cacao -> Issuer(cacao.payload.iss).accountId }
val chains = cacaos.map { cacao -> Issuer(cacao.payload.iss).chainId }
val namespace = Issuer(cacaos.first().payload.iss).namespace
val namespace = Issuer(cacaos.first().payload.iss).namespace //TODO: should always get iss from the first cacao?
val methods = cacaos.map { cacao -> cacao.payload.methods }.flatten().distinct()
val requiredNamespace: Map<String, Namespace.Proposal> = mapOf(namespace to Namespace.Proposal(events = listOf(), methods = methods, chains = chains))
val sessionNamespaces: Map<String, Namespace.Session> = mapOf(namespace to Namespace.Session(accounts = accounts, events = listOf(), methods = methods, chains = chains))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import kotlinx.coroutines.supervisorScope
internal class FormatAuthenticateMessageUseCase : FormatAuthenticateMessageUseCaseInterface {
override suspend fun formatMessage(payloadParams: PayloadParams, iss: String): String = supervisorScope {
val issuer = Issuer(iss)

//todo: add validation for one type of namespaces in payload chains and namespace MUST be in issuer
if (!payloadParams.chains.contains(issuer.chainId)) throw Exception("Issuer chainId does not match with PayloadParams")
payloadParams.chains.forEach { chainId -> if (!CoreValidator.isChainIdCAIP2Compliant(chainId)) throw Exception("Chains must be CAIP-2 compliant") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,13 @@ internal class SessionAuthenticateUseCase(
//TODO: Multi namespace - if other than eip155 - throw error, add chains validation
val pairing = getPairingForSessionAuthenticate(pairingTopic)
val optionalNamespaces = getNamespacesFromReCaps(payloadParams.chains, methods ?: emptyList()).toMapOfEngineNamespacesOptional()

val namespace = SignValidator.getNamespaceKeyFromChainId(payloadParams.chains.first())

// val signReCapsJson =
// JSONObject().put(
// "att",
// JSONObject().put(
// "eip155",
// JSONObject()
// .put("request/eth_signTypedData_v4", JSONArray().put(0, JSONObject()))
// .put("request/personal_sign", JSONArray().put(0, JSONObject()))
// )
// )

//TODO: BUILDING RECAPS JSON
val actionsJsonObject = JSONObject()
methods?.forEach { method -> JSONObject().put("request/$method", JSONArray().put(0, JSONObject())) }
methods?.forEach { method -> actionsJsonObject.put("request/$method", JSONArray().put(0, JSONObject())) }
val recaps = JSONObject().put(ATT_KEY, JSONObject().put(namespace, actionsJsonObject)).toString().replace("\\/", "/")

println("kobe: Sending ReCaps: $recaps")

val base64Recaps = Base64.toBase64String(recaps.toByteArray(Charsets.UTF_8))
val reCapsUrl = "$RECAPS_PREFIX$base64Recaps"


if (payloadParams.resources == null) payloadParams.resources = listOf(reCapsUrl) else payloadParams.resources!!.toMutableList().add(reCapsUrl)
val requesterPublicKey: PublicKey = crypto.generateAndStoreX25519KeyPair()
val responseTopic: Topic = crypto.getTopicFromKey(requesterPublicKey)
Expand Down

0 comments on commit 1779c0f

Please sign in to comment.