Skip to content

Commit 50cef6c

Browse files
committed
remove external dependency
1 parent bf39230 commit 50cef6c

File tree

5 files changed

+25
-67
lines changed

5 files changed

+25
-67
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
],
1616
"require": {
1717
"php": "^7.1",
18-
"bitwasp/bitcoin": "~0.0.34",
1918
"andkom/php-berkeley-db": "^1.1",
2019
"andkom/php-bcdatastream": "^1.1"
2120
},
@@ -32,4 +31,4 @@
3231
"AndKom\\Bitcoin\\Wallet\\Tests\\": "tests/"
3332
}
3433
}
35-
}
34+
}

src/Item/EncryptedKey.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,26 @@
66

77
use AndKom\Bitcoin\Wallet\Crypter;
88
use AndKom\Bitcoin\Wallet\Exception;
9-
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
109

1110
/**
1211
* Class EncryptedKey
1312
* @package AndKom\Bitcoin\Wallet\Item
1413
*/
1514
class EncryptedKey extends Key
1615
{
17-
/**
18-
* @var string
19-
*/
20-
protected $encrypted;
21-
22-
/**
23-
* EncryptedKey constructor.
24-
* @param string $public
25-
* @param string $encrypted
26-
* @param KeyMeta|null $meta
27-
*/
28-
public function __construct(string $public, string $encrypted, KeyMeta $meta = null)
29-
{
30-
$this->public = $public;
31-
$this->encrypted = $encrypted;
32-
$this->meta = $meta;
33-
}
34-
3516
/**
3617
* @return bool
3718
*/
3819
public function isEncrypted(): bool
3920
{
40-
return !$this->secret;
21+
return !$this->private;
4122
}
4223

4324
/**
44-
* @return PrivateKeyInterface
25+
* @return string
4526
* @throws Exception
4627
*/
47-
public function getPrivateKey(): PrivateKeyInterface
28+
public function getPrivateKey(): string
4829
{
4930
if ($this->isEncrypted()) {
5031
throw new Exception('Private key is encrypted.');
@@ -58,16 +39,15 @@ public function getPrivateKey(): PrivateKeyInterface
5839
*/
5940
public function getEncryptedPrivateKey(): string
6041
{
61-
return $this->encrypted;
42+
return $this->secret;
6243
}
6344

6445
/**
6546
* @return string
66-
* @throws Exception
6747
*/
6848
public function getIv(): string
6949
{
70-
$hash = Crypter::hash($this->getPublicKey()->getBinary());
50+
$hash = Crypter::hash($this->getPublicKey());
7151
$iv = substr($hash, 0, Crypter::WALLET_CRYPTO_IV_SIZE);
7252

7353
return $iv;
@@ -81,7 +61,7 @@ public function getIv(): string
8161
public function decrypt(string $masterKey): self
8262
{
8363
$crypter = new Crypter($masterKey, $this->getIv());
84-
$this->secret = $crypter->decrypt($this->getEncryptedPrivateKey());
64+
$this->private = $crypter->decrypt($this->getEncryptedPrivateKey());
8565

8666
return $this;
8767
}

src/Item/Key.php

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
namespace 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
}

src/Item/MasterKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getDerivationIterations(): int
7575
public function getHash(EncryptedKey $encryptedKey): string
7676
{
7777
$encrypted = bin2hex($encryptedKey->getEncryptedPrivateKey());
78-
$public = $encryptedKey->getPublicKey()->getHex();
78+
$public = bin2hex($encryptedKey->getPublicKey());
7979
$master = substr(bin2hex($this->getEncryptedKey()), -64); // last two aes blocks should be enough
8080
$salt = bin2hex($this->getSalt());
8181

tests/WalletTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public function testParse()
1818

1919
$key = \reset($keys);
2020

21-
$this->assertEquals($key->getPrivateKey()->toWif(), 'L1uaD1GSyvL78gRkBgMggLSUYkMrULFVGeS9wTxhLcBJN83HYRF3');
22-
$this->assertEquals($key->getPublicKey()->getPubKeyHash()->getHex(), '3f89ab613c2e51e0254513e1b3305dab0be0a8a4');
21+
$this->assertEquals(bin2hex($key->getPrivateKey()), '8bdb39c6f696408a4089a6b6d57007d58662f52bff0a609cdbca14baaa6c0563');
22+
$this->assertEquals(bin2hex($key->getPublicKey()), '02d96817786139a4958a2affd281cbc791eec18877fbc7534fdb488e1be5bcbfd6');
2323
}
2424

2525
public function testMasterKey()
@@ -47,8 +47,8 @@ public function testDecrypt()
4747
$keys = $wallet->getKeys();
4848
$key = \reset($keys);
4949

50-
$this->assertEquals($key->getPrivateKey()->toWif(), 'Kz1MJgnRAmUoeWq6gVwEmeCy1ykKPjNbDK9bcDbCUMipSLMKnwrm');
51-
$this->assertEquals($key->getPublicKey()->getPubKeyHash()->getHex(), 'ac1e83e60984cbd690d3b439f37cff88c38413e7');
50+
$this->assertEquals(bin2hex($key->getPrivateKey()), '53275c0a5ebc3c5510ad3ca5a0a25568fb0a57428091610455d8afba2edfba57');
51+
$this->assertEquals(bin2hex($key->getPublicKey()), '02c98bf925ccfa5e185380d90727358bffc1c7658feec95c30aab8afe20bdf428e');
5252
}
5353

5454
public function testVersion()

0 commit comments

Comments
 (0)