const eccrypto = require('eccrypto');
// Generate a message to sign and verify
const message = Buffer.from('Hello, world!');
// Bob's private key (hardcoded)
const bobPrivateKeyHex = 'bd6c823fe05c43c2eb06e7eb194d3a3dfb9dd2ba3a524f64b9b543ad20e4c3d2';
// Bob's public key (obtained securely, not generated here)
const bobPublicKeyHex = '04be97a0a70821f4c6a3b60476a4e0dd3a09a34878c8ae03936ebd1ff5e3c8fbc3';
(async () => {
try {
// Convert private and public keys to buffers
const bobPrivateKey = Buffer.from(bobPrivateKeyHex, 'hex');
const bobPublicKey = Buffer.from(bobPublicKeyHex, 'hex');
// Sign the message using Bob's private key
const signature = await eccrypto.sign(bobPrivateKey, message);
// Verify the signature with Bob's public key
const isValidSignature = await eccrypto.verify(bobPublicKey, message, signature);
// Check if the signature is valid
if (isValidSignature) {
console.log('The public key matches the private key.');
} else {
console.log('The public key does not match the private key.');
}
} catch (error) {
console.error('Error:', error);
}
})();
const eccrypto = require('eccrypto');
// Generate a new private key
const newPrivateKey = eccrypto.generatePrivate();
// Derive the corresponding public key from the private key
const newPublicKey = eccrypto.getPublic(newPrivateKey);
// Convert keys to hexadecimal strings for storage or transmission
const newPrivateKeyHex = newPrivateKey.toString('hex');
const newPublicKeyHex = newPublicKey.toString('hex');
// Print the new keys
console.log('New private key:', newPrivateKeyHex);
console.log('New public key:', newPublicKeyHex);