Skip to content

Commit fa26fec

Browse files
committed
format
1 parent 5da33f6 commit fa26fec

File tree

2 files changed

+232
-94
lines changed

2 files changed

+232
-94
lines changed

stacks/contracts/executor-state.clar

Lines changed: 109 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,38 @@
1313
;; Map to track relayer addresses for payments
1414
;; Universal address is keccak256(stacks-principal-as-string)
1515
(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
1818
)
1919

2020
;;;; Public functions
2121

2222
;; @desc Register a relayer's Stacks address for their universal address
2323
;; Anyone can call this to register themselves as a relayer
2424
(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+
)
2829
;; 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+
3134
;; Register the mapping
3235
(map-set relayer-to-stacks universal-addr stacks-addr)
33-
36+
3437
;; Return the universal address for confirmation
35-
(ok universal-addr)))
38+
(ok universal-addr)
39+
)
40+
)
3641

3742
;;;; Read-only functions
3843

3944
;; @desc Convert universal address to Stacks principal
4045
(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+
)
4248

4349
;; @desc Helper function to convert string to buffer for hashing
4450
;; Matches the exact implementation from Wormhole Core
@@ -48,47 +54,119 @@
4854
;; bytes[0]: Consensus Buff Type
4955
;; bytes[1..4]: String length
5056
;; bytes[5..]: String data
51-
(unwrap-panic (slice? cb u5 (len cb)))))
57+
(unwrap-panic (slice? cb u5 (len cb)))
58+
)
59+
)
5260

5361
;; principle to string conversion from https://explorer.hiro.so/txid/0xa0988bb5f2aa6179e61e7735b91f7276cf70106f05781a0c1c7dee663be5dc7c?chain=mainnet
5462
;; @desc Convert principal to string representation (C32 encoding)
5563
(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+
)
6184

6285
;; Constants and helpers for C32 encoding
6386
(define-constant C32 "0123456789ABCDEFGHJKMNPQRSTVWXYZ")
6487
(define-constant LIST_15 (list 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))
6588
(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))
6689
(define-constant LIST_39 (concat LIST_24 LIST_15))
6790

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+
)
70106

71107
(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+
)
75125

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+
)
78135

79136
(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+
)
81139

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+
)
83147
(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+
)
86155

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+
)
89167

90168
;;;; Map getters
91169

92170
(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

Comments
 (0)