Esempio n. 1
0
def decryptPrivateKey(_file, _pwd):
    with open(_file) as keyfile:
        encrypted_key = keyfile.read()
    keyfile.close()

    ac_obj = Account()

    decrypted_key = ac_obj.decrypt(encrypted_key, _pwd)
    return ac_obj.privateKeyToAccount(decrypted_key)
Esempio n. 2
0
 def eth_sign(self, params):
     sign_str = self.get_sign_str(params, None)
     message = Web3.toHex(Web3.sha3(text=sign_str))
     private_key = Account.decrypt(self.key_store, self.key_pwd)
     acct = Account.privateKeyToAccount(private_key)
     hash = acct.signHash(message)
     border = 'big'
     v = hash['v'].to_bytes(1, border)
     r = hash['r'].to_bytes(32, border)
     s = hash['s'].to_bytes(32, border)
     z = v + r + s
     sign = z.hex()
     params['presign'] = sign
from web3 import Web3, Account
from web3.utils.encoding import to_hex
import json

w3 = Web3(Web3.HTTPProvider('http://localhost:8545'))
accounts = w3.eth.accounts

# load wallet account
wfn = './keystore/{0}.json'.format(
    '0x6FAA97edd198F59DFb6f0063Bc34777eAa0BF02c')
with open(wfn, 'r') as f:
    wallet = json.load(f)
priv_key = Account.decrypt(wallet, '123')
account = Account.privateKeyToAccount(priv_key)

# funds wallet account
tx_hash = w3.eth.sendTransaction({
    'from': accounts[0],
    'to': account.address,
    'value': Web3.toWei(1, 'ether')
})
w3.eth.waitForTransactionReceipt(tx_hash)

balance = w3.eth.getBalance(account.address)
print('wallet balance before raw tx => {0}'.format(balance))

# prepare tx payload
nonce = w3.eth.getTransactionCount(account.address)
payload = {
    'to': accounts[9],
    'value': 1000,
def with_draw_one_times(output,
                        balance,
                        fee=0,
                        value=0,
                        keystore=keyfile_json,
                        keypass=password,
                        acc=None):
    try:
        if not acc:
            privatekey = Account.decrypt(keystore, keypass)
            log_debug_print("privateKey", binascii.b2a_hex(privatekey))
            acc = Account.privateKeyToAccount(privatekey)
        acc_balance = w3.eth.getBalance(acc.address)
        log_debug_print("use Address", acc.address, "balance", acc_balance)
        withdawcontact = w3.eth.contract(address=contract_info.address,
                                         abi=contract_info.abi)
        data = withdawcontact.encodeABI(fn_name="receivePayload",
                                        args=[output, balance, fee])
        nonce = w3.eth.getTransactionCount(acc.address)
        startvalue = w3.eth.getBalance(acc.address)

        transaction = {
            'to': contract_info.address,
            'value': value,
            'data': data,
            'nonce': nonce,
        }

        gas = get_buffered_gas_estimate(w3, transaction)
        gas_price = w3.eth.gasPrice
        log_debug_print("use gas ", gas, "gasPrice", gas_price)
        transaction['gas'] = gas
        transaction['gasPrice'] = gas_price
        signed = acc.signTransaction(transaction)

        result = w3.eth.sendRawTransaction(signed.rawTransaction)
        log_debug_print("We call contract. txHash:",
                        str(binascii.b2a_hex(result), encoding='utf-8'))
        # waiting for mined in block
        time.sleep(20)
        receipt = w3.eth.getTransactionReceipt(result)

        log_debug_print("TxReceipt status", receipt['status'])
        gasUsed = receipt['gasUsed']
        endvalue = w3.eth.getBalance(acc.address)
        subValue = startvalue - endvalue
        status = receipt['status']
        if status != 1:
            log_debug_print("Case not pass!, Tx not passed")
            return False, None, "Tx not passed"
        log_debug_print("startValue", startvalue, "endValue", endvalue,
                        "OutputValue", value, "Use Value ", subValue)
        log_debug_print("gas", gas, "gasPrice", gas_price, "gasUsed", gasUsed)
        if subValue - (value + gas_price * gasUsed) != 0.0:
            strerr = " Balance calc wrong! startBalance: " + str(
                startvalue) + " endBalance:" + str(
                    endvalue) + " subBalce:" + str(subValue)
            + " gasUsed: " + str(gasUsed) + " gasPrice" + str(gas_price)
            return False, None, strerr
        return True, result, None
    except Exception as e:
        return False, None, str(e) + ""
Esempio n. 5
0
    else:
        ETHEREUM_NETWORK = 'Mainnet'.upper()
        ROBOT_WALLET = settings.ETHEREUM_MASTER_WALLET

    print('Ethereum network:', ETHEREUM_NETWORK)
    print('Contract', settings.ETHEREUM_CONTRACT_ADDRESS)

    print('Loading robot wallet...')
    # load player wallet
    with open(ROBOT_WALLET) as json_file:
        wallet_json = json.load(json_file)

    wallet_secret = wallet_json['secret']

    # decrypt account
    account_key = Account.decrypt(wallet_json, wallet_secret)
    account = Account.privateKeyToAccount(account_key)
    w3.eth.defaultAccount = account.address
    print('Player account succesfully decrypted.')
    print('Robot wallet', w3.eth.defaultAccount)
    player_balance = w3.eth.getBalance(account=account.address)
    print(
        'Player wallet balanace eth-' +
        str(w3.fromWei(player_balance, 'ether')), 'ether')

    # initiating ethereum contract
    contract_instance = w3.eth.contract(
        address=w3.toChecksumAddress(settings.ETHEREUM_CONTRACT_ADDRESS),
        abi=settings.ETHEREUM_CONTRACT_ABI,
    )
    # setting up gas limit
Esempio n. 6
0
def get_foundation_account():
    private_key = Account.decrypt(keyfile_json, password)
    return Account.privateKeyToAccount(private_key)