Type
TOKEN
Validation date
2023-10-20 14:22:42 UTC
Fee
0 UCO

Code (2.1 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
    ),
  # 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
  address: true
]

actions triggered_by: transaction, on: request_funds(_end_time, amount, _user_address, _secret_hash) do
  args = [
    0x00001a4ab7ad0ce2b494c965c66ff2962692a5fe5ecb71b345abb53bad88a83a01f1,
    amount,
    transaction.address
  ]

  token_definition =
    Contract.call_function(0x0000795bfd6ca2f5ea9f378c20775caf798e9bfbff6aec94bf87ad8d59233b4fa299, "get_token_resupply_definition", args)

  Contract.set_type("token")
  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, 0x00001a4ab7ad0ce2b494c965c66ff2962692a5fe5ecb71b345abb53bad88a83a01f1)
      balance >= amount
    ),
  content: List.in?([80001], 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 == 0x00001a4ab7ad0ce2b494c965c66ff2962692a5fe5ecb71b345abb53bad88a83a01f1 &&
             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(0x00007df756ca80cf98a979a669ee0546de90f2b66f42596d494152956e88090ea723, "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
  ]

  htlc_genesis_address = String.to_hex(htlc_genesis_address)
  contract_content = Map.set(contract_content, htlc_genesis_address, htlc_map)

  Contract.add_recipient(
    address: 0x00007df756ca80cf98a979a669ee0546de90f2b66f42596d494152956e88090ea723,
    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), as: [
  type: "transfer",
  content:
    (
      # Ensure htlc_genesis_address exists in pool state
      # and end_time has not been reached
      contract_content = Contract.call_function(0x00007df756ca80cf98a979a669ee0546de90f2b66f42596d494152956e88090ea723, "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?
    ),
  # 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
  address: true
]

actions triggered_by: transaction, on: reveal_secret(htlc_genesis_address) do
  contract_content = Contract.call_function(0x00007df756ca80cf98a979a669ee0546de90f2b66f42596d494152956e88090ea723, "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: 0x00007df756ca80cf98a979a669ee0546de90f2b66f42596d494152956e88090ea723,
    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
  0x00001a4ab7ad0ce2b494c965c66ff2962692a5fe5ecb71b345abb53bad88a83a01f1
end

#####################
# Private functions #
#####################

fun valid_chargeable_code?(end_time, amount, user_address, secret_hash) do
  args = [
    end_time,
    user_address,
    0x0000072e418b60c6dabd272bc2728b17b352b8104f068d11365d50134f96a4199609,
    secret_hash,
    0x00001a4ab7ad0ce2b494c965c66ff2962692a5fe5ecb71b345abb53bad88a83a01f1,
    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,
      0x0000072e418b60c6dabd272bc2728b17b352b8104f068d11365d50134f96a4199609,
      0x00001a4ab7ad0ce2b494c965c66ff2962692a5fe5ecb71b345abb53bad88a83a01f1,
      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 (232 B)

{
  "aeip": [
    8,
    18,
    19
  ],
  "recipients": [
    {
      "amount": 199000,
      "to": "00005A3894C2C9DB7F97D8159E7B4869C5CBCFBCEE40A2AEC83A59A9A46593492D8F"
    }
  ],
  "supply": 199000,
  "token_reference": "00001A4AB7AD0CE2B494C965C66FF2962692A5FE5ECB71B345ABB53BAD88A83A01F1"
}

State (0 B)

Movements (0)

Ownerships (1)

  • Secret shared with 1 key

    Encoded secret

    74F2FFCE7EA5BC4A15C325BF2197BBD084ED263E535209F66B83733207AA245A0622254B8C171AAFA148A7842B42B1E2F3BA7351161D9ABBF2207899

    Authorized keys

    • 00017877BCF4122095926A49489009649603AB129822A19EF9D573B8FD714911ED7F

Contract recipients (0)

Inputs (0)

Contract inputs (0)

Unspent outputs (1)

Proofs and signatures

Previous public key

0001A13F6257B1CAA288D338BCEB45EB4121F5106FAF3D27443998CA013D0A103466

Previous signature

164A1A56360EDAD79BA761E776D2F129A1510CA08223C81F4A68C9C6FCE5BC15984236421DBF3B9F0EBCDE00D26F5CB12339B42C3072CA819DF8D40C05F8300A

Origin signature

3044022062A200E1F85A066A28E2ED02189DEE94BF276FBC9832314D36A0C871C1615AFA022040C5E504E90E15B498898E19EFD5AC38A26A58B2019830711E3DF265C83CFF17

Proof of work

010104AB672F1E69B064D192819F1797C1926F158DBA8F3924AC732B4C4D70D0C8EA0A0D6506E5896C9C2524D2BE26CB5016287E1816A597C408008BA36FCB154A7765

Proof of integrity

005936FF368ECBE8D1ECA59E2C76399112972732250A45573BB7DBC6FED4700A72

Coordinator signature

F20B6F2046ADDBE72EB0E89B6F6923DB7EF0218A9BE7079114090DB4FC0E4EB41E4A7A559B66D419F9967E293F8A5B5119278710DCB4954E172E243C4374E90A

Validator #1 public key

0001500FBE298B79FFBDD5CCA1798F30FD88A53D26EC39DE5DDE1F4137B032A4BC34

Validator #1 signature

275D968CD896E9FED8C1D0CB15B2CDC4E40FE36F1476D24CE2BE9E7646735C9C11C46C49CD9B49C8FCB341EBBF9E1ACB80983B9DA6A4A94EB894FF7C85686C0C

Validator #2 public key

00018A312AFA617E98B343D09AD2E73F0AB661DB0A59FB986D5DB8CE7664E14C25FC

Validator #2 signature

954D045B8216C417BC8972354327594D4FF56025820CE961466AAB1874B1AF73FAE6A0C7793EC1C0B842FBBADE6396FFEFF2B4DE20F754EB53CF8DA959D4C600