Skip to content

Commit

Permalink
Fully parse p2sh bitcoin transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
deweller committed Jan 5, 2016
1 parent cbf49d4 commit 06eea2a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function extractSourcesAndDestinations($tx) {
$quantity_by_destination = [];
foreach ($tx['vout'] as $vout) {
if (isset($vout['scriptPubKey']) AND isset($vout['scriptPubKey']['addresses'])) {
if ($vout['scriptPubKey']['type'] == 'pubkeyhash') {
if ($vout['scriptPubKey']['type'] == 'pubkeyhash' OR $vout['scriptPubKey']['type'] == 'scripthash') {
foreach($vout['scriptPubKey']['addresses'] as $destination_address) {
// ignore change
if (isset($sources_map[$destination_address])) { continue; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"hex" : "010000000124e8f871a3af980c19d75217fdb558e1521fb00b3621b41d24b06e87391d07e600000000fc00473044022013bdc1b841dc6de7cf81eb6b892e8954b8c252fd3626f51ec30eabac58bba6fa022037e07db62d80491572db4b11e41f9675f5309257914e1a0c52cb5f4ca88f75fc01473044022039109277d5f99475442ccc036b2978e5cbe993209d80d130d3fc1a4ad2bffc6302206401b81451175f8cac79eebb9c54b147025a5acb6e8b03ede1e277ae748a49ca014c6952210357605c1972a30f8593e5f459925a32b753e6d5735dc208ae1f3c3808ce87339321038319a9d2d7e62dc12ed1143cd4ab562505540604b8d960ff6d67f20059a90b972103d472c8b14e0a08ce28fbf5486a6f4908decb6fa86993c40e535e8f0af30c6c7d53aeffffffff025ab7f0050000000017a9143f1418c46865caa5299c1d619122b2f130a198de87a0a0a7010000000017a914398c88f25303858c4e3acac6528839fb398c6cf98700000000",
"txid" : "94415dc3ad8ada2f9a65623b67bd525b34f539703760a234beb6c612690abeef",
"version" : 1,
"locktime" : 0,
"vin" : [
{
"txid" : "e6071d39876eb0241db421360bb01f52e158b5fd1752d7190c98afa371f8e824",
"vout" : 0,
"scriptSig" : {
"asm" : "0 3044022013bdc1b841dc6de7cf81eb6b892e8954b8c252fd3626f51ec30eabac58bba6fa022037e07db62d80491572db4b11e41f9675f5309257914e1a0c52cb5f4ca88f75fc01 3044022039109277d5f99475442ccc036b2978e5cbe993209d80d130d3fc1a4ad2bffc6302206401b81451175f8cac79eebb9c54b147025a5acb6e8b03ede1e277ae748a49ca01 52210357605c1972a30f8593e5f459925a32b753e6d5735dc208ae1f3c3808ce87339321038319a9d2d7e62dc12ed1143cd4ab562505540604b8d960ff6d67f20059a90b972103d472c8b14e0a08ce28fbf5486a6f4908decb6fa86993c40e535e8f0af30c6c7d53ae",
"hex" : "00473044022013bdc1b841dc6de7cf81eb6b892e8954b8c252fd3626f51ec30eabac58bba6fa022037e07db62d80491572db4b11e41f9675f5309257914e1a0c52cb5f4ca88f75fc01473044022039109277d5f99475442ccc036b2978e5cbe993209d80d130d3fc1a4ad2bffc6302206401b81451175f8cac79eebb9c54b147025a5acb6e8b03ede1e277ae748a49ca014c6952210357605c1972a30f8593e5f459925a32b753e6d5735dc208ae1f3c3808ce87339321038319a9d2d7e62dc12ed1143cd4ab562505540604b8d960ff6d67f20059a90b972103d472c8b14e0a08ce28fbf5486a6f4908decb6fa86993c40e535e8f0af30c6c7d53ae"
},
"sequence" : 4294967295
}
],
"vout" : [
{
"value" : 0.99661658,
"n" : 0,
"scriptPubKey" : {
"asm" : "OP_HASH160 3f1418c46865caa5299c1d619122b2f130a198de OP_EQUAL",
"hex" : "a9143f1418c46865caa5299c1d619122b2f130a198de87",
"reqSigs" : 1,
"type" : "scripthash",
"addresses" : [
"37SYZyXvoGgPuZ5YTur9v6gtuYBj3Bh2XK"
]
}
},
{
"value" : 0.27762848,
"n" : 1,
"scriptPubKey" : {
"asm" : "OP_HASH160 398c88f25303858c4e3acac6528839fb398c6cf9 OP_EQUAL",
"hex" : "a914398c88f25303858c4e3acac6528839fb398c6cf987",
"reqSigs" : 1,
"type" : "scripthash",
"addresses" : [
"36wJo1xtHZ2NGcCovy6vHgWvyF7ryMTjHF"
]
}
}
],
"blockhash" : "0000000000000000050b5bb68b8f163b5f74eced5c409b14245e6fc1d74f83f5",
"confirmations" : 152,
"time" : 1451912683,
"blocktime" : 1451912683
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"hex" : "0100000001efbe0a6912c6b6be34a260377039f5345b52bd673b62659a2fda8aadc35d419401000000fc004730440220756f6bf38abe39aed90fb94cf2168b7d10cd3d3982dc33e949089deef420d0b4022023c40ca0f66a3e9d3280296c983a5cb60b04d26153cae31fa69983c5ed79bdfe014730440220570dbc2dde2678423f6c190be169f4c0b973b7da8e0e0c9b7d2ee304fa30ae4902203e6a1877f7899aae0f0ef932e2ce1bac20c2b7812800bfcea196746e7a388638014c69522103c5c1676c044d8f74e6539e8670c6394435503d29d6c238dc66ec8832bdf43dca2102e655beeec9e051747eaa82a3fbc40fc13b66bbc9aadfb64a76a4c3a0527d9e582102bfe87145a0aa7cc4f9ad79f3e30c1cc72689ce4047a0040a5f0fb19ec07536d753aeffffffff028d507b010000000017a9148ae113bf266f4510fabd8e0c258973e3b9f006f287eb392c000000000017a914706f0d6f929ffc74fc7ae476a9055029cdf8515c8700000000",
"txid" : "99c93bf83cdd4d60f234bd34ee39acc4c1b5eb66db8c932600de12b05c96d0ef",
"version" : 1,
"locktime" : 0,
"vin" : [
{
"txid" : "94415dc3ad8ada2f9a65623b67bd525b34f539703760a234beb6c612690abeef",
"vout" : 1,
"scriptSig" : {
"asm" : "0 30440220756f6bf38abe39aed90fb94cf2168b7d10cd3d3982dc33e949089deef420d0b4022023c40ca0f66a3e9d3280296c983a5cb60b04d26153cae31fa69983c5ed79bdfe01 30440220570dbc2dde2678423f6c190be169f4c0b973b7da8e0e0c9b7d2ee304fa30ae4902203e6a1877f7899aae0f0ef932e2ce1bac20c2b7812800bfcea196746e7a38863801 522103c5c1676c044d8f74e6539e8670c6394435503d29d6c238dc66ec8832bdf43dca2102e655beeec9e051747eaa82a3fbc40fc13b66bbc9aadfb64a76a4c3a0527d9e582102bfe87145a0aa7cc4f9ad79f3e30c1cc72689ce4047a0040a5f0fb19ec07536d753ae",
"hex" : "004730440220756f6bf38abe39aed90fb94cf2168b7d10cd3d3982dc33e949089deef420d0b4022023c40ca0f66a3e9d3280296c983a5cb60b04d26153cae31fa69983c5ed79bdfe014730440220570dbc2dde2678423f6c190be169f4c0b973b7da8e0e0c9b7d2ee304fa30ae4902203e6a1877f7899aae0f0ef932e2ce1bac20c2b7812800bfcea196746e7a388638014c69522103c5c1676c044d8f74e6539e8670c6394435503d29d6c238dc66ec8832bdf43dca2102e655beeec9e051747eaa82a3fbc40fc13b66bbc9aadfb64a76a4c3a0527d9e582102bfe87145a0aa7cc4f9ad79f3e30c1cc72689ce4047a0040a5f0fb19ec07536d753ae"
},
"sequence" : 4294967295
}
],
"vout" : [
{
"value" : 0.24858765,
"n" : 0,
"scriptPubKey" : {
"asm" : "OP_HASH160 8ae113bf266f4510fabd8e0c258973e3b9f006f2 OP_EQUAL",
"hex" : "a9148ae113bf266f4510fabd8e0c258973e3b9f006f287",
"reqSigs" : 1,
"type" : "scripthash",
"addresses" : [
"3EMLoeeUZtKBe6pg7wJZdVdXTJWa9pFNMR"
]
}
},
{
"value" : 0.02898411,
"n" : 1,
"scriptPubKey" : {
"asm" : "OP_HASH160 706f0d6f929ffc74fc7ae476a9055029cdf8515c OP_EQUAL",
"hex" : "a914706f0d6f929ffc74fc7ae476a9055029cdf8515c87",
"reqSigs" : 1,
"type" : "scripthash",
"addresses" : [
"3BwWe6D2znQ7XnMaGaddu1MdyyGWppxRwj"
]
}
}
],
"blockhash" : "0000000000000000040745c0a096a1d711dcc1462315ae3a076644038bc4a15b",
"confirmations" : 147,
"time" : 1451913663,
"blocktime" : 1451913663
}
29 changes: 29 additions & 0 deletions tests/tests/xchain/TransactionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,33 @@ public function testBitcoinTransactionEventBuilder() {
PHPUnit::assertEquals($expected_fingerprint, $parsed_data['transactionFingerprint']);
}

public function testP2SHBitcoinTransactionEventBuilder() {
$mock_calls = $this->app->make('CounterpartySenderMockBuilder')->installMockCounterpartySenderDependencies($this->app, $this);

$enhanced_builder = app('App\Handlers\XChain\Network\Bitcoin\EnhancedBitcoindTransactionBuilder');
$bitcoin_data = $enhanced_builder->buildTransactionData('99c93bf83cdd4d60f234bd34ee39acc4c1b5eb66db8c932600de12b05c96d0ef');

$builder = app('App\Handlers\XChain\Network\Bitcoin\BitcoinTransactionEventBuilder');
$ts = time() * 1000;
$parsed_data = $builder->buildParsedTransactionData($bitcoin_data, $ts);
// echo "\$parsed_data: ".json_encode($parsed_data, 192)."\n";

PHPUnit::assertEmpty($parsed_data['counterpartyTx']);
PHPUnit::assertEquals(['36wJo1xtHZ2NGcCovy6vHgWvyF7ryMTjHF'], $parsed_data['sources']);
PHPUnit::assertEquals(['3EMLoeeUZtKBe6pg7wJZdVdXTJWa9pFNMR', '3BwWe6D2znQ7XnMaGaddu1MdyyGWppxRwj'], $parsed_data['destinations']);
PHPUnit::assertNotEmpty($parsed_data['values']);
PHPUnit::assertEquals(0.24858765, $parsed_data['values']['3EMLoeeUZtKBe6pg7wJZdVdXTJWa9pFNMR']);
PHPUnit::assertEquals(0.02898411, $parsed_data['values']['3BwWe6D2znQ7XnMaGaddu1MdyyGWppxRwj']);
PHPUnit::assertEquals(0.27762848, $parsed_data['bitcoinTx']['valueIn']);
PHPUnit::assertEquals(0.27757176, $parsed_data['bitcoinTx']['valueOut']);
PHPUnit::assertEquals(0.00005672, $parsed_data['bitcoinTx']['fees']);

$expected_fingerprint = hash('sha256',
'94415dc3ad8ada2f9a65623b67bd525b34f539703760a234beb6c612690abeef:1'
.'|OP_HASH160 8ae113bf266f4510fabd8e0c258973e3b9f006f2 OP_EQUAL'
.'|OP_HASH160 706f0d6f929ffc74fc7ae476a9055029cdf8515c OP_EQUAL'
);
PHPUnit::assertEquals($expected_fingerprint, $parsed_data['transactionFingerprint']);
}

}

0 comments on commit 06eea2a

Please sign in to comment.