Commit Graph

176 Commits

Author SHA1 Message Date
callebtc
a518274f7e Nutshell cleanup wishlist (#332)
* fix keys

* fix tests

* backwards compatible api upgrade

* upgrade seems to work

* fix tests

* add deprecated api functions

* add more tests of backwards compat

* add test serialization for nut00

* remove a redundant test

* move mint and melt to new api

* mypy works

* CI: mypy --check-untyped-defs

* add deprecated router

* add hints and remove logs

* fix tests

* cleanup

* use new mint and melt endpoints

* tests passing?

* fix mypy

* make format

* make format

* make format

* commit

* errors gone

* save

* adjust the API

* store quotes in db

* make mypy happy

* add fakewallet settings

* remove LIGHTNING=True and pass quote id for melt

* format

* tests passing

* add CoreLightningRestWallet

* add macaroon loader

* add correct config

* preimage -> proof

* move wallet.status() to cli.helpers.print_status()

* remove statuses from tests

* remove

* make format

* Use httpx in deprecated wallet

* fix cln interface

* create invoice before quote

* internal transactions and deprecated api testing

* fix tests

* add deprecated API tests

* fastapi type hints break things

* fix duplicate wallet error

* make format

* update poetry in CI to 1.7.1

* precommit restore

* remove bolt11

* oops

* default poetry

* store fee reserve for melt quotes and refactor melt()

* works?

* make format

* test

* finally

* fix deprecated models

* rename v1 endpoints to bolt11

* raise restore and check to v1, bump version to 0.15.0

* add version byte to keyset id

* remove redundant fields in json

* checks

* generate bip32 keyset wip

* migrate old keysets

* load duplicate keys

* duplicate old keysets

* revert router changes

* add deprecated /check and /restore endpoints

* try except invalidate

* parse unit from derivation path, adjust keyset id calculation with bytes

* remove keyest id from functions again and rely on self.keyset_id

* mosts tests work

* mint loads multiple derivation paths

* make format

* properly print units

* fix tests

* wallet works with multiple units

* add strike wallet and choose backend dynamically

* fix mypy

* add get_payment_quote to lightning backends

* make format

* fix startup

* fix lnbitswallet

* fix tests

* LightningWallet -> LightningBackend

* remove comments

* make format

* remove msat conversion

* add Amount type

* fix regtest

* use melt_quote as argument for pay_invoice

* test old api

* fees in sats

* fix deprecated fees

* fixes

* print balance correctly

* internally index keyset response by int

* add pydantic validation to input models

* add timestamps to mint db

* store timestamps for invoices, promises, proofs_used

* fix wallet migration

* rotate keys correctly for testing

* remove print

* update latest keyset

* fix tests

* fix test

* make format

* make format with correct black version

* remove nsat and cheese

* test against deprecated mint

* fix tests?

* actually use env var

* mint run with env vars

* moar test

* cleanup

* simplify tests, load all keys

* try out testing with internal invoices

* fix internal melt test

* fix test

* deprecated checkfees expects appropriate fees

* adjust comment

* drop lightning table

* split migration for testing for now, remove it later

* remove unused lightning table

* skip_private_key -> skip_db_read

* throw error on migration error

* reorder

* fix migrations

* fix lnbits fee return value negative

* fix typo

* comments

* add type

* make format

* split must use correct amount

* fix tests

* test deprecated api with internal/external melts

* do not split if not necessary

* refactor

* fix test

* make format with new black

* cleanup and add comments

* add quote state check endpoints

* fix deprecated wallet response

* split -> swap endpoint

* make format

* add expiry to quotes, get quote endpoints, and adjust to nut review comments

* allow overpayment of melt

* add lightning wallet tests

* commiting to save

* fix tests a bit

* make format

* remove comments

* get mint info

* check_spendable default False, and return payment quote checking id

* make format

* bump version in pyproject

* update to /v1/checkstate

* make format

* fix mint api checks

* return witness on /v1/checkstate

* no failfast

* try fail-fast: false in ci.yaml

* fix db lookup

* clean up literals
2024-01-08 00:57:15 +01:00
callebtc
e6ed8fdb46 Mint: verify outputs first during mint (#379)
* verify outputs first during mint

* verify outputs during melt as well
2023-12-03 19:57:49 +01:00
callebtc
7d4ed959e3 Mint/verify_outputs_not_already_signed (#377)
* test output duplication in mint

* add comments to the db connection reuse
2023-12-03 19:26:07 +01:00
callebtc
34a2e7e5da Mint: invalidate and generate promises in single db transaction for split (#374)
* test for spending output again

* first gernerate (which can fail) then invalidate (db and memory)

* use external get_db_connection function to be compatible with existing Database class in LNbits
2023-12-02 22:54:28 +01:00
callebtc
0ec3af9bf1 Test/mint_add_verification_balance (#373)
* add verification balance

* verify input and outputs amounts before flow

* adjust tests
2023-12-02 18:55:19 +01:00
callebtc
fa5193cd8f index on db and read spent proofs from db (#370)
* index on db and read spent proofs from db

* add benchmark for testing

* remove benchmark

* add option to disable cached secrets

* disable python 3.9 tests
2023-11-26 06:07:38 -03:00
callebtc
bff30d493d tests failing? (#369) 2023-11-24 15:26:42 -03:00
callebtc
22e0ad4b57 use specific keyset for returned change (#351) 2023-10-29 08:57:25 -03:00
callebtc
0490f20932 Wallet: Lightning interface (#318)
* mint does not start yet

* fix import

* revert mint db migrations

* handle zero fee case

* cli: adjust fee message

* wallet: replace requests with httpx

* clean up

* rename http client decorator

* fix pending check in main, todo: TEST PROXIES WITH HTTPX

* fix up

* use httpx for nostr as well

* update packages to same versions as https://github.com/lnbits/lnbits/pull/1609/files

* fix proof deserialization

* check for string

* tests passing

* adjust wallet api tests

* lockfile

* add correct responses to Lightning interface and delete melt_id for proofs for which the payent has failed

* fix create_invoice checking_id response

* migrations atomic

* proofs are stored automatically when created

* make format

* use bolt11 lib

* stricter type checking

* add fee response to payments

* assert fees in test_melt

* test that mint_id and melt_id is stored correctly in proofs and proofs_used

* remove traces

* refactor: Lightning interface into own file and LedgerCrud with typing

* fix tests

* fix payment response

* rename variable
2023-10-21 14:38:16 +02:00
callebtc
8a4813aee6 Zero amount invoices: throw error before attempting a payment (#349)
* Zero amount invoices: throw error before attempting a payment

* better naming of fee variables during melt
2023-10-18 16:18:57 +02:00
callebtc
cfab668e54 Mint: Allow 0-valued amounts for blank outputs (#348)
* allow 0 amount for blank outputs

* fix precommit as well
2023-10-18 15:18:07 +02:00
callebtc
21bb42f294 fees were not returned if the fees were zero (#345) 2023-10-15 15:04:33 +02:00
callebtc
1f8d328354 more type hints (#330) 2023-09-25 00:47:16 +02:00
callebtc
638324940a Tests: Test with LIGHTNING=True and refactor mint (#326)
* refactor mint verification

* test with lightning=true

* rename proofs_used to secrets_used and refactor

* test with lightning

* spelling fixes
2023-09-24 14:35:13 +02:00
callebtc
f1b621fa90 HTLCs (#325)
* add htlc files

* refactor mint into several components

* add hash lock signatures

* add refund signature checks

* simplify hash lock signature check

* clean up
2023-09-23 19:08:38 +02:00
callebtc
6282e0a22a [Wallet/Mint] DLEQ proofs (#175)
* produce dleq

* start working on verification

* wip dleq

* Use C_ instead of C in verify DLEQ! (#176)

* Fix comments (DLEQ sign error)
* Fix alice_verify_dleq in d_dhke.py
* Fix_generate_promise in ledger.py
* Fix verify_proofs_dleq in wallet.py

* Fix: invalid public key (#182)

* Use C_ instead of C in verify DLEQ!

* Fix comments (DLEQ sign error)
* Fix alice_verify_dleq in d_dhke.py
* Fix_generate_promise in ledger.py
* Fix verify_proofs_dleq in wallet.py

* Fix: invalid public key

* Exception: Mint Error: invalid public key

* Update cashu/wallet/wallet.py

---------

Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>

* Update cashu/core/b_dhke.py

* Update tests/test_cli.py

* verify all constructed proofs

* dleq upon receive

* serialize without dleq

* all tests passing

* make format

* remove print

* remove debug

* option to send with dleq

* add tests

* fix test

* deterministic p in step2_dleq and fix mypy error for hash_to_curve

* test crypto/hash_e and crypto/step2_bob_dleq

* rename A to K in b_dhke.py and test_alice_verify_dleq

* rename tests

* make format

* store dleq in mint db (and readd balance view)

* remove `r` from dleq in tests

* add pending output

* make format

* works with pre-dleq mints

* fix comments

* make format

* fix some tests

* fix last test

* test serialize dleq fix

* flake

* flake

* keyset.id must be str

* fix test decorators

* start removing the duplicate fields from the dleq

* format

* remove print

* cleanup

* add type anotations to dleq functions

* remove unnecessary fields from BlindedSignature

* tests not working yet

* spelling mistakes

* spelling mistakes

* fix more spelling mistakes

* revert to normal

* add comments

* bdhke: generalize hash_e

* remove P2PKSecret changes

* revert tests for P2PKSecret

* revert tests

* revert test fully

* revert p2pksecret changes

* refactor proof invalidation

* store dleq proofs in wallet db

* make mypy happy

---------

Co-authored-by: moonsettler <moonsettler@protonmail.com>
2023-09-23 19:06:37 +02:00
callebtc
a1802b2d81 Refactor: P2PKSecret (#323)
* port changes from dleq branch

* adjust tests

* fix tag serialization

* refactor p2pk and secrets into a new Protocol class

* clean up code

* refactor p2pk types from base

* test second refund pubkey
2023-09-21 14:58:42 +02:00
dni ⚡
88393fa4c4 [DEV] add ruff and remove isort and flake (#300)
* [DEV] add ruff and remove isort and flake
- precommit
- workflow
- Makefile

updated black

* configure black to use default line-length

* reformat to 88 chars line-length

* fix ugly comments
2023-08-24 09:47:47 +02:00
dni ⚡
e3ed00640f FEAT: Add flake8 (#267)
* init flake8

* exclude nostr client, and add ds_store to gitignore

* fix flake8 F811 issue, redefinition of unused variables

* add flake8 to workflow

* F401 unused imports

* F541 f-string is missing placeholders

* E501 line too long > 150 characters

* E722 no bare except

* E402 module level import not at top of file

* F405 no star imports

* E712 comparison to False should be 'if cond is False:'

* F841 local variable is assigned to but never used

* E266 too many leading '#' for block comment

* E265, E261

* E713 test for membership should be 'not in'

* E711, E741

E741 ambiguous variable name 'l'
E711 comparison to None should be 'if cond is None:'

* flake config

* isort

* refactor makefile flake8 usage

* reflaking the rebase

* black

* fix tests?

* black

* fix line lenght it test_cli

* sort out makefile

* fix strings

* reintroduce black-check

* reflake and mypy

* isort

* Update cashu/wallet/wallet.py

Co-authored-by: Angus Pearson <angus@toaster.cc>

* Update cashu/mint/ledger.py

Co-authored-by: Angus Pearson <angus@toaster.cc>

---------

Co-authored-by: Angus Pearson <angus@toaster.cc>
2023-07-28 18:42:16 +02:00
callebtc
3d676dd35f Add custom error types (#290)
* add error types

* better subclassing

* add response models

* fix comments

* add response_description to mint endpoints
2023-07-25 23:26:50 +02:00
callebtc
b196c34427 [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
2023-07-25 11:13:20 +02:00
callebtc
0b2468914d Determinstic secrets / ecash restore (#131)
* first working version but some sats go missing

* back at it

* make format

* restore to main

* move mint database

* fix some tests

* make format

* remove old _construct_outputs we reintroduced in merge with main

* add type annotations

* add wallet private key to tests

* wallet: load proofs

* fix tests

* _generate_secrets with deterministic generation (temporary)

* allow wallet initialization with custom private key

* add pk to wallet api test

* mint scope=module

* remove private_key from test_wallet.py to see if it helps with the github tests

* readd private keys to tests

* workflow without env

* add more private key!

* readd env

* ledger scope session

* add default private key for testing

* generate private keys if not available

* testing

* its working!!!

* first iteration of bip32 working

* get mint info and add many type annotations

* tests

* fix tests with bip32

* restore from multiple mints

* disable profiler

* make format

* failed POST /mint do not increment secret counter

* store derivation path in each token

* fix tests

* refactor migrations so private keys can be generated by the wallet with .with_db() classmethod

* start fixing tests

* all tests passing except those that need to set a specific private key

* bip39 mnemonic to seed - with db but restore doesnt work yet with custom seed

* mnemonic restore works

* enter mnemonic in cli

* fix tests to use different mnemonic

* properly ask user for seed input

* tests: dont ask for inputs

* try to fix tests

* fix cashu -d

* fixing

* bump version and add more text to mnemonic enter

* add more comments

* add many more comments and type annotations in the wallet

* dont print generated mnemonic and dont wait for input

* fix test

* does this fix tests?

* sigh....

* make format

* do not restore from an initialized wallet

* fix mnemonics

* fix nitpicks

* print wallet name if nonstandard wallet

* fix merge error and remove comments

* poetry lock and requirements

* remove unused code

* fix tests

* mnemonic.lower() and add keyset id if not present for backwards compat

* edit comment
2023-07-24 13:42:56 +02:00
callebtc
337456333e P2PK: Sighash flags and multisig ecash because why not (#284)
* refactor split

* add sigflag to Secret base model

* add better comments

* add comments

* add witnesses inside /split

* more comments

* sign outputs if proofs indicate it

* add signature to outputs but mint does not check it yet

* multiple scriptsigs

* signatures on outputs

* add multisig tests

* rename test

* timelock -> locktime

* move parameters to tags

* convert back to List[List[str]]

* errors are back!

* fix all the stuff

* more testing

* make format
2023-07-18 21:38:31 +02:00
callebtc
1b6c68bc47 invalidate proofs before unsetting pending state (#288) 2023-07-18 15:17:15 +02:00
callebtc
73b015b642 Nut07/proof pending (#277)
* add pending state

* proofs spendable check and tests

* bump version to 0.12.3

* remove sleep for testing

* comment clarify

* use list comprehension in pending list
2023-07-08 22:50:17 +02:00
callebtc
01d498309b [Wallet/mint] P2PK with timelocks (#270)
* p2pk with nostr privatekey and timelocks

* add p2pk

* fix test

* fix test with custom secret

* sign whole split transaction

* p2pk signature now commits to entire secret and thus to a nonce

* use schnorr signatures

* revamp P2SH and P2PK with new Secret model

* test p2pk

* add comments

* add nostr private key to tests

* fix nostr receive

* make format

* test redemption after timelock

* refactor Server.serialize()

* sign sha256(secret)

* add optional refund pubkey that triggers after timelock

* use nostr private key for now (including nsec parser)

* use nostr private key and fix tests

* bump version to 0.12.2
2023-07-02 01:56:05 +02:00
callebtc
a3e67d21aa Use m̶u̶l̶t̶i̶p̶r̶o̶c̶e̶s̶s̶i̶n̶g̶ asyncio locks instead of db locks (#256)
* use mp locks instead of db locks

* replace mp.Lock with asyncio.Lock

* make format

* push

* remove db connection (and lock) and delete asyncio locks
2023-06-23 19:10:32 +02:00
callebtc
12bc0c23cb [Mint] Fix: check keyset id in mint and allow custom split amounts for LIGHTNING=False (#253)
* check keyset existence and return error

* fix split specific amounts for LIGHTNING=False

* wallet loading

* remove wallet balance checks
2023-06-18 14:00:34 +02:00
xphade
21069fb61a Implement dynamic amount of tokens for change (#223)
With the recent update to NUT-08, we can ensure that the amount of blank
outputs is always enough to cover any overpaid lightning fees. This
change implements this functionality for both the wallet and the mint.
The mint updateis backwards-compatible with respect to old wallets.
2023-05-23 17:40:48 +02:00
callebtc
f0735cfc4a fix postgres views, closes #206 (#222) 2023-05-16 22:14:13 +02:00
callebtc
d0283670d3 Refactor Compat and Database for LNbits comaptibility (#219)
* remove changes to Compat and Database so its compatible with the classes from LNbits

* ledger.py: Annotate Connection type

* fix invoice memp
2023-05-15 22:15:56 +02:00
callebtc
6272aee189 make format (#218) 2023-05-15 17:11:02 +02:00
Semisol
d4c7a15e89 add transactions and locking for certain operations (#217) 2023-05-15 17:09:28 +02:00
callebtc
ddd8cebdb0 more logging (#216) 2023-05-15 00:37:46 +02:00
callebtc
4789e2a504 Mint/moremore_traces (#215)
* more trace logging

* a lot more logging
2023-05-15 00:15:07 +02:00
callebtc
352f77f9e9 more traces (#214) 2023-05-14 23:39:03 +02:00
callebtc
68d9020cb3 mint: add trace logs (#213) 2023-05-14 23:06:35 +02:00
sihamon
e7df2028fa Allow to set maximum peg in/out for mint (#209)
* Allow to set maximum peg in/out for mint

* Make format

* remove duplicate error

* move business logic to ledger

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
2023-05-13 20:41:24 +02:00
callebtc
c31f008fb4 [Mint] load the latest keyset from db based on the derivation path (#193)
* load the latest keyset from db based on the derivation path and not the on-line generated keyset id (necessary because we changed the keyset id derivation algorithm)

* better comments

* generate keys for keyset from db
2023-05-07 17:47:38 +02:00
callebtc
e9f33337db Fix/nut 04 payment hash (#191)
* payment_hash -> hash

* add aes encryption

* urlsafe base64 for free

* move files to crypto

* use random hash instead of encryption

* get rid of useless code

* simplify
2023-05-04 00:12:18 +02:00
calle
89fb2787e6 Python/relative_import (#186)
* relative import all the things

* make format

* add __init__.py to cli/

* fix mypy errors

* get rid of more mypy

* mypy fix crud.py

* fix another mypy error
2023-05-01 22:39:23 +02:00
calle
f25e36f333 improve logging for keyests (#184) 2023-04-30 00:05:53 +02:00
calle
87f9241de1 [Mint] Fix key rotation (#177)
* rotate keys every 5 seconds

* every 10 seconds

* do not regenerate all past keys for each epoch cycle

* remove automatic rotation

* make format

* print to logger

* rephrase print
2023-04-26 20:20:17 +02:00
calle
6b631111d1 Fix/ln unsuccessful payment (#159)
* throws error for unsuccessful payment so that wallets get error message

* bump to 0.11.2
2023-04-03 20:52:14 +02:00
calle
47f886487c nut-09 mint info (#155)
* nut-09 mint info

* bump to 0.11.1

* make format
2023-04-01 00:47:27 +02:00
calle
70828b59d5 NUT-08 Lightning fee return (#114)
* skeleton

* works

* comments

* docsctrings ledger.py

* bump version to 0.10.

* fixes mypy stuff

* make format

* remove unwanted changes
2023-03-16 01:28:33 +01:00
calle
f3a31fd09e use settings module (#136)
* use settings module
2023-03-16 00:59:50 +01:00
calle
e8011a0f78 Type annotations for bdhke (#140)
* annotate dhke

* raise for errors

* remove backwards compatibility

* remove old code

* remove test code
2023-03-09 17:45:50 +01:00
calle
a7eef68c97 wallet: load keys without keysets (#123)
* wallet: load keys without keysets

* fix cli
2023-03-05 02:51:20 +01:00
calle
23f2b58430 wallet: mint specific amounts (#121)
* wallet: mint specific amounts

* make format
2023-03-04 21:49:39 +01:00