Skip to content
This repository has been archived by the owner on Sep 30, 2023. It is now read-only.

feat: remove provider #52

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions dist/index-browser.min.js

Large diffs are not rendered by default.

116 changes: 54 additions & 62 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 39 additions & 45 deletions src/identities.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const Identity = require('./identity')
const IdentityProvider = require('./identity-provider-interface.js')
const OrbitDBIdentityProvider = require('./orbit-db-identity-provider')
const Keystore = require('orbit-db-keystore')

const type = 'orbitdb'
const identityKeysPath = './orbitdb/identity/identitykeys'
const LRU = require('lru')
const identityKeysPath = './orbitdb/identity/keys'
shamb0t marked this conversation as resolved.
Show resolved Hide resolved
const defaultType = 'orbitdb'
const supportedTypes = {
orbitdb: OrbitDBIdentityProvider
}
Expand All @@ -18,79 +18,73 @@ const getHandlerFor = (type) => {
}

class Identities {
constructor (options) {
this._keystore = options.keystore
this._signingKeystore = options.signingKeystore || this._keystore
constructor (options = {}) {
this._keystore = options.keystore || new Keystore(options.identityKeysPath || identityKeysPath)
this._knownIdentities = options.cache || new LRU(options.cacheSize || 1000)
}

static get IdentityProvider () { return IdentityProvider }

get keystore () { return this._keystore }

get signingKeystore () { return this._signingKeystore }

async sign (identity, data) {
const signingKey = await this._keystore.getKey(identity.id)
const keystore = this.keystore
const signingKey = await keystore.getKey(identity.id)
if (!signingKey) {
throw new Error(`Private signing key not found from Keystore`)
}
const sig = await this._keystore.sign(signingKey, data)
return sig
}

async verify (signature, publicKey, data, verifier = 'v1') {
return this._keystore.verify(signature, publicKey, data, verifier)
return keystore.sign(signingKey, data)
}

async createIdentity (options = {}) {
const IdentityProvider = getHandlerFor(options.type)
const identityProvider = new IdentityProvider(options)
let identityProvider
const keystore = options.keystore || this.keystore
const type = options.type || defaultType
if (type === defaultType) {
identityProvider = new OrbitDBIdentityProvider(options.signingKeystore || keystore)
} else {
const IdentityProvider = getHandlerFor(type)
identityProvider = new IdentityProvider(options)
shamb0t marked this conversation as resolved.
Show resolved Hide resolved
}
const id = await identityProvider.getId(options)

if (options.migrate) {
await options.migrate({ targetStore: this._keystore._store, targetId: id })
await options.migrate({ targetStore: keystore._store, targetId: id })
}
const { publicKey, idSignature } = await this.signId(id)
const { publicKey, idSignature } = await this.signId(id, keystore)
const pubKeyIdSignature = await identityProvider.signIdentity(publicKey + idSignature, options)
return new Identity(id, publicKey, idSignature, pubKeyIdSignature, IdentityProvider.type, this)
return new Identity(id, publicKey, idSignature, pubKeyIdSignature, type)
}

async signId (id) {
const keystore = this._keystore
async signId (id, keystore) {
const key = await keystore.getKey(id) || await keystore.createKey(id)
const publicKey = keystore.getPublic(key)
const idSignature = await keystore.sign(key, id)
return { publicKey, idSignature }
}

async verifyIdentity (identity) {
const verified = await this._keystore.verify(
const knownID = this._knownIdentities.get(identity.signatures.id)
if (knownID) {
return identity.id === knownID.id &&
identity.publicKey === knownID.publicKey &&
identity.signatures.id === knownID.signatures.id &&
identity.signatures.publicKey === knownID.signatures.publicKey
}
const verified = await Identities.verifyIdentity(identity, this.keystore)
if (verified) {
this._knownIdentities.set(identity.signatures.id, identity)
}
return verified
}

static async verifyIdentity (identity, keystore) {
const verifyId = await keystore.verify(
identity.signatures.id,
identity.publicKey,
identity.id
)
return verified && Identities.verifyIdentity(identity)
}

static async verifyIdentity (identity) {
const IdentityProvider = getHandlerFor(identity.type)
return IdentityProvider.verifyIdentity(identity)
}

static async createIdentity (options = {}) {
if (!options.keystore) {
options.keystore = new Keystore(options.identityKeysPath || identityKeysPath)
}
if (!options.signingKeystore) {
if (options.signingKeysPath) {
options.signingKeystore = new Keystore(options.signingKeysPath)
} else {
options.signingKeystore = options.keystore
}
}
options = Object.assign({}, { type }, options)
const identities = new Identities(options)
return identities.createIdentity(options)
return verifyId && IdentityProvider.verifyIdentity(identity)
}

static isSupported (type) {
Expand Down
Loading