Fix/Set initially selected pubkey after connect (#96)

* set initial public key after connection

* remove stray console.error
This commit is contained in:
Matthew Russell 2022-03-19 18:31:44 -07:00 committed by GitHub
parent af333c2abd
commit 4355907c52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 24 deletions

View File

@ -1,9 +1,9 @@
import '@testing-library/jest-dom';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { VegaKey } from '@vegaprotocol/vegawallet-service-api-client';
import { RestConnector } from './connectors';
import { useVegaWallet } from './hooks';
import { VegaWalletProvider } from './provider';
import { WALLET_KEY } from './storage-keys';
const restConnector = new RestConnector();
@ -52,11 +52,9 @@ const generateJSX = () => (
test('Can connect, disconnect and retrieve keypairs', async () => {
const mockKeypairs = [{ pub: 'public key 1' }, { pub: 'public key 2' }];
localStorage.setItem(WALLET_KEY, mockKeypairs[0].pub);
jest
.spyOn(restConnector, 'connect')
// @ts-ignore just using pub to assert state logic
.mockImplementation(() => Promise.resolve(mockKeypairs));
.mockImplementation(() => Promise.resolve(mockKeypairs as VegaKey[]));
jest
.spyOn(restConnector, 'disconnect')

View File

@ -30,29 +30,37 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
// Reference to the current connector instance
const connector = useRef<VegaConnector | null>(null);
const connect = useCallback(async (c: VegaConnector) => {
connector.current = c;
try {
const res = await connector.current.connect();
const connect = useCallback(
async (c: VegaConnector) => {
connector.current = c;
try {
const res = await connector.current.connect();
if (!res) {
if (!res) {
return null;
}
const publicKeysWithName = res.map((pk) => {
const nameMeta = pk.meta?.find((m) => m.key === 'name');
return {
...pk,
name: nameMeta?.value ? nameMeta.value : 'None',
};
});
setKeypairs(publicKeysWithName);
if (publicKey === null) {
setPublicKey(publicKeysWithName[0].pub);
}
return publicKeysWithName;
} catch (err) {
return null;
}
const publicKeysWithName = res.map((pk) => {
const nameMeta = pk.meta?.find((m) => m.key === 'name');
return {
...pk,
name: nameMeta?.value ? nameMeta.value : 'None',
};
});
setKeypairs(publicKeysWithName);
return publicKeysWithName;
} catch (err) {
console.error(err);
return null;
}
}, []);
},
[publicKey]
);
const disconnect = useCallback(async () => {
try {