-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnwc.py
executable file
·123 lines (93 loc) · 3.1 KB
/
nwc.py
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
#!/usr/bin/env python3
"""
Entry point for this plugin
"""
try:
from pyln.client import Plugin, Millisatoshi
from coincurve import PrivateKey, PublicKey
import threading
import json
from lib.nip47 import URIOptions, NIP47URI
from lib.wallet import Wallet
from lib.utils import get_keypair
from utilities.rpc_plugin import plugin
except ImportError as e:
# TODO: if something isn't installed then disable the plugin
print("BAD STUFF", f"{e}")
DEFAULT_RELAY = 'wss://relay.getalby.com/v1'
@plugin.init()
def init(options, configuration, plugin: Plugin):
"""initialize the plugin"""
# TODO: create a Main class that implements Keys, Wallet, Plugin
privkey, pubkey = get_keypair(plugin)
plugin.privkey = privkey
plugin.pubkey = pubkey.hex()
# create a Wallet instance to listent for incoming nip47 requests
url = DEFAULT_RELAY
wallet = Wallet(url)
# start a new thread for the relay
wallet_thread = threading.Thread(target=wallet.listen_for_nip47_requests)
wallet_thread.start()
plugin.log(f"connected to {url}", 'info')
# https://github.com/nostr-protocol/nips/blob/master/47.md#example-connection-string
@plugin.method("nwc-create")
def create_nwc_uri(plugin: Plugin, expiry_unix: int = None,
budget_msat: int = None):
"""Create a new nostr wallet connection"""
wallet_pubkey = plugin.pubkey
relay_url = DEFAULT_RELAY
# 32-byte hex encoded secret to sign/encrypt
sk = PrivateKey()
secret = sk.secret.hex()
options = URIOptions(
relay_url=relay_url,
secret=secret,
wallet_pubkey=wallet_pubkey,
expiry_unix=expiry_unix or None,
budget_msat=Millisatoshi(budget_msat) if budget_msat else None
)
nwc = NIP47URI(options=options)
data_string = json.dumps({
"secret": nwc.secret,
"budget_msat": nwc.budget_msat,
"expiry_unix": nwc.expiry_unix,
"spent_msat": Millisatoshi(0)
})
plugin.rpc.datastore(key=nwc.datastore_key, string=data_string)
return {
"url": nwc.url,
"pubkey": nwc.pubkey
}
@plugin.method("nwc-list")
def list_nwc_uris(plugin: Plugin):
"""List all nostr wallet connections"""
all_connections = NIP47URI.find_all()
rtn = []
for nwc in all_connections:
remaining_budget_msat = None
if nwc.budget_msat:
try:
remaining_budget_msat = nwc.budget_msat - nwc.spent_msat
except:
remaining_budget_msat = Millisatoshi(0)
data = {
"url": nwc.url,
"pubkey": nwc.pubkey,
"expiry_unix": nwc.expiry_unix,
"remaining_budget_msat": remaining_budget_msat
}
rtn.append(data)
return {
"connections": rtn
}
@plugin.method("nwc-revoke")
def revoke_nwc_uri(plugin: Plugin, pubkey: str):
"""Revoke a nostr wallet connection"""
nwc = NIP47URI.find_unique(pubkey=pubkey)
if not nwc:
return {
"error": f"No wallet connection found for pubkey {pubkey}"
}
nwc.delete()
return True
plugin.run()