|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ngutech/lnd-adapter 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\Lnd\Service; |
|
10
|
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
|
12
|
|
|
use Daikon\Money\Exception\PaymentServiceFailed; |
|
13
|
|
|
use Invoicesrpc\AddHoldInvoiceRequest; |
|
14
|
|
|
use Invoicesrpc\AddHoldInvoiceResp; |
|
15
|
|
|
use Invoicesrpc\CancelInvoiceMsg; |
|
16
|
|
|
use Invoicesrpc\CancelInvoiceResp; |
|
17
|
|
|
use Invoicesrpc\SettleInvoiceMsg; |
|
18
|
|
|
use Invoicesrpc\SettleInvoiceResp; |
|
19
|
|
|
use NGUtech\Bitcoin\ValueObject\Hash; |
|
20
|
|
|
use NGUtech\Lightning\Entity\LightningInvoice; |
|
21
|
|
|
use NGUtech\Lightning\Service\LightningHoldServiceInterface; |
|
22
|
|
|
use NGUtech\Lnd\Connector\LndGrpcClient; |
|
23
|
|
|
|
|
24
|
|
|
final class LndHoldService extends LndService implements LightningHoldServiceInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public function request(LightningInvoice $invoice): LightningInvoice |
|
27
|
|
|
{ |
|
28
|
|
|
Assertion::true($this->canRequest($invoice->getAmount()), 'Lnd hold service cannot request given amount.'); |
|
29
|
|
|
|
|
30
|
|
|
$expiry = $invoice->getExpiry()->toNative(); |
|
31
|
|
|
Assertion::between($expiry, 60, 31536000, 'Invoice expiry is not acceptable.'); |
|
32
|
|
|
|
|
33
|
|
|
$preimageHash = Hash::sum($invoice->getPreimage()->toBinary()); |
|
34
|
|
|
|
|
35
|
|
|
/** @var LndGrpcClient $client */ |
|
36
|
|
|
$client = $this->connector->getConnection(); |
|
37
|
|
|
|
|
38
|
|
|
/** @var AddHoldInvoiceResp $response */ |
|
39
|
|
|
list($response, $status) = $client->invoicesrpc->AddHoldInvoice(new AddHoldInvoiceRequest([ |
|
40
|
|
|
'hash' => $preimageHash->toBinary(), |
|
41
|
|
|
'memo' => (string)$invoice->getLabel(), |
|
42
|
|
|
'value_msat' => $invoice->getAmount()->getAmount(), |
|
43
|
|
|
'expiry' => $expiry = min($invoice->getExpiry()->toNative(), 31536000), |
|
44
|
|
|
'cltv_expiry' => $invoice->getCltvExpiry()->toNative() |
|
45
|
|
|
]))->wait(); |
|
46
|
|
|
|
|
47
|
|
|
if ($status->code !== 0) { |
|
48
|
|
|
$this->logger->error($status->details); |
|
49
|
|
|
throw new PaymentServiceFailed($status->details); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $invoice->withValues([ |
|
53
|
|
|
'preimageHash' => $preimageHash, |
|
54
|
|
|
'request' => $response->getPaymentRequest(), |
|
55
|
|
|
'expiry' => $expiry, |
|
56
|
|
|
'blockHeight' => $this->getInfo()['blockHeight'] |
|
57
|
|
|
]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function settle(LightningInvoice $invoice): bool |
|
61
|
|
|
{ |
|
62
|
|
|
/** @var LndGrpcClient $client */ |
|
63
|
|
|
$client = $this->connector->getConnection(); |
|
64
|
|
|
|
|
65
|
|
|
/** @var SettleInvoiceResp $response */ |
|
66
|
|
|
list($response, $status) = $client->invoicesrpc->SettleInvoice(new SettleInvoiceMsg([ |
|
67
|
|
|
'preimage' => $invoice->getPreimage()->toBinary() |
|
68
|
|
|
]))->wait(); |
|
69
|
|
|
|
|
70
|
|
|
return $status->code === 0; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function cancel(LightningInvoice $invoice): bool |
|
74
|
|
|
{ |
|
75
|
|
|
/** @var LndGrpcClient $client */ |
|
76
|
|
|
$client = $this->connector->getConnection(); |
|
77
|
|
|
|
|
78
|
|
|
/** @var CancelInvoiceResp $response */ |
|
79
|
|
|
list($response, $status) = $client->invoicesrpc->CancelInvoice(new CancelInvoiceMsg([ |
|
80
|
|
|
'payment_hash' => $invoice->getPreimageHash()->toBinary() |
|
81
|
|
|
]))->wait(); |
|
82
|
|
|
|
|
83
|
|
|
return $status->code === 0; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|