Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reward calc #10

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/Packages
/*.xcodeproj
xcuserdata/
Tests/Dave-key
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ let package = Package(
name: "ArweaveTests",
dependencies: ["Arweave"],
path: "Tests",
resources: [.process("test-key.json")])
resources: [.process("test-key.json"), .process("dave-key")])
]
)
4 changes: 3 additions & 1 deletion Sources/API/Arweave.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation

let MIN_BYTE_SIZE = 256 * 1024

public struct Arweave {
static let shared = Arweave()
private init() {}
Expand Down Expand Up @@ -42,7 +44,7 @@ extension Arweave {
case let .walletBalance(walletAddress):
return "/wallet/\(walletAddress)/balance"
case let .reward(request):
var path = "/price/\(String(request.bytes))"
var path = "/price/\(String(request.bytesBase64URLEncoded.lengthOfBytes(using: .utf8) < MIN_BYTE_SIZE ? MIN_BYTE_SIZE : request.bytesBase64URLEncoded.lengthOfBytes(using: .utf8)))" // the new fee structure demands a minimum payout
if let target = request.target {
path.append("/\(target.address)")
}
Expand Down
9 changes: 3 additions & 6 deletions Sources/API/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Foundation

extension String: Error { }

struct HttpResponse {
let data: Data
let statusCode: Int
public struct HttpResponse {
public let data: Data
public let statusCode: Int
}

struct HttpClient {
Expand All @@ -22,9 +22,6 @@ struct HttpClient {
let statusCode = httpResponse?.statusCode ?? 0

if case .transactionStatus = target.route {}
else if statusCode != 200 {
throw "Bad response code \(statusCode)"
}

return HttpResponse(data: data, statusCode: statusCode)
}
Expand Down
67 changes: 66 additions & 1 deletion Sources/KeyUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,79 @@ extension Digest {
var data: Data { Data(bytes) }
}

extension String {
public extension String {
var base64URLEncoded: String {
Data(utf8).base64URLEncodedString()
}

var base64URLDecoded: String {
guard let data = Data(base64URLEncoded: self) else { return "" }
return String(data: data, encoding: .utf8) ?? ""
}
}

extension Array where Element == Data {
var combined: Data {
reduce(.init(), +)
}
}

func concatData(data: [Data]) -> Data {
var length = 0
for d in data {
length += d.count
}

var temp = Data(capacity: length)
for d in data {
temp.append(d)
}

return temp
}

func deepHash(data: DeepHashChunk) -> Data {
if case .dataArray(let dataArray) = data {
let tag = concatData(data: [
"list".data(using: .utf8)!,
String(dataArray.count).data(using: .utf8)!
])

return deepHashChunks(chunks: dataArray, acc: SHA384.hash(data: tag).data)
}

if case .data(let dataItem) = data {
let tag = concatData(data: [
"blob".data(using: .utf8)!,
dataItem.count.description.data(using: .utf8)!
])

let taggedHash = concatData(data: [
SHA384.hash(data: tag).data,
SHA384.hash(data: dataItem).data
])

return SHA384.hash(data: taggedHash).data
}

return "".data(using: .utf8)!
}

func deepHashChunks(chunks: DeepHashChunks, acc: Data) -> Data {
if chunks.count < 1 {
return acc
}

let hashPair = concatData(data: [
acc,
deepHash(data: chunks.first!)
])
let newAcc = SHA384.hash(data: hashPair).data
return deepHashChunks(chunks: Array(chunks[1..<chunks.count]), acc: newAcc)
}

enum DeepHashChunk {
case data(Data)
case dataArray(DeepHashChunks)
}
typealias DeepHashChunks = Array<DeepHashChunk>
Loading