Last active
April 21, 2022 02:27
-
-
Save giansalex/e7e0443d9e7faeef9f1f1da2ed393e9b to your computer and use it in GitHub Desktop.
Ethereum - Sign arbitrary data (sign cosmos tx bytes)
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
var Web3 = require('web3'); | |
var web3 = new Web3(window.web3.currentProvider); | |
var accountToSignWith = '0xbedcf417ff2752d996d2ade98b97a6f0bef4beb9'; | |
var message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Tubulum fuisse, qua illum, cuius is condemnatus est rogatione, P. Eaedem res maneant alio modo.' | |
function signHandler(err, signature) { | |
if (!err) { | |
console.log('Signature:', signature); | |
signature = signature.substr(2); | |
r = '0x' + signature.substr(0, 64); | |
s = '0x' + signature.substr(64, 64); | |
v = '0x' + signature.substr(128, 2) | |
console.log(' r:', r) | |
console.log(' s:', s) | |
console.log(' v:', v) | |
} else { | |
console.error('Coult not sign message:', err); | |
} | |
} | |
function ascii_to_hex(str) | |
{ | |
var arr1 = []; | |
for (var n = 0, l = str.length; n < l; n ++) | |
{ | |
var hex = Number(str.charCodeAt(n)).toString(16); | |
arr1.push(hex); | |
} | |
return arr1.join(''); | |
} | |
console.log('Message to sign:', message); | |
console.log('Sign with account:', accountToSignWith); | |
var messageHex = '0x' + ascii_to_hex(message); | |
// var messageHex = '0x' + keccak256(signDocBytes); // cosmos chain | |
window.ethereum.enable().then(function() { | |
web3.eth.sign(messageHex, accountToSignWith, signHandler); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment