/
index.php
153 lines (127 loc) · 4.58 KB
/
index.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
<?php
// Path to autoload.php created by composer.
require dirname(__FILE__).'/../vendor/autoload.php';
error_reporting(2);
// LND node IP and Port (lnd option "rpcport").
$lndIp = '127.0.0.1:10001';
// We need to set env variables to make ssl work:
putenv('GRPC_SSL_CIPHER_SUITES=ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256');
// Get tls and macaroon paths.
$tlsPath = '';
$macaroonPath = '';
$local_user = posix_getpwuid(posix_getuid());
switch (PHP_OS) {
case "Darwin":
$tlsPath = $local_user['dir'] . '/Library/Application Support/Lnd/tls.cert';
$macaroonPath = $local_user['dir'] . '/Library/Application Support/Lnd/macaroon.admin';
break;
case "Linux":
$tlsPath = $local_user['dir'] . '/.lnd/tls.cert';
$macaroonPath = $local_user['dir'] . '/.lnd/macaroon.admin';
}
if (! $sslCert = file_get_contents($tlsPath)) {
$certError = <<<EOT
tls.cert not found in "example" directory. Make sure to copy it from your
LND config directory.
MacOS: ~/Library/Application Support/Lnd/tls.cert
Linux: ~/.lnd/tls.cert
EOT;
throw new Exception($certError);
}
//// OVERWRITE MACAROON locally
$macaroonPath = "/Users/ndeet/go/dev/alice/test_data/admin.macaroon";
// Unlock the encrypted wallet.
$unlockWallet = new Lnrpc\WalletUnlockerClient($lndIp, [
'credentials' => Grpc\ChannelCredentials::createSsl($sslCert)
]);
if (is_null($unlockWallet)) {
throw new Exception('Could not connect or authenticate to LND node (unlockWallet).');
}
$unlockWalletRequest = new Lnrpc\UnlockWalletRequest();
$unlockWalletRequest->setWalletPassword("alicelight");
/** @var $reply Lnrpc\UnlockWalletResponse */
list($reply, $status) = $unlockWallet->UnlockWallet($unlockWalletRequest)->wait();
var_dump($reply);
$channel_credentials = Grpc\ChannelCredentials::createComposite(
Grpc\ChannelCredentials::createSsl($sslCert),
Grpc\CallCredentials::createFromPlugin('addMacaroon')
);
$opts = [
'credentials' => $channel_credentials
];
function addMacaroon($context) {
return [
'Grpc-Metadata-macaroon' => [file_get_contents("/Users/ndeet/go/dev/alice/test_data/admin.macaroon")]
];
}
// Create new client using SSL connection.
//$client = new Lnrpc\LightningClient($lndIp, $opts);
/*
// Create new client using SSL connection.
$client = new Lnrpc\LightningClient($lndIp, [
'credentials' => Grpc\ChannelCredentials::createSsl($sslCert),
'Grpc-Metadata-macaroon' => bin2hex(file_get_contents($macaroonPath))
]);
*/
try {
$client = new Lnrpc\LightningClient($lndIp, [
'credentials' => Grpc\ChannelCredentials::createSsl($sslCert),
'update_metadata' => addMacaroon('kasdkf')
]);
var_dump($client);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
if (is_null($client)) {
throw new Exception('Could not connect or authenticate to LND node.');
}
// As example we get the public key of the lnd node.
// See directory "src/Lnrpc" for available classes.
print('GetInfo:' . PHP_EOL);
try {
$getInfoRequest = new Lnrpc\GetInfoRequest();
/** @var $reply Lnrpc\GetInfoResponse */
list($reply, $status) = $client->GetInfo($getInfoRequest, addMacaroon('bla'))->wait();
print_r($reply->getIdentityPubkey());
print(PHP_EOL);
} catch (Exception $e) {
print($e->getMessage());
}
print('WalletBalance:' . PHP_EOL);
$walletBalanceRequest = new Lnrpc\WalletBalanceRequest();
/** @var $reply Lnrpc\WalletBalanceResponse */
list($reply, $status) = $client->WalletBalance($walletBalanceRequest)->wait();
print_r($reply->getTotalBalance());
print(PHP_EOL);
print('ChannelBalance:' . PHP_EOL);
$channelBalanceRequest = new Lnrpc\ChannelBalanceRequest();
/** @var $reply Lnrpc\ChannelBalanceResponse */
list($reply, $status) = $client->ChannelBalance($channelBalanceRequest)->wait();
print_r($reply->getBalance());
print(PHP_EOL);
print('GetTransactions:' . PHP_EOL);
/** @var $reply Lnrpc\TransactionDetails */
list($reply, $status) = $client->GetTransactions(new Lnrpc\GetTransactionsRequest())->wait();
$counter = 0;
foreach ($reply->getTransactions()->getIterator() as $key => $value) {
/** var $value Lnrpc\Transaction */
print_r($value->getAmount());
print("\n");
// Lets exit after 3 txs...
$counter++;
if ($counter >= 3) break;
}
print(PHP_EOL);
print('ListPeers:' . PHP_EOL);
/** @var $reply Lnrpc\ListPeersResponse */
list($reply, $status) = $client->ListPeers(new Lnrpc\ListPeersRequest())->wait();
$counter = 0;
foreach ($reply->getPeers()->getIterator() as $key => $value) {
/** var $value Lnrpc\Peer */
print_r($value->getPubKey());
print("\n");
// Lets exit after 3 txs...
$counter++;
if ($counter >= 3) break;
}
print(PHP_EOL);