feat(@desktop/walletconnect): aligning namespaces with WalletConnect v2.0 protocol

Closes: #12825
This commit is contained in:
Sale Djenic 2023-11-24 16:59:19 +01:00 committed by saledjenic
parent 1e567e4cc2
commit ce0dbe533b
6 changed files with 78 additions and 14 deletions

View File

@ -1,4 +1,4 @@
import NimQml, logging, json import NimQml, strutils, logging, json
import backend/wallet_connect as backend import backend/wallet_connect as backend
@ -70,22 +70,38 @@ QtObject:
proc respondSessionRequest*(self: Controller, sessionRequestJson: string, signedJson: string, error: bool) {.signal.} proc respondSessionRequest*(self: Controller, sessionRequestJson: string, signedJson: string, error: bool) {.signal.}
proc sendTransaction(self: Controller, signature: string) = proc sendTransactionAndRespond(self: Controller, signature: string) =
let finalSignature = singletonInstance.utils.removeHexPrefix(signature) let finalSignature = singletonInstance.utils.removeHexPrefix(signature)
var res: JsonNode var res: JsonNode
let err = backend.sendTransaction(res, finalSignature) let err = backend.sendTransactionWithSignature(res, finalSignature)
if err.len > 0: if err.len > 0:
error "Failed to send tx" error "Failed to send tx"
return return
let sessionResponseDto = res.toSessionResponseDto() let txHash = res.getStr
self.respondSessionRequest($self.sessionRequestJson, sessionResponseDto.signedMessage, false) self.respondSessionRequest($self.sessionRequestJson, txHash, false)
proc buildRawTransactionAndRespond(self: Controller, signature: string) =
let finalSignature = singletonInstance.utils.removeHexPrefix(signature)
var res: JsonNode
let err = backend.buildRawTransaction(res, finalSignature)
if err.len > 0:
error "Failed to send tx"
return
let txHash = res.getStr
self.respondSessionRequest($self.sessionRequestJson, txHash, false)
proc finishSessionRequest(self: Controller, signature: string) = proc finishSessionRequest(self: Controller, signature: string) =
let requestMethod = getRequestMethod(self.sessionRequestJson) let requestMethod = getRequestMethod(self.sessionRequestJson)
if requestMethod == RequestMethod.SendTransaction: if requestMethod == RequestMethod.SendTransaction:
self.sendTransaction(signature) self.sendTransactionAndRespond(signature)
elif requestMethod == RequestMethod.SignTransaction:
self.buildRawTransactionAndRespond(signature)
elif requestMethod == RequestMethod.PersonalSign: elif requestMethod == RequestMethod.PersonalSign:
self.respondSessionRequest($self.sessionRequestJson, signature, false) self.respondSessionRequest($self.sessionRequestJson, signature, false)
elif requestMethod == RequestMethod.EthSign:
self.respondSessionRequest($self.sessionRequestJson, signature, false)
elif requestMethod == RequestMethod.SignTypedData:
self.respondSessionRequest($self.sessionRequestJson, signature, false)
else: else:
error "Unknown request method" error "Unknown request method"

View File

@ -6,7 +6,10 @@ type
RequestMethod* {.pure.} = enum RequestMethod* {.pure.} = enum
Unknown = "unknown" Unknown = "unknown"
SendTransaction = "eth_sendTransaction" SendTransaction = "eth_sendTransaction"
SignTransaction = "eth_signTransaction"
PersonalSign = "personal_sign" PersonalSign = "personal_sign"
EthSign = "eth_sign"
SignTypedData = "eth_signTypedData"
## provided json represents a `SessionRequest` ## provided json represents a `SessionRequest`
proc getRequestMethod*(jsonObj: JsonNode): RequestMethod = proc getRequestMethod*(jsonObj: JsonNode): RequestMethod =

View File

@ -16,7 +16,13 @@ rpc(wCSignMessage, "wallet"):
address: string address: string
password: string password: string
rpc(wCSendTransaction, "wallet"): rpc(wCBuildRawTransaction, "wallet"):
signature: string
rpc(wCSendRawTransaction, "wallet"):
rawTx: string
rpc(wCSendTransactionWithSignature, "wallet"):
signature: string signature: string
rpc(wCPairSessionProposal, "wallet"): rpc(wCPairSessionProposal, "wallet"):
@ -40,9 +46,25 @@ proc signMessage*(res: var JsonNode, message: string, address: string, password:
warn e.msg warn e.msg
return e.msg return e.msg
proc sendTransaction*(res: var JsonNode, signature: string): string = proc buildRawTransaction*(res: var JsonNode, signature: string): string =
try: try:
let response = wCSendTransaction(signature) let response = wCBuildRawTransaction(signature)
return prepareResponse(res, response)
except Exception as e:
warn e.msg
return e.msg
proc sendRawTransaction*(res: var JsonNode, rawTx: string): string =
try:
let response = wCSendRawTransaction(rawTx)
return prepareResponse(res, response)
except Exception as e:
warn e.msg
return e.msg
proc sendTransactionWithSignature*(res: var JsonNode, signature: string): string =
try:
let response = wCSendTransactionWithSignature(signature)
return prepareResponse(res, response) return prepareResponse(res, response)
except Exception as e: except Exception as e:
warn e.msg warn e.msg

View File

@ -243,6 +243,18 @@ Item {
WebChannel.id: "statusObject" WebChannel.id: "statusObject"
function bubbleConsoleMessage(type, message) {
if (type === "warn") {
console.warn(message)
} else if (type === "debug") {
console.debug(message)
} else if (type === "error") {
console.error(message)
} else {
console.log(message)
}
}
function sdkInitialized(error) function sdkInitialized(error)
{ {
d.sdkReady = !error d.sdkReady = !error

File diff suppressed because one or more lines are too long

View File

@ -180,10 +180,21 @@ window.wc = {
respondSessionRequest: function (topic, id, signature) { respondSessionRequest: function (topic, id, signature) {
const response = formatJsonRpcResult(id, signature) const response = formatJsonRpcResult(id, signature)
try {
let r = window.wc.web3wallet.respondSessionRequest({ topic: topic, response: response });
return { return {
result: window.wc.web3wallet.respondSessionRequest({ topic, response }), result: r,
error: "" error: ""
}; };
} catch (e) {
wc.statusObject.bubbleConsoleMessage("error", `respondSessionRequest error: ${JSON.stringify(e, null, 2)}`)
return {
result: "",
error: e
};
}
}, },
rejectSessionRequest: function (topic, id, error = false) { rejectSessionRequest: function (topic, id, error = false) {