Skip to content

Commit

Permalink
Merge pull request #38 from portto/fix/solana-partial-sign
Browse files Browse the repository at this point in the history
Fix solana partial sign
  • Loading branch information
andrew54068 authored Nov 27, 2022
2 parents 8a93b1d + 28ccd42 commit dc9a446
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 60 deletions.
86 changes: 46 additions & 40 deletions Example/BloctoSDK/ViewControllers/SolanaDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,15 @@ final class SolanaDemoViewController: UIViewController {
transaction: transaction
) { [weak self] result in
guard let self = self else { return }
self.resetSetValueStatus()
switch result {
case let .success(txHsh):
self.setValueResultLabel.text = txHsh
self.setValueExplorerButton.isHidden = false
case let .failure(error):
self.handleSetValueError(error)
DispatchQueue.main.async {
self.resetSetValueStatus()
switch result {
case let .success(txHsh):
self.setValueResultLabel.text = txHsh
self.setValueExplorerButton.isHidden = false
case let .failure(error):
self.handleSetValueError(error)
}
}
}
}
Expand All @@ -660,17 +662,19 @@ final class SolanaDemoViewController: UIViewController {
connetion.getAccountInfo(
publicKey: dappPublicKey) { [weak self] (result: Result<AccountInfo<ValueAccountData>?, Connection.Error>) in
guard let self = self else { return }
self.resetGetValueStatus()
switch result {
case let .success(valueAccount):
guard let data = valueAccount?.data else {
self.handleGetValueError(Error.message("data not found."))
return
}
self.getValueResultLabel.text = "\(data.value)"
case let .failure(error):
debugPrint(error)
self.handleGetValueError(error)
DispatchQueue.main.async {
self.resetGetValueStatus()
switch result {
case let .success(valueAccount):
guard let data = valueAccount?.data else {
self.handleGetValueError(Error.message("data not found."))
return
}
self.getValueResultLabel.text = "\(data.value)"
case let .failure(error):
debugPrint(error)
self.handleGetValueError(error)
}
}
}
}
Expand Down Expand Up @@ -729,31 +733,33 @@ final class SolanaDemoViewController: UIViewController {
solanaAddress: userWalletAddress
) { [weak self] result in
guard let self = self else { return }
switch result {
case let .success(transaction):
do {
var newTransaction = transaction
try newTransaction.partialSign(signers: [newAccount])

self.bloctoSolanaSDK.signAndSendTransaction(
from: userWalletAddress,
transaction: newTransaction
) { [weak self] result in
guard let self = self else { return }
self.resetPartialSignTxStatus()
switch result {
case let .success(txHash):
self.partialSignTxResultLabel.text = txHash
self.partialSignTxExplorerButton.isHidden = false
case let .failure(error):
self.handlePartialSignTxError(error)
DispatchQueue.main.async {
switch result {
case let .success(transaction):
do {
var newTransaction = transaction
try newTransaction.partialSign(signers: [newAccount])

self.bloctoSolanaSDK.signAndSendTransaction(
from: userWalletAddress,
transaction: newTransaction
) { [weak self] result in
guard let self = self else { return }
self.resetPartialSignTxStatus()
switch result {
case let .success(txHash):
self.partialSignTxResultLabel.text = txHash
self.partialSignTxExplorerButton.isHidden = false
case let .failure(error):
self.handlePartialSignTxError(error)
}
}
} catch {
self.handlePartialSignTxError(error)
}
} catch {
case let .failure(error):
self.handlePartialSignTxError(error)
}
case let .failure(error):
self.handlePartialSignTxError(error)
}
}
}
} catch {
Expand Down
10 changes: 8 additions & 2 deletions Example/Tests/Methods/URLSessionMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ class URLSessionMock: URLSessionProtocol {
with request: URLRequest,
completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void
) -> URLSessionDataTask {
guard let url = request.url else {
return URLSessionDataTaskMock {
completionHandler(nil, nil, nil)
}
}
let error = error
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)
if let dataString = responseJsonString {
return URLSessionDataTaskMock {
completionHandler(Data(dataString.utf8), nil, error)
completionHandler(Data(dataString.utf8), response, error)
}
}
return URLSessionDataTaskMock {
completionHandler(nil, nil, error)
completionHandler(nil, response, error)
}
}

Expand Down
12 changes: 6 additions & 6 deletions Example/Tests/QueryItemCoding/QueryItemEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
func testDictionaryDecoding() throws {
// Given:
let param = [
QueryName.publicKeySignaturePairs.rawValue + "%5Btest1%5D": "1",
QueryName.publicKeySignaturePairs.rawValue + "%5Btest2%5D": "2"
QueryName.publicKeySignaturePairs.rawValue + "[test1]": "1",
QueryName.publicKeySignaturePairs.rawValue + "[test2]": "2"
]

let expect: [String: String] = [
Expand Down Expand Up @@ -64,8 +64,8 @@
"test2": data2
])
let expect: [URLQueryItem] = [
URLQueryItem(name: QueryName.appendTx.rawValue + "%5Btest1%5D", value: "0x1234"),
URLQueryItem(name: QueryName.appendTx.rawValue + "%5Btest2%5D", value: "0x2345")
URLQueryItem(name: QueryName.appendTx.rawValue + "[test1]", value: "0x1234"),
URLQueryItem(name: QueryName.appendTx.rawValue + "[test2]", value: "0x2345")
]

// When:
Expand All @@ -78,8 +78,8 @@
func testDictionaryDataDecoding() throws {
// Given:
let param = [
QueryName.appendTx.rawValue + "%5Btest1%5D": "1234",
QueryName.appendTx.rawValue + "%5Btest2%5D": "2345"
QueryName.appendTx.rawValue + "[test1]": "1234",
QueryName.appendTx.rawValue + "[test2]": "2345"
]

let expect: [String: Data] = [
Expand Down
10 changes: 5 additions & 5 deletions Sources/Core/Utilities/QueryDecoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public enum QueryDecoding {
param: [String: String],
target: String
) throws -> [String] {
let leftBrackets = QueryEscape.escape("[")
let rightBrackets = QueryEscape.escape("]")
let leftBrackets = "["
let rightBrackets = "]"
let array: [String] = param.reduce([]) {
if $1.key == (target + leftBrackets + rightBrackets) {
var copied = $0
Expand All @@ -100,14 +100,14 @@ public enum QueryDecoding {
param: [String: String],
target: String
) throws -> [String: Any] {
let leftBrackets = QueryEscape.escape("[")
let rightBrackets = QueryEscape.escape("]")
let leftBrackets = "["
let rightBrackets = "]"
let targets: [String: String] = param.reduce([:]) {
var copied = $0
copied[$1.key] = ($1.key.contains(target) ? $1.value : nil)
return copied
}
let regex = try NSRegularExpression(pattern: "(?<=\(leftBrackets))(.*?)(?=\(rightBrackets))")
let regex = try NSRegularExpression(pattern: #"(?<=\\#(leftBrackets))(.*?)(?=\\#(rightBrackets))"#)
let value = targets.reduce([:]) { result, target -> [String: String] in
var finalResult = result
if let result: NSTextCheckingResult = regex.firstMatch(
Expand Down
10 changes: 5 additions & 5 deletions Sources/Core/Utilities/URLEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ public enum URLEncoding {
let boolEncoding = BoolEncoding()
components.append(
.init(
name: escape(key),
name: key,
value: escape(boolEncoding.encode(value: number.boolValue))
))
} else {
components.append(
.init(
name: escape(key),
name: key,
value: escape("\(number)")
))
}
case let bool as Bool:
let boolEncoding = BoolEncoding()
components.append(
.init(
name: escape(key),
name: key,
value: escape(boolEncoding.encode(value: bool))
))
case let data as Data:
components.append(
.init(
name: escape(key),
name: key,
value: data.bloctoSDK.hexStringWith0xPrefix
))
case let string as String:
Expand All @@ -75,7 +75,7 @@ public enum URLEncoding {
default:
components.append(
.init(
name: escape(key),
name: key,
value: "\(value)"
))
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/Flow/Extensions/URLSessionExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ extension URLSession {

private func data(for request: URLRequest) async throws -> Data {
try await withCheckedThrowingContinuation { continuation in
dataTask(with: request) { data, _, error in
dataTask(with: request) { data, response, error in
guard let response = response as? HTTPURLResponse,
(200 ..< 300) ~= response.statusCode else {
log(enable: BloctoSDK.shared.logging, message: "error: \(error.debugDescription)")
if let data = data,
let text = String(data: data, encoding: .utf8) {
log(enable: BloctoSDK.shared.logging, message: "response: \(text)")
}
continuation.resume(with: .failure(BloctoSDKError.responseUnexpected))
return
}
if let error = error {
continuation.resume(with: .failure(error))
} else if let data = data {
Expand Down
12 changes: 11 additions & 1 deletion Sources/Solana/BloctoSolanaSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,21 @@ public class BloctoSolanaSDK {
]
)
session
.dataTask(with: request) { [weak self] data, _, error in
.dataTask(with: request) { [weak self] data, response, error in
do {
guard let self = self else {
throw BloctoSDKError.callbackSelfNotfound
}
guard let response = response as? HTTPURLResponse,
(200 ..< 300) ~= response.statusCode else {
log(enable: self.base.logging, message: "error: \(error.debugDescription)")
if let data = data,
let text = String(data: data, encoding: .utf8) {
log(enable: self.base.logging, message: "response: \(text)")
}
completion(.failure(BloctoSDKError.responseUnexpected))
return
}
if let error = error {
completion(.failure(error))
return
Expand Down

0 comments on commit dc9a446

Please sign in to comment.