Skip to content

Instantly share code, notes, and snippets.

@akolotov
Last active February 10, 2018 19:39
Show Gist options
  • Save akolotov/7b27902d196da15fde63fd4a484cb407 to your computer and use it in GitHub Desktop.
Save akolotov/7b27902d196da15fde63fd4a484cb407 to your computer and use it in GitHub Desktop.
Contract creation with the same address in two different networks without knowing a private key of account
contract TheSameAddress {
address public owner;
function TheSameAddress() public{
owner = msg.sender;
}
}
from time import sleep
from web3 import Web3
from web3.utils.transactions import wait_for_transaction_receipt
from ethereum.transactions import Transaction
from rlp import encode
from json import loads
# Contract bytecode and ABI
c_bin = '6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060e78061005d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680638da5cb5b146044575b600080fd5b3415604e57600080fd5b60546096565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a72305820b6197b8a8cf0fea98efc7f3f1c4527715f63940f6fd89a1f799986bf3cb1790c0029'
c_abi = loads('[{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]')
web3_ropsten = Web3(Web3.HTTPProvider('http://127.0.0.1:48002'))
web3_kovan = Web3(Web3.HTTPProvider('https://kovan.infura.io'))
# constants to be used in transactions which will deploy contract
nonce = 0
gasPrice = web3_ropsten.toWei(18, 'gwei')
gasLimit = 150000
to = b''
value = 0
data = bytes.fromhex(c_bin)
# having 27 or 28 for "v" is very important to be allowed to send such transaction
v = 27
# Choosing for "r" and "s" some big integers
r = int.from_bytes(web3_ropsten.sha3(b'The same contract address'), byteorder='big')//100
s = int.from_bytes(web3_ropsten.sha3(b'in different networks'), byteorder='big')//100
# prepare transaction which will deploy the contract
tx2deploy = Transaction(nonce, gasPrice, gasLimit, to, value, data, v, r, s)
# get the address which will be originator of the transaction to deploy the contract
sender = web3_ropsten.eth.account.recoverTransaction(encode(tx2deploy))
##########################################################################################
# ROPSTEN PART
##########################################################################################
print("Trying to get the same address of contract on Ropsten")
print("... Sending some money to the account", sender)
richman = "0x22D519acCb88f4A30589b89BaeB19D58419D682b"
t = web3_ropsten.personal.sendTransaction({'from': richman,
'to': sender,
'value': gasLimit*gasPrice,
'gasPrice': gasPrice}, '11')
print("TX:", t.hex())
while wait_for_transaction_receipt(web3_ropsten, t).blockNumber is None:
sleep(1)
print("Block number:", wait_for_transaction_receipt(web3_ropsten, t).blockNumber)
print("... Deploying contract from", sender)
t = web3_ropsten.eth.sendRawTransaction(encode(tx2deploy))
print("TX:", t.hex())
while wait_for_transaction_receipt(web3_ropsten, t).blockNumber is None:
sleep(1)
contractAddress = wait_for_transaction_receipt(web3_ropsten, t).contractAddress
print("Contract address:", contractAddress)
# Make sure that the contract is deployed successfully
RopstenContract = web3_ropsten.eth.contract(abi = c_abi)
contract_on_ropsten = RopstenContract(contractAddress)
local_request_to_ropsten = contract_on_ropsten.call().owner()
print("Result of getting owner of the contract: ", local_request_to_ropsten)
##########################################################################################
# KOVAN PART
##########################################################################################
print("Trying to get the same address of contract on KOVAN")
print("... Sending some money to the account", sender)
richman = web3_kovan.eth.account.privateKeyToAccount(bytes.fromhex('61be106c5753958655e7991d60381834145a45617a37e9a00e6cdb34ba409d4b'))
signed = richman.signTransaction({'from': richman,
'to': sender,
'value': gasLimit*gasPrice,
'gas': 21000,
'gasPrice': gasPrice,
'nonce': web3_kovan.eth.getTransactionCount(richman.address),
'chainId': int(web3_kovan.version.network)})
t = web3_kovan.eth.sendRawTransaction(signed.rawTransaction)
print("TX:", t.hex())
while wait_for_transaction_receipt(web3_kovan, t).blockNumber is None:
sleep(5)
print("Block number:", wait_for_transaction_receipt(web3_kovan, t).blockNumber)
print("... Deploying contract from", sender)
t = web3_kovan.eth.sendRawTransaction(encode(tx2deploy))
print("TX:", t.hex())
while wait_for_transaction_receipt(web3_kovan, t).blockNumber is None:
sleep(5)
contractAddress = wait_for_transaction_receipt(web3_kovan, t).contractAddress
print("Contract address:", contractAddress)
# Make sure that the contract is deployed successfully
KovanContract = web3_kovan.eth.contract(abi = c_abi)
contract_on_kovan = KovanContract(contractAddress)
local_request_to_kovan = contract_on_kovan.call().owner()
print("Result of getting owner of the contract: ", local_request_to_kovan)
Trying to get the same address of contract on Ropsten
... Sending some money to the account 0xEfC19342C43fF3DDfE6A9EA8f07B522ff140036D
TX: 0x85fb6de6b38dd2e6e619459e9264ea87676546ba9f2ff7f9a7d900698b1ba2f8
Block number: 2627695
... Deploying contract from 0xEfC19342C43fF3DDfE6A9EA8f07B522ff140036D
TX: 0xff250ba973411ab2183abc5e5634b1169f8025a896509d8c1efaef19bb09dacb
Contract address: 0x515B5CD81a90d579ee0BA3F567A69C03E1e2C4e0
Result of getting owner of the contract:  0xEfC19342C43fF3DDfE6A9EA8f07B522ff140036D
Trying to get the same address of contract on KOVAN
... Sending some money to the account 0xEfC19342C43fF3DDfE6A9EA8f07B522ff140036D
TX: 0xf78d97f600873892978f3919454e8a5a9e8c3900d1e883d2e2d4ef6bf0cca985
Block number: 5830432
... Deploying contract from 0xEfC19342C43fF3DDfE6A9EA8f07B522ff140036D
TX: 0xff250ba973411ab2183abc5e5634b1169f8025a896509d8c1efaef19bb09dacb
Contract address: 0x515B5CD81a90d579ee0BA3F567A69C03E1e2C4e0
Result of getting owner of the contract:  0xEfC19342C43fF3DDfE6A9EA8f07B522ff140036D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment