Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Upgrade libp2p-crypto-secp256k1 + fix Secp256k1 tests #151

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
60 changes: 24 additions & 36 deletions test/keys/secp256k1.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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()
})
})