Web3-Js: Ethereum signature data and verification

Data signature:
web3.eth.sign(data, address)
Unlock account:
web3.eth.personal.unlockAccount(address, password, unlockDuraction)
unlockDuration Duration of the account remains unlocked
Verify signature:
web3.eth.accounts.recover() ;

const Web3 = require('web3');
const ethereumUri = 'http://127.0.0.1:8545';
const web3 = new Web3(new Web3.providers.HttpProvider(ethereumUri));
const address = "以太坊账户";  //签名账户,需要解锁

 let msg = web3.utils.utf8ToHex("Hello world")  //要发送的data
//let msg = "Hello world";
//签名 有好多种方式去签名
let signature = web3.eth.sign(msg,address).then(function(receipte){
	
	console.log(receipte);
	
	let r = receipte.slice(0, 66)
	let s = '0x' + receipte.slice(66, 130)
	let v = '0x' + receipte.slice(130, 132)
	
	console.log('r', r) //签名的前32个字节
	console.log('s', s) //签名的后32个字节
	console.log('v', v)//恢复值+ 27
	console.log(web3.utils.toDecimal(v));


	//验证
	console.log("原始rlp编码验证:",web3.eth.accounts.recover(msg,receipte ));
	
	// message, v, r, s
	console.log("vrs验证:",web3.eth.accounts.recover(msg,v,r,s));

	//打印下签名的人
	console.log("签名者是",address)
  }).catch((error)=>{
	console.log(error);
 });



Guess you like

Origin blog.csdn.net/xia_xu_liang/article/details/108166960
Recommended