Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sign any message with the user’s private key and verify the signature on Ethereum

I'm trying to explore Ethereum and creating a app which let user sign message and validate the message. I'm using web3swift framework for this and what I have tried so far is as follows -

    let web3Rinkeby = Web3.InfuraRinkebyWeb3()
    let msgStr = "This is my first Ethereum App";
    let data = msgStr.data(using: .utf8)
    let signMsg = web3Rinkeby.wallet.signPersonalMessage(data!, account: address);

    print(signMsg);

but I'm not sure if this is right and how to validate any message as well. Please help.

like image 456
Suryakant Sharma Avatar asked Sep 22 '18 09:09

Suryakant Sharma


People also ask

How do I sign and verify messages on Ethereum?

To sign a message, click on the Sign Message button and you will be prompted to connect to our website via either MetaMask or WalletConnect. Once connected, you can enter the message that you want to sign and click Sign Message. Done! You should see the signature hash for your signed message right afterward.

How the signing and verification of a transaction take place in Ethereum?

Signing and Verifying Signatures Each account in the Ethereum network has a public key and a private key. An Ethereum address is essentially a hashed version of the public key. Accounts can use their private key to sign a piece of data, returning a signature of that data.

What is a signature in Ethereum?

Ethereum Signature is based on a blockchain-based electronic contract platform and compensation for the signature itself. Tokens generated within this ecosystem will become new digital assets and will be able to pay advisory services and electronic contract usage fees with Ethereum signature tokens.


1 Answers

Seems, that you didn't specify exact address. Here is an example:

let web3Rinkeby = Web3.InfuraRinkebyWeb3()

/// 
    let keystore = try! EthereumKeystoreV3(password: "")
    let keystoreManager = KeystoreManager([keystore!])
    web3Rinkeby.addKeystoreManager(keystoreManager)
    let address = keystoreManager.addresses![0]
///

let msgStr = "This is my first Ethereum App";
let data = msgStr.data(using: .utf8)
let signMsg = web3Rinkeby.wallet.signPersonalMessage(data!, account: address);

print(signMsg);

Here is an example, how to sign transactions in the tests of the project:

like image 162
skywinder Avatar answered Sep 28 '22 07:09

skywinder