Dapp / Requester 用法
我们建议在我们的Kotlin Github仓库中查看Requester的示例实现。
初始化认证客户端
初始化Auth客户端, 创建一个Auth.Params.Init
对象在Android Application类中。 Init对象将需要
application class, Project ID, 和 application AppMetaData. Auth.Params.Init
对象将被传递给 AuthClient
initialize
方法. Auth.Params.Init
也允许自定义URL,通过传递URL字符串到relayUrl
属性。iss
参数对于Dapp / Requester应该为空。
val projectId = "" // Get Project ID at https://cloud.walletconnect.com/
val relayUrl = "relay.walletconnect.com"
val serverUrl = "wss://$relayUrl?projectId=${projectId}"
val appMetaData = Core.Model.AppMetaData(name = "Kotlin.Requester",
description = "Kotlin AuthSDK Requester Implementation",
url = "kotlin.requester.walletconnect.com",
icons = listOf("https://raw.githubusercontent.com/WalletConnect/walletconnect-assets/master/Icon/Gradient/Icon.png"),
redirect = "kotlin-requester-wc:/request"
)
CoreClient.initialize(relayServerUrl = serverUrl, connectionType = ConnectionType.AUTOMATIC, application = this,metaData = appMetaData)
AuthClient.initialize(
init = Auth.Params.Init(core = CoreClient, iss = null)
) { error ->
Log.e(tag(this), error.throwable.stackTraceToString())
}
关于如何初始化CoreClient的更多内容,请访问CoreClient docs部分。
AuthClient.RequesterDelegate
AuthClient需要传递给它一个AuthClient.RequesterDelegate
,以便能够公开从Wallet / Responder发送的异步更新。
object RequesterDelegate : AuthClient.RequesterDelegate {
init {
AuthClient.setRequesterDelegate(this)
}
override fun onAuthResponse(authResponse: Auth.Event.AuthResponse) {
// Triggered when Wallet / Responder respondes to authorisation request. Result can be either signed Cacao object or Error
}
override fun onConnectionStateChange(connectionStateChange: Auth.Event.ConnectionStateChange) {
// Triggered whenever the connection state is changed
}
override fun onError(error: Auth.Event.Error) {
//Triggered whenever the error occurs with Relay Server
}
}
方法
Request
AuthClient.request
异步公开必须与钱包共享的配对URI,如qr码或移动链接。
为了在双方之间建立会话,Wallet / Responder必须将接收到的uri传递给自己端的AuthClient.pair
方法。Dapp / Requester将通过AuthClient.RequesterDelegate
在onAuthReponse
回调上收到响应。
fun randomNonce(): String = Random.nextBytes(16).bytesToHex()
val requestParams = Auth.Params.Request(
chainId = "1", // is the EIP-155 Chain ID to which the session is bound, and the network where Contract Accounts MUST be resolved.
domain = "kotlin.requester.walletconnect.com", // is the RFC 3986 authority that is requesting the signing.
nonce = randomNonce(), // is a randomized token typically chosen by the relying party and used to prevent replay attacks, at least 8 alphanumeric characters.
aud = "https://kotlin.requester.walletconnect.com/login", // is an RFC 3986 URI referring to the resource that is the subject of the signing (as in the subject of a claim).
type = null, // (Not yet implemented) Type of signing. Currently ignored and always set to `eip4361`.
nbf = null, // (optional) is the ISO 8601 datetime string that, if present, indicates when the signed authentication message will become valid.
exp = null, // (optional) is the ISO 8601 datetime string that, if present, indicates when the signed authentication message is no longer valid.
statement = "Sign in with wallet.", // (optional) is a human-readable ASCII assertion that the user will sign, and it must not contain '\n' (the byte 0x0a).
requestId = null, // (optional) is an system-specific identifier that may be used to uniquely refer to the sign-in request.
resources = null, // (optional) is a list of information or references to information the user wishes to have resolved as part of authentication by the relying party. They are expressed
// as RFC 3986 URIs.
)
AuthClient.request(requestParams,
onPairing = { pairing ->
// Callback with Auth.Model.Pairing that contains uri for making pairing with wallet / responder
},
onError = { error ->
Log.e("Requester request", error.throwable.stackTraceToString())
}
)
CACAO
更多关于CACAO的信息可以在这里(https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-74.md)找到。
SIWE / EIP-4361
更多关于SIWE的信息可以在这里(https://eips.ethereum.org/EIPS/eip-4361)找到。