ncryptsec.component.ts raw

   1  import {
   2    AfterViewInit,
   3    Component,
   4    ElementRef,
   5    inject,
   6    OnInit,
   7    ViewChild,
   8  } from '@angular/core';
   9  import { ActivatedRoute } from '@angular/router';
  10  import {
  11    IconButtonComponent,
  12    NavComponent,
  13    NostrHelper,
  14    StorageService,
  15    ToastComponent,
  16  } from '@common';
  17  import { FormsModule } from '@angular/forms';
  18  import * as QRCode from 'qrcode';
  19  
  20  @Component({
  21    selector: 'app-ncryptsec',
  22    imports: [IconButtonComponent, FormsModule, ToastComponent],
  23    templateUrl: './ncryptsec.component.html',
  24    styleUrl: './ncryptsec.component.scss',
  25  })
  26  export class NcryptsecComponent
  27    extends NavComponent
  28    implements OnInit, AfterViewInit
  29  {
  30    @ViewChild('passwordInput') passwordInput!: ElementRef<HTMLInputElement>;
  31  
  32    privkeyHex = '';
  33    ncryptsecPassword = '';
  34    ncryptsec = '';
  35    ncryptsecQr = '';
  36    isGenerating = false;
  37  
  38    readonly #activatedRoute = inject(ActivatedRoute);
  39    readonly #storage = inject(StorageService);
  40  
  41    ngOnInit(): void {
  42      const identityId = this.#activatedRoute.parent?.snapshot.params['id'];
  43      if (!identityId) {
  44        return;
  45      }
  46  
  47      this.#initialize(identityId);
  48    }
  49  
  50    ngAfterViewInit(): void {
  51      this.passwordInput.nativeElement.focus();
  52    }
  53  
  54    async generateNcryptsec() {
  55      if (!this.privkeyHex || !this.ncryptsecPassword) {
  56        return;
  57      }
  58  
  59      this.isGenerating = true;
  60      this.ncryptsec = '';
  61      this.ncryptsecQr = '';
  62  
  63      try {
  64        this.ncryptsec = await NostrHelper.privkeyToNcryptsec(
  65          this.privkeyHex,
  66          this.ncryptsecPassword
  67        );
  68  
  69        // Generate QR code
  70        this.ncryptsecQr = await QRCode.toDataURL(this.ncryptsec, {
  71          width: 250,
  72          margin: 2,
  73          color: {
  74            dark: '#000000',
  75            light: '#ffffff',
  76          },
  77        });
  78      } catch (error) {
  79        console.error('Failed to generate ncryptsec:', error);
  80      } finally {
  81        this.isGenerating = false;
  82      }
  83    }
  84  
  85    copyToClipboard(text: string) {
  86      navigator.clipboard.writeText(text);
  87    }
  88  
  89    #initialize(identityId: string) {
  90      const identity = this.#storage
  91        .getBrowserSessionHandler()
  92        .browserSessionData?.identities.find((x) => x.id === identityId);
  93  
  94      if (!identity) {
  95        return;
  96      }
  97  
  98      this.privkeyHex = identity.privkey;
  99    }
 100  }
 101