-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAbstractTransaction.php
More file actions
149 lines (118 loc) · 4.28 KB
/
AbstractTransaction.php
File metadata and controls
149 lines (118 loc) · 4.28 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
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
<?php
declare(strict_types=1);
namespace ArkEcosystem\Crypto\Transactions\Types;
use ArkEcosystem\Crypto\Helpers;
use ArkEcosystem\Crypto\Identities\Address;
use ArkEcosystem\Crypto\Identities\PrivateKey;
use ArkEcosystem\Crypto\Identities\PublicKey;
use ArkEcosystem\Crypto\Transactions\Serializer;
use ArkEcosystem\Crypto\Utils\TransactionUtils;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\CompactSignature;
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\CompactSignatureInterface;
use BitWasp\Buffertools\Buffer;
use BitWasp\Buffertools\BufferInterface;
abstract class AbstractTransaction
{
public array $data;
public Buffer $serialized;
public function __construct(array $data)
{
$this->data = $data ?? [];
$this->refreshPayloadData();
}
abstract public function getPayload(): string;
public function refreshPayloadData(): static
{
$this->data['data'] = Helpers::removeLeadingHexZero($this->getPayload());
return $this;
}
/**
* Sign the transaction using the given passphrase.
*/
public function sign(PrivateKey $privateKey): static
{
$hash = $this->hash(skipSignature: true);
$signature = $privateKey->sign($hash);
// Extract the recovery ID (an integer between 0 and 3) from the signature
$recoveryId = $signature->getRecoveryId();
$this->data['v'] = $recoveryId;
$this->data['r'] = Helpers::gmpToHex($signature->getR());
$this->data['s'] = Helpers::gmpToHex($signature->getS());
return $this;
}
public function recoverSender(): void
{
$compactSignature = $this->getSignature();
$publicKey = $this->recoverPublicKey($compactSignature);
$this->data['senderPublicKey'] = $publicKey->publicKey;
$this->data['from'] = Address::fromPublicKey($this->data['senderPublicKey']);
}
public function verify(): bool
{
$compactSignature = $this->getSignature();
$publicKey = $this->recoverPublicKey($compactSignature);
return $publicKey->instance->verify($this->hash(skipSignature: true), $compactSignature);
}
public function hash(bool $skipSignature = false): BufferInterface
{
return TransactionUtils::toHash($this->data, $skipSignature);
}
public function serialize(bool $skipSignature = false): Buffer
{
return Serializer::new($this)->serialize($skipSignature);
}
/**
* Convert the transaction to its array representation.
*/
public function toArray(): array
{
return array_filter([
'gasPrice' => $this->data['gasPrice'],
'gasLimit' => $this->data['gasLimit'],
'hash' => $this->data['hash'],
'nonce' => $this->data['nonce'],
'senderPublicKey' => $this->data['senderPublicKey'],
'to' => $this->data['to'] ?? null,
'value' => $this->data['value'],
'data' => $this->data['data'],
'r' => $this->data['r'],
's' => $this->data['s'],
'v' => $this->data['v'],
], function ($element) {
if (null !== $element) {
return true;
}
return false;
});
}
/**
* Convert the transaction to its JSON representation.
*/
public function toJson(): string
{
return json_encode($this->toArray());
}
protected function recoverPublicKey(CompactSignatureInterface $compactSignature): PublicKey
{
return PublicKey::recover($this->hash(skipSignature: true), $compactSignature);
}
private function getSignature(): CompactSignatureInterface
{
$ecAdapter = EcAdapterFactory::getPhpEcc(
Bitcoin::getMath(),
Bitcoin::getGenerator()
);
$recoverId = $this->data['v'];
$r = gmp_init($this->data['r'], 16);
$s = gmp_init($this->data['s'], 16);
return new CompactSignature(
adapter: $ecAdapter,
r: $r,
s: $s,
recid: $recoverId,
compressed: true
);
}
}