Should the "Generate a address from a SHA256 hash" example work in the browser?

#1

I’m trying to use the pre-generated bitcore-lib.js file in my website. I have successfully gotten the first and third examples to work, but not the second one. Is this something that should run in the browser, or is that something that needs to be run in node.js?

function myFunction() {
var bitcore = require(‘bitcore-lib’);

// Example 1
//    var privateKey = new bitcore.PrivateKey();
//    var address = privateKey.toAddress();

//  Example 2
    var value = new Buffer('correct horse battery staple');
    var hash = bitcore.crypto.Hash.sha256(value);
    var bn = bitcore.crypto.BN.fromBuffer(hash);
    var address = new bitcore.PrivateKey(bn).toAddress();

// Example 3    
//    var wif = 'Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct';
//    var address = new bitcore.PrivateKey(wif).toAddress();
    
    alert(address);
}
0 Likes

(Braydon Fuller) #2

Buffer is part of the Node.js API, however there is a browser compatible version packaged:

var hashBuffer = new bitcore.deps.Buffer('00000000004b8e21fd58e659ebde875b01407c85943a38807b17afc51ac36e83', 'hex');
0 Likes