Skip to content

Commit

Permalink
use-anon-encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Feb 21, 2023
1 parent c552796 commit 1af7013
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
1 change: 1 addition & 0 deletions anonmatch/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Deno.test("simple match", async () => {
const [newState, messagesToSend] = await handleMessage(secret)(
states[secret],
)(message, cbInfo);
console.log("finished processing");
messagesToSend.forEach((m) => queue.push(m));
states = { ...states, [secret]: newState };
}
Expand Down
26 changes: 11 additions & 15 deletions anonmatch/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {
AnonymousEncryption,
PublicKey,
decryptAnonymously,
encryptAnonymously,
encryptStable,
sign,
verify,
Expand All @@ -24,7 +27,7 @@ export type AnonMatchPeerState = {

type PeersNoticeMessage = { type: "peers-notice"; peers: PublicKey[] };
type EncryptedSignature = string;
type SelfEncryptedLikee = string;
type SelfEncryptedLikee = AnonymousEncryption;
type SignedLike = {
likee: SelfEncryptedLikee;
signature: EncryptedSignature;
Expand Down Expand Up @@ -80,11 +83,7 @@ export const createLikeMessage = async (
return {
type: "like",
like: {
likee: await nostrTools.nip04.encrypt(
source,
getPublicKey(source),
target,
),
likee: await encryptAnonymously(getPublicKey(source), target),
matchId,
signature: await createLikeSignature(source, target, matchId),
},
Expand Down Expand Up @@ -115,7 +114,7 @@ export const newState = (initialPeers: PublicKey[]): AnonMatchPeerState => ({

const makeMatchNotice = (
matchId: MatchId,
likee: string,
likee: SelfEncryptedLikee,
signature: EncryptedSignature,
): MatchNoticeMessage => ({
type: "match-notice",
Expand Down Expand Up @@ -148,9 +147,8 @@ export const handleMessage =
[signature]: [callbackInfo, likee],
},
};
const newS = { ...state, likesSeen };
return [
newS,
{ ...state, likesSeen },
objectSize(likesSeen[matchId]) === 2
? toMatchNoticeMessage(matchId, Object.entries(likesSeen[matchId]))
: [],
Expand All @@ -160,13 +158,11 @@ export const handleMessage =
const {
like: { matchId, signature, likee },
} = message;
console.log("try dcrypt");
const likeePubKey = await decryptAnonymously(me, likee);
console.log("decrypted", likeePubKey);
return [
(await verifyLikeSignature(
me,
await nostrTools.nip04.decrypt(me, getPublicKey(me), likee),
matchId,
signature,
))
(await verifyLikeSignature(me, likeePubKey, matchId, signature))
? { ...state, myMatches: union(state.myMatches, [matchId]) }
: state,
[],
Expand Down
5 changes: 2 additions & 3 deletions onion-routing/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ export const encryptAnonymously = async (
export const decryptAnonymously = async (
secret: SecretKey,
{ anonymousPublicKey, cipher }: AnonymousEncryption,
): Promise<Serializable> => {
return JSON.parse(
): Promise<Serializable> =>
JSON.parse(
await nostrTools.nip04.decrypt(secret, anonymousPublicKey, cipher),
);
};

// TODO: implement
export const sign = (secret: SecretKey, message: string): Signature =>
Expand Down

0 comments on commit 1af7013

Please sign in to comment.