This repository was archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathPublicKey.php
148 lines (123 loc) · 3.27 KB
/
PublicKey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @license Copyright 2011-2014 BitPay Inc., MIT License
* see https://github.com/bitpay/php-bitpay-client/blob/master/LICENSE
*/
namespace Bitpay;
use Bitpay\Math\Math;
use Bitpay\Util\Secp256k1;
use Bitpay\Util\Util;
/**
* @package Bitcore
*/
class PublicKey extends Key
{
/**
* @var SinKey
*/
protected $sin;
/**
* @var PrivateKey
*/
protected $privateKey;
/**
* Returns the compressed public key value
*
* @return string
*/
public function __toString()
{
if (is_null($this->x)) {
return '';
}
if (Math::mod('0x'.$this->y, '0'.'x02') == '1') {
return sprintf('03%s', $this->x);
} else {
return sprintf('02%s', $this->x);
}
}
/**
* @param PrivateKey
*/
public static function createFromPrivateKey(PrivateKey $private)
{
$public = new self();
$public->setPrivateKey($private);
return $public;
}
/**
* @return KeyInterface
*/
public function setPrivateKey(PrivateKey $privateKey)
{
$this->privateKey = $privateKey;
return $this;
}
/**
* Generates an uncompressed and compressed EC public key.
*
* @param \Bitpay\PrivateKey $privateKey
*
* @return Bitpay\PublicKey
*/
public function generate(PrivateKey $privateKey = null)
{
if ($privateKey instanceof PrivateKey) {
$this->setPrivateKey($privateKey);
}
if (!empty($this->hex)) {
return $this;
}
if (is_null($this->privateKey)) {
throw new \Exception('Please `setPrivateKey` before you generate a public key');
}
if (!$this->privateKey->isGenerated()) {
$this->privateKey->generate();
}
if (!$this->privateKey->isValid()) {
throw new \Exception('Private Key is invalid and cannot be used to generate a public key');
}
$point = new Point(
'0x'.substr(Secp256k1::G, 2, 64),
'0x'.substr(Secp256k1::G, 66, 64)
);
$R = Util::doubleAndAdd(
'0x'.$this->privateKey->getHex(),
$point
);
$RxHex = Util::encodeHex($R->getX());
$RyHex = Util::encodeHex($R->getY());
$RxHex = str_pad($RxHex, 64, '0', STR_PAD_LEFT);
$RyHex = str_pad($RyHex, 64, '0', STR_PAD_LEFT);
$this->x = $RxHex;
$this->y = $RyHex;
$this->hex = sprintf('%s%s', $RxHex, $RyHex);
$this->dec = Util::decodeHex($this->hex);
return $this;
}
/**
* Checks to see if the public key is not blank and contains
* valid decimal and hex valules for this->hex & this->dec
*
* @return boolean
*/
public function isValid()
{
return ((!empty($this->hex) && ctype_xdigit($this->hex)) && (!empty($this->dec) && ctype_digit($this->dec)));
}
/**
* @return SinKey
*/
public function getSin()
{
if (empty($this->hex)) {
$this->generate();
}
if (null === $this->sin) {
$this->sin = new SinKey();
$this->sin->setPublicKey($this);
$this->sin->generate();
}
return $this->sin;
}
}