This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.