mirror of
https://github.com/RoboSats/robosats.git
synced 2025-08-07 00:20:24 +00:00

commit a5b63aed93e084fae19d9e444e06238a52f24f3a Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Sun Oct 30 10:46:05 2022 -0700 Small fixes commit d64adfc2bf9b9c31dca47ab113c06a1268c347c6 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Sun Oct 30 06:02:06 2022 -0700 wip work on federation settings commit ca35d6b3d2776812b07109e197d2e1d46f9f4e81 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Sun Oct 30 04:05:33 2022 -0700 Refactor confirmation Dialogs commit c660a5b0d1345d4996efb10cb8999987689bede9 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Sat Oct 29 13:36:59 2022 -0700 refactor login (clean separation robot/info. Style navbar. commit b9dc7f7c95a683e3aca024ec6d7857176b4e3a25 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Fri Oct 28 09:54:38 2022 -0700 Add size slider and settings widget commit 20b2b3dcd6838b129741705f1c65d445271e231d Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Fri Oct 28 05:41:48 2022 -0700 Add show more and Dialogs commit da8b70091b5f28139cdec1a8895f4563d64d8e88 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 27 16:26:07 2022 -0700 Add sliding pages commit 6dd90aa1182a7a5e0f0189d1467ba474b68c28c2 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Thu Oct 27 06:34:58 2022 -0700 Add settings forms commit d3d0f3ee1a52bbf1829714050cc798d2542af8f6 Author: Reckless_Satoshi <reckless.satoshi@protonmail.com> Date: Wed Oct 26 04:16:06 2022 -0700 Refactor utils
85 lines
2.9 KiB
JavaScript
85 lines
2.9 KiB
JavaScript
import {
|
|
generateKey,
|
|
readKey,
|
|
readPrivateKey,
|
|
decryptKey,
|
|
encrypt,
|
|
decrypt,
|
|
createMessage,
|
|
readMessage,
|
|
} from 'openpgp/lightweight';
|
|
import { sha256 } from 'js-sha256';
|
|
|
|
// Generate KeyPair. Private Key is encrypted with the highEntropyToken
|
|
export async function genKey(highEntropyToken) {
|
|
const d = new Date();
|
|
const keyPair = await generateKey({
|
|
type: 'ecc', // Type of the key, defaults to ECC
|
|
curve: 'curve25519', // ECC curve name, defaults to curve25519
|
|
userIDs: [{ name: 'RoboSats ID ' + sha256(sha256(highEntropyToken)) }], // Ideally it would be the avatar nickname, but the nickname is generated only after submission. The second SHA256 can be converted into the Nickname using nick_generator package.
|
|
passphrase: highEntropyToken,
|
|
format: 'armored',
|
|
date: d.setDate(d.getDate() - 1), // One day of offset. Helps reducing errors due to client's system time being in the future.
|
|
});
|
|
|
|
return { publicKeyArmored: keyPair.publicKey, encryptedPrivateKeyArmored: keyPair.privateKey };
|
|
}
|
|
|
|
// Encrypt and sign a message
|
|
export async function encryptMessage(
|
|
plaintextMessage,
|
|
ownPublicKeyArmored,
|
|
peerPublicKeyArmored,
|
|
privateKeyArmored,
|
|
passphrase,
|
|
) {
|
|
const ownPublicKey = await readKey({ armoredKey: ownPublicKeyArmored });
|
|
const peerPublicKey = await readKey({ armoredKey: peerPublicKeyArmored });
|
|
const privateKey = await decryptKey({
|
|
privateKey: await readPrivateKey({ armoredKey: privateKeyArmored }),
|
|
passphrase,
|
|
});
|
|
|
|
const d = new Date();
|
|
const encryptedMessage = await encrypt({
|
|
message: await createMessage({ text: plaintextMessage }), // input as Message object, message must be string
|
|
encryptionKeys: [ownPublicKey, peerPublicKey],
|
|
signingKeys: privateKey, // optional
|
|
date: d.setDate(d.getDate() - 1), // One day of offset, avoids verification issue due to clock mismatch
|
|
});
|
|
|
|
return encryptedMessage; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
|
|
}
|
|
|
|
// Decrypt and check signature of a message
|
|
export async function decryptMessage(
|
|
encryptedMessage,
|
|
publicKeyArmored,
|
|
privateKeyArmored,
|
|
passphrase,
|
|
) {
|
|
const publicKey = await readKey({ armoredKey: publicKeyArmored });
|
|
const privateKey = await decryptKey({
|
|
privateKey: await readPrivateKey({ armoredKey: privateKeyArmored }),
|
|
passphrase,
|
|
});
|
|
|
|
const message = await readMessage({
|
|
armoredMessage: encryptedMessage, // parse armored message
|
|
});
|
|
const { data: decrypted, signatures } = await decrypt({
|
|
message,
|
|
verificationKeys: publicKey, // optional
|
|
decryptionKeys: privateKey,
|
|
});
|
|
|
|
// check signature validity (signed messages only)
|
|
try {
|
|
await signatures[0].verified; // throws on invalid signature
|
|
console.log('Signature is valid');
|
|
return { decryptedMessage: decrypted, validSignature: true };
|
|
} catch (e) {
|
|
return { decryptedMessage: decrypted, validSignature: false };
|
|
}
|
|
}
|