Completed
Pull Request — master (#364)
by thomas
143:17 queued 139:37
created

MessageSigner::sign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1.0054

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 18
ccs 14
cts 17
cp 0.8235
crap 1.0054
rs 9.4285
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());
0 ignored issues
show
Documentation introduced by
$address->getHash() is of type object<BitWasp\Buffertools\BufferInterface>, but the function expects a object<self>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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