-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMessage.php
More file actions
187 lines (158 loc) · 4.41 KB
/
Message.php
File metadata and controls
187 lines (158 loc) · 4.41 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
declare(strict_types=1);
namespace ArkEcosystem\Crypto\Utils;
use ArkEcosystem\Crypto\Helpers;
use ArkEcosystem\Crypto\Identities\PrivateKey;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\CompactSignature;
use BitWasp\Bitcoin\Key\Factory\PublicKeyFactory;
use BitWasp\Buffertools\Buffer;
use InvalidArgumentException;
use kornrunner\Keccak;
class Message
{
public const MESSAGE_PREFIX = "\x19Ethereum Signed Message:\n";
/**
* The message signer public key.
*
* @var string
*/
public $publicKey;
/**
* The message signature.
*
* @var string
*/
public $signature;
/**
* The message contents.
*
* @var string
*/
public $message;
/**
* Create a new message instance.
*
* @param object $message
*/
public function __construct(object $message)
{
if (! property_exists($message, 'publicKey')) {
throw new InvalidArgumentException('The given message did not contain a valid public key.');
}
$this->publicKey = $message->publicKey;
$this->signature = $message->signature;
$this->message = $message->message;
}
/**
* Convert the message to its JSON representation.
*
* @return string
*/
public function __toString(): string
{
return $this->toJson();
}
/**
* Create a new message instance.
*
* @param mixed $message
*
* @return Message
*/
public static function new($message): self
{
if (is_object($message)) {
return new static($message);
}
if (is_array($message)) {
return new static(json_decode(json_encode($message)));
}
if (is_string($message)) {
return new static(json_decode($message));
}
throw new InvalidArgumentException('The given message was neither an object, array nor JSON.');
}
/**
* Sign a message using the given passphrase.
*
* @param string $message
* @param string $passphrase
*
* @return Message
*/
public static function sign(string $message, string $passphrase): self
{
$privateKey = PrivateKey::fromPassphrase($passphrase);
$hash = Keccak::hash(static::MESSAGE_PREFIX.strlen($message).$message, 256);
$signature = $privateKey->sign(Buffer::hex($hash));
$r = Helpers::gmpToHex($signature->getR());
$s = Helpers::gmpToHex($signature->getS());
$v = str_pad(dechex($signature->getRecoveryId() + 27), 2, '0', STR_PAD_LEFT);
return static::new([
'publicKey' => $privateKey->publicKey,
'signature' => $r.$s.$v,
'message' => $message,
]);
}
/**
* Verify the message contents.
*
* @return bool
*/
public function verify(): bool
{
$factory = new PublicKeyFactory();
$signature = $this->getSignature();
$message = static::MESSAGE_PREFIX.strlen($this->message).$this->message;
return $factory
->fromHex($this->publicKey)
->verify(
Buffer::hex(Keccak::hash($message, 256)),
$signature,
);
}
/**
* Convert the message to its array representation.
*
* @return array
*/
public function toArray(): array
{
return [
'publicKey' => $this->publicKey,
'signature' => $this->signature,
'message' => $this->message,
];
}
/**
* Convert the message to its JSON representation.
*
* @return string
*/
public function toJson(): string
{
return json_encode($this->toArray());
}
private function getSignature(): CompactSignature
{
$r = substr($this->signature, 0, 64);
$s = substr($this->signature, 64, 64);
$v = hexdec(substr($this->signature, 128, 2));
$ecAdapter = EcAdapterFactory::getPhpEcc(
Bitcoin::getMath(),
Bitcoin::getGenerator()
);
$recoverId = $v - 27;
$r = gmp_init($r, 16);
$s = gmp_init($s, 16);
return new CompactSignature(
adapter: $ecAdapter,
r: $r,
s: $s,
recid: $recoverId,
compressed: true
);
}
}