use compressed secret

This commit is contained in:
callebtc
2022-09-22 11:52:40 +03:00
parent edaedf01b4
commit 43e7889cab
9 changed files with 85 additions and 46 deletions

View File

@@ -1,4 +1,4 @@
DEBUG = true DEBUG=FALSE
CASHU_DIR=~/.cashu CASHU_DIR=~/.cashu

View File

@@ -6,8 +6,8 @@ from pydantic import BaseModel
class Proof(BaseModel): class Proof(BaseModel):
amount: int amount: int
C: str
secret: str secret: str
C: str
reserved: bool = False # whether this proof is reserved for sending reserved: bool = False # whether this proof is reserved for sending
send_id: str = "" # unique ID of send attempt send_id: str = "" # unique ID of send attempt
time_created: str = "" time_created: str = ""
@@ -27,16 +27,21 @@ class Proof(BaseModel):
@classmethod @classmethod
def from_dict(cls, d: dict): def from_dict(cls, d: dict):
assert "secret" in d, "no secret in proof"
assert "amount" in d, "no amount in proof"
return cls( return cls(
amount=d["amount"], amount=d.get("amount"),
C=d["C"], C=d.get("C"),
secret=d["secret"], secret=d.get("secret"),
reserved=d["reserved"] or False, reserved=d.get("reserved") or False,
send_id=d["send_id"] or "", send_id=d.get("send_id") or "",
time_created=d["time_created"] or "", time_created=d.get("time_created") or "",
time_reserved=d["time_reserved"] or "", time_reserved=d.get("time_reserved") or "",
) )
def to_dict(self):
return dict(amount=self.amount, secret=self.secret, C=self.C)
def __getitem__(self, key): def __getitem__(self, key):
return self.__getattribute__(key) return self.__getattribute__(key)

View File

@@ -8,13 +8,8 @@ import requests
from core.settings import LNBITS_ENDPOINT, LNBITS_KEY from core.settings import LNBITS_ENDPOINT, LNBITS_KEY
from .base import ( from .base import (InvoiceResponse, PaymentResponse, PaymentStatus,
InvoiceResponse, StatusResponse, Wallet)
PaymentResponse,
PaymentStatus,
StatusResponse,
Wallet,
)
class LNbitsWallet(Wallet): class LNbitsWallet(Wallet):

View File

@@ -11,12 +11,8 @@ from secp256k1 import PublicKey
import core.settings as settings import core.settings as settings
from core.base import CheckPayload, MeltPayload, MintPayloads, SplitPayload from core.base import CheckPayload, MeltPayload, MintPayloads, SplitPayload
from core.settings import ( from core.settings import (CASHU_DIR, MINT_PRIVATE_KEY, MINT_SERVER_HOST,
CASHU_DIR, MINT_SERVER_PORT)
MINT_PRIVATE_KEY,
MINT_SERVER_HOST,
MINT_SERVER_PORT,
)
from lightning import WALLET from lightning import WALLET
from mint.ledger import Ledger from mint.ledger import Ledger
from mint.migrations import m001_initial from mint.migrations import m001_initial

View File

@@ -13,14 +13,9 @@ from core.secp import PrivateKey, PublicKey
from core.settings import LIGHTNING, MAX_ORDER from core.settings import LIGHTNING, MAX_ORDER
from core.split import amount_split from core.split import amount_split
from lightning import WALLET from lightning import WALLET
from mint.crud import ( from mint.crud import (get_lightning_invoice, get_proofs_used,
get_lightning_invoice, invalidate_proof, store_lightning_invoice,
get_proofs_used, store_promise, update_lightning_invoice)
invalidate_proof,
store_lightning_invoice,
store_promise,
update_lightning_invoice,
)
class Ledger: class Ledger:

46
poetry.lock generated
View File

@@ -323,6 +323,25 @@ docs = ["alabaster (==0.7.12)", "autodocsumm (==0.2.9)", "sphinx (==5.1.1)", "sp
lint = ["flake8 (==5.0.4)", "flake8-bugbear (==22.9.11)", "mypy (==0.971)", "pre-commit (>=2.4,<3.0)"] lint = ["flake8 (==5.0.4)", "flake8-bugbear (==22.9.11)", "mypy (==0.971)", "pre-commit (>=2.4,<3.0)"]
tests = ["pytest", "pytz", "simplejson"] tests = ["pytest", "pytz", "simplejson"]
[[package]]
name = "mypy"
version = "0.971"
description = "Optional static typing for Python"
category = "dev"
optional = false
python-versions = ">=3.6"
[package.dependencies]
mypy-extensions = ">=0.4.3"
tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""}
typed-ast = {version = ">=1.4.0,<2", markers = "python_version < \"3.8\""}
typing-extensions = ">=3.10"
[package.extras]
dmypy = ["psutil (>=4.0)"]
python2 = ["typed-ast (>=1.4.0,<2)"]
reports = ["lxml"]
[[package]] [[package]]
name = "mypy-extensions" name = "mypy-extensions"
version = "0.4.3" version = "0.4.3"
@@ -690,7 +709,7 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>=
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.7" python-versions = "^3.7"
content-hash = "68f6f6e33100fdd1bd86879f8c146a96a5ee822b0a6e8b9ec27205f6eab1fcd8" content-hash = "a4553430bb7df5a66a7006d638986509d065e931ee5009ded5244eaa017475b2"
[metadata.files] [metadata.files]
anyio = [ anyio = [
@@ -915,6 +934,31 @@ marshmallow = [
{file = "marshmallow-3.18.0-py3-none-any.whl", hash = "sha256:35e02a3a06899c9119b785c12a22f4cda361745d66a71ab691fd7610202ae104"}, {file = "marshmallow-3.18.0-py3-none-any.whl", hash = "sha256:35e02a3a06899c9119b785c12a22f4cda361745d66a71ab691fd7610202ae104"},
{file = "marshmallow-3.18.0.tar.gz", hash = "sha256:6804c16114f7fce1f5b4dadc31f4674af23317fcc7f075da21e35c1a35d781f7"}, {file = "marshmallow-3.18.0.tar.gz", hash = "sha256:6804c16114f7fce1f5b4dadc31f4674af23317fcc7f075da21e35c1a35d781f7"},
] ]
mypy = [
{file = "mypy-0.971-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:f2899a3cbd394da157194f913a931edfd4be5f274a88041c9dc2d9cdcb1c315c"},
{file = "mypy-0.971-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:98e02d56ebe93981c41211c05adb630d1d26c14195d04d95e49cd97dbc046dc5"},
{file = "mypy-0.971-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:19830b7dba7d5356d3e26e2427a2ec91c994cd92d983142cbd025ebe81d69cf3"},
{file = "mypy-0.971-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:02ef476f6dcb86e6f502ae39a16b93285fef97e7f1ff22932b657d1ef1f28655"},
{file = "mypy-0.971-cp310-cp310-win_amd64.whl", hash = "sha256:25c5750ba5609a0c7550b73a33deb314ecfb559c350bb050b655505e8aed4103"},
{file = "mypy-0.971-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:d3348e7eb2eea2472db611486846742d5d52d1290576de99d59edeb7cd4a42ca"},
{file = "mypy-0.971-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:3fa7a477b9900be9b7dd4bab30a12759e5abe9586574ceb944bc29cddf8f0417"},
{file = "mypy-0.971-cp36-cp36m-win_amd64.whl", hash = "sha256:2ad53cf9c3adc43cf3bea0a7d01a2f2e86db9fe7596dfecb4496a5dda63cbb09"},
{file = "mypy-0.971-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:855048b6feb6dfe09d3353466004490b1872887150c5bb5caad7838b57328cc8"},
{file = "mypy-0.971-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:23488a14a83bca6e54402c2e6435467a4138785df93ec85aeff64c6170077fb0"},
{file = "mypy-0.971-cp37-cp37m-win_amd64.whl", hash = "sha256:4b21e5b1a70dfb972490035128f305c39bc4bc253f34e96a4adf9127cf943eb2"},
{file = "mypy-0.971-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:9796a2ba7b4b538649caa5cecd398d873f4022ed2333ffde58eaf604c4d2cb27"},
{file = "mypy-0.971-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:5a361d92635ad4ada1b1b2d3630fc2f53f2127d51cf2def9db83cba32e47c856"},
{file = "mypy-0.971-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:b793b899f7cf563b1e7044a5c97361196b938e92f0a4343a5d27966a53d2ec71"},
{file = "mypy-0.971-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:d1ea5d12c8e2d266b5fb8c7a5d2e9c0219fedfeb493b7ed60cd350322384ac27"},
{file = "mypy-0.971-cp38-cp38-win_amd64.whl", hash = "sha256:23c7ff43fff4b0df93a186581885c8512bc50fc4d4910e0f838e35d6bb6b5e58"},
{file = "mypy-0.971-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:1f7656b69974a6933e987ee8ffb951d836272d6c0f81d727f1d0e2696074d9e6"},
{file = "mypy-0.971-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:d2022bfadb7a5c2ef410d6a7c9763188afdb7f3533f22a0a32be10d571ee4bbe"},
{file = "mypy-0.971-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:ef943c72a786b0f8d90fd76e9b39ce81fb7171172daf84bf43eaf937e9f220a9"},
{file = "mypy-0.971-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:d744f72eb39f69312bc6c2abf8ff6656973120e2eb3f3ec4f758ed47e414a4bf"},
{file = "mypy-0.971-cp39-cp39-win_amd64.whl", hash = "sha256:77a514ea15d3007d33a9e2157b0ba9c267496acf12a7f2b9b9f8446337aac5b0"},
{file = "mypy-0.971-py3-none-any.whl", hash = "sha256:0d054ef16b071149917085f51f89555a576e2618d5d9dd70bd6eea6410af3ac9"},
{file = "mypy-0.971.tar.gz", hash = "sha256:40b0f21484238269ae6a57200c807d80debc6459d444c0489a102d7c6a75fa56"},
]
mypy-extensions = [ mypy-extensions = [
{file = "mypy_extensions-0.4.3-py2.py3-none-any.whl", hash = "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d"}, {file = "mypy_extensions-0.4.3-py2.py3-none-any.whl", hash = "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d"},
{file = "mypy_extensions-0.4.3.tar.gz", hash = "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"}, {file = "mypy_extensions-0.4.3.tar.gz", hash = "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"},

View File

@@ -36,6 +36,9 @@ secp256k1 = "^0.14.0"
black = {version = "^22.8.0", allow-prereleases = true} black = {version = "^22.8.0", allow-prereleases = true}
isort = "^5.10.1" isort = "^5.10.1"
[tool.poetry.group.dev.dependencies]
mypy = "^0.971"
[build-system] [build-system]
requires = ["poetry-core>=1.0.0"] requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api" build-backend = "poetry.core.masonry.api"

View File

@@ -5,7 +5,6 @@ import base64
import json import json
import math import math
from datetime import datetime from datetime import datetime
from functools import wraps from functools import wraps
from itertools import groupby from itertools import groupby
from operator import itemgetter from operator import itemgetter
@@ -18,7 +17,7 @@ from core.base import Proof
from core.bolt11 import Invoice from core.bolt11 import Invoice
from core.helpers import fee_reserve from core.helpers import fee_reserve
from core.migrations import migrate_databases from core.migrations import migrate_databases
from core.settings import CASHU_DIR, LIGHTNING, MINT_URL from core.settings import CASHU_DIR, DEBUG, LIGHTNING, MINT_URL
from wallet import migrations from wallet import migrations
from wallet.crud import get_reserved_proofs from wallet.crud import get_reserved_proofs
from wallet.wallet import Wallet as Wallet from wallet.wallet import Wallet as Wallet

View File

@@ -1,25 +1,21 @@
import base64 import base64
import json import json
import random import random
import secrets as scrts
import uuid import uuid
from typing import List from typing import List
import requests import requests
import core.b_dhke as b_dhke import core.b_dhke as b_dhke
from core.base import ( from core.base import (BlindedMessage, BlindedSignature, CheckPayload,
BlindedMessage, MeltPayload, MintPayloads, Proof, SplitPayload)
BlindedSignature,
CheckPayload,
MeltPayload,
MintPayloads,
Proof,
SplitPayload,
)
from core.db import Database from core.db import Database
from core.secp import PublicKey from core.secp import PublicKey
from core.settings import DEBUG
from core.split import amount_split from core.split import amount_split
from wallet.crud import get_proofs, invalidate_proof, store_proof, update_proof_reserved from wallet.crud import (get_proofs, invalidate_proof, store_proof,
update_proof_reserved)
class LedgerAPI: class LedgerAPI:
@@ -55,6 +51,10 @@ class LedgerAPI:
proofs.append(proof) proofs.append(proof)
return proofs return proofs
def _generate_secret(self, randombits=128):
"""Returns base64 encoded random string."""
return scrts.token_urlsafe(randombits // 8)
def request_mint(self, amount): def request_mint(self, amount):
"""Requests a mint from the server and returns Lightning invoice.""" """Requests a mint from the server and returns Lightning invoice."""
r = requests.get(self.url + "/mint", params={"amount": amount}) r = requests.get(self.url + "/mint", params={"amount": amount})
@@ -66,7 +66,7 @@ class LedgerAPI:
secrets = [] secrets = []
rs = [] rs = []
for amount in amounts: for amount in amounts:
secret = str(random.getrandbits(128)) secret = self._generate_secret()
secrets.append(secret) secrets.append(secret)
B_, r = b_dhke.step1_bob(secret) B_, r = b_dhke.step1_bob(secret)
rs.append(r) rs.append(r)
@@ -94,7 +94,7 @@ class LedgerAPI:
secrets = [] secrets = []
payloads: MintPayloads = MintPayloads() payloads: MintPayloads = MintPayloads()
for output_amt in fst_outputs + snd_outputs: for output_amt in fst_outputs + snd_outputs:
secret = str(random.getrandbits(128)) secret = self._generate_secret()
B_, r = b_dhke.step1_bob(secret) B_, r = b_dhke.step1_bob(secret)
secrets.append((r, secret)) secrets.append((r, secret))
payload: BlindedMessage = BlindedMessage( payload: BlindedMessage = BlindedMessage(
@@ -191,7 +191,9 @@ class Wallet(LedgerAPI):
@staticmethod @staticmethod
async def serialize_proofs(proofs: List[Proof]): async def serialize_proofs(proofs: List[Proof]):
proofs_serialized = [p.dict() for p in proofs] proofs_serialized = [p.to_dict() for p in proofs]
if DEBUG:
print(proofs_serialized)
token = base64.urlsafe_b64encode( token = base64.urlsafe_b64encode(
json.dumps(proofs_serialized).encode() json.dumps(proofs_serialized).encode()
).decode() ).decode()