;; Base58 decoding function for Clarity
;; Takes a base58 string and returns its hex representation
(define-constant BASE58_ALPHABET "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
(define-constant ALL_HEX 0x000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF)
(define-private (char-to-u8 (c (string-ascii 1)))
(unwrap-panic
(index-of BASE58_ALPHABET c)))
;; Convert uint to a single byte
(define-read-only (uint-to-buff (val uint))
(unwrap-panic (element-at? ALL_HEX val)))
(define-read-only (b58-decode (input (string-ascii 34)))
(let
(
;; Extract characters from the input string
(c1 (unwrap-panic (element-at input u0)))
(c2 (unwrap-panic (element-at input u1)))
(c3 (unwrap-panic (element-at input u2)))
(c4 (unwrap-panic (element-at input u3)))
(c5 (unwrap-panic (element-at input u4)))
(c6 (unwrap-panic (element-at input u5)))
(c7 (unwrap-panic (element-at input u6)))
(c8 (unwrap-panic (element-at input u7)))
(c9 (unwrap-panic (element-at input u8)))
(c10 (unwrap-panic (element-at input u9)))
(c11 (unwrap-panic (element-at input u10)))
(c12 (unwrap-panic (element-at input u11)))
(c13 (unwrap-panic (element-at input u12)))
(c14 (unwrap-panic (element-at input u13)))
(c15 (unwrap-panic (element-at input u14)))
(c16 (unwrap-panic (element-at input u15)))
(c17 (unwrap-panic (element-at input u16)))
(c18 (unwrap-panic (element-at input u17)))
(c19 (unwrap-panic (element-at input u18)))
(c20 (unwrap-panic (element-at input u19)))
(c21 (unwrap-panic (element-at input u20)))
(c22 (unwrap-panic (element-at input u21)))
(c23 (unwrap-panic (element-at input u22)))
(c24 (unwrap-panic (element-at input u23)))
(c25 (unwrap-panic (element-at input u24)))
(c26 (unwrap-panic (element-at input u25)))
(c27 (unwrap-panic (element-at input u26)))
(c28 (unwrap-panic (element-at input u27)))
(c29 (unwrap-panic (element-at input u28)))
(c30 (unwrap-panic (element-at input u29)))
(c31 (unwrap-panic (element-at input u30)))
(c32 (unwrap-panic (element-at input u31)))
(c33 (unwrap-panic (element-at input u32)))
(c34 (unwrap-panic (element-at input u33)))
;; Convert each character to its numeric value in the Base58 alphabet
(v1 (char-to-u8 c1))
(v2 (char-to-u8 c2))
(v3 (char-to-u8 c3))
(v4 (char-to-u8 c4))
(v5 (char-to-u8 c5))
(v6 (char-to-u8 c6))
(v7 (char-to-u8 c7))
(v8 (char-to-u8 c8))
(v9 (char-to-u8 c9))
(v10 (char-to-u8 c10))
(v11 (char-to-u8 c11))
(v12 (char-to-u8 c12))
(v13 (char-to-u8 c13))
(v14 (char-to-u8 c14))
(v15 (char-to-u8 c15))
(v16 (char-to-u8 c16))
(v17 (char-to-u8 c17))
(v18 (char-to-u8 c18))
(v19 (char-to-u8 c19))
(v20 (char-to-u8 c20))
(v21 (char-to-u8 c21))
(v22 (char-to-u8 c22))
(v23 (char-to-u8 c23))
(v24 (char-to-u8 c24))
(v25 (char-to-u8 c25))
(v26 (char-to-u8 c26))
(v27 (char-to-u8 c27))
(v28 (char-to-u8 c28))
(v29 (char-to-u8 c29))
(v30 (char-to-u8 c30))
(v31 (char-to-u8 c31))
(v32 (char-to-u8 c32))
(v33 (char-to-u8 c33))
(v34 (char-to-u8 c34)))
(list
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34
)
)
)
;; Convert the list of base58 values to a hex buffer - caveman style!
(define-read-only (b58-to-hex (input (list 34 uint)))
(let
(
;; Get values from the list
(v1 (unwrap-panic (element-at? input u0)))
(v2 (unwrap-panic (element-at? input u1)))
(v3 (unwrap-panic (element-at? input u2)))
(v4 (unwrap-panic (element-at? input u3)))
(v5 (unwrap-panic (element-at? input u4)))
(v6 (unwrap-panic (element-at? input u5)))
(v7 (unwrap-panic (element-at? input u6)))
(v8 (unwrap-panic (element-at? input u7)))
(v9 (unwrap-panic (element-at? input u8)))
(v10 (unwrap-panic (element-at? input u9)))
(v11 (unwrap-panic (element-at? input u10)))
(v12 (unwrap-panic (element-at? input u11)))
(v13 (unwrap-panic (element-at? input u12)))
(v14 (unwrap-panic (element-at? input u13)))
(v15 (unwrap-panic (element-at? input u14)))
(v16 (unwrap-panic (element-at? input u15)))
(v17 (unwrap-panic (element-at? input u16)))
(v18 (unwrap-panic (element-at? input u17)))
(v19 (unwrap-panic (element-at? input u18)))
(v20 (unwrap-panic (element-at? input u19)))
(v21 (unwrap-panic (element-at? input u20)))
(v22 (unwrap-panic (element-at? input u21)))
(v23 (unwrap-panic (element-at? input u22)))
(v24 (unwrap-panic (element-at? input u23)))
(v25 (unwrap-panic (element-at? input u24)))
(v26 (unwrap-panic (element-at? input u25)))
(v27 (unwrap-panic (element-at? input u26)))
(v28 (unwrap-panic (element-at? input u27)))
(v29 (unwrap-panic (element-at? input u28)))
(v30 (unwrap-panic (element-at? input u29)))
(v31 (unwrap-panic (element-at? input u30)))
(v32 (unwrap-panic (element-at? input u31)))
(v33 (unwrap-panic (element-at? input u32)))
(v34 (unwrap-panic (element-at? input u33)))
;; Convert each uint to a byte using uint-to-buff
(b1 (uint-to-buff v1))
(b2 (uint-to-buff v2))
(b3 (uint-to-buff v3))
(b4 (uint-to-buff v4))
(b5 (uint-to-buff v5))
(b6 (uint-to-buff v6))
(b7 (uint-to-buff v7))
(b8 (uint-to-buff v8))
(b9 (uint-to-buff v9))
(b10 (uint-to-buff v10))
(b11 (uint-to-buff v11))
(b12 (uint-to-buff v12))
(b13 (uint-to-buff v13))
(b14 (uint-to-buff v14))
(b15 (uint-to-buff v15))
(b16 (uint-to-buff v16))
(b17 (uint-to-buff v17))
(b18 (uint-to-buff v18))
(b19 (uint-to-buff v19))
(b20 (uint-to-buff v20))
(b21 (uint-to-buff v21))
(b22 (uint-to-buff v22))
(b23 (uint-to-buff v23))
(b24 (uint-to-buff v24))
(b25 (uint-to-buff v25))
(b26 (uint-to-buff v26))
(b27 (uint-to-buff v27))
(b28 (uint-to-buff v28))
(b29 (uint-to-buff v29))
(b30 (uint-to-buff v30))
(b31 (uint-to-buff v31))
(b32 (uint-to-buff v32))
(b33 (uint-to-buff v33))
(b34 (uint-to-buff v34))
;; Concatenate all bytes to form the final hex buffer
(result (unwrap-panic (as-max-len? (concat (concat (concat (concat (concat (concat (concat (concat (concat (concat
(concat (concat (concat (concat (concat (concat (concat (concat (concat (concat
(concat (concat (concat (concat (concat (concat (concat (concat (concat (concat
(concat (concat (concat b1 b2) b3) b4) b5) b6) b7) b8) b9) b10)
b11) b12) b13) b14) b15) b16) b17) b18) b19) b20)
b21) b22) b23) b24) b25) b26) b27) b28) b29) b30)
b31) b32) b33) b34) u34)))
)
result
)
)