commit
d3083a8503
4 changed files with 91 additions and 87 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "bitcoinjs-lib",
|
"name": "bitcoinjs-lib",
|
||||||
"version": "2.1.2",
|
"version": "2.1.3",
|
||||||
"description": "Client-side Bitcoin JavaScript library",
|
"description": "Client-side Bitcoin JavaScript library",
|
||||||
"main": "./src/index.js",
|
"main": "./src/index.js",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
@ -68,11 +68,11 @@
|
||||||
"create-hmac": "^1.1.3",
|
"create-hmac": "^1.1.3",
|
||||||
"ecurve": "^1.0.0",
|
"ecurve": "^1.0.0",
|
||||||
"randombytes": "^2.0.1",
|
"randombytes": "^2.0.1",
|
||||||
"typeforce": "^1.3.0",
|
"typeforce": "^1.5.5",
|
||||||
"wif": "^1.1.0"
|
"wif": "^1.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"async": "^0.9.0",
|
"async": "^1.5.0",
|
||||||
"blanket": "^1.1.0",
|
"blanket": "^1.1.0",
|
||||||
"browserify": "^10.0.0",
|
"browserify": "^10.0.0",
|
||||||
"bs58": "^2.0.1",
|
"bs58": "^2.0.1",
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
var async = require('async')
|
||||||
var Blockchain = require('cb-http-client')
|
var Blockchain = require('cb-http-client')
|
||||||
var httpify = require('httpify')
|
var httpify = require('httpify')
|
||||||
|
|
||||||
|
@ -5,16 +6,42 @@ var BLOCKTRAIL_API_KEY = process.env.BLOCKTRAIL_API_KEY || 'c0bd8155c66e3fb148bb
|
||||||
|
|
||||||
var mainnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/BTC', { api_key: BLOCKTRAIL_API_KEY })
|
var mainnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/BTC', { api_key: BLOCKTRAIL_API_KEY })
|
||||||
var testnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/tBTC', { api_key: BLOCKTRAIL_API_KEY })
|
var testnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/tBTC', { api_key: BLOCKTRAIL_API_KEY })
|
||||||
testnet.faucet = function faucet (address, amount, callback) {
|
|
||||||
httpify({
|
testnet.faucet = function faucet (address, amount, done) {
|
||||||
method: 'POST',
|
var unspents = []
|
||||||
url: 'https://api.blocktrail.com/v1/tBTC/faucet/withdrawl?api_key=' + BLOCKTRAIL_API_KEY,
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
async.whilst(
|
||||||
body: JSON.stringify({
|
function condition () { return unspents.length === 0 },
|
||||||
address: address,
|
function f (callback) {
|
||||||
amount: amount
|
httpify({
|
||||||
})
|
method: 'POST',
|
||||||
}, callback)
|
url: 'https://api.blocktrail.com/v1/tBTC/faucet/withdrawl?api_key=' + BLOCKTRAIL_API_KEY,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
address: address,
|
||||||
|
amount: amount
|
||||||
|
})
|
||||||
|
}, function (err) {
|
||||||
|
if (err) return callback(err)
|
||||||
|
|
||||||
|
testnet.addresses.unspents(address, function (err, result) {
|
||||||
|
if (err) return callback(err)
|
||||||
|
|
||||||
|
// filter small unspents
|
||||||
|
unspents = result.filter(function (unspent) {
|
||||||
|
return unspent.value > 1e3
|
||||||
|
})
|
||||||
|
|
||||||
|
callback()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
function (err) {
|
||||||
|
if (err) return done(err)
|
||||||
|
|
||||||
|
done(null, unspents)
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -22,53 +22,42 @@ describe('bitcoinjs-lib (advanced)', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can create an OP_RETURN transaction', function (done) {
|
it('can create an OP_RETURN transaction', function (done) {
|
||||||
this.timeout(20000)
|
this.timeout(30000)
|
||||||
|
|
||||||
var network = bitcoin.networks.testnet
|
var network = bitcoin.networks.testnet
|
||||||
var keyPair = bitcoin.ECPair.makeRandom({ network: network })
|
var keyPair = bitcoin.ECPair.makeRandom({ network: network })
|
||||||
var address = keyPair.getAddress()
|
var address = keyPair.getAddress()
|
||||||
|
|
||||||
blockchain.t.faucet(address, 2e4, function (err) {
|
blockchain.t.faucet(address, 2e4, function (err, unspents) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
blockchain.t.addresses.unspents(address, function (err, unspents) {
|
// use the oldest unspent
|
||||||
|
var unspent = unspents.pop()
|
||||||
|
var tx = new bitcoin.TransactionBuilder(network)
|
||||||
|
var data = new Buffer('bitcoinjs-lib')
|
||||||
|
var dataScript = bitcoin.script.nullDataOutput(data)
|
||||||
|
|
||||||
|
tx.addInput(unspent.txId, unspent.vout)
|
||||||
|
tx.addOutput(dataScript, 1000)
|
||||||
|
tx.sign(0, keyPair)
|
||||||
|
|
||||||
|
var txBuilt = tx.build()
|
||||||
|
|
||||||
|
blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// filter small unspents
|
// check that the message was propagated
|
||||||
unspents = unspents.filter(function (unspent) {
|
blockchain.t.transactions.get(txBuilt.getId(), function (err, transaction) {
|
||||||
return unspent.value > 1e4
|
|
||||||
})
|
|
||||||
|
|
||||||
// use the oldest unspent
|
|
||||||
var unspent = unspents.pop()
|
|
||||||
if (!unspent) throw new Error('Faucet didn\'t provide an unspent')
|
|
||||||
|
|
||||||
var tx = new bitcoin.TransactionBuilder(network)
|
|
||||||
var data = new Buffer('bitcoinjs-lib')
|
|
||||||
var dataScript = bitcoin.script.nullDataOutput(data)
|
|
||||||
|
|
||||||
tx.addInput(unspent.txId, unspent.vout)
|
|
||||||
tx.addOutput(dataScript, 1000)
|
|
||||||
tx.sign(0, keyPair)
|
|
||||||
|
|
||||||
var txBuilt = tx.build()
|
|
||||||
|
|
||||||
blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) {
|
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// check that the message was propagated
|
var actual = bitcoin.Transaction.fromHex(transaction.txHex)
|
||||||
blockchain.t.transactions.get(txBuilt.getId(), function (err, transaction) {
|
var dataScript2 = actual.outs[0].script
|
||||||
if (err) return done(err)
|
var data2 = bitcoin.script.decompile(dataScript2)[1]
|
||||||
|
|
||||||
var actual = bitcoin.Transaction.fromHex(transaction.txHex)
|
assert.deepEqual(dataScript, dataScript2)
|
||||||
var dataScript2 = actual.outs[0].script
|
assert.deepEqual(data, data2)
|
||||||
var data2 = bitcoin.script.decompile(dataScript2)[1]
|
|
||||||
|
|
||||||
assert.deepEqual(dataScript, dataScript2)
|
done()
|
||||||
assert.deepEqual(data, data2)
|
|
||||||
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -37,53 +37,41 @@ describe('bitcoinjs-lib (multisig)', function () {
|
||||||
var address = bitcoin.address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet)
|
var address = bitcoin.address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet)
|
||||||
|
|
||||||
// attempt to send funds to the source address
|
// attempt to send funds to the source address
|
||||||
blockchain.t.faucet(address, 2e4, function (err) {
|
blockchain.t.faucet(address, 2e4, function (err, unspents) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// get latest unspents from the address
|
// use the oldest unspent
|
||||||
blockchain.t.addresses.unspents(address, function (err, unspents) {
|
var unspent = unspents.pop()
|
||||||
|
|
||||||
|
// make a random destination address
|
||||||
|
var targetAddress = bitcoin.ECPair.makeRandom({
|
||||||
|
network: bitcoin.networks.testnet
|
||||||
|
}).getAddress()
|
||||||
|
|
||||||
|
var txb = new bitcoin.TransactionBuilder(bitcoin.networks.testnet)
|
||||||
|
txb.addInput(unspent.txId, unspent.vout)
|
||||||
|
txb.addOutput(targetAddress, 1e4)
|
||||||
|
|
||||||
|
// sign with 1st and 3rd key
|
||||||
|
txb.sign(0, keyPairs[0], redeemScript)
|
||||||
|
txb.sign(0, keyPairs[2], redeemScript)
|
||||||
|
|
||||||
|
// broadcast our transaction
|
||||||
|
var tx = txb.build()
|
||||||
|
var txId = tx.getId()
|
||||||
|
|
||||||
|
blockchain.t.transactions.propagate(tx.toHex(), function (err) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// filter small unspents
|
// check that the above transaction included the intended address
|
||||||
unspents = unspents.filter(function (unspent) {
|
blockchain.t.addresses.unspents(targetAddress, function (err, unspents) {
|
||||||
return unspent.value > 1e4
|
|
||||||
})
|
|
||||||
|
|
||||||
// use the oldest unspent
|
|
||||||
var unspent = unspents.pop()
|
|
||||||
|
|
||||||
if (!unspent) throw new Error('Faucet didn\'t provide an unspent')
|
|
||||||
|
|
||||||
// make a random destination address
|
|
||||||
var targetAddress = bitcoin.ECPair.makeRandom({
|
|
||||||
network: bitcoin.networks.testnet
|
|
||||||
}).getAddress()
|
|
||||||
|
|
||||||
var txb = new bitcoin.TransactionBuilder(bitcoin.networks.testnet)
|
|
||||||
txb.addInput(unspent.txId, unspent.vout)
|
|
||||||
txb.addOutput(targetAddress, 1e4)
|
|
||||||
|
|
||||||
// sign with 1st and 3rd key
|
|
||||||
txb.sign(0, keyPairs[0], redeemScript)
|
|
||||||
txb.sign(0, keyPairs[2], redeemScript)
|
|
||||||
|
|
||||||
// broadcast our transaction
|
|
||||||
var tx = txb.build()
|
|
||||||
var txId = tx.getId()
|
|
||||||
|
|
||||||
blockchain.t.transactions.propagate(tx.toHex(), function (err) {
|
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// check that the above transaction included the intended address
|
assert(unspents.some(function (unspent) {
|
||||||
blockchain.t.addresses.unspents(targetAddress, function (err, unspents) {
|
return unspent.txId === txId && unspent.value === 1e4
|
||||||
if (err) return done(err)
|
}))
|
||||||
|
|
||||||
assert(unspents.some(function (unspent) {
|
done()
|
||||||
return unspent.txId === txId && unspent.value === 1e4
|
|
||||||
}))
|
|
||||||
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue