vega-frontend-monorepo/libs/wallet/src/use-json-rpc-connect.ts
Matthew Russell 4ed623c84c
feat#873): wallet service v2 (#1349)
* feat: update connect dialog to handle api v2

* feat: better error handling

* feat: update to only use strings for pubkey, add json rpc connector

* feat: make json connector follow same patterns as rest connector

* feat: add ability to change wallet location

* feat: add chain id fetch

* feat: improve types

* feat: adjust send tx types

* chore: remove dialog title and chain check temporarily

* feat: additional UI and error handling

* feat: rename keypair and keypairs to pubKey and pubKeys

* feat: make rest and json rpc connectors return consistent data

* feat: rename derived ids more clearly

* feat: update send_transaction response type

* chore: fix tests after context name change

* feat: add utils for wallet, add encode tx func

* feat: return null if tx is rejected

* feat: fix up styles for connect buttons

* feat: handle wallet version check

* feat: add chain id check

* chore: rename select pub key function to match, fix tests

* fix: tests for rest connector form

* feat: add tests for json rpc flow

* feat: connect dialog changes

* chore: change status to enum to avoid magic strings

* feat: add custom icons and handle provided key name

* chore: update global wallet connection e2d tests

* chore: change zod validation to only expected required values

* chore: ignore new generated code files

* chore: fix typos and add translations

* chore: set hosted wallet via env var and only show if not mainnet

* feat: add functionality for try again button

* test: fix failing tests

* chore: fix lint and test

* chore: remove double import

* chore: make console-lite-e2e strict so json connector compiles correctly

* chore: make token e2e tsconfig strict

* chore: make stats-e2e tsconfig strict

* feat: update json rpc request namespace

* feat: simplify connector setup, support try again

* chore: remove comment

* fix: build errors

* chore: make chainId check optional based on presence of appChainId, mock request for tests

* chore: mock chain id request for all apps on all pages

* fix: footer border on small screens

* fix: use beforeEach for chainId query mock

* chore: remove optional chain check, prevent rendering until fetch is complete

* chore: update NX_VEGA_WALLET_URLs as the application now appends the base path, adjust token tests

* fix: token e2e test that checks for pubkey name

* chore: remove duplicated test, update wallet title assertion

* fix: token tests

* fix: token e2e assertions

* fix: withdraw test

* feat: enable json RPC for token

* fix: sendTx command now accpets pubkey as separate arg

* fix: test to use gui option temporarily

Co-authored-by: Dexter <dexter.edwards93@gmail.com>
2022-10-03 11:12:34 -07:00

78 lines
2.5 KiB
TypeScript

import { useCallback, useState } from 'react';
import type { JsonRpcConnector } from './connectors';
import { ClientErrors } from './connectors';
import { WalletError } from './connectors';
import { useVegaWallet } from './use-vega-wallet';
export enum Status {
Idle = 'Idle',
CheckingVersion = 'CheckingVersion',
GettingChainId = 'GettingChainId',
Connecting = 'Connecting',
GettingPerms = 'GettingPerms',
RequestingPerms = 'RequestingPerms',
ListingKeys = 'ListingKeys',
Connected = 'Connected',
Error = 'Error',
}
export const useJsonRpcConnect = (onConnect: () => void) => {
const { connect } = useVegaWallet();
const [status, setStatus] = useState(Status.Idle);
const [error, setError] = useState<WalletError | null>(null);
const attemptConnect = useCallback(
async (connector: JsonRpcConnector, appChainId: string) => {
try {
// Check that the running wallet is compatible with this connector
setStatus(Status.CheckingVersion);
await connector.checkCompat();
// Check if wallet is configured for the same chain as the app
setStatus(Status.GettingChainId);
const chainIdResult = await connector.getChainId();
if (chainIdResult.chainID !== appChainId) {
// Throw wallet error for consitent error handling
throw ClientErrors.WRONG_NETWORK;
}
// Start connection flow. User will be prompted to select a wallet and enter
// its password in the wallet application, promise will resolve once successful
// or it will throw
setStatus(Status.Connecting);
await connector.connectWallet();
// Check wallet is permitted to reveal its public keys
setStatus(Status.GettingPerms);
const permsResult = await connector.getPermissions();
if (permsResult.permissions.public_keys === 'none') {
// Automatically request new perms. User will again be prompted to permit this change
// and enter their password
setStatus(Status.RequestingPerms);
await connector.requestPermissions();
}
// Call connect in the wallet provider. The connector will be stored for
// future actions such as sending transactions
await connect(connector);
setStatus(Status.Connected);
onConnect();
} catch (err) {
if (err instanceof WalletError) {
setError(err);
}
setStatus(Status.Error);
}
},
[onConnect, connect]
);
return {
status,
error,
connect: attemptConnect,
};
};