0

I am trying to generate WIF from extendedprivatekey, but i am getting invalid WIF.

How should WIF look : https://bitcoin.org/en/developer-guide#wallet-import-format-wif

I've followed instructions from above link and got this code:

 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));
    };

I suspect there is somewhere converting error.

EDIT : I've figured it myself. Will post solution later.

klonodo
  • 198
  • 2
  • 9

1 Answers1

1

I found the answer to this buried in the WalletKey.js code:

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();
ece-acar
  • 19
  • 3