如何从私钥文件得到私钥

最近一个问题卡住我好几天。。各种百度谷歌都没有找到,现在简单记录下

var privateKey = new Buffer('660acdf8ff31eea85d6b754238da16de45840190a9c8ed4b8aa27465b83f9a93', 'hex');
console.log(web3.toHex(web3.eth.gasPrice));
var rawTx = {
    nonce: '0x09',
    gasPrice: web3.toHex(web3.eth.gasPrice * 30),
    gasLimit: '0x800000000000000',
    gas:web3.toHex('210000000'),
    to: '0x640269C272C7e5f21Ba64661b5fAA94805503ba0',
    value: web3.toHex(web3.toWei('100', 'ether'))
    // data: '0x00'
};
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
console.log(serializedTx.toString('hex'));
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
    if (!err){
        console.log(hash);
    }else {
        console.log(err.toString());
    }
});

{"address":"a08b66c602eca33e065387ad2c02e781aec06ed7","crypto":{"cipher":"aes-128-ctr","ciphertext":"50b9da2797166585b4593182a45cf4b885790ed935375cc968ed5497868eb8c6","cipherparams":{"iv":"0a952f69e9c2ca8655a81baee01dd23c"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"320db5a821ab9f99c878b4d7f04e938c0de31c3e2e0af8c51bbe417c914627b3"},"mac":"8c23e0a90772eced716f041dd33e0be148140ca111c44d9a3164c047f4972c03"},"id":"a7eec16d-ac91-4855-9321-760e3e0770cd","version":3}

上面是私钥文件

就是web3.eth.sendRwaTransaction这个方法,我看了官方文档就这这么写。。一直报错说账户资金不足,

最后才发现,Buffer里面放的是私钥。。我一直以为就是字段ciphertext的值。。我一度以为是我的gas,gasPrice设置有问题,方向都错了,越跑越偏啊

不扯了,那个Buffer里的字符串,是秘钥文件通过密码解码出来的

这是目前我找的比较好的方法,参考https://ethereum.stackexchange.com/questions/3720/how-do-i-get-the-raw-private-key-from-my-mist-keystore-file

var keyth=require('keythereum');
var keyobj=keyth.importFromFile('账户地址','私钥文件地址,到keystore');
var privateKey=keyth.recover('password',keyobj);
console.log(privateKey.toString('hex'));

你可能感兴趣的:(区块链)