-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add htlc files * refactor mint into several components * add hash lock signatures * add refund signature checks * simplify hash lock signature check * clean up
- Loading branch information
Showing
14 changed files
with
865 additions
and
430 deletions.
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,17 @@ | ||
from typing import Union | ||
|
||
from .secret import Secret, SecretKind | ||
|
||
|
||
class HTLCSecret(Secret): | ||
@classmethod | ||
def from_secret(cls, secret: Secret): | ||
assert secret.kind == SecretKind.HTLC, "Secret is not a HTLC secret" | ||
# NOTE: exclude tags in .dict() because it doesn't deserialize it properly | ||
# need to add it back in manually with tags=secret.tags | ||
return cls(**secret.dict(exclude={"tags"}), tags=secret.tags) | ||
|
||
@property | ||
def locktime(self) -> Union[None, int]: | ||
locktime = self.tags.get_tag("locktime") | ||
return int(locktime) if locktime else None |
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
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,76 @@ | ||
import json | ||
from typing import Any, Dict, List, Optional, Union | ||
|
||
from loguru import logger | ||
from pydantic import BaseModel | ||
|
||
from .crypto.secp import PrivateKey | ||
|
||
|
||
class SecretKind: | ||
P2SH = "P2SH" | ||
P2PK = "P2PK" | ||
HTLC = "HTLC" | ||
|
||
|
||
class Tags(BaseModel): | ||
""" | ||
Tags are used to encode additional information in the Secret of a Proof. | ||
""" | ||
|
||
__root__: List[List[str]] = [] | ||
|
||
def __init__(self, tags: Optional[List[List[str]]] = None, **kwargs): | ||
super().__init__(**kwargs) | ||
self.__root__ = tags or [] | ||
|
||
def __setitem__(self, key: str, value: str) -> None: | ||
self.__root__.append([key, value]) | ||
|
||
def __getitem__(self, key: str) -> Union[str, None]: | ||
return self.get_tag(key) | ||
|
||
def get_tag(self, tag_name: str) -> Union[str, None]: | ||
for tag in self.__root__: | ||
if tag[0] == tag_name: | ||
return tag[1] | ||
return None | ||
|
||
def get_tag_all(self, tag_name: str) -> List[str]: | ||
all_tags = [] | ||
for tag in self.__root__: | ||
if tag[0] == tag_name: | ||
for t in tag[1:]: | ||
all_tags.append(t) | ||
return all_tags | ||
|
||
|
||
class Secret(BaseModel): | ||
"""Describes spending condition encoded in the secret field of a Proof.""" | ||
|
||
kind: str | ||
data: str | ||
tags: Tags | ||
nonce: Union[None, str] = None | ||
|
||
def serialize(self) -> str: | ||
data_dict: Dict[str, Any] = { | ||
"data": self.data, | ||
"nonce": self.nonce or PrivateKey().serialize()[:32], | ||
} | ||
if self.tags.__root__: | ||
logger.debug(f"Serializing tags: {self.tags.__root__}") | ||
data_dict["tags"] = self.tags.__root__ | ||
return json.dumps( | ||
[self.kind, data_dict], | ||
) | ||
|
||
@classmethod | ||
def deserialize(cls, from_proof: str): | ||
kind, kwargs = json.loads(from_proof) | ||
data = kwargs.pop("data") | ||
nonce = kwargs.pop("nonce") | ||
tags_list: List = kwargs.pop("tags", None) | ||
tags = Tags(tags=tags_list) | ||
logger.debug(f"Deserialized Secret: {kind}, {data}, {nonce}, {tags}") | ||
return cls(kind=kind, data=data, nonce=nonce, tags=tags) |
Oops, something went wrong.