-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase58.swift
More file actions
52 lines (35 loc) · 1.59 KB
/
Copy pathbase58.swift
File metadata and controls
52 lines (35 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// base58.swift
// Cardano Lib - iOS Test
//
// Created by HM on 21/10/2018.
// Copyright © 2018 Lambo Corp. All rights reserved.
//
import Foundation
class CardanoFuncs {
static func encode(stringToEncode: String) -> String {
let byteArrayToEncode: [UInt8] = Array(stringToEncode.utf8)
let encoded = UnsafeMutableRawPointer.allocate(bytes: 1000, alignedTo: MemoryLayout<UInt8>.alignment)
let opaquePtr = OpaquePointer(encoded)
let contextPtr = UnsafeMutablePointer<Int8>(opaquePtr)
let status = my_b58_encode(byteArrayToEncode,UInt32(byteArrayToEncode.count), contextPtr)
var encodedString = "*** ERROR ***"
if status >= 0 {
encodedString = String(cString: contextPtr)
}
encoded.deallocate(bytes: 1000, alignedTo: MemoryLayout<UInt8>.alignment)
return encodedString
}
static func decode(stringToDecode: String) -> String {
let raw = UnsafeMutableRawPointer.allocate(bytes: 1000, alignedTo: MemoryLayout<UInt8>.alignment)
let typedPtr = raw.initializeMemory(as: UInt8.self, at: 0, count: 1000, to: 0)
let cstr = stringToDecode.cString(using: String.Encoding.utf8)
let status = my_b58_decode(cstr, typedPtr)
var decodedString = "*** ERROR ***"
if status >= 0 {
decodedString = String(cString: typedPtr)
}
raw.deallocate(bytes: 1000, alignedTo: MemoryLayout<UInt8>.alignment)
return decodedString
}
}