Skip to content
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

[W3W] Support test namespaces #895

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ import Web3Wallet

final class SessionProposalInteractor {
func approve(proposal: Session.Proposal) async throws {
// Following properties are used to support all the required namespaces for the testing purposes
let supportedMethods = Array(proposal.requiredNamespaces.map { $0.value.methods }.first ?? [])
let supportedEvents = Array(proposal.requiredNamespaces.map { $0.value.events }.first ?? [])
let supportedChains = Array((proposal.requiredNamespaces.map { $0.value.chains }.first ?? [] )!)
let supportedAccounts = supportedChains.map { Account(blockchain: $0, address: ETHSigner.address)! }
// Following properties are used to support all the required and optional namespaces for the testing purposes
let supportedMethods = Set(proposal.requiredNamespaces.flatMap { $0.value.methods } + (proposal.optionalNamespaces?.flatMap { $0.value.methods } ?? []))
let supportedEvents = Set(proposal.requiredNamespaces.flatMap { $0.value.events } + (proposal.optionalNamespaces?.flatMap { $0.value.events } ?? []))

let supportedRequiredChains = proposal.requiredNamespaces["eip155"]?.chains
let supportedOptionalChains = proposal.optionalNamespaces?["eip155"]?.chains ?? []
let supportedChains = supportedRequiredChains?.union(supportedOptionalChains) ?? []

let supportedAccounts = Array(supportedChains).map { Account(blockchain: $0, address: ETHSigner.address)! }

/* Use only supported values for production. I.e:
let supportedMethods = ["eth_signTransaction", "personal_sign", "eth_signTypedData", "eth_sendTransaction", "eth_sign"]
let supportedEvents = ["accountsChanged", "chainChanged"]
let supportedChains = [Blockchain("eip155:1")!, Blockchain("eip155:5")!]
let supportedAccounts = [Account(blockchain: Blockchain("eip155:5")!, address: ETHSigner.address)!]
let supportedChains = [Blockchain("eip155:1")!, Blockchain("eip155:137")!]
let supportedAccounts = [Account(blockchain: Blockchain("eip155:1")!, address: ETHSigner.address)!, Account(blockchain: Blockchain("eip155:137")!, address: ETHSigner.address)!]
*/
do {
let sessionNamespaces = try AutoNamespaces.build(
sessionProposal: proposal,
chains: supportedChains,
methods: supportedMethods,
events: supportedEvents,
chains: Array(supportedChains),
methods: Array(supportedMethods),
events: Array(supportedEvents),
accounts: supportedAccounts
)
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces)
Expand Down