Skip to content

Commit

Permalink
Merge branch 'leapsamvel-feature/add-leap-social-login'
Browse files Browse the repository at this point in the history
  • Loading branch information
liujun93 committed Jan 23, 2024
2 parents f7d5955 + becac60 commit cee107f
Show file tree
Hide file tree
Showing 31 changed files with 6,206 additions and 2,898 deletions.
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
"pub": "lerna publish"
},
"devDependencies": {
"eslint": "8.23.1",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.2.1",
"jest": "29.0.3",
"lerna": "7.1.1",
"prettier": "2.7.1",
"@types/jest": "29.0.3",
"@types/node": "18.7.18",
"@typescript-eslint/eslint-plugin": "5.38.0",
"@typescript-eslint/parser": "5.38.0",
"eslint": "8.23.1",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-simple-import-sort": "8.0.0",
"eslint-plugin-unused-imports": "2.0.0",
"jest": "29.0.3",
"jest-in-case": "1.0.2",
"lerna": "7.1.1",
"prettier": "2.7.1",
"publish-scripts": "0.1.0",
"rimraf": "3.0.2",
"ts-jest": "29.0.1",
Expand All @@ -47,5 +47,8 @@
"repository": {
"type": "git",
"url": "https://github.com/cosmology-tech/cosmos-kit#readme"
},
"dependencies": {
"@leapwallet/cosmos-social-login-capsule-provider-ui": "^0.0.46"
}
}
66 changes: 37 additions & 29 deletions packages/core/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
WalletConnectOptions,
WalletName,
} from './types';
import { convertChain, Session } from './utils';
import { convertChain, Session, WalletNotProvidedError } from './utils';
import type { Logger } from './utils';

export class WalletManager extends StateBase {
Expand Down Expand Up @@ -129,7 +129,7 @@ export class WalletManager extends StateBase {
wallet.throwErrors = this.throwErrors;
wallet.session = this.session;
wallet.walletConnectOptions = this.walletConnectOptions;
wallet.setChains(this.chainRecords);
wallet?.setChains(this.chainRecords);
return wallet;
});

Expand Down Expand Up @@ -261,7 +261,7 @@ export class WalletManager extends StateBase {
const wallet = this.mainWallets.find((w) => w.walletName === walletName);

if (!wallet) {
throw new Error(`Wallet ${walletName} is not provided.`);
throw new WalletNotProvidedError(walletName);
}

return wallet;
Expand Down Expand Up @@ -363,35 +363,43 @@ export class WalletManager extends StateBase {
? IFRAME_WALLET_ID
: window.localStorage.getItem('cosmos-kit@2:core//current-wallet');
if (walletName) {
const mainWallet = this.getMainWallet(walletName);
mainWallet.activate();
try {
const mainWallet = this.getMainWallet(walletName);
mainWallet.activate();

if (mainWallet.clientMutable.state === State.Done) {
const accountsStr = window.localStorage.getItem(
'cosmos-kit@2:core//accounts'
);
if (accountsStr && accountsStr !== '[]') {
const accounts: SimpleAccount[] = JSON.parse(accountsStr);
accounts.forEach((data) => {
const chainWallet = mainWallet
.getChainWalletList(false)
.find(
(w) =>
w.chainRecord.chain?.chain_id === data.chainId &&
w.namespace === data.namespace
);
chainWallet?.activate();
if (mainWallet.walletInfo.mode === 'wallet-connect') {
chainWallet?.setData(data);
chainWallet?.setState(State.Done);
}
});
mainWallet.setState(State.Done);
if (mainWallet.clientMutable.state === State.Done) {
const accountsStr = window.localStorage.getItem(
'cosmos-kit@2:core//accounts'
);
if (accountsStr && accountsStr !== '[]') {
const accounts: SimpleAccount[] = JSON.parse(accountsStr);
accounts.forEach((data) => {
const chainWallet = mainWallet
.getChainWalletList(false)
.find(
(w) =>
w.chainRecord.chain?.chain_id === data.chainId &&
w.namespace === data.namespace
);
chainWallet?.activate();
if (mainWallet.walletInfo.mode === 'wallet-connect') {
chainWallet?.setData(data);
chainWallet?.setState(State.Done);
}
});
mainWallet.setState(State.Done);
}
}
}

if (mainWallet.walletInfo.mode !== 'wallet-connect') {
await this._reconnect(walletName);
if (mainWallet.walletInfo.mode !== 'wallet-connect') {
await this._reconnect(walletName);
}
} catch (error) {
if (error instanceof WalletNotProvidedError) {
this.logger?.warn(error.message);
} else {
throw error;
}
}
}
};
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ export const RejectedError = new Error('Request Rejected!');

// QRCode
export const ExpiredError = new Error('Expired!');

export class WalletNotProvidedError extends Error {
readonly walletName: string;

constructor(walletName: string) {
super(`Wallet ${walletName} is not provided.`);
this.walletName = walletName;
}
}
118 changes: 118 additions & 0 deletions packages/docs/pages/integrating-wallets/leap-capsule-social-login.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# @cosmos-kit/leap-capsule-social-login


`@cosmos-kit/leap-capsule-social-login` is the social login integration for CosmosKit using `@leapwallet/cosmos-social-login-capsule-provider`

> You need install [@leapwallet/cosmos-social-login-capsule-provider-ui](https://www.npmjs.com/package/@leapwallet/cosmos-social-login-capsule-provider-ui) package for UI.
> When you build it kindly have the environment vairables `CAPSULE_API_KEY` and `CAPSULE_ENV` you can get it from [UseCapsule.com](https://usecapsule.com/)
## NextJS

For nextjs we recommend to load the module dynamic or async as it is not yet supporting SSR.

> When you build it kindly have the environment vairables `NEXT_PUBLIC_CAPSULE_API_KEY` and `NEXT_PUBLIC_CAPSULE_ENV` you can get it from [UseCapsule.com](https://usecapsule.com/)
### In next.config.js

> transpilePackages: ["@cosmos-kit/leap-social-login", "@leapwallet/capsule-web-sdk-lite", "@leapwallet/cosmos-social-login-capsule-provider"],

### For example

```jsx

function MyApp({ Component, pageProps }: AppProps) {
const defaultWallets: MainWalletBase[] = [...keplrWallets, ...leapWallets];
const [wallets, setWallets] = useState<MainWalletBase[]>(defaultWallets)
const [loadingWallets, setLoadingWallet] = useState<boolean>(false);

useEffect(() => {
setLoadingWallet(true)
import("@cosmos-kit/leap-capsule-social-login").then(
(CapsuleModule) => {
return CapsuleModule.wallets;
},
).then((leapSocialLogin) => {
setWallets([...keplrWallets, ...leapWallets, ...leapSocialLogin])
setLoadingWallet(false);
})
}, [])


if (loadingWallets) {
return <>Loading...</>
}


return (
<RootLayout>
<ChainProvider
chains={chains}
assetLists={[...assets]}
wallets={wallets}
throwErrors={false}
subscribeConnectEvents={false}
defaultNameService={"stargaze"}
logLevel={"DEBUG"}
endpointOptions={{
isLazy: true,
endpoints: {
cosmoshub: {
rpc: [
{
url: "https://rpc.cosmos.directory/cosmoshub",
headers: {},
},
],
},
},
}}
disableIframe={false}
>
<Component {...pageProps} />
</ChainProvider>
<CustomCapsuleModalViewX />
</RootLayout>
);
}

export default MyApp;


const LeapSocialLogin = dynamic(
() =>
import("@leapwallet/cosmos-social-login-capsule-provider-ui").then(
(m) => m.CustomCapsuleModalView,
),
{ ssr: false },
);

export function CustomCapsuleModalViewX() {
const [showCapsuleModal, setShowCapsuleModal] = useState(false);

useEffect(() => {
window.openCapsuleModal = () => {
setShowCapsuleModal(true);
}
}, [])

return (
<>
<LeapSocialLogin
showCapsuleModal={showCapsuleModal}
setShowCapsuleModal={setShowCapsuleModal}
theme={'dark'}
onAfterLoginSuccessful={() => {
window.successFromCapsuleModal();
}}
onLoginFailure={
() => {
window.failureFromCapsuleModal();
}
}
/>
</>
);
}
```
3 changes: 2 additions & 1 deletion packages/example/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ const path = require("path");

/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: { esmExternals: true },
reactStrictMode: true,
transpilePackages: ["@cosmos-kit/web3auth"],
transpilePackages: ["@cosmos-kit/web3auth", "@cosmos-kit/leap-social-login", "@leapwallet/capsule-web-sdk-lite", "@leapwallet/cosmos-social-login-capsule-provider"],
/**
*
* @param {import('webpack').Configuration} config
Expand Down
4 changes: 3 additions & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@
"@cosmos-kit/ins": "^2.1.6",
"@cosmos-kit/keplr": "^2.6.6",
"@cosmos-kit/leap": "^2.6.6",
"@cosmos-kit/leap-metamask-cosmos-snap": "^0.5.6",
"@cosmos-kit/ledger": "^2.6.6",
"@cosmos-kit/react": "^2.10.6",
"@cosmos-kit/shell": "^2.6.6",
"@cosmos-kit/station": "^2.5.6",
"@cosmos-kit/vectis": "^2.6.6",
"@cosmos-kit/web3auth": "^2.5.6",
"@cosmos-kit/leap-capsule-social-login": "^0.3.10",
"@cosmos-kit/leap-metamask-cosmos-snap": "^0.5.5",
"@interchain-ui/react": "^1.16.6",
"@leapwallet/cosmos-social-login-capsule-provider-ui": "^0.0.47",
"@radix-ui/react-dropdown-menu": "^2.0.5",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-slot": "^1.0.2",
Expand Down
Loading

0 comments on commit cee107f

Please sign in to comment.