* init p2p polling app
This commit is contained in:
42
app/utils/crypto.ts
Normal file
42
app/utils/crypto.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
// utils/crypto.ts
|
||||
export const generateUserKeyPair = async () => {
|
||||
return await window.crypto.subtle.generateKey(
|
||||
{
|
||||
name: "RSASSA-PKCS1-v1_5",
|
||||
modulusLength: 2048,
|
||||
publicExponent: new Uint8Array([1, 0, 1]), // 65537
|
||||
hash: "SHA-256",
|
||||
},
|
||||
true, // extractable
|
||||
["sign", "verify"]
|
||||
);
|
||||
};
|
||||
|
||||
export const signVote = async (data: any, privateKey: CryptoKey) => {
|
||||
const encoder = new TextEncoder();
|
||||
const encodedData = encoder.encode(JSON.stringify(data));
|
||||
|
||||
const signature = await window.crypto.subtle.sign(
|
||||
"RSASSA-PKCS1-v1_5",
|
||||
privateKey,
|
||||
encodedData
|
||||
);
|
||||
|
||||
// Convert to Base64 or Hex to store in Yjs easily
|
||||
return btoa(String.fromCharCode(...new Uint8Array(signature)));
|
||||
};
|
||||
|
||||
export const verifyVote = async (data: any, signatureStr: string, publicKey: CryptoKey) => {
|
||||
const encoder = new TextEncoder();
|
||||
const encodedData = encoder.encode(JSON.stringify(data));
|
||||
|
||||
// Convert Base64 back to Uint8Array
|
||||
const signature = Uint8Array.from(atob(signatureStr), c => c.charCodeAt(0));
|
||||
|
||||
return await window.crypto.subtle.verify(
|
||||
"RSASSA-PKCS1-v1_5",
|
||||
publicKey,
|
||||
signature,
|
||||
encodedData
|
||||
);
|
||||
};
|
||||
25
app/utils/types.ts
Normal file
25
app/utils/types.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export interface PollProps {
|
||||
activePollId: string,
|
||||
pollData: PollData,
|
||||
addOption: (name: string) => void,
|
||||
vote: (optionName: string,uuid: string) => void
|
||||
}
|
||||
|
||||
export interface PollData extends Record<string, SignedData<VoteData>[]> {
|
||||
}
|
||||
|
||||
export interface SignedData<T> {
|
||||
data: T,
|
||||
signature: string
|
||||
}
|
||||
|
||||
export interface VoteData {
|
||||
userid: string,
|
||||
timestamp: string
|
||||
}
|
||||
|
||||
export interface OptionData {
|
||||
userid: string,
|
||||
timestamp: string,
|
||||
optionName: string
|
||||
}
|
||||
Reference in New Issue
Block a user