Skip to content

Commit

Permalink
Remove all old database files
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Works <jackworks@protonmail.com>
  • Loading branch information
Jack-Works committed Jun 4, 2019
1 parent 2bf4443 commit 5cb2272
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 167 deletions.
59 changes: 53 additions & 6 deletions src/database/migrate/old.avatar.1.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
import { queryAvatarDataDeprecated } from '../../key-management/avatar-db'
/**
* @deprecated
* ! This version of database is deprecated
* ! Don't use it.
*
* ! Scheduled to remove it after Jan/1/2019
* ! This database should be readonly now.
*/
import { PersonIdentifier, Relation } from '../type'
import { deleteDB } from 'idb/with-async-ittr'
import * as Avatar from '../avatar'
import * as People from '../people'

import { Entity, Index, Db } from 'typed-db'
import { buildQuery } from '../../utils/utils'

@Entity()
class AvatarRecord {
@Index({ unique: true })
username!: string
nickname!: string
avatar!: ArrayBuffer
lastUpdateTime!: Date
}

/**
* @deprecated
*/
async function queryAvatarData() {
// tslint:disable-next-line: deprecation
const query = buildQuery(new Db('maskbook-avatar-store', 1), AvatarRecord)
const record = await query(t => t.openCursor().asList())
return record
}
const cache = new Map<string, string>()
/**
* @deprecated
*/
async function toDataUrl(x: ArrayBuffer, username?: string): Promise<string> {
function ArrayBufferToBase64(buffer: ArrayBuffer) {
const f = new Blob([buffer], { type: 'image/png' })
const fr = new FileReader()
return new Promise<string>(resolve => {
fr.onload = () => resolve(fr.result as string)
fr.readAsDataURL(f)
})
}
const createAndStore = async () => {
const u = await ArrayBufferToBase64(x)
cache.set(username || '$', u)
return u
}
if (username) return cache.get(username) || createAndStore()
return createAndStore()
}

async function updatePartialPersonRecord(...args: Parameters<typeof People.updatePersonDB>) {
const id = args[0].identifier
if (await People.queryPersonDB(id)) {
Expand All @@ -19,18 +69,15 @@ async function updatePartialPersonRecord(...args: Parameters<typeof People.updat
})
}
}
/**
* ! Migrate old database into new one.
* ! Scheduled to remove it after Jan/1/2019
*/

export default async function migrate() {
if (indexedDB.databases) {
const dbs = await indexedDB.databases()
if (!dbs.find(x => x.name === 'maskbook-avatar-store')) return
}
const promises: Promise<unknown>[] = []
// tslint:disable-next-line: deprecation
for (const record of await queryAvatarDataDeprecated()) {
for (const record of await queryAvatarData()) {
const person = new PersonIdentifier('facebook.com', record.username)
promises.push(
Avatar.storeAvatarDB(person, record.avatar),
Expand Down
79 changes: 74 additions & 5 deletions src/database/migrate/old.keystore.1.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,81 @@
/**
* @deprecated
* ! This version of database is deprecated
* ! Don't use it.
*
* ! Scheduled to remove it after May/31/2019
* ! This database should be readonly now.
*/
import { Entity, Index, Db, Key } from 'typed-db'
import { OnlyRunInContext } from '@holoflows/kit/es'
// tslint:disable: deprecation
OnlyRunInContext('background', 'Key Store')
@Entity()
/** DO NOT Change the name of this class! It is used as key in the db! */
class CryptoKeyRecord {
@Index({ unique: true })
@Key()
username!: string
key!: { publicKey: JsonWebKey; privateKey?: JsonWebKey }
algor!: any
usages!: string[]
}
// ! This keystore is not stable and maybe drop in the future!

type Exporting = {
username: string
publicKey: CryptoKey
privateKey?: CryptoKey
}
/**
* @deprecated
*/
async function queryPeopleCryptoKey(): Promise<Exporting[]> {
async function toReadCryptoKey(y: CryptoKeyRecord): Promise<Exporting> {
const pub = await crypto.subtle.importKey('jwk', y.key.publicKey, y.algor, true, y.usages)
let priv: CryptoKey | undefined = undefined
if (y.key.privateKey) priv = await crypto.subtle.importKey('jwk', y.key.privateKey, y.algor, true, y.usages)
return {
username: y.username,
publicKey: pub,
privateKey: priv,
}
}
const query = buildQuery(new Db('maskbook-keystore-demo-v2', 1), CryptoKeyRecord)
const record = await query(t => t.openCursor().asList())
return Promise.all(record.map(toReadCryptoKey))
}
//#region Store & Read CryptoKey
//#endregion

//#region Generate a new private key

/**
* @deprecated
*/
async function generateNewKey(whoami: PersonIdentifier): Promise<People.PersonRecordPublicPrivate> {
const has = await People.queryMyIdentityAtDB(whoami)
if (has) throw new TypeError('You already have a key-pair!')

const mine = await crypto.subtle.generateKey({ name: 'ECDH', namedCurve: 'K-256' }, true, ['deriveKey'])
const record: People.PersonRecordPublicPrivate = {
identifier: whoami,
groups: [],
publicKey: mine.publicKey,
privateKey: mine.privateKey,
relation: [],
relationLastCheckTime: new Date(),
}
await People.storeMyIdentityDB(record)
return record
}
//#endregion

import { deleteDB } from 'idb/with-async-ittr'
import * as People from '../people'
import { queryPeopleCryptoKey } from '../../key-management/keystore-db'
import { PersonIdentifier } from '../type'
import { buildQuery } from '../../utils/utils'

/**
* ! Migrate old database into new one.
* ! Scheduled to remove it after May/31/2019
*/
export default async function migrate() {
if (indexedDB.databases) {
const dbs = await indexedDB.databases()
Expand Down
32 changes: 31 additions & 1 deletion src/database/migrate/old.localKeys.1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
/**
* @deprecated
* ! This version of database is deprecated
* ! Don't use it.
*
* ! Scheduled to remove it after Jan/1/2019
* ! This database should be readonly now.
*/
import { deleteDB } from 'idb/with-async-ittr'
import * as People from '../people'
import { getMyLocalKey } from '../../key-management/local-db'
import { Entity, Index, Db, Key } from 'typed-db'
import { OnlyRunInContext } from '@holoflows/kit/es'
import { buildQuery } from '../../utils/utils'

OnlyRunInContext('background', 'Local Key Store')
@Entity()
/** DO NOT Change the name of this class! It is used as key in the db! */
class LocalCryptoKeyRecord {
@Index({ unique: true })
@Key()
username!: string
key!: CryptoKey
}
/**
* @deprecated
*/
async function getMyLocalKey(): Promise<LocalCryptoKeyRecord | null> {
// tslint:disable-next-line: deprecation
const query = buildQuery(new Db('maskbook-localkeystore-demo-v1', 1), LocalCryptoKeyRecord)
const record = await query(t => t.get('$self'))
if (!record) return null
return record
}

/**
* ! Migrate old database into new one.
Expand Down
49 changes: 0 additions & 49 deletions src/key-management/avatar-db.ts

This file was deleted.

75 changes: 0 additions & 75 deletions src/key-management/keystore-db.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/key-management/local-db.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { CustomEventId } from './constants'
import { CustomEvents } from '../extension/injected-script/addEventListener'

export { sleep, timeout } from '@holoflows/kit/es/util/sleep'
/** Build a db */
/**
* Build a db
* @deprecated
*/
export const buildQuery = <Q extends Newable<any>>(db: Db, record: Q) => {
db.use(record)
return <T>(
Expand Down

0 comments on commit 5cb2272

Please sign in to comment.