Bit-Wasp / bitcoin-php

Bitcoin implementation in PHP
The Unlicense
1.05k stars 419 forks source link

Multisig address creation with uncompressed and compressed public keys #366

Closed beepworld closed 8 years ago

beepworld commented 8 years ago

Im am trying to create multisig address from uncompressed and compressed public keys: 040986ca780a396ec974d03309c31b6d79ff4c8ec011859bf13a9fbcc2f33432818df1efef7a23a3237ca7c808bcde554f3e16241dd4cdec16c4ef5329a93d73cc 048777374ae236dc1556a0b35f733c420a1b95e96c2dae33094fc735549942b0180f7018749ee96718c2884d62e38c0a5ff0c84321b36a8b3b33553bb57932be90 037c1eac3fd87aa7c044353272f6cee6a4865067d7900789d4020815dd3aa82762

.... PublicKeyFactory::fromHex($pubKey); .... $script = ScriptFactory::p2sh()->multisig(2, $pubKeyArray); print $script->getAddress()->getAddress();


If you work with uncompressed keys only, it'll return the same address than https://coinb.in/#newMultiSig. If one of the addresses is compressed, the address is different.

Does bitcoin-php include a function to decompress a compressed public key before feeding it into PublicKeyFactory::fromHex ? I'd like to avoid the usage of additional modules.

afk11 commented 8 years ago

If for some reason you need to take an already (compressed/uncompressed) key and convert it to (uncompressed/compressed), just instantiate a new PublicKey from the same values as that obtained by fromHex, but obviously set $compressed to whatever you'd like it to be.

It's probably a better idea to save the keys in the format you expect to use them, because as you're aware, fiddling produces different addresses, and you won't be looking for every possible form.

afk11 commented 8 years ago

(Oh, and the prefix param should be set correctly too - check the code: https://github.com/Bit-Wasp/bitcoin-php/blob/master/src/Crypto/EcAdapter/Impl/PhpEcc/Key/PublicKey.php)

Hmm, does coinbin only support compressed keys or something? Generally you just work on whatever serialized form you have, and avoid having to switch in your code.

beepworld commented 8 years ago

coinbin and bitcoin-js both support compressed and uncompressed keys. I suppose, they convert compressed pubkeys to uncompressed pubkeys before creating the multisig.

I'll convert and store all user entered pubkeys uncompressed, to avoid a mess down the road.

Thank you very much for your help, Thomas.