Add a getOperations helper to SteemURL

This commit is contained in:
Johan Nordberg 2018-06-12 10:54:32 +02:00 committed by Johan Nordberg
parent 62e38c761d
commit a2fb69b029
3 changed files with 23 additions and 2 deletions

View File

@ -4,7 +4,7 @@
import Foundation
/// A type that represents a operation on the Steem blockchain.
public protocol OperationType: SteemEncodable, Decodable {}
public protocol OperationType: SteemCodable {}
/// Namespace for all available Steem operations.
public struct Operation {

View File

@ -89,7 +89,7 @@ public struct SteemURL {
/// The signing params.
public let params: Params
let type: PayloadType
let payload: Any
@ -159,6 +159,24 @@ public struct SteemURL {
self.params = params
}
/// Returns the operations contained in the signing URL.
public func getOperations() throws -> [OperationType] {
let ops: [Any]?
switch self.type {
case .transaction:
ops = (self.payload as? [String: Any])?["operations"] as? [Any]
case .operation:
ops = [self.payload]
case .operations:
ops = self.payload as? [Any]
}
guard ops != nil else {
throw Error.payloadInvalid
}
let decoded = try decodeObject([AnyOperation].self, from: ops!)
return decoded.map { $0.operation }
}
/// Options used to resolve a signing url to a signer and transaction.
public struct ResolveOptions {
/// The ref block number used to fill in the `__ref_block_num` placeholder.

View File

@ -28,6 +28,9 @@ class SeemURLTest: XCTestCase {
params.noBroadcast = true
let url = SteemURL(operations: operations, params: params)
XCTAssertNotNil(url)
let ops = try url?.getOperations()
XCTAssertEqual(ops?[0] as? Steem.Operation.Vote, (operations[0] as! Steem.Operation.Vote))
XCTAssertEqual(ops?[1] as? Steem.Operation.Transfer, (operations[1] as! Steem.Operation.Transfer))
let options = SteemURL.ResolveOptions(refBlockNum: 0, refBlockPrefix: 1, expiration: Date(timeIntervalSinceReferenceDate: 0), signers: ["foo", "baz"], preferredSigner: "baz")
let result = try url?.resolve(with: options)
XCTAssertEqual(result?.signer, "foo")