Web3-js的学习(4)-实现自动账户解锁脚本

解锁账户

每次转币都需要事先把账户解锁,不然会报错,非常的麻烦。

可以将解锁的过程加到web3js的脚本中来处理。

但是其中还是有一些细节要注意:

  1. 首先在开启私链的时候需要在命令中赋予rpc调用personal这个api的权限,这一点至关重要。

因为默认不带api参数一般只提供eth、net等api,personal、db、admin等是不会向用户打开的,需要手动打开。

geth --datadir . --networkid 15 --rpc --rpcapi "eth,personal" console 2>output.log

  1. 在需要解锁才能调用的函数之前,使用如下代码:

web3.personal.unlockAccount(web3.eth.accounts[0], 'XXX(密码)', (err,res)=>{(回调函数)})

这里需要注意的是,解锁成功后的函数调用需要放在回调函数中才可。因为这是异步的。

实例

对于转代币合约的优化:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

var arguments = process.argv.splice(2);
var _from = arguments[0];	//发币者也是合约函数调用者
var _to = arguments[1];
var _value = arguments[2];
var _password = arguments[3];	//用户的账户密码

//创建合约实例
var abi = [{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"toAdd","type":"address"},{"name":"amount","type":"uint256"}],"name":"send","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"initalSupply","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Sent","type":"event"}];
var CoinContract = web3.eth.contract(abi);
var contractAddress = '0x767b276a86d36b66830a95720513e26a0773ca0c';
var contractInstance = CoinContract.at(contractAddress);

//解锁用户
web3.personal.unlockAccount(_from, _password, (err,res)=>{
	if (err)
		console.log('Error: ', err);
	else {
		//成功解锁才可转币
		//调用转币函数
		contractInstance.send(_to, _value, {from: _from}, (err,res)=>{
			if (err)
				console.log('Error: ', err);
			else
				console.log('Result: ', res);
		})
	}
		
});

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxx_undefined

谢谢请博主吃糖

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值