diff --git a/package.json b/package.json index cd1760c9..c7f1ef7c 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,8 @@ "bundlesize": "~0.18.0", "chai": "^4.2.0", "chai-string": "^1.5.0", - "dirty-chai": "^2.0.1" + "dirty-chai": "^2.0.1", + "sinon": "^7.3.2" }, "engines": { "node": ">=10.0.0", diff --git a/test/keys/secp256k1.spec.js b/test/keys/secp256k1.spec.js index 60af5d90..a40de231 100644 --- a/test/keys/secp256k1.spec.js +++ b/test/keys/secp256k1.spec.js @@ -5,34 +5,19 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) +const sinon = require('sinon') + const fixtures = require('../fixtures/secp256k1') const crypto = require('../../src') -const mockPublicKey = { - bytes: fixtures.pbmPublicKey -} - -const mockPrivateKey = { - bytes: fixtures.pbmPrivateKey, - public: mockPublicKey -} - -const mockSecp256k1Module = { - generateKeyPair (bits) { - return mockPrivateKey - }, - - unmarshalSecp256k1PrivateKey (buf) { - return mockPrivateKey - }, - - unmarshalSecp256k1PublicKey (buf) { - return mockPublicKey - } -} - describe('without libp2p-crypto-secp256k1 module present', () => { - crypto.keys.supportedKeys.secp256k1 = undefined + before(() => { + sinon.replace(crypto.keys.supportedKeys, 'secp256k1', null) + }) + + after(() => { + sinon.restore() + }) it('fails to generate a secp256k1 key', async () => { try { @@ -60,22 +45,15 @@ describe('without libp2p-crypto-secp256k1 module present', () => { }) describe('with libp2p-crypto-secp256k1 module present', () => { - let key - - before(async () => { - crypto.keys.supportedKeys.secp256k1 = mockSecp256k1Module - key = await crypto.keys.generateKeyPair('secp256k1', 256) - }) - - after(() => { - delete crypto.keys.secp256k1 - }) - - it('generates a valid key', () => { + it('generates a valid key', async () => { + const key = await crypto.keys.generateKeyPair('secp256k1', 256) expect(key).to.exist() }) it('protobuf encoding', async () => { + const key = await crypto.keys.generateKeyPair('secp256k1', 256) + expect(key).to.exist() + const keyMarshal = crypto.keys.marshalPrivateKey(key) const key2 = await crypto.keys.unmarshalPrivateKey(keyMarshal) const keyMarshal2 = crypto.keys.marshalPrivateKey(key2) @@ -89,4 +67,14 @@ describe('with libp2p-crypto-secp256k1 module present', () => { expect(pkMarshal).to.eql(pkMarshal2) }) + + it('unmarshals a secp256k1 private key', async () => { + const key = await crypto.keys.unmarshalPrivateKey(fixtures.pbmPrivateKey) + expect(key).to.exist() + }) + + it('unmarshals a secp256k1 public key', () => { + const key = crypto.keys.unmarshalPublicKey(fixtures.pbmPublicKey) + expect(key).to.exist() + }) })