Type
CONTRACT
Validation date
2023-10-19 21:26:20 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 (604 B)

{
  "000042D40F8A1F8FB03913C3EFDD45A5675DC5624610E1B8566B8941B2C9E4E33745": {
    "chain_id": 11155111,
    "end_time": 1697757600,
    "hmac_address": "00001799D58F728A8E082C58B9A4740BE101296D2E292BEA4B15976CC2685572AE00"
  },
  "0000C38D9489C558DAA2D9051A2ED3BF74D17DF3606FF73BD86F5DBFD51BFB9A701A": {
    "chain_id": 11155111,
    "end_time": 1697756220,
    "hmac_address": "00000D5E65FCD3DA3B30A91E938335C9C28A9C4F35D89437EB5671D77BA79C079704"
  },
  "0000D2784ED784F12D5593BF1698EA71E261195503160656090810825C8FC579FC8C": {
    "chain_id": 11155111,
    "end_time": 1697757960,
    "hmac_address": "0000FF272CB877CFFD2AE579F1223F1881F730E2F657062E1120686FE5DD5E6610F9"
  }
}

State (0 B)

Movements (0)

Ownerships (1)

  • Secret shared with 1 key

    Encoded secret

    8BF604F2CFEB2330C84BEC0177E2426B761CAECCB07D1113E6E3EEB23A6B876B1797885A3A943FE3836FEAB35ECC06DC9B7E78F25EE4E34FA29FA63B

    Authorized keys

    • 00017877BCF4122095926A49489009649603AB129822A19EF9D573B8FD714911ED7F

Contract recipients (1)

Inputs (0)

Contract inputs (0)

Unspent outputs (1)

Proofs and signatures

Previous public key

000174C266614FAA13146F3267ADBBBD566031597E4B7B3E81F16C9ED25BDB34310C

Previous signature

6F835F3A30330814B5757D4583F254CD1FFC5AB1ED94E5E7B337691A7C873B5116267EA756C9BE29DF328464330B0D29C5A4B7DE790E2490A6C0E7D4A270EF01

Origin signature

3046022100D57B64D2A58454A2C743D914E94E6E3CC4DCB13AC582CFEAC7FDF90D00BAC2B5022100AA15A34AE4890D773DD50BA9A9B4CD9A903BE026D7A17775BDEF1E66AF815898

Proof of work

0101046C39D56B717142B6EE14B0F8B2561ECA458F3D2D12C9977D613F78829419063211F9C21F7BB0D56B6523040A8156BE22B6E9D6332B88ED882574D12AE32F7A45

Proof of integrity

00DC942B904F1C98C42DBBFB9CFB3C231731374C352268017D9FB5C48AD280EF3C

Coordinator signature

DB0253EFA18819559B400B94EF5DFBF70CE1C2AC0EEDCFF2F0DB9E27D902F81DE489C8C94FF621274755FC87C34C4F8CBD66D641A39BED332448143A04A9540A

Validator #1 public key

0001500FBE298B79FFBDD5CCA1798F30FD88A53D26EC39DE5DDE1F4137B032A4BC34

Validator #1 signature

CEE6C235141E699E91F34EF3C2042E3879908DEB2EBA37AF742CA9709E0C26A07F93F53E401637F32796E9C60201808DBF9BCE3AE1EA950A5709C0387AEBA203

Validator #2 public key

00018A312AFA617E98B343D09AD2E73F0AB661DB0A59FB986D5DB8CE7664E14C25FC

Validator #2 signature

5C6E9CDF5D444CC8D5730DD8D1FE35C5636E8DA4B6D5F95920E7E09A7F4DC3DD0C29687B5AF99A681F2D98C87E0D8A1999FAED2AE1050B82041F7372C100C106