From d0583e540ad32f07c3fba3172917b7181cc265f0 Mon Sep 17 00:00:00 2001
From: Daniel Krol <orblivion@gmail.com>
Date: Thu, 5 May 2022 19:49:29 -0400
Subject: [PATCH] getPublicAccounts -> getChannels

Login won't give LBRY accounts, it'll give channels
---
 src/app/account.service.ts                    | 61 ++++++++++---------
 src/app/approve/approve.component.ts          |  2 +-
 src/app/identity.service.ts                   |  4 +-
 src/app/log-in-app/log-in-app.component.ts    |  6 +-
 src/app/logout/logout.component.ts            |  2 +-
 src/app/sign-up/sign-up.component.ts          |  2 +-
 .../test-sign-transaction.component.ts        |  2 +-
 src/app/test-sign/test-sign.component.ts      |  2 +-
 src/types/identity.ts                         |  4 +-
 9 files changed, 43 insertions(+), 42 deletions(-)

diff --git a/src/app/account.service.ts b/src/app/account.service.ts
index 606cbb7..0ce42d4 100644
--- a/src/app/account.service.ts
+++ b/src/app/account.service.ts
@@ -1,7 +1,7 @@
 import {Injectable} from '@angular/core';
 import {CryptoService} from './crypto.service';
 import {GlobalVarsService} from './global-vars.service';
-import {AccessLevel, PrivateAccountInfo, PublicAccountInfo} from '../types/identity';
+import {AccessLevel, PrivateAccountInfo, PublicChannelInfo} from '../types/identity';
 
 @Injectable({
   providedIn: 'root'
@@ -16,45 +16,46 @@ export class AccountService {
 
   // Public Getters
 
-  getAccountNames(): any {
-    // TODO - maybe write this in a safer, more future-perfect way since it's converting
-    // private to public
-    return Object.keys(this.getWalletAccounts());
-  }
-
   // TODO - As of this writing, we want to share channel claim ids with the
   // account on login, and spending addresses on request (probably with
   // explicit permission)
-  //
-  // This is in a state in between what DeSo had and what
-  // we want ultimately for LBRY.
-  getPublicAccounts(): {[key: string]: PublicAccountInfo} {
-    const hostname = this.globalVars.hostname;
+  getChannels(): {[key: string]: PublicChannelInfo} {
+    // TODO - will want for accessLevel stuff
+    // const hostname = this.globalVars.hostname;
+
     const privateAccounts = this.getWalletAccounts();
-    const publicAccounts: {[key: string]: PublicAccountInfo} = {};
+    const channels: {[key: string]: PublicChannelInfo} = {};
 
     for (const name of Object.keys(privateAccounts)) {
       const privateAccount = privateAccounts[name];
-      const accessLevel = this.getAccessLevel(name, hostname);
-      if (accessLevel === AccessLevel.None) {
-        continue;
+      for (const channelPubKeyAddress of Object.keys(privateAccount.certificates)) {
+        // TODO - For LBRY's purposes, not only will we want per-channel access
+        // levels, we'll want per channel per hostname per action access levels.
+
+        // TODO - finish when we have accessLevel stuff
+        /*
+        const accessLevel = this.getAccessLevel(name, hostname);
+        if (accessLevel === AccessLevel.None) {
+          continue;
+        }
+
+        // TODO - Implement the hmac properly
+        // TODO - why do we even have hmac if everything's in local storage anyway?
+        const accessLevelHmac = this.cryptoService.accessLevelHmac(accessLevel, privateAccount.seed);
+        */
+
+        channels[channelPubKeyAddress] = {
+          pubKeyAddress: channelPubKeyAddress,
+          network: privateAccount.ledger,
+
+          // TODO - fill in when we have accessLevel stuff
+          accessLevel: 0,
+          accessLevelHmac: "",
+        };
       }
-
-      // TODO
-      throw 'Implement the hmac properly'
-
-      // TODO - why do we even have hmac if everything's in local storage anyway? 
-      const accessLevelHmac = this.cryptoService.accessLevelHmac(accessLevel, privateAccount.seed);
-
-      publicAccounts[name] = {
-        name,
-        network: privateAccount.ledger,
-        accessLevel,
-        accessLevelHmac,
-      };
     }
 
-    return publicAccounts;
+    return channels;
   }
 
   // TODO - Need to confirm that this works I think
diff --git a/src/app/approve/approve.component.ts b/src/app/approve/approve.component.ts
index f87f4ac..e0f0a58 100644
--- a/src/app/approve/approve.component.ts
+++ b/src/app/approve/approve.component.ts
@@ -86,7 +86,7 @@ export class ApproveComponent implements OnInit {
 
   finishFlow(signedTransactionHex?: string): void {
     this.identityService.login({
-      accounts: this.accountService.getPublicAccounts(),
+      channels: this.accountService.getChannels(),
       signedTransactionHex,
     });
   }
diff --git a/src/app/identity.service.ts b/src/app/identity.service.ts
index 139d3bb..46bd959 100644
--- a/src/app/identity.service.ts
+++ b/src/app/identity.service.ts
@@ -1,7 +1,7 @@
 import {Injectable} from '@angular/core';
 import {Observable, Subject} from 'rxjs';
 import {v4 as uuid} from 'uuid';
-import {AccessLevel, PublicAccountInfo} from '../types/identity';
+import {AccessLevel, PublicChannelInfo} from '../types/identity';
 import {CryptoService} from './crypto.service';
 import {GlobalVarsService} from './global-vars.service';
 import {CookieService} from 'ngx-cookie';
@@ -41,7 +41,7 @@ export class IdentityService {
   }
 
   login(payload: {
-    accounts: {[key: string]: PublicAccountInfo}, // Channel ids
+    channels: {[key: string]: PublicChannelInfo},
     accountNameAdded?: string, // Which channel id was just authorized. the app may allow the user to switch between them, but this determines which is on now.
     signedUp?: boolean
     signedTransactionHex?: string,
diff --git a/src/app/log-in-app/log-in-app.component.ts b/src/app/log-in-app/log-in-app.component.ts
index 493cc1f..4c244bb 100644
--- a/src/app/log-in-app/log-in-app.component.ts
+++ b/src/app/log-in-app/log-in-app.component.ts
@@ -18,14 +18,14 @@ export class LogInAppComponent implements OnInit {
   ) { }
 
   ngOnInit(): void {
-    // TODO - Will be channel claim IDs
-    this.allAccountNames = this.accountService.getAccountNames();
+    // TODO - Will hopefully be channel claim IDs but I don't know what will be available in reality
+    // this.allAccountNames = ...
   }
 
   selectAccount(accountName: string): void {
     this.accountService.setAccessLevel(accountName, this.globalVars.hostname, this.globalVars.accessLevelRequest);
     this.identityService.login({
-      accounts: this.accountService.getPublicAccounts(),
+      channels: this.accountService.getChannels(),
       accountNameAdded: accountName,
       signedUp: false
     });
diff --git a/src/app/logout/logout.component.ts b/src/app/logout/logout.component.ts
index a435af2..68b614b 100644
--- a/src/app/logout/logout.component.ts
+++ b/src/app/logout/logout.component.ts
@@ -43,7 +43,7 @@ export class LogoutComponent implements OnInit {
 
   finishFlow(): void {
     this.identityService.login({
-      accounts: this.accountService.getPublicAccounts(),
+      channels: this.accountService.getChannels(),
     });
   }
 
diff --git a/src/app/sign-up/sign-up.component.ts b/src/app/sign-up/sign-up.component.ts
index 67f8296..da22524 100644
--- a/src/app/sign-up/sign-up.component.ts
+++ b/src/app/sign-up/sign-up.component.ts
@@ -106,7 +106,7 @@ export class SignUpComponent implements OnInit, OnDestroy {
 
   login(): void {
     this.identityService.login({
-      accounts: this.accountService.getPublicAccounts(),
+      channels: this.accountService.getChannels(),
       accountNameAdded: this.accountNameAdded,
       signedUp: true,
     });
diff --git a/src/app/test-sign-transaction/test-sign-transaction.component.ts b/src/app/test-sign-transaction/test-sign-transaction.component.ts
index 7dcad63..97d5804 100644
--- a/src/app/test-sign-transaction/test-sign-transaction.component.ts
+++ b/src/app/test-sign-transaction/test-sign-transaction.component.ts
@@ -41,7 +41,7 @@ export class TestSignTransactionComponent implements OnInit {
 
   finishFlow(signedTransactionHex?: string): void {
     this.identityService.login({
-      accounts: this.accountService.getPublicAccounts(),
+      channels: this.accountService.getChannels(),
       signedTransactionHex,
     });
   }
diff --git a/src/app/test-sign/test-sign.component.ts b/src/app/test-sign/test-sign.component.ts
index bbf2117..3adc6af 100644
--- a/src/app/test-sign/test-sign.component.ts
+++ b/src/app/test-sign/test-sign.component.ts
@@ -36,7 +36,7 @@ export class TestSignComponent implements OnInit {
 
   finishFlow(signatureHex?: string): void {
     this.identityService.login({
-      accounts: this.accountService.getPublicAccounts(),
+      channels: this.accountService.getChannels(),
       signatureHex,
     });
   }
diff --git a/src/types/identity.ts b/src/types/identity.ts
index 2b50927..9d5582b 100644
--- a/src/types/identity.ts
+++ b/src/types/identity.ts
@@ -36,9 +36,9 @@ export interface PrivateAccountInfo {
 }
 
 // can be sent to the app
-export interface PublicAccountInfo {
+export interface PublicChannelInfo {
   // TODO - add more useful stuff
-  name: string;
+  pubKeyAddress: string;
   network: Network;
   accessLevel: AccessLevel;
   accessLevelHmac: string;