@version 1
####################################
# EVM => Archethic : Request funds #
####################################
condition triggered_by: transaction, on: request_funds(end_time, amount, user_address, secret_hash), 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: (
# Ensure the pool has enough UCO to send the requested fund
balance = Chain.get_uco_balance(contract.address)
balance >= amount
),
address: (
# Here ensure Ethereum contract exists and check rules
# How to ensure Ethereum contract is a valid one ?
# Maybe get the ABI of HTLC on github and compare it to the one on Ethereum
# Then control rules
true
)
]
actions triggered_by: transaction, on: request_funds(_end_time, amount, _user_address, _secret_hash) do
Contract.set_type "transfer"
Contract.add_uco_transfer to: transaction.address, amount: amount
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_uco_balance(previous_address)
balance >= amount
),
content: List.in?([11155111,80001,97], chain_id),
uco_transfers: (
htlc_genesis_address = String.to_hex(htlc_genesis_address)
Map.get(htlc_genesis_address) == amount
)
]
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 = Map.new()
if Json.is_valid?(contract.content) do
contract_content = Json.parse(contract.content)
end
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
]
htlc_genesis_address = String.to_hex(htlc_genesis_address)
contract_content = Map.set(contract_content, htlc_genesis_address, htlc_map)
Contract.set_content Json.to_string(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), as: [
type: "transfer",
content: (
# Ensure htlc_genesis_address exists in pool state
# and end_time has not been reached
valid? = false
if Json.is_valid?(contract.content) do
htlc_genesis_address = String.to_hex(htlc_genesis_address)
htlc_map = Map.get(Json.parse(contract.content), htlc_genesis_address)
if htlc_map != nil do
valid? = htlc_map.end_time > Time.now()
end
end
valid?
),
address: (
# Here ensure Ethereum contract exists and check rules
# How to ensure Ethereum contract is a valid one ?
# Maybe get the ABI of HTLC on github and compare it to the one on Ethereum
# Then control rules
true
)
]
actions triggered_by: transaction, on: reveal_secret(htlc_genesis_address) do
contract_content = Json.parse(contract.content)
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.set_content Json.to_string(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
"UCO"
end
#####################
# Private functions #
#####################
fun valid_chargeable_code?(end_time, amount, user_address, secret_hash) do
args = [
end_time,
user_address,
0x000018d60115ece0c7558a46b4693749bf6beab524fddccf9b10b910619e4ee08801,
secret_hash,
"UCO",
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,
0x000018d60115ece0c7558a46b4693749bf6beab524fddccf9b10b910619e4ee08801,
"UCO",
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 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
State (0 B)
-
Secret shared with 1 key
Encoded secret
14F195D614EB14BFDCACDE70B39B0DDC83ACF5DEB3958C66AD2F6E3E3FB792BBECCD4EB96AFE8DF5D17822254BAA077B470E12B2C16821F9F8ED298B
Authorized keys
- 00017877BCF4122095926A49489009649603AB129822A19EF9D573B8FD714911ED7F
-
Resolved 0000349D...ACD6reveal_secret
[ "E7AEAFA0AC1E2E0375EEF4E4A282E6E5254D79B4F5E50DB47C28C047B06F9279", { "r": "0B2323C19B51FDC1B77D79DD0ED270F312A41360BF3F00075A616973FA197199", "s": "31E66F8583D41CBBB2603BE564F2FE4B03DF7114330E926D9FA98B2AE3E49919", "v": 27 } ]
Inputs (0)
Contract inputs (0)
Unspent outputs (1)
-
From 00006012...10F6At 2023-10-20 08:19:09 UTCAmount 247.76584885 UCO
Previous public key
00015C81A45BF91DB1C1B4B20FCAC8A69919CD5751A5C9AE70934CE1E2481CDF7FD5
Previous signature
49E417C8E84E144772DD735D5E8A48F926532557B18B2D9786E5E82D88C328E11D98A6A1FF3A37AE8C1F2DB22DE2EE667ED7945117E6307262CB66D83BEE9204
Origin signature
3045022100C997A58AE6BDF767C1DAEAC86F2EF79FD885D4C06866C8D04F0C96F8EDFC334B02201BDB0759E2516517AA1FD71A5C55445B88319CDE06997CA77B84031FDCDD9E90
Proof of work
010104AB672F1E69B064D192819F1797C1926F158DBA8F3924AC732B4C4D70D0C8EA0A0D6506E5896C9C2524D2BE26CB5016287E1816A597C408008BA36FCB154A7765
Proof of integrity
00047B80E4C6289FA14CC4495021DE8451701C2A05F844DF5CB2534B5C2AAFE29D
Coordinator signature
6F9ADAD13C00FF320F229CBFC74CC61A1E32DAE879C5BC39FD7185C1148E7DE2881D181D48AD9998618A8EC070C6423A4DA6A04E53C359EB7D4E45C6C384440B
Validator #1 public key
0001500FBE298B79FFBDD5CCA1798F30FD88A53D26EC39DE5DDE1F4137B032A4BC34
Validator #1 signature
FA0321BF590EDA10AA98F925FB81EBE409EDE10662B7E01163387DB3963BB33051957A8836FB449AC5CD887D4A12A91BB7BA55BD1A18223FA4915EF1589F0B0A
Validator #2 public key
00018A312AFA617E98B343D09AD2E73F0AB661DB0A59FB986D5DB8CE7664E14C25FC
Validator #2 signature
AA3A848D6CC8B0049A0F5ADAF553199BB98FB55AF33009215C1F06523BE3A85CA1FD19D233AD98696880E1E904BF932E201F5E1FF32D8CF8F46A6F2DB45AF60A