bitcoinjs-lib/README.md

187 lines
6.7 KiB
Markdown
Raw Normal View History

2014-07-01 19:30:14 +02:00
# BitcoinJS (bitcoinjs-lib)
2011-05-06 01:08:46 +02:00
2014-04-08 16:53:31 +02:00
[![Build Status](https://travis-ci.org/bitcoinjs/bitcoinjs-lib.png?branch=master)](https://travis-ci.org/bitcoinjs/bitcoinjs-lib) [![Coverage Status](https://coveralls.io/repos/bitcoinjs/bitcoinjs-lib/badge.png)](https://coveralls.io/r/bitcoinjs/bitcoinjs-lib)
2014-03-14 04:37:22 +01:00
2014-07-02 05:29:58 +02:00
[![NPM](https://nodei.co/npm/bitcoinjs-lib.png)](https://nodei.co/npm/bitcoinjs-lib/)
2014-07-02 05:27:58 +02:00
2014-03-14 19:15:56 +01:00
[![Browser Support](https://ci.testling.com/bitcoinjs/bitcoinjs-lib.png)](https://ci.testling.com/bitcoinjs/bitcoinjs-lib)
2013-04-21 11:04:23 +02:00
2014-07-01 19:30:14 +02:00
The pure JavaScript Bitcoin library for node.js and browsers.
A continued implementation of the original `0.1.3` version used by over a million wallet users; the backbone for almost all Bitcoin web wallets in production today.
2014-07-01 19:30:14 +02:00
## Features
2014-04-23 17:36:46 +02:00
2014-07-01 19:30:14 +02:00
- Clean: Pure JavaScript, concise code, easy to read.
- Tested: Coverage > 90%, third-party integration tests.
- Careful: Two person approval process for small, focused pull requests.
- Compatible: Works on Node.js and all modern browsers.
- Powerful: Support for advanced features, such as multi-sig, HD Wallets.
- Secure: Strong random number generation, PGP signed releases, trusted developers.
- Principled: No support for browsers with crap RNG (IE < 11)
- Standardized: Node community coding style, Browserify, Node's stdlib and Buffers.
- Fast: Optimized code, uses typed arrays instead of byte arrays for performance.
- Experiment-friendly: Bitcoin Mainnet and Testnet support.
- Altcoin-ready: Capable of working with bitcoin-derived cryptocurrencies (such as Dogecoin).
2014-03-08 22:08:08 +01:00
2014-07-01 19:30:14 +02:00
## Should I use this in production?
2014-07-02 01:02:52 +02:00
If you are thinking of using the master branch of this library in production, *stop*.
2014-07-01 19:30:14 +02:00
Master is not stable; it is our development branch, and only tagged releases may be classified as stable.
2011-05-06 01:08:46 +02:00
2014-07-02 01:02:52 +02:00
If you are looking for the original, it is tagged as `0.1.3`. Unless you need it for dependency reasons, it is strongly recommended that you use (or upgrade to) the newest version, which adds major functionality, cleans up the interface, fixes many bugs, and adds over 1,300 more tests.
2011-05-06 01:08:46 +02:00
2014-03-08 22:08:08 +01:00
## Installation
2011-12-20 12:47:50 +01:00
2014-03-01 14:03:37 +01:00
`npm install bitcoinjs-lib`
2014-03-19 04:10:39 +01:00
## Setup
2014-03-08 22:08:08 +01:00
### Node.js
var bitcoin = require('bitcoinjs-lib')
From the repo:
2014-03-01 14:03:37 +01:00
2014-03-08 22:08:08 +01:00
var bitcoin = require('./src/index.js')
2014-03-01 14:03:37 +01:00
2014-03-08 22:08:08 +01:00
### Browser
2014-07-02 01:02:52 +02:00
From the repository: Compile `bitcoinjs-min.js` with the following command:
2014-03-08 22:08:08 +01:00
$ npm run-script compile
2014-07-02 01:02:52 +02:00
From NPM:
$ npm -g install bitcoinjs-lib browserify uglify-js
$ browserify -r bitcoinjs-lib -s Bitcoin | uglifyjs > bitcoinjs.min.js
After loading this file in your browser, you will be able to use the global `bitcoin` object.
2014-03-01 14:03:37 +01:00
2014-03-19 04:10:39 +01:00
## Usage
These examples assume you are running bitcoinjs-lib in the browser.
2014-03-19 04:10:39 +01:00
### Generating a Bitcoin address
```javascript
2014-06-04 16:04:59 +02:00
key = bitcoin.ECKey.makeRandom()
2014-03-19 04:10:39 +01:00
2014-05-24 02:17:14 +02:00
// Print your private key (in WIF format)
console.log(key.toWIF())
// => Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct
2014-03-19 04:10:39 +01:00
2014-05-24 02:17:14 +02:00
// Print your public key (toString defaults to a Bitcoin address)
console.log(key.pub.getAddress().toString())
// => 14bZ7YWde4KdRb5YN7GYkToz3EHVCvRxkF
2014-03-19 04:10:39 +01:00
```
### Creating a Transaction
```javascript
2014-06-04 16:04:59 +02:00
tx = new bitcoin.Transaction()
2014-03-19 04:10:39 +01:00
// Add the input (who is paying) of the form [previous transaction hash, index of the output to use]
tx.addInput("aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31", 0)
2014-03-19 04:10:39 +01:00
// Add the output (who to pay to) of the form [payee's address, amount in satoshis]
tx.addOutput("1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK", 15000)
2014-03-19 04:10:39 +01:00
2014-05-25 02:26:24 +02:00
// Initialize a private key using WIF
2014-06-04 16:04:59 +02:00
key = bitcoin.ECKey.fromWIF("L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy")
2014-03-19 04:10:39 +01:00
// Sign the first input with the new key
2014-03-19 04:10:39 +01:00
tx.sign(0, key)
// Print transaction serialized as hex
2014-06-13 03:06:44 +02:00
console.log(tx.toHex())
// => 0100000001313eb630b128102b60241ca895f1d0ffca2170d5a0990e094f2182c102ab94aa000000008a47304402200169f1f844936dc60df54e812345f5dd3e6681fea52e33c25154ad9cc23a330402204381ed8e73d74a95b15f312f33d5a0072c7a12dd6c3294df6e8efbe4aff27426014104e75628573696aed32d7656fb35e9c71ea08eb6492837e13d2662b9a36821d0fff992692fd14d74fdec20fae29128ba12653249cbeef521fc5eba84dde0689f27ffffffff01983a0000000000001976a914ad618cf4333b3b248f9744e8e81db2964d0ae39788ac00000000
// You could now push the transaction onto the Bitcoin network manually (see https://blockchain.info/pushtx)
2014-03-19 04:10:39 +01:00
```
2014-07-10 05:48:54 +02:00
### Creating a P2SH Multsig Address
``` javascript
var bitcoin = require('bitcoinjs-lib')
2014-07-10 05:48:54 +02:00
var privKeys = [bitcoin.ECKey.makeRandom(), bitcoin.ECKey.makeRandom(), bitcoin.ECKey.makeRandom()]
var pubKeys = privKeys.map(function(x) { return x.pub })
2014-07-10 05:48:54 +02:00
var redeemScript = bitcoin.scripts.multisigOutput(2, pubKeys) // 2 of 3
var scriptPubKey = bitcoin.scripts.scriptHashOutput(redeemScript.getHash())
2014-07-10 05:48:54 +02:00
var multisigAddress = bitcoin.Address.fromOutputScript(scriptPubKey).toString()
console.log("multisigP2SH:", multisigAddress)
// => multisigP2SH: 35k9EWv2F1X5JKXHSF1DhTm7Ybdiwx4RkD
2014-07-10 05:48:54 +02:00
```
2014-03-19 04:10:39 +01:00
2014-07-02 01:02:52 +02:00
## Projects utilizing BitcoinJS
2014-03-01 14:03:37 +01:00
- [Coinpunk](https://coinpunk.com)
- [Hive Wallet](https://www.hivewallet.com)
- [Justchain Exchange](https://justcoin.com)
- [Skyhook ATM](http://projectskyhook.com)
2014-07-02 01:02:52 +02:00
- [BitAddress](https://www.bitaddress.org)
- [Blockchain.info](https://blockchain.info/wallet)
2014-07-01 19:30:14 +02:00
- [Brainwallet](https://brainwallet.github.io)
2014-07-02 01:02:52 +02:00
- [Dark Wallet](https://darkwallet.unsystem.net)
- [Dogechain Wallet](https://dogechain.info)
- [GreenAddress](https://greenaddress.it)
2014-07-16 06:36:58 +02:00
- [DecentralBank](http://decentralbank.com)
2014-07-02 01:02:52 +02:00
## Contributors
Stefan Thomas is the inventor and creator of this project. His pioneering work made Bitcoin web wallets possible.
Since then, many people have contributed. [Click here](https://github.com/bitcoinjs/bitcoinjs-lib/graphs/contributors) to see the comprehensive list.
Daniel Cousens, Wei Lu, JP Richardson and Kyle Drake lead the major refactor of the library from 0.1.3 to 1.0.0.
2014-03-19 04:10:39 +01:00
## Contributing
Join the ongoing IRC development channel at `#bitcoinjs-dev` on Freenode.
We are always accepting of Pull requests, but we do adhere to specific standards in regards to coding style, test driven development and commit messages.
2014-03-19 04:10:39 +01:00
Please make your best effort to adhere to these when contributing to save on trivial corrections.
2014-03-19 04:10:39 +01:00
### Running the test suite
2014-03-19 04:10:39 +01:00
$ npm test
2014-07-01 19:30:14 +02:00
$ npm run-script coverage
2014-03-19 04:10:39 +01:00
2014-07-02 01:02:52 +02:00
## Complementing Libraries
2014-07-02 01:02:52 +02:00
- [bip39](https://github.com/weilu/bip39) - Wei Lu's Mnemonic code generator
- [BCoin](https://github.com/indutny/bcoin) - BIP37 / Bloom Filters / SPV client
- [scryptsy](https://github.com/cryptocoinjs/scryptsy) - Private key encryption (BIP38)
- [insight](https://github.com/bitpay/insight) - A bitcoin blockchain API for web wallets.
## Alternatives
2014-03-01 14:03:37 +01:00
2014-03-08 22:08:08 +01:00
- [Bitcore](https://github.com/bitpay/bitcore)
- [Cryptocoin](https://github.com/cryptocoinjs/cryptocoin)
2014-03-08 22:08:08 +01:00
## License
2011-12-20 12:47:50 +01:00
2014-03-08 22:08:08 +01:00
This library is free and open-source software released under the MIT license.
2012-07-30 19:09:21 +02:00
2014-03-08 22:08:08 +01:00
## Copyright
2012-07-30 19:09:21 +02:00
BitcoinJS (c) 2011-2014 Bitcoinjs-lib contributors
2014-03-01 14:03:37 +01:00
Released under MIT license