Type
CONTRACT
Validation date
2023-10-20 10:06:02 UTC
Fee
0 UCO

Code (1.96 KB)

@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

Content (1006 B)

{
  "0000003AE6320FD2131479ABB7E133646DE3A95ABEACB8638FF29F15E8D8C1B9E660": {
    "chain_id": 11155111,
    "end_time": 1697803200,
    "hmac_address": "0000AFC53C90A5F29E93103D3A9EF8C76700EC0DBE294108BCE1E764F6C274BDFF51"
  },
  "000084AC49B72E3B74170211778B2E3AD147EBDBC05A3F1BBACBFD1174E9ADE3FEE8": {
    "chain_id": 11155111,
    "end_time": 1697803500,
    "hmac_address": "0000C5B98F7A86188010C7C58B7BCFEB08BCF26B6AEFAEA869CED6194C014E79EC0E"
  },
  "00008B502CF859CD0BEB2BC89FDA72F3C7F9901B482B7CB133EF2572645BED711AE1": {
    "chain_id": 11155111,
    "end_time": 1697802120,
    "hmac_address": "0000DE1FED4D061653CEED0FF4A0DC7F686D6639192537756A3C526B7A3338B3C451"
  },
  "0000CF1172FC915739196B86FA1D30989F443F0E69D22F224CA3A54DA69CBEF93FB5": {
    "chain_id": 11155111,
    "end_time": 1697801880,
    "hmac_address": "00009095B8C9711D3A86C1110D6495656AF4C1B9EFB506EFCD1BD9D247884C6D836F"
  },
  "0000D35B8492869E2AD2B726DD6F7631F101A6730336C51CA8F74A495993535AF702": {
    "chain_id": 11155111,
    "end_time": 1697803020,
    "hmac_address": "00008F4AF87F5D2436267526AC57FEB3FB0FB21BA588855ACC081EBA09E811FA3F58"
  }
}

State (0 B)

Movements (0)

Ownerships (1)

  • Secret shared with 1 key

    Encoded secret

    97F38F1D9065DF25E642863A97AE1A26B2D38B93F8EB9ECF784051578B9C21C81353C13A506587417F322EBA21884C11CC4AC8F66D7B33384EBABBB1

    Authorized keys

    • 00017877BCF4122095926A49489009649603AB129822A19EF9D573B8FD714911ED7F

Contract recipients (1)

Inputs (0)

Contract inputs (0)

Unspent outputs (1)

Proofs and signatures

Previous public key

00014D58A795EA129CFB168046422F5B5CAB52E345D73F28ECDE63CDA8A2A5F891FF

Previous signature

CD50EB44C650A12D7750D3E07FE31C17FEBEE27C83240E42AC4E3F8778AFCDD9B4716E899E9A63A880789CEE0B0FF02A3BC71ED70D3671B6E67050AE3F115E0D

Origin signature

304402201961EFB77922E1204C964B5BDA96BB6DAA26D0D4D9127E388A9FDE70C9A47FFE022027515BCEB4711F78922EF33BD513FA6BF3DB54C6D15D4C6E5403C07504B9899C

Proof of work

0101046C39D56B717142B6EE14B0F8B2561ECA458F3D2D12C9977D613F78829419063211F9C21F7BB0D56B6523040A8156BE22B6E9D6332B88ED882574D12AE32F7A45

Proof of integrity

004D38A45F1FE047C7E0DC5D01A83DF8EC01EBEC00C7216F59A85056B9EB77A5DA

Coordinator signature

2C2B4CCA5BB7574BCC1FF0447624E72D5F33780C9DDAFAE4C18F3DA1C832652EA00D9B8F66DDE208FEFFB8A18F24F7D2B9B3CAE6DD7D177EA92F41E9EAD51B06

Validator #1 public key

0001500FBE298B79FFBDD5CCA1798F30FD88A53D26EC39DE5DDE1F4137B032A4BC34

Validator #1 signature

BC331F6F0ACB2D52B6736DFC8E36116CFF1EF049FB71BCEE4D924B6DE5FE959AE1F4CA94370ECFE1905E422BFF4B1ACF60016156D9BC6439CAF7F5D4045D040A

Validator #2 public key

00018A312AFA617E98B343D09AD2E73F0AB661DB0A59FB986D5DB8CE7664E14C25FC

Validator #2 signature

D4A6E92E88002B98D6D72CA13C8F5188507DF90796CC9BD78D80E945FBD88F5B03286BFA6CB89AA03A0B6FB0D0EB88226AE9309C3943D7C33AD0480AD99FFC01