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

Misc update #1079

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
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 @@ -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);
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save selected sdkversion in localstorage, so sdk remians the same version on each refresh

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can derive union type from array, but not other way around. doing this so wallet can check is a string is valid web3method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it


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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another type that is used between WL extension and RN, adding it here as the new WalletLinkEventData tightened the types for walletlinkConnection

}
| {
method: 'connectAndSignIn';
params: {
Expand Down
Loading