Get the message and decypher it

import tweetnacl from 'tweetnacl';
import { Wallet } from "xrpl";
import {
edwardsToMontgomeryPub,
edwardsToMontgomeryPriv,
} from "@noble/curves/ed25519";
const { box } = tweetnacl;
export function decryptMessage(
messageWithNonce: string,
recipientPublicKey: string,
senderSecretKey: string,
): string {
const pubKeyBytes = Buffer.from(recipientPublicKey.slice(2), "hex");
const secretKeyBytes = Buffer.from(senderSecretKey.slice(2), "hex");
const pubKeyCurve = edwardsToMontgomeryPub(pubKeyBytes);
const privKeyCurve = edwardsToMontgomeryPriv(secretKeyBytes);
console.log(messageWithNonce)
const { encrypted, nonce } = JSON.parse(messageWithNonce);
const messageBytes = Buffer.from(encrypted,"base64");
const nonceBytes = Buffer.from(nonce,"base64");
const decryptedMessage = box.open(
messageBytes,
nonceBytes,
pubKeyCurve,
privKeyCurve,
);
if (!decryptedMessage) {
throw new Error("Failed to decrypt message");
}
return new TextDecoder().decode(decryptedMessage);
}
function main() {
const cypherMessage =
"Your cypher message goes here";
const senderPubKey =
"The sender public key goes here";
const mySeed = "your seed goes here";
const myPrivateKey = Wallet.fromSeed(mySeed).privateKey;
const clearMessage = decryptMessage(
Buffer.from(cypherMessage, "hex").toString(),
senderPubKey,
myPrivateKey,
);
console.log("==============CLEAR MESSAGE==============");
console.log(clearMessage);
console.log("all done");
}
main();
Last updated

