From bdecf43e18098f9332484dcc4957e482ef5cf347 Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Mon, 25 Mar 2024 12:58:55 +0200 Subject: [PATCH] refactor: reverts replacing chains on update. --- examples/wallet/package.json | 2 +- examples/wallet/yarn.lock | 95 ++++++++++++++++++++++++++++-------- src/controllers/engine.ts | 36 ++++++++------ test/shared/values.ts | 2 +- test/sign.spec.ts | 12 ++++- 5 files changed, 107 insertions(+), 40 deletions(-) diff --git a/examples/wallet/package.json b/examples/wallet/package.json index ca54f28..ea86e88 100644 --- a/examples/wallet/package.json +++ b/examples/wallet/package.json @@ -14,7 +14,7 @@ "@json-rpc-tools/utils": "1.7.6", "@nextui-org/react": "1.0.0-beta.12", "@walletconnect/core": "2.11.2", - "@walletconnect/se-sdk": "1.7.0-canary-82cf156-1", + "@walletconnect/se-sdk": "1.7.1-canary.1", "@walletconnect/utils": "2.11.2", "ethers": "5.7.2", "framer-motion": "9.0.2", diff --git a/examples/wallet/yarn.lock b/examples/wallet/yarn.lock index f708480..1370b0e 100644 --- a/examples/wallet/yarn.lock +++ b/examples/wallet/yarn.lock @@ -1921,6 +1921,29 @@ lodash.isequal "4.5.0" uint8arrays "^3.1.0" +"@walletconnect/core@2.11.3": + version "2.11.3" + resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.11.3.tgz#c81855722cb9afd411f91f5345c7874f48bade0b" + integrity sha512-/9m4EqiggFUwkQDv5PDWbcTI+yCVnBd/iYW5iIHEkivg2/mnBr2bQz2r/vtPjp19r/ZK62Dx0+UN3U+BWP8ulQ== + dependencies: + "@walletconnect/heartbeat" "1.2.1" + "@walletconnect/jsonrpc-provider" "1.0.13" + "@walletconnect/jsonrpc-types" "1.0.3" + "@walletconnect/jsonrpc-utils" "1.0.8" + "@walletconnect/jsonrpc-ws-connection" "1.0.14" + "@walletconnect/keyvaluestorage" "^1.1.1" + "@walletconnect/logger" "^2.0.1" + "@walletconnect/relay-api" "^1.0.9" + "@walletconnect/relay-auth" "^1.0.4" + "@walletconnect/safe-json" "^1.0.2" + "@walletconnect/time" "^1.0.2" + "@walletconnect/types" "2.11.3" + "@walletconnect/utils" "2.11.3" + events "^3.3.0" + isomorphic-unfetch "3.1.0" + lodash.isequal "4.5.0" + uint8arrays "^3.1.0" + "@walletconnect/core@^2.10.1": version "2.10.1" resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.10.1.tgz#d1fb442bd77424666bacdb0f5a07f7708fb3d984" @@ -2066,26 +2089,26 @@ dependencies: tslib "1.14.1" -"@walletconnect/se-sdk@1.7.0-canary-82cf156-1": - version "1.7.0-canary-82cf156-1" - resolved "https://registry.yarnpkg.com/@walletconnect/se-sdk/-/se-sdk-1.7.0-canary-82cf156-1.tgz#e8b52b29d1f32a55e9d21bf13ff6441cc9b771ea" - integrity sha512-3DMq5hUdIh68u9KeUEenDwkTDm4Kif2B2qscNlqqo0bp/6bM0tSlwM6XwtOgty3VOysUHhhSE4G+lJafs1TagA== +"@walletconnect/se-sdk@1.7.1-canary.1": + version "1.7.1-canary.1" + resolved "https://registry.yarnpkg.com/@walletconnect/se-sdk/-/se-sdk-1.7.1-canary.1.tgz#9b50fd5c36bc953e5de5bbf249b15112475791f3" + integrity sha512-FZx1a66VW7U07iUq0SwJH/dyEnX88GQIUHl1ceN5wSfL2HDAvJEjqHVWm4cLvozTLSA3Fd2ZU9iG3q7FQSmOwA== dependencies: - "@walletconnect/web3wallet" "1.10.2" + "@walletconnect/web3wallet" "1.10.3" -"@walletconnect/sign-client@2.11.2": - version "2.11.2" - resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.11.2.tgz#855609653855f0d23b0502cdbdcf43402e34c459" - integrity sha512-MfBcuSz2GmMH+P7MrCP46mVE5qhP0ZyWA0FyIH6/WuxQ6G+MgKsGfaITqakpRPsykWOJq8tXMs3XvUPDU413OQ== +"@walletconnect/sign-client@2.11.3": + version "2.11.3" + resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.11.3.tgz#3ea7b3acf92ee31cc42b45d42e66c44b4720b28b" + integrity sha512-JVjLTxN/3NjMXv5zalSGKuSYLRyU2yX6AWEdq17cInlrwODpbWZr6PS1uxMWdH4r90DXBLhdtwDbEq/pfd0BPg== dependencies: - "@walletconnect/core" "2.11.2" + "@walletconnect/core" "2.11.3" "@walletconnect/events" "^1.0.1" "@walletconnect/heartbeat" "1.2.1" "@walletconnect/jsonrpc-utils" "1.0.8" "@walletconnect/logger" "^2.0.1" "@walletconnect/time" "^1.0.2" - "@walletconnect/types" "2.11.2" - "@walletconnect/utils" "2.11.2" + "@walletconnect/types" "2.11.3" + "@walletconnect/utils" "2.11.3" events "^3.3.0" "@walletconnect/time@^1.0.2": @@ -2119,6 +2142,18 @@ "@walletconnect/logger" "^2.0.1" events "^3.3.0" +"@walletconnect/types@2.11.3": + version "2.11.3" + resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.11.3.tgz#8ce43cb77e8fd9d5269847cdd73bcfa7cce7dd1a" + integrity sha512-JY4wA9MVosDW9dcJMTpnwliste0aJGJ1X6Q4ulLsQsgWRSEBRkLila0oUT01TDBW9Yq8uUp7uFOUTaKx6KWVAg== + dependencies: + "@walletconnect/events" "^1.0.1" + "@walletconnect/heartbeat" "1.2.1" + "@walletconnect/jsonrpc-types" "1.0.3" + "@walletconnect/keyvaluestorage" "^1.1.1" + "@walletconnect/logger" "^2.0.1" + events "^3.3.0" + "@walletconnect/utils@2.10.1", "@walletconnect/utils@^2.10.1": version "2.10.1" resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.10.1.tgz#65b37c9800eb0e80a08385b6987471fb46e1e22e" @@ -2159,19 +2194,39 @@ query-string "7.1.3" uint8arrays "^3.1.0" -"@walletconnect/web3wallet@1.10.2": - version "1.10.2" - resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.10.2.tgz#5887642773e6e1b88d1bfb159a00ec45b31f762a" - integrity sha512-FbWsJwhihppl6poJ0+0WCkjXZDVdb11KJiS/AJt+qyNheIKQ7z6NfCnCnnKPBEpNNoUPclcFo4b/BmdFo2YlMw== +"@walletconnect/utils@2.11.3": + version "2.11.3" + resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.11.3.tgz#3731809b54902655cf202e0bf0e8f268780e8b54" + integrity sha512-jsdNkrl/IcTkzWFn0S2d0urzBXg6RxVJtUYRsUx3qI3wzOGiABP9ui3yiZ3SgZOv9aRe62PaNp1qpbYZ+zPb8Q== + dependencies: + "@stablelib/chacha20poly1305" "1.0.1" + "@stablelib/hkdf" "1.0.1" + "@stablelib/random" "^1.0.2" + "@stablelib/sha256" "1.0.1" + "@stablelib/x25519" "^1.0.3" + "@walletconnect/relay-api" "^1.0.9" + "@walletconnect/safe-json" "^1.0.2" + "@walletconnect/time" "^1.0.2" + "@walletconnect/types" "2.11.3" + "@walletconnect/window-getters" "^1.0.1" + "@walletconnect/window-metadata" "^1.0.1" + detect-browser "5.3.0" + query-string "7.1.3" + uint8arrays "^3.1.0" + +"@walletconnect/web3wallet@1.10.3": + version "1.10.3" + resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.10.3.tgz#8195308757bd298ccc9caa6e3fe9f4ff82b94607" + integrity sha512-1Dr2P8KIDCqEWZ+s4coKGJz/+pj87ogFs+icPDXPu9QpzTgY5Y1WSzuAHaqoY5gTlL7WS58YP49s0E7iacUz4g== dependencies: "@walletconnect/auth-client" "2.1.2" - "@walletconnect/core" "2.11.2" + "@walletconnect/core" "2.11.3" "@walletconnect/jsonrpc-provider" "1.0.13" "@walletconnect/jsonrpc-utils" "1.0.8" "@walletconnect/logger" "2.0.1" - "@walletconnect/sign-client" "2.11.2" - "@walletconnect/types" "2.11.2" - "@walletconnect/utils" "2.11.2" + "@walletconnect/sign-client" "2.11.3" + "@walletconnect/types" "2.11.3" + "@walletconnect/utils" "2.11.3" "@walletconnect/window-getters@^1.0.1": version "1.0.1" diff --git a/src/controllers/engine.ts b/src/controllers/engine.ts index 210c526..032e50f 100644 --- a/src/controllers/engine.ts +++ b/src/controllers/engine.ts @@ -55,7 +55,12 @@ export class Engine extends ISingleEthereumEngine { const parsed = parseChain(chain); return parseInt(parsed); }); - const approvedChains = [...new Set([chainId, ...requiredChains])]; + const optionalChains = (normalizedOptional[EVM_IDENTIFIER]?.chains || []).map((chain) => { + const parsed = parseChain(chain); + return parseInt(parsed); + }); + + const approvedChains = [...new Set([chainId, ...requiredChains, ...optionalChains])]; const approveParams = { id, namespaces: { @@ -111,16 +116,12 @@ export class Engine extends ISingleEthereumEngine { const namespaces = session.namespaces[EVM_IDENTIFIER]; let shouldUpdateSession = false; if (!chainAlreadyInSession(session, chainId)) { - const requiredChains = session.requiredNamespaces?.[EVM_IDENTIFIER]?.chains || []; - namespaces.chains = [formattedChain, ...requiredChains]; + namespaces?.chains?.push(formattedChain); shouldUpdateSession = true; } if (!accountsAlreadyInSession(session, formattedAccounts)) { - namespaces.accounts = - namespaces.chains - ?.map((chain) => formatAccounts(accounts, parseInt(parseChain(chain)))) - .flat() || []; + namespaces.accounts = namespaces.accounts.concat(formattedAccounts); shouldUpdateSession = true; } @@ -149,15 +150,18 @@ export class Engine extends ISingleEthereumEngine { await this.changeChain(topic, chainId); this.chainId = chainId; } - - await this.web3wallet.emitSessionEvent({ - topic, - event: { - name: "accountsChanged", - data: formattedAccounts, - }, - chainId: formattedChain, - }); + try { + await this.web3wallet.emitSessionEvent({ + topic, + event: { + name: "accountsChanged", + data: formattedAccounts, + }, + chainId: formattedChain, + }); + } catch (e) { + this.client.logger.warn(e); + } }; public approveRequest: ISingleEthereumEngine["approveRequest"] = async (params) => { diff --git a/test/shared/values.ts b/test/shared/values.ts index 8113892..4408ac5 100644 --- a/test/shared/values.ts +++ b/test/shared/values.ts @@ -34,7 +34,7 @@ export const TEST_NAMESPACES = { export const TEST_UPDATED_NAMESPACES = { eip155: { methods: TEST_METHODS.concat(TEST_OPTIONAL_METHODS), - accounts: [TEST_GOERLI_ACCOUNT, TEST_ETHEREUM_ACCOUNT], + accounts: [TEST_ETHEREUM_ACCOUNT, TEST_GOERLI_ACCOUNT], events: TEST_EVENTS, }, }; diff --git a/test/sign.spec.ts b/test/sign.spec.ts index 132803c..048f080 100644 --- a/test/sign.spec.ts +++ b/test/sign.spec.ts @@ -188,7 +188,15 @@ describe("Sign Integration", () => { new Promise((resolve) => { dapp.events.on("session_update", (session) => { const { params } = session; - expect(params.namespaces).to.toMatchObject(TEST_UPDATED_NAMESPACES); + expect(params.namespaces.eip155.accounts).to.toMatchObject( + TEST_UPDATED_NAMESPACES.eip155.accounts, + ); + expect(params.namespaces.eip155.methods).to.toMatchObject( + TEST_UPDATED_NAMESPACES.eip155.methods, + ); + expect(params.namespaces.eip155.events).to.toMatchObject( + TEST_UPDATED_NAMESPACES.eip155.events, + ); resolve(session); }); }), @@ -261,7 +269,7 @@ describe("Sign Integration", () => { ]); }); - it("should replace approved chainId on update", async () => { + it.skip("should replace approved chainId on update", async () => { dapp = await SignClient.init({ ...TEST_CORE_OPTIONS, name: "Dapp",