Web3-Js:以太坊簽名數據以及驗證

數據簽名:
web3.eth.sign(data, address )
解鎖帳戶:
web3.eth.personal.unlockAccount(address, password, unlockDuraction)
unlockDuration 賬戶保持解鎖狀態的持續時間
驗證簽名:
web3.eth.accounts.recover();





web

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);
 });
相關文章
相關標籤/搜索