Completed
Push — 0.0.34 ( 61212b...2e9020 )
by thomas
20:55
created

ElectrumKey   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMasterPublicKey() 0 4 1
A getMPK() 0 4 1
A getMasterPrivateKey() 0 8 2
A getSequenceOffset() 0 5 2
A deriveChild() 0 5 2
A __construct() 0 15 4
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\Deterministic;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
9
use BitWasp\Bitcoin\Crypto\Hash;
10
use BitWasp\Buffertools\Buffer;
11
12
class ElectrumKey
13
{
14
    /**
15
     * @var EcAdapterInterface
16
     */
17
    private $ecAdapter;
18
19
    /**
20
     * @var null|PrivateKeyInterface
21
     */
22
    private $masterPrivate;
23
24
    /**
25
     * @var PublicKeyInterface
26
     */
27
    private $masterPublic;
28
29
    /**
30
     * @param EcAdapterInterface $ecAdapter
31
     * @param KeyInterface $masterKey
32
     */
33 8
    public function __construct(EcAdapterInterface $ecAdapter, KeyInterface $masterKey)
34
    {
35 8
        if ($masterKey->isCompressed()) {
36 2
            throw new \RuntimeException('Electrum keys are not compressed');
37
        }
38
39 8
        if ($masterKey instanceof PrivateKeyInterface) {
40 6
            $this->masterPrivate = $masterKey;
41 6
            $this->masterPublic = $masterKey->getPublicKey();
42 4
        } elseif ($masterKey instanceof PublicKeyInterface) {
43 4
            $this->masterPublic = $masterKey;
44
        }
45
46 8
        $this->ecAdapter = $ecAdapter;
47 8
    }
48
49
    /**
50
     * @return PrivateKeyInterface
51
     */
52 4
    public function getMasterPrivateKey()
53
    {
54 4
        if (null === $this->masterPrivate) {
55 2
            throw new \RuntimeException("Cannot produce master private key from master public key");
56
        }
57
58 2
        return $this->masterPrivate;
59
    }
60
61
    /**
62
     * @return PublicKeyInterface
63
     */
64 2
    public function getMasterPublicKey()
65
    {
66 2
        return $this->masterPublic;
67
    }
68
69
    /**
70
     * @return Buffer
71
     */
72 2
    public function getMPK()
73
    {
74 2
        return $this->getMasterPublicKey()->getBuffer()->slice(1);
75
    }
76
77
    /**
78
     * @param int $sequence
79
     * @param bool $change
80
     * @return \GMP
81
     */
82 2
    public function getSequenceOffset($sequence, $change = false)
83
    {
84 2
        $seed = new Buffer(sprintf("%s:%s:%s", $sequence, $change ? '1' : '0', $this->getMPK()->getBinary()), null, $this->ecAdapter->getMath());
85 2
        return Hash::sha256d($seed)->getGmp();
86
    }
87
88
    /**
89
     * @param int $sequence
90
     * @param bool $change
91
     * @return PrivateKeyInterface|PublicKeyInterface
92
     */
93 2
    public function deriveChild($sequence, $change = false)
94
    {
95 2
        $key = is_null($this->masterPrivate) ? $this->masterPublic : $this->masterPrivate;
96 2
        return $key->tweakAdd($this->getSequenceOffset($sequence, $change));
97
    }
98
}
99