PHP - Validate Multisig Bitcoin Wallet Address with Leading 2 or 3

0 votes

I have used this function for validating the bitcoin wallet address to see if it's a bitcoin address. However, it's not working with the address that starts with "3" (multi-sig). For example, 3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX.

Can anyone help me with that function to make it work so that it can validate all major forms of bitcoin wallet addresses?

Apr 7, 2022 in Blockchain by Rahul
• 9,680 points
1,952 views

1 answer to this question.

0 votes

 The answer to your question is given below: 

function validate($address){ $decoded = decodeBase58($address); $d1 = hash("sha256", substr($decoded,0,21), true); $d2 = hash("sha256", $d1, true); if(substr_compare($decoded, $d2, 21, 4)){ throw new \Exception("bad digest"); } return true; } function decodeBase58($input) { $alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; $out = array_fill(0, 25, 0); for($i=0;$i<strlen($input);$i++){ if(($p=strpos($alphabet, $input[$i]))===false){ throw new \Exception("invalid character found"); } $c = $p; for ($j = 25; $j--; ) { $c += (int)(58 * $out[$j]); $out[$j] = (int)($c % 256); $c /= 256; $c = (int)$c; } if($c != 0){ throw new \Exception("address too long"); } } $result = ""; foreach($out as $val){ $result .= chr($val); } return $result; } function main () { $s = array( "1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9", "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", "1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9", "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I", ); foreach($s as $btc){ $message = "OK"; try{ validate($btc); }catch(\Exception $e){ $message = $e->getMessage(); } echo "$btc: $message\n"; } } main();

Hope this clarifies your doubt. 

Get your blockchain certification to become certified today.

answered Apr 11, 2022 by Soham
• 9,710 points

Related Questions In Blockchain

+3 votes
3 answers

How to validate bitcoin address?

I found these 2 scripts online: Javascript: <html> <head> <script type="text/javascript" ...READ MORE

answered Aug 17, 2018 in Blockchain by slayer
• 29,370 points
6,470 views
0 votes
1 answer

How to open bitcoin-qt wallet with bitcoinj?

I found this issue solved at the ...READ MORE

answered Aug 24, 2018 in Blockchain by slayer
• 29,370 points
699 views
0 votes
1 answer

How to create Bitcoin Address in PHP?

You need to understand that Bitcoin address and ...READ MORE

answered Aug 28, 2018 in Blockchain by Perry
• 17,100 points
5,435 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,409 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,913 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,442 views
0 votes
1 answer

Get amount of Bitcoins at an address with PHP

While checking the Coinbase address balance, the ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,710 points
494 views
0 votes
1 answer

Know which exchange or wallet is behind a BTC address?

To answer your question, due to each ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,710 points
6,084 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP