跳到主要内容

Dapp 用法

初始化 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 = "Dapp Name",
description = "Dapp Description",
url = "Dapp Url",
icons = /*list of icon url strings*/,
redirect = "kotlin-dapp-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客户端负责启动与钱包的连接,并从钱包中定义所需的名称空间(CAIP-2),还负责发送请求。要初始化Sign客户端,在Android Application类中创建一个Sign.Params.Init对象。然后,Sign.Params.Init对象将被传递给Sign.Params.Init初始化函数。

Dapp

SignClient.DappDelegate

val dappDelegate = object : SignClient.DappDelegate {
override fun onSessionApproved(approvedSession: Sign.Model.ApprovedSession) {
// Triggered when Dapp receives the session approval from wallet
}

override fun onSessionRejected(rejectedSession: Sign.Model.RejectedSession) {
// Triggered when Dapp receives the session rejection from wallet
}

override fun onSessionUpdate(updatedSession: Sign.Model.UpdatedSession) {
// Triggered when Dapp receives the session update from wallet
}

override fun onSessionExtend(session: Sign.Model.Session) {
// Triggered when Dapp receives the session extend from wallet
}

override fun onSessionEvent(sessionEvent: Sign.Model.SessionEvent) {
// Triggered when the peer emits events that match the list of events agreed upon session settlement
}

override fun onSessionDelete(deletedSession: Sign.Model.DeletedSession) {
// Triggered when Dapp receives the session delete from wallet
}

override fun onSessionRequestResponse(response: Sign.Model.SessionRequestResponse) {
// Triggered when Dapp receives the session request response from wallet
}

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(dappDelegate)

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

Connect

val namespace: String = /*Namespace identifier, see for reference: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md#syntax*/
val chains: List<String> = /*List of chains that wallet will be requested for*/
val methods: List<String> = /*List of methods that wallet will be requested for*/
val events: List<String> = /*List of events that wallet will be requested for*/
val namespaces: Map<String, Sign.Model.Namespaces.Proposal> = mapOf(namespace, Sign.Model.Namespaces.Proposal(accounts, methods, events))
val pairing: Core.Model.Pairing = /*Either an active or inactive pairing*/
val connectParams = Sign.Params.Connect(namespaces, pairing)

fun SignClient.connect(connectParams,
{ onSuccess ->
/*callback that returns letting you know that you have successfully initiated connecting*/
},
{ error ->
/*callback for error while trying to initiate a connection with a peer*/
}
)

获取已确定的会话列表

SignClient.getListOfSettledSessions()

获取当前建立的会话列表, 调用 SignClient.getListOfSettledSessions() 返回 Session.

获取topic挂起session请求的列表

SignClient.getPendingRequests(topic: String)

获取topic挂起session请求的列表, 调用SignClient.getPendingRequests() 返回 PendingRequest 对象包含requestId,方法,chainIs和等待请求的参数。

Project ID

有关项目ID,请参阅Project ID