Skip to content

Commit

Permalink
Merge branch 'u3-dev' into F-communityLinksModalV2-shixuewen
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlysxw committed Feb 23, 2024
2 parents c186d29 + 1494fcc commit 8a7a278
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function MultiPlatformShareModal({
<ModalBody isMobile={isMobile}>
<CloseBtn onClick={closeModal} />
<AddPostForm
onSubmitEnd={() => {
onSuccess={() => {
closeModal();
onSubmitEnd?.();
}}
Expand Down
2 changes: 2 additions & 0 deletions apps/u3/src/components/social/farcaster/FCast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default function FCast({
disableRenderUrl,
simpleLayout,
isV2Layout,
shareLink,
...wrapperProps
}: ComponentPropsWithRef<'div'> & {
cast: FarCast;
Expand All @@ -81,6 +82,7 @@ export default function FCast({
castHex: string
) => void;
isV2Layout?: boolean;
shareLink?: string;
}) {
const navigate = useNavigate();
const viewRef = useRef<HTMLDivElement>(null);
Expand Down
5 changes: 5 additions & 0 deletions apps/u3/src/container/community/FarcasterPostDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import useLogin from '@/hooks/shared/useLogin';
import { FARCASTER_NETWORK, FARCASTER_WEB_CLIENT } from '@/constants/farcaster';
import { ReplyCast } from '@/components/social/farcaster/FCastReply';
import { LoadingWrapper } from '../social/CommonStyles';
import { getCommunityPostDetailShareUrlWithFarcaster } from '@/utils/shared/share';

export default function FarcasterPostDetail() {
const navigate = useNavigate();
Expand Down Expand Up @@ -140,6 +141,10 @@ export default function FarcasterPostDetail() {
farcasterUserDataObj={farcasterUserDataObj}
isDetail
isV2Layout
shareLink={getCommunityPostDetailShareUrlWithFarcaster(
channelId,
castId
)}
castClickAction={(e, castHex) => {
navigate(`/community/${channelId}/posts/fc/${castHex}`);
}}
Expand Down
5 changes: 5 additions & 0 deletions apps/u3/src/container/community/PostsFcNewest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@/components/profile/FollowListWidgets';
import useListScroll from '@/hooks/social/useListScroll';
import { PostList } from './CommonStyles';
import { getCommunityPostDetailShareUrlWithFarcaster } from '@/utils/shared/share';

export default function PostsFcNewest() {
const [parentId] = useState('posts-fc-newest');
Expand Down Expand Up @@ -65,6 +66,10 @@ export default function PostsFcNewest() {
openFarcasterQR={openFarcasterQR}
farcasterUserData={{}}
farcasterUserDataObj={fcUserDataObj}
shareLink={getCommunityPostDetailShareUrlWithFarcaster(
channelId,
key
)}
castClickAction={(e, castHex) => {
setPostScroll({
currentParent: parentId,
Expand Down
5 changes: 5 additions & 0 deletions apps/u3/src/container/community/PostsFcTrending.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LoadingMoreWrapper } from '@/components/profile/FollowListWidgets';
import useListScroll from '@/hooks/social/useListScroll';
import useFarcasterTrending from '@/hooks/social/farcaster/useFarcasterTrending';
import { EndMsgContainer, PostList } from './CommonStyles';
import { getCommunityPostDetailShareUrlWithFarcaster } from '@/utils/shared/share';

export default function PostsFcTrending() {
const [parentId] = useState('posts-fc-trending');
Expand Down Expand Up @@ -62,6 +63,10 @@ export default function PostsFcTrending() {
openFarcasterQR={openFarcasterQR}
farcasterUserData={{}}
farcasterUserDataObj={farcasterTrendingUserDataObj}
shareLink={getCommunityPostDetailShareUrlWithFarcaster(
channelId,
key
)}
castClickAction={(e, castHex) => {
setPostScroll({
currentParent: parentId,
Expand Down
101 changes: 91 additions & 10 deletions apps/u3/src/hooks/social/farcaster/useFarcasterQR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import { toast } from 'react-toastify';
import {
generateKeyPair,
getSignedKeyRequest,
getTempFarsignPrivateKey,
getTempFarsignPubkey,
removeFarsignPrivateKey,
removeFarsignSigner,
removeTempFarsignPrivateKey,
removeTempFarsignPubkey,
saveTempFarsignPrivateKey,
saveTempFarsignPubkey,
setPrivateKey,
setSignedKeyRequest,
} from 'src/utils/social/farcaster/farsign-utils';
import { WARPCAST_API } from 'src/constants/farcaster';
import {
fetchFidSigners,
fetchFidWithVerificationAddress,
getFarcasterSignature,
getFarcasterUserInfo,
} from 'src/services/social/api/farcaster';
Expand All @@ -31,6 +39,7 @@ import {
getDefaultFarcaster,
setDefaultFarcaster,
} from '@/utils/social/farcaster/farcaster-default';
import useLogin from '@/hooks/shared/useLogin';

const stopSign = {
stop: false,
Expand Down Expand Up @@ -63,6 +72,7 @@ export type FarcasterBioLinkData = {

export default function useFarcasterQR() {
const { didSessionStr } = useU3Login();
const { walletAddress } = useLogin();
const [qrFid, setQrFid] = useState<number>();
const [openQR, setOpenQR] = useState(false);
const [showQR, setShowQR] = useState(false);
Expand Down Expand Up @@ -95,14 +105,14 @@ export default function useFarcasterQR() {
let signerSuccess = false;
let stopped = false;

while (tries < 40) {
while (tries < 60) {
if (stopSign.stop) {
stopped = true;
break;
}
tries += 1;
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 3000));
await new Promise((resolve) => setTimeout(resolve, 2000));

const result = await axios
.get(`${WARPCAST_API}/v2/signed-key-request`, {
Expand All @@ -116,6 +126,11 @@ export default function useFarcasterQR() {

if (signedKeyRequest.state === 'completed') {
setSignedKeyRequest(signedKeyRequest);
setPrivateKey(keyPair.privateKey);

removeTempFarsignPrivateKey();
removeTempFarsignPubkey();

if (!getDefaultFarcaster()) {
setDefaultFarcaster(`${signedKeyRequest.userFid}`);
}
Expand Down Expand Up @@ -168,6 +183,7 @@ export default function useFarcasterQR() {
setWarpcastErr('Internal server error');
return;
}

const { signature, appFid, deadline } = resp.data.data;

const { token, deeplinkUrl } = await axios
Expand All @@ -179,18 +195,87 @@ export default function useFarcasterQR() {
})
.then((response) => response.data.result.signedKeyRequest);

// save-temp
saveTempFarsignPrivateKey(keyPair.privateKey);
saveTempFarsignPubkey(keyPair.publicKey);

removeFarsignPrivateKey();
removeFarsignSigner();

setPrivateKey(keyPair.privateKey);
pollForSigner(token, keyPair);
setToken({ token, deepLink: deeplinkUrl });

setDeepLinkUrl(deeplinkUrl);
setShowQR(true);

pollForSigner(token, keyPair);
}, [pollForSigner]);

const checkTempSignerValid = useCallback(async () => {
if (!walletAddress) {
return;
}

const tempPrivateKey = getTempFarsignPrivateKey();
const tempPubkey = getTempFarsignPubkey();
if (!tempPrivateKey || !tempPubkey) {
return;
}

try {
let addrFid = '';
const resp = await fetchFidWithVerificationAddress(walletAddress);
addrFid = resp.data.data.fid;
if (!addrFid) {
return;
}
const fidSigners = await fetchFidSigners(addrFid);
if (!fidSigners.data.data?.signers) {
return;
}
if (fidSigners.data.data.signers.length === 0) {
return;
}
const validSigners = fidSigners.data.data.signers.map((item) => {
return Buffer.from(item.key.data).toString('hex');
});
// console.log('validSigners', validSigners);
if (!validSigners.includes(tempPubkey)) {
return;
}
// temp signer is valid
setPrivateKey(tempPrivateKey);
const tempSignerKeyRequest = {
key: `0x${tempPubkey}`,
userFid: Number(addrFid),
};
setSignedKeyRequest(tempSignerKeyRequest);

// wirte to u3 db
postProfileBiolink(
{
platform: BIOLINK_PLATFORMS.farcaster,
network: String(BIOLINK_FARCASTER_NETWORK),
handle: addrFid,
data: {
farcasterSignerType: FarcasterSignerType.QR,
privateKey: tempPrivateKey,
publicKey: tempPubkey,
signedKeyRequest: tempSignerKeyRequest,
},
},
didSessionStr
);

removeTempFarsignPrivateKey();
removeTempFarsignPubkey();
} catch (e) {
console.error(e);
}
}, [walletAddress]);

const restoreFromQRcode = useCallback(async () => {
// check Temp signer valid
await checkTempSignerValid();

const signer = getSignedKeyRequest();
// if NO signer in local storage, try to get from db
let signedKeyRequest;
Expand Down Expand Up @@ -229,10 +314,6 @@ export default function useFarcasterQR() {
}

if (signedKeyRequest) {
setToken({
token: 'already connected',
deepLink: 'already connected',
});
setQrSigner({
SignedKeyRequest: signedKeyRequest,
isConnected: true,
Expand All @@ -242,7 +323,7 @@ export default function useFarcasterQR() {
} else {
setQrCheckStatus('done');
}
}, [didSessionStr]);
}, [didSessionStr, checkTempSignerValid]);

const openFarcasterQR = useCallback(() => {
stopSign.stop = false;
Expand Down
14 changes: 14 additions & 0 deletions apps/u3/src/services/social/api/farcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,17 @@ export function claimRedEnvelope() {
},
});
}

export function fetchFidWithVerificationAddress(address: string) {
return request({
url: `${REACT_APP_API_SOCIAL_URL}/3r-farcaster/fid?address=${address}`,
method: 'get',
});
}

export function fetchFidSigners(fid: string) {
return request({
url: `${REACT_APP_API_SOCIAL_URL}/3r-farcaster/fid/signers?fid=${fid}`,
method: 'get',
});
}
6 changes: 6 additions & 0 deletions apps/u3/src/utils/shared/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const getSocialDetailShareUrlWithLens = (id: string | number) => {
export const getSocialDetailShareUrlWithFarcaster = (id: string | number) => {
return `${SHARE_DOMAIN}/social/post-detail/fcast/${id}`;
};
export const getCommunityPostDetailShareUrlWithFarcaster = (
channelId: string,
castHex: string | number
) => {
return `${SHARE_DOMAIN}/community/${channelId}/posts/fc/${castHex}`;
};
export const getProfileShareUrl = (identity: string | number) => {
return `${SHARE_DOMAIN}/u/${identity}`;
};
36 changes: 36 additions & 0 deletions apps/u3/src/utils/social/farcaster/farsign-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ function removeFarsignSigner() {
localStorage.removeItem(`farsign-signer-${FARCASTER_CLIENT_NAME}`);
}

function removeTempFarsignPrivateKey() {
localStorage.removeItem(`farsign-temp-privateKey-${FARCASTER_CLIENT_NAME}`);
}
function removeTempFarsignPubkey() {
localStorage.removeItem(`farsign-temp-signer-${FARCASTER_CLIENT_NAME}`);
}
function saveTempFarsignPrivateKey(privateKey: string) {
localStorage.setItem(
`farsign-temp-privateKey-${FARCASTER_CLIENT_NAME}`,
privateKey
);
}
function getTempFarsignPrivateKey() {
const privateKey = localStorage.getItem(
`farsign-temp-privateKey-${FARCASTER_CLIENT_NAME}`
);
return privateKey;
}

function saveTempFarsignPubkey(pubKey: string) {
localStorage.setItem(`farsign-temp-signer-${FARCASTER_CLIENT_NAME}`, pubKey);
}
function getTempFarsignPubkey() {
const pubKey = localStorage.getItem(
`farsign-temp-signer-${FARCASTER_CLIENT_NAME}`
);
return pubKey;
}

function setPrivateKey(privateKey: string) {
localStorage.setItem(
`farsign-privateKey-${FARCASTER_CLIENT_NAME}`,
Expand Down Expand Up @@ -70,4 +99,11 @@ export {
getPrivateKey,
removeFarsignPrivateKey,
removeFarsignSigner,
// temp
removeTempFarsignPrivateKey,
removeTempFarsignPubkey,
saveTempFarsignPrivateKey,
getTempFarsignPrivateKey,
saveTempFarsignPubkey,
getTempFarsignPubkey,
};

0 comments on commit 8a7a278

Please sign in to comment.