-
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.
- Loading branch information
Showing
8 changed files
with
142 additions
and
39 deletions.
There are no files selected for viewing
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,21 @@ | ||
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 */ | ||
abstract 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> | ||
} | ||
|
||
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,80 @@ | ||
import { AccountInterface, Provider } from 'starknet' | ||
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' | ||
readonly ready = typeof window != 'undefined' && !!window.starknet | ||
|
||
private starknet = getStarknet() | ||
|
||
constructor(options?: InjectedConnectorOptions) { | ||
super({ options }) | ||
} | ||
|
||
async connect() { | ||
if (!this.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 (!this.ready) { | ||
throw new ConnectorNotFoundError() | ||
} | ||
|
||
return this.starknet.account | ||
? Promise.resolve(this.starknet.account) | ||
: Promise.reject(new ConnectorNotConnectedError()) | ||
} | ||
} | ||
|
||
export type EventHandler = (accounts: string[]) => void | ||
|
||
interface IStarknetWindowObject { | ||
enable: (options?: { showModal?: boolean }) => Promise<string[]> | ||
isPreauthorized: () => Promise<boolean> | ||
on: (event: 'accountsChanged', handleEvent: EventHandler) => void | ||
off: (event: 'accountsChanged', handleEvent: EventHandler) => void | ||
account?: AccountInterface | ||
provider: Provider | ||
selectedAddress?: string | ||
version: string | ||
} | ||
|
||
interface ConnectedStarknetWindowObject extends IStarknetWindowObject { | ||
isConnected: true | ||
account: AccountInterface | ||
selectedAddress: string | ||
} | ||
|
||
interface DisconnectedStarknetWindowObject extends IStarknetWindowObject { | ||
isConnected: false | ||
} | ||
|
||
export type StarknetWindowObject = ConnectedStarknetWindowObject | DisconnectedStarknetWindowObject |
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