mirror of
https://github.com/RoboSats/robosats.git
synced 2024-12-26 05:43:34 +03:00
Add utility functions for keypair generation, encrypt and decrypt messages
This commit is contained in:
parent
ae4d6a1ac0
commit
59d8d325b2
5
frontend/package-lock.json
generated
5
frontend/package-lock.json
generated
@ -9248,6 +9248,11 @@
|
||||
"@sideway/pinpoint": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"js-sha256": {
|
||||
"version": "0.9.0",
|
||||
"resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz",
|
||||
"integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA=="
|
||||
},
|
||||
"js-tokens": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
|
||||
|
@ -52,6 +52,7 @@
|
||||
"i18next-browser-languagedetector": "^6.1.4",
|
||||
"i18next-http-backend": "^1.4.0",
|
||||
"i18next-xhr-backend": "^3.2.2",
|
||||
"js-sha256": "^0.9.0",
|
||||
"material-ui-image": "^3.3.2",
|
||||
"openpgp": "^5.2.1",
|
||||
"react": "^18.0.0",
|
||||
|
66
frontend/src/utils/pgp.js
Normal file
66
frontend/src/utils/pgp.js
Normal file
@ -0,0 +1,66 @@
|
||||
import * as openpgp from 'openpgp/lightweight';
|
||||
|
||||
// Generate KeyPair. Private Key is encrypted with the highEntropyToken
|
||||
export async function genKeys(highEntropyToken) {
|
||||
|
||||
const keyPair = await openpgp.generateKey({
|
||||
type: 'ecc', // Type of the key, defaults to ECC
|
||||
curve: 'curve25519', // ECC curve name, defaults to curve25519
|
||||
userIDs: [{name: 'RoboSats Avatar'}],
|
||||
passphrase: highEntropyToken,
|
||||
format: 'armored'
|
||||
})
|
||||
|
||||
console.log(keyPair)
|
||||
|
||||
const publicKeyArmored = keyPair.publicKey;
|
||||
const privateKeyArmored = keyPair.privateKey; // encrypted private key
|
||||
|
||||
return {publicKeyArmored: publicKeyArmored, privateKeyArmored: privateKeyArmored}
|
||||
};
|
||||
|
||||
// Encrypt and sign a message
|
||||
export async function encryptMessage(plainMessage, publicKeyArmored, privateKeyArmored, passphrase) {
|
||||
|
||||
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
||||
const privateKey = await openpgp.decryptKey({
|
||||
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
|
||||
passphrase
|
||||
});
|
||||
|
||||
const encryptedMessage = await openpgp.encrypt({
|
||||
message: await openpgp.createMessage({ text: plainMessage }), // input as Message object, message must be string
|
||||
encryptionKeys: publicKey,
|
||||
signingKeys: privateKey // optional
|
||||
});
|
||||
|
||||
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 openpgp.readKey({ armoredKey: publicKeyArmored });
|
||||
const privateKey = await openpgp.decryptKey({
|
||||
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
|
||||
passphrase
|
||||
});
|
||||
|
||||
const message = await openpgp.readMessage({
|
||||
armoredMessage: encryptedMessage // parse armored message
|
||||
});
|
||||
const { data: decrypted, signatures } = await openpgp.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};
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user