Comments (6)
Hey there @zurikdev thanks for the issue, we'll take a look into this
from web3.js.
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.
I'm currently using the web3.js v4.8.0
.
from web3.js.
Your saying after web3.eth.sign the address is changed? What provider are you using as well?
from web3.js.
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.
@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)
- Invalid type for block transactions HOT 3
- Quicknode provider error rate limit
- Create documentation for Quicknode provider feature HOT 1
- Web3.js Wikipedia Page
- Add Http and ws options to web3-rpc-provider
- {Ethereum / Web3.js} TransactionRevertInstructionError: Transaction has been reverted by the EVM HOT 5
- failing suite - release canary
- Metamask Tutorial (using React)
- hello
- Circullar dependencies (Svelte + Rollup) HOT 3
- Tx middleware - Contracts support
- getFeeHistory type problem HOT 4
- decodeParameters (web3-eth-abi) should accept readonly AbiInput[] HOT 2
- I need web3 developer
- An issue when calling a method on diamonds proxy contract HOT 1
- abitype@npm:0.7.1 [20ff3] → >=4.9.4 ✘ HOT 2
- Getting addresses and amount from internal transaction (Coinbase exchange) HOT 1
- Create a Middleware Guide HOT 1
- redundant constructors for ContractBuilder HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web3.js.