2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-10-18 16:10:20 +02:00
|
|
|
var assert = require('assert')
|
2014-11-24 11:15:28 +01:00
|
|
|
var bigi = require('bigi')
|
2014-10-18 16:10:20 +02:00
|
|
|
var bitcoin = require('../../')
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('bitcoinjs-lib (basic)', function () {
|
2015-03-19 01:15:51 +01:00
|
|
|
it('can generate a random bitcoin address', function () {
|
2014-11-24 11:02:19 +01:00
|
|
|
// for testing only
|
2015-03-19 01:15:51 +01:00
|
|
|
function rng () { return new Buffer('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') }
|
2014-11-24 11:02:19 +01:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
// generate random keyPair
|
|
|
|
var keyPair = bitcoin.ECPair.makeRandom({ rng: rng })
|
|
|
|
var address = keyPair.getAddress().toString()
|
2014-11-24 11:02:19 +01:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64')
|
2015-03-19 01:15:51 +01:00
|
|
|
})
|
2014-11-24 11:02:19 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('can generate an address from a SHA256 hash', function () {
|
2014-11-24 11:15:28 +01:00
|
|
|
var hash = bitcoin.crypto.sha256('correct horse battery staple')
|
|
|
|
var d = bigi.fromBuffer(hash)
|
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
var keyPair = new bitcoin.ECPair(d)
|
|
|
|
var address = keyPair.getAddress().toString()
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(address, '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8')
|
2014-11-24 11:15:28 +01:00
|
|
|
})
|
|
|
|
|
2015-04-16 01:19:49 +02:00
|
|
|
it('can generate a random keypair for alternative networks', function () {
|
|
|
|
// for testing only
|
|
|
|
function rng () { return new Buffer('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') }
|
|
|
|
|
|
|
|
var litecoin = bitcoin.networks.litecoin
|
|
|
|
|
|
|
|
var keyPair = bitcoin.ECPair.makeRandom({ network: litecoin, rng: rng })
|
|
|
|
var wif = keyPair.toWIF()
|
|
|
|
var address = keyPair.getAddress().toString()
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(address, 'LZJSxZbjqJ2XVEquqfqHg1RQTDdfST5PTn')
|
|
|
|
assert.strictEqual(wif, 'T7A4PUSgTDHecBxW1ZiYFrDNRih2o7M8Gf9xpoCgudPF9gDiNvuS')
|
2015-04-16 01:19:49 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('can import an address via WIF', function () {
|
2015-03-02 06:48:36 +01:00
|
|
|
var keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct')
|
|
|
|
var address = keyPair.getAddress().toString()
|
2014-10-18 16:10:20 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(address, '19AAjaTUbRjQCMuVczepkoPswiZRhjtg31')
|
2014-10-18 16:10:20 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('can create a Transaction', function () {
|
2015-03-02 06:48:36 +01:00
|
|
|
var keyPair = bitcoin.ECPair.fromWIF('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy')
|
2014-10-18 16:10:20 +02:00
|
|
|
var tx = new bitcoin.TransactionBuilder()
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
tx.addInput('aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31', 0)
|
|
|
|
tx.addOutput('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000)
|
2015-03-02 06:48:36 +01:00
|
|
|
tx.sign(0, keyPair)
|
2014-10-19 03:29:55 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(tx.build().toHex(), '0100000001313eb630b128102b60241ca895f1d0ffca2170d5a0990e094f2182c102ab94aa000000006b483045022100aefbcf847900b01dd3e3debe054d3b6d03d715d50aea8525f5ea3396f168a1fb022013d181d05b15b90111808b22ef4f9ebe701caf2ab48db269691fdf4e9048f4f60121029f50f51d63b345039a290c94bffd3180c99ed659ff6ea6b1242bca47eb93b59fffffffff01983a0000000000001976a914ad618cf4333b3b248f9744e8e81db2964d0ae39788ac00000000')
|
2014-10-19 03:29:55 +02:00
|
|
|
})
|
2014-10-18 16:10:20 +02:00
|
|
|
})
|