[WIP] [NUTs] NUT-06 update: deprecate amount field in /split (#263)

* mint upgraded and still backwards compatible

* cleanup

* fix tests

* fix things

* add deprecated message to new struct

* fix test

* fix typo

* readd endpoint that got lost during merge

* version bump in pyproject.toml

* remove wallet backwards compatibility because it makes no sense

* comment for backwards compat

* fix comment
This commit is contained in:
callebtc
2023-07-25 11:13:20 +02:00
committed by GitHub
parent 27bc2bda06
commit b196c34427
7 changed files with 117 additions and 101 deletions

View File

@@ -22,6 +22,7 @@ from ..core.base import (
PostRestoreResponse,
PostSplitRequest,
PostSplitResponse,
PostSplitResponse_Deprecated,
)
from ..core.errors import CashuError
from ..core.settings import settings
@@ -208,7 +209,7 @@ async def check_fees(payload: CheckFeesRequest) -> CheckFeesResponse:
@router.post("/split", name="Split", summary="Split proofs at a specified amount")
async def split(
payload: PostSplitRequest,
) -> Union[CashuError, PostSplitResponse]:
) -> Union[CashuError, PostSplitResponse, PostSplitResponse_Deprecated]:
"""
Requetst a set of tokens with amount "total" to be split into two
newly minted sets with amount "split" and "total-split".
@@ -219,17 +220,36 @@ async def split(
logger.trace(f"> POST /split: {payload}")
assert payload.outputs, Exception("no outputs provided.")
try:
split_return = await ledger.split(
payload.proofs, payload.amount, payload.outputs
promises = await ledger.split(
proofs=payload.proofs, outputs=payload.outputs, amount=payload.amount
)
except Exception as exc:
return CashuError(code=0, error=str(exc))
if not split_return:
if not promises:
return CashuError(code=0, error="there was an error with the split")
frst_promises, scnd_promises = split_return
resp = PostSplitResponse(fst=frst_promises, snd=scnd_promises)
logger.trace(f"< POST /split: {resp}")
return resp
if payload.amount:
# BEGIN backwards compatibility < 0.13
# old clients expect two lists of promises where the second one's amounts
# sum up to `amount`. The first one is the rest.
# The returned value `promises` has the form [keep1, keep2, ..., send1, send2, ...]
# The sum of the sendx is `amount`. We need to split this into two lists and keep the order of the elements.
frst_promises: List[BlindedSignature] = []
scnd_promises: List[BlindedSignature] = []
scnd_amount = 0
for promise in promises[::-1]: # we iterate backwards
if scnd_amount < payload.amount:
scnd_promises.insert(0, promise) # and insert at the beginning
scnd_amount += promise.amount
else:
frst_promises.insert(0, promise) # and insert at the beginning
logger.trace(
f"Split into keep: {len(frst_promises)}: {sum([p.amount for p in frst_promises])} sat and send: {len(scnd_promises)}: {sum([p.amount for p in scnd_promises])} sat"
)
return PostSplitResponse_Deprecated(fst=frst_promises, snd=scnd_promises)
# END backwards compatibility < 0.13
else:
return PostSplitResponse(promises=promises)
@router.post(