@version 1
####################################
# EVM => Archethic : Request funds #
####################################
condition triggered_by: transaction, on: request_funds(end_time, amount, user_address, secret_hash, evm_tx_address, evm_contract, chain_id), as: [
type: "contract",
code: valid_chargeable_code?(end_time, amount, user_address, secret_hash),
timestamp: (
# End time cannot be less than now or more than 1 day
now = Time.now()
end_time > now && end_time <= now + 86400
),
content: List.in?([11155111], chain_id),
address: (
valid? = false
tx_receipt_request = get_tx_receipt_request(evm_tx_address)
call_status_request = get_call_request(evm_contract, "status()", 2)
call_enough_funds_request = get_call_request(evm_contract, "enoughFunds()", 3)
call_hash_request = get_call_request(evm_contract, "hash()", 4)
call_end_time_request = get_call_request(evm_contract, "lockTime()", 5)
call_amount_request = get_call_request(evm_contract, "amount()", 6)
body = Json.to_string([
tx_receipt_request,
call_status_request,
call_enough_funds_request,
call_hash_request,
call_end_time_request,
call_amount_request
])
chain_data = get_chain_data(chain_id)
headers = ["Content-Type": "application/json"]
res = Http.request(chain_data.endpoint, "POST", headers, body)
if res.status == 200 && Json.is_valid?(res.body) do
responses = Json.parse(res.body)
tx_receipt = get_response(responses, 1)
call_status = get_response(responses, 2)
call_enough_funds = get_response(responses, 3)
call_hash = get_response(responses, 4)
call_end_time = get_response(responses, 5)
call_amount = get_response(responses, 6)
if !any_nil?([tx_receipt, call_status, call_enough_funds, call_hash, call_end_time, call_amount]) do
# event = Crypto.hash("ContractMinted(address,uint256)", "keccak256")
event = "0x8640c3cb3cba5653efe5a3766dc7a9fb9b02102a9f97fbe9ea39f0082c3bf497"
valid_tx_receipt? = valid_tx_receipt?(tx_receipt, chain_data.proxy_address, evm_contract, event)
# Pending is status 0
valid_status? = valid_status?(call_status, 0)
enough_funds? = enough_funds?(call_enough_funds)
valid_hash? = valid_hash?(call_hash, secret_hash)
valid_end_time? = valid_end_time?(call_end_time, end_time)
valid_amount? = valid_amount?(call_amount, amount, chain_data.decimals)
valid? = valid_tx_receipt? && valid_status? && enough_funds? && valid_hash? && valid_end_time? && valid_amount?
end
end
valid?
)
]
actions triggered_by: transaction, on: request_funds(_, amount, _, _, _, evm_contract, chain_id) do
chain_data = get_chain_data(chain_id)
args = [
0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4,
amount,
transaction.address
]
token_definition =
Contract.call_function(0x0000795bfd6ca2f5ea9f378c20775caf798e9bfbff6aec94bf87ad8d59233b4fa299, "get_token_resupply_definition", args)
Contract.set_type("token")
Contract.add_recipient(
address: transaction.address,
action: "provision",
args: [evm_contract, chain_data.endpoint]
)
Contract.set_content(token_definition)
end
##########################################
# Archethic => EVM : Request secret hash #
##########################################
condition triggered_by: transaction, on: request_secret_hash(htlc_genesis_address, amount, user_address, chain_id), as: [
type: "transfer",
code: valid_signed_code?(htlc_genesis_address, amount, user_address),
previous_public_key:
(
# Ensure contract has enough fund to withdraw
previous_address = Chain.get_previous_address()
balance = Chain.get_token_balance(previous_address, 0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4)
balance >= amount
),
content: List.in?([11155111], chain_id),
token_transfers:
(
valid? = false
htlc_genesis_address = String.to_hex(htlc_genesis_address)
transfers = Map.get(transaction.token_transfers, htlc_genesis_address, [])
for transfer in transfers do
if transfer.token_address == 0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4 &&
transfer.token_id == 0 &&
transfer.amount == amount do
valid? = true
end
end
valid?
)
]
actions triggered_by: transaction, on: request_secret_hash(htlc_genesis_address, amount, _user_address, chain_id) do
# Here delete old secret that hasn't been used before endTime
contract_content = Contract.call_function(0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, "get_state", [])
for key in Map.keys(contract_content) do
htlc_map = Map.get(contract_content, key)
if htlc_map.end_time > Time.now() do
contract_content = Map.delete(contract_content, key)
end
end
secret = Crypto.hmac(transaction.address)
secret_hash = Crypto.hash(secret, "sha256")
# Build signature for EVM decryption
signature = sign_for_evm(secret_hash, chain_id)
# Calculate endtime now + 2 hours
now = Time.now()
end_time = now - Math.rem(now, 60) + 7200
# Add secret and signature in content
htlc_map = [
hmac_address: transaction.address,
end_time: end_time,
chain_id: chain_id,
amount: amount
]
htlc_genesis_address = String.to_hex(htlc_genesis_address)
contract_content = Map.set(contract_content, htlc_genesis_address, htlc_map)
Contract.add_recipient(
address: 0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53,
action: "update_state",
args: [contract_content]
)
Contract.add_recipient(
address: htlc_genesis_address,
action: "set_secret_hash",
args: [secret_hash, signature, end_time]
)
end
####################################
# Archethic => EVM : Reveal secret #
####################################
condition triggered_by: transaction, on: reveal_secret(htlc_genesis_address, evm_tx_address, evm_contract), as: [
type: "transfer",
content: (
# Ensure htlc_genesis_address exists in pool state
# and end_time has not been reached
contract_content = Contract.call_function(0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, "get_state", [])
valid? = false
htlc_genesis_address = String.to_hex(htlc_genesis_address)
htlc_map = Map.get(contract_content, htlc_genesis_address)
if htlc_map != nil do
valid? = htlc_map.end_time > Time.now()
end
valid?
),
address: (
valid? = false
htlc_map = nil
contract_content = Contract.call_function(0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, "get_state", [])
htlc_genesis_address = String.to_hex(htlc_genesis_address)
htlc_map = Map.get(contract_content, htlc_genesis_address)
if htlc_map != nil do
tx_receipt_request = get_tx_receipt_request(evm_tx_address)
call_status_request = get_call_request(evm_contract, "status()", 2)
call_enough_funds_request = get_call_request(evm_contract, "enoughFunds()", 3)
call_hash_request = get_call_request(evm_contract, "hash()", 4)
call_end_time_request = get_call_request(evm_contract, "lockTime()", 5)
call_amount_request = get_call_request(evm_contract, "amount()", 6)
body = Json.to_string([
tx_receipt_request,
call_status_request,
call_enough_funds_request,
call_hash_request,
call_end_time_request,
call_amount_request
])
chain_data = get_chain_data(htlc_map.chain_id)
headers = ["Content-Type": "application/json"]
res = Http.request(chain_data.endpoint, "POST", headers, body)
if res.status == 200 && Json.is_valid?(res.body) do
responses = Json.parse(res.body)
tx_receipt = get_response(responses, 1)
call_status = get_response(responses, 2)
call_enough_funds = get_response(responses, 3)
call_hash = get_response(responses, 4)
call_end_time = get_response(responses, 5)
call_amount = get_response(responses, 6)
if !any_nil?([tx_receipt, call_status, call_enough_funds, call_hash, call_end_time, call_amount]) do
# event = Crypto.hash("ContractProvisioned(address,uint256)", "keccak256")
event = "0x0c5d1829e93110ff9c24aa8ac41893b65509108384b3036d4f73ffccb235e9ec"
secret = Crypto.hmac(htlc_map.hmac_address)
secret_hash = Crypto.hash(secret, "sha256")
htlc_data = Contract.call_function(htlc_genesis_address, "get_htlc_data", [])
valid_tx_receipt? = valid_tx_receipt?(tx_receipt, chain_data.proxy_address, evm_contract, event)
# Pending is status 0
valid_status? = valid_status?(call_status, 0)
enough_funds? = enough_funds?(call_enough_funds)
valid_hash? = valid_hash?(call_hash, secret_hash)
valid_end_time? = valid_end_time?(call_end_time, htlc_map.end_time)
valid_amount? = valid_amount?(call_amount, htlc_data.amount, chain_data.decimals)
valid? = valid_tx_receipt? && valid_status? && enough_funds? && valid_hash? && valid_end_time? && valid_amount?
end
end
end
valid?
)
]
actions triggered_by: transaction, on: reveal_secret(htlc_genesis_address, _evm_tx_address, _evm_contract_address) do
contract_content = Contract.call_function(0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, "get_state", [])
htlc_genesis_address = String.to_hex(htlc_genesis_address)
htlc_map = Map.get(contract_content, htlc_genesis_address)
contract_content = Map.delete(contract_content, htlc_genesis_address)
secret = Crypto.hmac(htlc_map.hmac_address)
# Do not use chain ID in signature for the secret reveal
signature = sign_for_evm(secret, nil)
Contract.add_recipient(
address: 0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53,
action: "update_state",
args: [contract_content]
)
Contract.add_recipient(
address: htlc_genesis_address,
action: "reveal_secret",
args: [secret, signature]
)
end
condition triggered_by: transaction, on: update_code(new_code), as: [
previous_public_key:
(
# Pool code can only be updated from the master chain if the bridge
# Transaction is not yet validated so we need to use previous address
# to get the genesis address
previous_address = Chain.get_previous_address()
Chain.get_genesis_address(previous_address) == 0x000020a6911478356ca264174e35d26021fb4cbe870703bb7c3859e01cdb34bcd823
),
code: Code.is_valid?(new_code)
]
actions triggered_by: transaction, on: update_code(new_code) do
Contract.set_type("contract")
# Keep contract state
Contract.set_content(contract.content)
Contract.set_code(new_code)
end
####################
# Public functions #
####################
export fun(get_token_address()) do
0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4
end
#####################
# Private functions #
#####################
fun valid_chargeable_code?(end_time, amount, user_address, secret_hash) do
args = [
end_time,
user_address,
0x000066706d704329ca8c2a4153991e2ddacc968622ca10217ca14952eb7abb966c30,
secret_hash,
0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4,
amount
]
expected_code = Contract.call_function(0x0000795bfd6ca2f5ea9f378c20775caf798e9bfbff6aec94bf87ad8d59233b4fa299, "get_chargeable_htlc", args)
Code.is_same?(expected_code, transaction.code)
end
fun valid_signed_code?(htlc_address, amount, user_address) do
valid? = false
htlc_address = String.to_hex(htlc_address)
last_htlc_transaction = Chain.get_last_transaction(htlc_address)
if last_htlc_transaction != nil do
args = [
user_address,
0x000066706d704329ca8c2a4153991e2ddacc968622ca10217ca14952eb7abb966c30,
0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4,
amount
]
expected_code = Contract.call_function(0x0000795bfd6ca2f5ea9f378c20775caf798e9bfbff6aec94bf87ad8d59233b4fa299, "get_signed_htlc", args)
valid? = Code.is_same?(expected_code, last_htlc_transaction.code)
end
valid?
end
fun get_chain_data(chain_id) do
data = Map.new()
if chain_id == 11155111 do
data = Map.set(data, "endpoint", "https://sepolia.infura.io/v3/3a7a2dbdbec046a4961550ddf8c7d78a")
data = Map.set(data, "proxy_address", "0xcfba4fa32527bff23e073406c772e9a8b8d02650")
data = Map.set(data, "decimals", 18)
end
data
end
fun get_call_request(evm_contract, call, id) do
abi_data = Evm.abi_encode(call)
tx = [to: evm_contract, data: "0x#{abi_data}"]
[jsonrpc: "2.0", id: id, method: "eth_call", params: [tx, "latest"]]
end
fun get_response(responses, id) do
response = nil
for res in responses do
if res.id == id do
response = Map.get(res, "result")
end
end
response
end
fun any_nil?(list) do
nil? = false
for i in list do
if i == nil do
nil? = true
end
end
nil?
end
fun get_tx_receipt_request(evm_tx_address) do
[
jsonrpc: "2.0",
id: 1,
method: "eth_getTransactionReceipt",
params: [evm_tx_address]
]
end
fun valid_tx_receipt?(tx_receipt, proxy_address, evm_contract, expected_event) do
logs = nil
for log in tx_receipt.logs do
if String.to_lowercase(log.address) == proxy_address do
logs = log
end
end
if logs != nil do
# Transaction is valid
valid_status? = tx_receipt.status == "0x1"
# Transaction interacted with proxy address
valid_proxy_address? = String.to_lowercase(tx_receipt.to) == proxy_address
# Logs are comming from proxy address
valid_logs_address? = String.to_lowercase(logs.address) == proxy_address
# Pool contract emmited expected event
event = List.at(logs.topics, 0)
valid_event? = String.to_lowercase(event) == expected_event
# Contract minted match evm_contract in parameters
decoded_data = Evm.abi_decode("(address)", List.at(logs.topics, 1))
topic_address = List.at(decoded_data, 0)
valid_contract_address? = topic_address == String.to_lowercase(evm_contract)
valid_status? && valid_proxy_address? && valid_logs_address? && valid_event? && valid_contract_address?
else
false
end
end
fun valid_status?(call_status, expected_status) do
decoded_data = Evm.abi_decode("(uint)", call_status)
List.at(decoded_data, 0) == expected_status
end
fun enough_funds?(call_enough_funds) do
decoded_data = Evm.abi_decode("(bool)", call_enough_funds)
List.at(decoded_data, 0) == true
end
fun valid_hash?(call_hash, secret_hash) do
secret_hash = "0x#{String.to_lowercase(secret_hash)}"
decoded_data = Evm.abi_decode("(bytes32)", call_hash)
List.at(decoded_data, 0) == secret_hash
end
fun valid_end_time?(call_end_time, end_time) do
decoded_data = Evm.abi_decode("(uint256)", call_end_time)
List.at(decoded_data, 0) == end_time
end
fun valid_amount?(call_amount, amount, decimals) do
decoded_data = Evm.abi_decode("(uint256)", call_amount)
big_int_amount = List.at(decoded_data, 0)
decimal_amount = big_int_amount / Math.pow(10, decimals)
decimal_amount == amount
end
fun sign_for_evm(data, chain_id) do
hash = data
if chain_id != nil do
# Perform a first hash to combine data and chain_id
abi_data = Evm.abi_encode("(bytes32,uint)", [data, chain_id])
hash = Crypto.hash(abi_data, "keccak256")
end
prefix = String.to_hex("\x19Ethereum Signed Message:\n32")
signature_payload = Crypto.hash("#{prefix}#{hash}", "keccak256")
sig = Crypto.sign_with_recovery(signature_payload)
if sig.v == 0 do
sig = Map.set(sig, "v", 27)
else
sig = Map.set(sig, "v", 28)
end
sig
end
Content (0 B)
State (0 B)
-
Secret shared with 1 key
Encoded secret
348AA45A7651916AA7142D82C26633442B644DDB6E73154D2429BE23DB96E52BF2B90F8A183EB94007527C3ACA52657A8DEB54BF18FB87F94F4CA9AE
Authorized keys
- 00017877BCF4122095926A49489009649603AB129822A19EF9D573B8FD714911ED7F
-
Resolved 0000152D...F68Cset_secret_hash
[ "C321C2293E7F68F838AFC21E563858613BE8491D0C330734F466B2821165B47D", { "r": "ECAACA4497955A5630CAD4927CA680C37AF7C825EAB41808216842BCB08D73FB", "s": "759B7673AF4811C5998690047F3355A70B9E5D8D97AC93E5644B987B2819B90F", "v": 28 }, 1698081360 ] -
Resolved 00000392...EB63update_state
[ { "00000A623E99991444586C82CD339ED76B1E31DD5A25C1406BC191E4D3C519942B3A": { "amount": 0.0001, "chain_id": 11155111, "end_time": 1697976480, "hmac_address": "00002E14EDF9203B30FF1BEB54E7D6F481FFA0874C2AA727CAA67064BD581FF807E6" }, "0000189E011D1E2F9D18B849E22763ADEC77AAC45A537F95E5850261ECDF6E5C9EDF": { "amount": 0.02, "chain_id": 11155111, "end_time": 1698069960, "hmac_address": "00004E0EFFCBF5D0F10B9AFEB33BEF005792CA4DC85BEDF19537A4C1681A1312BD95" }, "00005E42BDB93FF0A688134E99459F60A0663F83669EEC9D3E7EFEA903AEB8A2F113": { "amount": 0.03, "chain_id": 11155111, "end_time": 1697900400, "hmac_address": "00006B475FA3E7D98976A1C8016B130CA13AFC4F35E8A4304DC511277255875C634F" }, "0000E347FBB312892B3F0B67F951AD3F333B4DAFEA552DE59F41FDEDC752D5F7B1A1": { "amount": 0.02, "chain_id": 11155111, "end_time": 1698081360, "hmac_address": "000009523F39267440DE7EAB2D9CF802E01D260E2922E5F17879FA3B4A5497F4EB9A" } } ]
Inputs (0)
Contract inputs (0)
Unspent outputs (1)
-
From 00009F1C...5B36At 2023-10-23 15:16:34 UTCAmount 100.00533864 UCO
Previous public key
000145F52D95EA7250923F0FDC218AAD436EFC34205A7332ED781BB723DCA0C3C72F
Previous signature
50A7A43D0B75973AC6834F6B96456FC5D346801868931C8B5AF70B619AE91614285994FDE390CBBD098C685ABCDCEECECA7B64461280B2B03202B5B787259405
Origin signature
304402203B130302A0EE1BA971B29FAE22A49CB6D621252EB684D4D83BFE3CC5BA15878E0220692F0E8C5BA373432A04FD015138D1D637FF2409F41FE56BA2CA412DCFC84BA3
Proof of work
010104EB90F7BDD03D5A7FD9B61D9128D7CF24C11F3F7DA96825DA3680C2B6BCC48F1AFCE26E0A5F1A903EDAA4BC9390210A0A4F175847EC2A2BB325BB6D1CE8EC8F90
Proof of integrity
00ED0DBB277E0AC7CC7A93DCDFC6DF7672865490F8F0876078FEBCABED8B2D3982
Coordinator signature
51EB3050159C8E677BAB58221DD62ADE75C2B7F9671608B24EA82CA8F6D4B796437985ED8591AD4C16509BA52DE6F8B96961C74B6115D738F3BDCF0819A89606
Validator #1 public key
00018A312AFA617E98B343D09AD2E73F0AB661DB0A59FB986D5DB8CE7664E14C25FC
Validator #1 signature
176CE44D04702795C668749C7DA2FB32A597B235545103A6F496938C391FB3ABBA2154A45C1AFFB6A7218DF6B2F51493D0ED75DD9C7690D03F8E9F989AF9410D
Validator #2 public key
0001500FBE298B79FFBDD5CCA1798F30FD88A53D26EC39DE5DDE1F4137B032A4BC34
Validator #2 signature
1524EC6CB2F6FD763656DE7682F62B0D9220198C0021215EE1675B1B1D1A926AEFCCC9706C859796CE32C458F4E9D3BDCCC3765F7B3DC916EF75045579F8430D