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
feat: use noble-secp256k1 and noble-ed25519 #202
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2c641c0
feat: use noble-secp256k1
hugomrdias 1d1dd68
fix: error codes
hugomrdias 790536f
fix: linter
hugomrdias 9e26a6a
fix: use noble-ed25519
hugomrdias bded28c
fix ed25519 integration
dignifiedquire 455ce4f
fix: just use globalThis.crypto
hugomrdias 19f6f54
fix: web crypto error
hugomrdias 5cd1b6f
fix: linter
hugomrdias 2770645
fix: reduce size limit
hugomrdias c162e46
Merge branch 'master' into feat/noble-secp
achingbrain 43e775f
fix: fix tests and errors for hashAndVerify
hugomrdias 562d5e1
fix: feedback
hugomrdias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why removing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just moved the test up a bit check 'does not validate when validating a message with an invalid signature', because it doesnt throw now just returns false for an invalid sig
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, so this now returns a boolean
false
instead of throwing an error. Can we flag that breaking change in the commit message?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you do it when you squash and merge ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, I can do