Skip to content

Commit

Permalink
Change wnfsKey type
Browse files Browse the repository at this point in the history
  • Loading branch information
Homayoun authored and Homayoun committed Jul 27, 2023
1 parent 2ea002a commit ac5121d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 32 deletions.
35 changes: 4 additions & 31 deletions Sources/Wnfs/Wnfs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,6 @@ func toData(ptr: UnsafePointer<UInt8>?, size: Int) -> Data? {
return Data(buffer: buffer)
}

public class WnfsResult<T>{
private var _ok: Bool
private var _error: String?
private var _result: T
public init(ok: Bool, error: String?, result: T) {
self._ok = ok
self._error = error
self._result = result
}

public func ok() -> Bool {
return self._ok
}

public func error() -> String?{
return self._error
}

public func getResult() -> T {
return self._result
}
}

enum MyError: Error {
case runtimeError(String)
}
Expand Down Expand Up @@ -134,25 +111,21 @@ public class Wnfs {
self.blockStoreInterface = BlockStoreInterface(userdata: userdata, put_fn: cPutFn, get_fn: cGetFn, dealloc_after_get: cGetDeallocFn, dealloc_after_put: cPutDeallocFn)
}

public func Init(wnfsKey: String) throws -> Cid {
let msg = wnfsKey.data(using: .utf8)!
let hashed = sha256(data: msg)
public func Init(wnfsKey: Data) throws -> Cid {
var wnfs_key_ptr: UnsafePointer<UInt8>?
var wnfs_key_size: Int?
hashed.withUnsafeBytes { (unsafeBytes) in
wnfsKey.withUnsafeBytes { (unsafeBytes) in
wnfs_key_ptr = unsafeBytes.bindMemory(to: UInt8.self).baseAddress!
wnfs_key_size = unsafeBytes.count
}
let ptr = init_native(self.blockStoreInterface, RustBytes(data: wnfs_key_ptr, len: wnfs_key_size!, cap: wnfs_key_size!))
return try self.consumeRustResult_RustString(ptr)
}

public func LoadWithWNFSKey(wnfsKey: String, cid: Cid) throws {
let msg = wnfsKey.data(using: .utf8)!
let hashed = sha256(data: msg)
public func LoadWithWNFSKey(wnfsKey: Data, cid: Cid) throws {
var wnfs_key_ptr: UnsafePointer<UInt8>?
var wnfs_key_size: Int?
hashed.withUnsafeBytes { (unsafeBytes) in
wnfsKey.withUnsafeBytes { (unsafeBytes) in
wnfs_key_ptr = unsafeBytes.bindMemory(to: UInt8.self).baseAddress!
wnfs_key_size = unsafeBytes.count
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/WnfsTests/WnfsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class WnfsSwiftTest: XCTestCase {

func testOverall() throws {
let wnfs = Wnfs(putFn: mockFulaPut, getFn: mockFulaGet)
var cid = try wnfs.Init(wnfsKey: "test")
var cid = try wnfs.Init(wnfsKey: "test".data(using: .utf8)!)

let data = "hello, world!".data(using: .utf8)!
cid = try wnfs.WriteFile(cid: cid, remotePath: "/root/file.txt", data: data)
Expand Down

0 comments on commit ac5121d

Please sign in to comment.