-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discreet Log Contracts #576
Draft
lollerfirst
wants to merge
84
commits into
cashubtc:main
Choose a base branch
from
lollerfirst:dlc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
84 commits
Select commit
Hold shift + click to select a range
cc4aeb5
Models + Merkle function + Test draft
lollerfirst 97edb79
Merge remote-tracking branch 'origin/main' into dlc
lollerfirst 383e032
remove `test_mint_dlc`
lollerfirst 3f44d81
fix errors
lollerfirst 619c778
fix more errors
lollerfirst c22b800
merkle functions tests
lollerfirst d1fd1d7
making mypy happy
lollerfirst 043556b
SCT spending conditions
lollerfirst 7d22656
formatting errors
lollerfirst 5e11a99
Update cashu/core/crypto/dlc.py
lollerfirst a14707b
fix description `merkle_root`
lollerfirst 344cbff
Merge branch 'cashubtc:main' into dlc
lollerfirst 2dfdafe
secret generation
lollerfirst f45e5e5
fix broken import
lollerfirst 258d4ae
db add dlc_root and spending_conditions to proofs tables + related e…
lollerfirst 77b2631
fix error
lollerfirst aa1af77
move dlc from core to wallet
lollerfirst 8b134dd
move `add_witnessess_to_proofs` up to `wallet.py` for common use.
lollerfirst 4c67c3d
Merge remote-tracking branch 'origin/main' into dlc
lollerfirst e709af8
tests: swapping for locked, unlocked.
lollerfirst 08529b5
fix naive mistake
lollerfirst 32cc283
Better tests for dlc locked proofs spending validation
lollerfirst b0bfc0e
dlc funding token
lollerfirst 41ee5e1
fix selfpay
lollerfirst ea56b57
Merge remote-tracking branch 'origin/main' into dlc
lollerfirst 6a3e8d3
* mint DB add dlc table migration
lollerfirst 9a741cb
fix migration
lollerfirst 0b89fb7
verify threshold, verify funding amount and fees coverage.
lollerfirst 1bbca5a
embarassing error fix
lollerfirst fd12aa3
report index of proofs that failed verification
lollerfirst ffa6858
DlcFundingProof signature
lollerfirst 30ebc3f
Merge remote-tracking branch 'origin/main' into dlc
lollerfirst ff125d6
refactor: move dlc verification functions into `verification.py`, reg…
lollerfirst 4ce0b7b
funding proof signature fix
lollerfirst a5b147b
Database shenanigans
lollerfirst 1dd7abf
error fix
lollerfirst a01a77f
Removed `is_atomic`, `sign_dlc` with the first key of the active keys…
lollerfirst 8dc0c71
tests on `register_dlc` working
lollerfirst 932fa7a
Merge remote-tracking branch 'origin/main' into dlc
lollerfirst 10b6e9f
rename `DiscreteLogContract` to `DiscreetLogContract`, added `status_…
lollerfirst 0da2683
add test for `status_dlc`
lollerfirst cb7eea2
better threshold check + test
lollerfirst c6e06e7
settlement
lollerfirst 2ba5f5b
secret attestation verification
lollerfirst 6b1d04c
settlement database
lollerfirst c58cf9b
whitespace formatting
conduition 97684a6
rename DLCWitness -> SCTWitness
conduition 42964e7
raise errors instead of returning false in _verify_sct_spending_condi…
conduition 0b5c191
refactor handling of DLC input validation code
conduition 1f287d3
Fix linter errors
conduition e4463ac
Merge pull request #1 from conduition/dlc
lollerfirst 0c810fa
fix PostgreSQL's tantrum over BIT datatype
lollerfirst 162cd47
avoid re-settling already settled DLC
conduition 2a3049e
separate error types for registration/settlement responses
conduition 971b43e
update registration response to include funding proof keyset id
conduition 47ace2f
fix funding proof signature to match spec
conduition c38b2ed
Merge pull request #2 from conduition/dlc
lollerfirst 9213daf
Merge pull request #4 from conduition/dlc3
lollerfirst 18c5a34
test for dlc settlement and relative error fixes
lollerfirst a9fb148
Merge remote-tracking branch 'loller/dlc' into dlc2
conduition be81d78
update tests to reflect new response types
conduition 9c5bf34
Merge pull request #3 from conduition/dlc2
lollerfirst 62145e2
add leading slashes and `POST /v1/dlc/settle`
gudnuf a5f7590
Merge pull request #5 from gudnuf/dlc-fix-router
lollerfirst 3ed21e4
Merge remote-tracking branch 'upstream/main' into dlc
lollerfirst 0ad83a9
initial support for payouts
lollerfirst 9f10d34
dlc payouts: part 2
lollerfirst 0e35fbf
error fixes
lollerfirst ad65326
definition of `DlcPayoutWitness` before `DlcPayout`
lollerfirst 52729b2
fix more errors
lollerfirst 231eb25
fix for out of spec debts map
lollerfirst d561dbe
dlc payout tests + bug fixes
lollerfirst e35df9b
make format
lollerfirst 2b8fa8b
test fix
lollerfirst 39aa849
fix order of `sorted_merkle_hash`
lollerfirst cd2bf24
fix tests
lollerfirst c58947f
Merge remote-tracking branch 'origin/main' into dlc
lollerfirst 58aaee9
Merge remote-tracking branch 'origin' into dlc
lollerfirst a290313
Merge remote-tracking branch 'upstream/main' into dlc
lollerfirst 5b35eb7
corrections
lollerfirst 897141b
make format + remove weird SCTWitness clone
lollerfirst aa7e38e
remove more weird duplicates
lollerfirst 6e204bc
unused `proofs` in tests
lollerfirst a4084aa
fix some minor typos
lollerfirst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from hashlib import sha256 | ||
from typing import List, Optional, Tuple | ||
|
||
from secp256k1 import PrivateKey, PublicKey | ||
|
||
|
||
def sorted_merkle_hash(left: bytes, right: bytes) -> bytes: | ||
'''Sorts `left` and `right` in non-ascending order and | ||
computes the hash of their concatenation | ||
''' | ||
if right < left: | ||
left, right = right, left | ||
return sha256(left+right).digest() | ||
|
||
|
||
def merkle_root( | ||
leaf_hashes: List[bytes], | ||
track_branch: Optional[int] = None | ||
) -> Tuple[bytes, Optional[List[bytes]]]: | ||
'''Computes the root of a list of leaf hashes | ||
if `track_branch` is set, extracts the hashes for the branch that leads | ||
to `leaf_hashes[track_branch]` | ||
''' | ||
if track_branch is not None: | ||
if len(leaf_hashes) == 0: | ||
return b"", [] | ||
elif len(leaf_hashes) == 1: | ||
return leaf_hashes[0], [] | ||
else: | ||
split = len(leaf_hashes) // 2 | ||
left, left_branch_hashes = merkle_root(leaf_hashes[:split], | ||
track_branch if track_branch < split else None) | ||
right, right_branch_hashes = merkle_root(leaf_hashes[split:], | ||
track_branch-split if track_branch >= split else None) | ||
branch_hashes = (left_branch_hashes if | ||
track_branch < split else right_branch_hashes) | ||
hashh = sorted_merkle_hash(left, right) | ||
# Needed to pass mypy checks | ||
assert branch_hashes is not None, "merkle_root fail: branch_hashes == None" | ||
branch_hashes.append(right if track_branch < split else left) | ||
return hashh, branch_hashes | ||
else: | ||
if len(leaf_hashes) == 0: | ||
return b"", None | ||
elif len(leaf_hashes) == 1: | ||
return leaf_hashes[0], None | ||
else: | ||
split = len(leaf_hashes) // 2 | ||
left, _ = merkle_root(leaf_hashes[:split], None) | ||
right, _ = merkle_root(leaf_hashes[split:], None) | ||
hashh = sorted_merkle_hash(left, right) | ||
return hashh, None | ||
|
||
def merkle_verify(root: bytes, leaf_hash: bytes, proof: List[bytes]) -> bool: | ||
'''Verifies that `leaf_hash` belongs to a merkle tree | ||
that has `root` as root | ||
''' | ||
h = leaf_hash | ||
for branch_hash in proof: | ||
h = sorted_merkle_hash(h, branch_hash) | ||
return h == root | ||
|
||
def list_hash(leaves: List[str]) -> List[bytes]: | ||
return [sha256(leaf.encode()).digest() for leaf in leaves] | ||
|
||
def sign_dlc( | ||
dlc_root: str, | ||
funding_amount: int, | ||
privkey: PrivateKey, | ||
) -> bytes: | ||
message = ( | ||
bytes.fromhex(dlc_root) | ||
+funding_amount.to_bytes(8, "big") | ||
) | ||
return privkey.schnorr_sign(message, None, raw=True) | ||
|
||
def verify_dlc_signature( | ||
dlc_root: str, | ||
funding_amount: int, | ||
signature: bytes, | ||
pubkey: PublicKey, | ||
) -> bool: | ||
message = ( | ||
bytes.fromhex(dlc_root) | ||
+funding_amount.to_bytes(8, "big") | ||
) | ||
return pubkey.schnorr_verify(message, signature, None, raw=True) | ||
|
||
def verify_payout_signature( | ||
dlc_root: bytes, | ||
signature: bytes, | ||
pubkey: PublicKey, | ||
) -> bool: | ||
return pubkey.schnorr_verify(dlc_root, signature, None, raw=True) | ||
|
||
def verify_payout_secret( | ||
secret: bytes, | ||
pubkey: PublicKey, | ||
) -> bool: | ||
return pubkey == PrivateKey(secret, raw=True).pubkey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed this conflicts with the spec (the witness in the spec was an object, not a string). I see
Proof.witness
is strictly typed to be anOptional[str]
in nutshell, so i changed the spec to match.cashubtc/nuts@d1ffefa