Skip to content

Commit

Permalink
Add Station support (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo-sturdy authored Nov 22, 2023
1 parent 4c33ac8 commit 9da373b
Show file tree
Hide file tree
Showing 27 changed files with 1,017 additions and 124 deletions.
14 changes: 14 additions & 0 deletions example/starter/public/assets/wallet-icon-station.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions example/starter/src/utils/graz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const listedWallets = {
name: "Vectis",
imgSrc: "/assets/wallet-icon-vectis.svg",
},
[WalletType.STATION]: {
name: "Station",
imgSrc: "/assets/wallet-icon-station.svg",
},
[WalletType.METAMASK_SNAP_LEAP]: {
name: "Metamask Snap Leap",
imgSrc: "/assets/wallet-icon-metamask.png",
Expand Down
8 changes: 8 additions & 0 deletions packages/graz-adapter-station/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-check

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

module.exports = extendEslint(["browser-node", "react", "typescript", "tsup"], {
ignorePatterns: ["dist/**"],
root: true,
});
4 changes: 4 additions & 0 deletions packages/graz-adapter-station/.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-station/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Graz

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-station/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# graz-adapter-station

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

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

[MIT License, Copyright (c) 2023 Graz](./LICENSE)
Empty file.
6 changes: 6 additions & 0 deletions packages/graz-adapter-station/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Station from "@terra-money/station-connector";

// eslint-disable-next-line @typescript-eslint/no-empty-interface
declare interface Window {
station?: Station;
}
39 changes: 39 additions & 0 deletions packages/graz-adapter-station/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "graz-adapter-station",
"description": "Graz adapter for Station Wallet",
"version": "0.0.2",
"repository": "https://github.com/graz-sh/graz.git",
"homepage": "https://github.com/graz-sh/graz",
"bugs": "https://github.com/graz-sh/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": {
"graz": "*",
"typescript": "^5.2.2"
},
"keywords": [
"graz",
"graz-adapter",
"graz-adapter-station",
"station-wallet",
"station",
"use-station"
],
"license": "MIT",
"dependencies": {
"@terra-money/station-connector": "^1.0.15"
}
}
149 changes: 149 additions & 0 deletions packages/graz-adapter-station/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import type { ChainInfo } from "@keplr-wallet/types";
import type { AccountData, GrazAdapter } from "graz";
import { SignDoc } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { DirectSignResponse } from "@cosmjs/proto-signing";
import { ChainInfoResponse } from "@terra-money/station-connector/keplrConnector";

export class StationAdapter implements GrazAdapter {
name = "Station";
id = "station";
keystoreEvent = "station_wallet_change";

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

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

async enable(chainId: string) {
try {
const isAvailable = this.checkConnector();
if (!isAvailable) {
throw new Error("Station is not available");
}
await this.getConnector().enable(chainId);
} catch (error) {
console.error(error);
throw error;
}
}

async experimentalSuggestChain(chainInfo: ChainInfo) {
try {
const isAvailable = this.checkConnector();
if (!isAvailable) {
throw new Error("Station is not available");
}

const chainInfoResponse: ChainInfoResponse = Object.assign(chainInfo, {
chainSymbolImageUrl: chainInfo.chainSymbolImageUrl || "",
stakeCurrency: {
coinDecimals: chainInfo.stakeCurrency.coinDecimals,
coinDenom: chainInfo.stakeCurrency.coinDenom,
coinImageUrl: chainInfo.stakeCurrency.coinImageUrl || "",
coinMinimalDenom: chainInfo.stakeCurrency.coinMinimalDenom,
},
currencies: chainInfo.currencies.map((currency) => ({
coinDecimals: currency.coinDecimals,
coinDenom: currency.coinDenom,
coinImageUrl: currency.coinImageUrl || "",
coinMinimalDenom: currency.coinMinimalDenom,
})),
feeCurrencies: chainInfo.feeCurrencies.map((currency) => ({
coinDecimals: currency.coinDecimals,
coinDenom: currency.coinDenom,
coinImageUrl: currency.coinImageUrl || "",
coinMinimalDenom: currency.coinMinimalDenom,
gasPriceStep: {
average: currency.gasPriceStep?.average || 0,
high: currency.gasPriceStep?.high || 0,
low: currency.gasPriceStep?.low || 0,
},
})),
});

await this.getConnector().experimentalSuggestChain(chainInfoResponse);
} catch (error) {
console.error(error);
throw error;
}
}

async getAccount(chainId: string, prefix?: string): Promise<AccountData> {
try {
const isAvailable = this.checkConnector();
if (!isAvailable) {
throw new Error("Station is not available");
}
const key = await this.getConnector().getKey(chainId);
return {
address: key.address,
bech32Address: key.bech32Address,
pubKey: key.pubKey,
algo: key.algo,
};
} catch (error) {
console.error(error);
throw error;
}
}

getOfflineSigner(chainId: string) {
try {
const isAvailable = this.checkConnector();
if (!isAvailable) {
throw new Error("Station is not available");
}
const signerOnlyAmino = this.getConnector().getOfflineSignerOnlyAmino(chainId);
const signDirect: (signerAddress: string, signDoc: SignDoc) => Promise<DirectSignResponse> = async (
signerAddress: string,
signDoc: SignDoc,
) => {
throw new Error("signDirect not supported by Station");
};
const signer = Object.assign(signerOnlyAmino, { signDirect });

return signer;
} catch (error) {
console.error(error);
throw error;
}
}

getOfflineSignerOnlyAmino(chainId: string) {
try {
const isAvailable = this.checkConnector();
if (!isAvailable) {
throw new Error("Station is not available");
}
const signer = this.getConnector().getOfflineSignerOnlyAmino(chainId);
return signer;
} catch (error) {
console.error(error);
throw error;
}
}

async getOfflineSignerAuto(chainId: string) {
try {
const isAvailable = this.checkConnector();
if (!isAvailable) {
throw new Error("Station is not available");
}
const signer = await this.getConnector().getOfflineSignerAuto(chainId);
return signer;
} catch (error) {
console.error(error);
throw error;
}
}
}
9 changes: 9 additions & 0 deletions packages/graz-adapter-station/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@graz-sh/style-guide/tsconfig.json",
"compilerOptions": {
"moduleResolution": "node",
"resolveJsonModule": true
},
"exclude": ["node_modules"],
"include": ["env.d.ts", "**/*.ts", "**/*.tsx"]
}
13 changes: 13 additions & 0 deletions packages/graz-adapter-station/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "tsup";

export default defineConfig(({ watch }) => ({
cjsInterop: true,
clean: !watch,
dts: true,
entry: ["src/*.ts"],
format: ["cjs", "esm"],
minify: !watch,
shims: true,
splitting: true,
treeshake: true,
}));
1 change: 1 addition & 0 deletions packages/graz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@keplr-wallet/types": "^0.12.23",
"@metamask/providers": "^12.0.0",
"@tanstack/react-query": "^4.35.0",
"@terra-money/station-connector": "^1.0.15",
"@vectis/extension-client": "^0.7.2",
"@walletconnect/sign-client": "^2.10.0",
"@walletconnect/types": "^2.10.0",
Expand Down
12 changes: 5 additions & 7 deletions packages/graz/src/actions/wallet/cosmostation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ import { clearSession } from ".";
export const getCosmostation = (): Wallet => {
if (typeof window.cosmostation.providers.keplr !== "undefined") {
const cosmostation = window.cosmostation.providers.keplr;
const subscription: (reconnect: () => void) => void = (reconnect) => {
window.cosmostation.cosmos.on("accountChanged", () => {
const subscription: (reconnect: () => void) => () => void = (reconnect) => {
const listener = () => {
clearSession();
reconnect();
});
};
window.cosmostation.cosmos.on("accountChanged", listener);
return () => {
window.cosmostation.cosmos.off("accountChanged", () => {
clearSession();
reconnect();
});
window.cosmostation.cosmos.off("accountChanged", listener);
};
};
const res = Object.assign(cosmostation, {
Expand Down
4 changes: 4 additions & 0 deletions packages/graz/src/actions/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getWalletConnect } from "./wallet-connect";
import { getWCCosmostation } from "./wallet-connect/cosmostation";
import { getWCKeplr } from "./wallet-connect/keplr";
import { getWCLeap } from "./wallet-connect/leap";
import { getStation } from "./station";

/**
* Function to check whether given {@link WalletType} or default configured wallet exists.
Expand Down Expand Up @@ -76,6 +77,9 @@ export const getWallet = (type: WalletType = useGrazInternalStore.getState().wal
case WalletType.METAMASK_SNAP_LEAP: {
return getMetamaskSnapLeap();
}
case WalletType.STATION: {
return getStation();
}
default: {
throw new Error("Unknown wallet type");
}
Expand Down
12 changes: 5 additions & 7 deletions packages/graz/src/actions/wallet/keplr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ import { clearSession } from ".";
export const getKeplr = (): Wallet => {
if (typeof window.keplr !== "undefined") {
const keplr = window.keplr;
const subscription: (reconnect: () => void) => void = (reconnect) => {
window.addEventListener("keplr_keystorechange", () => {
const subscription: (reconnect: () => void) => () => void = (reconnect) => {
const listener = () => {
clearSession();
reconnect();
});
};
window.addEventListener("keplr_keystorechange", listener);
return () => {
window.removeEventListener("keplr_keystorechange", () => {
clearSession();
reconnect();
});
window.removeEventListener("keplr_keystorechange", listener);
};
};
const res = Object.assign(keplr, {
Expand Down
12 changes: 5 additions & 7 deletions packages/graz/src/actions/wallet/leap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ import { clearSession } from ".";
export const getLeap = (): Wallet => {
if (typeof window.leap !== "undefined") {
const leap = window.leap;
const subscription: (reconnect: () => void) => void = (reconnect) => {
window.addEventListener("leap_keystorechange", () => {
const subscription: (reconnect: () => void) => () => void = (reconnect) => {
const listener = () => {
clearSession();
reconnect();
});
};
window.addEventListener("leap_keystorechange", listener);
return () => {
window.removeEventListener("leap_keystorechange", () => {
clearSession();
reconnect();
});
window.removeEventListener("leap_keystorechange", listener);
};
};
const res = Object.assign(leap, {
Expand Down
Loading

0 comments on commit 9da373b

Please sign in to comment.