@crpdo/key
Version:
Streamlines key generation, derivation, and management through its simple and intuitive API
100 lines (86 loc) • 3.02 kB
JavaScript
const Key = require('../lib/key')
const seed = 'seed'
const data = 'foo'
const encrypted = '3UATe4tTkk731QzYn5dN8ZTN5une'
const signature = 'n7xQBorqS3NdBGqrxHyQJwy3S8tFXSW2ckwpQfieo7NHz8SWQVGnEksBJ2M3TNw5wvUbcJLnUPynK4REutYz4LQ'
const derivationSlug = 'slug'
describe('Key', () => {
const key = new Key(seed)
describe('#constructor()', () => {
it('should create a Key object with default parameters', () => {
const key = new Key()
expect(key).to.be.an('object')
expect(key).to.be.an.instanceOf(Key)
expect(key.hd).to.be.an.instanceOf(Key.Hd)
expect(key.hd).to.be.an.instanceOf(Key.Base)
expect(key.sig).to.be.an.instanceOf(Key.Base)
expect(key.sig).to.be.an.instanceOf(Key.Sign)
expect(key.box).to.be.an.instanceOf(Key.Base)
expect(key.box).to.be.an.instanceOf(Key.Box)
})
it('should construct and use the methods properly', () => {
const key = new Key({ seed })
const encryptedData = key.encrypt(data)
const decryptedData = key.decrypt(encryptedData)
const derivedKey = key.derive(derivationSlug)
const ratchetKey = key.ratchet(0)
const signature = key.sign(data)
const isValidSignature = key.verify(data, signature)
expect(decryptedData).to.equal(data)
expect(isValidSignature).to.be.true
})
})
describe('#derive()', () => {
it('should derive a new child key', () => {
const key = new Key()
const derivedKey = key.derive(derivationSlug)
expect(derivedKey).to.be.an.instanceOf(Key)
expect(key.children).to.have.property(derivationSlug)
})
})
describe('#ratchet()', () => {
it('should derive a new key at the specified index', () => {
const key = new Key()
const ratchetKey = key.ratchet(1)
expect(ratchetKey).to.be.an.instanceOf(Key)
expect(key.keys).to.have.property(`1'`)
})
})
describe('#publicKey', () => {
it('should get the public key', () => {
const publicKey = key.publicKey
expect(publicKey).to.be.a('string')
})
})
describe('#privateKey', () => {
it('should get the private key', () => {
const privateKey = key.privateKey
expect(privateKey).to.be.a('string')
})
})
describe('#encrypt()', () => {
it('should encrypt the data', () => {
const encryptedData = key.encrypt(data)
expect(encryptedData).to.be.an('string')
expect(encryptedData).to.equal(encrypted)
})
})
describe('#decrypt()', () => {
it('should decrypt the data', () => {
const decryptedData = key.decrypt(encrypted)
expect(decryptedData).to.be.equal(data)
})
})
describe('#sign()', () => {
it('should sign the data', () => {
const signature = key.sign(data)
expect(signature).to.be.an('string')
})
})
describe('#verify()', () => {
it('should verify the signature', () => {
const isValidSignature = key.verify(data, signature, key.publicKey)
expect(isValidSignature).to.be.true
})
})
})