我正在尝试从extendedprivatekey生成WIF,但是我的WIF无效。
WIF应如何显示:https://bitcoin.org/en/developer-guide#wallet-import-format-wif
我已按照上述链接的说明获取此代码:
var crypto = require('crypto');
var bitcore = require('bitcore');
var HierarchicalKey = bitcore.HierarchicalKey;
var Address = bitcore.Address;
var networks = bitcore.networks;
var coinUtil = bitcore.util;
var knownBytes = coinUtil.sha256('testing');
var hkey = HierarchicalKey.seed(knownBytes,'testnet');
var key = new Buffer(hkey.derive('m/0\'/0/0').extendedPrivateKey);
var hash = new Buffer ([0xef].concat(key).concat([1]));
var hashses = doubleSHA256(hash);
var checksum = hashses.slice(0, 4);
var data = Buffer.concat([hash, checksum]);
var ress = bitcore.base58.encode(data);
console.log(ress);
function sha256(data) {
return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary');
};
function doubleSHA256(data) {
return sha256(sha256(data));
};
我怀疑转换错误的地方。
编辑:我自己也想过了。稍后会发布解决方案。答案 0 :(得分:2)
我在WalletKey.js代码中找到了这个答案:
var bitcore = require('bitcore');
var HierarchicalKey = bitcore.HierarchicalKey;
var Address = bitcore.Address;
var networks = bitcore.networks;
var coinUtil = bitcore.util;
var knownBytes = coinUtil.sha256('testing');
var hkey = HierarchicalKey.seed(knownBytes,'testnet');
var derived = hkey.derive('m/0\'/0/0');
var priv = new bitcore.PrivateKey(networks.testnet.privKeyVersion, derived.eckey.private, derived.eckey.compressed);
var wif = priv.toString();