mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-10 12:32:34 +02:00
Working through, getting functions working
This commit is contained in:
@@ -1,24 +1,37 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
from typing import List, Optional, Union
|
from typing import List, Optional, Union
|
||||||
|
|
||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
from .models import Cashu, Pegs, Proof
|
from .models import Cashu, Pegs, Proof, Promises
|
||||||
|
|
||||||
from embit import script
|
from embit import script
|
||||||
from embit import ec
|
from embit import ec
|
||||||
from embit.networks import NETWORKS
|
from embit.networks import NETWORKS
|
||||||
|
from embit import bip32
|
||||||
|
from embit import bip39
|
||||||
from binascii import unhexlify, hexlify
|
from binascii import unhexlify, hexlify
|
||||||
|
import random
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
async def create_cashu(wallet_id: str, data: Cashu) -> Cashu:
|
async def create_cashu(wallet_id: str, data: Cashu) -> Cashu:
|
||||||
cashu_id = urlsafe_short_hash()
|
cashu_id = urlsafe_short_hash()
|
||||||
prv = ec.PrivateKey.from_wif(urlsafe_short_hash())
|
|
||||||
pub = prv.get_public_key()
|
entropy = bytes([random.getrandbits(8) for i in range(16)])
|
||||||
|
mnemonic = bip39.mnemonic_from_bytes(entropy)
|
||||||
|
seed = bip39.mnemonic_to_seed(mnemonic)
|
||||||
|
root = bip32.HDKey.from_seed(seed, version=NETWORKS["main"]["xprv"])
|
||||||
|
|
||||||
|
bip44_xprv = root.derive("m/44h/1h/0h")
|
||||||
|
bip44_xpub = bip44_xprv.to_public()
|
||||||
|
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO cashu.cashu (id, wallet, name, tickershort, fraction, maxsats, coins, prvkey, pubkey)
|
INSERT INTO cashu.cashu (id, wallet, name, tickershort, fraction, maxsats, coins, prvkey, pubkey)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
cashu_id,
|
cashu_id,
|
||||||
@@ -28,8 +41,8 @@ async def create_cashu(wallet_id: str, data: Cashu) -> Cashu:
|
|||||||
data.fraction,
|
data.fraction,
|
||||||
data.maxsats,
|
data.maxsats,
|
||||||
data.coins,
|
data.coins,
|
||||||
prv,
|
bip44_xprv.to_base58(),
|
||||||
pub
|
bip44_xpub.to_base58()
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,17 +52,20 @@ async def create_cashu(wallet_id: str, data: Cashu) -> Cashu:
|
|||||||
|
|
||||||
|
|
||||||
async def update_cashu_keys(cashu_id, wif: str = None) -> Optional[Cashu]:
|
async def update_cashu_keys(cashu_id, wif: str = None) -> Optional[Cashu]:
|
||||||
if not wif:
|
entropy = bytes([random.getrandbits(8) for i in range(16)])
|
||||||
prv = ec.PrivateKey.from_wif(urlsafe_short_hash())
|
mnemonic = bip39.mnemonic_from_bytes(entropy)
|
||||||
else:
|
seed = bip39.mnemonic_to_seed(mnemonic)
|
||||||
prv = ec.PrivateKey.from_wif(wif)
|
root = bip32.HDKey.from_seed(seed, version=NETWORKS["main"]["xprv"])
|
||||||
pub = prv.get_public_key()
|
|
||||||
await db.execute("UPDATE cashu.cashu SET prv = ?, pub = ? WHERE id = ?", (hexlify(prv.serialize()), hexlify(pub.serialize()), cashu_id))
|
bip44_xprv = root.derive("m/44h/1h/0h")
|
||||||
|
bip44_xpub = bip44_xprv.to_public()
|
||||||
|
|
||||||
|
await db.execute("UPDATE cashu.cashu SET prv = ?, pub = ? WHERE id = ?", bip44_xprv.to_base58(), bip44_xpub.to_base58(), cashu_id)
|
||||||
row = await db.fetchone("SELECT * FROM cashu.cashu WHERE id = ?", (cashu_id,))
|
row = await db.fetchone("SELECT * FROM cashu.cashu WHERE id = ?", (cashu_id,))
|
||||||
return Cashu(**row) if row else None
|
return Cashu(**row) if row else None
|
||||||
|
|
||||||
|
|
||||||
async def get_cashu(cashu_id: str) -> Optional[Cashu]:
|
async def get_cashu(cashu_id) -> Optional[Cashu]:
|
||||||
row = await db.fetchone("SELECT * FROM cashu.cashu WHERE id = ?", (cashu_id,))
|
row = await db.fetchone("SELECT * FROM cashu.cashu WHERE id = ?", (cashu_id,))
|
||||||
return Cashu(**row) if row else None
|
return Cashu(**row) if row else None
|
||||||
|
|
||||||
@@ -66,57 +82,62 @@ async def get_cashus(wallet_ids: Union[str, List[str]]) -> List[Cashu]:
|
|||||||
return [Cashu(**row) for row in rows]
|
return [Cashu(**row) for row in rows]
|
||||||
|
|
||||||
|
|
||||||
async def delete_cashu(cashu_id: str) -> None:
|
async def delete_cashu(cashu_id) -> None:
|
||||||
await db.execute("DELETE FROM cashu.cashu WHERE id = ?", (cashu_id,))
|
await db.execute("DELETE FROM cashu.cashu WHERE id = ?", (cashu_id,))
|
||||||
|
|
||||||
|
|
||||||
|
##########################################
|
||||||
###############MINT STUFF#################
|
###############MINT STUFF#################
|
||||||
|
##########################################
|
||||||
|
|
||||||
async def store_promise(
|
async def store_promise(
|
||||||
amount: int,
|
amount: int,
|
||||||
B_: str,
|
B_: str,
|
||||||
C_: str
|
C_: str,
|
||||||
|
cashu_id
|
||||||
):
|
):
|
||||||
|
promise_id = urlsafe_short_hash()
|
||||||
|
|
||||||
await (conn or db).execute(
|
await (conn or db).execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO promises
|
INSERT INTO cashu.promises
|
||||||
(amount, B_b, C_b)
|
(id, amount, B_b, C_b, cashu_id)
|
||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
|
promise_id,
|
||||||
amount,
|
amount,
|
||||||
str(B_),
|
str(B_),
|
||||||
str(C_),
|
str(C_),
|
||||||
|
cashu_id
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def get_promises(cashu_id) -> Optional[Cashu]:
|
||||||
|
row = await db.fetchall("SELECT * FROM cashu.promises WHERE cashu_id = ?", (promises_id,))
|
||||||
|
return Promises(**row) if row else None
|
||||||
|
|
||||||
async def get_proofs_used():
|
async def get_proofs_used(cashu_id):
|
||||||
|
rows = await db.fetchall("SELECT secret from cashu.proofs_used WHERE id = ?", (cashu_id,))
|
||||||
rows = await (conn or db).fetchall(
|
|
||||||
"""
|
|
||||||
SELECT secret from proofs_used
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
return [row[0] for row in rows]
|
return [row[0] for row in rows]
|
||||||
|
|
||||||
|
|
||||||
async def invalidate_proof(
|
async def invalidate_proof(
|
||||||
proof: Proof
|
proof: Proof,
|
||||||
|
cashu_id
|
||||||
):
|
):
|
||||||
|
invalidate_proof_id = urlsafe_short_hash()
|
||||||
# we add the proof and secret to the used list
|
|
||||||
await (conn or db).execute(
|
await (conn or db).execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO proofs_used
|
INSERT INTO cashu.proofs_used
|
||||||
(amount, C, secret)
|
(id, amount, C, secret, cashu_id)
|
||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
|
invalidate_proof_id,
|
||||||
proof.amount,
|
proof.amount,
|
||||||
str(proof.C),
|
str(proof.C),
|
||||||
str(proof.secret),
|
str(proof.secret),
|
||||||
|
cashu_id
|
||||||
),
|
),
|
||||||
)
|
)
|
@@ -5,22 +5,10 @@ from .models import BlindedMessage, BlindedSignature, Invoice, Proof
|
|||||||
from secp256k1 import PublicKey, PrivateKey
|
from secp256k1 import PublicKey, PrivateKey
|
||||||
|
|
||||||
from fastapi import Query
|
from fastapi import Query
|
||||||
|
from .crud import get_cashu
|
||||||
from lnbits.core.services import check_transaction_status, create_invoice
|
from lnbits.core.services import check_transaction_status, create_invoice
|
||||||
|
|
||||||
class Ledger:
|
def _derive_keys(master_key: str, cashu_id: str = Query(None)):
|
||||||
def __init__(self, secret_key: str, MAX_ORDER: int = Query(64)):
|
|
||||||
self.proofs_used: Set[str] = set()
|
|
||||||
|
|
||||||
self.master_key: str = secret_key
|
|
||||||
self.keys: List[PrivateKey] = self._derive_keys(self.master_key)
|
|
||||||
self.pub_keys: List[PublicKey] = self._derive_pubkeys(self.keys)
|
|
||||||
|
|
||||||
async def load_used_proofs(self):
|
|
||||||
self.proofs_used = set(await get_proofs_used)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _derive_keys(master_key: str):
|
|
||||||
"""Deterministic derivation of keys for 2^n values."""
|
"""Deterministic derivation of keys for 2^n values."""
|
||||||
return {
|
return {
|
||||||
2
|
2
|
||||||
@@ -33,18 +21,17 @@ class Ledger:
|
|||||||
for i in range(MAX_ORDER)
|
for i in range(MAX_ORDER)
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
def _derive_pubkeys(keys: List[PrivateKey], cashu_id: str = Query(None)):
|
||||||
def _derive_pubkeys(keys: List[PrivateKey]):
|
|
||||||
return {amt: keys[amt].pubkey for amt in [2**i for i in range(MAX_ORDER)]}
|
return {amt: keys[amt].pubkey for amt in [2**i for i in range(MAX_ORDER)]}
|
||||||
|
|
||||||
async def _generate_promises(self, amounts: List[int], B_s: List[str]):
|
async def _generate_promises(amounts: List[int], B_s: List[str], cashu_id: str = Query(None)):
|
||||||
"""Generates promises that sum to the given amount."""
|
"""Generates promises that sum to the given amount."""
|
||||||
return [
|
return [
|
||||||
await self._generate_promise(amount, PublicKey(bytes.fromhex(B_), raw=True))
|
await self._generate_promise(amount, PublicKey(bytes.fromhex(B_), raw=True))
|
||||||
for (amount, B_) in zip(amounts, B_s)
|
for (amount, B_) in zip(amounts, B_s)
|
||||||
]
|
]
|
||||||
|
|
||||||
async def _generate_promise(self, amount: int, B_: PublicKey):
|
async def _generate_promise(amount: int, B_: PublicKey, cashu_id: str = Query(None)):
|
||||||
"""Generates a promise for given amount and returns a pair (amount, C')."""
|
"""Generates a promise for given amount and returns a pair (amount, C')."""
|
||||||
secret_key = self.keys[amount] # Get the correct key
|
secret_key = self.keys[amount] # Get the correct key
|
||||||
C_ = step2_bob(B_, secret_key)
|
C_ = step2_bob(B_, secret_key)
|
||||||
@@ -53,11 +40,11 @@ class Ledger:
|
|||||||
)
|
)
|
||||||
return BlindedSignature(amount=amount, C_=C_.serialize().hex())
|
return BlindedSignature(amount=amount, C_=C_.serialize().hex())
|
||||||
|
|
||||||
def _check_spendable(self, proof: Proof):
|
def _check_spendable(proof: Proof, cashu_id: str = Query(None)):
|
||||||
"""Checks whether the proof was already spent."""
|
"""Checks whether the proof was already spent."""
|
||||||
return not proof.secret in self.proofs_used
|
return not proof.secret in self.proofs_used
|
||||||
|
|
||||||
def _verify_proof(self, proof: Proof):
|
def _verify_proof(proof: Proof, cashu_id: str = Query(None)):
|
||||||
"""Verifies that the proof of promise was issued by this ledger."""
|
"""Verifies that the proof of promise was issued by this ledger."""
|
||||||
if not self._check_spendable(proof):
|
if not self._check_spendable(proof):
|
||||||
raise Exception(f"tokens already spent. Secret: {proof.secret}")
|
raise Exception(f"tokens already spent. Secret: {proof.secret}")
|
||||||
@@ -65,9 +52,7 @@ class Ledger:
|
|||||||
C = PublicKey(bytes.fromhex(proof.C), raw=True)
|
C = PublicKey(bytes.fromhex(proof.C), raw=True)
|
||||||
return verify(secret_key, C, proof.secret)
|
return verify(secret_key, C, proof.secret)
|
||||||
|
|
||||||
def _verify_outputs(
|
def _verify_outputs(total: int, amount: int, output_data: List[BlindedMessage], cashu_id: str = Query(None)):
|
||||||
self, total: int, amount: int, output_data: List[BlindedMessage]
|
|
||||||
):
|
|
||||||
"""Verifies the expected split was correctly computed"""
|
"""Verifies the expected split was correctly computed"""
|
||||||
fst_amt, snd_amt = total - amount, amount # we have two amounts to split to
|
fst_amt, snd_amt = total - amount, amount # we have two amounts to split to
|
||||||
fst_outputs = amount_split(fst_amt)
|
fst_outputs = amount_split(fst_amt)
|
||||||
@@ -76,9 +61,7 @@ class Ledger:
|
|||||||
given = [o.amount for o in output_data]
|
given = [o.amount for o in output_data]
|
||||||
return given == expected
|
return given == expected
|
||||||
|
|
||||||
def _verify_no_duplicates(
|
def _verify_no_duplicates(proofs: List[Proof], output_data: List[BlindedMessage], cashu_id: str = Query(None)):
|
||||||
self, proofs: List[Proof], output_data: List[BlindedMessage]
|
|
||||||
):
|
|
||||||
secrets = [p.secret for p in proofs]
|
secrets = [p.secret for p in proofs]
|
||||||
if len(secrets) != len(list(set(secrets))):
|
if len(secrets) != len(list(set(secrets))):
|
||||||
return False
|
return False
|
||||||
@@ -87,7 +70,7 @@ class Ledger:
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _verify_split_amount(self, amount: int):
|
def _verify_split_amount(amount: int, cashu_id: str = Query(None)):
|
||||||
"""Split amount like output amount can't be negative or too big."""
|
"""Split amount like output amount can't be negative or too big."""
|
||||||
try:
|
try:
|
||||||
self._verify_amount(amount)
|
self._verify_amount(amount)
|
||||||
@@ -95,22 +78,20 @@ class Ledger:
|
|||||||
# For better error message
|
# For better error message
|
||||||
raise Exception("invalid split amount: " + str(amount))
|
raise Exception("invalid split amount: " + str(amount))
|
||||||
|
|
||||||
def _verify_amount(self, amount: int):
|
def _verify_amount(amount: int, cashu_id: str = Query(None)):
|
||||||
"""Any amount used should be a positive integer not larger than 2^MAX_ORDER."""
|
"""Any amount used should be a positive integer not larger than 2^MAX_ORDER."""
|
||||||
valid = isinstance(amount, int) and amount > 0 and amount < 2**MAX_ORDER
|
valid = isinstance(amount, int) and amount > 0 and amount < 2**MAX_ORDER
|
||||||
if not valid:
|
if not valid:
|
||||||
raise Exception("invalid amount: " + str(amount))
|
raise Exception("invalid amount: " + str(amount))
|
||||||
return amount
|
return amount
|
||||||
|
|
||||||
def _verify_equation_balanced(
|
def _verify_equation_balanced(proofs: List[Proof], outs: List[BlindedMessage], cashu_id: str = Query(None)):
|
||||||
self, proofs: List[Proof], outs: List[BlindedMessage]
|
|
||||||
):
|
|
||||||
"""Verify that Σoutputs - Σinputs = 0."""
|
"""Verify that Σoutputs - Σinputs = 0."""
|
||||||
sum_inputs = sum(self._verify_amount(p.amount) for p in proofs)
|
sum_inputs = sum(self._verify_amount(p.amount) for p in proofs)
|
||||||
sum_outputs = sum(self._verify_amount(p.amount) for p in outs)
|
sum_outputs = sum(self._verify_amount(p.amount) for p in outs)
|
||||||
assert sum_outputs - sum_inputs == 0
|
assert sum_outputs - sum_inputs == 0
|
||||||
|
|
||||||
def _get_output_split(self, amount: int):
|
def _get_output_split(amount: int, cashu_id: str):
|
||||||
"""Given an amount returns a list of amounts returned e.g. 13 is [1, 4, 8]."""
|
"""Given an amount returns a list of amounts returned e.g. 13 is [1, 4, 8]."""
|
||||||
self._verify_amount(amount)
|
self._verify_amount(amount)
|
||||||
bits_amt = bin(amount)[::-1][:-2]
|
bits_amt = bin(amount)[::-1][:-2]
|
||||||
@@ -120,7 +101,7 @@ class Ledger:
|
|||||||
rv.append(2**pos)
|
rv.append(2**pos)
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
async def _invalidate_proofs(self, proofs: List[Proof]):
|
async def _invalidate_proofs(proofs: List[Proof], cashu_id: str = Query(None)):
|
||||||
"""Adds secrets of proofs to the list of knwon secrets and stores them in the db."""
|
"""Adds secrets of proofs to the list of knwon secrets and stores them in the db."""
|
||||||
# Mark proofs as used and prepare new promises
|
# Mark proofs as used and prepare new promises
|
||||||
proof_msgs = set([p.secret for p in proofs])
|
proof_msgs = set([p.secret for p in proofs])
|
||||||
@@ -129,18 +110,21 @@ class Ledger:
|
|||||||
for p in proofs:
|
for p in proofs:
|
||||||
await invalidate_proof(p)
|
await invalidate_proof(p)
|
||||||
|
|
||||||
# Public methods
|
def get_pubkeys(cashu_id: str = Query(None)):
|
||||||
def get_pubkeys(self):
|
|
||||||
"""Returns public keys for possible amounts."""
|
"""Returns public keys for possible amounts."""
|
||||||
return {a: p.serialize().hex() for a, p in self.pub_keys.items()}
|
return {a: p.serialize().hex() for a, p in self.pub_keys.items()}
|
||||||
|
|
||||||
async def request_mint(self, amount):
|
async def request_mint(amount, cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
"""Returns Lightning invoice and stores it in the db."""
|
"""Returns Lightning invoice and stores it in the db."""
|
||||||
payment_request, payment_hash = payment_hash, payment_request = await create_invoice(
|
payment_hash, payment_request = await create_invoice(
|
||||||
wallet_id=link.wallet,
|
wallet_id=cashu.wallet,
|
||||||
amount=amount,
|
amount=amount,
|
||||||
memo=link.description,
|
memo=cashu.name,
|
||||||
unhashed_description=link.description.encode("utf-8"),
|
unhashed_description=cashu.name.encode("utf-8"),
|
||||||
extra={
|
extra={
|
||||||
"tag": "Cashu"
|
"tag": "Cashu"
|
||||||
},
|
},
|
||||||
@@ -151,13 +135,17 @@ class Ledger:
|
|||||||
)
|
)
|
||||||
if not payment_request or not payment_hash:
|
if not payment_request or not payment_hash:
|
||||||
raise Exception(f"Could not create Lightning invoice.")
|
raise Exception(f"Could not create Lightning invoice.")
|
||||||
await store_lightning_invoice(invoice)
|
|
||||||
return payment_request, payment_hash
|
return payment_request, payment_hash
|
||||||
|
|
||||||
async def mint(self, B_s: List[PublicKey], amounts: List[int], payment_hash=None):
|
async def mint(B_s: List[PublicKey], amounts: List[int], payment_hash: str = Query(None), cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
"""Mints a promise for coins for B_."""
|
"""Mints a promise for coins for B_."""
|
||||||
# check if lightning invoice was paid
|
# check if lightning invoice was paid
|
||||||
if payment_hash and not await check_transaction_status(payment_hash):
|
if payment_hash:
|
||||||
|
if not await check_transaction_status(wallet_id=cashu.wallet, payment_hash=payment_hash):
|
||||||
raise Exception("Lightning invoice not paid yet.")
|
raise Exception("Lightning invoice not paid yet.")
|
||||||
|
|
||||||
for amount in amounts:
|
for amount in amounts:
|
||||||
@@ -169,7 +157,11 @@ class Ledger:
|
|||||||
]
|
]
|
||||||
return promises
|
return promises
|
||||||
|
|
||||||
async def melt(self, proofs: List[Proof], amount: int, invoice: str):
|
async def melt(proofs: List[Proof], amount: int, invoice: str, cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
"""Invalidates proofs and pays a Lightning invoice."""
|
"""Invalidates proofs and pays a Lightning invoice."""
|
||||||
# if not LIGHTNING:
|
# if not LIGHTNING:
|
||||||
total = sum([p["amount"] for p in proofs])
|
total = sum([p["amount"] for p in proofs])
|
||||||
@@ -189,13 +181,19 @@ class Ledger:
|
|||||||
await self._invalidate_proofs(proofs)
|
await self._invalidate_proofs(proofs)
|
||||||
return status, payment_hash
|
return status, payment_hash
|
||||||
|
|
||||||
async def check_spendable(self, proofs: List[Proof]):
|
async def check_spendable(proofs: List[Proof], cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
"""Checks if all provided proofs are valid and still spendable (i.e. have not been spent)."""
|
"""Checks if all provided proofs are valid and still spendable (i.e. have not been spent)."""
|
||||||
return {i: self._check_spendable(p) for i, p in enumerate(proofs)}
|
return {i: self._check_spendable(p) for i, p in enumerate(proofs)}
|
||||||
|
|
||||||
async def split(
|
async def split(proofs: List[Proof], amount: int, output_data: List[BlindedMessage], cashu_id: str = Query(None)):
|
||||||
self, proofs: List[Proof], amount: int, output_data: List[BlindedMessage]
|
cashu = await get_cashu(cashu_id)
|
||||||
):
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
"""Consumes proofs and prepares new promises based on the amount split."""
|
"""Consumes proofs and prepares new promises based on the amount split."""
|
||||||
self._verify_split_amount(amount)
|
self._verify_split_amount(amount)
|
||||||
# Verify proofs are valid
|
# Verify proofs are valid
|
||||||
@@ -225,14 +223,21 @@ class Ledger:
|
|||||||
return prom_fst, prom_snd
|
return prom_fst, prom_snd
|
||||||
|
|
||||||
|
|
||||||
##############FUNCTIONS###############
|
async def fee_reserve(amount_msat: int, cashu_id: str = Query(None)):
|
||||||
def fee_reserve(amount_msat: int) -> int:
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
"""Function for calculating the Lightning fee reserve"""
|
"""Function for calculating the Lightning fee reserve"""
|
||||||
return max(
|
return max(
|
||||||
int(LIGHTNING_RESERVE_FEE_MIN), int(amount_msat * LIGHTNING_FEE_PERCENT / 100.0)
|
int(LIGHTNING_RESERVE_FEE_MIN), int(amount_msat * LIGHTNING_FEE_PERCENT / 100.0)
|
||||||
)
|
)
|
||||||
|
|
||||||
def amount_split(amount):
|
async def amount_split(amount, cashu_id: str):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
"""Given an amount returns a list of amounts returned e.g. 13 is [1, 4, 8]."""
|
"""Given an amount returns a list of amounts returned e.g. 13 is [1, 4, 8]."""
|
||||||
bits_amt = bin(amount)[::-1][:-2]
|
bits_amt = bin(amount)[::-1][:-2]
|
||||||
rv = []
|
rv = []
|
||||||
@@ -241,7 +246,11 @@ def amount_split(amount):
|
|||||||
rv.append(2**pos)
|
rv.append(2**pos)
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
def hash_to_point(secret_msg):
|
async def hash_to_point(secret_msg, cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
"""Generates x coordinate from the message hash and checks if the point lies on the curve.
|
"""Generates x coordinate from the message hash and checks if the point lies on the curve.
|
||||||
If it does not, it tries computing again a new x coordinate from the hash of the coordinate."""
|
If it does not, it tries computing again a new x coordinate from the hash of the coordinate."""
|
||||||
point = None
|
point = None
|
||||||
@@ -260,24 +269,39 @@ def hash_to_point(secret_msg):
|
|||||||
return point
|
return point
|
||||||
|
|
||||||
|
|
||||||
def step1_alice(secret_msg):
|
async def step1_alice(secret_msg, cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
secret_msg = secret_msg.encode("utf-8")
|
secret_msg = secret_msg.encode("utf-8")
|
||||||
Y = hash_to_point(secret_msg)
|
Y = hash_to_point(secret_msg)
|
||||||
r = PrivateKey()
|
r = PrivateKey()
|
||||||
B_ = Y + r.pubkey
|
B_ = Y + r.pubkey
|
||||||
return B_, r
|
return B_, r
|
||||||
|
|
||||||
|
async def step2_bob(B_, a, cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
def step2_bob(B_, a):
|
|
||||||
C_ = B_.mult(a)
|
C_ = B_.mult(a)
|
||||||
return C_
|
return C_
|
||||||
|
|
||||||
|
|
||||||
def step3_alice(C_, r, A):
|
async def step3_alice(C_, r, A, cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
C = C_ - A.mult(r)
|
C = C_ - A.mult(r)
|
||||||
return C
|
return C
|
||||||
|
|
||||||
|
|
||||||
def verify(a, C, secret_msg):
|
async def verify(a, C, secret_msg, cashu_id: str = Query(None)):
|
||||||
|
cashu = await get_cashu(cashu_id)
|
||||||
|
if not cashu:
|
||||||
|
raise Exception(f"Could not find Cashu")
|
||||||
|
|
||||||
Y = hash_to_point(secret_msg.encode("utf-8"))
|
Y = hash_to_point(secret_msg.encode("utf-8"))
|
||||||
return C == Y.mult(a)
|
return C == Y.mult(a)
|
||||||
|
@@ -8,7 +8,7 @@ async def m001_initial(db):
|
|||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
wallet TEXT NOT NULL,
|
wallet TEXT NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
tickershort TEXT NOT NULL,
|
tickershort TEXT DEFAULT 'sats',
|
||||||
fraction BOOL,
|
fraction BOOL,
|
||||||
maxsats INT,
|
maxsats INT,
|
||||||
coins INT,
|
coins INT,
|
||||||
@@ -32,3 +32,32 @@ async def m001_initial(db):
|
|||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
"""
|
||||||
|
Initial cashus table.
|
||||||
|
"""
|
||||||
|
await db.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE cashu.promises (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
amount INT,
|
||||||
|
B_b TEXT NOT NULL,
|
||||||
|
C_b TEXT NOT NULL,
|
||||||
|
cashu_id TEXT NOT NULL
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
"""
|
||||||
|
Initial cashus table.
|
||||||
|
"""
|
||||||
|
await db.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE cashu.proofs_used (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
amount INT,
|
||||||
|
C TEXT NOT NULL,
|
||||||
|
secret TEXT NOT NULL,
|
||||||
|
cashu_id TEXT NOT NULL
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
@@ -9,7 +9,7 @@ class Cashu(BaseModel):
|
|||||||
id: str = Query(None)
|
id: str = Query(None)
|
||||||
name: str = Query(None)
|
name: str = Query(None)
|
||||||
wallet: str = Query(None)
|
wallet: str = Query(None)
|
||||||
tickershort: str
|
tickershort: str = Query(None)
|
||||||
fraction: bool = Query(None)
|
fraction: bool = Query(None)
|
||||||
maxsats: int = Query(0)
|
maxsats: int = Query(0)
|
||||||
coins: int = Query(0)
|
coins: int = Query(0)
|
||||||
@@ -34,6 +34,13 @@ class Pegs(BaseModel):
|
|||||||
class PayLnurlWData(BaseModel):
|
class PayLnurlWData(BaseModel):
|
||||||
lnurl: str
|
lnurl: str
|
||||||
|
|
||||||
|
class Promises(BaseModel):
|
||||||
|
id: str
|
||||||
|
amount: int
|
||||||
|
B_b: str
|
||||||
|
C_b: str
|
||||||
|
cashu_id: str
|
||||||
|
|
||||||
class Proof(BaseModel):
|
class Proof(BaseModel):
|
||||||
amount: int
|
amount: int
|
||||||
secret: str
|
secret: str
|
||||||
|
@@ -9,7 +9,6 @@ from lnbits.tasks import internal_invoice_queue, register_invoice_listener
|
|||||||
|
|
||||||
from .crud import get_cashu
|
from .crud import get_cashu
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_paid_invoices():
|
async def wait_for_paid_invoices():
|
||||||
invoice_queue = asyncio.Queue()
|
invoice_queue = asyncio.Queue()
|
||||||
register_invoice_listener(invoice_queue)
|
register_invoice_listener(invoice_queue)
|
||||||
|
@@ -80,13 +80,10 @@
|
|||||||
<q-card class="q-pa-lg q-pt-xl" style="width: 500px">
|
<q-card class="q-pa-lg q-pt-xl" style="width: 500px">
|
||||||
<q-form @submit="createMint" class="q-gutter-md">
|
<q-form @submit="createMint" class="q-gutter-md">
|
||||||
<q-input filled dense v-model.trim="formDialog.data.name" label="Mint Name" placeholder="Cashu Mint"></q-input>
|
<q-input filled dense v-model.trim="formDialog.data.name" label="Mint Name" placeholder="Cashu Mint"></q-input>
|
||||||
<q-input filled dense v-model.trim="formDialog.data.tickershort" label="Ticker shorthand" placeholder="CC"
|
|
||||||
#></q-input>
|
|
||||||
<q-select filled dense emit-value v-model="formDialog.data.wallet" :options="g.user.walletOptions"
|
<q-select filled dense emit-value v-model="formDialog.data.wallet" :options="g.user.walletOptions"
|
||||||
label="Wallet *" ></q-select>
|
label="Wallet *" ></q-select>
|
||||||
<q-toggle v-model="toggleAdvanced" label="Show advanced options"></q-toggle>
|
<q-toggle v-model="toggleAdvanced" label="Show advanced options"></q-toggle>
|
||||||
<div v-show="toggleAdvanced">
|
<div v-show="toggleAdvanced">
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-5">
|
<div class="col-5">
|
||||||
<q-checkbox v-model="formDialog.data.fraction" color="primary" label="sats/coins?">
|
<q-checkbox v-model="formDialog.data.fraction" color="primary" label="sats/coins?">
|
||||||
@@ -96,6 +93,8 @@
|
|||||||
<div class="col-7">
|
<div class="col-7">
|
||||||
<q-input v-if="!formDialog.data.fraction" filled dense type="number" v-model.trim="formDialog.data.cost" label="Sat coin cost (optional)"
|
<q-input v-if="!formDialog.data.fraction" filled dense type="number" v-model.trim="formDialog.data.cost" label="Sat coin cost (optional)"
|
||||||
value="1" type="number"></q-input>
|
value="1" type="number"></q-input>
|
||||||
|
<q-input v-if="!formDialog.data.fraction" filled dense v-model.trim="formDialog.data.tickershort" label="Ticker shorthand" placeholder="CC"
|
||||||
|
#></q-input>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<q-input class="q-mt-md" filled dense type="number" v-model.trim="formDialog.data.maxsats"
|
<q-input class="q-mt-md" filled dense type="number" v-model.trim="formDialog.data.maxsats"
|
||||||
@@ -105,7 +104,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="row q-mt-md">
|
<div class="row q-mt-md">
|
||||||
<q-btn unelevated color="primary"
|
<q-btn unelevated color="primary"
|
||||||
:disable="formDialog.data.tickershort == null || formDialog.data.name == null" type="submit">Create Mint
|
:disable="formDialog.data.wallet == null || formDialog.data.name == null" type="submit">Create Mint
|
||||||
</q-btn>
|
</q-btn>
|
||||||
<q-btn v-close-popup flat color="grey" class="q-ml-auto">Cancel</q-btn>
|
<q-btn v-close-popup flat color="grey" class="q-ml-auto">Cancel</q-btn>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -22,7 +22,6 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
|
|||||||
"cashu/index.html", {"request": request, "user": user.dict()}
|
"cashu/index.html", {"request": request, "user": user.dict()}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@cashu_ext.get("/wallet")
|
@cashu_ext.get("/wallet")
|
||||||
async def cashu(request: Request):
|
async def cashu(request: Request):
|
||||||
return cashu_renderer().TemplateResponse("cashu/wallet.html",{"request": request})
|
return cashu_renderer().TemplateResponse("cashu/wallet.html",{"request": request})
|
||||||
|
@@ -15,10 +15,25 @@ from lnbits.core.views.api import api_payment
|
|||||||
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
|
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
|
||||||
|
|
||||||
from . import cashu_ext
|
from . import cashu_ext
|
||||||
from .crud import create_cashu, delete_cashu, get_cashu, get_cashus, update_cashu_keys
|
from .ledger import get_pubkeys, request_mint, mint
|
||||||
from .models import Cashu, Pegs, CheckPayload, MeltPayload, MintPayloads, SplitPayload, PayLnurlWData
|
|
||||||
|
|
||||||
from .ledger import Ledger, fee_reserve, amount_split, hash_to_point, step1_alice, step2_bob, step3_alice, verify
|
from .crud import (
|
||||||
|
create_cashu,
|
||||||
|
delete_cashu,
|
||||||
|
get_cashu,
|
||||||
|
get_cashus,
|
||||||
|
update_cashu_keys
|
||||||
|
)
|
||||||
|
|
||||||
|
from .models import (
|
||||||
|
Cashu,
|
||||||
|
Pegs,
|
||||||
|
CheckPayload,
|
||||||
|
MeltPayload,
|
||||||
|
MintPayloads,
|
||||||
|
SplitPayload,
|
||||||
|
PayLnurlWData
|
||||||
|
)
|
||||||
|
|
||||||
@cashu_ext.get("/api/v1/cashus", status_code=HTTPStatus.OK)
|
@cashu_ext.get("/api/v1/cashus", status_code=HTTPStatus.OK)
|
||||||
async def api_cashus(
|
async def api_cashus(
|
||||||
@@ -173,50 +188,54 @@ async def api_cashu_check_invoice(cashu_id: str, payment_hash: str):
|
|||||||
return status
|
return status
|
||||||
|
|
||||||
|
|
||||||
#################CASHU STUFF###################
|
########################################
|
||||||
|
#################MINT###################
|
||||||
|
########################################
|
||||||
|
|
||||||
@cashu_ext.get("/keys")
|
@cashu_ext.get("/keys")
|
||||||
def keys():
|
def keys(cashu_id: str):
|
||||||
"""Get the public keys of the mint"""
|
"""Get the public keys of the mint"""
|
||||||
return ledger.get_pubkeys()
|
return get_pubkeys(cashu_id)
|
||||||
|
|
||||||
|
|
||||||
@cashu_ext.get("/mint")
|
@cashu_ext.get("/mint")
|
||||||
async def request_mint(amount: int = 0):
|
async def mint_pay_request(amount: int = 0, cashu_id: str = Query(None)):
|
||||||
"""Request minting of tokens. Server responds with a Lightning invoice."""
|
"""Request minting of tokens. Server responds with a Lightning invoice."""
|
||||||
payment_request, payment_hash = await ledger.request_mint(amount)
|
payment_request, payment_hash = await request_mint(amount, cashu_id)
|
||||||
print(f"Lightning invoice: {payment_request}")
|
print(f"Lightning invoice: {payment_request}")
|
||||||
return {"pr": payment_request, "hash": payment_hash}
|
return {"pr": payment_request, "hash": payment_hash}
|
||||||
|
|
||||||
|
|
||||||
@cashu_ext.post("/mint")
|
@cashu_ext.post("/mint")
|
||||||
async def mint(payloads: MintPayloads, payment_hash: Union[str, None] = None):
|
async def mint_coins(payloads: MintPayloads, payment_hash: Union[str, None] = None, cashu_id: str = Query(None)):
|
||||||
amounts = []
|
amounts = []
|
||||||
B_s = []
|
B_s = []
|
||||||
for payload in payloads.blinded_messages:
|
for payload in payloads.blinded_messages:
|
||||||
amounts.append(payload.amount)
|
amounts.append(payload.amount)
|
||||||
B_s.append(PublicKey(bytes.fromhex(payload.B_), raw=True))
|
B_s.append(PublicKey(bytes.fromhex(payload.B_), raw=True))
|
||||||
|
promises = await mint(B_s, amounts, payment_hash, cashu_id)
|
||||||
|
logger.debug(promises)
|
||||||
try:
|
try:
|
||||||
promises = await ledger.mint(B_s, amounts, payment_hash=payment_hash)
|
promises = await mint(B_s, amounts, payment_hash, cashu_id)
|
||||||
return promises
|
return promises
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return {"error": str(exc)}
|
return {"error": str(exc)}
|
||||||
|
|
||||||
|
|
||||||
@cashu_ext.post("/melt")
|
@cashu_ext.post("/melt")
|
||||||
async def melt(payload: MeltPayload):
|
async def melt_coins(payload: MeltPayload, cashu_id: str = Query(None)):
|
||||||
|
|
||||||
ok, preimage = await ledger.melt(payload.proofs, payload.amount, payload.invoice)
|
ok, preimage = await melt(payload.proofs, payload.amount, payload.invoice, cashu_id)
|
||||||
return {"paid": ok, "preimage": preimage}
|
return {"paid": ok, "preimage": preimage}
|
||||||
|
|
||||||
|
|
||||||
@cashu_ext.post("/check")
|
@cashu_ext.post("/check")
|
||||||
async def check_spendable(payload: CheckPayload):
|
async def check_spendable_coins(payload: CheckPayload, cashu_id: str = Query(None)):
|
||||||
return await ledger.check_spendable(payload.proofs)
|
return await check_spendable(payload.proofs, cashu_id)
|
||||||
|
|
||||||
|
|
||||||
@cashu_ext.post("/split")
|
@cashu_ext.post("/split")
|
||||||
async def split(payload: SplitPayload):
|
async def spli_coinst(payload: SplitPayload, cashu_id: str = Query(None)):
|
||||||
"""
|
"""
|
||||||
Requetst a set of tokens with amount "total" to be split into two
|
Requetst a set of tokens with amount "total" to be split into two
|
||||||
newly minted sets with amount "split" and "total-split".
|
newly minted sets with amount "split" and "total-split".
|
||||||
@@ -225,7 +244,7 @@ async def split(payload: SplitPayload):
|
|||||||
amount = payload.amount
|
amount = payload.amount
|
||||||
output_data = payload.output_data.blinded_messages
|
output_data = payload.output_data.blinded_messages
|
||||||
try:
|
try:
|
||||||
split_return = await ledger.split(proofs, amount, output_data)
|
split_return = await split(proofs, amount, output_data)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return {"error": str(exc)}
|
return {"error": str(exc)}
|
||||||
if not split_return:
|
if not split_return:
|
||||||
|
Reference in New Issue
Block a user