|
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 AmericanExpressTest\HyperledgerFabricClient\Chaincode; |
|
22
|
|
|
|
|
23
|
|
|
use AmericanExpress\HyperledgerFabricClient\Nonce\NonceGeneratorInterface; |
|
24
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeHeaderExtensionFactory; |
|
25
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeIdFactory; |
|
26
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChaincodeProposalPayloadFactory; |
|
27
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ChannelHeaderFactory; |
|
28
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\HeaderFactory; |
|
29
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\ProposalFactory; |
|
30
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\SerializedIdentityFactory; |
|
31
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\SignatureHeaderFactory; |
|
32
|
|
|
use AmericanExpress\HyperledgerFabricClient\ProtoFactory\TimestampFactory; |
|
33
|
|
|
use AmericanExpress\HyperledgerFabricClient\Transaction\TransactionIdentifierGenerator; |
|
34
|
|
|
use Hyperledger\Fabric\Protos\Peer\Proposal; |
|
35
|
|
|
use org\bovigo\vfs\vfsStream; |
|
36
|
|
|
use org\bovigo\vfs\vfsStreamFile; |
|
37
|
|
|
use PHPUnit\Framework\TestCase; |
|
38
|
|
|
|
|
39
|
|
|
abstract class AbstractChaincodeTest extends TestCase |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* @var vfsStreamFile |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $privateKey; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \SplFileObject |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $privateKeyFile; |
|
50
|
|
|
|
|
51
|
|
|
protected function setUp() |
|
52
|
|
|
{ |
|
53
|
|
|
$files = vfsStream::setup('test'); |
|
54
|
|
|
|
|
55
|
|
|
$this->privateKey = vfsStream::newFile('foo'); |
|
56
|
|
|
$this->privateKey->setContent(<<<'TAG' |
|
57
|
|
|
-----BEGIN PRIVATE KEY----- |
|
58
|
|
|
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQghnA7rdgbZi/wndus |
|
59
|
|
|
iXjyf0KgE6OKZjQ+5INjwelRAC6hRANCAASb3u+hY+U/FZvhYDN6d08HJ1v56UJU |
|
60
|
|
|
yz/n2NHyJgTg6kC05AaJMeGIinEF0JeJtRDNVQGzoQJQYjnzUTS9FvGh |
|
61
|
|
|
-----END PRIVATE KEY----- |
|
62
|
|
|
TAG |
|
63
|
|
|
); |
|
64
|
|
|
$files->addChild($this->privateKey); |
|
65
|
|
|
$this->privateKeyFile = new \SplFileObject($this->privateKey->url()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function createMockTransactionIdentifierGenerator(): TransactionIdentifierGenerator |
|
69
|
|
|
{ |
|
70
|
|
|
return new TransactionIdentifierGenerator( |
|
71
|
|
|
new class implements NonceGeneratorInterface { |
|
72
|
|
|
public function generateNonce(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return 'u23m5k4hf86j'; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function createChaincodeProposal(string $dateTime, \SplFileObject $privateKeyFile): Proposal |
|
81
|
|
|
{ |
|
82
|
|
|
$transactionContextFactory = $this->createMockTransactionIdentifierGenerator(); |
|
83
|
|
|
$identity = SerializedIdentityFactory::fromFile('1234', $privateKeyFile); |
|
84
|
|
|
$transactionContext = $transactionContextFactory->fromSerializedIdentity($identity); |
|
85
|
|
|
|
|
86
|
|
|
$channelHeader = ChannelHeaderFactory::create('MyChannelId'); |
|
87
|
|
|
$channelHeader->setTxId($transactionContext->getId()); |
|
88
|
|
|
$channelHeader->setEpoch(0); |
|
89
|
|
|
$channelHeader->setTimestamp(TimestampFactory::fromDateTime(new \DateTime($dateTime))); |
|
90
|
|
|
|
|
91
|
|
|
$chaincodeId = ChaincodeIdFactory::create( |
|
92
|
|
|
'MyChaincodePath', |
|
93
|
|
|
'MyChaincodeName', |
|
94
|
|
|
'MyChaincodeVersion' |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$chaincodeHeaderExtension = ChaincodeHeaderExtensionFactory::fromChaincodeId($chaincodeId); |
|
98
|
|
|
$channelHeader->setExtension($chaincodeHeaderExtension->serializeToString()); |
|
99
|
|
|
|
|
100
|
|
|
$header = HeaderFactory::create(SignatureHeaderFactory::create( |
|
101
|
|
|
$identity, |
|
102
|
|
|
$transactionContext->getNonce() |
|
103
|
|
|
), $channelHeader); |
|
104
|
|
|
|
|
105
|
|
|
$chaincodeProposalPayload = ChaincodeProposalPayloadFactory::fromChaincodeInvocationSpecArgs([]); |
|
106
|
|
|
return ProposalFactory::create($header, $chaincodeProposalPayload->serializeToString()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return array[] |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function loadStaticData(): array |
|
113
|
|
|
{ |
|
114
|
|
|
$contents = file_get_contents(__DIR__ . '/../../_files/signed-proposals.json'); |
|
115
|
|
|
|
|
116
|
|
|
$json = json_decode($contents, true); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
119
|
|
|
throw new \RuntimeException(json_last_error_msg(), json_last_error()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $json; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return array[] |
|
127
|
|
|
*/ |
|
128
|
|
View Code Duplication |
public function getChainCodeProposalDataset(): array |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
$data = $this->loadStaticData(); |
|
131
|
|
|
|
|
132
|
|
|
return array_map( |
|
133
|
|
|
function ($value) { |
|
134
|
|
|
return array_intersect_key( |
|
135
|
|
|
$value, |
|
136
|
|
|
array_flip(['dateTime', 'proposalHeader', 'proposalPayload', 'proposalExtension']) |
|
137
|
|
|
); |
|
138
|
|
|
}, |
|
139
|
|
|
$data |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return \SplFileObject |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getPrivateKeyFile(): \SplFileObject |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->privateKeyFile; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|