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

Commit

Permalink
fix: circular circular dep -> DI
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jul 22, 2017
1 parent 8401154 commit 0dcf1a6
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 185 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"license": "MIT",
"dependencies": {
"async": "^2.5.0",
"libp2p-crypto": "~0.9.2",
"multihashing-async": "~0.4.6",
"nodeify": "^1.0.1",
"safe-buffer": "^5.1.1",
Expand All @@ -37,6 +36,7 @@
"devDependencies": {
"aegir": "^11.0.2",
"benchmark": "^2.1.4",
"libp2p-crypto": "~0.9.4",
"chai": "^4.1.0",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2"
Expand Down
89 changes: 89 additions & 0 deletions src/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict'

const secp256k1 = require('secp256k1')
const multihashing = require('multihashing-async')
const setImmediate = require('async/setImmediate')

const HASH_ALGORITHM = 'sha2-256'

module.exports = (randomBytes) => {
const privateKeyLength = 32

function generateKey (callback) {
const done = (err, res) => setImmediate(() => callback(err, res))

let privateKey
do {
privateKey = randomBytes(32)
} while (!secp256k1.privateKeyVerify(privateKey))

done(null, privateKey)
}

function hashAndSign (key, msg, callback) {
const done = (err, res) => setImmediate(() => callback(err, res))

multihashing.digest(msg, HASH_ALGORITHM, (err, digest) => {
if (err) { return done(err) }

try {
const sig = secp256k1.sign(digest, key)
const sigDER = secp256k1.signatureExport(sig.signature)
return done(null, sigDER)
} catch (err) { done(err) }
})
}

function hashAndVerify (key, sig, msg, callback) {
const done = (err, res) => setImmediate(() => callback(err, res))

multihashing.digest(msg, HASH_ALGORITHM, (err, digest) => {
if (err) { return done(err) }
try {
sig = secp256k1.signatureImport(sig)
const valid = secp256k1.verify(digest, sig, key)
return done(null, valid)
} catch (err) { done(err) }
})
}

function compressPublicKey (key) {
if (!secp256k1.publicKeyVerify(key)) {
throw new Error('Invalid public key')
}
return secp256k1.publicKeyConvert(key, true)
}

function decompressPublicKey (key) {
return secp256k1.publicKeyConvert(key, false)
}

function validatePrivateKey (key) {
if (!secp256k1.privateKeyVerify(key)) {
throw new Error('Invalid private key')
}
}

function validatePublicKey (key) {
if (!secp256k1.publicKeyVerify(key)) {
throw new Error('Invalid public key')
}
}

function computePublicKey (privateKey) {
validatePrivateKey(privateKey)
return secp256k1.publicKeyCreate(privateKey)
}

return {
generateKey: generateKey,
privateKeyLength: privateKeyLength,
hashAndSign: hashAndSign,
hashAndVerify: hashAndVerify,
compressPublicKey: compressPublicKey,
decompressPublicKey: decompressPublicKey,
validatePrivateKey: validatePrivateKey,
validatePublicKey: validatePublicKey,
computePublicKey: computePublicKey
}
}
85 changes: 0 additions & 85 deletions src/crypto/index.js

This file was deleted.

177 changes: 88 additions & 89 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,118 @@
'use strict'

const multihashing = require('multihashing-async')
const crypto = require('./crypto')
const pbm = require('libp2p-crypto').keys.pbm

class Secp256k1PublicKey {
constructor (key) {
crypto.validatePublicKey(key)
this._key = key
}
module.exports = (keysProtobuf, randomBytes, crypto) => {
crypto = crypto || require('./crypto')(randomBytes)

verify (data, sig, callback) {
ensure(callback)
crypto.hashAndVerify(this._key, sig, data, callback)
}
class Secp256k1PublicKey {
constructor (key) {
crypto.validatePublicKey(key)
this._key = key
}

marshal () {
return crypto.compressPublicKey(this._key)
}
verify (data, sig, callback) {
ensure(callback)
crypto.hashAndVerify(this._key, sig, data, callback)
}

get bytes () {
return pbm.PublicKey.encode({
Type: pbm.KeyType.Secp256k1,
Data: this.marshal()
})
}
marshal () {
return crypto.compressPublicKey(this._key)
}

equals (key) {
return this.bytes.equals(key.bytes)
}
get bytes () {
return keysProtobuf.PublicKey.encode({
Type: keysProtobuf.KeyType.Secp256k1,
Data: this.marshal()
})
}

hash (callback) {
ensure(callback)
multihashing(this.bytes, 'sha2-256', callback)
}
}
equals (key) {
return this.bytes.equals(key.bytes)
}

class Secp256k1PrivateKey {
constructor (key, publicKey) {
this._key = key
this._publicKey = publicKey || crypto.computePublicKey(key)
crypto.validatePrivateKey(this._key)
crypto.validatePublicKey(this._publicKey)
hash (callback) {
ensure(callback)
multihashing(this.bytes, 'sha2-256', callback)
}
}

sign (message, callback) {
ensure(callback)
crypto.hashAndSign(this._key, message, callback)
}
class Secp256k1PrivateKey {
constructor (key, publicKey) {
this._key = key
this._publicKey = publicKey || crypto.computePublicKey(key)
crypto.validatePrivateKey(this._key)
crypto.validatePublicKey(this._publicKey)
}

get public () {
return new Secp256k1PublicKey(this._publicKey)
}
sign (message, callback) {
ensure(callback)
crypto.hashAndSign(this._key, message, callback)
}

get public () {
return new Secp256k1PublicKey(this._publicKey)
}

marshal () {
return this._key
}

marshal () {
return this._key
get bytes () {
return keysProtobuf.PrivateKey.encode({
Type: keysProtobuf.KeyType.Secp256k1,
Data: this.marshal()
})
}

equals (key) {
return this.bytes.equals(key.bytes)
}

hash (callback) {
ensure(callback)
multihashing(this.bytes, 'sha2-256', callback)
}
}

get bytes () {
return pbm.PrivateKey.encode({
Type: pbm.KeyType.Secp256k1,
Data: this.marshal()
})
function unmarshalSecp256k1PrivateKey (bytes, callback) {
callback(null, new Secp256k1PrivateKey(bytes), null)
}

equals (key) {
return this.bytes.equals(key.bytes)
function unmarshalSecp256k1PublicKey (bytes) {
return new Secp256k1PublicKey(bytes)
}

hash (callback) {
function generateKeyPair (_bits, callback) {
if (callback === undefined && typeof _bits === 'function') {
callback = _bits
}

ensure(callback)
multihashing(this.bytes, 'sha2-256', callback)
}
}

function unmarshalSecp256k1PrivateKey (bytes, callback) {
callback(null, new Secp256k1PrivateKey(bytes), null)
}
crypto.generateKey((err, privateKeyBytes) => {
if (err) { return callback(err) }

function unmarshalSecp256k1PublicKey (bytes) {
return new Secp256k1PublicKey(bytes)
}
let privkey
try {
privkey = new Secp256k1PrivateKey(privateKeyBytes)
} catch (err) { return callback(err) }

function generateKeyPair (_bits, cb) {
if (cb === undefined && typeof _bits === 'function') {
cb = _bits
callback(null, privkey)
})
}
ensure(cb)

crypto.generateKey((err, privateKeyBytes) => {
if (err) {
return cb(err)
}
let privkey
try {
privkey = new Secp256k1PrivateKey(privateKeyBytes)
} catch (err) {
cb(err)
return
function ensure (callback) {
if (typeof callback !== 'function') {
throw new Error('callback is required')
}

cb(null, privkey)
})
}

function ensure (cb) {
if (typeof cb !== 'function') {
throw new Error('callback is required')
}
}

module.exports = {
Secp256k1PublicKey,
Secp256k1PrivateKey,
unmarshalSecp256k1PrivateKey,
unmarshalSecp256k1PublicKey,
generateKeyPair
return {
Secp256k1PublicKey,
Secp256k1PrivateKey,
unmarshalSecp256k1PrivateKey,
unmarshalSecp256k1PublicKey,
generateKeyPair
}
}
Loading

0 comments on commit 0dcf1a6

Please sign in to comment.