44
55namespace AndKom \Bitcoin \Blockchain ;
66
7+ use AndKom \Bitcoin \Blockchain \Exception \AddressException ;
78use AndKom \Bitcoin \Blockchain \Network \Bitcoin ;
89use function BitWasp \Bech32 \encodeSegwit ;
10+ use StephenHill \Base58 ;
911
1012/**
1113 * Class AddressSerializer
@@ -31,24 +33,49 @@ public function __construct(Bitcoin $network = null)
3133 $ this ->network = $ network ;
3234 }
3335
36+ /**
37+ * @param string $hash160
38+ * @param int $prefix
39+ * @return string
40+ * @throws \Exception
41+ */
42+ public function hashToAddress (string $ hash160 , int $ prefix ): string
43+ {
44+ $ hash160 = chr ($ prefix ) . $ hash160 ;
45+ $ checksum = substr (Utils::hash256 ($ hash160 , true ), 0 , 4 );
46+ $ address = $ hash160 . $ checksum ;
47+ return (new Base58 ())->encode ($ address );
48+ }
49+
50+ /**
51+ * @param string $pubKey
52+ * @param int $prefix
53+ * @return string
54+ * @throws \Exception
55+ */
56+ public function pubKeyToAddress (string $ pubKey , int $ prefix ): string
57+ {
58+ return $ this ->hashToAddress (Utils::hash160 ($ pubKey , true ), $ prefix );
59+ }
60+
3461 /**
3562 * @param string $pubKey
3663 * @return string
3764 * @throws \Exception
3865 */
39- public function getPayToPubKeyAddress (string $ pubKey ): string
66+ public function getPayToPubKey (string $ pubKey ): string
4067 {
41- return Utils:: pubKeyToAddress ($ pubKey , $ this ->network ::P2PKH_PREFIX );
68+ return $ this -> pubKeyToAddress ($ pubKey , $ this ->network ::P2PKH_PREFIX );
4269 }
4370
4471 /**
4572 * @param string $pubKeyHash
4673 * @return string
4774 * @throws \Exception
4875 */
49- public function getPayToPubKeyHashAddress (string $ pubKeyHash ): string
76+ public function getPayToPubKeyHash (string $ pubKeyHash ): string
5077 {
51- return Utils:: hash160ToAddress ($ pubKeyHash , $ this ->network ::P2PKH_PREFIX );
78+ return $ this -> hashToAddress ($ pubKeyHash , $ this ->network ::P2PKH_PREFIX );
5279 }
5380
5481 /**
@@ -58,7 +85,7 @@ public function getPayToPubKeyHashAddress(string $pubKeyHash): string
5885 */
5986 public function getPayToScriptHash (string $ scriptHash ): string
6087 {
61- return Utils:: hash160ToAddress ($ scriptHash , $ this ->network ::P2SH_PREFIX );
88+ return $ this -> hashToAddress ($ scriptHash , $ this ->network ::P2SH_PREFIX );
6289 }
6390
6491 /**
0 commit comments