|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\MessageSigner; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress; |
|
6
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
|
8
|
|
|
use BitWasp\Bitcoin\Crypto\Hash; |
|
9
|
|
|
use BitWasp\Bitcoin\Crypto\Random\Rfc6979; |
|
10
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
|
11
|
|
|
use BitWasp\Buffertools\Buffer; |
|
12
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
13
|
|
|
use BitWasp\Buffertools\Buffertools; |
|
14
|
|
|
|
|
15
|
|
|
class MessageSigner |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EcAdapterInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $ecAdapter; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param EcAdapterInterface $ecAdapter |
|
24
|
|
|
*/ |
|
25
|
6 |
|
public function __construct(EcAdapterInterface $ecAdapter = null) |
|
26
|
|
|
{ |
|
27
|
6 |
|
$this->ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
|
28
|
6 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $message |
|
32
|
|
|
* @return BufferInterface |
|
33
|
|
|
* @throws \Exception |
|
34
|
|
|
*/ |
|
35
|
6 |
|
private function calculateBody($message) |
|
36
|
|
|
{ |
|
37
|
6 |
|
return new Buffer("\x18Bitcoin Signed Message:\n" . Buffertools::numToVarInt(strlen($message))->getBinary() . $message, null, $this->ecAdapter->getMath()); |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $message |
|
42
|
|
|
* @return \BitWasp\Buffertools\BufferInterface |
|
43
|
|
|
*/ |
|
44
|
6 |
|
public function calculateMessageHash($message) |
|
45
|
|
|
{ |
|
46
|
6 |
|
return Hash::sha256d($this->calculateBody($message)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param SignedMessage $signedMessage |
|
51
|
|
|
* @param PayToPubKeyHashAddress $address |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
6 |
|
public function verify(SignedMessage $signedMessage, PayToPubKeyHashAddress $address) |
|
55
|
|
|
{ |
|
56
|
6 |
|
$hash = $this->calculateMessageHash($signedMessage->getMessage()); |
|
57
|
|
|
|
|
58
|
6 |
|
$publicKey = $this->ecAdapter->recover( |
|
59
|
6 |
|
$hash, |
|
60
|
6 |
|
$signedMessage->getCompactSignature() |
|
61
|
6 |
|
); |
|
62
|
|
|
|
|
63
|
6 |
|
return $publicKey->getAddress()->getHash()->equals($address->getHash()); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $message |
|
68
|
|
|
* @param PrivateKeyInterface $privateKey |
|
69
|
|
|
* @return SignedMessage |
|
70
|
|
|
*/ |
|
71
|
5 |
|
public function sign($message, PrivateKeyInterface $privateKey) |
|
72
|
|
|
{ |
|
73
|
5 |
|
$hash = $this->calculateMessageHash($message); |
|
74
|
|
|
|
|
75
|
5 |
|
return new SignedMessage( |
|
76
|
5 |
|
$message, |
|
77
|
5 |
|
$this->ecAdapter->signCompact( |
|
78
|
5 |
|
$hash, |
|
79
|
5 |
|
$privateKey, |
|
80
|
5 |
|
new Rfc6979( |
|
81
|
5 |
|
$this->ecAdapter, |
|
82
|
5 |
|
$privateKey, |
|
83
|
5 |
|
$hash, |
|
84
|
|
|
'sha256' |
|
85
|
5 |
|
) |
|
86
|
5 |
|
) |
|
87
|
5 |
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: