|
13 | 13 | ;; Map to track relayer addresses for payments |
14 | 14 | ;; Universal address is keccak256(stacks-principal-as-string) |
15 | 15 | (define-map relayer-to-stacks |
16 | | - (buff 32) ;; Universal address (32-byte hash) |
17 | | - principal ;; Stacks principal for STX payments |
| 16 | + (buff 32) ;; Universal address (32-byte hash) |
| 17 | + principal ;; Stacks principal for STX payments |
18 | 18 | ) |
19 | 19 |
|
20 | 20 | ;;;; Public functions |
21 | 21 |
|
22 | 22 | ;; @desc Register a relayer's Stacks address for their universal address |
23 | 23 | ;; Anyone can call this to register themselves as a relayer |
24 | 24 | (define-public (register-relayer (stacks-addr principal)) |
25 | | - (let ((p-as-string (principal-to-string stacks-addr)) |
26 | | - (universal-addr (keccak256 (string-ascii-to-buff p-as-string)))) |
27 | | - |
| 25 | + (let ( |
| 26 | + (p-as-string (principal-to-string stacks-addr)) |
| 27 | + (universal-addr (keccak256 (string-ascii-to-buff p-as-string))) |
| 28 | + ) |
28 | 29 | ;; Check if relayer already exists |
29 | | - (asserts! (is-none (relayer-to-stacks-get universal-addr)) ERR_STATE_RELAYER_EXISTS) |
30 | | - |
| 30 | + (asserts! (is-none (relayer-to-stacks-get universal-addr)) |
| 31 | + ERR_STATE_RELAYER_EXISTS |
| 32 | + ) |
| 33 | + |
31 | 34 | ;; Register the mapping |
32 | 35 | (map-set relayer-to-stacks universal-addr stacks-addr) |
33 | | - |
| 36 | + |
34 | 37 | ;; Return the universal address for confirmation |
35 | | - (ok universal-addr))) |
| 38 | + (ok universal-addr) |
| 39 | + ) |
| 40 | +) |
36 | 41 |
|
37 | 42 | ;;;; Read-only functions |
38 | 43 |
|
39 | 44 | ;; @desc Convert universal address to Stacks principal |
40 | 45 | (define-read-only (universal-addr-to-principal (universal-addr (buff 32))) |
41 | | - (relayer-to-stacks-get universal-addr)) |
| 46 | + (relayer-to-stacks-get universal-addr) |
| 47 | +) |
42 | 48 |
|
43 | 49 | ;; @desc Helper function to convert string to buffer for hashing |
44 | 50 | ;; Matches the exact implementation from Wormhole Core |
|
48 | 54 | ;; bytes[0]: Consensus Buff Type |
49 | 55 | ;; bytes[1..4]: String length |
50 | 56 | ;; bytes[5..]: String data |
51 | | - (unwrap-panic (slice? cb u5 (len cb))))) |
| 57 | + (unwrap-panic (slice? cb u5 (len cb))) |
| 58 | + ) |
| 59 | +) |
52 | 60 |
|
53 | 61 | ;; principle to string conversion from https://explorer.hiro.so/txid/0xa0988bb5f2aa6179e61e7735b91f7276cf70106f05781a0c1c7dee663be5dc7c?chain=mainnet |
54 | 62 | ;; @desc Convert principal to string representation (C32 encoding) |
55 | 63 | (define-read-only (principal-to-string (p principal)) |
56 | | - (let ((destructed (unwrap-panic (principal-destruct? p))) |
57 | | - (checksum (unwrap-panic (slice? (sha256 (sha256 (concat (get version destructed) (get hash-bytes destructed)))) u0 u4))) |
58 | | - (data (unwrap-panic (as-max-len? (concat (get hash-bytes destructed) checksum) u24))) |
59 | | - (result (concat (concat "S" (unwrap-panic (element-at? C32 (buff-to-uint-be (get version destructed))))) (append-leading-0 data (trim-leading-0 (hash-bytes-to-string data)))))) |
60 | | - (match (get name destructed) n (concat (concat result ".") n) result))) |
| 64 | + (let ( |
| 65 | + (destructed (unwrap-panic (principal-destruct? p))) |
| 66 | + (checksum (unwrap-panic (slice? |
| 67 | + (sha256 (sha256 (concat (get version destructed) (get hash-bytes destructed)))) |
| 68 | + u0 u4 |
| 69 | + ))) |
| 70 | + (data (unwrap-panic (as-max-len? (concat (get hash-bytes destructed) checksum) u24))) |
| 71 | + (result (concat |
| 72 | + (concat "S" |
| 73 | + (unwrap-panic (element-at? C32 (buff-to-uint-be (get version destructed)))) |
| 74 | + ) |
| 75 | + (append-leading-0 data (trim-leading-0 (hash-bytes-to-string data))) |
| 76 | + )) |
| 77 | + ) |
| 78 | + (match (get name destructed) |
| 79 | + n (concat (concat result ".") n) |
| 80 | + result |
| 81 | + ) |
| 82 | + ) |
| 83 | +) |
61 | 84 |
|
62 | 85 | ;; Constants and helpers for C32 encoding |
63 | 86 | (define-constant C32 "0123456789ABCDEFGHJKMNPQRSTVWXYZ") |
64 | 87 | (define-constant LIST_15 (list 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)) |
65 | 88 | (define-constant LIST_24 (list 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)) |
66 | 89 | (define-constant LIST_39 (concat LIST_24 LIST_15)) |
67 | 90 |
|
68 | | -(define-read-only (c32-to-string-iter (idx int) (it { s: (string-ascii 39), r: uint })) |
69 | | - { s: (unwrap-panic (as-max-len? (concat (unwrap-panic (element-at? C32 (mod (get r it) u32))) (get s it)) u39)), r: (/ (get r it) u32) }) |
| 91 | +(define-read-only (c32-to-string-iter |
| 92 | + (idx int) |
| 93 | + (it { |
| 94 | + s: (string-ascii 39), |
| 95 | + r: uint, |
| 96 | + }) |
| 97 | + ) |
| 98 | + { |
| 99 | + s: (unwrap-panic (as-max-len? |
| 100 | + (concat (unwrap-panic (element-at? C32 (mod (get r it) u32))) (get s it)) |
| 101 | + u39 |
| 102 | + )), |
| 103 | + r: (/ (get r it) u32), |
| 104 | + } |
| 105 | +) |
70 | 106 |
|
71 | 107 | (define-read-only (hash-bytes-to-string (data (buff 24))) |
72 | | - (let ((low-part (get s (fold c32-to-string-iter LIST_24 { s: "", r: (buff-to-uint-be (unwrap-panic (as-max-len? (unwrap-panic (slice? data u9 u24)) u16)))}))) |
73 | | - (high-part (get s (fold c32-to-string-iter LIST_15 { s: "", r: (buff-to-uint-be (unwrap-panic (as-max-len? (unwrap-panic (slice? data u0 u9)) u16)))})))) |
74 | | - (unwrap-panic (as-max-len? (concat high-part low-part) u39)))) |
| 108 | + (let ( |
| 109 | + (low-part (get s |
| 110 | + (fold c32-to-string-iter LIST_24 { |
| 111 | + s: "", |
| 112 | + r: (buff-to-uint-be (unwrap-panic (as-max-len? (unwrap-panic (slice? data u9 u24)) u16))), |
| 113 | + }) |
| 114 | + )) |
| 115 | + (high-part (get s |
| 116 | + (fold c32-to-string-iter LIST_15 { |
| 117 | + s: "", |
| 118 | + r: (buff-to-uint-be (unwrap-panic (as-max-len? (unwrap-panic (slice? data u0 u9)) u16))), |
| 119 | + }) |
| 120 | + )) |
| 121 | + ) |
| 122 | + (unwrap-panic (as-max-len? (concat high-part low-part) u39)) |
| 123 | + ) |
| 124 | +) |
75 | 125 |
|
76 | | -(define-read-only (trim-leading-0-iter (idx int) (it (string-ascii 39))) |
77 | | - (if (is-eq (element-at? it u0) (some "0")) (unwrap-panic (slice? it u1 (len it))) it)) |
| 126 | +(define-read-only (trim-leading-0-iter |
| 127 | + (idx int) |
| 128 | + (it (string-ascii 39)) |
| 129 | + ) |
| 130 | + (if (is-eq (element-at? it u0) (some "0")) |
| 131 | + (unwrap-panic (slice? it u1 (len it))) |
| 132 | + it |
| 133 | + ) |
| 134 | +) |
78 | 135 |
|
79 | 136 | (define-read-only (trim-leading-0 (s (string-ascii 39))) |
80 | | - (fold trim-leading-0-iter LIST_39 s)) |
| 137 | + (fold trim-leading-0-iter LIST_39 s) |
| 138 | +) |
81 | 139 |
|
82 | | -(define-read-only (append-leading-0-iter (idx int) (it { hash-bytes: (buff 24), address: (string-ascii 39)})) |
| 140 | +(define-read-only (append-leading-0-iter |
| 141 | + (idx int) |
| 142 | + (it { |
| 143 | + hash-bytes: (buff 24), |
| 144 | + address: (string-ascii 39), |
| 145 | + }) |
| 146 | + ) |
83 | 147 | (if (is-eq (element-at? (get hash-bytes it) u0) (some 0x00)) |
84 | | - { hash-bytes: (unwrap-panic (slice? (get hash-bytes it) u1 (len (get hash-bytes it)))), address: (unwrap-panic (as-max-len? (concat "0" (get address it)) u39)) } |
85 | | - it)) |
| 148 | + { |
| 149 | + hash-bytes: (unwrap-panic (slice? (get hash-bytes it) u1 (len (get hash-bytes it)))), |
| 150 | + address: (unwrap-panic (as-max-len? (concat "0" (get address it)) u39)), |
| 151 | + } |
| 152 | + it |
| 153 | + ) |
| 154 | +) |
86 | 155 |
|
87 | | -(define-read-only (append-leading-0 (hash-bytes (buff 24)) (s (string-ascii 39))) |
88 | | - (get address (fold append-leading-0-iter LIST_24 { hash-bytes: hash-bytes, address: s }))) |
| 156 | +(define-read-only (append-leading-0 |
| 157 | + (hash-bytes (buff 24)) |
| 158 | + (s (string-ascii 39)) |
| 159 | + ) |
| 160 | + (get address |
| 161 | + (fold append-leading-0-iter LIST_24 { |
| 162 | + hash-bytes: hash-bytes, |
| 163 | + address: s, |
| 164 | + }) |
| 165 | + ) |
| 166 | +) |
89 | 167 |
|
90 | 168 | ;;;; Map getters |
91 | 169 |
|
92 | 170 | (define-read-only (relayer-to-stacks-get (universal-addr (buff 32))) |
93 | | - (map-get? relayer-to-stacks universal-addr)) |
94 | | - |
| 171 | + (map-get? relayer-to-stacks universal-addr) |
| 172 | +) |
0 commit comments