From 466c367e58bd806455eeefc0c11fc43a4d83c3f5 Mon Sep 17 00:00:00 2001 From: Felix Zhang <22125939+fan-zhang-sv@users.noreply.github.com> Date: Tue, 21 Nov 2023 16:36:39 -0800 Subject: [PATCH] Misc update (#1079) * web3Methods * childRequestEthereumAccounts * save sdk version in localstorage --- .../RpcMethods/method/RpcRequestInput.ts | 4 +-- apps/testapp/src/context/CBWSDKProvider.tsx | 33 ++++++++++++++----- .../src/relay/walletlink/type/Web3Method.ts | 33 ++++++++++--------- .../src/relay/walletlink/type/Web3Request.ts | 3 ++ 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/apps/testapp/src/components/RpcMethods/method/RpcRequestInput.ts b/apps/testapp/src/components/RpcMethods/method/RpcRequestInput.ts index 8f8d76c0d3..a34ee55492 100644 --- a/apps/testapp/src/components/RpcMethods/method/RpcRequestInput.ts +++ b/apps/testapp/src/components/RpcMethods/method/RpcRequestInput.ts @@ -2,11 +2,9 @@ import { JSONRPCMethod } from '@coinbase/wallet-sdk/dist/provider/JSONRPC'; type FormattedParamsType = Record | string; -type methodType = `${JSONRPCMethod}`; - export type RpcRequestInput = { connected?: boolean; - method: methodType; + method: JSONRPCMethod; params: Array<{ key: string; required?: boolean }>; format?: (data: Record) => FormattedParamsType[]; }; diff --git a/apps/testapp/src/context/CBWSDKProvider.tsx b/apps/testapp/src/context/CBWSDKProvider.tsx index 998174e58a..484963da20 100644 --- a/apps/testapp/src/context/CBWSDKProvider.tsx +++ b/apps/testapp/src/context/CBWSDKProvider.tsx @@ -8,6 +8,7 @@ type CBWSDKProviderProps = { }; const CBWSDKContext = React.createContext(null); +const SELECTED_SDK_KEY = 'selected_sdk_version'; export const sdkVersions = ['master', '3.9', '3.7'] as const; export type SDKVersionType = (typeof sdkVersions)[number]; @@ -27,27 +28,41 @@ const dynamicallyImportSDK = (version: SDKVersionType) => { }; export function CBWSDKProvider({ children }: CBWSDKProviderProps) { - const [version, setVersion] = React.useState('master'); + const [version, setVersion] = React.useState(undefined); const [sdk, setSdk] = React.useState(null); const [provider, setProvider] = React.useState(null); + useEffect(() => { + if (version === undefined) { + const savedVersion = localStorage.getItem(SELECTED_SDK_KEY) as SDKVersionType; + setVersion(sdkVersions.includes(savedVersion) ? (savedVersion as SDKVersionType) : 'master'); + } + }, [version]); + useEffect(() => { const selectedSDK = dynamicallyImportSDK(version); - const cbwsdk = new selectedSDK({ - appName: 'Test App', - enableMobileWalletLink: true, // beta feature - }); - setSdk(cbwsdk); - const cbwprovider = cbwsdk.makeWeb3Provider('http'); - setProvider(cbwprovider); + if (selectedSDK) { + const cbwsdk = new selectedSDK({ + appName: 'Test App', + enableMobileWalletLink: true, // beta feature + }); + setSdk(cbwsdk); + const cbwprovider = cbwsdk.makeWeb3Provider('http'); + setProvider(cbwprovider); + } }, [version]); + const setSDKVersion = (version: SDKVersionType) => { + localStorage.setItem(SELECTED_SDK_KEY, version); + setVersion(version); + }; + const ctx = useMemo( () => ({ sdk, provider, sdkVersion: version, - setSDKVersion: setVersion, + setSDKVersion, }), [sdk, provider] ); diff --git a/packages/wallet-sdk/src/relay/walletlink/type/Web3Method.ts b/packages/wallet-sdk/src/relay/walletlink/type/Web3Method.ts index cdfbad8102..08d1500e13 100644 --- a/packages/wallet-sdk/src/relay/walletlink/type/Web3Method.ts +++ b/packages/wallet-sdk/src/relay/walletlink/type/Web3Method.ts @@ -1,18 +1,21 @@ // Copyright (c) 2018-2023 Coinbase, Inc. // Licensed under the Apache License, version 2.0 -export type Web3Method = - | 'requestEthereumAccounts' - | 'signEthereumMessage' - | 'signEthereumTransaction' - | 'submitEthereumTransaction' - | 'ethereumAddressFromSignedMessage' - | 'scanQRCode' - | 'generic' - | 'childRequestEthereumAccounts' - | 'addEthereumChain' - | 'switchEthereumChain' - | 'makeEthereumJSONRPCRequest' - | 'watchAsset' - | 'selectProvider' - | 'connectAndSignIn'; +export const web3Methods = [ + 'requestEthereumAccounts', + 'signEthereumMessage', + 'signEthereumTransaction', + 'submitEthereumTransaction', + 'ethereumAddressFromSignedMessage', + 'scanQRCode', + 'generic', + 'childRequestEthereumAccounts', + 'addEthereumChain', + 'switchEthereumChain', + 'makeEthereumJSONRPCRequest', + 'watchAsset', + 'selectProvider', + 'connectAndSignIn', +] as const; + +export type Web3Method = (typeof web3Methods)[number]; diff --git a/packages/wallet-sdk/src/relay/walletlink/type/Web3Request.ts b/packages/wallet-sdk/src/relay/walletlink/type/Web3Request.ts index 5e0ea74e09..c862b5b507 100644 --- a/packages/wallet-sdk/src/relay/walletlink/type/Web3Request.ts +++ b/packages/wallet-sdk/src/relay/walletlink/type/Web3Request.ts @@ -23,6 +23,9 @@ type _Web3Request = appLogoUrl: string | null; }; } + | { + method: 'childRequestEthereumAccounts'; + } | { method: 'connectAndSignIn'; params: {