99import time
1010from datetime import datetime
1111from hashlib import sha256
12- from typing import NamedTuple
12+ from typing import NamedTuple , Any , List , Optional , Tuple , Union
1313import rlp
1414from base58 import b58decode , b58encode
1515from construct import Bytes , Int8ul , Int32ul , Int64ul
@@ -121,13 +121,17 @@ def write_layout(offset, data):
121121 len (data ).to_bytes (8 , byteorder = "little" )+
122122 data )
123123
124- def accountWithSeed (base , seed , program ):
125- # logger.debug(type(base), str(base), type(seed), str(seed), type(program), str(program))
126- result = PublicKey (sha256 (bytes (base ) + bytes (seed ) + bytes (program )).digest ())
124+ def accountWithSeed (base : PublicKey , seed : Union [bytes , str ], program : PublicKey ):
125+ if isinstance (seed , bytes ):
126+ seed = str (seed , 'utf8' )
127+
128+ logger .debug ('accountWithSeed base %s seed %s program %s' , base , seed , program )
129+
130+ result = PublicKey .create_with_seed (base , seed , program )
127131 logger .debug ('accountWithSeed %s' , str (result ))
128132 return result
129133
130- def createAccountWithSeedTrx (funding , base , seed , lamports , space , program ):
134+ def createAccountWithSeedTrx (funding : PublicKey , base : PublicKey , seed : bytes , lamports : int , space : int , program : PublicKey ):
131135 seed_str = str (seed , 'utf8' )
132136 data = SYSTEM_INSTRUCTIONS_LAYOUT .build (
133137 dict (
@@ -142,7 +146,7 @@ def createAccountWithSeedTrx(funding, base, seed, lamports, space, program):
142146 )
143147 )
144148 logger .debug ("createAccountWithSeedTrx %s %s %s" , type (base ), base , data .hex ())
145- created = accountWithSeed (base , seed , PublicKey ( program ) )
149+ created = accountWithSeed (base , seed , program )
146150 logger .debug ("created %s" , created )
147151 return TransactionInstruction (
148152 keys = [
@@ -303,14 +307,23 @@ def ether2program(ether, program_id, base):
303307 return (items [0 ], int (items [1 ]))
304308
305309
306- def ether2seed (ether , program_id , base ) :
310+ def ether2seed (ether : Union [ bytes , str ], program_id : Union [ str , PublicKey ], base : PublicKey ) -> Tuple [ PublicKey , bytes ] :
307311 if isinstance (ether , str ):
308312 if ether .startswith ('0x' ): ether = ether [2 :]
309- else : ether = ether .hex ()
310- seed = b58encode (bytes .fromhex (ether ))
311- acc = accountWithSeed (base , seed , PublicKey (program_id ))
313+ ether = bytes .fromhex (ether )
314+
315+ if isinstance (program_id , str ):
316+ program_id = PublicKey (program_id )
317+
318+ # We need append ACCOUNT_SEED_VERSION to created CODE account (to avoid using previously created accounts to store the code)
319+ # when calculate contract_sol variable ACCOUNT_SEED_VERSION already added in `neon-cli create-program-address`.
320+ ether = ACCOUNT_SEED_VERSION + ether
321+
322+ seed = b58encode (ether )
323+ acc = accountWithSeed (base , seed , program_id )
312324 logger .debug ('ether2program: {} {} => {} (seed {})' .format (ether , 255 , acc , seed ))
313- return (acc , 255 , seed )
325+
326+ return acc , seed
314327
315328
316329def neon_config_load (ethereum_model ):
@@ -846,14 +859,14 @@ def create_account_list_by_emulate(signer, client, ethTrx):
846859 logger .debug ("Create solana accounts for %s: %s %s" , acc_desc ["address" ], acc_desc ["account" ], acc_desc ["contract" ])
847860 code_account = None
848861 if acc_desc ["code_size" ]:
849- seed = b58encode (address )
850- code_account = accountWithSeed ( signer . public_key (), seed , PublicKey ( evm_loader_id ))
862+ ( code_account , code_account_seed ) = ether2seed (address , evm_loader_id , signer . public_key () )
863+
851864 logger .debug (" with code account %s" , code_account )
852865 code_size = acc_desc ["code_size" ]
853866 valids_size = (code_size // 8 ) + 1
854867 code_account_size = CODE_INFO_LAYOUT .sizeof () + code_size + valids_size + 2048
855868 code_account_balance = client .get_minimum_balance_for_rent_exemption (code_account_size )["result" ]
856- trx .add (createAccountWithSeedTrx (signer .public_key (), signer .public_key (), seed , code_account_balance , code_account_size , PublicKey (evm_loader_id )))
869+ trx .add (createAccountWithSeedTrx (signer .public_key (), signer .public_key (), code_account_seed , code_account_balance , code_account_size , PublicKey (evm_loader_id )))
857870 add_keys_05 .append (AccountMeta (pubkey = code_account , is_signer = False , is_writable = acc_desc ["writable" ]))
858871
859872 (create_trx , solana_address , token_address ) = createEtherAccountTrx (client , address , evm_loader_id , signer , code_account )
@@ -1084,13 +1097,11 @@ def deploy_contract(signer, client, ethTrx, perm_accs, steps):
10841097 contract_eth = keccak_256 (rlp .encode ((sender_ether , ethTrx .nonce ))).digest ()[- 20 :]
10851098 (contract_sol , contract_nonce ) = ether2program (contract_eth .hex (), evm_loader_id , signer .public_key ())
10861099
1087- # We need append ACCOUNT_SEED_VERSION to created CODE account (to avoid using previously created accounts to store the code)
1088- # when calculate contract_sol variable ACCOUNT_SEED_VERSION already added in `neon-cli create-program-address`.
1089- (code_sol , code_nonce , code_seed ) = ether2seed (ACCOUNT_SEED_VERSION + contract_eth , evm_loader_id , signer .public_key ())
1100+ (code_sol , code_seed ) = ether2seed (contract_eth , evm_loader_id , signer .public_key ())
10901101
10911102 logger .debug ("Legacy contract address ether: %s" , contract_eth .hex ())
10921103 logger .debug ("Legacy contract address solana: %s %s" , contract_sol , contract_nonce )
1093- logger .debug ("Legacy code address solana: %s %s " , code_sol , code_nonce )
1104+ logger .debug ("Legacy code address solana: %s" , code_sol )
10941105
10951106 write_trx_to_holder_account (signer , client , perm_accs .holder , ethTrx )
10961107
0 commit comments