Skip to content

Commit

Permalink
feat: vectis integration
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nl1 committed Jun 22, 2023
1 parent ef20d05 commit 48d3a55
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/docs/hooks/useActiveWalletType.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function App() {
isKeplr: boolean;
isKeplrMobile: boolean;
isLeap: boolean;
isVectis: boolean;
isLeapMobile: boolean;
isWalletConnect: boolean;
}
Expand Down
1 change: 1 addition & 0 deletions docs/docs/types/walletType.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function App() {
enum WalletType {
KEPLR = "keplr",
LEAP = "leap",
VECTIS = "vectis",
COSMOSTATION = "cosmostation",
WALLETCONNECT = "walletconnect",
WC_KEPLR_MOBILE = "wc_keplr_mobile",
Expand Down
8 changes: 8 additions & 0 deletions packages/graz-adapter-vectis/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-check

const { extendEslint } = require("@strangelovelabs/style-guide");

module.exports = extendEslint(["browser-node", "react", "typescript", "tsup"], {
ignorePatterns: ["dist/**"],
root: true,
});
4 changes: 4 additions & 0 deletions packages/graz-adapter-vectis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/chains/*
/dist/**/*
!/chains/*.stub
!/dist/**/.gitkeep
21 changes: 21 additions & 0 deletions packages/graz-adapter-vectis/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Strangelove Ventures

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions packages/graz-adapter-vectis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# graz-adapter-vectis

[![npm/v](https://badgen.net/npm/v/graz-adapter-vectis)](https://www.npmjs.com/package/graz-adapter-vectis)
[![npm/dt](https://badgen.net/npm/dt/graz-adapter-vectis)](https://www.npmjs.com/package/graz-adapter-vectis)
[![stars](https://badgen.net/github/stars/strangelove-ventures/graz)](https://github.com/strangelove-ventures/graz)

React hooks for Cosmos. Learn more about graz on the [official GitHub repository](https://github.com/strangelove-ventures/graz).

[MIT License, Copyright (c) 2023 Strangelove Ventures](./LICENSE)
Empty file.
6 changes: 6 additions & 0 deletions packages/graz-adapter-vectis/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type VectisWindow = import("@vectis/extension-client").VectisWindow;

// eslint-disable-next-line @typescript-eslint/no-empty-interface
declare interface Window extends VectisWindow {
//
}
40 changes: 40 additions & 0 deletions packages/graz-adapter-vectis/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "graz-adapter-vectis",
"description": "Graz adapter for Vectis Wallet",
"version": "0.0.2",
"repository": "https://github.com/strangelove-ventures/graz.git",
"homepage": "https://github.com/strangelove-ventures/graz",
"bugs": "https://github.com/strangelove-ventures/graz/issues",
"files": [
"dist/*.d.ts",
"dist/*.js",
"dist/*.mjs"
],
"sideEffects": false,
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"lint": "eslint --fix \"src/**/*.{ts,tsx}\"",
"prepublishOnly": "pnpm build"
},
"peerDependencies": {
"graz": ">=0.0.37"
},
"devDependencies": {
"@types/node": "^20.3.1",
"graz": "*",
"typescript": "^5.1.3"
},
"keywords": [
"graz",
"graz-adapter",
"graz-adapter-vectis",
"vectis-wallet",
"vectis",
"use-vectis"
],
"license": "MIT",
"dependencies": {
"@vectis/extension-client": "^0.7.0"
}
}
98 changes: 98 additions & 0 deletions packages/graz-adapter-vectis/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import type { ChainInfo, KeyInfo, OfflineAminoSigner, OfflineDirectSigner } from "@vectis/extension-client";
import type { AccountData, GrazAdapter } from "graz";

export class VectisAdapter implements GrazAdapter {
name = "vectis";
id = "vectis";
keystoreEvent = "vectis_accountChanged";

private getConnector() {
if (typeof window.vectis.cosmos !== "undefined") return window.vectis.cosmos;
throw new Error("window.vectis is not defined");
}

checkConnector() {
try {
this.getConnector();
return true;
} catch (error) {
console.error(error);
return false;
}
}

async enable(chainId: string) {
try {
await this.getConnector().enable(chainId);
} catch (error) {
console.error(error);
throw error;
}
}

async experimentalSuggestChain(chainInfo: ChainInfo) {
try {
await this.getConnector().suggestChains([chainInfo]);
} catch (error) {
console.error(error);
throw error;
}
}

async getKey(chainId: string): Promise<KeyInfo> {
try {
const key = await this.getConnector().getKey(chainId);
return key;
} catch (error) {
console.error(error);
throw error;
}
}

async getAccount(chainId: string, _prefix?: string): Promise<AccountData> {
try {
const [account] = await this.getConnector().getAccounts(chainId);
if (!account) throw new Error("Account not found");
return {
address: Uint8Array.from([]),
bech32Address: account.address,
pubKey: account.pubkey,
algo: account.algo,
};
} catch (error) {
console.error(error);
throw error;
}
}

getOfflineSigner(chainId: string) {
try {
const signer = this.getConnector().getOfflineSigner(chainId);
return signer as OfflineAminoSigner & OfflineDirectSigner;
} catch (error) {
console.error(error);
throw error;
}
}

getOfflineSignerOnlyAmino(chainId: string) {
try {
const signer = this.getConnector().getOfflineSignerAmino(chainId);
return signer;
} catch (error) {
console.error(error);
throw error;
}
}

async getOfflineSignerAuto(chainId: string) {
try {
const key = await this.getConnector().getKey(chainId);
if (key.isNanoLedger) return this.getConnector().getOfflineSignerAmino(chainId);
return await this.getConnector().getOfflineSignerAuto(chainId);
} catch (error) {
console.error(error);
throw error;
}
}
}
15 changes: 15 additions & 0 deletions packages/graz-adapter-vectis/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "@strangelovelabs/style-guide/tsconfig.json",
"compilerOptions": {
"moduleResolution": "node",
"resolveJsonModule": true
},
"exclude": [
"node_modules"
],
"include": [
"env.d.ts",
"**/*.ts",
"**/*.tsx"
]
}
12 changes: 12 additions & 0 deletions packages/graz-adapter-vectis/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from "tsup";

export default defineConfig(({ watch }) => ({
clean: !watch,
dts: true,
entry: ["src/*.ts"],
format: ["cjs", "esm"],
minify: !watch,
minifyIdentifiers: !watch,
minifySyntax: !watch,
minifyWhitespace: !watch,
}));
3 changes: 2 additions & 1 deletion packages/graz/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type KeplrWindow = import("@keplr-wallet/types").Window;
type VectisWindow = import("@vectis/extension-client").VectisWindow;

declare interface Window extends KeplrWindow {
declare interface Window extends KeplrWindow, VectisWindow {
leap: KeplrWindow["keplr"];
cosmostation: {
cosmos: {
Expand Down
42 changes: 42 additions & 0 deletions packages/graz/src/actions/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,45 @@ export const getCosmostation = (): Wallet => {
throw new Error("window.cosmostation.providers.keplr is not defined");
};

/**
* Function to return {@link Wallet} object and throws and error if it does not exist on `window`.
*
* @example
* ```ts
* try {
* const vectis = getVectis();
* } catch (error: Error) {
* console.error(error.message);
* }
* ```
*
*
*/
export const getVectis = (): Wallet => {
if (typeof window.vectis !== "undefined") {
const vectis = window.vectis.cosmos;
const subscription: (reconnect: () => void) => void = (reconnect) => {
window.addEventListener("vectis_accountChanged", () => {
clearSession();
reconnect();
});
return () => {
window.removeEventListener("vectis_accountChanged", () => {
clearSession();
reconnect();
});
};
};
const res = Object.assign(vectis, {
subscription,
});
return res;
}

useGrazInternalStore.getState()._notFoundFn();
throw new Error("window.keplr is not defined");
};

type SignDirectParams = Parameters<Wallet["signDirect"]>;
type SignAminoParams = Parameters<Wallet["signAmino"]>;

Expand Down Expand Up @@ -719,6 +758,9 @@ export const getWallet = (type: WalletType = useGrazInternalStore.getState().wal
case WalletType.COSMOSTATION: {
return getCosmostation();
}
case WalletType.VECTIS: {
return getVectis();
}
case WalletType.WALLETCONNECT: {
return getWalletConnect();
}
Expand Down
1 change: 1 addition & 0 deletions packages/graz/src/hooks/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const useActiveWalletType = () => {
isKeplrMobile: x.walletType === WalletType.WC_KEPLR_MOBILE,
isLeap: x.walletType === WalletType.LEAP,
isLeapMobile: x.walletType === WalletType.WC_LEAP_MOBILE,
isVectis: x.walletType === WalletType.VECTIS,
isWalletConnect: x.walletType === WalletType.WALLETCONNECT,
}),
shallow,
Expand Down
7 changes: 6 additions & 1 deletion packages/graz/src/provider/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { FC } from "react";
import { useEffect } from "react";

import { reconnect } from "../actions/account";
import { getCosmostation, getKeplr, getLeap, getWalletConnect } from "../actions/wallet";
import { getCosmostation, getKeplr, getLeap, getVectis, getWalletConnect } from "../actions/wallet";
import { RECONNECT_SESSION_KEY } from "../constant";
import { useGrazInternalStore, useGrazSessionStore } from "../store";
import { WalletType } from "../types/wallet";
Expand Down Expand Up @@ -55,6 +55,11 @@ export const useGrazEvents = () => {
void reconnect({ onError: _onReconnectFailed });
});
}
if (_reconnectConnector === WalletType.VECTIS) {
getVectis().subscription?.(() => {
void reconnect({ onError: _onReconnectFailed });
});
}
if (_reconnectConnector === WalletType.WALLETCONNECT) {
if (wcSignClient) {
getWalletConnect().subscription?.(() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/graz/src/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Keplr } from "@keplr-wallet/types";
export enum WalletType {
KEPLR = "keplr",
LEAP = "leap",
VECTIS = "vectis",
COSMOSTATION = "cosmostation",
WALLETCONNECT = "walletconnect",
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -16,6 +17,7 @@ export enum WalletType {
export const WALLET_TYPES = [
WalletType.KEPLR,
WalletType.LEAP,
WalletType.VECTIS,
WalletType.COSMOSTATION,
WalletType.WALLETCONNECT,
WalletType.WC_KEPLR_MOBILE,
Expand Down
22 changes: 21 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 48d3a55

Please sign in to comment.