1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the ngutech/lightning-interop project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace NGUtech\Lightning\Entity; |
10
|
|
|
|
11
|
|
|
use Daikon\Entity\Attribute; |
12
|
|
|
use Daikon\Entity\AttributeMap; |
13
|
|
|
use Daikon\Entity\Entity; |
14
|
|
|
use Daikon\Money\Entity\TransactionInterface; |
15
|
|
|
use Daikon\ValueObject\Natural; |
16
|
|
|
use Daikon\ValueObject\Text; |
17
|
|
|
use Daikon\ValueObject\Timestamp; |
18
|
|
|
use NGUtech\Bitcoin\Service\SatoshiCurrencies; |
19
|
|
|
use NGUtech\Bitcoin\ValueObject\Hash; |
20
|
|
|
use NGUtech\Bitcoin\ValueObject\Bitcoin; |
21
|
|
|
use NGUtech\Lightning\ValueObject\InvoiceState; |
22
|
|
|
use NGUtech\Lightning\ValueObject\Request; |
23
|
|
|
|
24
|
|
|
final class LightningInvoice extends Entity implements TransactionInterface |
25
|
|
|
{ |
26
|
|
|
public const AMOUNT_MIN = '1'.SatoshiCurrencies::MSAT; |
27
|
|
|
public const AMOUNT_MAX = '4294967295'.SatoshiCurrencies::MSAT; |
28
|
|
|
|
29
|
|
|
public static function getAttributeMap(): AttributeMap |
30
|
|
|
{ |
31
|
|
|
return new AttributeMap([ |
32
|
|
|
Attribute::define('preimage', Hash::class), |
33
|
|
|
Attribute::define('preimageHash', Hash::class), |
34
|
|
|
Attribute::define('request', Request::class), |
35
|
|
|
Attribute::define('destination', Text::class), |
36
|
|
|
Attribute::define('amount', Bitcoin::class), |
37
|
|
|
Attribute::define('amountPaid', Bitcoin::class), |
38
|
|
|
Attribute::define('label', Text::class), |
39
|
|
|
Attribute::define('description', Text::class), |
40
|
|
|
Attribute::define('expiry', Natural::class), |
41
|
|
|
Attribute::define('cltvExpiry', Natural::class), |
42
|
|
|
Attribute::define('blockHeight', Natural::class), |
43
|
|
|
Attribute::define('state', InvoiceState::class), |
44
|
|
|
Attribute::define('createdAt', Timestamp::class), |
45
|
|
|
Attribute::define('settledAt', Timestamp::class), |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getIdentity(): Hash |
50
|
|
|
{ |
51
|
|
|
return $this->getPreimageHash(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getPreimage(): Hash |
55
|
|
|
{ |
56
|
|
|
return $this->get('preimage') ?? Hash::makeEmpty(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getPreimageHash(): Hash |
60
|
|
|
{ |
61
|
|
|
return $this->get('preimageHash') ?? Hash::makeEmpty(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getRequest(): Request |
65
|
|
|
{ |
66
|
|
|
return $this->get('request') ?? Request::makeEmpty(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getDestination(): Text |
70
|
|
|
{ |
71
|
|
|
return $this->get('destination') ?? Text::makeEmpty(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getAmount(): Bitcoin |
75
|
|
|
{ |
76
|
|
|
return $this->get('amount') ?? Bitcoin::makeEmpty(); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getAmountPaid(): Bitcoin |
80
|
|
|
{ |
81
|
|
|
return $this->get('amountPaid') ?? Bitcoin::makeEmpty(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getLabel(): Text |
85
|
|
|
{ |
86
|
|
|
return $this->get('label') ?? Text::makeEmpty(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getDescription(): Text |
90
|
|
|
{ |
91
|
|
|
return $this->get('description') ?? Text::makeEmpty(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getExpiry(): Natural |
95
|
|
|
{ |
96
|
|
|
return $this->get('expiry') ?? Natural::fromNative(86400); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getCltvExpiry(): Natural |
100
|
|
|
{ |
101
|
|
|
return $this->get('cltvExpiry') ?? Natural::fromNative(10); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getBlockHeight(): Natural |
105
|
|
|
{ |
106
|
|
|
return $this->get('blockHeight') ?? Natural::makeEmpty(); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getExpiryHeight(): Natural |
110
|
|
|
{ |
111
|
|
|
//@todo handle error cases |
112
|
|
|
return $this->getBlockHeight()->add($this->getCltvExpiry()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getState(): InvoiceState |
116
|
|
|
{ |
117
|
|
|
return $this->get('state') ?? InvoiceState::makeEmpty(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getCreatedAt(): Timestamp |
121
|
|
|
{ |
122
|
|
|
return $this->get('createdAt') ?? Timestamp::makeEmpty(); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getExpiresAt(): Timestamp |
126
|
|
|
{ |
127
|
|
|
return $this->getCreatedAt()->modify("+{$this->getExpiry()} seconds"); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function hasExpired(Timestamp $time): bool |
131
|
|
|
{ |
132
|
|
|
return $this->getExpiresAt()->isBefore($time); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getSettledAt(): Timestamp |
136
|
|
|
{ |
137
|
|
|
return $this->get('settledAt') ?? Timestamp::makeEmpty(); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|