Coder Social home page Coder Social logo

Comments (6)

luu-alex avatar luu-alex commented on July 22, 2024 1

Hey there @zurikdev thanks for the issue, we'll take a look into this

from web3.js.

SantiagoDevRel avatar SantiagoDevRel commented on July 22, 2024

Hi @zurikdev , which version of web3.js are you using?
Here is a working example of what I think you are trying to do:
https://docs.web3js.org/guides/wallet/transactions#sending-a-raw-transaction
Pls let us know if this doesn't work and what error you are getting

from web3.js.

zurikdev avatar zurikdev commented on July 22, 2024

I'm currently using the web3.js v4.8.0.

from web3.js.

luu-alex avatar luu-alex commented on July 22, 2024

Your saying after web3.eth.sign the address is changed? What provider are you using as well?

from web3.js.

zurikdev avatar zurikdev commented on July 22, 2024

Your saying after web3.eth.sign the address is changed? What provider are you using as well?

Yes, after it got signed and rvs added to the transaction, the from address changes.

from web3.js.

jdevcs avatar jdevcs commented on July 22, 2024

@zurikdev I saw you are using web3.eth.sign(sha3_, target) for signing.
target address in above call will be used for signing regardless what from you specify in your transaction in above scenario. so it depends on which provider you injected in web3 because in that injected wallet there must be account with target address and it must be unlocked. Based on these I created minimum code to sign and recover signer address and it works fine:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Web3 Signing Example</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
        }

        #result {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #f9f9f9;
        }
    </style>
</head>

<body>
    <h1>Web3 Signing Example</h1>
    <button id="signButton">Sign Message</button>
    <div id="result"></div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const signButton = document.getElementById('signButton');
            const resultDiv = document.getElementById('result');

            let web3;

            async function initWeb3() {
                if (typeof window.ethereum !== 'undefined') {
                    try {
                        // Request account access
                        await window.ethereum.request({ method: 'eth_requestAccounts' });
                        web3 = new Web3(window.ethereum);
                        console.log('Web3 initialized with MetaMask');
                    } catch (error) {
                        console.error('User denied account access');
                    }
                } else {
                    console.log('MetaMask not detected');
                }
            }

            async function signMessage() {
                if (!web3) {
                    resultDiv.textContent = 'Web3 is not initialized. Please make sure MetaMask is installed and connected.';
                    return;
                }

                try {
                    const accounts = await web3.eth.getAccounts();
                    const message = "Hello, World!";

                    // Hash the message
                    const messageHash = web3.utils.sha3(message);

                    const addr = "0x....."; // account with this address must be present and unlocked in injected wallet
                    const signature = await web3.eth.sign(messageHash,  addr);

                    // Recover the signer address
                    const signerAddress = web3.eth.accounts.recover(messageHash, signature, true);

                    console.log('Signer address:', signerAddress);

                    
                    resultDiv.textContent = `Signature: ${signature} recovered: ${signerAddress} same:${addr==signerAddress}`;

                } catch (error) {
                    resultDiv.textContent = `Error: ${error.message}`;
                }
            }

            signButton.addEventListener('click', signMessage);

            // Initialize Web3 when the page loads
            initWeb3();
        });
    </script>
</body>

</html>

btw, you should be using personal sign or sign typed data instead of eth_sign

from web3.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.