Bitcoin Forum
September 25, 2018, 04:28:33 PM *
News: ♦♦ New info! Bitcoin Core users absolutely must upgrade to previously-announced 0.16.3 [Torrent]. All Bitcoin users should temporarily trust confirmations slightly less. More info.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: How to use BitcoinJS in a normal website? (not using node.js or anything)  (Read 7830 times)
RingMaster
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
August 21, 2014, 10:14:21 AM
 #1

I'm trying to use BitcoinJS in a Bitcoin-related website. However, it seems to be written with node.js in mind, whereas I am just building a normal Html5 site.

Normally I just include a 3rd party javascript lib in my own page, and use it from there. However, this BitcoinJS lib has "require()" all over the place, and require.js doesn't help as it also contains references to other (external) stuff, like require('assert') or require('crypto-js') etc, which I assume are node.js specific modules or something.

How do I deal with this? The getting started instructions at bitcoinjs.org seem to involve "$ npm" but I don't know what $ is (is that from jquery, or something unix related?) nor do I know what "npm" is.
1537892913
Hero Member
*
Offline Offline

Posts: 1537892913

View Profile Personal Message (Offline)

Ignore
1537892913
Reply with quote  #2

1537892913
Report to moderator
1537892913
Hero Member
*
Offline Offline

Posts: 1537892913

View Profile Personal Message (Offline)

Ignore
1537892913
Reply with quote  #2

1537892913
Report to moderator
1537892913
Hero Member
*
Offline Offline

Posts: 1537892913

View Profile Personal Message (Offline)

Ignore
1537892913
Reply with quote  #2

1537892913
Report to moderator
Make a difference with your Ether.
Donate Ether for the greater good.
SPRING.WETRUST.IO
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1537892913
Hero Member
*
Offline Offline

Posts: 1537892913

View Profile Personal Message (Offline)

Ignore
1537892913
Reply with quote  #2

1537892913
Report to moderator
1537892913
Hero Member
*
Offline Offline

Posts: 1537892913

View Profile Personal Message (Offline)

Ignore
1537892913
Reply with quote  #2

1537892913
Report to moderator
1537892913
Hero Member
*
Offline Offline

Posts: 1537892913

View Profile Personal Message (Offline)

Ignore
1537892913
Reply with quote  #2

1537892913
Report to moderator
dabura667
Sr. Member
****
Offline Offline

Activity: 478
Merit: 250


View Profile
August 22, 2014, 01:23:17 PM
 #2

Crypto-JS is a vital part of BitcoinJS. It's basically all the Elliptic Curve calculations.

Without it you wouldn't be able to turn a private key into a public key.

Is the problem that you just don't understand it? or is it that you have some sort of limitation on your site?

If you have a limitation, please specify the limitation in detail, and someone can help you better.

My Tip Address:
1DXcHTJS2DJ3xDoxw22wCt11FeAsgfzdBU
R2D221
Hero Member
*****
Offline Offline

Activity: 658
Merit: 500



View Profile
August 22, 2014, 02:58:37 PM
 #3

Hive Wallet is already using bitcoinjs and crypto-js (I think). You could ask them how they used them.

An economy based on endless growth is unsustainable.
RingMaster
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
August 25, 2014, 10:39:13 AM
 #4

Crypto-JS is a vital part of BitcoinJS. It's basically all the Elliptic Curve calculations.

Without it you wouldn't be able to turn a private key into a public key.

Is the problem that you just don't understand it? or is it that you have some sort of limitation on your site?

If you have a limitation, please specify the limitation in detail, and someone can help you better.

Thanks for the help. I am familiar with Crypto-JS, I've used it before and I see how this is a vital part for BitcoinJS.

My problem is, several .js files in BitcoinJS have a line "require('crypto-js')" which is supposed to include Crypto-JS, but
1. require(...) is some NodeJS-specific construct, and not part of JavaScript.
2. the Crypto-JS library isn't even included in the BitcoinJS code, so I assume it requires some environment or setup where Crypto-JS is already available as some kind of pre-installed library or something.

There are many more require(...) lines, in fact most files require eachother in this way, so other than just the missing Crypto-JS thing I don't even know how to get this JavaScript library working at all.

Basically, what I need is just one big bitcoin.js (or bitcoin.min.js) file that contains everything. Apparently this can be produced with something called "Browserify" but that seems to require NodeJS as well Sad

throwawayasdasd
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
August 25, 2014, 04:12:38 PM
 #5

Hi,

You can get npm from nodejs website (nodejs.org). You can then install browserify and uglify (also using npm) to help you. If you trust me I can also send you bitcoin.min.js that I generated
RingMaster
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
August 26, 2014, 04:08:14 PM
 #6

Hi,

You can get npm from nodejs website (nodejs.org). You can then install browserify and uglify (also using npm) to help you. If you trust me I can also send you bitcoin.min.js that I generated
That would be cool, yes!

Actually, I messed around with NodeJS a bit further (installing npm and browserify and uglify etc) and I think I now managed to create a bitcoin.min.js on my own. That is, after following the instructions from BitcoinJS.org, the file is now there (about 220Kb).

However, when I try the first example from the BitcoinJS page, it doesn't work. It seems an error occurs right at the first line already, the one with Bitcoin.ECKey.makeRandom(), here is a minimal example .html file:

Code:
<script type='text/javascript' src='bitcoinjs.min.js'></script>
<script type='text/javascript'>
var key = Bitcoin.ECKey.makeRandom();
alert(key.toWIF());
</script>

And when I open this file:

Quote from: JavaScript Error Console
TypeMismatchError: The type of an object is incompatible with the expected type of the parameter associated to the object
(this occurs in the bitcoinjs.min.js file)

Does it work OK with your bitcoinjs.min.js? If yes, I'm really curious exactly how to create it!
throwawayasdasda
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile
August 27, 2014, 12:53:13 PM
 #7

My 3rd username for this website (I tend to use captcha as part of my password and end up not remembering it later). My bitcoin.min.js is also around 220kb so most likely you are on the right track. Browser choice is important as bitcoinjs doesn't work with older browser (IE <11?) and Android's WebView. I've tried the following code with Chrome:

<!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="bitcoinjs.min.js"></script>
<script>
function myFunction() {
   key = Bitcoin.ECKey.makeRandom();
    document.getElementById("demo").innerHTML = key.toWIF();
}
</script>

</body>
</html>

RingMaster
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
August 27, 2014, 02:34:44 PM
 #8

Thanks, interesting, I've copied your example html and tried that here in Chrome as well.

Still doesn't work, but Chrome's console shows this error:

Quote from: Chrome JavaScript console
Uncaught ReferenceError: Uint8ClampedArray is not defined
Which makes me wonder, is there anything I need to update or recompile? I would assume that by performing the npm and browserify steps (according to the BitcoinJS website) that I'm getting the latest version of everything, but I'm totally clueless regarding to all this NodeJS stuff Smiley

My previous attempts were done in Firefox (latest version).
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1013


I may write code in exchange for bitcoins.


View Profile
August 27, 2014, 02:40:26 PM
 #9

I'm trying to use BitcoinJS in a Bitcoin-related website. However, it seems to be written with node.js in mind, whereas I am just building a normal Html5 site.

Normally I just include a 3rd party javascript lib in my own page, and use it from there. However, this BitcoinJS lib has "require()" all over the place, and require.js doesn't help as it also contains references to other (external) stuff, like require('assert') or require('crypto-js') etc, which I assume are node.js specific modules or something.

How do I deal with this? The getting started instructions at bitcoinjs.org seem to involve "$ npm" but I don't know what $ is (is that from jquery, or something unix related?) nor do I know what "npm" is.

I can answer the last part.  "npm" is node package manager.  I'm using GNU/Linux, and after installing node/npm through the normal package manager on my system (apt-get), I use npm commands to install further node packages.

The "$" might refer to a bash prompt, which would be something "unix related" Smiley  But it might stand for the jquery object $.  Depending on the context.   If your examples all start:

$ npm ...

Then it's referring to the bash prompt on a unix-like system (OSX, GNU/Linux, etc).
RingMaster
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
August 27, 2014, 02:52:23 PM
 #10

I can answer the last part.  "npm" is node package manager.  I'm using GNU/Linux, and after installing node/npm through the normal package manager on my system (apt-get), I use npm commands to install further node packages.

The "$" might refer to a bash prompt, which would be something "unix related" Smiley  But it might stand for the jquery object $.  Depending on the context.   If your examples all start:

$ npm ...

Then it's referring to the bash prompt on a unix-like system (OSX, GNU/Linux, etc).
OK thanks. Well I'm using Windows only, but apparently, after running the NodeJS installer for Windows, when opening a command prompt and going to the NodeJS program directory, I can magically perform npm commands from there. So it seems the $ in this case also refers to the Windows command prompt.

However, I wonder if some Windows-incompatibilities of NodeJS or npm may be the cause of the strange issues I'm getting with the bitcoinjs.min.js I built myself...?
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1013


I may write code in exchange for bitcoins.


View Profile
August 27, 2014, 02:57:16 PM
 #11

I can answer the last part.  "npm" is node package manager.  I'm using GNU/Linux, and after installing node/npm through the normal package manager on my system (apt-get), I use npm commands to install further node packages.

The "$" might refer to a bash prompt, which would be something "unix related" Smiley  But it might stand for the jquery object $.  Depending on the context.   If your examples all start:

$ npm ...

Then it's referring to the bash prompt on a unix-like system (OSX, GNU/Linux, etc).
OK thanks. Well I'm using Windows only, but apparently, after running the NodeJS installer for Windows, when opening a command prompt and going to the NodeJS program directory, I can magically perform npm commands from there. So it seems the $ in this case also refers to the Windows command prompt.

However, I wonder if some Windows-incompatibilities of NodeJS or npm may be the cause of the strange issues I'm getting with the bitcoinjs.min.js I built myself...?

I can't address whether your issues have to do with windows incompatibilties as I haven't used windows in almost 10 years (switched to GNU/Linux and couldn't never wanted to back).  About the prompt, if you actually see a "$" prompt then I guess you're right that "$" might refer to this prompt.  The "$" classically indicates the bash shell on *nix these days because the other famous shells have a different character at the prompt (cshell uses %, IIRC).
throwawayasdasda
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile
August 27, 2014, 03:07:05 PM
 #12

Hmmm... I'm on Windows and it works fine for me. Not really sure what happened. My chrome version is 36.0.1985.143 m on Windows 7
throwawayasdasda
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile
August 27, 2014, 03:25:55 PM
 #13

Firefox also works fine. As a last ditch effort I can send you bitcoin.min.js if you PM me your email address (as a rule of thumb please be careful when receiving attachment)
RingMaster
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
August 28, 2014, 09:12:26 AM
 #14

Firefox also works fine. As a last ditch effort I can send you bitcoin.min.js if you PM me your email address (as a rule of thumb please be careful when receiving attachment)
Thanks, see PM
RingMaster
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
September 02, 2014, 05:10:41 PM
 #15

Thanks, received the file, and tested it locally. Didn't work either (same errors in Chrome and Firefox) so I'm starting to wonder if something is wrong with my system.... I'm gonna do a system reinstall and see if that makes things work Smiley
abstream
Member
**
Offline Offline

Activity: 81
Merit: 10


View Profile
December 13, 2014, 09:38:37 PM
 #16

Interesting, which docs did you follow for the browser version, is there separate that are not nodejs related?

Did you ever solve this?

I had a similar problem, but I got the following Javascript to work on a bare-bones HTML page:

Code:
<script src='http://cdn.bitcoinjs.org/libs/bitcoinjs-lib/latest/bitcoinjs-min.js'></script>
<script>
  var key = new Bitcoin.ECKey();
  alert(key.getBitcoinAddress().toString());
</script>

RingMaster
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
December 16, 2014, 11:45:27 AM
 #17

Did you ever solve this?

I had a similar problem, but I got the following Javascript to work on a bare-bones HTML page:
No, unfortunately I didn't get it working correctly.

Your example does work here (it shows an address), but if I change key = new Bitcoin.ECKey(); into key = bitcoin.ECKey.makeRandom(); it doesn't work.

Also, If I change key.getBitcoinAddress().toString() into key.toWIF() (to show the private key instead of the address) it doesn't work either.

Even though both these samples are litterely mentioned as such on the bitcoinjs.org frontpage Sad
In fact, if you search through the bitcoinjs-min.js file, the search terms 'makeRandom' and 'toWIF' don't even seem to exist at all in the entire .js (neither does 'fromWIF'). I've got a feeling that somehow the page is really outdated, or not being maintained anymore.

Too bad, this would be a great library, if it was clear how to actually use it Undecided
extro24
Sr. Member
****
Offline Offline

Activity: 481
Merit: 252


View Profile
December 29, 2014, 03:17:46 PM
 #18

Same problem.   Tongue

someguy123
Sr. Member
****
Offline Offline

Activity: 334
Merit: 250


PHP Coder, Bitcoin enthusiast, Litecointalk admin


View Profile WWW
January 01, 2015, 02:54:01 AM
 #19

Same problem.   Tongue

For those still having issues, I have a compiled version which works perfectly over here:

https://gist.github.com/Someguy123/49f849d693a063e4fbb2

Test it out like this:

Code:
<a href="#" onclick="randomAddress()">Generate Random Address</a>

<script src="bitcoinjs.min.js"></script>

<script>
function randomAddress() {
        var key = Bitcoin.ECKey.makeRandom();
        var PubKey = key.pub.getAddress().toString();
        var PrivKey = key.toWIF();
        // Hit F12 to enter the Chrome Developer Tools, hit ESC to access the console from there; On Mac, use ⌘-⌥ I
        console.log("Public Key: " + PubKey);
        console.log("Private Key: " + PrivKey);
}
</script>

1SoMGuYknDgyYypJPVVKE2teHBN4HDAh3
Administrator/owner of LTC Forum | LTC Block Explorer | Someguy123 Web Development
namuyang
Newbie
*
Offline Offline

Activity: 22
Merit: 0


View Profile
December 13, 2016, 12:56:27 PM
 #20

Same problem.   Tongue

For those still having issues, I have a compiled version which works perfectly over here:

https://gist.github.com/Someguy123/49f849d693a063e4fbb2

Test it out like this:

Code:
<a href="#" onclick="randomAddress()">Generate Random Address</a>

<script src="bitcoinjs.min.js"></script>

<script>
function randomAddress() {
        var key = Bitcoin.ECKey.makeRandom();
        var PubKey = key.pub.getAddress().toString();
        var PrivKey = key.toWIF();
        // Hit F12 to enter the Chrome Developer Tools, hit ESC to access the console from there; On Mac, use ⌘-⌥ I
        console.log("Public Key: " + PubKey);
        console.log("Private Key: " + PrivKey);
}
</script>
It works!
Thank you good man Cheesy
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!