Skip to content

Commit

Permalink
chore: posting with gnokey key
Browse files Browse the repository at this point in the history
  • Loading branch information
iuricmp committed Oct 8, 2024
1 parent 41c38e8 commit b3fe51a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 29 deletions.
29 changes: 7 additions & 22 deletions mobile/app/post/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Stack, useNavigation, useRouter } from "expo-router";
import { useEffect, useState } from "react";
import { KeyboardAvoidingView, Platform } from "react-native";
import { broadcastTxCommit, hasParam, makeCallTxAndRedirect, selectAccount, selectQueryParams, selectQueryParamsAddress, useAppDispatch, useAppSelector } from "@gno/redux";
import * as Linking from 'expo-linking';

export default function Search() {
const [postContent, setPostContent] = useState("");
Expand All @@ -19,13 +18,11 @@ export default function Search() {
const dispatch = useAppDispatch();
const account = useAppSelector(selectAccount);

// address from the url to be used in the makeCallTx
const bech32 = useAppSelector(selectQueryParamsAddress);

const queryParams = useAppSelector(selectQueryParams);

// hook to handle the signed tx from the Gnokey and broadcast it
useEffect(() => {
if ( queryParams && hasParam("tx", queryParams)) {
if (queryParams && hasParam("tx", queryParams)) {

const signedTx = decodeURIComponent(queryParams.tx as string)
console.log("signedTx: ", signedTx);
Expand All @@ -40,19 +37,9 @@ export default function Search() {
} finally {
setLoading(false);
}
}
}
}, [queryParams]);

useEffect(() => {
(async () => {
if (bech32 && typeof bech32 == 'string' && postContent) {
const argsTx = await dispatch(makeCallTxAndRedirect({ bech32, postContent })).unwrap();

console.log("Opening Gnokey to sign the transaction, argsTx: ", argsTx.txJson);
}
})()
}, [bech32]);

useEffect(() => {
const unsubscribe = navigation.addListener("focus", async () => {
setPostContent("");
Expand All @@ -61,11 +48,9 @@ export default function Search() {
return unsubscribe;
}, [navigation]);

const requestAddress = async () => {
console.log("requesting address for GnokeyMobile");
// await dispatch(requestAddressForGnokeyMobile()).unwrap();
const callback = encodeURIComponent('tech.berty.dsocial://post');
Linking.openURL(`land.gno.gnokey://toselect?callback=${callback}`);
const onPressPost = async () => {
if (!account || !account.bech32) throw new Error("No active account: " + JSON.stringify(account));
await dispatch(makeCallTxAndRedirect({ bech32: account.bech32, postContent })).unwrap();
}

return (
Expand All @@ -90,7 +75,7 @@ export default function Search() {
style={{ height: 200 }}
/>
<Spacer space={24} />
<Button.TouchableOpacity loading={loading} title="Post" variant="primary" onPress={requestAddress} />
<Button.TouchableOpacity loading={loading} title="Post" variant="primary" onPress={onPressPost} />
<Spacer space={48} />
</KeyboardAvoidingView>
</Layout.BodyAlignedBotton>
Expand Down
8 changes: 1 addition & 7 deletions mobile/redux/features/linkingSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ export const requestLoginForGnokeyMobile = createAsyncThunk<boolean>("tx/request
return await Linking.openURL(`land.gno.gnokey://tologin?callback=${callback}`);
})

export const requestAddressForGnokeyMobile = createAsyncThunk<boolean>("tx/requestAddressForGnokeyMobile", async () => {
console.log("requesting address for GnokeyMobile");
const callback = encodeURIComponent('tech.berty.dsocial://post');
return await Linking.openURL(`land.gno.gnokey://toselect?callback=${callback}`);
});

export const makeCallTxAndRedirect = createAsyncThunk<MakeTxResponse, { bech32: string, postContent: string }, ThunkExtra>("tx/makeCallTx", async ({ bech32, postContent }, thunkAPI) => {
console.log("making a tx to: ", bech32);

Expand Down Expand Up @@ -96,7 +90,7 @@ export const linkingSlice = createSlice({
selectPath: (state: State) => state.path,
selectQueryParams: (state: State) => state.queryParams,
selectLinkingParsedURL: (state: State) => state.linkingParsedURl,
selectQueryParamsAddress: (state: State) => state.linkingParsedURl?.queryParams?.address,
selectQueryParamsAddress: (state: State) => state.linkingParsedURl?.queryParams?.address as string | undefined,
},
});

Expand Down

0 comments on commit b3fe51a

Please sign in to comment.