From 6a8fb082cf201fc3e337c764727149daf327a4a7 Mon Sep 17 00:00:00 2001 From: Axel Benjaminsson Date: Sat, 8 Apr 2023 17:35:32 +0200 Subject: [PATCH] Add bs64 encode implementation --- addons/solana_client/scripts/bs64.gd | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/addons/solana_client/scripts/bs64.gd b/addons/solana_client/scripts/bs64.gd index 7becb62..e0c0dd9 100644 --- a/addons/solana_client/scripts/bs64.gd +++ b/addons/solana_client/scripts/bs64.gd @@ -8,7 +8,26 @@ const mapping := [ ] static func encode(bytes: PackedByteArray) -> String: - return "" + var r: String = ""; + var p: String = ""; + + var c = bytes.size() % 3; + + if c > 0: + for i in range(c, 3): + p += '='; + bytes.append(0); + + for i in range(0, bytes.size(), 3): + var n = (bytes[i] << 16) + (bytes[i + 1] << 8) + bytes[i + 2] + + r += mapping[(n >> 18) & 63] + r += mapping[(n >> 12) & 63] + r += mapping[(n >> 6) & 63] + r += mapping[n & 63]; + + return r.substr(0, r.length() - p.length()) + p; + static func decode(str: String) -> PackedByteArray: var ret := PackedByteArray()