-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connectors): modularize connectors
- Loading branch information
Showing
14 changed files
with
138 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@starknet-react/core': minor | ||
--- | ||
|
||
Modularize connectors |
6 changes: 3 additions & 3 deletions
6
examples/starknet-react-next/src/components/ConnectWallet.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { useStarknet } from '@starknet-react/core' | ||
import { useStarknet, InjectedConnector } from '@starknet-react/core' | ||
|
||
export function ConnectWallet() { | ||
const { account, connectBrowserWallet } = useStarknet() | ||
const { account, connect } = useStarknet() | ||
|
||
if (account) { | ||
return <p>Account: {account}</p> | ||
} | ||
|
||
return <button onClick={connectBrowserWallet}>Connect</button> | ||
return <button onClick={() => connect(new InjectedConnector())}>Connect</button> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { AccountInterface } from 'starknet' | ||
|
||
export abstract class Connector<Options = any> { | ||
/** Unique connector id */ | ||
abstract readonly id: string | ||
/** Connector name */ | ||
abstract readonly name: string | ||
/** Whether connector is usable */ | ||
static readonly ready: boolean | ||
/** Options to use with connector */ | ||
readonly options: Options | ||
|
||
constructor({ options }: { options: Options }) { | ||
this.options = options | ||
} | ||
|
||
abstract connect(): Promise<AccountInterface> | ||
abstract account(): Promise<AccountInterface> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { Connector } from './base' | ||
export { InjectedConnector } from './injected' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getStarknet } from '@argent/get-starknet' | ||
|
||
import { Connector } from './index' | ||
import { | ||
ConnectorNotConnectedError, | ||
ConnectorNotFoundError, | ||
UserRejectedRequestError, | ||
} from '../errors' | ||
|
||
type InjectedConnectorOptions = { | ||
showModal?: boolean | ||
} | ||
|
||
export class InjectedConnector extends Connector<InjectedConnectorOptions> { | ||
readonly id = 'injected' | ||
readonly name = 'argent' | ||
static readonly ready = typeof window != 'undefined' && !!window.starknet | ||
|
||
private starknet = getStarknet() | ||
|
||
constructor(options?: InjectedConnectorOptions) { | ||
super({ options }) | ||
} | ||
|
||
async connect() { | ||
if (!InjectedConnector.ready) { | ||
throw new ConnectorNotFoundError() | ||
} | ||
|
||
try { | ||
await this.starknet.enable(this.options) | ||
} catch { | ||
// NOTE: Argent v3.0.0 swallows the `.enable` call on reject, so this won't get hit. | ||
throw new UserRejectedRequestError() | ||
} | ||
|
||
if (!this.starknet.isConnected) { | ||
// NOTE: Argent v3.0.0 swallows the `.enable` call on reject, so this won't get hit. | ||
throw new UserRejectedRequestError() | ||
} | ||
|
||
return this.starknet.account | ||
} | ||
|
||
account() { | ||
if (!InjectedConnector.ready) { | ||
throw new ConnectorNotFoundError() | ||
} | ||
|
||
return this.starknet.account | ||
? Promise.resolve(this.starknet.account) | ||
: Promise.reject(new ConnectorNotConnectedError()) | ||
} | ||
} | ||
|
||
export type EventHandler = (accounts: string[]) => void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export class ConnectorAlreadyConnectedError extends Error { | ||
name = 'ConnectorAlreadyConnectedError' | ||
message = 'Connector already connected' | ||
} | ||
|
||
export class ConnectorNotConnectedError extends Error { | ||
name = 'ConnectorNotConnectedError' | ||
message = 'Connector not connected' | ||
} | ||
|
||
export class ConnectorNotFoundError extends Error { | ||
name = 'ConnectorNotFoundError' | ||
message = 'Connector not found' | ||
} | ||
|
||
export class UserRejectedRequestError extends Error { | ||
name = 'UserRejectedRequestError' | ||
message = 'User rejected request' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
import { defaultProvider, ProviderInterface } from 'starknet' | ||
import { Connector } from '../../connectors' | ||
|
||
export interface StarknetState { | ||
account?: string | ||
hasStarknet: boolean | ||
connectBrowserWallet: () => void | ||
connect: (connector: Connector) => void | ||
library: ProviderInterface | ||
error?: string | ||
} | ||
|
||
export const STARKNET_INITIAL_STATE: StarknetState = { | ||
account: undefined, | ||
hasStarknet: false, | ||
connectBrowserWallet: () => undefined, | ||
connect: () => undefined, | ||
library: defaultProvider, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters