Bitcoinjs-lib: Newb question, but after running the sample script I get Uncaught ReferenceError: bitcoin is not defined

Created on 22 Jul 2016  路  10Comments  路  Source: bitcoinjs/bitcoinjs-lib

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<p id="demo">A Paragraph.</p>

<button type="button" onclick="myFunction()">Try it</button>

<script src='https://cdnjs.cloudflare.com/ajax/libs/bitcoinjs-lib/0.2.0-1/bitcoinjs-min.js'></script>

<script>
function myFunction() {

    var keyPair = bitcoin.ECPair.makeRandom()

    // Print your private key (in WIF format)
    console.log(keyPair.toWIF())
    // => Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct

    // Print your public key address
    console.log(keyPair.getAddress())
    // => 14bZ7YWde4KdRb5YN7GYkToz3EHVCvRxkF

  //  key = Bitcoin.ECKey.makeRandom();
    //document.getElementById("demo").innerHTML = key.toWIF();
}
</script>

</body>
</html>
how to / question / docs

All 10 comments

I'm not sure where https://cdnjs.cloudflare.com/ajax/libs/bitcoinjs-lib/0.2.0-1/bitcoinjs-min.js comes from, nevermind: was added to cdnjs here, but it certainly isn't supported by bitcoinjs maintainers.

For your issue, you must use the Bitcoin namespace, but even then, the API version is too old, we are up to 2.x.y, you are using 0.2.0.

@chris41188 I suggest you read this in its entirety, and try again.

Thanks,

yes I tried this but an error was thrown here
$ browserify foo.js -s foobar > bar.js

is there a place I can get a pre browserfied version?

What was the error?

I created the foobar.js, added the code and ran this

as-MacBook-Pro:~ a$ sudo nano foobar.js
as-MacBook-Pro:~ a$ sudo browserify foo.js -s foobar > bar.js
Error: Cannot find module '/Users/a/foo.js' from '/Users/a'

Don't use sudo, there is no need.

Also, it appears the README has a mistake which I just fixed in https://github.com/bitcoinjs/bitcoinjs-lib/pull/608.
To fix your issue:

mv foobar.js foo.js
browserify foo.js -s foobar > bar.js

Perhaps more semantic names will be more helpful than foo and bar.
I was trying to keep it generic.

Thanks for your help I will give it another try

Everything is working well now, thank you

Awesome, thanks for making the issue!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhaozhiming picture zhaozhiming  路  3Comments

thrastarson picture thrastarson  路  3Comments

prahaladbelavadi picture prahaladbelavadi  路  3Comments

Beardcoding picture Beardcoding  路  3Comments

itsMikeLowrey picture itsMikeLowrey  路  3Comments