Okay, perfect:
main function:
# initialize an algodClient
headers = {
"X-API-Key": algod_token,
}
# Initialize an algod client
algod_client = algod.AlgodClient(algod_token, algod_address, headers)
creator_private_key = get_private_key_from_mnemonic(creator_mnemonic)
user_private_key = get_private_key_from_mnemonic(user_mnemonic)
# read teal program
program_source = open(approval_program_source_initial, 'r').read()
clear_state = open(clear_program_source, 'r').read()
approval_program = compile_program(algod_client, program_source)
clear_program = compile_program(algod_client, clear_state)
# In this function I get biometrics from image (return array of value)
# and an array of random value
biometric, cryptoKey = helper.bindBioCryptoKey()
biometric = bytearray(biometric)
cryptoKey = bytearray(cryptoKey)
idUtente = hashLib.md5NameAssets("JohnSmith".encode('utf-8'))
app_id = create_app(algod_client, creator_private_key, approval_program, clear_program, global_schema, local_schema)
opt_in_app (algod_client, user_private_key, app_id)
call_app (algod_client, user_private_key, app_id, [biometric, cryptoKey, idUtente])
Next, create_app function:
def create_app(client, private_key, approval_program, clear_program, global_schema, local_schema):
# define sender as creator
sender = account.address_from_private_key(private_key)
# declare on_complete as NoOp
on_complete = transaction.OnComplete.NoOpOC.real
# get node suggested parameters
params = client.suggested_params()
# comment out the next two (2) lines to use suggested fees
params.flat_fee = True
params.fee = 1000
# create unsigned transaction
txn = transaction.ApplicationCreateTxn(sender, params, on_complete, approval_program, clear_program, global_schema, local_schema)
# sign transaction
signed_txn = txn.sign(private_key)
tx_id = signed_txn.transaction.get_txid()
# send transaction
client.send_transactions([signed_txn]) # HERE THE ERROR
# await confirmation
wait_for_confirmation(client, tx_id)
# display results
transaction_response = client.pending_transaction_info(tx_id)
app_id = transaction_response['application-index']
print("Created new app-id: ", app_id)
Rewrite here the error:
logic eval error: invalid ApplicationArgs index 2
Here Smart Contract code (in PyTeal)
from pyteal import *
def approval_program():
biometric = Txn.application_args[0]
cryptoKey = Txn.application_args[1]
idUtente = Txn.application_args[2]
idUtente_tally = App.globalGet(idUtente)
on_creation = Seq ([
App.localPut((Int(0)), Bytes("IdUtente"), idUtente_tally),
Return(Int(1))
])
program = Cond(
[Int(1), on_creation]
)
return program
def clear_state_program():
program = Seq([
Return(Int(1))
])
return program
if __name__ == "__main__":
with open('mySmartContract_approval.teal', 'w') as f:
compiled = compileTeal(approval_program(), Mode.Application)
f.write(compiled)
with open('mySmartContract_clearstate.teal', 'w') as f:
compiled = compileTeal(clear_state_program(), Mode.Application)
f.write(compiled)