Skip to content

Commit

Permalink
namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
javisantos committed Feb 11, 2021
1 parent 9c81807 commit 3757557
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
Binary file modified dist/faythe.br
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/faythe.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/faythe.js.map

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions src/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const secretEncryptErrorHandler = function (args) {

export class Identity extends EventEmitter {
constructor (idspace, opts = {}) {
// name, passphrase, mnemonic, rotation, seed
// namespace, passphrase, mnemonic, rotation, seed
super()
this.contents = []
this.encryptedContents = null
Expand All @@ -94,14 +94,14 @@ export class Identity extends EventEmitter {

this[MASTERKEY] = deriveFromKey(this[SEED], this.rotation, '_faythe_')

this.name = opts.name || 'default'
this.namespace = opts.namespace || 'default'

this.contents.push({
type: 'metadata',
description: opts.description || `Identity for ${encode(this.idspace)} (${this.name})`,
description: opts.description || `Identity for ${encode(this.idspace)} (${this.namespace})`,
tags: [].concat(opts.tags || []),
idspace: this.idspace,
name: this.name,
namespace: this.namespace,
rotation: this.rotation
})

Expand All @@ -110,9 +110,9 @@ export class Identity extends EventEmitter {
value: opts.seed ? entropyToMnemonic(opts.seed) : this[MNEMONIC]
})

this.keyPair = this.keyPairFor(this.idspace, this.name)
this.keyPair = this.keyPairFor(this.idspace, this.namespace)

this[ROTATIONKEY] = hash(generateKeyPair(derive(deriveFromKey(this[SEED], this.rotation + 1, '_faythe_'), this.idspace, this.name)).publicKey)
this[ROTATIONKEY] = hash(generateKeyPair(derive(deriveFromKey(this[SEED], this.rotation + 1, '_faythe_'), this.idspace, this.namespace)).publicKey)

this.on('change', () => this.export())
this.emit('unlocked')
Expand All @@ -121,24 +121,24 @@ export class Identity extends EventEmitter {
this.setMaxListeners(0)
}

keyPairFor (space, name, info = {}) {
keyPairFor (space, namespace, info = {}) {
if (!space) throw new Error('Idspace is required')
name = name || this.name
namespace = namespace || this.namespace

space = ensureBuffer(space)

if (!this.locked) {
const exist = this.contents.find(c => c.idspace === encode(space) && c.name === name)
const exist = this.contents.find(c => c.idspace === encode(space) && c.namespace === namespace)
const tags = ['keyPair', 'verification'].concat(info.tags || [])
const keyPair = generateKeyPair(derive(this.masterKey, space, name))
const description = info.description || `KeyPair for ${encode(space)} (${name})`
const keyPair = generateKeyPair(derive(this.masterKey, space, namespace))
const description = info.description || `KeyPair for ${encode(space)} (${namespace})`

const id = 'did:key:' + encode(multicodec.addPrefix('ed25519-pub', keyPair.publicKey), 'base58btc')
const kp = {
id,
type: VERIFICATIONKEYTYPE,
idspace: encode(space),
name,
namespace,
description,
tags,
rotation: this.rotation
Expand All @@ -164,7 +164,7 @@ export class Identity extends EventEmitter {
this.contents.push({
id,
type: 'connection',
name: id,
namespace: id,
offer,
state,
status: 'offered'
Expand All @@ -183,7 +183,7 @@ export class Identity extends EventEmitter {
this.contents.push({
id,
type: 'connection',
name: id,
namespace: id,
offer: offer,
sharedKeys,
keyPair,
Expand All @@ -195,7 +195,7 @@ export class Identity extends EventEmitter {
}

finish (id, accept, state) {
const restoreState = state || this.contents.find((c) => c.name === id).state
const restoreState = state || this.contents.find((c) => c.namespace === id).state
const hs = noise.initialize('NN', true, Buffer.alloc(0), null, { publicKey: restoreState.epk, secretKey: restoreState.esk })
hs.symmetricState = restoreState.symmetricState
hs.messagePatterns.shift()
Expand Down Expand Up @@ -326,7 +326,7 @@ export class Identity extends EventEmitter {
let identity
const idspace = metadata.idspace
const opts = {
name: metadata.name,
namespace: metadata.namespace,
rotation: metadata.rotation,
passphrase
}
Expand Down Expand Up @@ -365,11 +365,11 @@ export function sha256 (data) {
return b
}

export function derive (key, namespace, name) {
export function derive (key, idspace, namespace) {
const derived = Buffer.alloc(RANDOMBYTES)
sodium.crypto_generichash_batch(derived, [
Buffer.from(Buffer.byteLength(namespace, 'ascii') + '\n' + namespace, 'ascii'),
ensureBuffer(name)
Buffer.from(Buffer.byteLength(idspace, 'ascii') + '\n' + idspace, 'ascii'),
ensureBuffer(namespace)
], key)

return derived
Expand Down
26 changes: 13 additions & 13 deletions test/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ let alice, bob, charlie
})

test('Init (' + env + ')', async (t) => {
alice = alice || new faythe.Identity('test', { name: 'alice', passphrase: 'secret' })
alice = alice || new faythe.Identity('test', { namespace: 'alice', passphrase: 'secret' })

bob = bob || new faythe.Identity()
charlie = charlie || new faythe.Identity('test', { name: 'charlie', rotation: 1 })
charlie = charlie || new faythe.Identity('test', { namespace: 'charlie', rotation: 1 })
t.end()
})

test('fromMnemonic (' + env + ')', (t) => {
const alice2 = faythe.Identity.fromMnemonic('test', alice.mnemonic, { name: 'alice', passphrase: 'secret' })
const alice2 = faythe.Identity.fromMnemonic('test', alice.mnemonic, { namespace: 'alice', passphrase: 'secret' })
t.equal(alice.publicKey.toString('hex'), alice2.publicKey.toString('hex'), 'Should be the same')
t.equal(alice.privateKey.toString('hex'), alice2.secretKey.toString('hex'), 'Should be the same')

const bob2 = faythe.Identity.fromMnemonic(null, bob.mnemonic)
t.equal(bob.publicKey.toString('hex'), bob2.publicKey.toString('hex'), 'Should be the same')

const charlie2 = faythe.Identity.fromMnemonic('test', charlie.mnemonic, { name: 'charlie', rotation: 1 })
const charlie2 = faythe.Identity.fromMnemonic('test', charlie.mnemonic, { namespace: 'charlie', rotation: 1 })
t.equal(charlie.publicKey.toString('hex'), charlie2.publicKey.toString('hex'), 'Should be the same')
t.end()
})

test('fromEntropy (' + env + ')', (t) => {
const alice3 = faythe.Identity.fromEntropy('test', alice.entropy, { name: 'alice', passphrase: 'secret' })
const alice3 = faythe.Identity.fromEntropy('test', alice.entropy, { namespace: 'alice', passphrase: 'secret' })
t.equal(alice.publicKey.toString('hex'), alice3.publicKey.toString('hex'), 'Should be the same')
const alice4 = faythe.Identity.fromMnemonic('test', alice3.mnemonic, { name: 'alice', passphrase: 'secret' })
const alice4 = faythe.Identity.fromMnemonic('test', alice3.mnemonic, { namespace: 'alice', passphrase: 'secret' })
t.equal(alice3.publicKey.toString('hex'), alice4.publicKey.toString('hex'), 'Should be the same')
try {
const _ = faythe.Identity.fromMnemonic(null)
Expand All @@ -54,17 +54,17 @@ let alice, bob, charlie

test('fromSeedPhrase (' + env + ')', (t) => {
const seedPhrase = alice.seedPhrase
const alice2 = faythe.Identity.fromSeedPhrase('test', seedPhrase, { name: 'alice' })
const alice2 = faythe.Identity.fromSeedPhrase('test', seedPhrase, { namespace: 'alice' })
t.equal(alice2.publicKey.toString('hex'), alice.publicKey.toString('hex'), 'Should be the same')
t.end()
})

test('export (' + env + ')', (t) => {
const exported = alice.export()
const imported = alice.import(exported)
const c = imported.find((c) => c.name === 'alice' && c.type === 'Ed25519VerificationKey2018')
const c = imported.find((c) => c.namespace === 'alice' && c.type === 'Ed25519VerificationKey2018')
t.equal(
alice.keyPairFor(faythe.decode(c.idspace), c.name).publicKey.toString('hex'),
alice.keyPairFor(faythe.decode(c.idspace), c.namespace).publicKey.toString('hex'),
alice.publicKey.toString('hex'), 'Should import and export')
t.end()
})
Expand All @@ -78,12 +78,12 @@ let alice, bob, charlie

test('restore seeded (' + env + ')', (t) => {
const seedPhrase = faythe.entropyToMnemonic(alice.seed)
const alice2 = faythe.Identity.fromSeedPhrase('test', seedPhrase, { name: 'alice', passphrase: 'othersecret' })
const alice2 = faythe.Identity.fromSeedPhrase('test', seedPhrase, { namespace: 'alice', passphrase: 'othersecret' })
const encryptedContent = alice2.export()
const alice3 = faythe.Identity.restore(encryptedContent, 'othersecret')
t.equal(alice2.publicKey.toString('hex'), alice3.publicKey.toString('hex'), 'Should be the same')

const alice4 = faythe.Identity.fromSeed('test', alice3.seed, { name: 'alice', passphrase: 'othersecret' })
const alice4 = faythe.Identity.fromSeed('test', alice3.seed, { namespace: 'alice', passphrase: 'othersecret' })
t.equal(alice2.publicKey.toString('hex'), alice4.publicKey.toString('hex'), 'Should be the same')

t.equal(alice2.mnemonic, null, 'Seeded identity no mnemonic')
Expand Down Expand Up @@ -121,7 +121,7 @@ let alice, bob, charlie

test('rotation (' + env + ')', (t) => {
const rotationKey = alice.rotationKey.toString('hex')
const alice1 = faythe.Identity.fromEntropy('test', alice.entropy, { name: 'alice', passphrase: 'secret', rotation: 1 })
const alice1 = faythe.Identity.fromEntropy('test', alice.entropy, { namespace: 'alice', passphrase: 'secret', rotation: 1 })
t.equal(faythe.hash(alice1.publicKey).toString('hex'), rotationKey, 'Should be the same!!')
t.end()
})
Expand Down Expand Up @@ -411,7 +411,7 @@ let alice, bob, charlie
const { offer } = alice.offer('bob')
const accept = bob.accept('alice', offer)
alice.finish('bob', accept)
t.equal(alice.contents.find((c) => c.name === 'bob').sharedKeys.tx.toString('hex'), bob.contents.find((c) => c.name === 'alice').sharedKeys.rx.toString('hex'), 'Should have same shared key')
t.equal(alice.contents.find((c) => c.namespace === 'bob').sharedKeys.tx.toString('hex'), bob.contents.find((c) => c.namespace === 'alice').sharedKeys.rx.toString('hex'), 'Should have same shared key')
t.end()
})
})

0 comments on commit 3757557

Please sign in to comment.