|
| 1 | +;; title: executor |
| 2 | +;; version: |
| 3 | +;; summary: |
| 4 | +;; description: |
| 5 | + |
| 6 | +;; traits |
| 7 | +;; |
| 8 | + |
| 9 | +;; token definitions |
| 10 | +;; |
| 11 | + |
| 12 | +;; constants |
| 13 | +(define-constant EXECUTOR-VERSION "Executor-0.0.1") |
| 14 | +(define-constant OUR-CHAIN u1) ;; Must be manually updated before deployment. Worth doing it this way or an initialize call? |
| 15 | + |
| 16 | +;; errors |
| 17 | +(define-constant ERR-QUOTE-SRC-CHAIN-MISMATCH (err u1001)) |
| 18 | +(define-constant ERR-QUOTE-DST-CHAIN-MISMATCH (err u1002)) |
| 19 | +(define-constant ERR-QUOTE-EXPIRED (err u1003)) |
| 20 | +(define-constant ERR-UNREGISTERED-RELAYER (err u1004)) |
| 21 | +(define-constant ERR-INVALID-PAYEE-ADDRESS (err u1005)) |
| 22 | +(define-constant ERR-BUFFER-PARSE-ERROR (err u1006)) |
| 23 | +;; |
| 24 | + |
| 25 | +;; data vars |
| 26 | +;; |
| 27 | + |
| 28 | +;; data maps |
| 29 | +;; |
| 30 | + |
| 31 | +;; public functions |
| 32 | +(define-public (request-execution |
| 33 | + (dst-chain uint) |
| 34 | + (dst-addr (buff 32)) |
| 35 | + (refund-addr principal) |
| 36 | + (signed-quote-bytes (buff 8192)) |
| 37 | + (request-bytes (buff 8192)) |
| 38 | + (relay-instructions (buff 8192)) |
| 39 | + (payment uint)) ;; STX amount in microSTX |
| 40 | + |
| 41 | + (begin |
| 42 | + ;; Validate quote header |
| 43 | + (try! (validate-quote-header signed-quote-bytes dst-chain)) |
| 44 | + |
| 45 | + ;; Extract quoter and payee info |
| 46 | + (match (extract-quote-addresses signed-quote-bytes) |
| 47 | + quote-addresses |
| 48 | + (let ((payee-universal-addr (get payee quote-addresses))) |
| 49 | + ;; 1. Verify universal address is properly formatted (32 bytes, non-zero) |
| 50 | + (asserts! (is-eq (len payee-universal-addr) u32) ERR-INVALID-PAYEE-ADDRESS) |
| 51 | + (asserts! (not (is-eq payee-universal-addr 0x0000000000000000000000000000000000000000000000000000000000000000)) ERR-INVALID-PAYEE-ADDRESS) |
| 52 | + |
| 53 | + ;; 2. Verify relayer is registered and get principal |
| 54 | + (let ((payee-lookup-result (contract-call? .executor-state relayer-to-stacks-get payee-universal-addr))) |
| 55 | + (asserts! (is-some payee-lookup-result) ERR-UNREGISTERED-RELAYER) |
| 56 | + |
| 57 | + ;; 3. Extract the principal and validate it's not contract address |
| 58 | + (let ((payee-principal (unwrap-panic payee-lookup-result))) |
| 59 | + (asserts! (not (is-eq payee-principal (as-contract tx-sender))) ERR-INVALID-PAYEE-ADDRESS) |
| 60 | + |
| 61 | + ;; 4. Perform the payment after all validations pass |
| 62 | + (try! (stx-transfer? payment tx-sender payee-principal)) |
| 63 | + |
| 64 | + ;; Emit event for off-chain relayers |
| 65 | + (print { |
| 66 | + event: "RequestForExecution", |
| 67 | + quoter-address: (get quoter quote-addresses), |
| 68 | + amount-paid: payment, |
| 69 | + dst-chain: dst-chain, |
| 70 | + dst-addr: dst-addr, |
| 71 | + refund-addr: refund-addr, |
| 72 | + signed-quote: signed-quote-bytes, |
| 73 | + request-bytes: request-bytes, |
| 74 | + relay-instructions: relay-instructions, |
| 75 | + block-height: stacks-block-height, |
| 76 | + tx-sender: tx-sender |
| 77 | + }) |
| 78 | + |
| 79 | + (ok true)))) |
| 80 | + err-case |
| 81 | + ERR-BUFFER-PARSE-ERROR))) |
| 82 | +;; |
| 83 | + |
| 84 | +;; read only functions |
| 85 | +;; Extract uint16 from buffer at specific offset (big-endian) using efficient slice operation |
| 86 | +(define-read-only (extract-uint16-be (data (buff 8192)) (offset uint)) |
| 87 | + (let ((extracted (slice? data offset (+ offset u2)))) |
| 88 | + (match extracted |
| 89 | + result (if (is-eq (len result) u2) |
| 90 | + (ok (buff-to-uint-be (unwrap-panic (as-max-len? result u2)))) |
| 91 | + (err ERR-BUFFER-PARSE-ERROR)) |
| 92 | + (err ERR-BUFFER-PARSE-ERROR)))) |
| 93 | + |
| 94 | +;; Extract uint64 from buffer at specific offset (big-endian) using efficient slice operation |
| 95 | +(define-read-only (extract-uint64-be (data (buff 8192)) (offset uint)) |
| 96 | + (let ((extracted (slice? data offset (+ offset u8)))) |
| 97 | + (match extracted |
| 98 | + result (if (is-eq (len result) u8) |
| 99 | + (ok (buff-to-uint-be (unwrap-panic (as-max-len? result u8)))) |
| 100 | + (err ERR-BUFFER-PARSE-ERROR)) |
| 101 | + (err ERR-BUFFER-PARSE-ERROR)))) |
| 102 | + |
| 103 | +;; Extract bytes32 from buffer at specific offset |
| 104 | +;; Returns a properly sized 32-byte buffer using efficient slice operation |
| 105 | +(define-read-only (extract-bytes32 (data (buff 8192)) (offset uint)) |
| 106 | + (let ((extracted (slice? data offset (+ offset u32)))) |
| 107 | + (match extracted |
| 108 | + result (if (is-eq (len result) u32) |
| 109 | + (ok (unwrap-panic (as-max-len? result u32))) |
| 110 | + (err ERR-BUFFER-PARSE-ERROR)) |
| 111 | + (err ERR-BUFFER-PARSE-ERROR)))) |
| 112 | + |
| 113 | +;; Extract address (20 bytes) from buffer at specific offset |
| 114 | +(define-read-only (extract-address (data (buff 8192)) (offset uint)) |
| 115 | + (slice? data offset (+ offset u20))) |
| 116 | + |
| 117 | +;; Validate quote header |
| 118 | +(define-read-only (validate-quote-header (signed-quote-bytes (buff 8192)) (dst-chain uint)) |
| 119 | + (match (extract-uint16-be signed-quote-bytes u56) |
| 120 | + quote-src-chain (match (extract-uint16-be signed-quote-bytes u58) |
| 121 | + quote-dst-chain (match (extract-uint64-be signed-quote-bytes u60) |
| 122 | + expiry-time (if (is-eq quote-src-chain OUR-CHAIN) |
| 123 | + (if (is-eq quote-dst-chain dst-chain) |
| 124 | + ;; Currently comparing Unix timestamp (expiry-time) with block height (stacks-block-height) |
| 125 | + ;; Correct way is likely to do: (get-stacks-block-info? time stacks-block-height) which should return Unix timestamp |
| 126 | + ;; Unable to write tests for it though as of now. |
| 127 | + (if (> expiry-time stacks-block-height) |
| 128 | + (ok true) |
| 129 | + ERR-QUOTE-EXPIRED) |
| 130 | + ERR-QUOTE-DST-CHAIN-MISMATCH) |
| 131 | + ERR-QUOTE-SRC-CHAIN-MISMATCH) |
| 132 | + err3 ERR-BUFFER-PARSE-ERROR) |
| 133 | + err2 ERR-BUFFER-PARSE-ERROR) |
| 134 | + err1 ERR-BUFFER-PARSE-ERROR)) |
| 135 | + |
| 136 | +;; Extract quoter and payee addresses from quote |
| 137 | +(define-read-only (extract-quote-addresses (signed-quote-bytes (buff 8192))) |
| 138 | + (match (extract-address signed-quote-bytes u4) |
| 139 | + quoter-addr (match (extract-bytes32 signed-quote-bytes u24) |
| 140 | + payee-addr-32 (ok { |
| 141 | + quoter: quoter-addr, |
| 142 | + payee: payee-addr-32 |
| 143 | + }) |
| 144 | + err (err ERR-BUFFER-PARSE-ERROR)) |
| 145 | + (err ERR-BUFFER-PARSE-ERROR))) |
| 146 | + |
| 147 | + |
| 148 | +;; Convert 32-byte universal address hash back to a Stacks principal |
| 149 | +;; Uses the executor-state contract's relayer registry |
| 150 | +(define-read-only (universal-addr-to-principal (universal-addr (buff 32))) |
| 151 | + (contract-call? .executor-state universal-addr-to-principal universal-addr)) |
| 152 | + |
| 153 | +;; Read-only functions for external access |
| 154 | +(define-read-only (get-executor-version) |
| 155 | + EXECUTOR-VERSION) |
| 156 | + |
| 157 | +(define-read-only (get-our-chain) |
| 158 | + OUR-CHAIN) |
| 159 | +;; |
| 160 | + |
| 161 | +;; private functions |
| 162 | +;; |
0 commit comments