1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter; |
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key\PrivateKey; |
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PrivateKeySerializerInterface; |
9
|
|
|
use BitWasp\Buffertools\Buffer; |
10
|
|
|
use BitWasp\Buffertools\BufferInterface; |
11
|
|
|
use BitWasp\Buffertools\Parser; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Private Key Serializer - specific to secp256k1 |
15
|
|
|
*/ |
16
|
|
|
class PrivateKeySerializer implements PrivateKeySerializerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var EcAdapter |
20
|
|
|
*/ |
21
|
|
|
private $ecAdapter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param EcAdapter $ecAdapter |
25
|
|
|
*/ |
26
|
83 |
|
public function __construct(EcAdapter $ecAdapter) |
27
|
|
|
{ |
28
|
83 |
|
$this->ecAdapter = $ecAdapter; |
29
|
83 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param PrivateKey $privateKey |
33
|
|
|
* @return BufferInterface |
34
|
|
|
*/ |
35
|
68 |
|
private function doSerialize(PrivateKey $privateKey) |
36
|
|
|
{ |
37
|
68 |
|
return new Buffer($privateKey->getSecretBinary(), 32, $this->ecAdapter->getMath()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param PrivateKeyInterface $privateKey |
42
|
|
|
* @return BufferInterface |
43
|
|
|
*/ |
44
|
68 |
|
public function serialize(PrivateKeyInterface $privateKey) |
45
|
|
|
{ |
46
|
|
|
/** @var PrivateKey $privateKey */ |
47
|
68 |
|
return $this->doSerialize($privateKey); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Parser $parser |
52
|
|
|
* @return PrivateKey |
53
|
|
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
54
|
|
|
*/ |
55
|
40 |
|
public function fromParser(Parser $parser) |
56
|
|
|
{ |
57
|
40 |
|
return $this->ecAdapter->getPrivateKey($parser->readBytes(32)->getGmp()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \BitWasp\Buffertools\BufferInterface|string $data |
62
|
|
|
* @return PrivateKey |
63
|
|
|
*/ |
64
|
40 |
|
public function parse($data) |
65
|
|
|
{ |
66
|
40 |
|
return $this->fromParser(new Parser($data, $this->ecAdapter->getMath())); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|