Created
August 8, 2025 11:27
-
-
Save nCally/08bf901e2b24036c5ebf21c710f6b145 to your computer and use it in GitHub Desktop.
Example flow of sign in with Ethereum
This file contains hidden or 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
| import { createWalletClient, http } from "viem"; | |
| import { privateKeyToAccount } from "viem/accounts"; | |
| import { assetChainTestnet } from "viem/chains"; | |
| import dotenv from "dotenv"; | |
| import readline from 'readline'; | |
| import { stdin, stderr, exit, env } from 'process'; | |
| dotenv.config(); | |
| // Create readline interface for user input | |
| const rl = readline.createInterface({ | |
| input: stdin, | |
| output: stderr // Use stderr for prompts so stdout only contains the signature | |
| }); | |
| // Function to prompt for user input | |
| function prompt(question) { | |
| return new Promise((resolve) => { | |
| rl.question(question, (answer) => { | |
| resolve(answer); | |
| }); | |
| }); | |
| } | |
| async function main() { | |
| try { | |
| // Prompt for nonce value | |
| const stringFromBackendApi = await prompt("Enter the nonce value: "); | |
| const privateKey = env.PK; | |
| if (!stringFromBackendApi) { | |
| console.error("Error: Nonce value is required"); | |
| rl.close(); | |
| exit(1); | |
| } | |
| const account = privateKeyToAccount(privateKey); | |
| const client = createWalletClient({ | |
| transport: http(), | |
| chain: assetChainTestnet, | |
| account, | |
| }); | |
| const signature = await client.signMessage({ | |
| account, | |
| message: `Sign in to Asset Chain Liquidity: ${stringFromBackendApi}`, | |
| }); | |
| // Close readline before outputting signature | |
| rl.close(); | |
| // Only output the signature to stdout (for piping) | |
| console.log(signature); | |
| // Output additional info to stderr | |
| console.error("\nSignature has been generated and copied to clipboard if piped to pbcopy."); | |
| return signature; | |
| } catch (error) { | |
| console.error("Error:", error.message); | |
| rl.close(); | |
| exit(1); | |
| } | |
| } | |
| main(); | |
| // node sign-login.mjs --nonce=string | pbcopy | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment