add Transaction#getReceivingAddresses and send
This commit is contained in:
parent
486f2ae1dc
commit
f8d7d1da9d
39349
browser/bundle.js
39349
browser/bundle.js
File diff suppressed because one or more lines are too long
|
@ -96,7 +96,7 @@ Address.fromScript = function(script, network) {
|
|||
return new Address(version, hash);
|
||||
};
|
||||
|
||||
//extract and address from scriptPubKey
|
||||
//extract an address from scriptPubKey
|
||||
Address.fromScriptPubKey = function(scriptPubKey, network) {
|
||||
|
||||
if (typeof scriptPubKey === 'string') {
|
||||
|
@ -182,6 +182,14 @@ Address.prototype.getScriptPubKey = function() {
|
|||
return script;
|
||||
};
|
||||
|
||||
Address.fromPubkeyHashScriptSig = function(scriptSig, network) {
|
||||
return Address.fromPubKey(scriptSig.chunks[1], network);
|
||||
};
|
||||
|
||||
Address.getScriptPubKeyFor = function(s) {
|
||||
return new Address(s).getScriptPubKey();
|
||||
};
|
||||
|
||||
Address.validate = function(s) {
|
||||
return new Address(s).isValid();
|
||||
};
|
||||
|
|
|
@ -16,6 +16,7 @@ var NetworkMonitor = function(peerman) {
|
|||
util.inherits(NetworkMonitor, EventEmitter);
|
||||
|
||||
NetworkMonitor.create = function(config) {
|
||||
this.config = config;
|
||||
var peerman = new PeerManager({
|
||||
network: config.networkName
|
||||
});
|
||||
|
@ -38,12 +39,12 @@ NetworkMonitor.prototype.init = function() {
|
|||
var handleTx = function(info) {
|
||||
var tx = info.message.tx;
|
||||
|
||||
var from = tx.getSendingAddresses();
|
||||
var from = tx.getSendingAddresses(self.config.networkName);
|
||||
for (var i = 0; i < from.length; i++) {
|
||||
var addr = from[i];
|
||||
self.emit('out:'+addr, tx);
|
||||
}
|
||||
var to = tx.getReceivingAddresses();
|
||||
var to = tx.getReceivingAddresses(self.config.networkName);
|
||||
for (var i = 0; i < to.length; i++) {
|
||||
var addr = to[i];
|
||||
self.emit('in:'+addr, tx);
|
||||
|
|
|
@ -10,7 +10,6 @@ var Parser = require('../util/BinaryParser');
|
|||
var Step = require('step');
|
||||
var buffertools = require('buffertools');
|
||||
var error = require('../util/error');
|
||||
var networks = require('../networks');
|
||||
var WalletKey = require('./WalletKey');
|
||||
var PrivateKey = require('./PrivateKey');
|
||||
|
||||
|
@ -621,5 +620,28 @@ Transaction.prototype.isComplete = function() {
|
|||
return ret;
|
||||
};
|
||||
|
||||
Transaction.prototype.getReceivingAddresses = function(networkName) {
|
||||
if (!networkName) networkName = 'livenet';
|
||||
ret = [];
|
||||
for (var i = 0; i<this.outs.length; i++) {
|
||||
var o = this.outs[i];
|
||||
var addr = Address.fromScriptPubKey(o.getScript(), networkName)[0].toString();
|
||||
ret.push(addr);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
Transaction.prototype.getSendingAddresses = function(networkName) {
|
||||
var ret = [];
|
||||
if (!networkName) networkName = 'livenet';
|
||||
for (var i = 0; i<this.ins.length; i++) {
|
||||
var o = this.ins[i];
|
||||
var scriptSig = o.getScript();
|
||||
if (scriptSig.getBuffer().length === 0) continue;
|
||||
var addr = Address.fromPubkeyHashScriptSig(o.getScript(), networkName).toString();
|
||||
ret.push(addr);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
||||
module.exports = Transaction;
|
||||
|
|
|
@ -433,7 +433,9 @@ TransactionBuilder._mapKeys = function(keys) {
|
|||
} else {
|
||||
throw new Error('argument must be an array of strings (WIF format) or WalletKey objects');
|
||||
}
|
||||
walletKeyMap[wk.storeObj().addr] = wk;
|
||||
var addr = wk.storeObj().addr;
|
||||
walletKeyMap[addr] = wk;
|
||||
|
||||
}
|
||||
return walletKeyMap;
|
||||
};
|
||||
|
@ -751,6 +753,7 @@ TransactionBuilder.prototype.sign = function(keys) {
|
|||
ins = tx.ins,
|
||||
l = ins.length,
|
||||
walletKeyMap = TransactionBuilder._mapKeys(keys);
|
||||
|
||||
|
||||
for (var i = 0; i < l; i++) {
|
||||
var input = this.inputMap[i];
|
||||
|
|
|
@ -213,6 +213,7 @@ describe('Address', function() {
|
|||
var d = data[i];
|
||||
var b = new Address(d[1]).getScriptPubKey().getBuffer();
|
||||
b.toString('hex').should.equal(d[0]);
|
||||
Address.getScriptPubKeyFor(d[1]).getBuffer().toString('hex').should.equal(d[0]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -7,17 +7,17 @@ var bitcore = bitcore || require('../bitcore');
|
|||
var should = chai.should();
|
||||
|
||||
var Transaction = bitcore.Transaction;
|
||||
var In;
|
||||
var Out;
|
||||
var TransactionBuilder = bitcore.TransactionBuilder;
|
||||
var Script = bitcore.Script;
|
||||
var Address = bitcore.Address;
|
||||
var util = bitcore.util;
|
||||
var buffertools = require('buffertools');
|
||||
var testdata = testdata || require('./testdata');
|
||||
|
||||
// Read tests from test/data/tx_valid.json and tx_invalid.json
|
||||
// Format is an array of arrays
|
||||
// Inner arrays are either [ "comment" ]
|
||||
// or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
|
||||
// Inner arrays are either [ 'comment' ]
|
||||
// or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],'], serializedTransaction, enforceP2SH
|
||||
// ... where all scripts are stringified scripts.
|
||||
// Returns an object with the Transaction object, and an array of input objects
|
||||
function parse_test_transaction(entry) {
|
||||
|
@ -50,10 +50,8 @@ function parse_test_transaction(entry) {
|
|||
describe('Transaction', function() {
|
||||
it('should initialize the main objects', function() {
|
||||
should.exist(Transaction);
|
||||
In = Transaction.In;
|
||||
Out = Transaction.Out;
|
||||
should.exist(In);
|
||||
should.exist(Out);
|
||||
should.exist(Transaction.In);
|
||||
should.exist(Transaction.Out);
|
||||
});
|
||||
|
||||
|
||||
|
@ -122,19 +120,60 @@ describe('Transaction', function() {
|
|||
coreTest(testdata.dataTxInvalid, false);
|
||||
|
||||
it('#normalized hash', function() {
|
||||
// string output generated from: bitcoind createrawtransaction '[{"txid": "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1","vout":1},{"txid":"2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc2","vout":0} ]' '{"mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE":0.08}'
|
||||
// string output generated from: bitcoind createrawtransaction '[{'txid': '2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1','vout':1},{'txid':'2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc2','vout':0} ]' '{'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE':0.08}'
|
||||
//
|
||||
var Parser = bitcore.BinaryParser;
|
||||
var tx = new Transaction();
|
||||
tx.parse(new Buffer('0100000002c1cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a0100000000ffffffffc2cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a0000000000ffffffff0100127a00000000001976a914774e603bafb717bd3f070e68bbcccfd907c77d1388ac00000000','hex'));
|
||||
tx.parse(new Buffer('0100000002c1cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a0100000000ffffffffc2cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a0000000000ffffffff0100127a00000000001976a914774e603bafb717bd3f070e68bbcccfd907c77d1388ac00000000', 'hex'));
|
||||
|
||||
tx.getNormalizedHash().toString('hex').should.equal('1f7d2666e2d0d663e098abb76db6ba392da972d21c14b6ea6f4336171d29966b');
|
||||
|
||||
var tx2 = new Transaction();
|
||||
tx2.parse(new Buffer('0100000001c1cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a010000004b00473044022059085ff1b8ad03033e60969b1c770aa29ba5d74c28a9992c514b100d860792f1022057a307f77f91f4563651eefc0a959aa916d275c58525320309b6aeeff43d0d8a010000ffffffff0215cd5b07000000001976a91434f8e0c5be216025a52addf18a987543cad23f7a88acdbd53e340000000017a9147a769913c0721b1e0aa6bf8a93f4ef810c60587a8700000000','hex'));
|
||||
tx2.parse(new Buffer('0100000001c1cf12ab89729d19d3cdec8ae531b5038d56c741006a105d532b3a7afa65c12a010000004b00473044022059085ff1b8ad03033e60969b1c770aa29ba5d74c28a9992c514b100d860792f1022057a307f77f91f4563651eefc0a959aa916d275c58525320309b6aeeff43d0d8a010000ffffffff0215cd5b07000000001976a91434f8e0c5be216025a52addf18a987543cad23f7a88acdbd53e340000000017a9147a769913c0721b1e0aa6bf8a93f4ef810c60587a8700000000', 'hex'));
|
||||
|
||||
tx2.getNormalizedHash().toString('hex').should.equal('e298bbf3734898581b8e342f2064236abf0acca6ac7e9a3009a16ef7b64d4983');
|
||||
});
|
||||
|
||||
describe('#send and receiving addresses', function() {
|
||||
var a1 = 'n1pKARYYUnZwxBuGj3y7WqVDu6VLN7n971';
|
||||
var a2 = 'mtxYYJXZJmQc2iJRHQ4RZkfxU5K7TE2qMJ';
|
||||
var utxos = [{
|
||||
address: a1,
|
||||
txid: '2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1',
|
||||
vout: 1,
|
||||
scriptPubKey: Address.getScriptPubKeyFor(a1).serialize().toString('hex'),
|
||||
amount: 0.5,
|
||||
confirmations: 200
|
||||
}, {
|
||||
address: a2,
|
||||
txid: '88c4520ffd97ea565578afe0b40919120be704b36561c71ba4e450e83cb3c9fd',
|
||||
vout: 1,
|
||||
scriptPubKey: Address.getScriptPubKeyFor(a2).serialize().toString('hex'),
|
||||
amount: 0.5001,
|
||||
confirmations: 200
|
||||
}];
|
||||
|
||||
var destAddress = 'myuAQcCc1REUgXGsCTiYhZvPPc3XxZ36G1';
|
||||
var outs = [{
|
||||
address: destAddress,
|
||||
amount: 1.0
|
||||
}];
|
||||
var txb = new TransactionBuilder()
|
||||
.setUnspent(utxos)
|
||||
.setOutputs(outs)
|
||||
.sign(['cVBtNonMyTydnS3NnZyipbduXo9KZfF1aUZ3uQHcvJB6UARZbiWG',
|
||||
'cRVF68hhZp1PUQCdjr2k6aVYb2cn6uabbySDPBizAJ3PXF7vDXTL']);
|
||||
var tx = txb.build();
|
||||
|
||||
it('should find receiving addresses', function() {
|
||||
var to = tx.getReceivingAddresses('testnet');
|
||||
to.length.should.equal(1);
|
||||
to[0].should.equal(destAddress);
|
||||
});
|
||||
it('should find sending addresses', function() {
|
||||
var from = tx.getSendingAddresses('testnet');
|
||||
from.length.should.equal(2);
|
||||
from[0].should.equal(a1);
|
||||
from[1].should.equal(a2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue