Skip to content

Commit

Permalink
Default optional fields to null
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Mar 8, 2024
1 parent 314b5e0 commit 9e01512
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ internal fun Modal.Params.Authenticate.toSign(): Sign.Params.Authenticate = with
Sign.Params.Authenticate(
pairingTopic,
chains = chains,
type = type ?: CacaoType.CAIP222.header,
domain = domain,
uri = aud,
uri = uri,
nonce = nonce,
nbf = nbf,
exp = exp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ object Modal {
val chains: List<String>,
val domain: String,
val nonce: String,
val aud: String,
val type: String?,
val nbf: String?,
val exp: String?,
val statement: String?,
val requestId: String?,
val resources: List<String>?,
val methods: List<String>?,
val expiry: Long?
val uri: String,
val nbf: String? = null,
val exp: String? = null,
val statement: String? = null,
val requestId: String? = null,
val resources: List<String>? = null,
val methods: List<String>? = null,
val expiry: Long? = null
) : Params()

data class Disconnect(val sessionTopic: String) : Params()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object WalletConnectModal {

//Responses
fun onSessionRequestResponse(response: Modal.Model.SessionRequestResponse)
fun onSessionAuthenticateResponse(sessionUpdateResponse: Modal.Model.SessionAuthenticateResponse)
fun onSessionAuthenticateResponse(sessionUpdateResponse: Modal.Model.SessionAuthenticateResponse) {}

// Utils
fun onProposalExpired(proposal: Modal.Model.ExpiredProposal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,14 @@ internal fun Modal.Params.Authenticate.toSign(): Sign.Params.Authenticate = with
pairingTopic,
chains = chains,
domain = domain,
uri = aud,
uri = uri,
nonce = nonce,
nbf = nbf,
exp = exp,
statement = statement,
requestId = requestId,
resources = resources,
methods = methods,
type = type ?: CacaoType.CAIP222.header,
expiry = expiry
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ object Modal {
val chains: List<String>,
val domain: String,
val nonce: String,
val aud: String,
val type: String?,
val nbf: String?,
val exp: String?,
val statement: String?,
val requestId: String?,
val resources: List<String>?,
val methods: List<String>?,
val expiry: Long
val uri: String,
val nbf: String? = null,
val exp: String? = null,
val statement: String? = null,
val requestId: String? = null,
val resources: List<String>? = null,
val methods: List<String>? = null,
val expiry: Long? = null
) : Params()

@Deprecated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object Web3Modal {

//Responses
fun onSessionRequestResponse(response: Modal.Model.SessionRequestResponse)
fun onSessionAuthenticateResponse(sessionUpdateResponse: Modal.Model.SessionAuthenticateResponse)
fun onSessionAuthenticateResponse(sessionUpdateResponse: Modal.Model.SessionAuthenticateResponse) {}

// Utils
fun onProposalExpired(proposal: Modal.Model.ExpiredProposal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.walletconnect.sign.client.Sign
import com.walletconnect.sign.client.SignClient
import com.walletconnect.sign.client.utils.CacaoSigner
import com.walletconnect.sign.client.utils.generateAuthObject
import com.walletconnect.sign.client.utils.generateAuthPayloadParams
import com.walletconnect.sign.test.scenario.SignClientInstrumentedActivityScenario
import com.walletconnect.sign.test.utils.TestClient
import com.walletconnect.sign.test.utils.dapp.DappDelegate
Expand Down Expand Up @@ -49,16 +50,23 @@ class SessionAuthenticateInstrumentedAndroidTest {
val issuerToMessages = mutableListOf<Pair<String, String>>()
val cacaos = mutableListOf<Sign.Model.Cacao>()

sessionAuthenticate.payloadParams.chains.forEach { chain ->
val authPayloadParams =
generateAuthPayloadParams(
sessionAuthenticate.payloadParams,
supportedChains = listOf("eip155:1", "eip155:137", "eip155:56"),
supportedMethods = listOf("personal_sign", "eth_signTypedData", "eth_sign")
)

authPayloadParams.chains.forEach { chain ->
val issuer = "did:pkh:$chain:$address"
val message = WalletSignClient.formatAuthMessage(Sign.Params.FormatMessage(sessionAuthenticate.payloadParams, issuer)) ?: throw Exception("Invalid message")
val message = WalletSignClient.formatAuthMessage(Sign.Params.FormatMessage(authPayloadParams, issuer)) ?: throw Exception("Invalid message")
issuerToMessages.add(issuer to message)
}

issuerToMessages.forEach { issuerToMessage ->
val messageToSign = Numeric.toHexString(issuerToMessage.second.toByteArray())
val signature = CacaoSigner.signHex(messageToSign, privateKey.hexToBytes(), SignatureType.EIP191)
val cacao = generateAuthObject(sessionAuthenticate.payloadParams, issuerToMessage.first, signature)
val cacao = generateAuthObject(authPayloadParams, issuerToMessage.first, signature)
cacaos.add(cacao)
}

Expand Down Expand Up @@ -90,16 +98,23 @@ class SessionAuthenticateInstrumentedAndroidTest {
val issuerToMessages = mutableListOf<Pair<String, String>>()
val cacaos = mutableListOf<Sign.Model.Cacao>()

sessionAuthenticate.payloadParams.chains.forEach { chain ->
val authPayloadParams =
generateAuthPayloadParams(
sessionAuthenticate.payloadParams,
supportedChains = listOf("eip155:1", "eip155:137", "eip155:56"),
supportedMethods = listOf("personal_sign", "eth_signTypedData", "eth_sign")
)

authPayloadParams.chains.forEach { chain ->
val issuer = "did:pkh:$chain:$address"
val message = WalletSignClient.formatAuthMessage(Sign.Params.FormatMessage(sessionAuthenticate.payloadParams, issuer)) ?: throw Exception("Invalid message")
val message = WalletSignClient.formatAuthMessage(Sign.Params.FormatMessage(authPayloadParams, issuer)) ?: throw Exception("Invalid message")
issuerToMessages.add(issuer to message)
}

issuerToMessages.forEach { issuerToMessage ->
val messageToSign = Numeric.toHexString(issuerToMessage.second.toByteArray())
val signature = CacaoSigner.signHex(messageToSign, privateKey.hexToBytes(), SignatureType.EIP191)
val cacao = generateAuthObject(sessionAuthenticate.payloadParams, issuerToMessage.first, signature)
val cacao = generateAuthObject(authPayloadParams, issuerToMessage.first, signature)
cacaos.add(cacao)
}

Expand Down Expand Up @@ -143,15 +158,22 @@ class SessionAuthenticateInstrumentedAndroidTest {
val messages = mutableListOf<Pair<String, String>>()
val cacaos = mutableListOf<Sign.Model.Cacao>()

sessionAuthenticate.payloadParams.chains.forEach { chain ->
val authPayloadParams =
generateAuthPayloadParams(
sessionAuthenticate.payloadParams,
supportedChains = listOf("eip155:1", "eip155:137", "eip155:56"),
supportedMethods = listOf("personal_sign", "eth_signTypedData", "eth_sign")
)

authPayloadParams.chains.forEach { chain ->
val issuer = "did:pkh:$chain:$address"
val message = WalletSignClient.formatAuthMessage(Sign.Params.FormatMessage(sessionAuthenticate.payloadParams, issuer)) ?: throw Exception("Invalid message")
val message = WalletSignClient.formatAuthMessage(Sign.Params.FormatMessage(authPayloadParams, issuer)) ?: throw Exception("Invalid message")
messages.add(issuer to message)
}

messages.forEach { message ->
val signature = CacaoSigner.signHex("messageToSign", privateKey.hexToBytes(), SignatureType.EIP191)
val cacao = generateAuthObject(sessionAuthenticate.payloadParams, message.first, signature)
val cacao = generateAuthObject(authPayloadParams, message.first, signature)
cacaos.add(cacao)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ val dappClientConnect = { pairing: Core.Model.Pairing ->

fun dappClientAuthenticate(onPairing: (String) -> Unit) {
val authenticateParams = Sign.Params.Authenticate(
type = "caip222",
chains = listOf("eip155:1", "eip155:137"),
domain = "sample.dapp",
uri = "https://react-auth-dapp.vercel.app/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,13 @@ object Sign {
val domain: String,
val nonce: String,
val uri: String,
val type: String?,
val nbf: String?,
val exp: String?,
val statement: String?,
val requestId: String?,
val resources: List<String>?,
val methods: List<String>?,
val expiry: Long?
val nbf: String? = null,
val exp: String? = null,
val statement: String? = null,
val requestId: String? = null,
val resources: List<String>? = null,
val methods: List<String>? = null,
val expiry: Long? = null
) : Params()

data class FormatMessage(val payloadParams: Model.PayloadParams, val iss: String) : Params()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ internal fun Sign.Model.PayloadParams.toEngine(): EngineDO.PayloadParams = with(
@JvmSynthetic
internal fun Sign.Params.Authenticate.toAuthenticate(): EngineDO.Authenticate = with(this) {
EngineDO.Authenticate(
type = CacaoType.CAIP222.header,
type = CacaoType.EIP4361.header,
chains = chains,
domain = domain,
aud = uri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,9 @@ class ChainSelectionViewModel : ViewModel() {

val authenticateParams
get() = Modal.Params.Authenticate(
type = "eip4361",
chains = uiState.value.filter { it.isSelected }.map { it.chainId },
domain = "sample.kotlin.dapp",
aud = "https://web3inbox.com/all-apps",
uri = "https://web3inbox.com/all-apps",
nonce = randomBytes(12).bytesToHex(),
exp = null,
nbf = null,
Expand All @@ -224,10 +223,9 @@ class ChainSelectionViewModel : ViewModel() {

val siweParams
get() = Modal.Params.Authenticate(
type = "eip4361",
chains = uiState.value.filter { it.isSelected }.map { it.chainId },
domain = "sample.kotlin.dapp",
aud = "https://web3inbox.com/all-apps",
uri = "https://web3inbox.com/all-apps",
nonce = randomBytes(12).bytesToHex(),
exp = null,
nbf = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SessionAuthenticateViewModel : ViewModel() {
try {
val sessionAuthenticate = WCDelegate.sessionAuthenticateEvent!!.first
val auths = mutableListOf<Wallet.Model.Cacao>()

val authPayloadParams =
Web3Wallet.generateAuthPayloadParams(
sessionAuthenticate.payloadParams,
Expand Down

0 comments on commit 9e01512

Please sign in to comment.