Simplify SteemURL resolving

This commit is contained in:
Johan Nordberg 2018-06-12 14:38:42 +02:00 committed by Johan Nordberg
parent a2fb69b029
commit 40f2368c06
2 changed files with 11 additions and 20 deletions

View File

@ -185,23 +185,20 @@ public struct SteemURL {
public var refBlockPrefix: UInt32
/// The date string used to fill in the `__expiration` placeholder.
public var expiration: Date
/// List of account names available as signers.
public var signers: [String]
/// Preferred signer if none is explicitly set in params.
public var preferredSigner: String
/// Account name used to fill in `__signer` placeholder.
public var signer: String
/// Create a new instance.
public init(refBlockNum: UInt16, refBlockPrefix: UInt32, expiration: Date, signers: [String], preferredSigner: String) {
public init(refBlockNum: UInt16, refBlockPrefix: UInt32, expiration: Date, signer: String) {
self.refBlockNum = refBlockNum
self.refBlockPrefix = refBlockPrefix
self.expiration = expiration
self.signers = signers
self.preferredSigner = preferredSigner
self.signer = signer
}
}
/// Resolve this url to a signer and transaction.
public func resolve(with options: ResolveOptions) throws -> (signer: String, tx: Transaction) {
public func resolve(with options: ResolveOptions) throws -> Transaction {
let tx: [String: Any]?
switch self.type {
case .transaction:
@ -219,17 +216,13 @@ public struct SteemURL {
guard tx != nil else {
throw Error.payloadInvalid
}
let signer = self.params.signer ?? options.preferredSigner
guard options.signers.contains(signer) else {
throw Error.signerNotAvailable
}
func walk(_ value: Any) -> Any {
switch value {
case let str as String:
if str == "__ref_block_num" { return options.refBlockNum }
if str == "__ref_block_prefix" { return options.refBlockPrefix }
if str == "__expiration" { return Client.dateFormatter.string(from: options.expiration) }
return str.replacingOccurrences(of: "__signer", with: signer)
return str.replacingOccurrences(of: "__signer", with: options.signer)
case let val as Array<Any>:
return val.map(walk)
case let val as Dictionary<String, Any>:
@ -244,7 +237,7 @@ public struct SteemURL {
}
let resolved = walk(tx!)
let transaction = try decodeObject(Transaction.self, from: resolved)
return (signer, transaction)
return transaction
}
/// Used to resolve callback urls, should be populated after signed txn has been broadcast.

View File

@ -11,10 +11,9 @@ class SeemURLTest: XCTestCase {
let url = SteemURL(operation: Operation.Vote(voter: "foo", author: "bar", permlink: "baz"))
XCTAssertEqual(url?.url?.absoluteString, "steem://sign/op/WyJ2b3RlIix7InZvdGVyIjoiZm9vIiwiYXV0aG9yIjoiYmFyIiwicGVybWxpbmsiOiJiYXoiLCJ3ZWlnaHQiOjEwMDAwfV0.")
XCTAssertEqual(url, SteemURL(string: "steem://sign/op/WyJ2b3RlIix7InZvdGVyIjoiZm9vIiwiYXV0aG9yIjoiYmFyIiwicGVybWxpbmsiOiJiYXoiLCJ3ZWlnaHQiOjEwMDAwfV0."))
let options = SteemURL.ResolveOptions(refBlockNum: 0, refBlockPrefix: 1, expiration: Date(timeIntervalSinceReferenceDate: 0), signers: ["foo"], preferredSigner: "foo")
let options = SteemURL.ResolveOptions(refBlockNum: 0, refBlockPrefix: 1, expiration: Date(timeIntervalSinceReferenceDate: 0), signer: "foo")
let result = try? url?.resolve(with: options)
XCTAssertEqual(result??.signer, "foo")
XCTAssertEqual(result??.tx, Transaction(refBlockNum: 0, refBlockPrefix: 1, expiration: Date(timeIntervalSinceReferenceDate: 0), operations: [Operation.Vote(voter: "foo", author: "bar", permlink: "baz")], extensions: []))
XCTAssertEqual(result, Transaction(refBlockNum: 0, refBlockPrefix: 1, expiration: Date(timeIntervalSinceReferenceDate: 0), operations: [Operation.Vote(voter: "foo", author: "bar", permlink: "baz")], extensions: []))
}
func testParams() throws {
@ -31,10 +30,9 @@ class SeemURLTest: XCTestCase {
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 options = SteemURL.ResolveOptions(refBlockNum: 0, refBlockPrefix: 1, expiration: Date(timeIntervalSinceReferenceDate: 0), signer: "baz")
let result = try url?.resolve(with: options)
XCTAssertEqual(result?.signer, "foo")
XCTAssertEqual(result?.tx, Transaction(refBlockNum: 0, refBlockPrefix: 1, expiration: Date(timeIntervalSinceReferenceDate: 0), operations: operations, extensions: []))
XCTAssertEqual(result, Transaction(refBlockNum: 0, refBlockPrefix: 1, expiration: Date(timeIntervalSinceReferenceDate: 0), operations: operations, extensions: []))
let ctx = SteemURL.CallbackContext(signature: sig)
let cbUrl = url?.resolveCallback(with: ctx)
XCTAssertEqual(cbUrl?.absoluteString, "https://example.com/sign?sig=207a6fa349f1f624643119f667f394a435c1d31d6f39d8191389305e519a0c051222df037180dc86e00ca4fe43ab638f1e8a96403b3857780abaad4017e03d1ef0")