From 944d2e2bdde339ac8556429771c83d6efc4f3abf Mon Sep 17 00:00:00 2001 From: kalfada Date: Tue, 3 Sep 2024 12:54:21 +0300 Subject: [PATCH 001/143] fix: EOL bug --- packages/yoroi-extension/chrome/constants-mv2.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/yoroi-extension/chrome/constants-mv2.js b/packages/yoroi-extension/chrome/constants-mv2.js index f81572f587..d4f72659b9 100644 --- a/packages/yoroi-extension/chrome/constants-mv2.js +++ b/packages/yoroi-extension/chrome/constants-mv2.js @@ -10,12 +10,12 @@ export function genCSP(request: {| isDev: boolean, additional: {| 'default-src'?: Array, - 'frame-src'?: Array, - 'script-src'?: Array, - 'object-src'?: Array, - 'connect-src'?: Array, - 'style-src'?: Array, - 'img-src'?: Array, + 'frame-src' ?: Array < string >, + 'script-src' ?: Array < string >, + 'object-src' ?: Array < string >, + 'connect-src' ?: Array < string >, + 'style-src' ?: Array < string >, + 'img-src' ?: Array < string >, |}, |}): string { const defaultSrc = request.additional['default-src'] ?? []; @@ -49,6 +49,8 @@ export function genCSP(request: {| frameSrc.push('https://emurgo.github.io/yoroi-extension-ledger-bridge'); frameSrc.push('https://emurgo.github.io/'); + // Bringweb3 + // Analytics connectSrc.push('https://analytics.emurgo-rnd.com/'); connectSrc.push('https://api2.amplitude.com/'); From 6bc3d452c5cd6b36722894f804623d3cc9e89d6f Mon Sep 17 00:00:00 2001 From: kalfada Date: Wed, 11 Sep 2024 10:03:14 +0300 Subject: [PATCH 002/143] feat: add cashback tab and page --- packages/yoroi-extension/app/Routes.js | 7 + .../app/api/ada/lib/test-config.forTests.js | 5 + .../app/containers/cashback/CashbackPage.js | 173 ++++++++++++++++++ packages/yoroi-extension/app/environment.js | 34 ++-- .../app/i18n/global-messages.js | 4 + .../app/i18n/locales/en-US.json | 3 +- .../app/i18n/locales/hu-HU.json | 3 +- .../app/i18n/locales/vi-VN.json | 3 +- packages/yoroi-extension/app/routes-config.js | 4 + .../app/stores/stateless/sidebarCategories.js | 29 +-- .../yoroi-extension/chrome/constants-mv2.js | 3 + .../chrome/content-scripts/bring.js | 8 + .../chrome/extension/background/bring.js | 11 ++ .../chrome/extension/background/index.js | 4 +- .../chrome/manifest-mv2.template.js | 2 + .../yoroi-extension/config/config-types.js | 55 +++--- .../yoroi-extension/config/development.json | 7 +- packages/yoroi-extension/config/mainnet.json | 7 +- packages/yoroi-extension/config/test.json | 7 +- packages/yoroi-extension/package-lock.json | 6 + packages/yoroi-extension/package.json | 3 +- 21 files changed, 320 insertions(+), 58 deletions(-) create mode 100644 packages/yoroi-extension/app/containers/cashback/CashbackPage.js create mode 100644 packages/yoroi-extension/chrome/content-scripts/bring.js create mode 100644 packages/yoroi-extension/chrome/extension/background/bring.js diff --git a/packages/yoroi-extension/app/Routes.js b/packages/yoroi-extension/app/Routes.js index dd24adae2f..3b60d30b98 100644 --- a/packages/yoroi-extension/app/Routes.js +++ b/packages/yoroi-extension/app/Routes.js @@ -125,6 +125,9 @@ const TokensPageRevamp = React.lazy(TokensPageRevampPromise); const TokensDetailPageRevampPromise = () => import('./containers/wallet/TokenDetailPageRevamp'); const TokensDetailPageRevamp = React.lazy(TokensDetailPageRevampPromise); +const CashbackPagePromise = () => import('./containers/cashback/CashbackPage'); +const CashbackPage = React.lazy(CashbackPagePromise) + const NFTsPageRevampPromise = () => import('./containers/wallet/NFTsPageRevamp'); const NFTsPageRevamp = React.lazy(NFTsPageRevampPromise); @@ -245,6 +248,10 @@ export const Routes = (stores: StoresMap, actions: ActionsMap): Node => { path={ROUTES.NFTS.ROOT} component={props => wrapNFTs({ ...props, stores, actions }, NFTsSubPages(stores, actions))} /> + } + /> Node |}; +type AllProps = {| ...Props, ...InjectedLayoutProps |}; +type State = {| iframeSrc: string, status: string, popup: boolean, overlayBgColor: string |}; + +type IframeMessageData = {| + action: string, + bgColor ?: string, + messageToSign: string + |}; + +@observer +class CashbackPageContainer extends Component { + static contextTypes: {| intl: $npm$ReactIntl$IntlFormat |} = { + intl: intlShape.isRequired, + }; + +state = { + iframeSrc: '', // Initial URL + status: 'loading', // Initial status + popup: false, + overlayBgColor: '#000000fa' +}; + +async componentDidMount() { + // User should not be able to access the route when using Yoroi Light + if (environment.isLight) { + this.props.actions.router.goToRoute.trigger({ + route: ROUTES.MY_WALLETS, + }); + } + this.fetchIframeUrl() + this.setupMessageListener(); +} + +componentWillUnmount() { + this.removeMessageListener(); +} + +setupMessageListener = () => { + window.addEventListener('message', this.handleMessage); +} + +removeMessageListener = () => { + window.removeEventListener('message', this.handleMessage); +} + +handleMessage = (event: MessageEvent) => { + const iframeOrigin = new URL(this.state.iframeSrc).origin + + if (event.origin !== iframeOrigin) { + return; + } + + console.log('Received message from iframe:', event.data); + + const messageData: IframeMessageData = (event.data: any); + + if (messageData.action === 'SIGN_MESSAGE') { + // messageData.messageToSign + } + else if (messageData.action === 'OPEN_POPUP') { + this.setState({ popup: true, overlayBgColor: messageData.bgColor || this.state.overlayBgColor }) + } + else if (messageData.action === 'CLOSE_POPUP') { + this.setState({ popup: false }) + } +} + +async fetchIframeUrl(){ + try { + const publicDeriver = this.props.stores.wallets.selected + if (!publicDeriver) throw Error('no publicDeriver') + const walletAddress = publicDeriver.externalAddressesByType[CoreAddressTypes.CARDANO_BASE][0].address; + + const response = await fetch(`${environment.bringBaseUrl}check/portal`, { + method: 'POST', + headers: { + 'x-api-key': environment.bringIdentifier, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + walletAddress, + + }), + }); // Replace with your API endpoint + const data = await response.json(); + + // Convert data to query parameters (assuming data is an object) + const queryParams = new URLSearchParams({ ...data, theme: environment.isLight ? 'light' : 'dark' }).toString(); + const iframeSrc = `${environment.bringIframeSrc}/?${queryParams}`; + + // Update the state to reflect the new iframe URL + this.setState({ iframeSrc, status: 'done' }); + } catch (error) { + console.error('Error fetching data:', error); + } +} + + + +render(): Node { + const { actions, stores } = this.props; + const sidebarContainer = ; + const { intl } = this.context; + + return ( + } + sidebar={sidebarContainer} + navbar={ + } + /> + } + > + + {this.state.popup ? +
{ + const iframeElement = document.getElementById('bringweb3'); + if (iframeElement instanceof HTMLIFrameElement) { + iframeElement.contentWindow.postMessage({ action: 'CLOSE_POPUP' }, '*') + } + this.setState({ popup: false }) + }} + >
+ : + null + } + + {this.state.iframeSrc.length ? +