Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const publickey = async (sk) => {
await sodiumsumo.ready;
const seed = sodiumsumo.crypto_sign_ed25519_sk_to_seed(sk)
return sodiumsumo.crypto_sign_seed_keypair(seed, '');
}
if (typeof sk === 'function') {
cb = sk;
sk = null;
}
const done = ret(cb);
let payload, isk;
[ payload ] = iparse(message);
[ sk ] = cparse(sk);
switch (sk && Buffer.byteLength(sk)) {
case sodium.crypto_sign_SECRETKEYBYTES:
isk = sk;
break;
case sodium.crypto_sign_SEEDBYTES:
isk = sodium.crypto_sign_seed_keypair(sk).privateKey;
break;
default:
isk = sodium.crypto_sign_keypair().privateKey;
sk = sodium.crypto_sign_ed25519_sk_to_seed(isk);
}
let signature;
try {
signature = sodium.crypto_sign_detached(payload, isk);
} catch(ex) {
return done(new Error('Libsodium error: ' + ex));
}
return done(null, convert({
alg: 'ed25519',
sk: sk,
function vsign(message, pk, signature, ispk, cb) {
if (typeof ispk === 'function') {
cb = ispk;
ispk = false;
}
const done = ret(cb);
if (!pk) { return done(new Error('Cannot verify without a key')); }
let payload, received, ipk;
[ payload ] = iparse(message);
[ pk, received ] = cparse(pk, signature);
ipk = (ispk) ? pk : sodium.crypto_sign_seed_keypair(pk).publicKey;
let verified;
try {
verified = sodium.crypto_sign_verify_detached(received, payload, ipk);
} catch(ex) {
return done(new Error('Libsodium error: ' + ex));
}
if (!verified) { return done(new Error('Invalid signature')); }
return done();
}
const keys = async (seed) => {
await sodiumsumo.ready;
return sodiumsumo.crypto_sign_seed_keypair(seed, '');
}
if (self.protocol() instanceof V2) {
if (!(rkey instanceof Buffer)) { return done(new TypeError('Raw key must be provided as a buffer')); }
const len = Buffer.byteLength(rkey);
if (len === sodium.crypto_sign_KEYPAIRBYTES) {
rkey = rkey.slice(0, sodium.crypto_sign_SECRETKEYBYTES);
} else if (len !== sodium.crypto_sign_SECRETKEYBYTES) {
if (len !== sodium.crypto_sign_SEEDBYTES) {
throw new Error('Secret keys must be 32 or 64 bytes long; ' + len + ' given.')
}
rkey = Buffer.from(sodium.crypto_sign_seed_keypair(rkey).privateKey);
}
}
self._key = rkey;
return done();
});
}