Skip to content

Commit

Permalink
proof of concept copy clipboard data from ionic to iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
sultanmyrza committed Aug 7, 2023
1 parent 5cdb5f0 commit 4823a4d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/app/features/wallets/wallets.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ng-template #bubbleIframe>
<iframe
#walletIframe
[src]="iframeUrl$ | async | safeResourceUrl"
class="bubble-iframe"
></iframe>
Expand Down
29 changes: 26 additions & 3 deletions src/app/features/wallets/wallets.page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { Component, ElementRef, ViewChild } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Router } from '@angular/router';
import { Clipboard } from '@capacitor/clipboard';
Expand All @@ -7,12 +7,16 @@ import { TranslocoService } from '@ngneat/transloco';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { NgxQrcodeElementTypes } from '@techiediaries/ngx-qrcode';
import { BehaviorSubject, fromEvent } from 'rxjs';
import { concatMap, first, map, tap } from 'rxjs/operators';
import { concatMap, filter, first, map, tap } from 'rxjs/operators';
import { CaptureAppWebCryptoApiSignatureProvider } from '../../shared/collector/signature/capture-app-web-crypto-api-signature-provider/capture-app-web-crypto-api-signature-provider.service';
import { DiaBackendAuthService } from '../../shared/dia-backend/auth/dia-backend-auth.service';
import { BUBBLE_IFRAME_URL } from '../../shared/dia-backend/secret';
import { DiaBackendWalletService } from '../../shared/dia-backend/wallet/dia-backend-wallet.service';
import { BubbleToIonicPostMessage } from '../../shared/iframe/iframe';
import {
BubbleToIonicPostMessage,
IonicIframePostMessage,
IonicToBubblePostMessage,
} from '../../shared/iframe/iframe';
@UntilDestroy({ checkProperties: true })
@Component({
selector: 'app-wallets',
Expand All @@ -28,6 +32,7 @@ export class WalletsPage {

readonly iframeUrl$ = this.diaBackendAuthService.cachedQueryJWTToken$.pipe(
map(token => {
return 'https://captureappiframe.numbersprotocol.io/version-87gw/experiment_paste';
const queryParams = `token=${token.access}&refresh_token=${token.refresh}`;
const url = `${BUBBLE_IFRAME_URL}/wallet?${queryParams}`;
return url;
Expand All @@ -38,6 +43,8 @@ export class WalletsPage {
elementType = NgxQrcodeElementTypes.URL;
shouldHideDepositButton = false;

@ViewChild('walletIframe') walletIframe?: ElementRef<HTMLIFrameElement>;

constructor(
private readonly diaBackendWalletService: DiaBackendWalletService,
private readonly diaBackendAuthService: DiaBackendAuthService,
Expand All @@ -53,6 +60,7 @@ export class WalletsPage {
private processIframeEvents() {
fromEvent(window, 'message')
.pipe(
filter(event => (event as MessageEvent).origin === BUBBLE_IFRAME_URL),
tap(event => {
const postMessageEvent = event as MessageEvent;
const data = postMessageEvent.data as BubbleToIonicPostMessage;
Expand All @@ -75,6 +83,9 @@ export class WalletsPage {
case BubbleToIonicPostMessage.IFRAME_COPY_TO_CLIPBOARD_PRIVATE_KEY:
this.copyToClipboardPrivateKey();
break;
case BubbleToIonicPostMessage.REQUEST_CLIPBOARD_DATA_FROM_IONIC:
this.sendClipboarDataToIonic();
break;
default:
break;
}
Expand Down Expand Up @@ -114,6 +125,18 @@ export class WalletsPage {
.subscribe();
}

private async sendClipboarDataToIonic() {
const clipboardData = await Clipboard.read();
const iframePostMessage: IonicIframePostMessage = {
type: IonicToBubblePostMessage.CLIPBOARD_DATA_FROM_IONIC,
payload: clipboardData.type === 'text/plain' ? clipboardData.value : '',
};
this.walletIframe?.nativeElement.contentWindow?.postMessage(
iframePostMessage,
BUBBLE_IFRAME_URL
);
}

navigateToBuyNumPage() {
this.router.navigate(['wallets', 'buy-num']);
}
Expand Down
7 changes: 7 additions & 0 deletions src/app/shared/iframe/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ export enum BubbleToIonicPostMessage {
IFRAME_COPY_TO_CLIPBOARD_ASSET_WALLET = 'iframe-copy-to-clipboard-asset-wallet',
IFRAME_COPY_TO_CLIPBOARD_INTEGRITY_WALLET = 'iframe-copy-to-clipboard-integrity-wallet',
IFRAME_COPY_TO_CLIPBOARD_PRIVATE_KEY = 'iframe-copy-to-clipboard-private-key',
REQUEST_CLIPBOARD_DATA_FROM_IONIC = 'request-clipboard-data-from-ionic',
}

export enum IonicToBubblePostMessage {
ANDROID_BACK_BUTTON = 'android-back-button',
CLIPBOARD_DATA_FROM_IONIC = 'clipboard-data-from-ionic',
}

export interface IonicIframePostMessage {
type: BubbleToIonicPostMessage | IonicToBubblePostMessage;
payload: any;
}

0 comments on commit 4823a4d

Please sign in to comment.