Type
CONTRACT
Validation date
2023-10-10 18:13:15 UTC
Fee
0 UCO

Code (2.09 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)
  ),
  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
  args = [
    0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4,
    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, 0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4)
    balance >= amount
  ),
  content: List.in?([11155111,80001,97], 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 == 0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4 &&
         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(0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, "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: 0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, 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(0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, "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?
  ),
  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 = Contract.call_function(0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, "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: 0x0000efaca06da86f910a9362e6eb7692723b7d3f350ccef27119cb3fb7661c2a1c53, 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
  0x00003df600e329199bf3ee8fbe2b8223413d70bcdd97e15089e6a74d94de3f1173b4
end

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

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

{
  "aeip": [
    2,
    8,
    18,
    19
  ],
  "supply": 1,
  "type": "fungible",
  "symbol": "aeETH",
  "name": "aeETH",
  "allow_mint": true,
  "properties": {},
  "recipients": [
    {
      "to": "00000000000000000000000000000000000000000000000000000000000000000000",
      "amount": 1
    }
  ]
}

State (0 B)

Movements (0)

Ownerships (1)

  • Secret shared with 1 key

    Encoded secret

    9EC2FC31E2DEC2486F4B2AB619B2AD3ED753184BBA36B591120632A4E8EEC81247C11CC7F0035D24208E3A7624765162A63B19BAAD95901BEA536A67

    Authorized keys

    • 00017877BCF4122095926A49489009649603AB129822A19EF9D573B8FD714911ED7F

Contract recipients (0)

Inputs (0)

Contract inputs (0)

Unspent outputs (1)

Proofs and signatures

Previous public key

0001E92814B1666ABCFAE0C46C40C438A8BDC123FCCB8B7F5394A1D0609E80856639

Previous signature

51254D0A9AA4B6290792722B7E894F841669FFAAEF46EAD2E0186AE02356D13C9FD07E60F19F37A74B46DA34C18F7F722ABA35C7859653FDB6046E001AD05304

Origin signature

3046022100E0D966083E493B25D6E79579FC6130096FF800D31ABD95757C315E1C645F247602210084B846C644618E4FBB1103C7A379B7C66DDCE94072D0D75F7A59F40972A24F29

Proof of work

010104EB90F7BDD03D5A7FD9B61D9128D7CF24C11F3F7DA96825DA3680C2B6BCC48F1AFCE26E0A5F1A903EDAA4BC9390210A0A4F175847EC2A2BB325BB6D1CE8EC8F90

Proof of integrity

00DBB09DEDD358DCED138FAFB70AAD6A56C47E817F8B80172DBD30A64480064BC3

Coordinator signature

CD83C21687285A1197F35834E8CCAB6712D3933B818EB2D30B505C5B48A3D1304E3C698D4907F8835B35DC357C6E7AF4102520E095F6287E01A32D710F703B04

Validator #1 public key

00018A312AFA617E98B343D09AD2E73F0AB661DB0A59FB986D5DB8CE7664E14C25FC

Validator #1 signature

A60C0B58574470C01A801DC054CAD7E2CC43B2941AC3DBB79C631E1F4B4F10A36C8896CA1C964E4E2E61D2A7A087503FE9460C9A8F1D4BF7CBDDD6EB95819505

Validator #2 public key

0001500FBE298B79FFBDD5CCA1798F30FD88A53D26EC39DE5DDE1F4137B032A4BC34

Validator #2 signature

74C61B5A5E5F5643385ACEB5E2C8FEE230456E092F5D16CCE81A1675E28F8CE27AAE8998B506BB8066192C91147C9A1209DDEC939BAA9700B2EA9B3E8D9A7B01