Skip to content

Build KeychainProgram #216

@benjaminpreiss

Description

@benjaminpreiss

We build a new KeychainProgram that extends Program and can be used by devs to handle keys for them.

How might this be implemented?

 import { Keychain } from '@peerbit/keychain' 

class KeychainDB extends Program implements Keychain, EncryptionProvider ? 
{
    @field({type:  Documents<Key>}}
    keys: Documents<Key>

    async open() {

          await this.keys.open({ encryption: new EncryptionProvider(this) }} // create a encryption provider from 'this' because 'this' implements Keychain interface 
   }

   async addKey(key: Key) {
        return this.keys.put(key, { encryption: { specific encryption options }}
   }

}

... And how could we possibly use it?

import { KeychainDB } from '@peerbit/keychain'


class MySpecialDBAndKeychain extends Program 
{
    @field({type: KeychainProgram}}
    keychain: KeychainProgram

    @field({type: Posts}}
    posts: Posts

    async open() {
          await this.keychain.open()
          await this.posts.open( { encryption: this.keychain.encryption }  )  // use custom encryption provider  (layer 1)
   }

   async createPost(content: string) {
        return this.posts.put(new Post(content))
   }

}

And then the Posts Document can use the passed encryption and decryption provider to store encrypted data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions