Source Code

;; Base58 decoding function for Clarity
;; Takes a base58 string and returns its hex representation

(define-constant BASE58_ALPHABET "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")

(define-private (char-to-u8 (c (string-ascii 1)))
  (unwrap-panic
    (index-of BASE58_ALPHABET c)))

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

;; Example usage:
;; (b58-decode "1JSjpoj1m57KQqd9SvYgFzbRVUwrmk4Emd")
;; This should return the hex representation of the bitcoin address

Functions (2)

FunctionAccessArgs
char-to-u8privatec: (string-ascii 1
b58-decoderead-onlyinput: (string-ascii 34