Browse Source

update examples and readme

patch-2
Matias Alejo Garcia 11 years ago
parent
commit
26b7f89f39
  1. 2
      Gruntfile.js
  2. 64
      README.md
  3. 1
      browser/build.js
  4. 40
      examples/CreateAndSignTx.js

2
Gruntfile.js

@ -50,6 +50,6 @@ module.exports = function(grunt) {
}); });
grunt.registerTask('default', ['watch']); grunt.registerTask('default', ['shell','watch']);
}; };

64
README.md

@ -124,19 +124,23 @@ rpc.getBlock(hash, function(err, ret) {
Check the list of all supported RPC call at [RpcClient.js](RpcClient.js) Check the list of all supported RPC call at [RpcClient.js](RpcClient.js)
## Creating and sending a Transaction through P2P ## Creating and sending a Transaction through P2P
For this example you need a running bitcoind instance with RPC enabled.
The fee of the transaction can be given in `opts` or it will be determined
by the transaction size. Documentation on the paramets of `TransactionBuilder`
can be found on the source file.
```js ```js
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var networks = bitcore.networks; var networks = bitcore.networks;
var Peer = bitcore.Peer; var Peer = bitcore.Peer;
var Transaction = bitcore.Transaction; var TransactionBuilder = bitcore.TransactionBuilder;
var PeerManager = require('soop').load('../PeerManager', { var PeerManager = require('soop').load('../PeerManager', {
network: networks.testnet network: networks.testnet
}); });
// this can be get from insight.bitcore.io API o blockchain.info // this can be get from insight.bitcore.io API o blockchain.info
var utxos = {
"unspent": [ var unspent = [
{ {
"address": "n4g2TFaQo8UgedwpkYdcQFF6xE2Ei9Czvy", "address": "n4g2TFaQo8UgedwpkYdcQFF6xE2Ei9Czvy",
"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1", "txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1",
@ -153,29 +157,15 @@ var utxos = {
"confirmations": 1, "confirmations": 1,
"amount": 10 "amount": 10
}, },
}; ];
//private keys in WIF format (see Transaction.js for other options) //private keys in WIF format (see TransactionBuilder.js for other options)
var keys = [ var keys = [
"cSq7yo4fvsbMyWVN945VUGUWMaSazZPWqBVJZyoGsHmNq6W4HVBV", "cSq7yo4fvsbMyWVN945VUGUWMaSazZPWqBVJZyoGsHmNq6W4HVBV",
"cPa87VgwZfowGZYaEenoQeJgRfKW6PhZ1R65EHTkN1K19cSvc92G", "cPa87VgwZfowGZYaEenoQeJgRfKW6PhZ1R65EHTkN1K19cSvc92G",
"cPQ9DSbBRLva9av5nqeF5AGrh3dsdW8p2E5jS4P8bDWZAoQTeeKB" "cPQ9DSbBRLva9av5nqeF5AGrh3dsdW8p2E5jS4P8bDWZAoQTeeKB"
]; ];
function createTx() {
var outs = [{address:'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE', amount:0.08}];
var ret = Transaction.createAndSign(utxos, outs, keys);
/ * create and signing can be done in 2 steps using:
* var ret = Transaction.create(utxos,outs);
* and later:
* ret.tx.sign(ret.tx.selectedUtxos, outs, keys);
*/
return ret.tx.serialize().toString('hex');
};
var peerman = new PeerManager(); var peerman = new PeerManager();
peerman.addPeer(new Peer('127.0.0.1', 18333)); peerman.addPeer(new Peer('127.0.0.1', 18333));
@ -183,7 +173,39 @@ peerman.addPeer(new Peer('127.0.0.1', 18333));
peerman.on('connect', function() { peerman.on('connect', function() {
var conn = peerman.getActiveConnection(); var conn = peerman.getActiveConnection();
if (conn) { if (conn) {
conn.sendTx(createTx()); var outs = [{address:toAddress, amount:amt}];
var opts = {remainderAddress: changeAddressString};
var builder = new bitcore.TransactionBuilder;
var tx = builder
.init(opts)
.setUnspent(Unspent)
.setOutputs(outs)
.sign(keys)
.build();
/* create and signing can be done in multiple steps using:
*
* var builder = bitcore
* .TransactionBuilder
* .init(opts)
* .setUnspent(utxos)
* .setOutputs(outs);
* //later
* builder.sign(key1);
* // get partially signed tx
* var tx = builder.build();
*
* //later
* builder.sign(key2);
* if (builder.isFullySigned()){
* var tx = builder.build();
* }
*
* The selected Unspent Outputs for the transaction can be retrieved with:
* var selectedUnspent = build.getSelectedUnspent();
*/
conn.sendTx(tx.serialize().toString('hex'));
} }
conn.on('reject', function() { conn.on('reject', function() {
console.log('Transaction Rejected'); console.log('Transaction Rejected');

1
browser/build.js

@ -43,6 +43,7 @@ var modules = [
'ScriptInterpreter', 'ScriptInterpreter',
'Sign', 'Sign',
'Transaction', 'Transaction',
'TransactionBuilder',
'Wallet', 'Wallet',
'WalletKey', 'WalletKey',
'config', 'config',

40
examples/CreateAndSignTx.js

@ -25,22 +25,44 @@ var run = function() {
var outs = [{address:toAddress, amount:amt}]; var outs = [{address:toAddress, amount:amt}];
var keys = [priv]; var keys = [priv];
var opts = {remainderAddress: changeAddressString};
var builder = new bitcore.TransactionBuilder;
var ret = bitcore.Transaction.createAndSign(utxos, outs, keys, var tx = builder
{remainderAddress: changeAddressString}); .init(opts)
.setUnspent(utxos)
.setOutputs(outs)
.sign(keys)
.build();
/* create and signing can be done in 2 steps using: /* create and signing can be done in multiple steps using:
* var ret = Transaction.create(utxos,outs); *
* and later: * var builder = bitcore
* ret.tx.sign(ret.tx.selectedUtxos, outs, keys); * .TransactionBuilder
* .init(opts)
* .setUnspent(utxos)
* .setOutputs(outs);
*
* builder.sign(key1);
* builder.sign(key2);
* ...
* if (builder.isFullySigned()){
* var tx = builder.build();
* }
*
* The selected Unspent Outputs for the transaction can be retrieved with:
*
* var selectedUnspent = build.getSelectedUnspent();
*/ */
var txHex = ret.tx.serialize().toString('hex'); var txHex = tx.serialize().toString('hex');
console.log('TX HEX IS: ', txHex); console.log('TX HEX IS: ', txHex);
}; };
// browser example compatibility
module.exports.run = run; if (module) {
module.exports.run = run;
}
if (require.main === module) { if (require.main === module) {
run(); run();
} }

Loading…
Cancel
Save