Manuel Araoz
11 years ago
6 changed files with 187 additions and 137 deletions
@ -1,19 +1,27 @@ |
|||||
'use strict'; |
'use strict'; |
||||
|
|
||||
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
|
|
||||
|
|
||||
var Address = require('../Address'); |
var run = function() { |
||||
|
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
|
||||
|
var Address = require('../Address'); |
||||
|
|
||||
var addrs = [ |
var addrs = [ |
||||
'1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
'1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
||||
'1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx', |
'1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx', |
||||
'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
||||
'1600 Pennsylvania Ave NW', |
'1600 Pennsylvania Ave NW', |
||||
].map(function(addr) { |
].map(function(addr) { |
||||
return new Address(addr); |
return new Address(addr); |
||||
}); |
}); |
||||
|
|
||||
addrs.forEach(function(addr) { |
addrs.forEach(function(addr) { |
||||
var valid = addr.isValid(); |
var valid = addr.isValid(); |
||||
console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid'); |
console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid'); |
||||
}); |
}); |
||||
|
|
||||
|
}; |
||||
|
|
||||
|
module.exports.run = run; |
||||
|
if (require.main === module) { |
||||
|
run(); |
||||
|
} |
||||
|
@ -1,47 +1,47 @@ |
|||||
'use strict'; |
'use strict'; |
||||
|
|
||||
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
|
var run = function() { |
||||
|
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
|
||||
|
var networks = require('../networks'); |
||||
|
var Peer = require('../Peer'); |
||||
|
var PeerManager = require('soop').load('../PeerManager', { |
||||
|
network: networks.testnet |
||||
|
}); |
||||
|
|
||||
var util = require('util'); |
var handleBlock = function(info) { |
||||
var networks = require('../networks'); |
console.log('** Block Received **'); |
||||
var Peer = require('../Peer'); |
console.log(info.message); |
||||
var PeerManager = require('soop').load('../PeerManager', |
}; |
||||
{network: networks.testnet}); |
|
||||
|
|
||||
var handleBlock = function(info) { |
var handleTx = function(info) { |
||||
|
var tx = info.message.tx.getStandardizedObject(); |
||||
|
|
||||
console.log('** Block Received **'); |
console.log('** TX Received **'); |
||||
console.log(info.message); |
console.log(tx); |
||||
|
}; |
||||
|
|
||||
}; |
var handleInv = function(info) { |
||||
|
console.log('** Inv **'); |
||||
var handleTx = function(info) { |
console.log(info.message); |
||||
|
|
||||
var tx = info.message.tx.getStandardizedObject(); |
var invs = info.message.invs; |
||||
|
info.conn.sendGetData(invs); |
||||
|
}; |
||||
|
|
||||
console.log('** Block TX **'); |
var peerman = new PeerManager(); |
||||
console.log(tx); |
|
||||
|
|
||||
}; |
|
||||
|
|
||||
var handleInv = function(info) { |
peerman.addPeer(new Peer('127.0.0.1', 18333)); |
||||
|
|
||||
console.log('** Block Inv **'); |
peerman.on('connection', function(conn) { |
||||
console.log(info.message); |
conn.on('inv', handleInv); |
||||
|
conn.on('block', handleBlock); |
||||
var invs = info.message.invs; |
conn.on('tx', handleTx); |
||||
info.conn.sendGetData(invs); |
}); |
||||
|
|
||||
|
peerman.start(); |
||||
}; |
}; |
||||
|
|
||||
var peerman = new PeerManager(); |
module.exports.run = run; |
||||
|
if (require.main === module) { |
||||
peerman.addPeer( new Peer('127.0.0.1', 18333) ); |
run(); |
||||
|
} |
||||
peerman.on('connection', function(conn) { |
|
||||
conn.on('inv', handleInv); |
|
||||
conn.on('block', handleBlock); |
|
||||
conn.on('tx', handleTx); |
|
||||
}); |
|
||||
|
|
||||
peerman.start(); |
|
||||
|
@ -1,29 +1,31 @@ |
|||||
'use strict'; |
'use strict'; |
||||
|
|
||||
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
|
var run = function() { |
||||
|
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
|
||||
|
var RpcClient = require('../RpcClient'); |
||||
|
var hash = '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; |
||||
|
|
||||
var util = require('util'); |
var config = { |
||||
var RpcClient = require('../RpcClient'); |
protocol: 'http', |
||||
var hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; |
user: 'user', |
||||
|
pass: 'pass', |
||||
|
host: '127.0.0.1', |
||||
|
port: '18332', |
||||
|
}; |
||||
|
|
||||
var config = { |
var rpc = new RpcClient(config); |
||||
protocol: 'http', |
|
||||
user: 'user', |
|
||||
pass: 'pass', |
|
||||
host: '127.0.0.1', |
|
||||
port: '18332', |
|
||||
}; |
|
||||
|
|
||||
var rpc = new RpcClient(config); |
|
||||
|
|
||||
rpc.getBlock(hash, function(err, ret) { |
|
||||
|
|
||||
if(err) { |
rpc.getBlock(hash, function(err, ret) { |
||||
console.error("An error occured fetching block", hash); |
if (err) { |
||||
console.error(err); |
console.error('An error occured fetching block', hash); |
||||
return; |
console.error(err); |
||||
} |
return; |
||||
|
} |
||||
console.log(ret); |
console.log(ret); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
}); |
module.exports.run = run; |
||||
|
if (require.main === module) { |
||||
|
run(); |
||||
|
} |
||||
|
@ -1,73 +1,75 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var run = function() { |
||||
|
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
|
||||
|
var networks = require('../networks'); |
||||
|
var Peer = require('../Peer'); |
||||
|
var Transaction = require('../Transaction'); |
||||
|
var Address = require('../Address'); |
||||
|
var Script = require('../Script'); |
||||
|
var coinUtil = require('../util/util'); |
||||
|
var PeerManager = require('soop').load('../PeerManager', { |
||||
|
network: networks.testnet |
||||
|
}); |
||||
|
|
||||
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
|
var createTx = function() { |
||||
|
var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265'; |
||||
var networks = require('../networks'); |
var TXIN_N = 0; |
||||
var Peer = require('../Peer'); |
var ADDR = 'muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz'; |
||||
var Transaction = require('../Transaction'); |
var VAL = '0.001'; |
||||
var Address = require('../Address'); |
|
||||
var Script = require('../Script'); |
var txobj = { |
||||
var coinUtil = require('../util/util'); |
version: 1, |
||||
var PeerManager = require('soop').load('../PeerManager', |
lock_time: 0, |
||||
{network: networks.testnet}); |
ins: [], |
||||
|
outs: [] |
||||
var createTx = function() { |
}; |
||||
|
|
||||
var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265'; |
var txin = { |
||||
var TXIN_N = 0; |
s: coinUtil.EMPTY_BUFFER, // Add signature
|
||||
var ADDR = 'muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz'; |
q: 0xffffffff |
||||
var VAL = '1.234'; |
}; |
||||
|
|
||||
var txobj = { |
var hash = new Buffer(TXIN.split('').reverse(), 'hex'); |
||||
version: 1, |
var vout = parseInt(TXIN_N); |
||||
lock_time: 0, |
var voutBuf = new Buffer(4); |
||||
ins: [], |
|
||||
outs: [] |
voutBuf.writeUInt32LE(vout, 0); |
||||
} |
txin.o = Buffer.concat([hash, voutBuf]); |
||||
|
txobj.ins.push(txin); |
||||
var txin = { |
|
||||
s: coinUtil.EMPTY_BUFFER, // Add signature
|
var addr = new Address(ADDR); |
||||
q: 0xffffffff |
var script = Script.createPubKeyHashOut(addr.payload()); |
||||
}; |
var valueNum = coinUtil.parseValue(VAL); |
||||
|
var value = coinUtil.bigIntToValue(valueNum); |
||||
var hash = new Buffer(TXIN.split('').reverse(), 'hex'); |
|
||||
|
var txout = { |
||||
var vout = parseInt(TXIN_N); |
v: value, |
||||
var voutBuf = new Buffer(4); |
s: script.getBuffer(), |
||||
|
}; |
||||
voutBuf.writeUInt32LE(vout, 0); |
txobj.outs.push(txout); |
||||
txin.o = Buffer.concat([hash, voutBuf]); |
|
||||
txobj.ins.push(txin); |
return new Transaction(txobj); |
||||
|
|
||||
var addr = new Address(ADDR); |
|
||||
var script = Script.createPubKeyHashOut(addr.payload()); |
|
||||
var valueNum = coinUtil.parseValue(VAL); |
|
||||
var value = coinUtil.bigIntToValue(valueNum); |
|
||||
|
|
||||
var txout = { |
|
||||
v: value, |
|
||||
s: script.getBuffer(), |
|
||||
}; |
}; |
||||
txobj.outs.push(txout); |
|
||||
|
|
||||
return new Transaction(txobj); |
|
||||
|
|
||||
}; |
|
||||
|
|
||||
var peerman = new PeerManager(); |
|
||||
peerman.addPeer(new Peer('127.0.0.1', 18333)); |
|
||||
|
|
||||
peerman.on('connect', function(conn) { |
var peerman = new PeerManager(); |
||||
|
peerman.addPeer(new Peer('127.0.0.1', 18333)); |
||||
var conn = peerman.getActiveConnection(); |
|
||||
|
peerman.on('connect', function() { |
||||
if (conn) { |
var conn = peerman.getActiveConnection(); |
||||
conn.sendTx(createTx()); |
if (conn) { |
||||
} |
conn.sendTx(createTx()); |
||||
|
} |
||||
conn.on('reject', function () { |
conn.on('reject', function() { |
||||
console.log('Transaction Rejected'); |
console.log('Transaction Rejected'); |
||||
|
}); |
||||
}); |
}); |
||||
|
|
||||
}); |
peerman.start(); |
||||
|
}; |
||||
|
|
||||
peerman.start(); |
module.exports.run = run; |
||||
|
if (require.main === module) { |
||||
|
run(); |
||||
|
} |
||||
|
@ -0,0 +1,14 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var backup = console.log; |
||||
|
var nop = function() {}; |
||||
|
var mute = function() { |
||||
|
console.log = nop; |
||||
|
}; |
||||
|
|
||||
|
var unmute = function() { |
||||
|
console.log = backup; |
||||
|
}; |
||||
|
|
||||
|
module.exports.mute = mute; |
||||
|
module.exports.unmute = unmute; |
@ -0,0 +1,24 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var chai = chai || require('chai'); |
||||
|
var should = chai.should(); |
||||
|
var mute = require('./mute').mute; |
||||
|
var unmute = require('./mute').unmute; |
||||
|
|
||||
|
var examples = [ |
||||
|
'Address', |
||||
|
'PeerManager', |
||||
|
'Rpc', |
||||
|
'SendTx', |
||||
|
]; |
||||
|
|
||||
|
describe('Examples run', function() { |
||||
|
before(mute); |
||||
|
after(unmute); |
||||
|
examples.forEach(function(example) { |
||||
|
it('valid '+example, function() { |
||||
|
var ex = require('../examples/'+example); |
||||
|
ex.run.should.not.throw(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
Loading…
Reference in new issue