Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
}
},
"test": {
"builder": "@angular-bulders/custom-webpack:karma",
"builder": "@angular-builders/custom-webpack:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
Expand Down
109 changes: 103 additions & 6 deletions src/app/account.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,113 @@
import { TestBed } from '@angular/core/testing';
import {TestBed} from '@angular/core/testing';

import { AccountService } from './account.service';
import {AccountService} from './account.service';
import {CookieModule} from 'ngx-cookie';
import {AccessLevel, Network} from '../types/identity';
import {CryptoService} from './crypto.service';
import * as bip39 from 'bip39';
import {GlobalVarsService} from './global-vars.service';

describe('AccountService', () => {
let service: AccountService;
let accountService: AccountService;
let cryptoService: CryptoService;
let globalVarsService: GlobalVarsService;

const hostname = 'example.com';

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AccountService);
TestBed.configureTestingModule({
providers: [ CryptoService, GlobalVarsService ],
imports: [ CookieModule.forRoot() ],
});

accountService = TestBed.inject(AccountService);
cryptoService = TestBed.inject(CryptoService);
globalVarsService = TestBed.inject(GlobalVarsService);

globalVarsService.hostname = hostname;
});

afterEach(() => {
// clean up any leftover users
deleteUsers(accountService);
});

it('should be created', () => {
expect(service).toBeTruthy();
expect(accountService).toBeTruthy();
});

it('can add and remove users', () => {
const numUsers = 10;
for (let i = 0; i < numUsers; i++) {
const newUser = mockUser(cryptoService);
accountService.addUser(newUser);

// adding the same user twice should be a no-op
accountService.addUser(newUser);
}

expect(accountService.getPublicKeys().length).toEqual(numUsers);

deleteUsers(accountService);
expect(accountService.getPublicKeys.length).toEqual(0);
});

it('can set and revoke access', () => {
const user = mockUser(cryptoService);
const pubKey = accountService.addUser(user);

// No access by default
expect(accountService.getEncryptedUsers()).toEqual({});

// Read-only access does not return encrypted seed
accountService.setAccessLevel(pubKey, hostname, AccessLevel.ReadOnly);
expect(accountService.getEncryptedUsers()).toEqual({
[pubKey]: {
hasExtraText: true,
btcDepositAddress: user.btcDepositAddress,
encryptedSeedHex: '',
accessLevel: AccessLevel.ReadOnly,
network: Network.mainnet,
},
});

// Full access returns encrypted seed
accountService.setAccessLevel(pubKey, hostname, AccessLevel.Full);
expect(accountService.getEncryptedUsers()).toEqual({
[pubKey]: {
hasExtraText: true,
btcDepositAddress: user.btcDepositAddress,
encryptedSeedHex: cryptoService.encryptSeedHex(user.seedHex, hostname),
accessLevel: AccessLevel.Full,
network: Network.mainnet,
},
});

// Revoking access works
accountService.setAccessLevel(pubKey, hostname, AccessLevel.None);
expect(accountService.getEncryptedUsers()).toEqual({});
});
});

const deleteUsers = (accountService: AccountService) => {
for (const publicKey of accountService.getPublicKeys()) {
accountService.deleteUser(publicKey);
}
};

const mockUser = (cryptoService: CryptoService) => {
const network = Network.mainnet;
const mnemonic = bip39.generateMnemonic();
const extraText = 'testing';
const keychain = cryptoService.mnemonicToKeychain(mnemonic, extraText);
const seedHex = cryptoService.keychainToSeedHex(keychain);
const btcDepositAddress = cryptoService.keychainToBtcAddress(keychain, network);

return {
seedHex,
mnemonic,
extraText,
btcDepositAddress,
network,
};
};
11 changes: 3 additions & 8 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import {CookieModule} from 'ngx-cookie';

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule
RouterTestingModule,
CookieModule.forRoot(),
],
declarations: [
AppComponent
Expand All @@ -25,11 +27,4 @@ describe('AppComponent', () => {
const app = fixture.componentInstance;
expect(app.title).toEqual('identity');
});

it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('.content span').textContent).toContain('identity app is running!');
});
});
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class AppComponent implements OnInit {
ngOnInit(): void {
// We must be in an iframe OR opened with window.open
if (!this.globalVars.inTab && !this.globalVars.inFrame()) {
window.location.href = 'https://bitclout.com';
this.globalVars.getWindow().location.href = 'https://bitclout.com';
return;
}

Expand Down
5 changes: 4 additions & 1 deletion src/app/backend-api.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { TestBed } from '@angular/core/testing';

import { BackendAPIService } from './backend-api.service';
import {HttpClientTestingModule} from '@angular/common/http/testing';

describe('BackendAPIService', () => {
let service: BackendAPIService;

beforeEach(() => {
TestBed.configureTestingModule({});
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
});
service = TestBed.inject(BackendAPIService);
});

Expand Down
5 changes: 4 additions & 1 deletion src/app/crypto.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { TestBed } from '@angular/core/testing';

import { CryptoService } from './crypto.service';
import {CookieModule} from 'ngx-cookie';

describe('CryptoService', () => {
let service: CryptoService;

beforeEach(() => {
TestBed.configureTestingModule({});
TestBed.configureTestingModule({
imports: [ CookieModule.forRoot() ],
});
service = TestBed.inject(CryptoService);
});

Expand Down
4 changes: 2 additions & 2 deletions src/app/crypto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class CryptoService {
privateKeyToBitcloutPublicKey(privateKey: EC.KeyPair, network: Network): string {
const prefix = CryptoService.PUBLIC_KEY_PREFIXES[network].bitclout;
const key = privateKey.getPublic().encode('array', true);
const prefixAndKey = Uint8Array.from([...prefix, ...key]);
const prefixAndKey = Buffer.from([...prefix, ...key]);

return bs58check.encode(prefixAndKey);
}
Expand All @@ -122,7 +122,7 @@ export class CryptoService {
const prefix = CryptoService.PUBLIC_KEY_PREFIXES[network].bitcoin;
// @ts-ignore TODO: add "identifier" to type definition
const identifier = keychain.identifier;
const prefixAndKey = Uint8Array.from([...prefix, ...identifier]);
const prefixAndKey = Buffer.from([...prefix, ...identifier]);

return bs58check.encode(prefixAndKey);
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/embed/embed.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { EmbedComponent } from './embed.component';
import {CookieModule} from 'ngx-cookie';

describe('EmbedComponent', () => {
let component: EmbedComponent;
let fixture: ComponentFixture<EmbedComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ EmbedComponent ]
declarations: [ EmbedComponent ],
imports: [ CookieModule.forRoot() ],
})
.compileComponents();
});
Expand Down
22 changes: 20 additions & 2 deletions src/app/global-vars.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class GlobalVarsService {
network = Network.mainnet;
hostname = '';

inTab = !!window.opener;
inTab = !!this.getOpener();

constructor() { }

Expand All @@ -21,10 +21,28 @@ export class GlobalVarsService {

inFrame(): boolean {
try {
return window.self !== window.top;
return this.getWindow().self !== this.getWindow().top;
} catch (e) {
// Most browsers block access to window.top when in an iframe
return true;
}
}

getWindow(): Window {
return window;
}

getOpener(): Window {
return this.getWindow().opener;
}

getParent(): Window {
return this.getWindow().parent;
}

getCurrentWindow(): Window {
// Opener can be null, parent is never null
return this.getOpener() || this.getParent();
}

}
Loading