mirror of
https://github.com/aljazceru/nutshell.git
synced 2026-02-07 09:44:20 +01:00
p2sh working
This commit is contained in:
@@ -1,17 +1,25 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from environs import Env # type: ignore
|
||||
from loguru import logger
|
||||
|
||||
env = Env()
|
||||
env.read_env()
|
||||
|
||||
ENV_FILE = os.path.join(str(Path.home()), ".cashu", ".env")
|
||||
if not os.path.isfile(ENV_FILE):
|
||||
ENV_FILE = os.path.join(os.getcwd(), ".env")
|
||||
if os.path.isfile(ENV_FILE):
|
||||
logger.info("Using .env: " + ENV_FILE)
|
||||
env.read_env(ENV_FILE)
|
||||
else:
|
||||
env.read_env()
|
||||
|
||||
DEBUG = env.bool("DEBUG", default=False)
|
||||
if not DEBUG:
|
||||
sys.tracebacklimit = 0
|
||||
|
||||
CASHU_DIR = env.str("CASHU_DIR", default="~/.cashu")
|
||||
CASHU_DIR = CASHU_DIR.replace("~", str(Path.home()))
|
||||
CASHU_DIR = env.str("CASHU_DIR", default=os.path.join(str(Path.home()), ".cashu"))
|
||||
assert len(CASHU_DIR), "CASHU_DIR not defined"
|
||||
|
||||
LIGHTNING = env.bool("LIGHTNING", default=True)
|
||||
|
||||
@@ -95,6 +95,11 @@ class Ledger:
|
||||
return b_dhke.verify(secret_key, C, proof.secret)
|
||||
|
||||
def _verify_script(self, idx: int, proof: Proof):
|
||||
"""
|
||||
Verify bitcoin script in proof.script commited to by <address> in proof.secret.
|
||||
proof.secret format: P2SH:<address>:<secret>
|
||||
"""
|
||||
# if no script is given
|
||||
if (
|
||||
proof.script is None
|
||||
or proof.script.script is None
|
||||
@@ -106,23 +111,17 @@ class Ledger:
|
||||
else:
|
||||
# secret indicates no script, so treat script as valid
|
||||
return True
|
||||
# execute and verify P2SH
|
||||
txin_p2sh_address, valid = verify_script(
|
||||
proof.script.script, proof.script.signature
|
||||
)
|
||||
if valid:
|
||||
# check if secret commits to script address
|
||||
# format: P2SH:<address>:<secret>
|
||||
assert len(proof.secret.split(":")) == 3, "secret format wrong"
|
||||
assert len(proof.secret.split(":")) == 3, "secret format wrong."
|
||||
assert proof.secret.split(":")[1] == str(
|
||||
txin_p2sh_address
|
||||
), f"secret does not contain P2SH address: {proof.secret.split(':')[1]}!={txin_p2sh_address}"
|
||||
# print(
|
||||
# f"Script {proof.script.script.__repr__()} {'valid' if valid else 'invalid'}."
|
||||
# )
|
||||
# if valid:
|
||||
# print(f"{idx}:P2SH:{txin_p2sh_address}")
|
||||
# print("proof.secret", proof.secret)
|
||||
# proof.secret = f"{idx}:P2SH:{txin_p2sh_address}"
|
||||
), f"secret does not contain correct P2SH address: {proof.secret.split(':')[1]}!={txin_p2sh_address}."
|
||||
return valid
|
||||
|
||||
def _verify_outputs(
|
||||
@@ -276,16 +275,15 @@ class Ledger:
|
||||
"""Consumes proofs and prepares new promises based on the amount split."""
|
||||
total = sum([p.amount for p in proofs])
|
||||
|
||||
# if not all([self._verify_secret_or_script(p) for p in proofs]):
|
||||
# raise Exception("can't use secret and script at the same time.")
|
||||
# Verify scripts
|
||||
if not all([self._verify_script(i, p) for i, p in enumerate(proofs)]):
|
||||
raise Exception("could not verify scripts.")
|
||||
# verify that amount is kosher
|
||||
self._verify_split_amount(amount)
|
||||
# verify overspending attempt
|
||||
if amount > total:
|
||||
raise Exception("split amount is higher than the total sum.")
|
||||
|
||||
# Verify scripts
|
||||
if not all([self._verify_script(i, p) for i, p in enumerate(proofs)]):
|
||||
raise Exception("script verification failed.")
|
||||
# Verify secret criteria
|
||||
if not all([self._verify_secret_criteria(p) for p in proofs]):
|
||||
raise Exception("secrets do not match criteria.")
|
||||
@@ -309,5 +307,6 @@ class Ledger:
|
||||
prom_fst, prom_snd = await self._generate_promises(
|
||||
outs_fst, B_fst
|
||||
), await self._generate_promises(outs_snd, B_snd)
|
||||
# verify amounts in produced proofs
|
||||
self._verify_equation_balanced(proofs, prom_fst + prom_snd)
|
||||
return prom_fst, prom_snd
|
||||
|
||||
@@ -2,7 +2,7 @@ import asyncio
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from cashu.core.settings import CASHU_DIR
|
||||
from cashu.core.settings import CASHU_DIR, LIGHTNING
|
||||
from cashu.lightning import WALLET
|
||||
from cashu.mint.migrations import m001_initial
|
||||
|
||||
@@ -13,13 +13,14 @@ async def load_ledger():
|
||||
await asyncio.wait([m001_initial(ledger.db)])
|
||||
await ledger.load_used_proofs()
|
||||
|
||||
error_message, balance = await WALLET.status()
|
||||
if error_message:
|
||||
logger.warning(
|
||||
f"The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'",
|
||||
RuntimeWarning,
|
||||
)
|
||||
if LIGHTNING:
|
||||
error_message, balance = await WALLET.status()
|
||||
if error_message:
|
||||
logger.warning(
|
||||
f"The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'",
|
||||
RuntimeWarning,
|
||||
)
|
||||
logger.info(f"Lightning balance: {balance} sat")
|
||||
|
||||
logger.info(f"Lightning balance: {balance} sat")
|
||||
logger.info(f"Data dir: {CASHU_DIR}")
|
||||
logger.info("Mint started.")
|
||||
|
||||
Reference in New Issue
Block a user