-
Notifications
You must be signed in to change notification settings - Fork 32
/
Transaction.php
307 lines (265 loc) · 7.46 KB
/
Transaction.php
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php declare(strict_types=1);
namespace FOXRP\Rippled\Api;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Process\Process;
use FOXRP\Rippled\Client;
use FOXRP\Rippled\Exception\TransactionException;
use FOXRP\Rippled\Exception\TransactionSignException;
use FOXRP\Rippled\Exception\TransactionTypeException;
class Transaction
{
/** @var Client */
private $client;
/** @var string */
private $json;
/** @var bool */
private $signed;
/** @var FieldableInterface */
private $type;
/** @var array */
private $tx;
/** @var string */
private $txBlob;
/** @var string */
private $txId;
/**
* Transaction constructor.
* @param array|string $tx A transaction represented by JSON or an associative array.
* @param Client|null $client
* @throws TransactionException
* @throws TransactionTypeException
*/
public function __construct($tx, Client $client = null)
{
// Dynamically set tx & json based on type.
if (\is_array($tx)) {
$this->setTx($tx);
} elseif (\is_string($tx)) {
$this->setJson($tx);
} else {
throw new TransactionException('Invalid tx passed into the constructor; Must be a string or array');
}
if ($client !== null) {
$this->setClient($client);
}
}
/**
* @param string $type
* @return string
* @throws TransactionTypeException
*/
public function findClass(string $type): string
{
$class = '\\FOXRP\Rippled\\Api\\TransactionType\\' . $type;
if (!class_exists($class)) {
throw new TransactionTypeException(sprintf('No class found for transaction type %s', $type));
}
return $class;
}
/**
* @param string $secret
* @throws TransactionSignException
* @throws TransactionTypeException
* @throws TransactionException
*/
public function signLocal(string $secret): void
{
// Auto-fillable fields are required before signing.
$tx = $this->getTx();
// Set sequence if it does not exist.
if (!isset($tx['Sequence']) || $tx['Sequence'] < 1) {
$tx['Sequence'] = $this->getAccountSequence($tx['Account']);
$this->setTx($tx);
}
$process = new Process(['xrpsign', $this->getJson(), $secret]);
try {
$process->run();
if (!$process->isSuccessful()) {
$response['stderr'] = trim($process->getErrorOutput());
throw new TransactionSignException(sprintf('Unable to sign transaction: %s', $process->getErrorOutput()));
}
$json = trim($process->getOutput());
$data = json_decode($json, true);
if (json_last_error()!== JSON_ERROR_NONE) {
throw new TransactionSignException('Unable to parse output of xrpsign command');
}
$this->setTx($data['tx']);
$this->setTxBlob($data['tx_blob']);
$this->setTxId($data['tx_id']);
$this->setSigned(true);
} catch (\Exception $e) {
throw new TransactionSignException('Unable to sign transaction: ' . $e->getMessage());
}
}
/**
* Retrieves the current account sequence.
*
* @param string $account Account id.
* @return int Account sequence.
* @throws TransactionSignException
*/
public function getAccountSequence(string $account): int
{
$client = $this->getClient();
if ($client === null) {
throw new TransactionSignException('Client must be present if Sequence paramter does not exist');
}
$params = [
'account' => $account,
];
$res = $client->send('account_info', $params);
return $res->getResult()['account_data']['Sequence'];
}
/**
* Submit this transaction, signed or unsigned.
*
* @param string $secret Regular key.
* @param bool $signLocal Sign locally.
* @return Response|null
* @throws TransactionException
* @throws \Exception
* @throws \Http\Client\Exception
*/
public function submit(string $secret, bool $signLocal = true)
{
if ($this->getClient() === null) {
throw new TransactionException('Transaction must have a Client to submit');
}
$res = null;
if ($signLocal) {
$this->signLocal($secret);
$txBlob = $this->getTxBlob();
if ($txBlob === null) {
throw new TransactionException('Local sign was unsuccessful.');
}
// Submit signed transaction.
$res = $this->getClient()->send('submit', [
'tx_blob' => $txBlob
]);
} else {
// Submit unsigned transaction with secret.
$res = $this->getClient()->send('submit', [
'tx_json' => $this->getTx(),
'secret' => $secret
]);
}
return $res;
}
/**
* @return string
*/
public function getJson(): string
{
return $this->json;
}
/**
* @param string $json
* @throws TransactionException
* @throws TransactionTypeException
*/
public function setJson(string $json): void
{
$tx = json_decode($json, true);
if (json_last_error()!== JSON_ERROR_NONE) {
throw new TransactionException('Invalid JSON passed into the constructor');
}
$this->json = $json;
$this->tx = $tx;
$this->setType();
}
/**
* @return bool
*/
public function isSigned(): bool
{
return $this->signed;
}
/**
* @param bool $signed
*/
public function setSigned(bool $signed): void
{
$this->signed = $signed;
}
/**
* @return array
*/
public function getTx(): array
{
return $this->tx;
}
/**
* @param array $tx
* @throws TransactionException
* @throws TransactionTypeException
*/
public function setTx(array $tx): void
{
$this->tx = $tx;
$this->json = json_encode($tx);
$this->setType();
}
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @throws TransactionException
* @throws TransactionTypeException
*/
public function setType(): void
{
$tx = $this->getTx();
if (!isset($tx['TransactionType'])) {
throw new TransactionException('TransactionType is required');
}
$txType = $tx['TransactionType'];
$class = $this->findClass($txType);
$this->type = new $class($tx);
}
/**
* @return string
*/
public function getTxBlob(): ?string
{
return $this->txBlob;
}
/**
* @param string $txBlob
*/
public function setTxBlob(string $txBlob = null): void
{
$this->txBlob = $txBlob;
}
/**
* @return string
*/
public function getTxId(): ?string
{
return $this->txId;
}
/**
* @param string $txId
*/
public function setTxId(string $txId = null): void
{
$this->txId = $txId;
}
/**
* @return Client
*/
public function getClient(): ?Client
{
return $this->client;
}
/**
* @param Client $client
*/
public function setClient(Client $client = null): void
{
$this->client = $client;
}
}