Skip to content

Commit

Permalink
support newest cosmiframe with regex origins and chain ID signer over…
Browse files Browse the repository at this point in the history
…rides
  • Loading branch information
NoahSaso committed Jan 7, 2025
1 parent 5e3afcb commit dd62fc5
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@cosmjs/cosmwasm-stargate": "^0.32.3",
"@cosmjs/proto-signing": "^0.32.3",
"@cosmjs/stargate": "^0.32.3",
"@dao-dao/cosmiframe": "^0.1.0",
"@dao-dao/cosmiframe": "^1.0.0-rc.1",
"@walletconnect/types": "2.11.0",
"bowser": "2.11.0",
"cosmjs-types": "^0.9.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/cosmiframe/cosmiframe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Origin } from '@dao-dao/cosmiframe';

import { cosmiframeExtensionInfo, CosmiframeWallet } from './extension';

export const makeCosmiframeWallet = (allowedParentOrigins: string[]) =>
export const makeCosmiframeWallet = (allowedParentOrigins: Origin[]) =>
new CosmiframeWallet({
...cosmiframeExtensionInfo,
allowedParentOrigins,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/cosmiframe/extension/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Origin } from '@dao-dao/cosmiframe';

import { Wallet } from '../../types';

export type CosmiframeWalletInfo = Wallet & {
allowedParentOrigins: string[];
allowedParentOrigins: Origin[];
};
14 changes: 10 additions & 4 deletions packages/core/src/manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { AssetList, Chain } from '@chain-registry/types';
import { isInIframe } from '@dao-dao/cosmiframe';
import { isInIframe, Origin } from '@dao-dao/cosmiframe';
import Bowser from 'bowser';
import EventEmitter from 'events';

Expand Down Expand Up @@ -52,7 +52,13 @@ export class WalletManager extends StateBase {
logger: Logger,
throwErrors: boolean | 'connect_only',
subscribeConnectEvents = true,
allowedCosmiframeParentOrigins?: string[],
allowedCosmiframeParentOrigins: Origin[] = [
/^https?:\/\/localhost(:\d+)?/,
/^https:\/\/(.+\.)?osmosis\.zone/,
/^https:\/\/(.+\.)?daodao\.zone/,
/^https:\/\/.+-da0da0\.vercel\.app/,
/^https:\/\/(.+\.)?abstract\.money/,
],
assetLists?: AssetList[],
defaultNameService?: NameServiceName,
walletConnectOptions?: WalletConnectOptions,
Expand Down Expand Up @@ -87,8 +93,8 @@ export class WalletManager extends StateBase {
];
wallets.forEach(
({ walletName }) =>
(this._reconnectMap[walletName] = () =>
this._reconnect(walletName, true))
(this._reconnectMap[walletName] = () =>
this._reconnect(walletName, true))
);
this.init(
chains,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-lite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"dependencies": {
"@chain-registry/types": "^0.46.11",
"@cosmos-kit/core": "^2.15.0",
"@dao-dao/cosmiframe": "^0.1.0"
"@dao-dao/cosmiframe": "^1.0.0-rc.1"
},
"resolutions": {
"@types/react": "^18.2"
Expand Down
29 changes: 20 additions & 9 deletions packages/react-lite/src/hooks/useIframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@cosmos-kit/core';
import {
Cosmiframe,
Origin,
OverrideHandler,
ParentMetadata,
} from '@dao-dao/cosmiframe';
Expand All @@ -20,6 +21,12 @@ export type FunctionKeys<T> = {
[K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? K : never;
}[keyof T];

export type UseIframeSignerOverrides = Partial<{
[K in keyof (OfflineAminoSigner & OfflineDirectSigner)]: (
...params: Parameters<(OfflineAminoSigner & OfflineDirectSigner)[K]>
) => OverrideHandler | Promise<OverrideHandler>;
}>;

export type UseIframeOptions = {
/**
* Optionally attempt to use a specific wallet. Otherwise get the first active
Expand Down Expand Up @@ -47,16 +54,15 @@ export type UseIframeOptions = {
* should handle the function. By default, if nothing is returned, an error
* will be thrown with the message "Handled by outer wallet."
*/
signerOverrides?: Partial<{
[K in keyof (OfflineAminoSigner & OfflineDirectSigner)]: (
...params: Parameters<(OfflineAminoSigner & OfflineDirectSigner)[K]>
) => OverrideHandler | Promise<OverrideHandler>;
}>;
signerOverrides?:
| UseIframeSignerOverrides
| ((chainId: string) => UseIframeSignerOverrides)
| ((chainId: string) => Promise<UseIframeSignerOverrides>);
/**
* Optionally only respond to requests from iframes of specific origin. If
* undefined or empty, all origins are allowed.
*/
origins?: string[];
origins?: Origin[];
};

export type UseIframeReturn = {
Expand Down Expand Up @@ -157,7 +163,7 @@ export const useIframe = ({
(() => Promise.reject(COSMIFRAME_NOT_CONNECTED_MESSAGE)),
nonSignerOverrides: () => ({
...walletClientOverridesRef.current,
// Override connect to return wallet info.
// Override connect to return specific error.
connect: async (...params) => {
if (walletClientOverridesRef.current?.connect) {
return await walletClientOverridesRef.current.connect(
Expand All @@ -169,12 +175,17 @@ export const useIframe = ({
} else {
return {
type: 'error',
error: COSMIFRAME_NOT_CONNECTED_MESSAGE,
error:
COSMIFRAME_NOT_CONNECTED_MESSAGE +
' No connect client function or override provided.',
};
}
},
}),
signerOverrides: () => signerOverridesRef.current,
signerOverrides: async (chainId) =>
typeof signerOverridesRef.current === 'function'
? signerOverridesRef.current(chainId)
: signerOverridesRef.current,
origins,
metadata: {
name:
Expand Down
20 changes: 6 additions & 14 deletions packages/react-lite/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
WalletModalProps,
WalletRepo,
} from '@cosmos-kit/core';
import { Origin } from '@dao-dao/cosmiframe';
import { createContext, ReactNode, useEffect, useMemo, useState } from 'react';

export const walletContext = createContext<{
Expand All @@ -35,16 +36,7 @@ export function ChainProvider({
endpointOptions,
sessionOptions,
logLevel = 'WARN',
allowedIframeParentOrigins = [
'http://localhost:3000',
'https://localhost:3000',
'https://app.osmosis.zone',
'https://daodao.zone',
'https://dao.daodao.zone',
'https://my.abstract.money',
'https://apps.abstract.money',
'https://console.abstract.money',
],
allowedIframeParentOrigins,
children,
}: {
chains: (Chain | ChainName)[];
Expand All @@ -63,9 +55,9 @@ export function ChainProvider({
* Origins to allow wrapping this app in an iframe and connecting to this
* Cosmos Kit instance.
*
* Defaults to Osmosis and DAO DAO.
* Defaults to localhost, Osmosis, DAO DAO, and Abstract.
*/
allowedIframeParentOrigins?: string[];
allowedIframeParentOrigins?: Origin[];
children: ReactNode;
}) {
const logger = useMemo(() => new Logger(logLevel), []);
Expand Down Expand Up @@ -143,10 +135,10 @@ export function ChainProvider({
}, []);

useEffect(() => {
walletManager && walletManager.onMounted();
walletManager?.onMounted();
return () => {
setViewOpen(false);
walletManager && walletManager.onUnmounted();
walletManager?.onUnmounted();
};
}, [render, walletManager]);

Expand Down
3 changes: 2 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
},
"devDependencies": {
"@types/react": "^18.2",
"@types/react-dom": "^18.2"
"@types/react-dom": "^18.2",
"@dao-dao/cosmiframe": "^1.0.0-rc.1"
},
"dependencies": {
"@chain-registry/types": "^0.46.11",
Expand Down
18 changes: 5 additions & 13 deletions packages/react/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
WalletModalProps,
} from '@cosmos-kit/core';
import { ChainProvider as ChainProviderLite } from '@cosmos-kit/react-lite';
import { Origin } from '@dao-dao/cosmiframe';
import { ReactNode, useCallback, useMemo } from 'react';

import { SelectedWalletRepoProvider } from './context';
import { ThemeCustomizationProps, WalletModal } from './modal';
import { defaultModalViews } from './modal/components/views';
import { SelectedWalletRepoProvider } from './context';

export const ChainProvider = ({
chains,
Expand All @@ -33,16 +34,7 @@ export const ChainProvider = ({
endpointOptions,
sessionOptions,
logLevel = 'WARN',
allowedIframeParentOrigins = [
'http://localhost:3000',
'https://localhost:3000',
'https://app.osmosis.zone',
'https://daodao.zone',
'https://dao.daodao.zone',
'https://my.abstract.money',
'https://apps.abstract.money',
'https://console.abstract.money'
],
allowedIframeParentOrigins,
children,
modalTheme = {},
modalOptions,
Expand All @@ -64,9 +56,9 @@ export const ChainProvider = ({
* Origins to allow wrapping this app in an iframe and connecting to this
* Cosmos Kit instance.
*
* Defaults to Osmosis and DAO DAO.
* Defaults to Osmosis, DAO DAO, and Abstract.
*/
allowedIframeParentOrigins?: string[];
allowedIframeParentOrigins?: Origin[];
children: ReactNode;
modalTheme?: ThemeCustomizationProps;
modalOptions?: ModalOptions;
Expand Down
39 changes: 7 additions & 32 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1581,10 +1581,10 @@
node-appwrite "^14.0.0"
ses "^0.18.4"

"@dao-dao/cosmiframe@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@dao-dao/cosmiframe/-/cosmiframe-0.1.0.tgz#5ba241d0c14a45d62df60cff7d48ba26844ebeac"
integrity sha512-NW4pGt1ctqDfhn/A6RU2vwnFEu3O4aBNnBMrGnw31n+L35drYNEsA9ZB7KZsHmRRlkNx+jSuJSv2Fv0BFBDDJQ==
"@dao-dao/cosmiframe@^1.0.0-rc.1":
version "1.0.0-rc.1"
resolved "https://registry.yarnpkg.com/@dao-dao/cosmiframe/-/cosmiframe-1.0.0-rc.1.tgz#51d4d1801605b8abfd01ae0425c797b6b876991e"
integrity sha512-XftJPwbqgS1RWg9SUNBI58vTGnjINZ995OYRwDuUZ6CsuENS2DifAzqgzDgdac+0o5Hqvy/5Aq+HN1Jl/dptTw==
dependencies:
uuid "^9.0.1"

Expand Down Expand Up @@ -18024,16 +18024,7 @@ string-length@^4.0.1:
char-regex "^1.0.2"
strip-ansi "^6.0.0"

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -18149,7 +18140,7 @@ stringify-entities@^4.0.0:
character-entities-html4 "^2.0.0"
character-entities-legacy "^3.0.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -18177,13 +18168,6 @@ strip-ansi@^5.1.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
Expand Down Expand Up @@ -19739,7 +19723,7 @@ wordwrap@^1.0.0:
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -19757,15 +19741,6 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit dd62fc5

Please sign in to comment.