-
Notifications
You must be signed in to change notification settings - Fork 735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: tvf #5630
Merged
+534
−14
Merged
feat: tvf #5630
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7de3328
feat: sets TVF params to relay messages
ganchoradkov e8300b5
feat: tests
ganchoradkov 7449929
refactor: updates `correlationId` type to number
ganchoradkov 45a44f7
refactor: adds tvf for solana methods & improves handling
ganchoradkov b164e49
feat: tests
ganchoradkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ | |
getSearchParamFromURL, | ||
isReactNative, | ||
isTestRun, | ||
isValidArray, | ||
} from "@walletconnect/utils"; | ||
import EventEmmiter from "events"; | ||
import { | ||
|
@@ -109,6 +110,7 @@ | |
WALLETCONNECT_DEEPLINK_CHOICE, | ||
ENGINE_QUEUE_STATES, | ||
AUTH_PUBLIC_KEY_NAME, | ||
TVF_METHODS, | ||
} from "../constants"; | ||
|
||
export class Engine extends IEngine { | ||
|
@@ -557,14 +559,14 @@ | |
else resolve(result); | ||
}, | ||
); | ||
|
||
const protocolMethod = "wc_sessionRequest"; | ||
const appLink = this.getAppLinkIfEnabled(session.peer.metadata, session.transportType); | ||
if (appLink) { | ||
await this.sendRequest({ | ||
clientRpcId, | ||
relayRpcId, | ||
topic, | ||
method: "wc_sessionRequest", | ||
method: protocolMethod, | ||
params: { | ||
request: { | ||
...request, | ||
|
@@ -587,22 +589,28 @@ | |
return result; | ||
} | ||
|
||
const protocolRequestParams: JsonRpcTypes.RequestParams["wc_sessionRequest"] = { | ||
request: { | ||
...request, | ||
expiryTimestamp: calcExpiry(expiry), | ||
}, | ||
chainId, | ||
}; | ||
const shouldSetTVF = this.shouldSetTVF(protocolMethod, protocolRequestParams); | ||
|
||
return await Promise.all([ | ||
new Promise<void>(async (resolve) => { | ||
await this.sendRequest({ | ||
clientRpcId, | ||
relayRpcId, | ||
topic, | ||
method: "wc_sessionRequest", | ||
params: { | ||
request: { | ||
...request, | ||
expiryTimestamp: calcExpiry(expiry), | ||
}, | ||
chainId, | ||
}, | ||
method: protocolMethod, | ||
params: protocolRequestParams, | ||
expiry, | ||
throwOnFailedPublish: true, | ||
...(shouldSetTVF && { | ||
tvf: this.getTVFParams(clientRpcId, protocolRequestParams), | ||
}), | ||
}).catch((error) => reject(error)); | ||
this.client.events.emit("session_request_sent", { | ||
topic, | ||
|
@@ -1449,6 +1457,7 @@ | |
clientRpcId, | ||
throwOnFailedPublish, | ||
appLink, | ||
tvf, | ||
} = args; | ||
const payload = formatJsonRpcRequest(method, params, clientRpcId); | ||
|
||
|
@@ -1483,6 +1492,12 @@ | |
const opts = ENGINE_RPC_OPTS[method].req; | ||
if (expiry) opts.ttl = expiry; | ||
if (relayRpcId) opts.id = relayRpcId; | ||
|
||
opts.tvf = { | ||
...tvf, | ||
correlationId: payload.id, | ||
}; | ||
|
||
if (throwOnFailedPublish) { | ||
opts.internal = { | ||
...opts.internal, | ||
|
@@ -1518,8 +1533,17 @@ | |
throw error; | ||
} | ||
let record; | ||
let tvf; | ||
try { | ||
record = await this.client.core.history.get(topic, id); | ||
const request = record.request; | ||
try { | ||
if (this.shouldSetTVF(request.method as JsonRpcTypes.WcMethod, request.params)) { | ||
tvf = this.getTVFParams(id, request.params, result); | ||
} | ||
} catch (error) { | ||
this.client.logger.warn(`sendResult() -> getTVFParams() failed`, error); | ||
} | ||
} catch (error) { | ||
this.client.logger.error(`sendResult() -> history.get(${topic}, ${id}) failed`); | ||
throw error; | ||
|
@@ -1530,6 +1554,12 @@ | |
await (global as any).Linking.openURL(redirectURL, this.client.name); | ||
} else { | ||
const opts = ENGINE_RPC_OPTS[record.request.method].res; | ||
|
||
opts.tvf = { | ||
...tvf, | ||
correlationId: id, | ||
}; | ||
|
||
if (throwOnFailedPublish) { | ||
opts.internal = { | ||
...opts.internal, | ||
|
@@ -2084,7 +2114,7 @@ | |
}, 500); | ||
}; | ||
|
||
private onSessionDeleteRequest: EnginePrivate["onSessionDeleteRequest"] = async ( | ||
topic, | ||
payload, | ||
) => { | ||
|
@@ -2941,4 +2971,79 @@ | |
} | ||
} | ||
}; | ||
|
||
private shouldSetTVF = ( | ||
protocolMethod: JsonRpcTypes.WcMethod, | ||
params: JsonRpcTypes.RequestParams["wc_sessionRequest"], | ||
) => { | ||
if (!params) return false; | ||
if (protocolMethod !== "wc_sessionRequest") return false; | ||
const { request } = params; | ||
return Object.keys(TVF_METHODS).includes(request.method); | ||
}; | ||
|
||
private getTVFParams = ( | ||
id: number, | ||
params: JsonRpcTypes.RequestParams["wc_sessionRequest"], | ||
result?: any, | ||
) => { | ||
try { | ||
const requestMethod = params.request.method; | ||
const txHashes = this.extractTxHashesFromResult(requestMethod, result); | ||
const tvf: RelayerTypes.ITVF = { | ||
correlationId: id, | ||
ganchoradkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rpcMethods: [requestMethod], | ||
chainId: params.chainId, | ||
...(this.isValidContractData(params.request.params) && { | ||
// initially only get contractAddresses from EVM txs | ||
contractAddresses: [params.request.params?.[0]?.to], | ||
}), | ||
txHashes, | ||
}; | ||
return tvf; | ||
} catch (e) { | ||
this.client.logger.warn("Error getting TVF params", e); | ||
} | ||
return {}; | ||
Comment on lines
+3004
to
+3007
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious why you're adopting this pattern of returning outside of the catch ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems easier to read whats going on in the fx |
||
}; | ||
|
||
private isValidContractData = (params: any) => { | ||
if (!params) return false; | ||
try { | ||
const data = params?.data || params?.[0]?.data; | ||
|
||
if (!data.startsWith("0x")) return false; | ||
|
||
const hexPart = data.slice(2); | ||
if (!/^[0-9a-fA-F]*$/.test(hexPart)) return false; | ||
|
||
return hexPart.length % 2 === 0; | ||
} catch (e) {} | ||
return false; | ||
}; | ||
|
||
private extractTxHashesFromResult = (method: string, result: any): string[] => { | ||
try { | ||
const methodConfig = TVF_METHODS[method as keyof typeof TVF_METHODS]; | ||
// result = 0x... | ||
if (typeof result === "string") { | ||
return [result]; | ||
} | ||
|
||
// result = { key: [0x...] } or { key: 0x... } | ||
const hashes = result[methodConfig.key]; | ||
|
||
// result = { key: [0x...] } | ||
if (isValidArray(hashes)) { | ||
return hashes; | ||
|
||
// result = { key: 0x... } | ||
} else if (typeof hashes === "string") { | ||
return [hashes]; | ||
} | ||
} catch (e) { | ||
this.client.logger.warn("Error extracting tx hashes from result", e); | ||
} | ||
return []; | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we validate/parse the request to avoid having to cast the method to
JsonRpcTypes.WcMethod
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
request.method
type isstring
so i'm casting it to predefined set of protocol methods to be more intuitive of the values. I can try to refactor the typeJsonRpcRecord
that history returns but it might cause unexpected breaking changes in the types