From 9a697079433eda23f2d00092f72cabed991b4710 Mon Sep 17 00:00:00 2001 From: Maharshi Mishra Date: Wed, 27 Sep 2023 09:37:00 +0530 Subject: [PATCH] fixed sign method web3 --- .../sfa-next-jwt-example/app/evm.web3.ts | 31 +++++++++---------- .../sfa-react-vite-example/src/web3RPC.ts | 31 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/single-factor-auth-web/sfa-next-jwt-example/app/evm.web3.ts b/single-factor-auth-web/sfa-next-jwt-example/app/evm.web3.ts index 919280e4..176463eb 100644 --- a/single-factor-auth-web/sfa-next-jwt-example/app/evm.web3.ts +++ b/single-factor-auth-web/sfa-next-jwt-example/app/evm.web3.ts @@ -29,24 +29,23 @@ export default class EthereumRpc { } } - async signMessage(): Promise { + async signMessage() { try { - const web3 = new Web3(this.provider as IProvider); - const accounts = await web3.eth.getAccounts(); - const message = "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"; - (web3.currentProvider as any)?.send( - { - method: "eth_sign", - params: [accounts[0], message], - from: accounts[0], - }, - (err: Error, result: any) => { - if (err) { - return console.error(err); - } - return result; - } + const web3 = new Web3(this.provider as any); + + // Get user's Ethereum public address + const fromAddress = (await web3.eth.getAccounts())[0]; + + const originalMessage = "YOUR_MESSAGE"; + + // Sign the message + const signedMessage = await web3.eth.personal.sign( + originalMessage, + fromAddress, + "test password!" // configure your own password here. ); + + return signedMessage; } catch (error) { return error as string; } diff --git a/single-factor-auth-web/sfa-react-vite-example/src/web3RPC.ts b/single-factor-auth-web/sfa-react-vite-example/src/web3RPC.ts index 919280e4..176463eb 100644 --- a/single-factor-auth-web/sfa-react-vite-example/src/web3RPC.ts +++ b/single-factor-auth-web/sfa-react-vite-example/src/web3RPC.ts @@ -29,24 +29,23 @@ export default class EthereumRpc { } } - async signMessage(): Promise { + async signMessage() { try { - const web3 = new Web3(this.provider as IProvider); - const accounts = await web3.eth.getAccounts(); - const message = "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"; - (web3.currentProvider as any)?.send( - { - method: "eth_sign", - params: [accounts[0], message], - from: accounts[0], - }, - (err: Error, result: any) => { - if (err) { - return console.error(err); - } - return result; - } + const web3 = new Web3(this.provider as any); + + // Get user's Ethereum public address + const fromAddress = (await web3.eth.getAccounts())[0]; + + const originalMessage = "YOUR_MESSAGE"; + + // Sign the message + const signedMessage = await web3.eth.personal.sign( + originalMessage, + fromAddress, + "test password!" // configure your own password here. ); + + return signedMessage; } catch (error) { return error as string; }