Type
CONTRACT
Validation date
2023-10-18 21:28:57 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 (1.96 KB)

{
  "00000C22E15C9C1B5299886E4144F2777583135C18BEFE4C5D27BE644B39AE5F6F77": {
    "chain_id": 11155111,
    "end_time": 1697666460,
    "hmac_address": "000031CF3D089C4058DEADDEA1CD617019FB91B143960F777A6F73A9612C36031C2B"
  },
  "00001D7183D1C64B29B8365A094C03C3C4493E86ECBFB025686F8A4E37221E8C48C4": {
    "chain_id": 11155111,
    "end_time": 1697671560,
    "hmac_address": "00009575AB3729FDD58FCC8626AFA1B204695502E764D05DD5849C67E8FCC1F2A975"
  },
  "00002D87935C0344CF8036C729FCEB802F0A055BF9FCD3E2BD1CF120A2CFEBF315ED": {
    "chain_id": 11155111,
    "end_time": 1697670900,
    "hmac_address": "000054B70F6CCBFAF18955B8EDE0EDB7078C828D6C453A8819818FD096130D2DD01B"
  },
  "000036A35BF369B25229DEAC2EE8797F8E1913FA603249466765BAA7BBAD6157397D": {
    "chain_id": 11155111,
    "end_time": 1697669160,
    "hmac_address": "0000C1A264D46545EA8B6FCF0F2AA79B25A97C4D005E805B11BE067556B3C9089EC4"
  },
  "000043631C82F7C691E7D55A33C29AE9D25D6D011476A36DA3033F78CE684603CAC7": {
    "chain_id": 11155111,
    "end_time": 1697669220,
    "hmac_address": "00006F1967B17227A9B4695CD69F9F8F2E5A1AAB175002FA69157300A42CC62A34A7"
  },
  "00004956E64C6EFC06115798D6F7F8023DE2D8CC30BC6899E8F164F7E48E1AF89E74": {
    "chain_id": 11155111,
    "end_time": 1697671680,
    "hmac_address": "0000CC40CA3EFB082F4A1EDEA1C4E442916D4C2F0FC9BD831F47CEE5EDD8845C41BD"
  },
  "0000A5215EDE0C7660573FD1852EC407028D64BEBDF8F6BD9C49BF84A9ADA40CDE31": {
    "chain_id": 11155111,
    "end_time": 1697669520,
    "hmac_address": "00002D348BF496FA9ED902E780D5E9F2E29165F46ADDA65D40D770C7E8E09E37EA4F"
  },
  "0000BD5E579B62A2394D4524624B7E32D54B43433B2B44AB079F3F00871DDBF79AD6": {
    "chain_id": 11155111,
    "end_time": 1697669340,
    "hmac_address": "0000FC3047F1490365C6CAC791C69841D16E6D59AA08F1F1C331499129A92DDB3BB3"
  },
  "0000BD81E524120AC10B80A81AD7EEE56E0BE15AE2FB35B95EB1DBAF292F94E6A104": {
    "chain_id": 11155111,
    "end_time": 1697671020,
    "hmac_address": "00006931D29E278B864DAFD241F38877D34468E6250BE67307574A64A4C0EBD517AD"
  },
  "0000CB84DC1D733CABB4AE3BBA95EB372CEA9DDBE1125F32A9D7B654048E10C9273D": {
    "chain_id": 11155111,
    "end_time": 1697669820,
    "hmac_address": "0000776E8C61B815A6F0FB446B66901859743C741221AE73ECD903E94203B6DC9122"
  }
}

State (0 B)

Movements (0)

Ownerships (1)

  • Secret shared with 1 key

    Encoded secret

    700900DAF66828B5719523639BAB14187D5FD029C3A68CFE46ADAA18C05D60F3D7CAC6636FE09DC1E61576F3609F38F48E32BF3C10106AE35C0269B8

    Authorized keys

    • 00017877BCF4122095926A49489009649603AB129822A19EF9D573B8FD714911ED7F

Contract recipients (1)

Inputs (0)

Contract inputs (0)

Unspent outputs (1)

Proofs and signatures

Previous public key

0001B1AFA161916357C19296AD7E157D43D1DB88D5DE5D391B412A38FD57B0589C3D

Previous signature

63BDD2A9D61700447DC2796D39F3C5B4D39D6AD482E66F1BA2707E90D9B48BA07A30F22EB84BAE792E3A88F14F300E8A975AA80D9BE02B925F9BFC1FD2136E0F

Origin signature

3044022039CB9458CD42E7E48FF50758B957FB8198922E186D1A3FE3EBB0B5550E5013EE02207B64A9A3E04739CFCF65AF0444B03B9B21EDB0069449E547C7DF2F5EED56C465

Proof of work

0101046C39D56B717142B6EE14B0F8B2561ECA458F3D2D12C9977D613F78829419063211F9C21F7BB0D56B6523040A8156BE22B6E9D6332B88ED882574D12AE32F7A45

Proof of integrity

005139E178B0E9B6516BFCEF2CD398C76DA9F2945E5CFAF024D6DD69E4031D5B46

Coordinator signature

8585A1E9664724E52381B064075D2ACAEFFFA3A68FB80D76BDEB2D1FCAE3EFB52885740DE01F174834BE3348ADD7CB00D15D737787F03E8EA7BB9B4F4EB54301

Validator #1 public key

00018A312AFA617E98B343D09AD2E73F0AB661DB0A59FB986D5DB8CE7664E14C25FC

Validator #1 signature

CC6990D3EF730AF843FC50DF1B0E3023E4363F8356349D0893A47010A766E4EB959C8C29094D45CFFFC7E2AB291400EB428C1C6D33F20B23FFB1F893C794CA06

Validator #2 public key

0001500FBE298B79FFBDD5CCA1798F30FD88A53D26EC39DE5DDE1F4137B032A4BC34

Validator #2 signature

2A03C5FD0FD15587C195162324F14DDA6A85E3D4AE188E059DADCCF4272655E6479BDD4F19515A6DD59D893E10DA14E2DCB1E88F0DCD3738623F0A1141C97D08