|
10 | 10 |
|
11 | 11 | ;;;; Data maps |
12 | 12 |
|
13 | | -;; Map to track relayer addresses for payments |
| 13 | +;; Map to track payee addresses for payments |
14 | 14 | ;; Universal address is keccak256(stacks-principal-as-string) |
15 | | -(define-map relayer-to-stacks |
| 15 | +(define-map universal-address-to-principal |
16 | 16 | (buff 32) ;; Universal address (32-byte hash) |
17 | 17 | principal ;; Stacks principal for STX payments |
18 | 18 | ) |
19 | 19 |
|
20 | 20 | ;;;; Public functions |
21 | 21 |
|
22 | | -;; @desc Register a relayer's Stacks address for their universal address |
23 | | -;; Anyone can call this to register themselves as a relayer |
24 | | -(define-public (register-relayer (stacks-addr principal)) |
| 22 | +;; @desc Register a payee's Stacks address for their universal address |
| 23 | +;; Anyone can call this to register themselves as a payee |
| 24 | +(define-public (register-payee (stacks-addr principal)) |
25 | 25 | (let ( |
26 | | - (p-as-string (principal-to-string stacks-addr)) |
| 26 | + (p-as-string (contract-call? |
| 27 | + 'SP1E0XBN9T4B10E9QMR7XMFJPMA19D77WY3KP2QKC.self-listing-helper-v3 |
| 28 | + principal-to-string stacks-addr |
| 29 | + )) |
27 | 30 | (universal-addr (keccak256 (string-ascii-to-buff p-as-string))) |
28 | 31 | ) |
29 | | - ;; Check if relayer already exists |
30 | | - (asserts! (is-none (relayer-to-stacks-get universal-addr)) |
| 32 | + ;; Check if payee already exists |
| 33 | + (asserts! (is-none (universal-address-to-principal-get universal-addr)) |
31 | 34 | ERR_STATE_RELAYER_EXISTS |
32 | 35 | ) |
33 | 36 |
|
34 | 37 | ;; Register the mapping |
35 | | - (map-set relayer-to-stacks universal-addr stacks-addr) |
| 38 | + (map-set universal-address-to-principal universal-addr stacks-addr) |
36 | 39 |
|
37 | 40 | ;; Return the universal address for confirmation |
38 | 41 | (ok universal-addr) |
|
41 | 44 |
|
42 | 45 | ;;;; Read-only functions |
43 | 46 |
|
44 | | -;; @desc Convert universal address to Stacks principal |
45 | | -(define-read-only (universal-addr-to-principal (universal-addr (buff 32))) |
46 | | - (relayer-to-stacks-get universal-addr) |
47 | | -) |
48 | | - |
49 | 47 | ;; @desc Helper function to convert string to buffer for hashing |
50 | 48 | ;; Matches the exact implementation from Wormhole Core |
51 | 49 | (define-read-only (string-ascii-to-buff (s (string-ascii 256))) |
|
58 | 56 | ) |
59 | 57 | ) |
60 | 58 |
|
61 | | -;; principle to string conversion from https://explorer.hiro.so/txid/0xa0988bb5f2aa6179e61e7735b91f7276cf70106f05781a0c1c7dee663be5dc7c?chain=mainnet |
62 | | -;; @desc Convert principal to string representation (C32 encoding) |
63 | | -(define-read-only (principal-to-string (p principal)) |
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 | | -) |
84 | | - |
85 | | -;; Constants and helpers for C32 encoding |
86 | | -(define-constant C32 "0123456789ABCDEFGHJKMNPQRSTVWXYZ") |
87 | | -(define-constant LIST_15 (list 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)) |
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)) |
89 | | -(define-constant LIST_39 (concat LIST_24 LIST_15)) |
90 | | - |
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 | | -) |
106 | | - |
107 | | -(define-read-only (hash-bytes-to-string (data (buff 24))) |
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 | | -) |
125 | | - |
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 | | -) |
135 | | - |
136 | | -(define-read-only (trim-leading-0 (s (string-ascii 39))) |
137 | | - (fold trim-leading-0-iter LIST_39 s) |
138 | | -) |
139 | | - |
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 | | - ) |
147 | | - (if (is-eq (element-at? (get hash-bytes it) u0) (some 0x00)) |
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 | | -) |
155 | | - |
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 | | -) |
167 | | - |
168 | 59 | ;;;; Map getters |
169 | 60 |
|
170 | | -(define-read-only (relayer-to-stacks-get (universal-addr (buff 32))) |
171 | | - (map-get? relayer-to-stacks universal-addr) |
| 61 | +(define-read-only (universal-address-to-principal-get (universal-addr (buff 32))) |
| 62 | + (map-get? universal-address-to-principal universal-addr) |
172 | 63 | ) |
0 commit comments