mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-20 18:44:20 +01:00
use tokenObj in burn (#156)
* use tokenObj in burn * refactor token serialization * add tests * rename
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import base64
|
||||
import json
|
||||
from sqlite3 import Row
|
||||
from typing import Any, Dict, List, Optional, TypedDict, Union
|
||||
|
||||
@@ -357,3 +359,37 @@ class TokenV3(BaseModel):
|
||||
if self.memo:
|
||||
return_dict.update(dict(memo=self.memo)) # type: ignore
|
||||
return return_dict
|
||||
|
||||
def get_proofs(self):
|
||||
return [proof for token in self.token for proof in token.proofs]
|
||||
|
||||
def get_amount(self):
|
||||
return sum([p.amount for p in self.get_proofs()])
|
||||
|
||||
def get_keysets(self):
|
||||
return list(set([p.id for p in self.get_proofs()]))
|
||||
|
||||
@classmethod
|
||||
def deserialize(cls, tokenv3_serialized: str):
|
||||
"""
|
||||
Takes a TokenV3 and serializes it as "cashuA<json_urlsafe_base64>.
|
||||
"""
|
||||
prefix = "cashuA"
|
||||
assert tokenv3_serialized.startswith(prefix), Exception(
|
||||
f"Token prefix not valid. Expected {prefix}."
|
||||
)
|
||||
token_base64 = tokenv3_serialized[len(prefix) :]
|
||||
token = json.loads(base64.urlsafe_b64decode(token_base64))
|
||||
return cls.parse_obj(token)
|
||||
|
||||
def serialize(self):
|
||||
"""
|
||||
Takes a TokenV3 and serializes it as "cashuA<json_urlsafe_base64>.
|
||||
"""
|
||||
prefix = "cashuA"
|
||||
tokenv3_serialized = prefix
|
||||
# encode the token as a base64 string
|
||||
tokenv3_serialized += base64.urlsafe_b64encode(
|
||||
json.dumps(self.to_dict()).encode()
|
||||
).decode()
|
||||
return tokenv3_serialized
|
||||
|
||||
Reference in New Issue
Block a user