44
55namespace AndKom \Bitcoin \Wallet \Item ;
66
7- use AndKom \Bitcoin \Wallet \Exception ;
8- use BitWasp \Bitcoin \Crypto \EcAdapter \Key \PrivateKeyInterface ;
9- use BitWasp \Bitcoin \Crypto \EcAdapter \Key \PublicKeyInterface ;
10- use BitWasp \Bitcoin \Key \PrivateKeyFactory ;
11- use BitWasp \Bitcoin \Key \PublicKeyFactory ;
12-
137/**
148 * Class Key
159 * @package AndKom\Bitcoin\Wallet\Item
@@ -26,12 +20,12 @@ class Key
2620 /**
2721 * @var string
2822 */
29- protected $ private ;
23+ protected $ secret ;
3024
3125 /**
3226 * @var string
3327 */
34- protected $ secret ;
28+ protected $ private ;
3529
3630 /**
3731 * @var KeyMeta
@@ -41,15 +35,14 @@ class Key
4135 /**
4236 * Key constructor.
4337 * @param string $public
44- * @param string $private
38+ * @param string $secret
4539 * @param KeyMeta $meta
4640 */
47- public function __construct (string $ public , string $ private , KeyMeta $ meta = null )
41+ public function __construct (string $ public , string $ secret , KeyMeta $ meta = null )
4842 {
4943 $ this ->public = $ public ;
50- $ this ->private = $ private ;
44+ $ this ->secret = $ secret ;
5145 $ this ->meta = $ meta ;
52- $ this ->secret = $ this ->parseSecret ($ private );
5346 }
5447
5548 /**
@@ -70,36 +63,22 @@ protected function parseSecret(string $data): string
7063 }
7164
7265 /**
73- * @return PublicKeyInterface
74- * @throws Exception
66+ * @return string
7567 */
76- public function getPublicKey (): PublicKeyInterface
68+ public function getPublicKey (): string
7769 {
78- $ hex = bin2hex ($ this ->public );
79-
80- try {
81- $ publicKey = PublicKeyFactory::fromHex ($ hex );
82- } catch (\Exception $ exception ) {
83- throw new Exception ('Unable to decode public key: ' . $ exception ->getMessage ());
84- }
85-
86- return $ publicKey ;
70+ return $ this ->public ;
8771 }
8872
8973 /**
90- * @return PrivateKeyInterface
91- * @throws Exception
74+ * @return string
9275 */
93- public function getPrivateKey (): PrivateKeyInterface
76+ public function getPrivateKey (): string
9477 {
95- $ hex = bin2hex ($ this ->secret );
96-
97- try {
98- $ privateKey = PrivateKeyFactory::fromHex ($ hex , $ this ->getPublicKey ()->isCompressed ());
99- } catch (\Exception $ exception ) {
100- throw new Exception ('Unable to decode private key: ' . $ exception ->getMessage ());
78+ if (!$ this ->private ) {
79+ $ this ->private = $ this ->parseSecret ($ this ->secret );
10180 }
10281
103- return $ privateKey ;
82+ return $ this -> private ;
10483 }
10584}
0 commit comments