跳到主要内容

Wallet 用法

初始化 Sign Client

val projectId = "" // Get Project ID at https://cloud.walletconnect.com/
val relayUrl = "relay.walletconnect.com"
val serverUrl = "wss://$relayUrl?projectId=$projectId"
val connectionType = ConnectionType.AUTOMATIC or ConnectionType.MANUAL
val appMetaData = Core.Model.AppMetaData(
name = "Wallet Name",
description = "Wallet Description",
url = "Wallet Url",
icons = /*list of icon url strings*/,
redirect = "kotlin-wallet-wc:/request" // Custom Redirect URI
)

CoreClient.initialize(relayServerUrl = serverUrl, connectionType = connectionType, application = this, metaData = appMetaData)

val init = Sign.Params.Init(coreClient = CoreClient)

SignClient.initalize(init) { error ->
// Error will be thrown if there's an isssue during initalization
}

钱包客户端将始终负责向Dapp公开账户(CAPI10兼容),因此也负责签名。 要初始化Sign客户端,在Android Application类中创建一个Sign.Params.Init对象。然后, Sign.Params.Init 对象将被传递给SignClient初始化函数。

钱包

SignClient.WalletDelegate

val walletDelegate = object : SignClient.WalletDelegate {
override fun onSessionProposal(sessionProposal: Sign.Model.SessionProposal) {
// Triggered when wallet receives the session proposal sent by a Dapp
}

override fun onSessionRequest(sessionRequest: Sign.Model.SessionRequest) {
// Triggered when a Dapp sends SessionRequest to sign a transaction or a message
}

override fun onSessionDelete(deletedSession: Sign.Model.DeletedSession) {
// Triggered when the session is deleted by the peer
}

override fun onSessionSettleResponse(settleSessionResponse: Sign.Model.SettledSessionResponse) {
// Triggered when wallet receives the session settlement response from Dapp
}

override fun onSessionUpdateResponse(sessionUpdateResponse: Sign.Model.SessionUpdateResponse) {
// Triggered when wallet receives the session update response from Dapp
}

override fun onConnectionStateChange(state: Sign.Model.ConnectionState) {
//Triggered whenever the connection state is changed
}

override fun onError(error: Sign.Model.Error) {
// Triggered whenever there is an issue inside the SDK
}
}
SignClient.setWalletDelegate(walletDelegate)

SignClient需要传递给它一个SignClient.WalletDelegate ,以便能够公开从Dapp发送的异步更新。

会话允许

注意:在 accounts数组中提供的地址应该跟随CAPI10规则

val proposerPublicKey: String = /*Proposer publicKey from SessionProposal object*/
val namespace: String = /*Namespace identifier, see for reference: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md#syntax*/
val accounts: List<String> = /*List of accounts on chains*/
val methods: List<String> = /*List of methods that wallet approves*/
val events: List<String> = /*List of events that wallet approves*/
val namespaces: Map<String, Sign.Model.Namespaces.Session> = mapOf(namespace, Sign.Model.Namespaces.Session(accounts, methods, events))

val approveParams: Sign.Params.Approve = Sign.Params.Approve(proposerPublicKey, namespaces)
SignClient.approveSession(approveParams) { error -> /*callback for error while approving a session*/ }

要发送批准,将提议者的公钥连同名称空间映射一起传递给SignClient.approveSession函数。

会话拒绝

val proposerPublicKey: String = /*Proposer publicKey from SessionProposal object*/
val rejectionReason: String = /*The reason for rejecting the Session Proposal*/
val rejectionCode: String = /*The code for rejecting the Session Proposal*/
For reference use CAIP-25: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-25.md

val rejectParams: Sign.Params.Reject = Reject(proposerPublicKey, rejectionReason, rejectionCode)
SignClient.rejectSession(rejectParams) { error -> /*callback for error while rejecting a session*/ }

要发送对会话提议的拒绝,传递一个提议publickey,拒绝原因和拒绝代码到SignClient.rejectSession函数。

断开会话

val disconnectionReason: String = /*The reason for disconnecting the Session*/
val disconnectionCode: String = /*The code for for disconnecting the Session*/
val sessionTopic: String = /*Topic from the Session*/
For reference use CAIP-25: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-25.md
val disconnectParams = Sign.Params.Disconnect(sessionTopic, disconnectionReason, disconnectionCode)

SignClient.disconnect(disconnectParams) { error -> /*callback for error while disconnecting a session*/ }

要断开已固定的会话,将断开原因和代码和会话主题传递给SignClient.disconnect函数。

Respond Request

val sessionTopic: String = /*Topic of Session*/
val jsonRpcResponse: Sign.Model.JsonRpcResponse.JsonRpcResult = /*Settled Session Request ID along with request data*/
val result = Sign.Params.Response(sessionTopic = sessionTopic, jsonRpcResponse = jsonRpcResponse)

SignClient.respond(result) { error -> /*callback for error while responding session request*/ }

要响应从Dapps为会话发送的JSON-RPC方法,请提交包含会话topic和请求的Sign.Params.ResponseID以及SignClient.respond 函数的响应数据。

Reject Request

val sessionTopic: String = /*Topic of Session*/
val jsonRpcResponseError: Sign.Model.JsonRpcResponse.JsonRpcError = /*Session Request ID along with error code and message*/
val result = Sign.Params.Response(sessionTopic = sessionTopic, jsonRpcResponse = jsonRpcResponseError)

SignClient.respond(result) { error -> /*callback for error while responding session request*/ }

要拒绝从Dapps为会话发送的JSON-RPC方法,请提交一个带有已确定会话topic和的' aaa ' 请求ID和拒绝数据一起发送到SignClient.respond 函数。

会话更新

accounts 数组中提供的地址应跟随CAPI10规则。

val sessionTopic: String = /*Topic of Session*/
val namespace: String = /*Namespace identifier, see for reference: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md#syntax*/
val accounts: List<String> = /*List of accounts on chains*/
val methods: List<String> = /*List of methods that wallet approves*/
val events: List<String> = /*List of events that wallet approves*/
val namespaces: Map<String, Sign.Model.Namespaces.Session> = mapOf(namespace, Sign.Model.Namespaces.Session(accounts, methods, events))
val updateParams = Sign.Params.Update(sessionTopic, namespaces)

SignClient.update(updateParams) { error -> /*callback for error while sending session update*/ }

要用名称空间更新会话,请提交一个 Sing.Params.Update对象,其中包含要更新会话的会话topic和名称空间SignClient.Update

会话延长

val sessionTopic: String = /*Topic of Session*/
val extendParams = Sign.Params.Extend(sessionTopic = sessionTopic)

WalletConnectClient.extend(exdendParams) { error -> /*callback for error while extending a session*/ }

要扩展一个会话,创建一个Sign.Params.Extend对象,该对象带有会话的主题,以便将会话更新为Sign.Extend。会话 延长7天。

会话 Ping

val sessionTopic: String = /*Topic of Session*/
val pingParams = Sign.Params.Ping(sessionTopic)
val listener = object : Sign.Listeners.SessionPing {
override fun onSuccess(pingSuccess: Model.Ping.Success) {
// Topic being pinged
}

override fun onError(pingError: Model.Ping.Error) {
// Error
}
}

WalletConnectClient.ping(pingParams, listener)

要用会话ping一个对等体,用会话topic的Sign.Params.Ping调用 SignClient.ping 。如果ping成功,则触发Ping监听事件。