-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcashu.py
executable file
·231 lines (176 loc) · 5.95 KB
/
cashu.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/nix/store/5k91mg4qjylxbfvrv748smfh51ppjq0g-python3-3.11.6/bin/python
from pyln.client import Plugin
from utilities.models import (
Mint,
GetKeysResponse,
GetKeysetsResponse,
PostQuoteMintResponse,
PostMintResponse,
PostQuoteMeltResponse,
PostMeltResponse,
PostSwapResponse,
MeltQuote
)
from utilities.utils import (create_blinded_sigs,
tokens_issued,
mark_quote_issued,
mark_token_spent,
validate_inputs,
find_invoice
)
from utilities import crypto
from utilities.rpc_plugin import plugin
# TODO: handle returns... maybe just throw errors and then catch them all the same way rather than directly returning errors?
mint: Mint = None
@plugin.init()
def init(options, configuration, plugin):
"""initialize the plugin"""
# TODO: can we use the hsm secret from the node? Or make the node derive the seed
seed = "johnny apple"
max_order = int(options['max_order'], 10)
derivation_path = options["path"]
global mint
mint = Mint(derivation_path, seed, max_order)
plugin.melt_quotes = {}
plugin.log(f"plugin initialized.")
# TODO: add getinfo method
@plugin.method("cashu-get-keys")
def get_keys():
"""Returns the active public keyset of the mint"""
return GetKeysResponse(keyset=mint.keyset)
@plugin.method("cashu-get-keysets")
def get_keysets(plugin: Plugin, keyset_id=None):
"""Returns all keysets of the mint or a specific keyset if requested"""
if (keyset_id):
keyset = mint.keyset if keyset_id == mint.keyset.id else None
return GetKeysResponse(keyset=keyset)
else:
return GetKeysetsResponse(keysets=[mint.keyset])
# TODO: make a way / figure out how to turn this off xD
@plugin.method("cashu-dev-get-privkeys")
def get_priv_keys(plugin: Plugin):
"""get mint's private keys"""
return {key: value.secret.hex() for key, value in plugin.keyset.private_keys.items()}
@plugin.method("cashu-quote-mint")
def get_mint_quote(plugin: Plugin, amount, unit):
"""Returns a quote for minting tokens"""
quote = mint.mint_quote(amount_sat=int(amount))
return PostQuoteMintResponse(
quote=quote.quote_id,
request=quote.request,
paid=quote.paid,
expiry=quote.expiry
)
@plugin.method("cashu-check-mint")
def check_mint_status(plugin: Plugin, quote: str):
"""Checks the status of a quote request"""
mint_quote = mint.get_mint_quote(quote)
return PostQuoteMintResponse(
quote=mint_quote.quote_id,
request=mint_quote.request,
paid=mint_quote.paid,
expiry=mint_quote.expiry
)
@plugin.method("cashu-mint")
def mint_token(plugin: Plugin, quote: str, outputs):
"""
Returns blinded signatures for blinded messages once a quote request is paid
"""
# TODO: validate outputs list
promises = mint.mint_tokens(outputs=outputs, quote_id=quote)
return PostMintResponse(promises)
@plugin.method("cashu-quote-melt")
def get_melt_quote(plugin: Plugin, req: str, unit: str):
"""Returns a quote for melting tokens"""
melt_quote = mint.melt_quote(bolt11=req)
melt_quote.save()
return PostQuoteMeltResponse(
quote=melt_quote.quote_id,
amount=melt_quote.amount_sat,
fee_reserve=melt_quote.fee_reserve,
paid=melt_quote.paid,
expiry=melt_quote.expiry
)
@plugin.method("cashu-check-melt")
def check_melt_quote(plugin: Plugin, quote: str):
"""Checks the status of a melt quote request"""
melt_quote: MeltQuote = MeltQuote.find(quote_id=quote)
paid = melt_quote.is_paid()
melt_quote.paid = paid
melt_quote.update()
return PostQuoteMeltResponse(quote=quote,
amount=melt_quote.amount_sat,
fee_reserve=melt_quote.fee_reserve,
paid=melt_quote.paid,
expiry=melt_quote.expiry
)
@plugin.method("cashu-melt")
def melt_token(plugin: Plugin, quote: str, inputs: list, outputs: list):
"""melt tokens"""
quote = MeltQuote.find(quote_id=quote)
paid, preimage = mint.melt_tokens(quote, inputs, outputs)
return PostMeltResponse(paid=paid, preimage=preimage)
@plugin.method("cashu-swap")
def swap(plugin, inputs, outputs):
"""swap tokens for other tokens"""
blinded_sigs = mint.swap_tokens(inputs, outputs)
return PostSwapResponse(sigs=blinded_sigs)
@plugin.method("cashu-route-map")
def route_map(plugin):
route_map = [
{
"path": "/keys",
"cmd": "cashu-get-keys",
"method": "GET"
},
{
"path": "/keysets",
"cmd": "cashu-get-keysets",
"method": "GET"
},
{
"path": "/keys/<keyset_id>",
"cmd": "cashu-get-keys",
"method": "GET"
},
{
"path": "/mint/quote/bolt11",
"cmd": "cashu-quote-mint",
"method": "POST"
},
{
"path": "/mint/quote/bolt11/<quote_id>",
"cmd": "cashu-check-mint",
"method": "GET"
},
{
"path": "/mint/bolt11",
"cmd": "cashu-mint",
"method": "POST"
},
{
"path": "/melt/quote/bolt11",
"cmd": "cashu-quote-melt",
"method": "POST",
"param_overrides": {
"request": "req"
}
},
{
"path": "/melt/quote/bolt11/<quote_id>",
"cmd": "cashu-check-melt",
"method": "GET"
},
{
"path": "/melt/bolt11",
"cmd": "cashu-melt",
"method": "POST"
},
{
"path": "/swap",
"cmd": "cashu-swap",
"method": "POST"
}
]
return route_map
plugin.run()