Skip to content

Commit

Permalink
fixup grpc-plugin-hold README
Browse files Browse the repository at this point in the history
  • Loading branch information
daywalker90 committed Jun 21, 2023
1 parent 2fb6721 commit b115112
Showing 1 changed file with 21 additions and 36 deletions.
57 changes: 21 additions & 36 deletions plugins/grpc-plugin-hold/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# GRPC hold-plugin for Core Lightning

This plugin is much like cln-grpc, but only has the Hold-Invoice related methods.

This plugin exposes the Hold-Invoice related JSON-RPC interface through grpc over the
network. It listens on a configurable port, authenticates clients
using mTLS certificates, and will forward any Hold-Invoice related request to the JSON-RPC
Expand All @@ -10,7 +12,7 @@ interface, performing translations from protobuf to JSON and back.

The plugin only runs when `lightningd` is configured with the option
`--grpc-hold-port`. Upon starting the plugin generates a number of files,
if they don't already exist:
if they don't already exist (uses the same ones cln-grpc uses):

- `ca.pem` and `ca-key.pem`: These are the certificate and private
key for your own certificate authority. The plugin will only accept
Expand Down Expand Up @@ -49,51 +51,36 @@ Next we generate the bindings in the current directory:

```bash
python -m grpc_tools.protoc \
-I path/to/cln-grpc/proto \
path/to/cln-grpc/proto/node.proto \
--python_out=. \
--grpc_python_out=. \
--experimental_allow_proto3_optional
```
```bash
python -m grpc_tools.protoc \
-I path/to/cln-grpc/proto \
path/to/cln-grpc/proto/hold.proto \
-I path/to/cln-grpc-hold/proto \
path/to/cln-grpc-hold/proto/hold.proto \
--python_out=. \
--grpc_python_out=. \
--experimental_allow_proto3_optional
```
```bash
python -m grpc_tools.protoc \
-I path/to/cln-grpc/proto \
path/to/cln-grpc/proto/primitives.proto \
-I path/to/cln-grpc-hold/proto \
path/to/cln-grpc-hold/proto/primitives.proto \
--python_out=. \
--grpc_python_out=. \
--experimental_allow_proto3_optional
```

This will generate 6 files in the current directory:
This will generate 3 relevant files in the current directory:

- `node_pb2.py`: the description of the protobuf messages we'll be
exchanging with the server.
- `node_pb2_grpc.py`: the service and method stubs representing the
server-side methods as local objects and associated methods.
- `hold_pb2.py`: the description of the hold-invoice related protobuf messages we'll be
exchanging with the server.
- `hold_pb2_grpc.py`: the service and method stubs representing the
server-side hold-invoice related methods as local objects and associated methods.
- `primitives_pb2.py`: the description of the primitives protobuf messages we'll be
exchanging with the server.
- `primitives_pb2_grpc.py`: the service and method stubs representing the
server-side primitives methods as local objects and associated methods.


And finally we can use the generated stubs and mTLS identity to
connect to the node:

```python
from pathlib import Path
from node_pb2_grpc import NodeStub
import node_pb2
from hold_pb2_grpc import HoldStub
import hold_pb2
import primitives_pb2
Expand All @@ -109,23 +96,22 @@ creds = grpc.ssl_channel_credentials(
certificate_chain=cert_path.open('rb').read()
)

channel = grpc.secure_channel(
f"localhost:{grpc_port}",
creds,
options=(('grpc.ssl_target_name_override', 'cln'),)
)
channel = grpc.secure_channel(
f"localhost:{grpc_hold_port}",
creds,
options=(('grpc.ssl_target_name_override', 'cln'),)
)
holdstub = HoldStub(channel)

request = node_pb2.InvoiceRequest(amount_msat=primitives_pb2.AmountOrAny(amount=primitives_pb2.Amount(msat=10_000)), description="test", label="test", cltv=500)
request = hold_pb2.HoldInvoiceRequest(amount_msat=primitives_pb2.AmountOrAny(amount=primitives_pb2.Amount(msat=10_000)), description="test", label="test", cltv=500)
response = holdstub.HoldInvoice(request)

print(response)

print(holdstub.HoldInvoice(request))
request2 = hold_pb2.HoldInvoiceLookupRequest(payment_hash=response.payment_hash)
response2 = holdstub.HoldInvoiceLookup(request)

print(stub.Getinfo(node_pb2.GetinfoRequest()))
print(response2)
```

In this example we first local the client identity, as well as the CA
Expand All @@ -138,11 +124,10 @@ required because the plugin does not know the domain under which it
will be reachable, and will therefore use `cln` as a standin. See
custom certificate generation for how this could be changed.

We then use the channel to instantiate the `NodeStub` representing the
normal cln service and its methods aswell as the `HoldStub` representing
the hold service and its methods. Then we create an `InvoiceRequest` and call
`HoldInvoice` with it. Finally we call the `Getinfo` method
with default arguments.
We then use the channel to instantiate the `HoldStub` representing
the hold service and its methods. Then we create a `HoldInvoiceRequest` and call
`HoldInvoice` with it. Finally we create a `HoldInvoiceLookupRequest` and call
`HoldInvoiceLookup` with it to check the current state of our Hold-Invoice.

## Generating custom certificates

Expand Down Expand Up @@ -179,4 +164,4 @@ allowing you to access the node through its real domain name. You can
now move `server.pem` and `server-key.pem` into the lightning
directory, and they should be picked up during the start.

[proto]: https://github.com/ElementsProject/lightning/blob/master/cln-grpc/proto/node.proto
[proto]: https://github.com/ElementsProject/lightning/blob/master/cln-grpc-hold/proto/hold.proto

0 comments on commit b115112

Please sign in to comment.