Title: How to convert btc address to hash160 and other way around in PHP ? Post by: btctousd81 on November 09, 2017, 06:16:43 AM i tried looking for ways to convert btc address to hash160 hex and other way around,
i have found few tools iwritten n c++, which works on command line., but i am trying to implement it in my own php code. but i couldn't find any php code to convert btc address to hash160 address or the other way around. this guys has the code http://bitcoinvalued.com/tools.php i tried contacting him, but he is no mood to share the code., even blockchain does it on the fly , https://blockchain.info/address/14CFVhgRnd94q9Bq6uMigptDfwtKnMPVZs Address 14CFVhgRnd94q9Bq6uMigptDfwtKnMPVZs Hash 160 23090d78bc991026455285f511c5563e6765f03d any other guys have php code ? Thanks for your time. Title: Re: How to convert btc address to hash160 and other way around in PHP ? Post by: aleksej996 on November 09, 2017, 08:23:40 PM As I understand you just need to Base58 decode it and strip off first byte (network byte) and last 4 (checksum bytes). That should give you your 160 bit number you are looking for. I found this PHP library for Base58 encoding/decoding for you https://github.com/stephen-hill/base58php (https://github.com/stephen-hill/base58php)
Title: Re: How to convert btc address to hash160 and other way around in PHP ? Post by: btctousd81 on November 09, 2017, 08:30:48 PM As I understand you just need to Base58 decode it and strip off first byte (network byte) and last 4 (checksum bytes). That should give you your 160 bit number you are looking for. I found this PHP library for Base58 encoding/decoding for you https://github.com/stephen-hill/base58php (https://github.com/stephen-hill/base58php) thanks looking in to it, and what about hash160 to btc address., where does the last 4 bytes we stripped off comes from ? while doinng hash 160 to btc address, i mean if we reverse the process. Title: Re: How to convert btc address to hash160 and other way around in PHP ? Post by: aleksej996 on November 09, 2017, 08:37:03 PM As I understand you just need to Base58 decode it and strip off first byte (network byte) and last 4 (checksum bytes). That should give you your 160 bit number you are looking for. I found this PHP library for Base58 encoding/decoding for you https://github.com/stephen-hill/base58php (https://github.com/stephen-hill/base58php) thanks looking in to it, and what about hash160 to btc address., where does the last 4 bytes we stripped off comes from ? while doinng hash 160 to btc address, i mean if we reverse the process. Theose are the first 4 bytes of a double sha256 hash of the hash160 with the added first network byte. So the first network byte (mainnet is zero) and 160 bit number is 168 bits. You hash that with sha256 and the result hash again with sha256, then just take the first 4 bytes. You can find more information here https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses (https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses) Title: Re: How to convert btc address to hash160 and other way around in PHP ? Post by: btctousd81 on November 09, 2017, 09:38:22 PM As I understand you just need to Base58 decode it and strip off first byte (network byte) and last 4 (checksum bytes). That should give you your 160 bit number you are looking for. I found this PHP library for Base58 encoding/decoding for you https://github.com/stephen-hill/base58php (https://github.com/stephen-hill/base58php) thanks looking in to it, and what about hash160 to btc address., where does the last 4 bytes we stripped off comes from ? while doinng hash 160 to btc address, i mean if we reverse the process. Theose are the first 4 bytes of a double sha256 hash of the hash160 with the added first network byte. So the first network byte (mainnet is zero) and 160 bit number is 168 bits. You hash that with sha256 and the result hash again with sha256, then just take the first 4 bytes. You can find more information here https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses (https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses) okie., so far i have managed to do bitcoin address to hash160 for p2pkh for p2sh hash160 to btc address for p2pkh only how to make it work with p2sh ? thanks why p2sh 's hash160 gives p2pkh address upon conversion through hash160 to bitcoin address and not the p2sh addresss ? i mean https://blockchain.info/address/39ZPyRrVR4jYoRG1f3MeMm9jES9SPZyMCB has hash160 as 565015b43956dd69623061d66039ff854c0690ba but if i go for https://blockchain.info/address/565015b43956dd69623061d66039ff854c0690ba i get Address 18sP3tN3sARAiFZaXwh3w8no5uriuM8Yyz so if i again g o to https://blockchain.info/address/18sP3tN3sARAiFZaXwh3w8no5uriuM8Yyz i fuckin get Hash 160 565015b43956dd69623061d66039ff854c0690ba so in short https://blockchain.info/address/39ZPyRrVR4jYoRG1f3MeMm9jES9SPZyMCB and https://blockchain.info/address/18sP3tN3sARAiFZaXwh3w8no5uriuM8Yyz both have same hash160 , how is that possible ? something is not right., or its the way it is ? my code is also giving same outputs as blockchain.info . so ????????????? thanks Title: Re: How to convert btc address to hash160 and other way around in PHP ? Post by: HCP on November 09, 2017, 11:13:07 PM hash160 to btc address Because, as Danny already told you (https://bitcointalk.org/index.php?topic=2294855.msg23375280#msg23375280), you don't hash a public key to get to get a P2SH address... it is a "Pay to SCRIPT hash"... you need to hash a "redeem script", not a public key. Read my answer in your other thread: https://bitcointalk.org/index.php?topic=2380374.msg24318230#msg24318230for p2pkh only how to make it work with p2sh ? why p2sh 's hash160 gives p2pkh address upon conversion through hash160 to bitcoin address and not the p2sh addresss ? PS. don't create multiple threads for the same question, it'll fragment/split the answers and make things more confusing. Suggest you pick one thread and lock the other. Title: Re: How to convert btc address to hash160 and other way around in PHP ? Post by: btctousd81 on November 10, 2017, 02:37:02 AM hash160 to btc address Because, as Danny already told you (https://bitcointalk.org/index.php?topic=2294855.msg23375280#msg23375280), you don't hash a public key to get to get a P2SH address... it is a "Pay to SCRIPT hash"... you need to hash a "redeem script", not a public key. Read my answer in your other thread: https://bitcointalk.org/index.php?topic=2380374.msg24318230#msg24318230for p2pkh only how to make it work with p2sh ? why p2sh 's hash160 gives p2pkh address upon conversion through hash160 to bitcoin address and not the p2sh addresss ? PS. don't create multiple threads for the same question, it'll fragment/split the answers and make things more confusing. Suggest you pick one thread and lock the other. okie., thanks , looking in to it., this is completely different thread than my other thread, here i am working on php code to get hash160 from btc address. while on other thread i am asking for how to create p2sh address and tx. thanks Title: Re: How to convert btc address to hash160 and other way around in PHP ? Post by: Sneginka111 on November 11, 2017, 06:11:08 AM As I understand you just need to Base58 decode it and strip off first byte (network byte) and last 4 (checksum bytes). That should give you your 160 bit number you are looking for. I found this PHP library for Base58 encoding/decoding for you https://github.com/stephen-hill/base58php (https://github.com/stephen-hill/base58php) Yes, I found it there tooTitle: Re: How to convert btc address to hash160 and other way around in PHP ? Post by: aleksej996 on November 11, 2017, 06:43:22 PM As I understand you just need to Base58 decode it and strip off first byte (network byte) and last 4 (checksum bytes). That should give you your 160 bit number you are looking for. I found this PHP library for Base58 encoding/decoding for you https://github.com/stephen-hill/base58php (https://github.com/stephen-hill/base58php) thanks looking in to it, and what about hash160 to btc address., where does the last 4 bytes we stripped off comes from ? while doinng hash 160 to btc address, i mean if we reverse the process. Theose are the first 4 bytes of a double sha256 hash of the hash160 with the added first network byte. So the first network byte (mainnet is zero) and 160 bit number is 168 bits. You hash that with sha256 and the result hash again with sha256, then just take the first 4 bytes. You can find more information here https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses (https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses) okie., so far i have managed to do bitcoin address to hash160 for p2pkh for p2sh hash160 to btc address for p2pkh only how to make it work with p2sh ? thanks why p2sh 's hash160 gives p2pkh address upon conversion through hash160 to bitcoin address and not the p2sh addresss ? i mean https://blockchain.info/address/39ZPyRrVR4jYoRG1f3MeMm9jES9SPZyMCB has hash160 as 565015b43956dd69623061d66039ff854c0690ba but if i go for https://blockchain.info/address/565015b43956dd69623061d66039ff854c0690ba i get Address 18sP3tN3sARAiFZaXwh3w8no5uriuM8Yyz so if i again g o to https://blockchain.info/address/18sP3tN3sARAiFZaXwh3w8no5uriuM8Yyz i fuckin get Hash 160 565015b43956dd69623061d66039ff854c0690ba so in short https://blockchain.info/address/39ZPyRrVR4jYoRG1f3MeMm9jES9SPZyMCB and https://blockchain.info/address/18sP3tN3sARAiFZaXwh3w8no5uriuM8Yyz both have same hash160 , how is that possible ? something is not right., or its the way it is ? my code is also giving same outputs as blockchain.info . so ????????????? thanks This is because they are encoded differently. It seems that the only difference is that the version byte is 5 in P2SH instead of 0. https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki (https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki) hash160 to btc address Because, as Danny already told you (https://bitcointalk.org/index.php?topic=2294855.msg23375280#msg23375280), you don't hash a public key to get to get a P2SH address... it is a "Pay to SCRIPT hash"... you need to hash a "redeem script", not a public key. Read my answer in your other thread: https://bitcointalk.org/index.php?topic=2380374.msg24318230#msg24318230for p2pkh only how to make it work with p2sh ? why p2sh 's hash160 gives p2pkh address upon conversion through hash160 to bitcoin address and not the p2sh addresss ? PS. don't create multiple threads for the same question, it'll fragment/split the answers and make things more confusing. Suggest you pick one thread and lock the other. OP is just asking of getting hash 160 of a bitcoin address and vice versa, whether or not he understands what an address even is or what that hash 160 is of is a whole other story. |