Smart Contract

Base64Util

A.a45ead1cf1ca9eda.Base64Util

Valid From

85,389,647

Deployed

2d ago
Feb 25, 2026, 07:03:29 PM UTC

Dependents

0 imports
1/// Base64Util contract
2///
3/// Used by FlowRewards to encode NFT SVG metadata
4///
5/// CREDIT: This contract was originally found on Mainnet at 0x754c1187102a4b94 by an uknown author
6///     and was adapted for use in the FlowRewards contract.
7///     Many thanks to the original developer!
8///     https://contractbrowser.com/A.754c1187102a4b94.Base64Util
9///
10access(all) contract Base64Util {
11    access(all) fun encodeFromDict(_ dict: {String: String}): String {
12        let jsonStr = Base64Util.dictToJsonStr(dict)
13        return Base64Util.encode(jsonStr)
14    }
15
16    access(all) fun encode(_ str: String): String {
17        let base64Map: {UInt8: String} = {
18            0: "A", 1: "B", 2: "C", 3: "D",
19            4: "E", 5: "F", 6: "G", 7: "H",
20            8: "I", 9: "J", 10: "K", 11: "L",
21            12: "M", 13: "N", 14: "O", 15: "P",
22            16: "Q", 17: "R", 18: "S", 19: "T",
23            20: "U", 21: "V", 22: "W", 23: "X",
24            24: "Y", 25: "Z", 26: "a", 27: "b",
25            28: "c", 29: "d", 30: "e", 31: "f",
26            32: "g", 33: "h", 34: "i", 35: "j",
27            36: "k", 37: "l", 38: "m", 39: "n",
28            40: "o", 41: "p", 42: "q", 43: "r",
29            44: "s", 45: "t", 46: "u", 47: "v",
30            48: "w", 49: "x", 50: "y", 51: "z",
31            52: "0", 53: "1", 54: "2", 55: "3",
32            56: "4", 57: "5", 58: "6", 59: "7",
33            60: "8", 61: "9", 62: "+", 63: "/"
34        }
35
36        var res: [Character] = []
37        let bytes = str.utf8
38        let remainder = bytes.length % 3
39        while bytes.length % 3 != 0 {
40            bytes.append(0)
41        }
42        var i = 0
43        while i < bytes.length {
44            res.append(base64Map[bytes[i] >> 2]![0])
45            res.append((base64Map[((bytes[i] << 6) >> 2) + (bytes[i + 1] >> 4)]!)[0])
46            res.append((base64Map[((bytes[i + 1] << 4) >> 2) + (bytes[i + 2] >> 6)]!)[0])
47            res.append((base64Map[((bytes[i + 2] << 2) >> 2)]!)[0])
48            i = i + 3
49        }
50        if remainder > 0 {
51            res = res.slice(from: 0, upTo: res.length - (remainder == 1 ? 2 : 1))
52        }
53        return String.fromCharacters(res)
54    }
55
56    access(self) fun dictToJsonStr(_ dict: {String: String}): String {
57        var res = "{"
58        var flag = false
59        for key in dict.keys {
60            if !flag {
61                flag = true
62            } else {
63                res = res.concat(",")
64            }
65            res = res.concat("\"")
66                    .concat(key)
67                    .concat("\":\"")
68                    .concat(Base64Util.escape(dict[key]!))
69                    .concat("\"")
70        }
71        res = res.concat("}")
72        return res
73    }
74
75    access(self) fun escape(_ str: String): String {
76        var res = ""
77        var i = 0
78        while i < str.length {
79            let s = str.slice(from: i, upTo: i + 1)
80            if s == "\"" || s == "\\" {
81            res = res.concat("\\")
82            }
83            res = res.concat(s)
84            i = i + 1
85        }
86        return res
87    }
88}