Skip to content

Commit

Permalink
fix: update to latest get-starknet dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Mar 19, 2024
1 parent 1f2771e commit 017702f
Show file tree
Hide file tree
Showing 23 changed files with 479 additions and 215 deletions.
4 changes: 2 additions & 2 deletions src/wallet/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './getst/main';

/**
* Request Permission for wallet account, return addresses that's allowed by user
* Request Permission for wallet account, return addresses that are allowed by user
* @param silentMode false: request user interaction allowance. true: return only pre-allowed
* @returns allowed accounts addresses
*/
Expand Down Expand Up @@ -51,7 +51,7 @@ export function watchAsset(swo: StarknetWindowObject, asset: WatchAssetParameter
* @returns boolean
*/
export function addStarknetChain(swo: StarknetWindowObject, chain: AddStarknetChainParameters) {
// Can this set custom RPC endpoint ?
// TODO: This should set custom RPC endpoint ?
return swo.request({
type: 'wallet_addStarknetChain',
params: chain,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import { RequestFn } from './rpcMessage';
import { WalletEventHandlers } from './walletEvents';

declare type WalletEventListener = <E extends keyof WalletEventHandlers>(
type WalletEventListener = <E extends keyof WalletEventHandlers>(
event: E,
handleEvent: WalletEventHandlers[E]
) => void;

// Implement the StarknetWindowObject interface with the improved request method
export interface StarknetWindowObject {
id: string;
name: string;
version: string;
icon:
| string
| {
dark: string;
light: string;
};
icon: string | { dark: string; light: string };
request: RequestFn;
on: WalletEventListener;
off: WalletEventListener;
}

declare global {
interface Window {
[key: `starknet_${string}`]: StarknetWindowObject | undefined;
}
}
export {};
4 changes: 0 additions & 4 deletions src/wallet/getst/core.d.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/wallet/getst/discovery.d.ts

This file was deleted.

36 changes: 36 additions & 0 deletions src/wallet/getst/discovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export type WalletProvider = {
id: string;
name: string;
icon: string;
downloads:
| { chrome?: `https://chrome.google.com/webstore/detail/${string}` }
| { firefox?: `https://addons.mozilla.org/en-US/firefox/addon/${string}` }
| { edge?: `https://microsoftedge.microsoft.com/addons/detail/${string}` };
};

const wallets: WalletProvider[] = [
{
id: 'argentX',
name: 'Argent X',
icon: 'data:image/svg+xml;base64,Cjxzdmcgd2lkdGg9IjQwIiBoZWlnaHQ9IjM2IiB2aWV3Qm94PSIwIDAgNDAgMzYiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGQ9Ik0yNC43NTgyIC0zLjk3MzY0ZS0wN0gxNC42MjM4QzE0LjI4NTEgLTMuOTczNjRlLTA3IDE0LjAxMzggMC4yODExNzggMTQuMDA2NCAwLjYzMDY4M0MxMy44MDE3IDEwLjQ1NDkgOC44MjIzNCAxOS43NzkyIDAuMjUxODkzIDI2LjM4MzdDLTAuMDIwMjA0NiAyNi41OTMzIC0wLjA4MjE5NDYgMjYuOTg3MiAwLjExNjczNCAyNy4yNzA5TDYuMDQ2MjMgMzUuNzM0QzYuMjQ3OTYgMzYuMDIyIDYuNjQwOTkgMzYuMDg3IDYuOTE3NjYgMzUuODc1NEMxMi4yNzY1IDMxLjc3MjggMTYuNTg2OSAyNi44MjM2IDE5LjY5MSAyMS4zMzhDMjIuNzk1MSAyNi44MjM2IDI3LjEwNTcgMzEuNzcyOCAzMi40NjQ2IDM1Ljg3NTRDMzIuNzQxIDM2LjA4NyAzMy4xMzQxIDM2LjAyMiAzMy4zMzYxIDM1LjczNEwzOS4yNjU2IDI3LjI3MDlDMzkuNDY0MiAyNi45ODcyIDM5LjQwMjIgMjYuNTkzMyAzOS4xMzA0IDI2LjM4MzdDMzAuNTU5NyAxOS43NzkyIDI1LjU4MDQgMTAuNDU0OSAyNS4zNzU5IDAuNjMwNjgzQzI1LjM2ODUgMC4yODExNzggMjUuMDk2OSAtMy45NzM2NGUtMDcgMjQuNzU4MiAtMy45NzM2NGUtMDdaIiBmaWxsPSIjRkY4NzVCIi8+Cjwvc3ZnPgo=',
downloads: {
chrome:
'https://chrome.google.com/webstore/detail/argent-x-starknet-wallet/dlcobpjiigpikoobohmabehhmhfoodbb',
firefox: 'https://addons.mozilla.org/en-US/firefox/addon/argent-x',
edge: 'https://microsoftedge.microsoft.com/addons/detail/argent-x/ajcicjlkibolbeaaagejfhnofogocgcj',
},
},
{
id: 'braavos',
name: 'Braavos',
icon: 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgdmlld0JveD0iMCAwIDEwMCAxMDAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CiAgICA8cGF0aAogICAgICAgIGQ9Ik02Mi43MDUgMTMuOTExNkM2Mi44MzU5IDE0LjEzMzMgNjIuNjYyMSAxNC40MDcgNjIuNDAzOSAxNC40MDdDNTcuMTgwNyAxNC40MDcgNTIuOTM0OCAxOC41NDI3IDUyLjgzNTEgMjMuNjgxN0M1MS4wNDY1IDIzLjM0NzcgNDkuMTkzMyAyMy4zMjI2IDQ3LjM2MjYgMjMuNjMxMUM0Ny4yMzYxIDE4LjUxNTYgNDMuMDAwOSAxNC40MDcgMzcuNzk0OCAxNC40MDdDMzcuNTM2NSAxNC40MDcgMzcuMzYyNSAxNC4xMzMxIDM3LjQ5MzUgMTMuOTExMkM0MC4wMjE3IDkuNjI4MDkgNDQuNzIwNCA2Ljc1IDUwLjA5OTEgNi43NUM1NS40NzgxIDYuNzUgNjAuMTc2OSA5LjYyODI2IDYyLjcwNSAxMy45MTE2WiIKICAgICAgICBmaWxsPSJ1cmwoI3BhaW50MF9saW5lYXJfMzcyXzQwMjU5KSIgLz4KICAgIDxwYXRoCiAgICAgICAgZD0iTTc4Ljc2MDYgNDUuODcxOEM4MC4yNzI1IDQ2LjMyOTcgODEuNzAyNSA0NS4wMDU1IDgxLjE3MTQgNDMuNTIyMkM3Ni40MTM3IDMwLjIzMzQgNjEuMzkx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Y5NjAwIiAvPgogICAgICAgIDwvbGluZWFyR3JhZGllbnQ+CiAgICAgICAgPGxpbmVhckdyYWRpZW50IGlkPSJwYWludDFfbGluZWFyXzM3Ml80MDI1OSIgeDE9IjQ5LjMwNTciIHkxPSIyLjA3OSIgeDI9IjgwLjM2MjciIHkyPSI5My42NTk3IgogICAgICAgICAgICBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSI+CiAgICAgICAgICAgIDxzdG9wIHN0b3AtY29sb3I9IiNGNUQ0NUUiIC8+CiAgICAgICAgICAgIDxzdG9wIG9mZnNldD0iMSIgc3RvcC1jb2xvcj0iI0ZGOTYwMCIgLz4KICAgICAgICA8L2xpbmVhckdyYWRpZW50PgogICAgICAgIDxsaW5lYXJHcmFkaWVudCBpZD0icGFpbnQyX2xpbmVhcl8zNzJfNDAyNTkiIHgxPSI0OS4zMDU3IiB5MT0iMi4wNzkiIHgyPSI4MC4zNjI3IiB5Mj0iOTMuNjU5NyIKICAgICAgICAgICAgZ3JhZGllbnRVbml0cz0idXNlclNwYWNlT25Vc2UiPgogICAgICAgICAgICA8c3RvcCBzdG9wLWNvbG9yPSIjRjVENDVFIiAvPgogICAgICAgICAgICA8c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiNGRjk2MDAiIC8+CiAgICAgICAgPC9saW5lYXJHcmFkaWVudD4KICAgIDwvZGVmcz4KPC9zdmc+',
downloads: {
chrome:
'https://chrome.google.com/webstore/detail/braavos-wallet/jnlgamecbpmbajjfhmmmlhejkemejdma',
firefox: 'https://addons.mozilla.org/en-US/firefox/addon/braavos-wallet',
edge: 'https://microsoftedge.microsoft.com/addons/detail/braavos-wallet/hkkpjehhcnhgefhbdcgfkeegglpjchdc',
},
},
];

export default wallets;
14 changes: 0 additions & 14 deletions src/wallet/getst/localStorageStore.d.ts

This file was deleted.

81 changes: 81 additions & 0 deletions src/wallet/getst/localStorageStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { generateUID } from './utils';

export interface IStorageWrapper {
set(value: string | null | undefined): boolean;
get(): string | null | undefined;
delete(): boolean;
}

export class LocalStorageWrapper implements IStorageWrapper {
#initialized = false;

#key: string | undefined = undefined;

#prefix: string;

value: string | null | undefined = undefined;

constructor(key: string) {
this.#prefix = key;

this.#init();
}

set(value: string | null | undefined) {
if (!this.#initialized && !this.#init()) {
return false;
}

this.delete(); // clear current key

this.value = value;
if (value) {
this.#key = `${this.#prefix}-${generateUID()}`;
localStorage.setItem(this.#key, value);
}

return true;
}

get() {
this.#validateValue();
return this.value;
}

delete() {
if (!this.#initialized && !this.#init()) {
return false;
}

this.value = null;
if (this.#key) localStorage.removeItem(this.#key);

return true;
}

#validateValue() {
if (this.value) {
this.set(this.value);
}
}

#init() {
try {
if (!this.#initialized && typeof window !== 'undefined') {
// init with prev key/value
this.#key = Object.keys(localStorage).find((sk) => sk.startsWith(this.#prefix));

// set initialized as soon as we managed to extract data
// from localStorage, so the `set` call below won't result
// in a endless-recursive loop
this.#initialized = true;
if (this.#key) {
this.set(localStorage.getItem(this.#key));
}
}
} catch (err) {
console.warn(err);
}
return this.#initialized;
}
}
40 changes: 0 additions & 40 deletions src/wallet/getst/main.d.ts

This file was deleted.

131 changes: 131 additions & 0 deletions src/wallet/getst/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* eslint-disable @typescript-eslint/no-shadow */
import { type StarknetWindowObject } from './StarknetWindowObject';
import discovery, { type WalletProvider } from './discovery';
import { LocalStorageWrapper } from './localStorageStore';
import { Permission } from './rpcMessage';
import type { GetStarknetOptions, GetStarknetResult } from './types';
import { pipe } from './utils';
import { filterBy, filterByAuthorized } from './wallet/filter';
import { isWalletObj } from './wallet/isWalletObject';
import { scanObjectForWallets } from './wallet/scan';
import { sortBy } from './wallet/sort';

export type { StarknetWindowObject } from './StarknetWindowObject';

export type {
AddDeclareTransactionParameters,
AddDeclareTransactionResult,
AddDeployAccountTransactionParameters,
AddDeployAccountTransactionResult,
AddInvokeTransactionParameters,
AddInvokeTransactionResult,
AddStarknetChainParameters,
RequestAccountsParameters,
StarknetChainId,
SwitchStarknetChainParameters,
GetDeploymentDataResult,
WatchAssetParameters,
TypedData,
RequestFn,
RpcMessage,
IsParamsOptional,
RpcTypeToMessageMap,
RequestFnCall,
} from './rpcMessage';

export type {
WalletEvents,
AccountChangeEventHandler,
NetworkChangeEventHandler,
WalletEventHandlers,
} from './walletEvents';

export { Permission } from './rpcMessage';

export type {
DisconnectOptions,
GetStarknetOptions,
GetStarknetResult,
GetWalletOptions,
WalletProvider,
} from './types';

const ssrSafeWindow = typeof window !== 'undefined' ? window : {};

const defaultOptions: GetStarknetOptions = {
windowObject: ssrSafeWindow,
isWalletObject: isWalletObj,
storageFactoryImplementation: (name: string) => new LocalStorageWrapper(name),
};

export function getStarknet(options: Partial<GetStarknetOptions> = {}): GetStarknetResult {
const { storageFactoryImplementation, windowObject, isWalletObject } = {
...defaultOptions,
...options,
};
const lastConnectedStore = storageFactoryImplementation('gsw-last');

return {
getAvailableWallets: async (options = {}) => {
const availableWallets = scanObjectForWallets(windowObject, isWalletObject);
return pipe<StarknetWindowObject[]>(
(_) => filterBy(_, options),
(_) => sortBy(_, options.sort)
)(availableWallets);
},
getAuthorizedWallets: async (options = {}) => {
const availableWallets = scanObjectForWallets(windowObject, isWalletObject);
return pipe<StarknetWindowObject[]>(
(_) => filterByAuthorized(_),
(_) => filterBy(_, options),
(_) => sortBy(_, options.sort)
)(availableWallets);
},
getDiscoveryWallets: async (options = {}) => {
return pipe<WalletProvider[]>(
(_) => filterBy(_, options),
(_) => sortBy(_, options.sort)
)(discovery);
},
getLastConnectedWallet: async () => {
const lastConnectedWalletId = lastConnectedStore.get();
const allWallets = scanObjectForWallets(windowObject, isWalletObject);
const lastConnectedWallet = allWallets.find((w) => w.id === lastConnectedWalletId);
const [firstAuthorizedWallet] = await filterByAuthorized(
lastConnectedWallet ? [lastConnectedWallet] : []
);

if (!firstAuthorizedWallet) {
lastConnectedStore.delete();
return null;
}

return firstAuthorizedWallet;
},
enable: async (wallet, options) => {
await wallet.request({
type: 'wallet_requestAccounts',
params: {
silentMode: options?.silentMode,
},
});

// check for permissions
const permissions = await wallet.request({
type: 'wallet_getPermissions',
});
if (!permissions?.includes(Permission.Accounts)) {
throw new Error('Failed to connect to wallet');
}
lastConnectedStore.set(wallet.id);
return wallet;
},
disconnect: async ({ clearLastWallet } = {}) => {
if (clearLastWallet) {
lastConnectedStore.delete();
}
},
};
}

export default getStarknet();
Loading

0 comments on commit 017702f

Please sign in to comment.