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.
feat: use noble-secp256k1 and noble-ed25519 (#202)
BREAKING CHANGE: keys function hashAndVerify returns boolean false when fail, instead of throwing error
- Loading branch information
1 parent
2e40aea
commit 167eace
Showing
7 changed files
with
122 additions
and
57 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 |
---|---|---|
@@ -1,24 +1,68 @@ | ||
'use strict' | ||
|
||
require('node-forge/lib/ed25519') | ||
const forge = require('node-forge/lib/forge') | ||
exports.publicKeyLength = forge.pki.ed25519.constants.PUBLIC_KEY_BYTE_LENGTH | ||
exports.privateKeyLength = forge.pki.ed25519.constants.PRIVATE_KEY_BYTE_LENGTH | ||
const ed = require('noble-ed25519') | ||
|
||
exports.generateKey = async function () { // eslint-disable-line require-await | ||
return forge.pki.ed25519.generateKeyPair() | ||
const PUBLIC_KEY_BYTE_LENGTH = 32 | ||
const PRIVATE_KEY_BYTE_LENGTH = 64 // private key is actually 32 bytes but for historical reasons we concat private and public keys | ||
const KEYS_BYTE_LENGTH = 32 | ||
|
||
exports.publicKeyLength = PUBLIC_KEY_BYTE_LENGTH | ||
exports.privateKeyLength = PRIVATE_KEY_BYTE_LENGTH | ||
|
||
exports.generateKey = async function () { | ||
// the actual private key (32 bytes) | ||
const privateKeyRaw = ed.utils.randomPrivateKey() | ||
const publicKey = await ed.getPublicKey(privateKeyRaw) | ||
|
||
// concatenated the public key to the private key | ||
const privateKey = concatKeys(privateKeyRaw, publicKey) | ||
|
||
return { | ||
privateKey, | ||
publicKey | ||
} | ||
} | ||
|
||
/** | ||
* Generate keypair from a seed | ||
* | ||
* @param {Uint8Array} seed - seed should be a 32 byte uint8array | ||
* @returns | ||
*/ | ||
exports.generateKeyFromSeed = async function (seed) { | ||
if (seed.length !== KEYS_BYTE_LENGTH) { | ||
throw new TypeError('"seed" must be 32 bytes in length.') | ||
} else if (!(seed instanceof Uint8Array)) { | ||
throw new TypeError('"seed" must be a node.js Buffer, or Uint8Array.') | ||
} | ||
|
||
// based on node forges algorithm, the seed is used directly as private key | ||
const privateKeyRaw = seed | ||
const publicKey = await ed.getPublicKey(privateKeyRaw) | ||
|
||
const privateKey = concatKeys(privateKeyRaw, publicKey) | ||
|
||
return { | ||
privateKey, | ||
publicKey | ||
} | ||
} | ||
|
||
// seed should be a 32 byte uint8array | ||
exports.generateKeyFromSeed = async function (seed) { // eslint-disable-line require-await | ||
return forge.pki.ed25519.generateKeyPair({ seed }) | ||
exports.hashAndSign = function (privateKey, msg) { | ||
const privateKeyRaw = privateKey.slice(0, KEYS_BYTE_LENGTH) | ||
|
||
return ed.sign(msg, privateKeyRaw) | ||
} | ||
|
||
exports.hashAndSign = async function (key, msg) { // eslint-disable-line require-await | ||
return forge.pki.ed25519.sign({ message: msg, privateKey: key }) | ||
// return Uint8Array.from(nacl.sign.detached(msg, key)) | ||
exports.hashAndVerify = function (publicKey, sig, msg) { | ||
return ed.verify(sig, msg, publicKey) | ||
} | ||
|
||
exports.hashAndVerify = async function (key, sig, msg) { // eslint-disable-line require-await | ||
return forge.pki.ed25519.verify({ signature: sig, message: msg, publicKey: key }) | ||
function concatKeys (privateKeyRaw, publicKey) { | ||
const privateKey = new Uint8Array(exports.privateKeyLength) | ||
for (let i = 0; i < KEYS_BYTE_LENGTH; i++) { | ||
privateKey[i] = privateKeyRaw[i] | ||
privateKey[KEYS_BYTE_LENGTH + i] = publicKey[i] | ||
} | ||
return privateKey | ||
} |
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
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
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