22
33namespace BlockCypher \Crypto ;
44
5+ use BitWasp \Bitcoin \Key \PrivateKeyInterface ;
56use BlockCypher \Validation \ArgumentArrayValidator ;
67use BlockCypher \Validation \CoinSymbolValidator ;
78
@@ -14,14 +15,11 @@ class PrivateKeyList
1415 /**
1516 * @var PrivateKey[]
1617 */
17- private $ keys ;
18+ private $ privateKeys ;
1819
19- /**
20- * @param PrivateKey[] $keys
21- */
22- function __construct ($ keys = null )
20+ function __construct ()
2321 {
24- $ this ->keys = $ keys ;
22+ $ this ->privateKeys = array () ;
2523 }
2624
2725 /**
@@ -34,101 +32,159 @@ public static function fromHexPrivateKeyArray($hexPrivateKeys, $coinSymbol)
3432 ArgumentArrayValidator::validate ($ hexPrivateKeys , 'hexPrivateKeys ' );
3533 CoinSymbolValidator::validate ($ coinSymbol , 'coinSymbol ' );
3634
37- $ privateKeyList = array ();
35+ $ privateKeyList = new self ($ coinSymbol );
36+
3837 foreach ($ hexPrivateKeys as $ hexPrivateKey ) {
3938 $ compressed = true ;
4039 $ privateKey = PrivateKeyManipulator::importPrivateKeyFromHex ($ hexPrivateKey , $ compressed );
4140
42- // Add private key indexed by address
43- $ address = PrivateKeyManipulator::getAddressFromPrivateKey ($ privateKey , $ coinSymbol );
44- $ privateKeyList [$ address ] = $ privateKey ;
45-
4641 // Add private key indexed by public key
47- $ pubKeyHex = $ privateKey ->getPublicKey ()->getHex ();
48- $ privateKeyList [$ pubKeyHex ] = $ privateKey ;
42+ $ privateKeyList ->addPrivateKey ($ privateKey );
4943 }
50-
51- return new self ($ privateKeyList );
44+ return $ privateKeyList ;
5245 }
5346
5447 /**
55- * Append Key to the list.
48+ * Append private key to the list.
5649 *
57- * @param PrivateKey $key
58- * @param string $address
50+ * @param PrivateKeyInterface $privateKey
5951 * @return $this
60- * @throws \Exception
6152 */
62- public function addKey ($ key , $ address = null )
53+ public function addPrivateKey (PrivateKeyInterface $ privateKey )
54+ {
55+ $ pubKeyHex = $ privateKey ->getPublicKey ()->getHex ();
56+ $ this ->privateKeys [$ pubKeyHex ] = $ privateKey ;
57+ }
58+
59+ /**
60+ * @param string $addressOrPublicKey
61+ * @param string $coinSymbol
62+ * @return PrivateKey|null
63+ */
64+ public function getPrivateKey ($ addressOrPublicKey , $ coinSymbol )
6365 {
64- if ($ address === null ) {
65- $ this ->keys [] = $ key ;
66+ $ privateKey = $ this ->getPrivateKeyByPublicKey ($ addressOrPublicKey );
67+ if ($ privateKey !== null ) {
68+ return $ privateKey ;
69+ }
70+
71+ $ privateKey = $ this ->getPrivateKeyByAddress ($ addressOrPublicKey , $ coinSymbol );
72+ if ($ privateKey !== null ) {
73+ return $ privateKey ;
74+ }
75+
76+ return null ;
77+ }
78+
79+ /**
80+ * @param $publicKeyHex
81+ * @return PrivateKey|null
82+ */
83+ private function getPrivateKeyByPublicKey ($ publicKeyHex )
84+ {
85+ if (isset ($ this ->privateKeys [$ publicKeyHex ])) {
86+ return $ this ->privateKeys [$ publicKeyHex ];
6687 } else {
67- if (isset ($ this ->keys [$ address ])) {
68- throw new \Exception ("Key $ address already in use. " );
69- } else {
70- $ this ->keys [$ address ] = $ key ;
88+ return null ;
89+ }
90+ }
91+
92+ /**
93+ * @param string $addressToFind
94+ * @param $coinSymbol
95+ * @return PrivateKey|null
96+ */
97+ private function getPrivateKeyByAddress ($ addressToFind , $ coinSymbol )
98+ {
99+ foreach ($ this ->privateKeys as $ privateKey ) {
100+ $ address = PrivateKeyManipulator::getAddressFromPrivateKey ($ privateKey , $ coinSymbol );
101+ if ($ address == $ addressToFind ) {
102+ return $ privateKey ;
71103 }
72104 }
105+ return null ;
73106 }
74107
75108 /**
76- * @param string $address
77- * @throws \Exception
109+ * @param string $addressOrPublicKey
110+ * @param string $coinSymbol
111+ * @return bool
78112 */
79- public function deleteKey ( $ address )
113+ public function privateKeyExists ( $ addressOrPublicKey , $ coinSymbol )
80114 {
81- if (isset ($ this ->keys [$ address ])) {
82- unset($ this ->keys [$ address ]);
83- } else {
84- throw new \Exception ("Invalid address $ address. " );
115+ if ($ this ->privateKeyExistsForPubKey ($ addressOrPublicKey )) {
116+ return true ;
85117 }
118+
119+ if ($ this ->privateKeyExistsForAddress ($ addressOrPublicKey , $ coinSymbol )) {
120+ return true ;
121+ }
122+
123+ return false ;
86124 }
87125
88126 /**
89- * @param string $address
90- * @return PrivateKey
91- * @throws \Exception
127+ * @param string $pubKeyHex
128+ * @return bool
92129 */
93- public function getKey ( $ address )
130+ private function privateKeyExistsForPubKey ( $ pubKeyHex )
94131 {
95- if (isset ( $ this ->keys [ $ address ]) ) {
96- return $ this -> keys [ $ address ] ;
132+ if ($ this ->getPrivateKeyByPublicKey ( $ pubKeyHex ) !== null ) {
133+ return true ;
97134 } else {
98- throw new \ Exception ( " Address $ address not found in PrivateKeyList. " ) ;
135+ return false ;
99136 }
100137 }
101138
102139 /**
103- * @param string $address
140+ * @param string $addressToFind
141+ * @param $coinSymbol
104142 * @return bool
105143 */
106- public function keyExists ( $ address )
144+ private function privateKeyExistsForAddress ( $ addressToFind , $ coinSymbol )
107145 {
108- return isset ($ this ->keys [$ address ]);
146+ if ($ this ->getPrivateKeyByAddress ($ addressToFind , $ coinSymbol ) !== null ) {
147+ return true ;
148+ } else {
149+ return false ;
150+ }
109151 }
110152
111153 /**
112154 * @return PrivateKey[]
113155 */
114- public function getKeys ()
156+ public function getPrivateKeys ()
115157 {
116- return $ this ->keys ;
158+ return $ this ->privateKeys ;
117159 }
118160
119161 /**
120162 * @return string[]
121163 */
122- public function addresses ()
164+ public function getPublicKeys ()
165+ {
166+ return array_keys ($ this ->privateKeys );
167+ }
168+
169+ /**
170+ * @param string $coinSymbol
171+ * @return \string[]
172+ */
173+ public function getAddresses ($ coinSymbol )
123174 {
124- return array_keys ($ this ->keys );
175+ $ addresses = array ();
176+ foreach ($ this ->privateKeys as $ privateKey ) {
177+ $ address = PrivateKeyManipulator::getAddressFromPrivateKey ($ privateKey , $ coinSymbol );
178+ $ addresses [] = $ address ;
179+ }
180+ return $ addresses ;
125181 }
126182
127183 /**
128184 * @return int
129185 */
130186 public function length ()
131187 {
132- return count ($ this ->keys );
188+ return count ($ this ->privateKeys );
133189 }
134- }
190+ }
0 commit comments