Skip to content

Commit cf99636

Browse files
authored
chore: Remove getActiveAccount usage (#183)
Similar to PR gnoverse/dsocial#130, we update gnoboard to use the new Gno Native Kit API which removes `selectAccount` and requires passing the account address to `setPassword`, etc. This PR has five commits: 1. The app needs to keep track of the active account, so add `GnoboardProvider`. 2. Instead of calling Gno Native Kit `selectAccount`, call `GnoboardProvider` `setAccount` to remember the active account. 3. `setPassword` needs the account address, so in `ReenterPassword` add param `accountAddress`. 4. In wallet/home: Pass in the account instead of calling `getActiveAccount`. Use the account address to call `queryAccount`. 5. As in a [similar dsocial PR](gnoverse/dsocial#134), fix the Makefile to call ts_check after node_modules. With this PR, all known apps have been updated to remove `selectAcount` and to pass the account address to the methods of Gno Native Kit, which can be updated to remove deprecated functions and to make the address not optional. --------- Signed-off-by: Jeff Thompson <[email protected]>
1 parent 642904f commit cf99636

File tree

9 files changed

+78
-24
lines changed

9 files changed

+78
-24
lines changed

examples/js/expo/gnoboard/App.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import 'react-native-polyfill-globals/auto';
33

44
import { GnoNativeProvider } from '@gnolang/gnonative';
5+
import { GnoboardProvider } from '@gno/provider/gnoboard-provider';
56
import CustomRouter from '@gno/router/custom-router';
67

78
// Polyfill async.Iterator. For some reason, the Babel presets and plugins are not doing the trick.
@@ -16,7 +17,9 @@ function App() {
1617

1718
return (
1819
<GnoNativeProvider config={defaultConfig}>
19-
<CustomRouter />
20+
<GnoboardProvider>
21+
<CustomRouter/>
22+
</GnoboardProvider>
2023
</GnoNativeProvider>
2124
);
2225
}

examples/js/expo/gnoboard/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ ts_check:
2929

3030
# - Node: Handle node_modules
3131

32-
node_modules: ts_check package.json package-lock.json
32+
node_modules: package.json package-lock.json
3333
$(call check-program, npm)
3434
(npm install && touch $@) || true
3535
.PHONY: node_modules
3636

3737
######### ANDROID #########
3838

39-
android: node_modules $(ANDROID_FRAMEWORK_SRC)
39+
android: node_modules ts_check $(ANDROID_FRAMEWORK_SRC)
4040
$(call check-program, npx)
4141
npx expo run:android
4242
.PHONY: build.android
@@ -46,7 +46,7 @@ $(ANDROID_FRAMEWORK_SRC): $(go_deps)
4646

4747
######### IOS #########
4848

49-
ios: node_modules $(IOS_FRAMEWORK_SRC)
49+
ios: node_modules ts_check $(IOS_FRAMEWORK_SRC)
5050
$(call check-program, npx)
5151
npx expo run:ios
5252
.PHONY: build.ios
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { createContext, useContext, useState, useCallback } from 'react';
2+
3+
import { KeyInfo } from '@gnolang/gnonative';
4+
5+
interface GnoboardProviderProps {
6+
children: React.ReactNode;
7+
}
8+
interface GnoboardContextType {
9+
account: KeyInfo | undefined;
10+
setAccount: (keyInfo : KeyInfo | undefined) => void;
11+
}
12+
13+
const GnoboardContext = createContext<GnoboardContextType | null>(null);
14+
15+
const GnoboardProvider: React.FC<GnoboardProviderProps> = ({ children }) => {
16+
const [account, setAccount] = useState<KeyInfo | undefined>(undefined)
17+
18+
const value = {
19+
account,
20+
setAccount
21+
};
22+
23+
return <GnoboardContext.Provider value={value}>{children}</GnoboardContext.Provider>;
24+
};
25+
26+
function useGnoboardContext() {
27+
const context = useContext(GnoboardContext) as GnoboardContextType;
28+
29+
if (context === undefined) {
30+
throw new Error('useGnoboardContext must be used within a GnoboardProvider');
31+
}
32+
return context;
33+
}
34+
35+
export { GnoboardProvider, useGnoboardContext };

examples/js/expo/gnoboard/src/screens/certify/create-password/index.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SeedBox from '@gno/components/seedbox';
1212
import Alert from '@gno/components/alert';
1313
import { Spacer } from '@gno/components/row';
1414
import { ModalConfirm } from '@gno/components/modal';
15+
import { useGnoboardContext } from '@gno/provider/gnoboard-provider';
1516

1617
const text = {
1718
title: 'Create\na Password',
@@ -27,7 +28,7 @@ const CreatePassword: React.FC<Props> = ({ route }) => {
2728
const [confirmPassword, setConfirmPassword] = useState('');
2829
const [error, setError] = useState<string | undefined>(undefined);
2930
const [showModal, setShowModal] = useState(false);
30-
31+
const { setAccount } = useGnoboardContext();
3132
const { gnonative } = useGnoNativeContext();
3233
const navigation = useNavigation<RouterWelcomeStackProp>();
3334

@@ -51,8 +52,9 @@ const CreatePassword: React.FC<Props> = ({ route }) => {
5152

5253
const response = await gnonative.createAccount(name, phrase, password);
5354
console.log('createAccount response: ' + response);
54-
await gnonative.selectAccount(name);
55-
await gnonative.setPassword(password);
55+
await gnonative.activateAccount(name);
56+
setAccount(response);
57+
await gnonative.setPassword(password, response!.address);
5658
navigation.navigate(RoutePath.Home);
5759
} catch (error) {
5860
setError(JSON.stringify(error));

examples/js/expo/gnoboard/src/screens/certify/enter-seed/index.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import TextInput from '@gno/components/textinput';
1111
import Alert from '@gno/components/alert';
1212
import { Spacer } from '@gno/components/row';
1313
import { ModalConfirm } from '@gno/components/modal';
14+
import { useGnoboardContext } from '@gno/provider/gnoboard-provider';
1415

1516
const walletContent = {
1617
title: 'Import with Seed Phrase',
@@ -27,6 +28,7 @@ const EnterSeedPhrase = () => {
2728
const [showModal, setShowModal] = useState(false);
2829
const { gnonative } = useGnoNativeContext();
2930
const navigation = useNavigation<RouterWelcomeStackProp>();
31+
const { setAccount } = useGnoboardContext();
3032

3133
const recoverAccount = async (override: boolean = false) => {
3234
if (!recoveryPhrase || !name || !password || !confirmPassword) return;
@@ -47,8 +49,9 @@ const EnterSeedPhrase = () => {
4749
}
4850

4951
const response = await gnonative.createAccount(name, recoveryPhrase, password);
50-
await gnonative.selectAccount(name);
51-
await gnonative.setPassword(password);
52+
await gnonative.activateAccount(name);
53+
await gnonative.setPassword(password, response!.address);
54+
setAccount(response);
5255
console.log('createAccount response: ' + response);
5356
navigation.navigate(RoutePath.Home);
5457
} catch (error) {

examples/js/expo/gnoboard/src/screens/devmode/index.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,40 @@ import { ConnectError } from '@connectrpc/connect';
1414
import { useNavigation } from '@react-navigation/native';
1515
import { RouterWelcomeStackProp } from '@gno/router/custom-router';
1616
import { RoutePath } from '@gno/router/path';
17+
import { useGnoboardContext } from '@gno/provider/gnoboard-provider';
1718

1819
function DevMode() {
1920
const [postContent, setPostContent] = useState('');
2021
const [appConsole, setAppConsole] = useState<string>('');
2122
const [loading, setLoading] = useState<string | undefined>(undefined);
2223
const [reenterPassword, setReenterPassword] = useState<string | undefined>(undefined);
24+
const [reenterPasswordAddress, setReenterPasswordAddress] = useState<Uint8Array | undefined>(undefined);
2325
const navigate = useNavigation<RouterWelcomeStackProp>();
2426

2527
const { gnonative } = useGnoNativeContext();
28+
const { account } = useGnoboardContext();
2629

2730
const onPostPress = async () => {
31+
if (!account)
32+
// shouldn't happen, but just in case
33+
throw new Error("No active account");
34+
2835
setLoading('Replying to a post...');
2936
setAppConsole('replying to a post...');
3037
const gasFee = '1000000ugnot';
3138
const gasWanted = BigInt(2000000);
3239
const args: Array<string> = ['1', '1', '1', postContent];
3340
try {
34-
for await (const response of await gnonative.call('gno.land/r/demo/boards', 'CreateReply', args, gasFee, gasWanted)) {
41+
for await (const response of await gnonative.call('gno.land/r/demo/boards', 'CreateReply', args, gasFee, gasWanted, account.address)) {
3542
console.log('response: ', response);
3643
setAppConsole(Buffer.from(response.result).toString());
3744
}
3845
} catch (error) {
3946
if (error instanceof ConnectError) {
4047
const err = new GRPCError(error);
4148
if (err.errCode() === ErrCode.ErrDecryptionFailed) {
42-
const account = await gnonative.getActiveAccount();
43-
setReenterPassword(account.key?.name);
49+
setReenterPassword(account.name);
50+
setReenterPasswordAddress(account.address);
4451
return;
4552
}
4653
}
@@ -86,7 +93,7 @@ function DevMode() {
8693
</Layout.Body>
8794
</Layout.Container>
8895
{reenterPassword ? (
89-
<ReenterPassword visible={Boolean(reenterPassword)} accountName={reenterPassword} onClose={onCloseReenterPassword} />
96+
<ReenterPassword visible={Boolean(reenterPassword)} accountName={reenterPassword} accountAddress={reenterPasswordAddress!} onClose={onCloseReenterPassword} />
9097
) : null}
9198
</>
9299
);

examples/js/expo/gnoboard/src/screens/switch-accounts/ReenterPassword.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import { ConnectError } from '@connectrpc/connect';
1212
export type Props = {
1313
visible: boolean;
1414
accountName: string;
15+
accountAddress: Uint8Array;
1516
onClose: (sucess: boolean) => void;
1617
};
1718

18-
const ReenterPassword = ({ visible, accountName, onClose }: Props) => {
19+
const ReenterPassword = ({ visible, accountName, accountAddress, onClose }: Props) => {
1920
const { gnonative } = useGnoNativeContext();
2021
const [password, setPassword] = useState('');
2122
const [error, setError] = useState<string | undefined>(undefined);
@@ -25,7 +26,7 @@ const ReenterPassword = ({ visible, accountName, onClose }: Props) => {
2526

2627
try {
2728
setError(undefined);
28-
await gnonative.setPassword(password);
29+
await gnonative.setPassword(password, accountAddress);
2930
onClose(true);
3031
} catch (error) {
3132
if (error instanceof ConnectError) {

examples/js/expo/gnoboard/src/screens/switch-accounts/index.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { KeyInfo, useGnoNativeContext } from '@gnolang/gnonative';
44
import { RouterWelcomeStackProp } from '@gno/router/custom-router';
55
import { useNavigation } from '@react-navigation/native';
66
import { useEffect, useState } from 'react';
7+
import { useGnoboardContext } from '@gno/provider/gnoboard-provider';
78
import Loading from '../loading';
89
import SideMenuAccountList from '@gno/components/common/side-menu-account-list/side-menu-account-list';
910
import { RoutePath } from '@gno/router/path';
@@ -15,6 +16,8 @@ const SwitchAccounts = () => {
1516
const [loading, setLoading] = useState<string | undefined>(undefined);
1617
const [accounts, setAccounts] = useState<KeyInfo[]>([]);
1718
const [reenterPassword, setReenterPassword] = useState<string | undefined>(undefined);
19+
const [reenterPasswordAddress, setReenterPasswordAddress] = useState<Uint8Array | undefined>(undefined);
20+
const { setAccount } = useGnoboardContext();
1821

1922
useEffect(() => {
2023
const unsubscribe = navigation.addListener('focus', async () => {
@@ -35,10 +38,12 @@ const SwitchAccounts = () => {
3538
const onChangeAccountHandler = async (value: KeyInfo) => {
3639
try {
3740
setLoading('Changing account...');
38-
const response = await gnonative.selectAccount(value.name);
41+
const response = await gnonative.activateAccount(value.name);
42+
setAccount(value);
3943
setLoading(undefined);
4044
if (!response.hasPassword) {
4145
setReenterPassword(value.name);
46+
setReenterPasswordAddress(value.address);
4247
return;
4348
}
4449
navigation.navigate(RoutePath.Home);
@@ -68,7 +73,7 @@ const SwitchAccounts = () => {
6873
</Layout.Body>
6974
</Layout.Container>
7075
{reenterPassword ? (
71-
<ReenterPassword visible={Boolean(reenterPassword)} accountName={reenterPassword} onClose={onCloseReenterPassword} />
76+
<ReenterPassword visible={Boolean(reenterPassword)} accountName={reenterPassword} accountAddress={reenterPasswordAddress!} onClose={onCloseReenterPassword} />
7277
) : null}
7378
</>
7479
);

examples/js/expo/gnoboard/src/screens/wallet/home/index.tsx

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,25 @@ import { AccountBalance } from '@gno/components/account';
1212
import { Spacer } from '@gno/components/row';
1313
import { ConnectError } from '@connectrpc/connect';
1414
import { ErrCode, GRPCError, useGnoNativeContext, KeyInfo, QueryAccountResponse } from '@gnolang/gnonative';
15+
import { useGnoboardContext } from '@gno/provider/gnoboard-provider';
1516

1617
export const Home: React.FC = () => {
1718
const navigation = useNavigation<RouterWelcomeStackProp>();
1819
const { gnonative } = useGnoNativeContext();
1920

21+
const { account } = useGnoboardContext();
2022
const [loading, setLoading] = React.useState<string | undefined>(undefined);
21-
const [account, setAccount] = React.useState<KeyInfo | undefined>(undefined);
2223
const [balance, setBalance] = React.useState<QueryAccountResponse | undefined>(undefined);
2324
const [unknownAddress, setUnknownAddress] = React.useState<boolean>(false);
2425

2526
useEffect(() => {
2627
const unsubscribe = navigation.addListener('focus', async () => {
2728
setUnknownAddress(false);
28-
setAccount(undefined);
2929
setBalance(undefined);
3030

3131
try {
32-
const response = await gnonative.getActiveAccount();
33-
setAccount(response.key);
34-
if (response.key) {
35-
const balance = await gnonative.queryAccount(response.key.address);
32+
if (account) {
33+
const balance = await gnonative.queryAccount(account.address);
3634
setBalance(balance);
3735
}
3836
} catch (error) {
@@ -47,7 +45,7 @@ export const Home: React.FC = () => {
4745
}
4846
});
4947
return unsubscribe;
50-
}, [navigation]);
48+
}, [navigation, account]);
5149

5250
if (loading) {
5351
return <Loading message={loading} />;

0 commit comments

Comments
 (0)