1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 American Express Travel Related Services Company, Inc. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
15
|
|
|
* or implied. See the License for the specific language governing |
16
|
|
|
* permissions and limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace AmericanExpress\HyperledgerFabricClient\Transaction; |
22
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\Nonce\NonceGeneratorInterface; |
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\Serializer\AsciiCharStringSerializer; |
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\HashAlgorithm; |
26
|
|
|
use AmericanExpress\HyperledgerFabricClient\Serializer\SignedCharStringSerializer; |
27
|
|
|
use Hyperledger\Fabric\Protos\MSP\SerializedIdentity; |
28
|
|
|
|
29
|
|
|
final class TransactionIdentifierGenerator implements TransactionIdentifierGeneratorInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var NonceGeneratorInterface |
33
|
|
|
*/ |
34
|
|
|
private $nonceGenerator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var HashAlgorithm |
38
|
|
|
*/ |
39
|
|
|
private $hashAlgorithm; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var AsciiCharStringSerializer |
43
|
|
|
*/ |
44
|
|
|
private $asciiCharStringSerializer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var SignedCharStringSerializer |
48
|
|
|
*/ |
49
|
|
|
private $signedCharStringSerializer; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param NonceGeneratorInterface $nonceGenerator |
53
|
|
|
* @param HashAlgorithm $hashAlgorithm |
54
|
|
|
*/ |
55
|
1 |
|
public function __construct( |
56
|
|
|
NonceGeneratorInterface $nonceGenerator, |
57
|
|
|
HashAlgorithm $hashAlgorithm = null |
58
|
|
|
) { |
59
|
1 |
|
$this->nonceGenerator = $nonceGenerator; |
60
|
1 |
|
$this->hashAlgorithm = $hashAlgorithm ?: new HashAlgorithm(); |
61
|
1 |
|
$this->asciiCharStringSerializer = new AsciiCharStringSerializer(); |
62
|
1 |
|
$this->signedCharStringSerializer = new SignedCharStringSerializer(); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param SerializedIdentity $serializedIdentity |
67
|
|
|
* @return TransactionIdentifier |
68
|
|
|
*/ |
69
|
1 |
|
public function fromSerializedIdentity(SerializedIdentity $serializedIdentity): TransactionIdentifier |
70
|
|
|
{ |
71
|
1 |
|
$nonce = $this->nonceGenerator->generateNonce(); |
72
|
|
|
|
73
|
1 |
|
$noArray = $this->signedCharStringSerializer->deserialize($nonce); |
74
|
|
|
|
75
|
1 |
|
$identityArray = $this->signedCharStringSerializer->deserialize($serializedIdentity->serializeToString()); |
76
|
|
|
|
77
|
1 |
|
$comp = \array_merge($noArray, $identityArray); |
78
|
|
|
|
79
|
1 |
|
$compString = $this->asciiCharStringSerializer->serialize($comp); |
80
|
|
|
|
81
|
1 |
|
return new TransactionIdentifier( |
82
|
1 |
|
\hash((string) $this->hashAlgorithm, $compString), |
83
|
1 |
|
$nonce |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|