-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added python getting-started example.
- Loading branch information
Showing
8 changed files
with
172 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIICYjCCAgigAwIBAgIUDEw2osNBr+H1o4WCvPSRIjNzUzQwCgYIKoZIzj0EAwIw | ||
fjELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh | ||
biBGcmFuY2lzY28xFDASBgNVBAoTC0Jsb2Nrc3RyZWFtMR0wGwYDVQQLExRDZXJ0 | ||
aWZpY2F0ZUF1dGhvcml0eTENMAsGA1UEAxMER0wgLzAeFw0yMTA0MjYxNzE0MDBa | ||
Fw0zMTA0MjQxNzE0MDBaMH4xCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9y | ||
bmlhMRYwFAYDVQQHEw1TYW4gRnJhbmNpc2NvMRQwEgYDVQQKEwtCbG9ja3N0cmVh | ||
bTEdMBsGA1UECxMUQ2VydGlmaWNhdGVBdXRob3JpdHkxDTALBgNVBAMTBEdMIC8w | ||
WTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATp83k4SqQ5geGRpIpDuU20vrZz8qJ8 | ||
eBDYbW3nIlC8UM/PzVBSNA/MqWlAamB3YGK+VlgsEMbeOUWEM4c9ztVlo2QwYjAO | ||
BgNVHQ8BAf8EBAMCAaYwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIG | ||
A1UdEwEB/wQIMAYBAf8CAQMwHQYDVR0OBBYEFM6ha+o75cd25WbWGqXGR1WKTikj | ||
MAoGCCqGSM49BAMCA0gAMEUCIGBkjyp1Nd/m/b3jEAUmxAisqCahuQUPuyQrIwo0 | ||
ZF/9AiEAsZ8qZfkUZH2Ya7y6ccFTDps/ahsFWSrRao8ru3yhhrs= | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import bip39 # type: ignore | ||
from glclient import Credentials, Signer, Scheduler # type: ignore | ||
from pathlib import Path | ||
from pyln import grpc as clnpb # type: ignore | ||
import secrets # Make sure to use cryptographically sound randomness | ||
|
||
|
||
def save_to_file(file_name: str, data: bytes) -> None: | ||
with open(file_name, "wb") as file: | ||
file.write(data) | ||
|
||
|
||
def read_file(file_name: str) -> bytes: | ||
with open(file_name, "rb") as file: | ||
return file.read() | ||
|
||
|
||
def create_seed() -> bytes: | ||
# ---8<--- [start: create_seed] | ||
rand = secrets.randbits(256).to_bytes(32, "big") # 32 bytes of randomness | ||
|
||
# Show seed phrase to user | ||
phrase = bip39.encode_bytes(rand) | ||
|
||
seed = bip39.phrase_to_seed(phrase)[:32] # Only need the first 32 bytes | ||
|
||
# Store the seed on the filesystem, or secure configuration system | ||
save_to_file("seed", seed) | ||
|
||
# ---8<--- [end: create_seed] | ||
return seed | ||
|
||
|
||
def nobody_with_identity(developer_cert: bytes, developer_key: bytes) -> Credentials: | ||
ca = Path("ca.pem").open(mode="rb").read() | ||
return Credentials.nobody_with(developer_cert, developer_key, ca) | ||
|
||
|
||
Credentials.nobody_with_identity = nobody_with_identity | ||
|
||
|
||
# Validated against gl-testing | ||
def register_node(seed: bytes, developer_cert_path: str, developer_key_path: str) -> None: | ||
# ---8<--- [start: dev_creds] | ||
developer_cert = Path(developer_cert_path).open(mode="rb").read() | ||
developer_key = Path(developer_key_path).open(mode="rb").read() | ||
|
||
developer_creds = Credentials.nobody_with_identity(developer_cert, developer_key) | ||
# ---8<--- [end: dev_creds] | ||
|
||
# ---8<--- [start: init_signer] | ||
network = "bitcoin" | ||
signer = Signer(seed, network, developer_creds) | ||
# ---8<--- [end: init_signer] | ||
|
||
# ---8<--- [start: register_node] | ||
scheduler = Scheduler(signer.node_id(), network, developer_creds) | ||
|
||
# Passing in the signer is required because the client needs to prove | ||
# ownership of the `node_id` | ||
registration_response = scheduler.register(signer, invite_code=None) | ||
|
||
device_creds = Credentials.from_bytes(registration_response.creds) | ||
# save_to_file("creds", device_creds.to_bytes()); | ||
# ---8<--- [end: register_node] | ||
|
||
# ---8<--- [start: get_node] | ||
scheduler = scheduler.authenticate(device_creds) | ||
node = scheduler.node() | ||
# ---8<--- [end: get_node] | ||
|
||
|
||
# TODO: Remove node_id from signature and add node_id to credentials | ||
def start_node(device_creds_path: str, node_id: bytes) -> None: | ||
# ---8<--- [start: start_node] | ||
network = "bitcoin" | ||
device_creds = Credentials.from_path(device_creds_path) | ||
scheduler = Scheduler(node_id, network, device_creds) | ||
|
||
node = scheduler.node() | ||
# ---8<--- [end: start_node] | ||
|
||
# ---8<--- [start: list_peers] | ||
info = node.get_info() | ||
peers = node.list_peers() | ||
# ---8<--- [end: list_peers] | ||
|
||
# ---8<--- [start: start_signer] | ||
seed = read_file("seed") | ||
signer = Signer(seed, network, device_creds) | ||
|
||
signer.run_in_thread() | ||
# ---8<--- [end: start_signer] | ||
|
||
# ---8<--- [start: create_invoice] | ||
node.invoice( | ||
amount_msat=clnpb.AmountOrAny(amount=clnpb.Amount(msat=10000)), | ||
description="description", | ||
label="label", | ||
) | ||
# ---8<--- [end: create_invoice] | ||
|
||
|
||
def recover_node(device_cert: bytes, device_key: bytes) -> None: | ||
# ---8<--- [start: recover_node] | ||
seed = read_file("seed") | ||
network = "bitcoin" | ||
signer_creds = Credentials.with_identity(device_cert, device_key) | ||
signer = Signer(seed, network, signer_creds) | ||
|
||
scheduler = Scheduler( | ||
signer.node_id(), | ||
network, | ||
signer_creds, | ||
) | ||
|
||
scheduler_creds = signer_creds.upgrade(signer, scheduler) | ||
|
||
scheduler = Scheduler( | ||
signer.node_id(), | ||
network, | ||
scheduler_creds, | ||
) | ||
|
||
scheduler.recover(signer) | ||
# ---8<--- [end: recover_node] |
Oops, something went wrong.