1313See the License for the specific language governing permissions and
1414limitations under the License.
1515"""
16-
16+ from enum import IntEnum
1717from struct import error as StructError , pack
1818from typing import Tuple
1919
3232# Signal bist (B), version (B), inputs len (B), outputs len (B)
3333_SIGHASH_ALL_FORMAT_STRING = '!BBBB'
3434
35+
3536# used when (de)serializing token information
36- # version 1 expects only token name and symbol
37- TOKEN_INFO_VERSION = 1
37+ # version 1 is the default behavior
38+ class TokenVersion (IntEnum ):
39+ NATIVE = 0
40+ DEPOSIT = 1
41+ FEE = 2
3842
3943
4044class TokenCreationTransaction (Transaction ):
@@ -43,11 +47,19 @@ def __init__(self) -> None:
4347 # for this special tx, its own hash is used as the created token uid. We're artificially
4448 # creating the tokens list here
4549 self .tokens = []
50+ self .token_version : TokenVersion = TokenVersion .DEPOSIT
4651
4752 def __str__ (self ) -> str :
48- return ('TokenCreationTransaction(nonce=%d, timestamp=%s, version=%s, weight=%f, hash=%s, '
49- 'token_name=%s, token_symbol=%s)' % (self .nonce , self .timestamp , int (self .version ),
50- self .weight , self .hash_hex , self .token_name , self .token_symbol ))
53+ return (
54+ f'TokenCreationTransaction(nonce={ self .nonce } , '
55+ f'timestamp={ self .timestamp } , '
56+ f'version={ int (self .version )} , '
57+ f'weight={ self .weight :.6f} , '
58+ f'hash={ self .hash_hex } , '
59+ f'token_name={ self .token_name } , '
60+ f'token_symbol={ self .token_symbol } , '
61+ f'token_version={ self .token_version } )'
62+ )
5163
5264 def update_hash (self ) -> None :
5365 """ When we update the hash, we also have to update the tokens uid list
@@ -78,7 +90,12 @@ def get_funds_fields_from_struct(self, buf: bytes) -> bytes:
7890 self .outputs .append (txout )
7991
8092 # token name and symbol
81- self .token_name , self .token_symbol , buf = TokenCreationTransaction .deserialize_token_info (buf )
93+ (
94+ self .token_name ,
95+ self .token_symbol ,
96+ self .token_version ,
97+ buf
98+ ) = TokenCreationTransaction .deserialize_token_info (buf )
8299
83100 return buf
84101
@@ -148,31 +165,35 @@ def serialize_token_info(self) -> bytes:
148165 encoded_symbol = self .token_symbol .encode ('utf-8' )
149166
150167 ret = b''
151- ret += int_to_bytes (TOKEN_INFO_VERSION , 1 )
168+ ret += int_to_bytes (self . token_version , 1 )
152169 ret += int_to_bytes (len (encoded_name ), 1 )
153170 ret += encoded_name
154171 ret += int_to_bytes (len (encoded_symbol ), 1 )
155172 ret += encoded_symbol
173+
156174 return ret
157175
158176 @classmethod
159- def deserialize_token_info (cls , buf : bytes ) -> Tuple [str , str , bytes ]:
160- """ Gets the token name and symbol from serialized format
177+ def deserialize_token_info (cls , buf : bytes ) -> Tuple [str , str , TokenVersion , bytes ]:
178+ """ Gets the token name, symbol and version from serialized format
161179 """
162- (token_info_version ,), buf = unpack ('!B' , buf )
163- if token_info_version != TOKEN_INFO_VERSION :
164- raise ValueError ('unknown token info version: {}' .format (token_info_version ))
180+ (raw_token_version ,), buf = unpack ('!B' , buf )
181+ try :
182+ token_version = TokenVersion (raw_token_version )
183+ except ValueError :
184+ raise ValueError ('unknown token version: {}' .format (raw_token_version ))
165185
166186 (name_len ,), buf = unpack ('!B' , buf )
167187 name , buf = unpack_len (name_len , buf )
188+
168189 (symbol_len ,), buf = unpack ('!B' , buf )
169190 symbol , buf = unpack_len (symbol_len , buf )
170191
171192 # Token name and symbol can be only utf-8 valid strings for now
172193 decoded_name = decode_string_utf8 (name , 'Token name' )
173194 decoded_symbol = decode_string_utf8 (symbol , 'Token symbol' )
174195
175- return decoded_name , decoded_symbol , buf
196+ return decoded_name , decoded_symbol , token_version , buf
176197
177198 def verify_token_info (self ) -> None :
178199 """ Validates token info
@@ -190,6 +211,10 @@ def verify_token_info(self) -> None:
190211 if clean_token_string (self .token_symbol ) == clean_token_string (settings .HATHOR_TOKEN_SYMBOL ):
191212 raise TransactionDataError ('Invalid token symbol ({})' .format (self .token_symbol ))
192213
214+ # Can't create the token with NATIVE version
215+ if self .token_version == TokenVersion .NATIVE :
216+ raise TransactionDataError ('Invalid token version ({})' .format (self .token_version ))
217+
193218 def is_nft_creation_standard (self ) -> bool :
194219 """Returns True if it's a standard NFT creation transaction"""
195220 # We will check the outputs to validate that we have an NFT standard creation
0 commit comments