Skip to content

Commit

Permalink
feat: add optional label field to ConnectedWallet instances
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronCQL committed Jul 3, 2024
1 parent a41a462 commit b76472b
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 31 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## `v0.0.71`

### Features

- Add optional `label` field to `ConnectedWallet` instances, which contains the user-defined label of the wallet

### Fixes

- Handle more wallet errors in the `wrap` function

## `v0.0.70`

### Features
Expand Down
10 changes: 5 additions & 5 deletions examples/solid-vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const CHAINS: Record<string, string> = {
const WALLETS: Record<WalletName, string> = {
[WalletName.KEPLR]: "Keplr",
[WalletName.COSMOSTATION]: "Cosmostation",
[WalletName.STATION]: "Terra Station",
[WalletName.STATION]: "Station",
[WalletName.LEAP]: "Leap",
[WalletName.COMPASS]: "Compass",
[WalletName.METAMASK_INJECTIVE]: "MetaMask",
Expand Down Expand Up @@ -325,13 +325,13 @@ const App: Component = () => {
Broadcast Tx
</button>

<div class="flex flex-col">
<code>CONNECTED WALLETS</code>
<div class="flex flex-col items-center border py-2 px-4 border-gray-400">
<div class="font-semibold">Connected Wallets</div>
<For each={Object.values(wallets)}>
{(wallet) => (
<code>
{wallet.address.slice(0, 10)}
...{wallet.address.slice(-5)} | {WALLETS[wallet.id]}
{wallet.address.slice(0, 10)}{wallet.address.slice(-5)} [
{WALLETS[wallet.id]}: {wallet.label ?? "-"}]
</code>
)}
</For>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cosmes",
"version": "0.0.70",
"version": "0.0.71",
"private": false,
"packageManager": "pnpm@8.3.0",
"sideEffects": false,
Expand Down
1 change: 1 addition & 0 deletions src/wallet/walletconnect/WalletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type StorageSession = {
};

type GetAccountResponse = {
name?: string | undefined;
address: string;
algo: string;
pubkey: string;
Expand Down
4 changes: 4 additions & 0 deletions src/wallet/wallets/ConnectedWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export abstract class ConnectedWallet {
public readonly id: WalletName;
/** The type of connection to the wallet. */
public readonly type: WalletType;
/** The user-defined label for this wallet, if any. */
public readonly label: string | undefined;
/** The chain ID this wallet is connected to. */
public readonly chainId: string;
/** The public key. */
Expand All @@ -62,6 +64,7 @@ export abstract class ConnectedWallet {
constructor(
id: WalletName,
type: WalletType,
label: string | undefined,
chainId: string,
pubKey: Secp256k1PubKey,
address: string,
Expand All @@ -70,6 +73,7 @@ export abstract class ConnectedWallet {
) {
this.id = id;
this.type = type;
this.label = label;
this.chainId = chainId;
this.pubKey = pubKey;
this.address = address;
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/wallets/compass/CompassController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ export class CompassController extends WalletController {
}
await WalletError.wrap(ext.enable(chains.map(({ chainId }) => chainId)));
for (const { chainId, rpc, gasPrice } of Object.values(chains)) {
const { bech32Address, pubKey, isNanoLedger } = await WalletError.wrap(
ext.getKey(chainId)
);
const { name, bech32Address, pubKey, isNanoLedger } =
await WalletError.wrap(ext.getKey(chainId));
const key = new Secp256k1PubKey({
chainId,
key: pubKey,
Expand All @@ -49,6 +48,7 @@ export class CompassController extends WalletController {
chainId,
new CompassExtension(
this.id,
name,
ext,
chainId,
key,
Expand Down
9 changes: 5 additions & 4 deletions src/wallet/wallets/cosmostation/CosmostationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class CosmostationController extends WalletController {
);
for (let i = 0; i < chains.length; i++) {
const { chainId, rpc, gasPrice } = chains[i];
const { pubkey, address } = await WalletError.wrap(
const { name, pubkey, address } = await WalletError.wrap(
this.wc.getAccount(chainId)
);
const key = new Secp256k1PubKey({
Expand All @@ -50,6 +50,7 @@ export class CosmostationController extends WalletController {
chainId,
new CosmostationWalletConnectV2(
this.id,
name,
this.wc,
chainId,
key,
Expand All @@ -71,9 +72,8 @@ export class CosmostationController extends WalletController {
}
await WalletError.wrap(ext.enable(chains.map(({ chainId }) => chainId)));
for (const { chainId, rpc, gasPrice } of Object.values(chains)) {
const { bech32Address, pubKey, isNanoLedger } = await WalletError.wrap(
ext.getKey(chainId)
);
const { name, bech32Address, pubKey, isNanoLedger } =
await WalletError.wrap(ext.getKey(chainId));
const key = new Secp256k1PubKey({
chainId,
key: pubKey,
Expand All @@ -82,6 +82,7 @@ export class CosmostationController extends WalletController {
chainId,
new CosmostationExtension(
this.id,
name,
ext,
chainId,
key,
Expand Down
9 changes: 5 additions & 4 deletions src/wallet/wallets/keplr/KeplrController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class KeplrController extends WalletController {
);
for (let i = 0; i < chains.length; i++) {
const { chainId, rpc, gasPrice } = chains[i];
const { pubkey, address } = await WalletError.wrap(
const { name, pubkey, address } = await WalletError.wrap(
this.wc.getAccount(chainId)
);
const key = new Secp256k1PubKey({
Expand All @@ -50,6 +50,7 @@ export class KeplrController extends WalletController {
chainId,
new KeplrWalletConnectV2(
this.id,
name,
this.wc,
chainId,
key,
Expand All @@ -71,9 +72,8 @@ export class KeplrController extends WalletController {
}
await WalletError.wrap(ext.enable(chains.map(({ chainId }) => chainId)));
for (const { chainId, rpc, gasPrice } of Object.values(chains)) {
const { bech32Address, pubKey, isNanoLedger } = await WalletError.wrap(
ext.getKey(chainId)
);
const { name, bech32Address, pubKey, isNanoLedger } =
await WalletError.wrap(ext.getKey(chainId));
const key = new Secp256k1PubKey({
key: pubKey,
chainId,
Expand All @@ -82,6 +82,7 @@ export class KeplrController extends WalletController {
chainId,
new KeplrExtension(
this.id,
name,
ext,
chainId,
key,
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallets/keplr/KeplrExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class KeplrExtension extends ConnectedWallet {

constructor(
walletName: WalletName,
label: string | undefined,
ext: Keplr,
chainId: string,
pubKey: Secp256k1PubKey,
Expand All @@ -39,6 +40,7 @@ export class KeplrExtension extends ConnectedWallet {
super(
walletName,
WalletType.EXTENSION,
label,
chainId,
pubKey,
address,
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallets/keplr/KeplrWalletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class KeplrWalletConnectV2 extends ConnectedWallet {

constructor(
walletName: WalletName,
label: string | undefined,
wc: WalletConnectV2,
chainId: string,
pubKey: Secp256k1PubKey,
Expand All @@ -37,6 +38,7 @@ export class KeplrWalletConnectV2 extends ConnectedWallet {
super(
walletName,
WalletType.WALLETCONNECT,
label,
chainId,
pubKey,
address,
Expand Down
9 changes: 5 additions & 4 deletions src/wallet/wallets/leap/LeapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class LeapController extends WalletController {
);
for (let i = 0; i < chains.length; i++) {
const { chainId, rpc, gasPrice } = chains[i];
const { pubkey, address } = await WalletError.wrap(
const { name, pubkey, address } = await WalletError.wrap(
this.wc.getAccount(chainId)
);
const key = new Secp256k1PubKey({
Expand All @@ -49,6 +49,7 @@ export class LeapController extends WalletController {
chainId,
new LeapWalletConnectV2(
this.id,
name,
this.wc,
chainId,
key,
Expand All @@ -70,9 +71,8 @@ export class LeapController extends WalletController {
}
await WalletError.wrap(ext.enable(chains.map(({ chainId }) => chainId)));
for (const { chainId, rpc, gasPrice } of Object.values(chains)) {
const { bech32Address, pubKey, isNanoLedger } = await WalletError.wrap(
ext.getKey(chainId)
);
const { name, bech32Address, pubKey, isNanoLedger } =
await WalletError.wrap(ext.getKey(chainId));
const key = new Secp256k1PubKey({
chainId,
key: pubKey,
Expand All @@ -81,6 +81,7 @@ export class LeapController extends WalletController {
chainId,
new LeapExtension(
this.id,
name,
ext,
chainId,
key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class MetamaskInjectiveController extends WalletController {
chain.chainId,
new MetamaskInjectiveExtension(
this.id,
undefined,
ext,
chain.chainId,
pubKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class MetamaskInjectiveExtension extends ConnectedWallet {

constructor(
walletName: WalletName,
label: string | undefined,
ext: Ethereum,
chainId: string,
pubKey: Secp256k1PubKey,
Expand All @@ -36,6 +37,7 @@ export class MetamaskInjectiveExtension extends ConnectedWallet {
super(
walletName,
WalletType.EXTENSION,
label,
chainId,
pubKey,
bech32Address,
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallets/mnemonic/MnemonicWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class MnemonicWallet extends ConnectedWallet {
// unlikely to be used by most consumers of CosmES.
"mnemonic" as WalletName,
"mnemonic" as WalletType,
undefined,
chainId,
new Secp256k1PubKey({
chainId,
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/wallets/ninji/NinjiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ export class NinjiController extends WalletController {
}
await WalletError.wrap(ext.enable(chains.map(({ chainId }) => chainId)));
for (const { chainId, rpc, gasPrice } of Object.values(chains)) {
const { bech32Address, pubKey, isNanoLedger } = await WalletError.wrap(
ext.getKey(chainId)
);
const { name, bech32Address, pubKey, isNanoLedger } =
await WalletError.wrap(ext.getKey(chainId));
const key = new Secp256k1PubKey({
chainId,
key: pubKey,
Expand All @@ -48,6 +47,7 @@ export class NinjiController extends WalletController {
chainId,
new NinjiExtension(
this.id,
name,
ext,
chainId,
key,
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/wallets/owallet/OWalletController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ export class OWalletController extends WalletController {
}
await WalletError.wrap(ext.enable(chains.map(({ chainId }) => chainId)));
for (const { chainId, rpc, gasPrice } of Object.values(chains)) {
const { bech32Address, pubKey, isNanoLedger } = await WalletError.wrap(
ext.getKey(chainId)
);
const { name, bech32Address, pubKey, isNanoLedger } =
await WalletError.wrap(ext.getKey(chainId));
const key = new Secp256k1PubKey({
chainId,
key: pubKey,
Expand All @@ -49,6 +48,7 @@ export class OWalletController extends WalletController {
chainId,
new OWalletExtension(
this.id,
name,
ext,
chainId,
key,
Expand Down
16 changes: 12 additions & 4 deletions src/wallet/wallets/station/StationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ export class StationController extends WalletController {
);
wallets.set(
chainId,
new StationWalletConnectV1(wc, chainId, key, address, rpc, gasPrice)
new StationWalletConnectV1(
undefined,
wc,
chainId,
key,
address,
rpc,
gasPrice
)
);
} catch (err) {
// We simply log and ignore the error for now
Expand All @@ -93,9 +101,8 @@ export class StationController extends WalletController {
await WalletError.wrap(ext.enable(chains.map(({ chainId }) => chainId)));
for (const { chainId, rpc, gasPrice } of Object.values(chains)) {
try {
const { bech32Address, pubKey, isNanoLedger } = await WalletError.wrap(
ext.getKey(chainId)
);
const { name, bech32Address, pubKey, isNanoLedger } =
await WalletError.wrap(ext.getKey(chainId));
const key = new Secp256k1PubKey({
key: pubKey,
chainId,
Expand All @@ -104,6 +111,7 @@ export class StationController extends WalletController {
chainId,
new StationExtension(
this.id,
name,
ext,
chainId,
key,
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallets/station/StationWalletConnectV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class StationWalletConnectV1 extends ConnectedWallet {
private readonly wc: WalletConnect;

constructor(
label: string | undefined,
wc: WalletConnect,
chainId: string,
pubKey: Secp256k1PubKey,
Expand All @@ -33,6 +34,7 @@ export class StationWalletConnectV1 extends ConnectedWallet {
super(
WalletName.STATION,
WalletType.WALLETCONNECT,
label,
chainId,
pubKey,
address,
Expand Down

0 comments on commit b76472b

Please sign in to comment.