Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { secretKey, publicKey } = naclKeypairFromSeed(stringToU8a(seedAlice));
// Encrypt message. Create Uint8Array's filled with random bytes of specified length
const secret = randomAsU8a(32);
const messagePreEncryption = stringToU8a('please send me DOTs');
const noncePreEncryption = randomAsU8a(24);
const { encrypted } = naclEncrypt(messagePreEncryption, secret, noncePreEncryption);
// Sign the message with a valid signature
const messageSignature = naclSign(encrypted, secretKey);
console.log(`Message signature: ${u8aToHex(messageSignature)}`);
// Validate that the message was correctly signed
const isValidSignature = naclVerify(encrypted, messageSignature, publicKey);
console.log(`Was the message correctly signed? ${isValidSignature}`);
}
useEffect((): void => {
let cryptoType: CryptoTypes = 'unknown';
let isValid = isValidPk && isValidSignature;
// We cannot just use the keyring verify since it may be an address. So here we first check
// for ed25519, if not valid, we try against sr25519 - if neither are valid, well, we have
// not been able to validate the signature
if (isValid && publicKey) {
let isValidSr = false;
let isValidEd = false;
try {
isValidEd = naclVerify(data, signature, publicKey);
} catch (error) {
// do nothing, already set to false
}
if (isValidEd) {
cryptoType = 'ed25519';
} else {
try {
isValidSr = schnorrkelVerify(data, signature, publicKey);
} catch (error) {
// do nothing, already set to false
}
if (isValidSr) {
cryptoType = 'sr25519';
} else {
instrument('ed25519_verify', (): number => {
const m = heap.get(msgPtr, msgLen);
const s = heap.get(sigPtr, 64);
const p = heap.get(pubkeyPtr, 32);
l.debug((): any[] => ['ed25519_verify', [msgPtr, msgLen, sigPtr, pubkeyPtr], '<-', { m, p, s }]);
try {
return naclVerify(m, s, p) ? 0 : 5;
} catch (error) {
return 5;
}
}),
// eslint-disable-next-line @typescript-eslint/camelcase
const verify = (type: KeypairType, message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean =>
isSr25519(type)
? schnorrkelVerify(message, signature, publicKey)
: naclVerify(message, signature, publicKey);