Skip to content

Commit

Permalink
Merge pull request #20 from nabla-studio/DavideSegullo/feat-terra_sta…
Browse files Browse the repository at this point in the history
…tion

Davide segullo/feat terra station
  • Loading branch information
DavideSegullo authored Nov 17, 2023
2 parents 5c90f09 + dd1dec3 commit fd2c013
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/core/src/utils/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ const objectTraverse = <T extends { [key: string]: never }>(
obj: T,
keys: string[],
) => {
let cursor = obj;
try {
let cursor = obj;

for (const key of keys) {
cursor = cursor[key];
}
for (const key of keys) {
cursor = cursor[key];
}

return cursor;
return cursor;
} catch {
return undefined;
}
};

export const getClientFromExtension = async <T>(
Expand All @@ -22,17 +26,17 @@ export const getClientFromExtension = async <T>(

const keys = Array.isArray(key) ? key : key.split('.');
const wallet = objectTraverse(window as never, keys) as T;
const latestKey = [...keys].pop();
const firstKey = [...keys].shift();

if (!latestKey) {
if (!firstKey) {
throw Error(`Invalid key: ${JSON.stringify(key)}`);
}

if (wallet) {
return wallet;
}

const clientNotExistError = createClientNotExistError(latestKey);
const clientNotExistError = createClientNotExistError(firstKey);

if (document.readyState === 'complete') {
if (wallet) {
Expand Down
2 changes: 2 additions & 0 deletions packages/store/src/slices/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export const createConnectSlice: StateCreator<
throw createInvalidWalletName(walletName);
}

await wallet.init();

set(() => ({ reconnectionStatus: ReconnectionStates.WAITING }));

if (get().options.autoSuggestions) {
Expand Down
1 change: 1 addition & 0 deletions packages/wallets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './keplr';
export * from './leap';
export * from './cosmostation';
export * from './xdefi';
export * from './station';
167 changes: 167 additions & 0 deletions packages/wallets/src/station/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import type {
OfflineAminoSigner,
StdSignDoc,
AminoSignResponse,
StdSignature,
} from '@cosmjs/amino';
import type {
OfflineDirectSigner,
DirectSignResponse,
} from '@cosmjs/proto-signing';
import type {
Key,
SignOptions,
SuggestChain,
SuggestToken,
WalletOptions,
} from '@quirks/core';
import { ExtensionWallet, assertIsDefined } from '@quirks/core';
import type { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import type { Keplr } from '@keplr-wallet/types';
import Long from 'long';
import { chainRegistryChainToKeplr } from '../keplr/utils';

export class StationWalletExtension extends ExtensionWallet<Keplr> {
constructor(options: WalletOptions) {
super(options);
}

override enable(chainIds: string[]): Promise<void> {
assertIsDefined(this.client);

return this.client.enable(chainIds);
}

override disable(chainIds: string[]): Promise<void> {
assertIsDefined(this.client);

return this.client.disable(chainIds);
}

override async getAccount(chainId: string) {
assertIsDefined(this.client);

return await this.client.getKey(chainId);
}

override async getAccounts(chainIds: string[]) {
assertIsDefined(this.client);

const keys = await this.client.getKeysSettled(chainIds);

return keys
.map((key) => {
if (key.status === 'fulfilled') {
return key.value;
}

return undefined;
})
.filter((key) => key !== undefined) as Key[];
}

override async getOfflineSigner(
chainId: string,
options?: SignOptions | undefined,
): Promise<OfflineAminoSigner & OfflineDirectSigner> {
assertIsDefined(this.client);

return await this.client.getOfflineSigner(chainId, options);
}

override async getOfflineSignerOnlyAmino(
chainId: string,
options?: SignOptions | undefined,
): Promise<OfflineAminoSigner> {
assertIsDefined(this.client);

return await this.client.getOfflineSignerOnlyAmino(chainId, options);
}

override getOfflineSignerAuto(
chainId: string,
options?: SignOptions | undefined,
): Promise<OfflineAminoSigner | OfflineDirectSigner> {
assertIsDefined(this.client);

return this.client.getOfflineSignerAuto(chainId, options);
}

override signAmino(
chainId: string,
signer: string,
signDoc: StdSignDoc,
signOptions?: SignOptions | undefined,
): Promise<AminoSignResponse> {
assertIsDefined(this.client);

return this.client.signAmino(chainId, signer, signDoc, signOptions);
}

override signDirect(
chainId: string,
signer: string,
signDoc: SignDoc,
signOptions?: SignOptions | undefined,
): Promise<DirectSignResponse> {
assertIsDefined(this.client);

return this.client.signDirect(
chainId,
signer,
{
...signDoc,
accountNumber: Long.fromString(signDoc.accountNumber.toString(10)),
},
signOptions,
);
}

override signArbitrary(
chainId: string,
signer: string,
data: string | Uint8Array,
): Promise<StdSignature> {
assertIsDefined(this.client);

return this.client.signArbitrary(chainId, signer, data);
}

override verifyArbitrary(
chainId: string,
signer: string,
data: string | Uint8Array,
signature: StdSignature,
): Promise<boolean> {
assertIsDefined(this.client);

return this.client.verifyArbitrary(chainId, signer, data, signature);
}

override async suggestTokens(suggestions: SuggestToken[]): Promise<void> {
assertIsDefined(this.client);

for (const suggestion of suggestions) {
for (const token of suggestion.tokens) {
await this.client.suggestToken(
suggestion.chainId,
token.contractAddress,
token.viewingKey,
);
}
}
}

override async suggestChains(suggestions: SuggestChain[]): Promise<void> {
assertIsDefined(this.client);

for (const suggestion of suggestions) {
const suggestChain = chainRegistryChainToKeplr(
suggestion.chain,
suggestion.assetList ? [suggestion.assetList] : [],
);

await this.client.experimentalSuggestChain(suggestChain);
}
}
}
6 changes: 6 additions & 0 deletions packages/wallets/src/station/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { StationWalletExtension } from './extension';
import { stationExtensionOptions } from './registry';

const stationExtension = new StationWalletExtension(stationExtensionOptions);

export { stationExtension };
38 changes: 38 additions & 0 deletions packages/wallets/src/station/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { WalletConnectionTypes, type WalletOptions } from '@quirks/core';

export const stationExtensionOptions: WalletOptions = {
name: 'station-extension',
prettyName: 'Station',
connectionType: WalletConnectionTypes.EXTENSION,
windowKey: 'station.keplr',
downloads: [
{
device: 'desktop',
browser: 'chrome',
link: 'https://chrome.google.com/webstore/detail/station-wallet/aiifbnbfobpmeekipheeijimdpnlpgpp',
},
{
device: 'desktop',
browser: 'firefox',
link: 'https://addons.mozilla.org/en-US/firefox/addon/terra-station-wallet/',
},
{
device: 'desktop',
browser: 'edge',
link: 'https://microsoftedge.microsoft.com/addons/detail/station-wallet/ajkhoeiiokighlmdnlakpjfoobnjinie',
},
],
logoUrls: {
light: {
svg: 'https://assets.website-files.com/611153e7af981472d8da199c/62d8d4463816d7f427a7600b_01_Tarra_icon.svg',
png: 'https://assets.website-files.com/611153e7af981472d8da199c/62d8b6af5e23fd010628b529_01_Tarra_icon.png',
},
dark: {
svg: 'https://assets.website-files.com/611153e7af981472d8da199c/62d8d4463816d7f427a7600b_01_Tarra_icon.svg',
png: 'https://assets.website-files.com/611153e7af981472d8da199c/62d8b6af5e23fd010628b529_01_Tarra_icon.png',
},
},
events: {
keystorechange: 'station_wallet_change',
},
};

0 comments on commit fd2c013

Please sign in to comment.