StarknetKit and starknet-react

Connect to the Keon extension with StarknetKit or starknet-react, send transactions and sign messages

Keon's browser extension injects a standard Starknet wallet object at window.starknet_keon, with the wallet id keon. Any library that finds injected wallets by id can connect to it. This page shows the two most common ones.

The same code also works inside Keon's in-app browser on Android, because Keon injects the same object there. For a dApp opened in the phone's own browser, see Connect from Android.

StarknetKit

This is what the Keon test dApp uses.

Code
bash
1npm install starknetkit starknet

Connect

Code
ts
1import { connect, disconnect } from 'starknetkit'
2import { InjectedConnector } from 'starknetkit/injected'
3 
4const keon = new InjectedConnector({ options: { id: 'keon', name: 'Keon Wallet' } })
5 
6const { wallet, connectorData } = await connect({
7 connectors: [keon],
8 modalMode: 'alwaysAsk', // 'neverAsk' connects straight to Keon with no modal
9})
10 
11if (!wallet || !connectorData?.account) throw new Error('Not connected')
12console.log('Connected:', connectorData.account)

List Keon next to other wallets by adding more connectors to the array. StarknetKit shows them all in its modal.

Send a transaction

Code
ts
1const txHash = await wallet.request({
2 type: 'wallet_addInvokeTransaction',
3 params: {
4 calls: [
5 {
6 contract_address: '0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d', // STRK
7 entry_point: 'transfer',
8 calldata: [recipient, amountLow, amountHigh],
9 },
10 ],
11 },
12})

Prefer a starknet.js account? Ask the connector for one and use execute:

Code
ts
1import { RpcProvider } from 'starknet'
2 
3const account = await keon.account(new RpcProvider({ nodeUrl: 'https://your-starknet-rpc' }))
4const { transaction_hash } = await account.execute([
5 { contractAddress: '0x...', entrypoint: 'transfer', calldata: [recipient, amountLow, amountHigh] },
6])

Sign a message

Code
ts
1const signature = await wallet.request({ type: 'wallet_signTypedData', params: typedData })

typedData is a SNIP-12 object; see the example in Connect from Android.

Disconnect

Code
ts
1await disconnect()

This clears StarknetKit's state on your side. The user revokes access in Keon under Apps, Connected apps.

starknet-react

Code
bash
1npm install @starknet-react/core @starknet-react/chains starknet

Set up the provider

Code
tsx
1import { StarknetConfig, InjectedConnector, publicProvider } from '@starknet-react/core'
2import { mainnet, sepolia } from '@starknet-react/chains'
3 
4const connectors = [new InjectedConnector({ options: { id: 'keon', name: 'Keon Wallet' } })]
5 
6export function Providers({ children }: { children: React.ReactNode }) {
7 return (
8 <StarknetConfig chains={[mainnet, sepolia]} provider={publicProvider()} connectors={connectors}>
9 {children}
10 </StarknetConfig>
11 )
12}

injected({ id: 'keon' }) from @starknet-react/core does the same thing in one call.

Connect

Code
tsx
1import { useAccount, useConnect, useDisconnect } from '@starknet-react/core'
2 
3export function ConnectButton() {
4 const { connect, connectors } = useConnect()
5 const { disconnect } = useDisconnect()
6 const { address } = useAccount()
7 
8 if (address) return <button onClick={() => disconnect()}>Disconnect {address.slice(0, 6)}…</button>
9 
10 const keon = connectors.find((c) => c.id === 'keon')
11 return (
12 <button disabled={!keon?.available()} onClick={() => keon && connect({ connector: keon })}>
13 {keon?.available() ? 'Connect Keon' : 'Install Keon'}
14 </button>
15 )
16}

Send a transaction

Code
tsx
1import { useSendTransaction } from '@starknet-react/core'
2 
3export function SendStrk({ recipient }: { recipient: string }) {
4 const { sendAsync, isPending } = useSendTransaction({
5 calls: [
6 {
7 contractAddress: '0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d',
8 entrypoint: 'transfer',
9 calldata: [recipient, '1000000000000000000', '0'], // 1 STRK
10 },
11 ],
12 })
13 
14 return (
15 <button disabled={isPending} onClick={async () => console.log((await sendAsync()).transaction_hash)}>
16 Send 1 STRK
17 </button>
18 )
19}

Sign a message

Code
tsx
1import { useSignTypedData } from '@starknet-react/core'
2 
3export function SignIn() {
4 const { signTypedDataAsync } = useSignTypedData({ params: typedData })
5 return <button onClick={async () => console.log(await signTypedDataAsync())}>Sign in</button>
6}

What Keon shows the user

  • Connect opens Keon's connect screen, where the user picks the account and what your dApp may ask for. If you request something they didn't allow, Keon answers with error code 4100.
  • Every transaction is simulated first, so the user sees what leaves their wallet and the fee before approving.
  • Messages are shown in full. Keep sign-in messages plain: one that lets someone send transactions (an outside execution) or move tokens gets a red warning, and so does a message for another network.
  • Rejecting returns error code 4001. Treat it as "cancelled", not as a failure.

The full list of error codes is on the SDK Integration page.