Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initial ui hook for oshhhnap #4227

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"ajv-formats": "^2.1.1",
"autolinker": "^4.0.0",
"bluebird": "^3.7.2",
"drand-client": "^1.2.1",
"graphql": "^16.6.0",
"graphql-tag": "^2.12.6",
"js-sha256": "^0.9.0",
Expand All @@ -62,6 +63,7 @@
"readable-stream": "3.6.0",
"remarkable": "^2.0.1",
"remove-markdown": "^0.5.0",
"tlock-js": "^0.7.0",
"typescript": "^5.0.4",
"v-viewer": "^3.0.11",
"vue": "^3.3.4",
Expand Down
44 changes: 37 additions & 7 deletions src/components/ModalVote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { shorten, getChoiceString, explorerUrl } from '@/helpers/utils';
import { getPower, voteValidation } from '@/helpers/snapshot';
import { ExtendedSpace, Proposal } from '@/helpers/interfaces';
import shutterEncryptChoice from '@/helpers/shutter';
import { timelockEncryptionForOshhhnap } from '@/helpers/oshhhnap';

const { web3Account } = useWeb3();

Expand Down Expand Up @@ -33,6 +34,7 @@ const { addVotedProposalId } = useProposals();
const { isGnosisAndNotSpaceNetwork } = useGnosis(props.space);

const isLoadingShutter = ref(false);
const isLoadingOshhnap = ref(false);

const symbols = computed(() =>
props.strategies.map(strategy => strategy.params.symbol || '')
Expand Down Expand Up @@ -69,19 +71,47 @@ async function voteShutter() {
});
}

async function voteOshhhnap() {
// TODO: Implement Oshhhnap
isLoadingOshhnap.value = true;
const timelockEncryptedChoice = await timelockEncryptionForOshhhnap(
JSON.stringify(props.selectedChoices),
props.proposal.id,
props.proposal.end
);

if (!timelockEncryptedChoice) return null;
const resultantVote = vote({
proposal: props.proposal,
choice: timelockEncryptedChoice,
privacy: 'oshhhnap',
reason: reason.value
});
isLoadingOshhnap.value = false;
return resultantVote;
}

async function vote(payload) {
return send(props.space, 'vote', payload);
}

async function handleSubmit() {
let result: { id: string; ipfs?: string } | null = null;
if (props.proposal.privacy === 'shutter') result = await voteShutter();
else
result = await vote({
proposal: props.proposal,
choice: props.selectedChoices,
reason: reason.value
});
switch (props.proposal.privacy) {
case 'shutter':
result = await voteShutter();
break;
case 'oshhhnap':
result = await voteOshhhnap();
break;
default:
result = await vote({
proposal: props.proposal,
choice: props.selectedChoices,
reason: reason.value
});
break;
}

console.log('Result', result);

Expand Down
64 changes: 64 additions & 0 deletions src/helpers/oshhhnap.ts
Original file line number Diff line number Diff line change
@@ -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
};
}
38 changes: 37 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,11 @@
"@motionone/dom" "^10.16.2"
tslib "^2.3.1"

"@noble/bls12-381@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@noble/bls12-381/-/bls12-381-1.4.0.tgz#4db0e1577ef85632347e86eee81062acdda6d65b"
integrity sha512-mIYqC2jMX7Lcu1QtU/FFftMPDSXNCdlGex6BSf5nPojHjnzzBgs1klFWpB1R8YjqHmOO9xrCzF19f2c42+z3vg==

"@noble/[email protected]", "@noble/curves@~1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.0.0.tgz#e40be8c7daf088aaf291887cbc73f43464a92932"
Expand All @@ -1488,6 +1493,13 @@
dependencies:
"@noble/hashes" "1.3.1"

"@noble/curves@^1.1.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35"
integrity sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==
dependencies:
"@noble/hashes" "1.3.2"

"@noble/[email protected]":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.1.2.tgz#e9e035b9b166ca0af657a7848eb2718f0f22f183"
Expand All @@ -1503,6 +1515,11 @@
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==

"@noble/[email protected]", "@noble/hashes@^1.3.1":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==

"@noble/[email protected]":
version "1.7.1"
resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-1.7.1.tgz#b251c70f824ce3ca7f8dc3df08d58f005cc0507c"
Expand Down Expand Up @@ -1948,7 +1965,7 @@
resolved "https://registry.yarnpkg.com/@stablelib/bytes/-/bytes-1.0.1.tgz#0f4aa7b03df3080b878c7dea927d01f42d6a20d8"
integrity sha512-Kre4Y4kdwuqL8BR2E9hV/R5sOrUj6NanZaZis0V6lX5yzqC3hBuVSDXUIBqQv/sCpmuWRiHLwqiT1pqqjuBXoQ==

"@stablelib/[email protected]":
"@stablelib/[email protected]", "@stablelib/chacha20poly1305@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@stablelib/chacha20poly1305/-/chacha20poly1305-1.0.1.tgz#de6b18e283a9cb9b7530d8767f99cde1fec4c2ee"
integrity sha512-MmViqnqHd1ymwjOQfghRKw2R/jMIGT3wySN7cthjXCBdO+qErNPUBnRzqNpnvIwg7JBCg3LdeCZZO4de/yEhVA==
Expand Down Expand Up @@ -6575,6 +6592,14 @@ download@^8.0.0:
p-event "^2.1.0"
pify "^4.0.1"

[email protected], drand-client@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/drand-client/-/drand-client-1.2.1.tgz#dceea2f3c908df37d22e2126bc6ebfa6bcd8f81c"
integrity sha512-ht7ku30RfqJDFVv1jVE6EaHTfu8w6DQ4BZbviLRqn3ZCp+8kI38XFwr61Y3GFEFBM0cAwfnTtgjJ9f541XIBqQ==
dependencies:
"@noble/curves" "^1.1.0"
buffer "^6.0.3"

drbg.js@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/drbg.js/-/drbg.js-1.0.1.tgz#3e36b6c42b37043823cdbc332d58f31e2445480b"
Expand Down Expand Up @@ -14472,6 +14497,17 @@ title-case@^2.1.0:
no-case "^2.2.0"
upper-case "^1.0.3"

tlock-js@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/tlock-js/-/tlock-js-0.7.0.tgz#cea856869cb2ec5102820ce8851200c6d643fc33"
integrity sha512-Liwhm39z0uCokT9shOLaz37p2xWpN/4EFqB7xd9tJ+sR0J9eparfvz1HcG2EbU8NBuiCKppGJT8kYLj4EOwd+g==
dependencies:
"@noble/bls12-381" "^1.4.0"
"@noble/hashes" "^1.3.1"
"@stablelib/chacha20poly1305" "^1.0.1"
buffer "^6.0.3"
drand-client "1.2.1"

tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
Expand Down