-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAbstractTransactionBuilder.php
More file actions
94 lines (70 loc) · 2.18 KB
/
AbstractTransactionBuilder.php
File metadata and controls
94 lines (70 loc) · 2.18 KB
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
<?php
declare(strict_types=1);
namespace ArkEcosystem\Crypto\Transactions\Builder;
use ArkEcosystem\Crypto\Identities\PrivateKey;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use Brick\Math\BigDecimal;
abstract class AbstractTransactionBuilder
{
public AbstractTransaction $transaction;
public function __construct(?array $data = null)
{
$this->transaction = $this->getTransactionInstance($data ?? [
'value' => BigDecimal::zero(),
'senderPublicKey' => '',
'gasPrice' => '5',
'gasLimit' => 1_000_000,
'nonce' => '1',
'data' => '',
]);
}
public function __toString(): string
{
return $this->toJson();
}
public static function new(?array $data = null): static
{
return new static($data);
}
public function gasLimit(BigDecimal $gasLimit): static
{
$this->transaction->data['gasLimit'] = $gasLimit;
return $this;
}
public function to(string $to): static
{
$this->transaction->data['to'] = $to;
return $this;
}
public function gasPrice(BigDecimal $gasPrice): static
{
$this->transaction->data['gasPrice'] = $gasPrice;
return $this;
}
public function nonce(string $nonce): static
{
$this->transaction->data['nonce'] = $nonce;
return $this;
}
public function sign(string $passphrase): static
{
$privateKey = PrivateKey::fromPassphrase($passphrase);
$this->transaction->data['senderPublicKey'] = $privateKey->publicKey;
$this->transaction = $this->transaction->sign($privateKey);
$this->transaction->data['hash'] = $this->transaction->hash()->getHex();
return $this;
}
public function verify(): bool
{
return $this->transaction->verify();
}
public function toArray(): array
{
return $this->transaction->toArray();
}
public function toJson(): string
{
return $this->transaction->toJson();
}
abstract protected function getTransactionInstance(array $data): AbstractTransaction;
}