From 2b4f5e5f0999459c0552a405a23c44f84eaa898b Mon Sep 17 00:00:00 2001 From: james-a-morris Date: Mon, 25 Sep 2023 14:51:03 -0400 Subject: [PATCH] improve: add custom encrypt/decrypt --- src/helpers/oshhhnap.ts | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/helpers/oshhhnap.ts diff --git a/src/helpers/oshhhnap.ts b/src/helpers/oshhhnap.ts new file mode 100644 index 00000000000..9585d8dd526 --- /dev/null +++ b/src/helpers/oshhhnap.ts @@ -0,0 +1,64 @@ +import { timelockDecrypt, timelockEncrypt } from 'tlock-js'; +import { HttpChainClient, HttpCachingChain } from 'drand-client'; + +const testnetUnchainedUrl = + 'https://pl-eu.testnet.drand.sh/7672797f548f3f4748ac4bf3352fc6c6b6468c9ad40ad456a397545c6e2df5bf'; + +const getFastestNode = async () => { + const chain = new HttpCachingChain(testnetUnchainedUrl); + const client = new HttpChainClient(chain); + + return client; +}; + +export async function timelockEncryption(message: string, duration: number) { + if (duration < 30) + throw new Error( + 'Duration must be positive and greater or equal to 30 seconds' + ); + const fastestNodeClient = await getFastestNode(); + const latestRound = await fastestNodeClient.latest(); + const chain = new HttpCachingChain(testnetUnchainedUrl); + + const { period } = await chain.info(); + + const roundNumber = latestRound.round + Math.floor(duration / period); + + const result = await timelockEncrypt( + latestRound.round + roundNumber, + Buffer.from(message), + fastestNodeClient + ); + + return result; +} + +export async function timelockEncryptionForOshhhnap( + message: string, + proposalId: string, + duration: number +) { + let result = await timelockEncryption(message, duration); + + // We need to append the ID to the ciphertext, so we can decrypt it later + // and know which proposal it belongs to. + result = `${result}__ID__${proposalId}`; + + return result; +} + +export async function timelockDecryption(ciphertext: string) { + const fastestNodeClient = await getFastestNode(); + const result = await timelockDecrypt(ciphertext, fastestNodeClient); + return result; +} + +export async function timelockDecryptionForOshhhnap(ciphertext: string) { + // We need to remove the ID from the ciphertext, so we can decrypt it. + const [ciphertextWithoutId, proposalId] = ciphertext.split('__ID__'); + const result = await timelockDecryption(ciphertextWithoutId); + return { + result, + proposalId + }; +}