Skip to content

Commit

Permalink
Misc update (#1079)
Browse files Browse the repository at this point in the history
* web3Methods

* childRequestEthereumAccounts

* save sdk version in localstorage
  • Loading branch information
fan-zhang-sv authored Nov 22, 2023
1 parent fd6b7fa commit 466c367
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { JSONRPCMethod } from '@coinbase/wallet-sdk/dist/provider/JSONRPC';

type FormattedParamsType = Record<string, unknown> | string;

type methodType = `${JSONRPCMethod}`;

export type RpcRequestInput = {
connected?: boolean;
method: methodType;
method: JSONRPCMethod;
params: Array<{ key: string; required?: boolean }>;
format?: (data: Record<string, string>) => FormattedParamsType[];
};
33 changes: 24 additions & 9 deletions apps/testapp/src/context/CBWSDKProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -27,27 +28,41 @@ const dynamicallyImportSDK = (version: SDKVersionType) => {
};

export function CBWSDKProvider({ children }: CBWSDKProviderProps) {
const [version, setVersion] = React.useState<SDKVersionType>('master');
const [version, setVersion] = React.useState<SDKVersionType | undefined>(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]
);
Expand Down
33 changes: 18 additions & 15 deletions packages/wallet-sdk/src/relay/walletlink/type/Web3Method.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
// 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];
3 changes: 3 additions & 0 deletions packages/wallet-sdk/src/relay/walletlink/type/Web3Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type _Web3Request =
appLogoUrl: string | null;
};
}
| {
method: 'childRequestEthereumAccounts';
}
| {
method: 'connectAndSignIn';
params: {
Expand Down

0 comments on commit 466c367

Please sign in to comment.