Bit-Wasp / bitcoin-lib-php

PHP libraries implementing bitcoin key functions, as well as BIP32 and electrum.
The Unlicense
136 stars 86 forks source link

Altcoins #79

Closed coingeek closed 9 years ago

coingeek commented 9 years ago

Hi there, I see the bitcoin-lib-php is works perfect with Bitcoin. can any you show me what and where the parameters to change in the code to be work perfect wirth Litecoin. I mean the address prefix , private key prefix and any other required parameters.

regards,

btcdrak commented 9 years ago

The APIs already cater for it as far as I am aware.

coingeek commented 9 years ago

where exactly in the code? can you explain more?

btcdrak commented 9 years ago

BitcoinLib::get_new_key_set($magic_byte);

coingeek commented 9 years ago

how i can apply it for litecoin? can you do it as example?

coingeek commented 9 years ago

I see this interested thing: https://github.com/Bit-Wasp/bitcoin-lib-php/issues/19

btcdrak commented 9 years ago

I think BitcoinLib::get_new_key_set("48");

afk11 commented 9 years ago

They're actually in hex - I was trying this for a few minutes :P

It looks like LTC follows BTC, so doing BitcoinLib::magicBytePair("30|05") will make the change stick unless you change it back.

coingeek commented 9 years ago

hi, @afk11 where you get 05 ? and how you get if from litecoin source code?

afk11 commented 9 years ago

I had to google, for some reason their github repository is not searchable. 05 is the pay to script hash version

coingeek commented 9 years ago

Yes, this is in hex? my coin is 35 for F,05 for pay private static $magic_byte_presets = array( 'bitcoin' => '00|05', 'bitcoin-testnet' => '6f|c4', );

coingeek commented 9 years ago

it should be private static $magic_byte_presets = array( 'bitcoin' => '00|05', 'bitcoin-testnet' => '6f|c4', 'mycoin' => '23|05', )

coingeek commented 9 years ago

Now, the Private key has also prefix? where i can change it?

afk11 commented 9 years ago

Yeah - that would work actually, so you can use BitcoinLib::setMagicByteDefaults('mycoin');

Your private key probably (should) have a prefix of: 0x23 + 0x80 == 35 + 128 = 163 = 0xA3 if it's compatible with bitcoind.

You can check your byte matches this by doing: BitcoinLib::base58_decode('WIF from mycoin'); <- do this: mycoin dumpprivkey mycoin getnewaddress

afk11 commented 9 years ago

If it doesn't match, you can tweak this line: https://github.com/Bit-Wasp/bitcoin-lib-php/blob/master/src/BitcoinLib.php#L534

See above, 0x80 is 128 in hex? You can tweak line 534 by doing: gmp_init('number', 10) so that the library produces the same private key prefix byte as your coin, if you want.

coingeek commented 9 years ago

Exactly it is: 0xA3 so the library get it automated ..

I have my own privatekey, i need to create the transaction ... where to go?

afk11 commented 9 years ago

Now it's the same as the examples! You get the inputs information, you create and sign a transaction, connect to your JSON RPC and broadcast :)

coingeek commented 9 years ago

mycoin publickey prefix = 35 (0x23) mycoin privatekey prefix = 163 (0xA3)

now? what i can replace 'number' here:

" See above, 0x80 is 128 in hex? You can tweak line 534 by doing: gmp_init('number', 10) so that the library produces the same private key prefix byte as your coin, if you want. "

afk11 commented 9 years ago

If you wanted to make changes to your coins private key byte, you'd make those changes to the daemon, and would have to change the number on L534 for this library to still be compatible. You don't have to do that though

coingeek commented 9 years ago

I tried like this example: https://github.com/Bit-Wasp/bitcoin-lib-php/blob/master/examples/raw_transaction.php

its shows nothing

how i can debug it.

coingeek commented 9 years ago

when i debug it. It shows:

PHP Catchable fatal error: Argument 1 passed to Mdanter\Ecc\Point::__construct() must implement interface Mdanter\Ecc\MathAdapterInterface, instance of Mdanter\Ecc\Curves\NamedCurveFp given, called in /var/www/t2/bitcoin-lib-php/src/BitcoinLib.php on line 679 and defined in /var/www/t2/bitcoin-lib-php/vendor/mdanter/ecc/src/Point.php on line 63

coingeek commented 9 years ago

i fixed the previous problem, but i face new one:

PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Values should be in Satoshis [0.00015]' in /var/www/x/bitcoin-lib-php/src/RawTransaction.php:1020 Stack trace:

0 /var/www/x/bitcoin-lib-php/examples/ts.php(36): BitWasp\BitcoinLib\RawTransaction::create(Array, Array)

1 {main}

thrown in /var/www/x/bitcoin-lib-php/src/RawTransaction.php on line 1020

my ts.php code:

<?php

use BitWasp\BitcoinLib\RawTransaction;

require_once(DIR. '/../vendor/autoload.php');

///////////////////////////// // Parameters for creation.. // Set up inputs here $inputs = array( array( 'txid' => '6a6f4c832fce9e8cc489cadb6f2da4f3bcb7c7437ca1fa7abd562c5b3327954d', 'vout' => 0 ) ); // Set up outputs here. $outputs = array( 'FJqhLzywMxh5xywBLPQKH7Na1M29szuVvD' => "0.00015");

//////////////////////////// // Parameters for signing. // Create JSON inputs parameter // - These can come from bitcoind, or just knowledge of the txid/vout/scriptPubKey, // and redeemScript if needed. $json_inputs = json_encode( array( array( 'txid' => '6a6f4c832fce9e8cc489cadb6f2da4f3bcb7c7437ca1fa7abd562c5b3327954d', 'vout' => 0, // OP_DUP OP_HASH160 push14bytes PkHash OP_EQUALVERIFY OP_CHECKSIG 'scriptPubKey' => '76a914'.'7e3f939e8ded8c0d93695310d6d481ae5da39616'.'88ac') ) ); // Private Key $wallet = array(); RawTransaction::private_keys_to_wallet($wallet, array('xxtCx8Ug5j1bEW7paTpmTRnxxx3HEVXSBYies9FXQr9uk211Bcxx'), '23');

// Create raw transaction $raw_transaction = RawTransaction::create($inputs, $outputs);

// Sign the transaction // To broadcast you would send the $sign['hex'] on to the network // eg; with bitcoind sendrawtransaction <hex> $sign = RawTransaction::sign($wallet, $raw_transaction, $json_inputs); print_r($sign); echo "\n";

// Get the transaction hash from the raw transaction $txhash = RawTransaction::hash_from_raw($sign['hex']); print_r($txhash); echo "\n";

afk11 commented 9 years ago

That is correct, it is different to bitcoind that you have to pass the amount as satoshis, not like 0.00015. There are some functions in BitcoinLib to convert between satoshis/BTC if you have bcmath installed!

coingeek commented 9 years ago

So what the correct format. What i can suppose to passs.

coingeek commented 9 years ago

i did this and still has error:

$outputs = array( 'FJqhLzywMxh5xywBLPQKH7Na1M29szuVvD' => 100000000);

the error:

php ts.php Array ( [hex] => 01000000014d9527335b2c56bd7afaa17c43c7b7bcf3a42d6fdbca89c48c9ece2f834c6f6a0000000000ffffffff0100e1f505000000001976a9148eb5eb48c373db22e6bd6f6e5c2c5ad3460f791f88ac00000000 [complete] => false [sign_count] => 0 [req_sigs] => 1 )

PHP Fatal error: Call to undefined method BitWasp\BitcoinLib\RawTransaction::hash_from_raw() in /var/www/x/bitcoin-lib-php/examples/ts.php on line 47

coingeek commented 9 years ago

i found the problem it was in fixing: 'scriptPubKey' => '76a914'.'df77210148477741f54dc76c98478dd9ec252694'.'88ac', thenit sign as true.

now the issue it created rawtransaction, but it still not work:

root@Debian# php full_transaction.php Array ( [FJqhLzywMxh5xywBLPQKH7Na1M29szuVvD] => 100000000 [FSCgroVeSHeSZ1SKr8gpnFauqyN8T8s6p3] => 697890000 )

Array ( [df77210148477741f54dc76c98478dd9ec252694] => Array ( [type] => pubkeyhash [private_key] => xxxxb239f0f68047aaf5d59d6f63357f2d77bf1bf339f71f107a6f09807af4a9 [public_key] => 028f10e917f4f197eb3ebb5e5b9422d783d7fa6786df19bd8eafd3377c8da8a71e [uncompressed_key] => 048f10e917f4f197eb3ebb5e5b9422d783d7fa6786df19bd8eafd3377c8da8a71eb23073e931ffcd555a0ff16a2a010b8bcc5b3fe4116baa549065fa2692079726 [is_compressed] => 1 [address] => FSCgroVeSHeSZ1SKr8gpnFauqyN8T8s6p3 )

)

Array ( [hex] => 01000000014d9527335b2c56bd7afaa17c43c7b7bcf3a42d6fdbca89c48c9ece2f834c6f6a000000006b48304502201ab111635c204f742af64eabf833d21f2d9188d02d6a39f82d6c2b72c86982c0022100d0342e9672b5177e38f702267ac9fa1ce862ef9bd7f63348fdc0ae175c6b181c0121028f10e917f4f197eb3ebb5e5b9422d783d7fa6786df19bd8eafd3377c8da8a71effffffff0200e1f505000000001976a9148eb5eb48c373db22e6bd6f6e5c2c5ad3460f791f88acd0f49829000000001976a914df77210148477741f54dc76c98478dd9ec25269488ac00000000 [complete] => true [sign_count] => 1 [req_sigs] => 1 )

23e18c15cac8b98a221bbf11ce9da871c40eafd031a48bb0a3a8341c172d608f


see this image when i broadcast the rawtransaction:

http://i.imgur.com/EB1P5tX.png

afk11 commented 9 years ago

Can you look at your debug log and tell me what comes up when you try put through one of these transactions?

This lib is being used in a few production settings against bitcoin v0.10, so I'm skeptical about problems arising from integrating with other coins - I think you might need to find more details for us to help you

coingeek commented 9 years ago

this the result when i put it in my debug:

http://i.imgur.com/EB1P5tX.png

afk11 commented 9 years ago

I mean this file: C:\Users\Documents and settings\you\AppData\Roaming\yourcoind\debug.log

coingeek commented 9 years ago

Sure, will do that.

coingeek commented 9 years ago

2015-03-30 23:20:59 ThreadRPCServer method=sendrawtransaction 2015-03-30 23:20:59 ERROR: CheckInputs() : 077ceb8ff94255e730c576357e6ad4803a404032bab270deefcb008e53873e0d value in < value out 2015-03-30 23:20:59 ERROR: CTxMemPool::accept() : ConnectInputs failed 077ceb8ff94255e730c576357e6ad4803a404032bab270deefcb008e53873e0d

coingeek commented 9 years ago

2015-03-30 23:24:59 ERROR: CScriptCheck() : e634d11f0000933f12986d3ef1e744eb4903eb2d43868dec4093bf10b095cd11 VerifySignature failed 2015-03-30 23:24:59 ERROR: CScriptCheck() : e634d11f0000933f12986d3ef1e744eb4903eb2d43868dec4093bf10b095cd11 VerifySignature failed 2015-03-30 23:24:59 ERROR: CTxMemPool::accept() : ConnectInputs failed e634d11f0000933f12986d3ef1e744eb4903eb2d43868dec4093bf10b095cd11

afk11 commented 9 years ago

Your first paste: you tried to spend more than you should Second paste: the signature was incorrect for some reason. This is probably the case if your $inputs doesn't match the real $json_inputs. The scriptPubKey is used to make a hash of the transaction, which your signature is made from. If you do it wrong, the network will realize.

I'd suggest trying to set up a blockexplorer for this if you can, or perhaps use testnet for practicing since you can see confirmed tx's on https://blockexplorer.com/testnet - raw transactions aren't the easiest to jump into :)

coingeek commented 9 years ago

I think the main problem is how to get valid scriptPubKey??

$inputs = array( array( 'txid' => '075319346d8f07b0bf15917f10fdcdee8e2c099060df80d743d1956abb913d83', 'vout' => 0 ) ); // Set up outputs here. $outputs = array('FSCgroVeSHeSZ1SKr8gpnFauqyN8T8s6p3' => BitcoinLib::toSatoshi(0.0000085)); $json_inputs = json_encode( array( array('txid' => '075319346d8f07b0bf15917f10fdcdee8e2c099060df80d743d1956abb913d83', 'vout' => 0, // OP_DUP OP_HASH160 push14bytes PkHash OP_EQUALVERIFY OP_CHECKSIG 'scriptPubKey' =>'76a914' . '8eb5eb48c373db22e6bd6f6e5c2c5ad3460f791f' . '88ac')

coingeek commented 9 years ago

it give me like that: Array ( [hex] => 0100000001833d91bb6a95d143d780df6090092c8eeecdfd107f9115bfb0078f6d34195307000000006b4830450221009ae8f0a9833d38ccfeb381dddb430203aac3ba9e6e88dc6224bdb67ffc62e08102205ff4a1e819021369a83d973444c3f17b478517143b86ffb52ae7536cf331c8b301210372f040b09055ae37ee0bd8c1ec694b786faab5cf267f36976d8117b126140916ffffffff0152030000000000001976a914df77210148477741f54dc76c98478dd9ec25269488ac00000000 [complete] => true [sign_count] => 1 [req_sigs] => 1 )

7415cf366fdf4c362961da2fed0992e71399d731b5d45203bb4353ba3ae3a478

but when execute it it gave me: error: {"code":-22,"message":"TX rejected"}

coingeek commented 9 years ago

and the debug.log:

2015-03-31 00:04:03 ThreadRPCServer method=sendrawtransaction 2015-03-31 00:04:03 ERROR: CScriptCheck() : 50b49a937d284033a1403118769d1e4fd05861fd0a6fcb6e6bf4c877e570e8af VerifySignature failed 2015-03-31 00:04:03 ERROR: CScriptCheck() : 50b49a937d284033a1403118769d1e4fd05861fd0a6fcb6e6bf4c877e570e8af VerifySignature failed 2015-03-31 00:04:03 ERROR: CTxMemPool::accept() : ConnectInputs failed 50b49a937d284033a1403118769d1e4fd05861fd0a6fcb6e6bf4c877e570e8af

afk11 commented 9 years ago

Ok, just to help me, can you tell me what you're putting in for the scriptPubKey and where you get it? The scriptPubKey middle section is the hash that addresses are made from. The value should be substr(BitcoinLib::base58_decode($currentAddress), 2, 40);

coingeek commented 9 years ago

@afk11 thanks, this method gives me the scriptPubKey, but after that the debug gives me the same error. see: 2015-03-31 07:16:55 ERROR: CScriptCheck() : 9af7a3395ff4b8206efdcf1e5451e7c0f581bf934c6d1ccec63d5de3b2f4a7a1 VerifySignature failed 2015-03-31 07:16:55 ERROR: CScriptCheck() : 9af7a3395ff4b8206efdcf1e5451e7c0f581bf934c6d1ccec63d5de3b2f4a7a1 VerifySignature failed 2015-03-31 07:16:55 ERROR: CTxMemPool::accept() : ConnectInputs failed 9af7a3395ff4b8206efdcf1e5451e7c0f581bf934c6d1ccec63d5de3b2f4a7a1


this the code: $currentAddress1='FJqhLzywMxh5xywBLPQKH7Na1M29szuVvD'; $lx = substr(BitcoinLib::base58_decode($currentAddress1), 2, 40); print_r($lx); echo "\n";


$json_inputs = json_encode( array( array('txid' => '075319346d8f07b0bf15917f10fdcdee8e2c099060df80d743d1956abb913d83', 'vout' => 0, // OP_DUP OP_HASH160 push14bytes PkHash OP_EQUALVERIFY OP_CHECKSIG 'scriptPubKey' => '76a914' . $lx . '88ac')
)

);

$inputs = array( array( 'txid' => '075319346d8f07b0bf15917f10fdcdee8e2c099060df80d743d1956abb913d83', 'vout' => 0 ) );