-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from FLock-io/development
Development
- Loading branch information
Showing
13 changed files
with
565 additions
and
82 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
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Chain, Connector, ConnectorData, WalletClient } from '@wagmi/core'; | ||
import { privateKeyToAccount } from 'viem/accounts'; | ||
import { PrivateKeyAccount, createWalletClient, http } from 'viem'; | ||
import { CONFIG } from './config'; | ||
|
||
export default class CustomConnector extends Connector<any, any> { | ||
readonly id = 'privateKey'; | ||
|
||
readonly name = 'Private Key Wallet'; | ||
|
||
readonly ready = true; | ||
|
||
#provider?: any; | ||
|
||
#account?: PrivateKeyAccount; | ||
|
||
constructor({ chains, options }: { chains?: Chain[]; options: any }) { | ||
super({ chains, options }); | ||
} | ||
|
||
async getProvider() { | ||
if (!this.#provider) { | ||
// Initialize the provider with the Ethereum JSON-RPC endpoint | ||
this.#provider = http(CONFIG.WEB3_AUTH_RPC); | ||
} | ||
return this.#provider; | ||
} | ||
|
||
async getAccount() { | ||
const privateKey = await this.getPrivateKey(); | ||
|
||
// Set up the provider with the private key | ||
const account = privateKeyToAccount(privateKey); | ||
this.#account = account; | ||
// return checksum address | ||
return account.address; | ||
} | ||
|
||
async getPrivateKey() { | ||
return localStorage.getItem('privateKey'); | ||
} | ||
|
||
async setPrivateKey(privateKey: string) { | ||
localStorage.setItem('privateKey', privateKey); | ||
} | ||
|
||
async connect({ chainId }: { chainId?: number } = {}): Promise< | ||
Required<ConnectorData> | ||
> { | ||
try { | ||
this.emit('message', { | ||
type: 'connecting', | ||
}); | ||
|
||
await this.getProvider(); | ||
await this.getAccount(); | ||
|
||
return { | ||
account: this.#account?.address as `0x${string}`, | ||
chain: { | ||
id: Number(chainId), | ||
unsupported: false, | ||
}, | ||
}; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
async getWalletClient({ | ||
chainId, | ||
}: { chainId?: number } = {}): Promise<WalletClient> { | ||
if (!this.#provider) { | ||
await this.getProvider(); | ||
} | ||
await this.getAccount(); | ||
|
||
const chain = this.chains.find((x) => x.id === chainId); | ||
|
||
return createWalletClient({ | ||
account: this.#account, | ||
chain, | ||
transport: this.#provider, | ||
}); | ||
} | ||
|
||
async isAuthorized() { | ||
try { | ||
await this.getAccount(); | ||
return !!this.#account; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
async disconnect() { | ||
// Clear the private key and provider | ||
this.setPrivateKey(undefined); | ||
this.#provider = undefined; | ||
} | ||
} |
Oops, something went wrong.