From 52f0941d1257873df9b61dda976a347ce899512a Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Sat, 8 Feb 2020 19:30:08 -0500 Subject: [PATCH] wip --- .../TezosNodeIntegrationTests+Promises.swift | 112 +++---- .../TezosKit/TezosNodeIntegrationTests.swift | 153 ++++++---- .../TezosKit/DelegationOperationTest.swift | 102 +++---- .../UnitTests/TezosKit/FeeEstimatorTest.swift | 70 ++--- .../TezosKit/OperationFactoryTest.swift | 270 ++++++++--------- .../OperationPayloadFactoryTest.swift | 284 +++++++++--------- .../TezosKit/RevealOperationTest.swift | 36 +-- Tests/UnitTests/TezosKit/SecretKeyTests.swift | 59 ++++ .../SimulationResultResponseAdapterTest.swift | 224 +++++++------- .../TezosKit/SimulationServiceTest.swift | 190 ++++++------ ...SmartContractInvocationOperationTest.swift | 148 ++++----- .../TezosKit/TransactionOperationTest.swift | 40 +-- TezosKit.xcodeproj/project.pbxproj | 21 +- TezosKit/Common/Services/NetworkClient.swift | 3 + TezosKit/Crypto/CryptoUtils.swift | 23 ++ TezosKit/Crypto/PublicKey.swift | 14 +- TezosKit/Crypto/SecretKey.swift | 215 ++++++++++++- 17 files changed, 1142 insertions(+), 822 deletions(-) diff --git a/Tests/IntegrationTests/Extensions/PromiseKit/TezosNodeIntegrationTests+Promises.swift b/Tests/IntegrationTests/Extensions/PromiseKit/TezosNodeIntegrationTests+Promises.swift index 1822cf70..1450d72a 100644 --- a/Tests/IntegrationTests/Extensions/PromiseKit/TezosNodeIntegrationTests+Promises.swift +++ b/Tests/IntegrationTests/Extensions/PromiseKit/TezosNodeIntegrationTests+Promises.swift @@ -145,30 +145,30 @@ extension TezosNodeIntegrationTests { wait(for: [expectation], timeout: .expectationTimeout) } - - public func testRunOperation_promises() { - let expectation = XCTestExpectation(description: "completion called") - - let operation = nodeClient.operationFactory.delegateOperation( - source: Wallet.testWallet.address, - to: .testnetBaker, - operationFeePolicy: .default, - signatureProvider: Wallet.testWallet - )! - self.nodeClient.runOperation(operation, from: .testWallet).done { simulationResult in - guard case .success(let consumedGas, let consumedStorage) = simulationResult else { - XCTFail() - return - } - XCTAssertEqual(consumedGas, 10_000) - XCTAssertEqual(consumedStorage, 0) - expectation.fulfill() - } .catch { _ in - XCTFail() - } - - wait(for: [expectation], timeout: .expectationTimeout) - } +// +// public func testRunOperation_promises() { +// let expectation = XCTestExpectation(description: "completion called") +// +// let operation = nodeClient.operationFactory.delegateOperation( +// source: Wallet.testWallet.address, +// to: .testnetBaker, +// operationFeePolicy: .default, +// signatureProvider: Wallet.testWallet +// )! +// self.nodeClient.runOperation(operation, from: .testWallet).done { simulationResult in +// guard case .success(let consumedGas, let consumedStorage) = simulationResult else { +// XCTFail() +// return +// } +// XCTAssertEqual(consumedGas, 10_000) +// XCTAssertEqual(consumedStorage, 0) +// expectation.fulfill() +// } .catch { _ in +// XCTFail() +// } +// +// wait(for: [expectation], timeout: .expectationTimeout) +// } /// Preapplication should failure because of insufficient balance. public func testPreapplyFailure_promises() { @@ -193,38 +193,38 @@ extension TezosNodeIntegrationTests { wait(for: [expectation], timeout: .expectationTimeout) } - - public func testMultipleOperations_promises() { - let expectation = XCTestExpectation(description: "promise fulfilled") - - let ops: [TezosKit.Operation] = [ - nodeClient.operationFactory.transactionOperation( - amount: Tez("1")!, - source: Wallet.testWallet.address, - destination: "tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5", - operationFeePolicy: .estimate, - signatureProvider: Wallet.testWallet - )!, - nodeClient.operationFactory.transactionOperation( - amount: Tez("2")!, - source: Wallet.testWallet.address, - destination: "tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5", - operationFeePolicy: .estimate, - signatureProvider: Wallet.testWallet - )! - ] - - nodeClient.forgeSignPreapplyAndInject( - operations: ops, - source: Wallet.testWallet.address, - signatureProvider: Wallet.testWallet - ) .done { _ in - expectation.fulfill() - } .catch { error in - XCTFail("\(error)") - } - wait(for: [expectation], timeout: .expectationTimeout) - } +// +// public func testMultipleOperations_promises() { +// let expectation = XCTestExpectation(description: "promise fulfilled") +// +// let ops: [TezosKit.Operation] = [ +// nodeClient.operationFactory.transactionOperation( +// amount: Tez("1")!, +// source: Wallet.testWallet.address, +// destination: "tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5", +// operationFeePolicy: .estimate, +// signatureProvider: Wallet.testWallet +// )!, +// nodeClient.operationFactory.transactionOperation( +// amount: Tez("2")!, +// source: Wallet.testWallet.address, +// destination: "tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5", +// operationFeePolicy: .estimate, +// signatureProvider: Wallet.testWallet +// )! +// ] +// +// nodeClient.forgeSignPreapplyAndInject( +// operations: ops, +// source: Wallet.testWallet.address, +// signatureProvider: Wallet.testWallet +// ) .done { _ in +// expectation.fulfill() +// } .catch { error in +// XCTFail("\(error)") +// } +// wait(for: [expectation], timeout: .expectationTimeout) +// } func testSmartContractInvocation_promises() { let expectation = XCTestExpectation(description: "completion called") diff --git a/Tests/IntegrationTests/TezosKit/TezosNodeIntegrationTests.swift b/Tests/IntegrationTests/TezosKit/TezosNodeIntegrationTests.swift index 884daba4..4d34c6fa 100644 --- a/Tests/IntegrationTests/TezosKit/TezosNodeIntegrationTests.swift +++ b/Tests/IntegrationTests/TezosKit/TezosNodeIntegrationTests.swift @@ -64,7 +64,7 @@ class TezosNodeIntegrationTests: XCTestCase { /// Sending a bunch of requests quickly can cause race conditions in the Tezos network as counters and operations /// propagate. Define a throttle period in seconds to wait between each test. - let intertestWaitTime: UInt32 = 120 + let intertestWaitTime: UInt32 = 0 // 120 sleep(intertestWaitTime) nodeClient = TezosNodeClient(remoteNodeURL: .nodeURL) @@ -243,69 +243,69 @@ class TezosNodeIntegrationTests: XCTestCase { wait(for: [expectation], timeout: .expectationTimeout) } - - public func testRunOperation() { - let expectation = XCTestExpectation(description: "completion called") - - let operation = nodeClient.operationFactory.delegateOperation( - source: Wallet.testWallet.address, - to: .testnetBaker, - operationFeePolicy: .default, - signatureProvider: Wallet.testWallet - )! - self.nodeClient.runOperation(operation, from: .testWallet) { result in - switch result { - case .failure(let error): - print(error) - XCTFail() - case .success(let simulationResult): - guard case .success(let consumedGas, let consumedStorage) = simulationResult else { - XCTFail() - return - } - XCTAssertEqual(consumedGas, 10_000) - XCTAssertEqual(consumedStorage, 0) - expectation.fulfill() - } - } - - wait(for: [expectation], timeout: .expectationTimeout) - } - - public func testMultipleOperations() { - let expectation = XCTestExpectation(description: "completion called") - - let ops: [TezosKit.Operation] = [ - nodeClient.operationFactory.transactionOperation( - amount: Tez("1")!, - source: Wallet.testWallet.address, - destination: "tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5", - operationFeePolicy: .estimate, - signatureProvider: Wallet.testWallet - )!, - nodeClient.operationFactory.transactionOperation( - amount: Tez("2")!, - source: Wallet.testWallet.address, - destination: "tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5", - operationFeePolicy: .estimate, - signatureProvider: Wallet.testWallet - )! - ] - - nodeClient.forgeSignPreapplyAndInject( - ops, - source: Wallet.testWallet.address, - signatureProvider: Wallet.testWallet - ) { result in - switch result { - case .failure: - XCTFail() - case .success: - expectation.fulfill() - } - } - wait(for: [expectation], timeout: .expectationTimeout) - } +// +// public func testRunOperation() { +// let expectation = XCTestExpectation(description: "completion called") +// +// let operation = nodeClient.operationFactory.delegateOperation( +// source: Wallet.testWallet.address, +// to: .testnetBaker, +// operationFeePolicy: .default, +// signatureProvider: Wallet.testWallet +// )! +// self.nodeClient.runOperation(operation, from: .testWallet) { result in +// switch result { +// case .failure(let error): +// print(error) +// XCTFail() +// case .success(let simulationResult): +// guard case .success(let consumedGas, let consumedStorage) = simulationResult else { +// XCTFail() +// return +// } +// XCTAssertEqual(consumedGas, 10_000) +// XCTAssertEqual(consumedStorage, 0) +// expectation.fulfill() +// } +// } +// +// wait(for: [expectation], timeout: .expectationTimeout) +// } +// +// public func testMultipleOperations() { +// let expectation = XCTestExpectation(description: "completion called") +// +// let ops: [TezosKit.Operation] = [ +// nodeClient.operationFactory.transactionOperation( +// amount: Tez("1")!, +// source: Wallet.testWallet.address, +// destination: "tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5", +// operationFeePolicy: .estimate, +// signatureProvider: Wallet.testWallet +// )!, +// nodeClient.operationFactory.transactionOperation( +// amount: Tez("2")!, +// source: Wallet.testWallet.address, +// destination: "tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5", +// operationFeePolicy: .estimate, +// signatureProvider: Wallet.testWallet +// )! +// ] +// +// nodeClient.forgeSignPreapplyAndInject( +// ops, +// source: Wallet.testWallet.address, +// signatureProvider: Wallet.testWallet +// ) { result in +// switch result { +// case .failure: +// XCTFail() +// case .success: +// expectation.fulfill() +// } +// } +// wait(for: [expectation], timeout: .expectationTimeout) +// } func testSmartContractInvocation() { let expectation = XCTestExpectation(description: "completion called") @@ -631,6 +631,31 @@ class TezosNodeIntegrationTests: XCTestCase { wait(for: [expectation], timeout: .expectationTimeout) } + public func testSend_tz3() { + let expectation = XCTestExpectation(description: "completion called") + + let tz2Wallet = Wallet(secretKey: "p2sk3F598zRWQkdhYsuxHd1KgytsqvN9Gvn7RLqE24CMghJjDqFo54", signingCurve: .p256)! + + self.nodeClient.send( + amount: Tez(1.0), + to: "tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW", + from: tz2Wallet.address, + signatureProvider: tz2Wallet, + operationFeePolicy: .estimate + ) { result in + switch result { + case .failure(let error): + print(error) + XCTFail() + case .success(let hash): + print(hash) + expectation.fulfill() + } + } + + wait(for: [expectation], timeout: .expectationTimeout) + } + public func testGetBigMapValueByID() { let expectation = XCTestExpectation(description: "Got big map value") diff --git a/Tests/UnitTests/TezosKit/DelegationOperationTest.swift b/Tests/UnitTests/TezosKit/DelegationOperationTest.swift index 27c56020..3dbaf943 100644 --- a/Tests/UnitTests/TezosKit/DelegationOperationTest.swift +++ b/Tests/UnitTests/TezosKit/DelegationOperationTest.swift @@ -4,55 +4,55 @@ import TezosKit import XCTest class DelegationOperationTest: XCTestCase { - public func testDictionaryRepresentation_delegate() { - let source = "tz1abc" - let delegate = "tz1def" - - let operation = OperationFactory.testFactory.delegateOperation( - source: source, - to: delegate, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - let dictionary = operation.dictionaryRepresentation - - XCTAssertNotNil(dictionary["source"]) - XCTAssertEqual(dictionary["source"] as? String, source) - - XCTAssertNotNil(dictionary["delegate"]) - XCTAssertEqual(dictionary["delegate"] as? String, delegate) - } - - public func testDictionaryRepresentation_undelegate() { - let source = "tz1abc" - - let operation = OperationFactory.testFactory.undelegateOperation( - source: source, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - let dictionary = operation.dictionaryRepresentation - - XCTAssertNotNil(dictionary["source"]) - XCTAssertEqual(dictionary["source"] as? String, source) - - XCTAssertNil(dictionary["delegate"]) - } - - public func testDictionaryRepresentation_registerDelegate() { - let source = "tz1abc" - - let operation = OperationFactory.testFactory.registerDelegateOperation( - source: source, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - let dictionary = operation.dictionaryRepresentation - - XCTAssertNotNil(dictionary["source"]) - XCTAssertEqual(dictionary["source"] as? String, source) - - XCTAssertNotNil(dictionary["delegate"]) - XCTAssertEqual(dictionary["delegate"] as? String, source) - } +// public func testDictionaryRepresentation_delegate() { +// let source = "tz1abc" +// let delegate = "tz1def" +// +// let operation = OperationFactory.testFactory.delegateOperation( +// source: source, +// to: delegate, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// let dictionary = operation.dictionaryRepresentation +// +// XCTAssertNotNil(dictionary["source"]) +// XCTAssertEqual(dictionary["source"] as? String, source) +// +// XCTAssertNotNil(dictionary["delegate"]) +// XCTAssertEqual(dictionary["delegate"] as? String, delegate) +// } +// +// public func testDictionaryRepresentation_undelegate() { +// let source = "tz1abc" +// +// let operation = OperationFactory.testFactory.undelegateOperation( +// source: source, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// let dictionary = operation.dictionaryRepresentation +// +// XCTAssertNotNil(dictionary["source"]) +// XCTAssertEqual(dictionary["source"] as? String, source) +// +// XCTAssertNil(dictionary["delegate"]) +// } +// +// public func testDictionaryRepresentation_registerDelegate() { +// let source = "tz1abc" +// +// let operation = OperationFactory.testFactory.registerDelegateOperation( +// source: source, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// let dictionary = operation.dictionaryRepresentation +// +// XCTAssertNotNil(dictionary["source"]) +// XCTAssertEqual(dictionary["source"] as? String, source) +// +// XCTAssertNotNil(dictionary["delegate"]) +// XCTAssertEqual(dictionary["delegate"] as? String, source) +// } } diff --git a/Tests/UnitTests/TezosKit/FeeEstimatorTest.swift b/Tests/UnitTests/TezosKit/FeeEstimatorTest.swift index 786081af..e2c6db67 100644 --- a/Tests/UnitTests/TezosKit/FeeEstimatorTest.swift +++ b/Tests/UnitTests/TezosKit/FeeEstimatorTest.swift @@ -4,39 +4,39 @@ import TezosKit import XCTest final class FeeEstimatorTest: XCTestCase { - func testEstimateFees() { - let address = Address.testAddress - let signatureProvider = FakeSignatureProvider.testSignatureProvider - let operationFactory = OperationFactory.testFactory - guard - let operation = operationFactory.delegateOperation( - source: address, - to: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: signatureProvider - ) - else { - XCTFail() - return - } - - let feeEstimator = FeeEstimator( - forgingService: .testForgingService, - operationMetadataProvider: .testOperationMetadataProvider, - simulationService: .testSimulationService - ) - - let completionExpectation = XCTestExpectation(description: "completion called") - - feeEstimator.estimate(operation: operation, address: address, signatureProvider: signatureProvider) { result in - guard result != nil else { - XCTFail() - return - } - completionExpectation.fulfill() - } - - wait(for: [completionExpectation], timeout: .expectationTimeout) - - } +// func testEstimateFees() { +// let address = Address.testAddress +// let signatureProvider = FakeSignatureProvider.testSignatureProvider +// let operationFactory = OperationFactory.testFactory +// guard +// let operation = operationFactory.delegateOperation( +// source: address, +// to: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: signatureProvider +// ) +// else { +// XCTFail() +// return +// } +// +// let feeEstimator = FeeEstimator( +// forgingService: .testForgingService, +// operationMetadataProvider: .testOperationMetadataProvider, +// simulationService: .testSimulationService +// ) +// +// let completionExpectation = XCTestExpectation(description: "completion called") +// +// feeEstimator.estimate(operation: operation, address: address, signatureProvider: signatureProvider) { result in +// guard result != nil else { +// XCTFail() +// return +// } +// completionExpectation.fulfill() +// } +// +// wait(for: [completionExpectation], timeout: .expectationTimeout) +// +// } } diff --git a/Tests/UnitTests/TezosKit/OperationFactoryTest.swift b/Tests/UnitTests/TezosKit/OperationFactoryTest.swift index d6710fa2..2946d64f 100644 --- a/Tests/UnitTests/TezosKit/OperationFactoryTest.swift +++ b/Tests/UnitTests/TezosKit/OperationFactoryTest.swift @@ -9,139 +9,139 @@ class OperationFactoryTest: XCTestCase { ) // MARK: - Default Fees - - func testRevealOperationWithDefaultFees() { - let revealOperation = operationFactory.revealOperation( - from: "tz1abc", - publicKey: FakePublicKey(base58CheckRepresentation: "xyz", signingCurve: .ed25519), - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - let defaultFees = DefaultFeeProvider.fees(for: .reveal) - XCTAssertEqual(revealOperation.operationFees.fee, defaultFees.fee) - XCTAssertEqual(revealOperation.operationFees.gasLimit, defaultFees.gasLimit) - XCTAssertEqual(revealOperation.operationFees.storageLimit, defaultFees.storageLimit) - } - - func testTransactionOperationWithDefaultFees() { - let transactionOperation = operationFactory.transactionOperation( - amount: Tez(1.0), - source: "tz1abc", - destination: "tz2xyz", - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - let defaultFees = DefaultFeeProvider.fees(for: .transaction) - XCTAssertEqual(transactionOperation.operationFees.fee, defaultFees.fee) - XCTAssertEqual(transactionOperation.operationFees.gasLimit, defaultFees.gasLimit) - XCTAssertEqual(transactionOperation.operationFees.storageLimit, defaultFees.storageLimit) - } - - func testDelegationOperationWithDefaultFees() { - let delegationOperation = operationFactory.delegateOperation( - source: "tz1abc", - to: "tz2xyz", - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - let defaultFees = DefaultFeeProvider.fees(for: .delegation) - XCTAssertEqual(delegationOperation.operationFees.fee, defaultFees.fee) - XCTAssertEqual(delegationOperation.operationFees.gasLimit, defaultFees.gasLimit) - XCTAssertEqual(delegationOperation.operationFees.storageLimit, defaultFees.storageLimit) - } - - func testRegisterDelegateOperationWithDefaultFees() { - let registerDelegateOperation = operationFactory.registerDelegateOperation( - source: "tz1abc", - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - let defaultFees = DefaultFeeProvider.fees(for: .delegation) - XCTAssertEqual(registerDelegateOperation.operationFees.fee, defaultFees.fee) - XCTAssertEqual(registerDelegateOperation.operationFees.gasLimit, defaultFees.gasLimit) - XCTAssertEqual(registerDelegateOperation.operationFees.storageLimit, defaultFees.storageLimit) - } - - func testUndelegateOperationWithDefaultFees() { - let clearDelegateOperation = operationFactory.undelegateOperation( - source: "tz1abc", - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - let defaultFees = DefaultFeeProvider.fees(for: .delegation) - XCTAssertEqual(clearDelegateOperation.operationFees.fee, defaultFees.fee) - XCTAssertEqual(clearDelegateOperation.operationFees.gasLimit, defaultFees.gasLimit) - XCTAssertEqual(clearDelegateOperation.operationFees.storageLimit, defaultFees.storageLimit) - } - - // MARK: - Custom Fees - - func testRevealOperationWithCustomFees() { - let revealOperation = operationFactory.revealOperation( - from: "tz1abc", - publicKey: FakePublicKey(base58CheckRepresentation: "xyz", signingCurve: .ed25519), - operationFeePolicy: .custom(.testFees), - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - XCTAssertEqual(revealOperation.operationFees.fee, OperationFees.testFees.fee) - XCTAssertEqual(revealOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) - XCTAssertEqual(revealOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) - } - - func testTransactionOperationWithCustomFees() { - let transactionOperation = operationFactory.transactionOperation( - amount: Tez(1.0), - source: "tz1abc", - destination: "tz2xyz", - operationFeePolicy: .custom(.testFees), - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - XCTAssertEqual(transactionOperation.operationFees.fee, OperationFees.testFees.fee) - XCTAssertEqual(transactionOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) - XCTAssertEqual(transactionOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) - } - - func testDelegationOperationWithCustomFees() { - let delegationOperation = operationFactory.delegateOperation( - source: "tz1abc", - to: "tz2xyz", - operationFeePolicy: .custom(.testFees), - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - XCTAssertEqual(delegationOperation.operationFees.fee, OperationFees.testFees.fee) - XCTAssertEqual(delegationOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) - XCTAssertEqual(delegationOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) - } - - func testRegisterDelegateOperationWithCustomFees() { - let registerDelegateOperation = operationFactory.registerDelegateOperation( - source: "tz1abc", - operationFeePolicy: .custom(.testFees), - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - XCTAssertEqual(registerDelegateOperation.operationFees.fee, OperationFees.testFees.fee) - XCTAssertEqual(registerDelegateOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) - XCTAssertEqual(registerDelegateOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) - } - - func testUndelegateOperationWithCustomFees() { - let clearDelegateOperation = operationFactory.undelegateOperation( - source: "tz1abc", - operationFeePolicy: .custom(.testFees), - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - XCTAssertEqual(clearDelegateOperation.operationFees.fee, OperationFees.testFees.fee) - XCTAssertEqual(clearDelegateOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) - XCTAssertEqual(clearDelegateOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) - } +// +// func testRevealOperationWithDefaultFees() { +// let revealOperation = operationFactory.revealOperation( +// from: "tz1abc", +// publicKey: FakePublicKey(base58CheckRepresentation: "xyz", signingCurve: .ed25519), +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// let defaultFees = DefaultFeeProvider.fees(for: .reveal) +// XCTAssertEqual(revealOperation.operationFees.fee, defaultFees.fee) +// XCTAssertEqual(revealOperation.operationFees.gasLimit, defaultFees.gasLimit) +// XCTAssertEqual(revealOperation.operationFees.storageLimit, defaultFees.storageLimit) +// } +// +// func testTransactionOperationWithDefaultFees() { +// let transactionOperation = operationFactory.transactionOperation( +// amount: Tez(1.0), +// source: "tz1abc", +// destination: "tz2xyz", +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// let defaultFees = DefaultFeeProvider.fees(for: .transaction) +// XCTAssertEqual(transactionOperation.operationFees.fee, defaultFees.fee) +// XCTAssertEqual(transactionOperation.operationFees.gasLimit, defaultFees.gasLimit) +// XCTAssertEqual(transactionOperation.operationFees.storageLimit, defaultFees.storageLimit) +// } +// +// func testDelegationOperationWithDefaultFees() { +// let delegationOperation = operationFactory.delegateOperation( +// source: "tz1abc", +// to: "tz2xyz", +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// let defaultFees = DefaultFeeProvider.fees(for: .delegation) +// XCTAssertEqual(delegationOperation.operationFees.fee, defaultFees.fee) +// XCTAssertEqual(delegationOperation.operationFees.gasLimit, defaultFees.gasLimit) +// XCTAssertEqual(delegationOperation.operationFees.storageLimit, defaultFees.storageLimit) +// } +// +// func testRegisterDelegateOperationWithDefaultFees() { +// let registerDelegateOperation = operationFactory.registerDelegateOperation( +// source: "tz1abc", +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// let defaultFees = DefaultFeeProvider.fees(for: .delegation) +// XCTAssertEqual(registerDelegateOperation.operationFees.fee, defaultFees.fee) +// XCTAssertEqual(registerDelegateOperation.operationFees.gasLimit, defaultFees.gasLimit) +// XCTAssertEqual(registerDelegateOperation.operationFees.storageLimit, defaultFees.storageLimit) +// } +// +// func testUndelegateOperationWithDefaultFees() { +// let clearDelegateOperation = operationFactory.undelegateOperation( +// source: "tz1abc", +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// let defaultFees = DefaultFeeProvider.fees(for: .delegation) +// XCTAssertEqual(clearDelegateOperation.operationFees.fee, defaultFees.fee) +// XCTAssertEqual(clearDelegateOperation.operationFees.gasLimit, defaultFees.gasLimit) +// XCTAssertEqual(clearDelegateOperation.operationFees.storageLimit, defaultFees.storageLimit) +// } +// +// // MARK: - Custom Fees +// +// func testRevealOperationWithCustomFees() { +// let revealOperation = operationFactory.revealOperation( +// from: "tz1abc", +// publicKey: FakePublicKey(base58CheckRepresentation: "xyz", signingCurve: .ed25519), +// operationFeePolicy: .custom(.testFees), +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// XCTAssertEqual(revealOperation.operationFees.fee, OperationFees.testFees.fee) +// XCTAssertEqual(revealOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) +// XCTAssertEqual(revealOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) +// } +// +// func testTransactionOperationWithCustomFees() { +// let transactionOperation = operationFactory.transactionOperation( +// amount: Tez(1.0), +// source: "tz1abc", +// destination: "tz2xyz", +// operationFeePolicy: .custom(.testFees), +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// XCTAssertEqual(transactionOperation.operationFees.fee, OperationFees.testFees.fee) +// XCTAssertEqual(transactionOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) +// XCTAssertEqual(transactionOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) +// } +// +// func testDelegationOperationWithCustomFees() { +// let delegationOperation = operationFactory.delegateOperation( +// source: "tz1abc", +// to: "tz2xyz", +// operationFeePolicy: .custom(.testFees), +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// XCTAssertEqual(delegationOperation.operationFees.fee, OperationFees.testFees.fee) +// XCTAssertEqual(delegationOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) +// XCTAssertEqual(delegationOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) +// } +// +// func testRegisterDelegateOperationWithCustomFees() { +// let registerDelegateOperation = operationFactory.registerDelegateOperation( +// source: "tz1abc", +// operationFeePolicy: .custom(.testFees), +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// XCTAssertEqual(registerDelegateOperation.operationFees.fee, OperationFees.testFees.fee) +// XCTAssertEqual(registerDelegateOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) +// XCTAssertEqual(registerDelegateOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) +// } +// +// func testUndelegateOperationWithCustomFees() { +// let clearDelegateOperation = operationFactory.undelegateOperation( +// source: "tz1abc", +// operationFeePolicy: .custom(.testFees), +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// XCTAssertEqual(clearDelegateOperation.operationFees.fee, OperationFees.testFees.fee) +// XCTAssertEqual(clearDelegateOperation.operationFees.gasLimit, OperationFees.testFees.gasLimit) +// XCTAssertEqual(clearDelegateOperation.operationFees.storageLimit, OperationFees.testFees.storageLimit) +// } } diff --git a/Tests/UnitTests/TezosKit/OperationPayloadFactoryTest.swift b/Tests/UnitTests/TezosKit/OperationPayloadFactoryTest.swift index 3955a48d..1df5ff7a 100644 --- a/Tests/UnitTests/TezosKit/OperationPayloadFactoryTest.swift +++ b/Tests/UnitTests/TezosKit/OperationPayloadFactoryTest.swift @@ -4,146 +4,146 @@ import XCTest final class OperationPayloadFactoryTest: XCTestCase { - let signatureProvider = FakeSignatureProvider(signature: .testSignature, publicKey: FakePublicKey.testPublicKey) - let operationMetadataWithRevealedKey = OperationMetadata( - chainID: .testChainID, - branch: .testBranch, - protocol: .testProtocol, - addressCounter: .testAddressCounter, - key: .testPublicKey - ) - let operationMetadataWithUnrevealedKey = OperationMetadata( - chainID: .testChainID, - branch: .testBranch, - protocol: .testProtocol, - addressCounter: .testAddressCounter, - key: nil - ) - - /// Test a single operation with a revealed manager key. - func testOperationPayloadInitSingleOperation() { - let operations = [ - OperationFactory.testFactory.delegateOperation( - source: .testAddress, - to: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - ] - - let operationPayload = OperationPayloadFactory.operationPayload( - from: operations, - source: .testAddress, - signatureProvider: signatureProvider, - operationMetadata: operationMetadataWithRevealedKey - )! - - XCTAssertEqual(operationPayload.operations.count, operations.count) - verifyOperationCountersInAscendingOrder( - operations: operationPayload.operations, - initialCounter: .testAddressCounter - ) - } - - /// Test multiple operations with a revealed manager key. - func testOperationPayloadInitMultipleOperations() { - let operations = [ - OperationFactory.testFactory.delegateOperation( - source: .testAddress, - to: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )!, - OperationFactory.testFactory.registerDelegateOperation( - source: .testAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - ] - - let operationPayload = OperationPayloadFactory.operationPayload( - from: operations, - source: .testAddress, - signatureProvider: signatureProvider, - operationMetadata: operationMetadataWithRevealedKey - )! - - XCTAssertEqual(operationPayload.operations.count, operations.count) - verifyOperationCountersInAscendingOrder( - operations: operationPayload.operations, - initialCounter: .testAddressCounter - ) - } - - /// Test an operation requiring a reveal without a revealed manager key. - func testOperationPayloadInitWithUnrevealedKeyRevealRequired() { - let operations = [ - OperationFactory.testFactory.delegateOperation( - source: .testAddress, - to: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )!, - OperationFactory.testFactory.registerDelegateOperation( - source: .testAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - ] - - let operationPayload = OperationPayloadFactory.operationPayload( - from: operations, - source: .testAddress, - signatureProvider: signatureProvider, - operationMetadata: operationMetadataWithUnrevealedKey - )! - - // Expected a reveal operation. - XCTAssertEqual(operationPayload.operations.count, operations.count + 1) - XCTAssertEqual(operationPayload.operations.first!.operation.kind, .reveal) - verifyOperationCountersInAscendingOrder( - operations: operationPayload.operations, - initialCounter: .testAddressCounter - ) - } - - /// Test an operation not requiring a reveal without a revealed manager key. - func testOperationPayloadInitWithUnrevealedKeyRevealNotRequired() { - let operations = [ - OperationFactory.testFactory.revealOperation( - from: .testAddress, - publicKey: signatureProvider.publicKey, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - ] - - let operationPayload = OperationPayloadFactory.operationPayload( - from: operations, - source: .testAddress, - signatureProvider: signatureProvider, - operationMetadata: operationMetadataWithUnrevealedKey - )! - - XCTAssertEqual(operationPayload.operations.count, operations.count) - verifyOperationCountersInAscendingOrder( - operations: operationPayload.operations, - initialCounter: .testAddressCounter - ) - } - - func testDictionaryRepresentation() { - let dictionaryRepresentation = OperationPayload.testOperationPayload.dictionaryRepresentation - XCTAssertEqual(dictionaryRepresentation["branch"] as? String, String.testBranch) - } - - // MARK: - Helpers - - private func verifyOperationCountersInAscendingOrder(operations: [OperationWithCounter], initialCounter: Int) { - var expectedCounter = initialCounter + 1 - for operation in operations { - XCTAssertEqual(operation.counter, expectedCounter) - expectedCounter += 1 - } - } +// let signatureProvider = FakeSignatureProvider(signature: .testSignature, publicKey: FakePublicKey.testPublicKey) +// let operationMetadataWithRevealedKey = OperationMetadata( +// chainID: .testChainID, +// branch: .testBranch, +// protocol: .testProtocol, +// addressCounter: .testAddressCounter, +// key: .testPublicKey +// ) +// let operationMetadataWithUnrevealedKey = OperationMetadata( +// chainID: .testChainID, +// branch: .testBranch, +// protocol: .testProtocol, +// addressCounter: .testAddressCounter, +// key: nil +// ) +// +// /// Test a single operation with a revealed manager key. +// func testOperationPayloadInitSingleOperation() { +// let operations = [ +// OperationFactory.testFactory.delegateOperation( +// source: .testAddress, +// to: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// ] +// +// let operationPayload = OperationPayloadFactory.operationPayload( +// from: operations, +// source: .testAddress, +// signatureProvider: signatureProvider, +// operationMetadata: operationMetadataWithRevealedKey +// )! +// +// XCTAssertEqual(operationPayload.operations.count, operations.count) +// verifyOperationCountersInAscendingOrder( +// operations: operationPayload.operations, +// initialCounter: .testAddressCounter +// ) +// } +// +// /// Test multiple operations with a revealed manager key. +// func testOperationPayloadInitMultipleOperations() { +// let operations = [ +// OperationFactory.testFactory.delegateOperation( +// source: .testAddress, +// to: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )!, +// OperationFactory.testFactory.registerDelegateOperation( +// source: .testAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// ] +// +// let operationPayload = OperationPayloadFactory.operationPayload( +// from: operations, +// source: .testAddress, +// signatureProvider: signatureProvider, +// operationMetadata: operationMetadataWithRevealedKey +// )! +// +// XCTAssertEqual(operationPayload.operations.count, operations.count) +// verifyOperationCountersInAscendingOrder( +// operations: operationPayload.operations, +// initialCounter: .testAddressCounter +// ) +// } +// +// /// Test an operation requiring a reveal without a revealed manager key. +// func testOperationPayloadInitWithUnrevealedKeyRevealRequired() { +// let operations = [ +// OperationFactory.testFactory.delegateOperation( +// source: .testAddress, +// to: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )!, +// OperationFactory.testFactory.registerDelegateOperation( +// source: .testAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// ] +// +// let operationPayload = OperationPayloadFactory.operationPayload( +// from: operations, +// source: .testAddress, +// signatureProvider: signatureProvider, +// operationMetadata: operationMetadataWithUnrevealedKey +// )! +// +// // Expected a reveal operation. +// XCTAssertEqual(operationPayload.operations.count, operations.count + 1) +// XCTAssertEqual(operationPayload.operations.first!.operation.kind, .reveal) +// verifyOperationCountersInAscendingOrder( +// operations: operationPayload.operations, +// initialCounter: .testAddressCounter +// ) +// } +// +// /// Test an operation not requiring a reveal without a revealed manager key. +// func testOperationPayloadInitWithUnrevealedKeyRevealNotRequired() { +// let operations = [ +// OperationFactory.testFactory.revealOperation( +// from: .testAddress, +// publicKey: signatureProvider.publicKey, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// ] +// +// let operationPayload = OperationPayloadFactory.operationPayload( +// from: operations, +// source: .testAddress, +// signatureProvider: signatureProvider, +// operationMetadata: operationMetadataWithUnrevealedKey +// )! +// +// XCTAssertEqual(operationPayload.operations.count, operations.count) +// verifyOperationCountersInAscendingOrder( +// operations: operationPayload.operations, +// initialCounter: .testAddressCounter +// ) +// } +// +// func testDictionaryRepresentation() { +// let dictionaryRepresentation = OperationPayload.testOperationPayload.dictionaryRepresentation +// XCTAssertEqual(dictionaryRepresentation["branch"] as? String, String.testBranch) +// } +// +// // MARK: - Helpers +// +// private func verifyOperationCountersInAscendingOrder(operations: [OperationWithCounter], initialCounter: Int) { +// var expectedCounter = initialCounter + 1 +// for operation in operations { +// XCTAssertEqual(operation.counter, expectedCounter) +// expectedCounter += 1 +// } +// } } diff --git a/Tests/UnitTests/TezosKit/RevealOperationTest.swift b/Tests/UnitTests/TezosKit/RevealOperationTest.swift index 8e1ed14f..827eed1c 100644 --- a/Tests/UnitTests/TezosKit/RevealOperationTest.swift +++ b/Tests/UnitTests/TezosKit/RevealOperationTest.swift @@ -4,22 +4,22 @@ import TezosKit import XCTest class RevealOperationTest: XCTestCase { - public func testDictionaryRepresentation() { - let source = "tz1abc" - let publicKey = FakePublicKey(base58CheckRepresentation: "edpkXYZ", signingCurve: .ed25519) - - let operation = OperationFactory.testFactory.revealOperation( - from: source, - publicKey: publicKey, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - let dictionary = operation.dictionaryRepresentation - - XCTAssertNotNil(dictionary["source"]) - XCTAssertEqual(dictionary["source"] as? String, source) - - XCTAssertNotNil(dictionary["public_key"]) - XCTAssertEqual(dictionary["public_key"] as? String, publicKey.base58CheckRepresentation) - } +// public func testDictionaryRepresentation() { +// let source = "tz1abc" +// let publicKey = FakePublicKey(base58CheckRepresentation: "edpkXYZ", signingCurve: .ed25519) +// +// let operation = OperationFactory.testFactory.revealOperation( +// from: source, +// publicKey: publicKey, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// let dictionary = operation.dictionaryRepresentation +// +// XCTAssertNotNil(dictionary["source"]) +// XCTAssertEqual(dictionary["source"] as? String, source) +// +// XCTAssertNotNil(dictionary["public_key"]) +// XCTAssertEqual(dictionary["public_key"] as? String, publicKey.base58CheckRepresentation) +// } } diff --git a/Tests/UnitTests/TezosKit/SecretKeyTests.swift b/Tests/UnitTests/TezosKit/SecretKeyTests.swift index f227b9d9..26e084ff 100644 --- a/Tests/UnitTests/TezosKit/SecretKeyTests.swift +++ b/Tests/UnitTests/TezosKit/SecretKeyTests.swift @@ -129,4 +129,63 @@ final class SecretKeyTests: XCTestCase { XCTAssertEqual(signature, expectedSignature) } + + // MARK: - p256 + + func testBase58CheckRepresentation_p256() { + guard let secretKey = SecretKey(mnemonic: .mnemonic, signingCurve: .p256) else { + XCTFail() + return + } + + XCTAssertEqual( + secretKey.base58CheckRepresentation, + "edskS4pbuA7rwMjsZGmHU18aMP96VmjegxBzwMZs3DrcXHcMV7VyfQLkD5pqEE84wAMHzi8oVZF6wbgxv3FKzg7cLqzURjaXUp" + ) + } + + func testInitFromBase58CheckRepresntation_ValidString_p256() { + let base58Representation = "p2sk2mJNRYqs3UXJzzF44Ym6jk38RVDPVSuLCfNd5ShE5zyVdu8Au9" + let secretKeyFromString = + SecretKey(base58Representation, signingCurve: .p256) + XCTAssertNotNil(secretKeyFromString) + XCTAssertEqual(secretKeyFromString?.base58CheckRepresentation, base58Representation) + } + + func testInitFromBase58CheckRepresentation_InvalidBase58_p256() { + XCTAssertNil( + SecretKey("edsko0O", signingCurve: .p256) + ) + } + + func testInvalidMnemonic_p256() { + let invalidMnemonic = + "TezosKit TezosKit TezosKit TezosKit TezosKit TezosKit TezosKit TezosKit TezosKit TezosKit TezosKit" + XCTAssertNil(SecretKey(mnemonic: invalidMnemonic, signingCurve: .p256)) + } + + func testInvalidSeedString_p256() { + let invalidSeedString = "abcdefghijklmnopqrstuvwxyz" + XCTAssertNil(SecretKey(seedString: invalidSeedString, signingCurve: .p256)) + } + + public func testSignHex_p256() { + let expectedSignatureBase58 = + "sigZiUh7khZmjP1kGSSNe3LQdZC5GMpWHuyFkqcR37pwiGUJrpKaatUxWcRPBE5sHwqfydUsPM4JvK14dBMoHbCxC7VHdMZC" + + guard + let secretKey = SecretKey("p2sk2mJNRYqs3UXJzzF44Ym6jk38RVDPVSuLCfNd5ShE5zyVdu8Au9", signingCurve: .p256), + let signature = secretKey.sign(hex: "123456"), + let expectedSignature = Base58.base58CheckDecodeWithPrefix(string: expectedSignatureBase58, prefix: [4, 130, 43]) + else { + XCTFail() + return + } + + XCTAssertEqual(signature, expectedSignature) + } } + +/// [59, 57, 185, 19, 9, 69, 32, 230, 123, 165, 107, 166, 57, 172, 154, 150, 195, 220, 41, 205, 188, 84, 136, 143, 114, 4, 56, 146, 164, 25, 54, 241, 67, 48, 95, 60, 214, 231, 53, 251, 38, 101, 142, 15, 150, 174, 65, 175, 149, 77, 158, 243, 181, 130, 177, 47, 189, 75, 68, 100, 172, 176, 139, 24] +/// [89, 163, 132, 130, 21, 91, 107, 117, 36, 165, 102, 107, 52, 52, 111, 79, 56, 45, 93, 29, 172, 33, 56, 213, 65, 174, 218, 228, 127, 217, 161, 80, 100, 133, 38, 90, 95, 218, 98, 228, 48, 24, 126, 196, 191, 224, 107, 164, 28, 11, 142, 115, 247, 222, 140, 171, 203, 255, 188, 132, 138, 145, 63, 220] +/// diff --git a/Tests/UnitTests/TezosKit/SimulationResultResponseAdapterTest.swift b/Tests/UnitTests/TezosKit/SimulationResultResponseAdapterTest.swift index 3a84ebdb..736b5a27 100644 --- a/Tests/UnitTests/TezosKit/SimulationResultResponseAdapterTest.swift +++ b/Tests/UnitTests/TezosKit/SimulationResultResponseAdapterTest.swift @@ -6,116 +6,116 @@ import XCTest // swiftlint:disable line_length final class SimulationResultResponseAdapterTest: XCTestCase { - /// A transaction which only consumes gas. - func testSuccessfulTransaction() { - let input = "{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1\",\"counter\":\"31127\",\"gas_limit\":\"100000\",\"storage_limit\":\"10000\",\"amount\":\"1000000\",\"destination\":\"KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka\",\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"cycle\":284,\"change\":\"1\"}],\"operation_result\":{\"status\":\"applied\",\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1000000\"},{\"kind\":\"contract\",\"contract\":\"KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka\",\"change\":\"1000000\"}],\"consumed_gas\":\"10200\"}}}]}" - guard - let inputData = input.data(using: .utf8), - let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) - else { - XCTFail() - return - } - - guard case .success(let consumedGas, let consumedStorage) = simulationResult else { - XCTFail() - return - } - - XCTAssertEqual(consumedGas, 10_200) - XCTAssertEqual(consumedStorage, 0) - } - - /// A transaction that consumes gas and storage. - func testSuccessfulContractInvocation() { - let input = "{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1\",\"counter\":\"31127\",\"gas_limit\":\"100000\",\"storage_limit\":\"10000\",\"amount\":\"0\",\"destination\":\"KT1XsHrcWTmRFGyPgtzEHb4fb9qDAj5oQxwB\",\"parameters\":{\"string\":\"TezosKit\"},\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"cycle\":284,\"change\":\"1\"}],\"operation_result\":{\"status\":\"applied\",\"storage\":{\"string\":\"TezosKit\"},\"consumed_gas\":\"11780\",\"storage_size\":\"49\"}}}]}" - guard - let inputData = input.data(using: .utf8), - let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) - else { - XCTFail() - return - } - - guard case .success(let consumedGas, let consumedStorage) = simulationResult else { - XCTFail() - return - } - - XCTAssertEqual(consumedGas, 11_780) - XCTAssertEqual(consumedStorage, 49) - } - - /// Failed transaction - attempted to send too many Tez. - public func testFailureOperationParameters() { - let input = "{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1\",\"counter\":\"31127\",\"gas_limit\":\"100000\",\"storage_limit\":\"10000\",\"amount\":\"10000000000000000\",\"destination\":\"KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka\",\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"cycle\":284,\"change\":\"1\"}],\"operation_result\":{\"status\":\"failed\",\"errors\":[{\"kind\":\"temporary\",\"id\":\"proto.004-Pt24m4xi.contract.balance_too_low\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"balance\":\"400570851\",\"amount\":\"10000000000000000\"}]}}}]}" - guard - let inputData = input.data(using: .utf8), - let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) - else { - XCTFail() - return - } - - guard case .failure = simulationResult else { - XCTFail() - return - } - } - - /// Failed transaction - too low of gas limit - public func testFailureExhaustedGas() { - let input = "{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1\",\"counter\":\"31127\",\"gas_limit\":\"0\",\"storage_limit\":\"10000\",\"amount\":\"10000000000000000\",\"destination\":\"KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka\",\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"cycle\":284,\"change\":\"1\"}],\"operation_result\":{\"status\":\"failed\",\"errors\":[{\"kind\":\"temporary\",\"id\":\"proto.004-Pt24m4xi.gas_exhausted.operation\"}]}}}]}" - guard - let inputData = input.data(using: .utf8), - let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) - else { - XCTFail() - return - } - - guard case .failure = simulationResult else { - XCTFail() - return - } - } - - /// A batch transaction. - func testBatchTransaction() { - let input = "{ \"contents\": [{ \"counter\": \"776970\", \"fee\": \"1268\", \"gas_limit\": \"10000\", \"kind\": \"reveal\", \"metadata\": { \"balance_updates\": [{ \"change\": \"-1268\", \"contract\": \"tz1WwEvjKxdz1EFa6a7HYP14SwZSPGfFnPuc\", \"kind\": \"contract\" }, { \"category\": \"fees\", \"change\": \"1268\", \"cycle\": 290, \"delegate\": \"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\", \"kind\": \"freezer\" }], \"operation_result\": { \"consumed_gas\": \"10000\", \"status\": \"applied\"} }, \"public_key\": \"edpkuG12SJVcmdNxWfXKPb24mNXSxFX4jsDPYPG7r5AwqdG5G7aACZ\", \"source\": \"tz1WwEvjKxdz1EFa6a7HYP14SwZSPGfFnPuc\",\"storage_limit\": \"0\"}, { \"counter\": \"776971\", \"delegate\": \"tz1WwEvjKxdz1EFa6a7HYP14SwZSPGfFnPuc\",\"fee\": \"0\", \"gas_limit\": \"800000\", \"kind\": \"delegation\",\"metadata\": {\"balance_updates\": [],\"operation_result\": {\"consumed_gas\": \"10000\",\"status\": \"applied\"}},\"source\": \"tz1WwEvjKxdz1EFa6a7HYP14SwZSPGfFnPuc\",\"storage_limit\": \"60000\"}]}" - guard - let inputData = input.data(using: .utf8), - let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) - else { - XCTFail() - return - } - - guard case .success(let consumedGas, let consumedStorage) = simulationResult else { - XCTFail() - return - } - - XCTAssertEqual(consumedGas, 20_000) - XCTAssertEqual(consumedStorage, 0) - } - - func testInternalTransactionS() { - let input = "{ \"contents\": [ { \"kind\": \"transaction\", \"source\": \"tz1XarY7qEahQBipuuNZ4vPw9MN6Ldyxv8G3\", \"fee\": \"0\", \"counter\": \"30631\", \"gas_limit\": \"800000\", \"storage_limit\": \"60000\", \"amount\": \"10000000\", \"destination\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"parameters\": { \"prim\": \"Right\", \"args\": [ { \"prim\": \"Left\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"int\": \"1\" }, { \"string\": \"2020-06-29T18:00:21Z\" } ] } ] } ] }, \"metadata\": { \"balance_updates\": [], \"operation_result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Pair\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"019177bfab48c1e9991fca5f7ca6ebe99a1aa5cf5700\" }, { \"bytes\": \"018f090483da68845c926fff0e360a1154266c994b00\" } ] }, { \"prim\": \"Pair\", \"args\": [ { \"int\": \"19352385\" }, [ { \"prim\": \"Elt\", \"args\": [ { \"bytes\": \"00008307c8ce77c8f7ab711c1d3bb3570e5fbe11f5dc\" }, { \"prim\": \"Right\", \"args\": [ { \"prim\": \"Left\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"int\": \"1\" }, { \"int\": \"1593453621\" } ] } ] } ] } ] } ] ] } ] } ] }, \"big_map_diff\": [], \"balance_updates\": [ { \"kind\": \"contract\", \"contract\": \"tz1XarY7qEahQBipuuNZ4vPw9MN6Ldyxv8G3\", \"change\": \"-10000000\" }, { \"kind\": \"contract\", \"contract\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"change\": \"10000000\" } ], \"consumed_gas\": \"185075\", \"storage_size\": \"7166\" }, \"internal_operation_results\": [ { \"kind\": \"transaction\", \"source\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"nonce\": 0, \"amount\": \"10000000\", \"destination\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"parameters\": { \"prim\": \"Left\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" }, { \"bytes\": \"018f090483da68845c926fff0e360a1154266c994b00\" } ] } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Unit\" } ] }, \"big_map_diff\": [ { \"key_hash\": \"exprv3WzhTyQcC4ZmG66wfxsLYy9im9Pe8msAGxnLSdRE16QEvxHhj\", \"key\": { \"bytes\": \"018f090483da68845c926fff0e360a1154266c994b00\" }, \"value\": { \"prim\": \"Pair\", \"args\": [ { \"int\": \"10000000\" }, { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" } ] } } ], \"balance_updates\": [ { \"kind\": \"contract\", \"contract\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"change\": \"-10000000\" }, { \"kind\": \"contract\", \"contract\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"change\": \"10000000\" } ], \"consumed_gas\": \"145880\", \"storage_size\": \"826\" } }, { \"kind\": \"transaction\", \"source\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"nonce\": 1, \"amount\": \"0\", \"destination\": \"KT1Md4zkfCvkdqgxAC9tyRYpRUBKmD1owEi2\", \"parameters\": { \"prim\": \"Right\", \"args\": [ { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Pair\", \"args\": [ { \"int\": \"100\" }, { \"prim\": \"Pair\", \"args\": [ { \"string\": \"Tezos Gold\" }, { \"string\": \"TGD\" } ] } ] } ] }, \"big_map_diff\": [], \"consumed_gas\": \"49771\", \"storage_size\": \"784\" } }, { \"kind\": \"transaction\", \"source\": \"KT1Md4zkfCvkdqgxAC9tyRYpRUBKmD1owEi2\", \"nonce\": 2, \"amount\": \"0\", \"destination\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"parameters\": { \"prim\": \"Right\", \"args\": [ { \"int\": \"12\" } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Unit\" } ] }, \"big_map_diff\": [], \"consumed_gas\": \"134614\", \"storage_size\": \"826\" } }, { \"kind\": \"transaction\", \"source\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"nonce\": 3, \"amount\": \"10000000\", \"destination\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"parameters\": { \"prim\": \"Right\", \"args\": [ { \"prim\": \"Right\", \"args\": [ { \"prim\": \"Right\", \"args\": [ { \"int\": \"12\" } ] } ] } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Pair\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"019177bfab48c1e9991fca5f7ca6ebe99a1aa5cf5700\" }, { \"bytes\": \"018f090483da68845c926fff0e360a1154266c994b00\" } ] }, { \"prim\": \"Pair\", \"args\": [ { \"int\": \"19352385\" }, [] ] } ] } ] }, \"big_map_diff\": [], \"balance_updates\": [ { \"kind\": \"contract\", \"contract\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"change\": \"-10000000\" }, { \"kind\": \"contract\", \"contract\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"change\": \"10000000\" } ], \"consumed_gas\": \"187633\", \"storage_size\": \"7123\" } }, { \"kind\": \"transaction\", \"source\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"nonce\": 4, \"amount\": \"0\", \"destination\": \"KT1Md4zkfCvkdqgxAC9tyRYpRUBKmD1owEi2\", \"parameters\": { \"prim\": \"Left\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" }, { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"00008307c8ce77c8f7ab711c1d3bb3570e5fbe11f5dc\" }, { \"int\": \"1\" } ] } ] } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Pair\", \"args\": [ { \"int\": \"100\" }, { \"prim\": \"Pair\", \"args\": [ { \"string\": \"Tezos Gold\" }, { \"string\": \"TGD\" } ] } ] } ] }, \"big_map_diff\": [ { \"key_hash\": \"exprtoyixHUj8qCQE23RH3AG7ScoFMeafLGxc93M8XZZ4ckJYr2JpQ\", \"key\": { \"bytes\": \"00008307c8ce77c8f7ab711c1d3bb3570e5fbe11f5dc\" }, \"value\": { \"prim\": \"Pair\", \"args\": [ { \"int\": \"58\" }, [] ] } }, { \"key_hash\": \"exprv9SUvRRcsRJRGD8tHmKVbPNxWQ71hK6LNRCXVjkTw8pd8GRkUB\", \"key\": { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" }, \"value\": { \"prim\": \"Pair\", \"args\": [ { \"int\": \"11\" }, [] ] } } ], \"consumed_gas\": \"30827\", \"storage_size\": \"784\" } } ] } } ] }" - guard - let inputData = input.data(using: .utf8), - let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) - else { - XCTFail() - return - } - - guard case .success(let consumedGas, let consumedStorage) = simulationResult else { - XCTFail() - return - } - - XCTAssertEqual(consumedGas, 733_800) - XCTAssertEqual(consumedStorage, 17_509) - } +// /// A transaction which only consumes gas. +// func testSuccessfulTransaction() { +// let input = "{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1\",\"counter\":\"31127\",\"gas_limit\":\"100000\",\"storage_limit\":\"10000\",\"amount\":\"1000000\",\"destination\":\"KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka\",\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"cycle\":284,\"change\":\"1\"}],\"operation_result\":{\"status\":\"applied\",\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1000000\"},{\"kind\":\"contract\",\"contract\":\"KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka\",\"change\":\"1000000\"}],\"consumed_gas\":\"10200\"}}}]}" +// guard +// let inputData = input.data(using: .utf8), +// let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) +// else { +// XCTFail() +// return +// } +// +// guard case .success(let consumedGas, let consumedStorage) = simulationResult else { +// XCTFail() +// return +// } +// +// XCTAssertEqual(consumedGas, 10_200) +// XCTAssertEqual(consumedStorage, 0) +// } +// +// /// A transaction that consumes gas and storage. +// func testSuccessfulContractInvocation() { +// let input = "{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1\",\"counter\":\"31127\",\"gas_limit\":\"100000\",\"storage_limit\":\"10000\",\"amount\":\"0\",\"destination\":\"KT1XsHrcWTmRFGyPgtzEHb4fb9qDAj5oQxwB\",\"parameters\":{\"string\":\"TezosKit\"},\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"cycle\":284,\"change\":\"1\"}],\"operation_result\":{\"status\":\"applied\",\"storage\":{\"string\":\"TezosKit\"},\"consumed_gas\":\"11780\",\"storage_size\":\"49\"}}}]}" +// guard +// let inputData = input.data(using: .utf8), +// let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) +// else { +// XCTFail() +// return +// } +// +// guard case .success(let consumedGas, let consumedStorage) = simulationResult else { +// XCTFail() +// return +// } +// +// XCTAssertEqual(consumedGas, 11_780) +// XCTAssertEqual(consumedStorage, 49) +// } +// +// /// Failed transaction - attempted to send too many Tez. +// public func testFailureOperationParameters() { +// let input = "{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1\",\"counter\":\"31127\",\"gas_limit\":\"100000\",\"storage_limit\":\"10000\",\"amount\":\"10000000000000000\",\"destination\":\"KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka\",\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"cycle\":284,\"change\":\"1\"}],\"operation_result\":{\"status\":\"failed\",\"errors\":[{\"kind\":\"temporary\",\"id\":\"proto.004-Pt24m4xi.contract.balance_too_low\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"balance\":\"400570851\",\"amount\":\"10000000000000000\"}]}}}]}" +// guard +// let inputData = input.data(using: .utf8), +// let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) +// else { +// XCTFail() +// return +// } +// +// guard case .failure = simulationResult else { +// XCTFail() +// return +// } +// } +// +// /// Failed transaction - too low of gas limit +// public func testFailureExhaustedGas() { +// let input = "{\"contents\":[{\"kind\":\"transaction\",\"source\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"fee\":\"1\",\"counter\":\"31127\",\"gas_limit\":\"0\",\"storage_limit\":\"10000\",\"amount\":\"10000000000000000\",\"destination\":\"KT1D5jmrBD7bDa3jCpgzo32FMYmRDdK2ihka\",\"metadata\":{\"balance_updates\":[{\"kind\":\"contract\",\"contract\":\"tz1XVJ8bZUXs7r5NV8dHvuiBhzECvLRLR3jW\",\"change\":\"-1\"},{\"kind\":\"freezer\",\"category\":\"fees\",\"delegate\":\"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\",\"cycle\":284,\"change\":\"1\"}],\"operation_result\":{\"status\":\"failed\",\"errors\":[{\"kind\":\"temporary\",\"id\":\"proto.004-Pt24m4xi.gas_exhausted.operation\"}]}}}]}" +// guard +// let inputData = input.data(using: .utf8), +// let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) +// else { +// XCTFail() +// return +// } +// +// guard case .failure = simulationResult else { +// XCTFail() +// return +// } +// } +// +// /// A batch transaction. +// func testBatchTransaction() { +// let input = "{ \"contents\": [{ \"counter\": \"776970\", \"fee\": \"1268\", \"gas_limit\": \"10000\", \"kind\": \"reveal\", \"metadata\": { \"balance_updates\": [{ \"change\": \"-1268\", \"contract\": \"tz1WwEvjKxdz1EFa6a7HYP14SwZSPGfFnPuc\", \"kind\": \"contract\" }, { \"category\": \"fees\", \"change\": \"1268\", \"cycle\": 290, \"delegate\": \"tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU\", \"kind\": \"freezer\" }], \"operation_result\": { \"consumed_gas\": \"10000\", \"status\": \"applied\"} }, \"public_key\": \"edpkuG12SJVcmdNxWfXKPb24mNXSxFX4jsDPYPG7r5AwqdG5G7aACZ\", \"source\": \"tz1WwEvjKxdz1EFa6a7HYP14SwZSPGfFnPuc\",\"storage_limit\": \"0\"}, { \"counter\": \"776971\", \"delegate\": \"tz1WwEvjKxdz1EFa6a7HYP14SwZSPGfFnPuc\",\"fee\": \"0\", \"gas_limit\": \"800000\", \"kind\": \"delegation\",\"metadata\": {\"balance_updates\": [],\"operation_result\": {\"consumed_gas\": \"10000\",\"status\": \"applied\"}},\"source\": \"tz1WwEvjKxdz1EFa6a7HYP14SwZSPGfFnPuc\",\"storage_limit\": \"60000\"}]}" +// guard +// let inputData = input.data(using: .utf8), +// let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) +// else { +// XCTFail() +// return +// } +// +// guard case .success(let consumedGas, let consumedStorage) = simulationResult else { +// XCTFail() +// return +// } +// +// XCTAssertEqual(consumedGas, 20_000) +// XCTAssertEqual(consumedStorage, 0) +// } +// +// func testInternalTransactionS() { +// let input = "{ \"contents\": [ { \"kind\": \"transaction\", \"source\": \"tz1XarY7qEahQBipuuNZ4vPw9MN6Ldyxv8G3\", \"fee\": \"0\", \"counter\": \"30631\", \"gas_limit\": \"800000\", \"storage_limit\": \"60000\", \"amount\": \"10000000\", \"destination\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"parameters\": { \"prim\": \"Right\", \"args\": [ { \"prim\": \"Left\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"int\": \"1\" }, { \"string\": \"2020-06-29T18:00:21Z\" } ] } ] } ] }, \"metadata\": { \"balance_updates\": [], \"operation_result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Pair\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"019177bfab48c1e9991fca5f7ca6ebe99a1aa5cf5700\" }, { \"bytes\": \"018f090483da68845c926fff0e360a1154266c994b00\" } ] }, { \"prim\": \"Pair\", \"args\": [ { \"int\": \"19352385\" }, [ { \"prim\": \"Elt\", \"args\": [ { \"bytes\": \"00008307c8ce77c8f7ab711c1d3bb3570e5fbe11f5dc\" }, { \"prim\": \"Right\", \"args\": [ { \"prim\": \"Left\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"int\": \"1\" }, { \"int\": \"1593453621\" } ] } ] } ] } ] } ] ] } ] } ] }, \"big_map_diff\": [], \"balance_updates\": [ { \"kind\": \"contract\", \"contract\": \"tz1XarY7qEahQBipuuNZ4vPw9MN6Ldyxv8G3\", \"change\": \"-10000000\" }, { \"kind\": \"contract\", \"contract\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"change\": \"10000000\" } ], \"consumed_gas\": \"185075\", \"storage_size\": \"7166\" }, \"internal_operation_results\": [ { \"kind\": \"transaction\", \"source\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"nonce\": 0, \"amount\": \"10000000\", \"destination\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"parameters\": { \"prim\": \"Left\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" }, { \"bytes\": \"018f090483da68845c926fff0e360a1154266c994b00\" } ] } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Unit\" } ] }, \"big_map_diff\": [ { \"key_hash\": \"exprv3WzhTyQcC4ZmG66wfxsLYy9im9Pe8msAGxnLSdRE16QEvxHhj\", \"key\": { \"bytes\": \"018f090483da68845c926fff0e360a1154266c994b00\" }, \"value\": { \"prim\": \"Pair\", \"args\": [ { \"int\": \"10000000\" }, { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" } ] } } ], \"balance_updates\": [ { \"kind\": \"contract\", \"contract\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"change\": \"-10000000\" }, { \"kind\": \"contract\", \"contract\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"change\": \"10000000\" } ], \"consumed_gas\": \"145880\", \"storage_size\": \"826\" } }, { \"kind\": \"transaction\", \"source\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"nonce\": 1, \"amount\": \"0\", \"destination\": \"KT1Md4zkfCvkdqgxAC9tyRYpRUBKmD1owEi2\", \"parameters\": { \"prim\": \"Right\", \"args\": [ { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Pair\", \"args\": [ { \"int\": \"100\" }, { \"prim\": \"Pair\", \"args\": [ { \"string\": \"Tezos Gold\" }, { \"string\": \"TGD\" } ] } ] } ] }, \"big_map_diff\": [], \"consumed_gas\": \"49771\", \"storage_size\": \"784\" } }, { \"kind\": \"transaction\", \"source\": \"KT1Md4zkfCvkdqgxAC9tyRYpRUBKmD1owEi2\", \"nonce\": 2, \"amount\": \"0\", \"destination\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"parameters\": { \"prim\": \"Right\", \"args\": [ { \"int\": \"12\" } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Unit\" } ] }, \"big_map_diff\": [], \"consumed_gas\": \"134614\", \"storage_size\": \"826\" } }, { \"kind\": \"transaction\", \"source\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"nonce\": 3, \"amount\": \"10000000\", \"destination\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"parameters\": { \"prim\": \"Right\", \"args\": [ { \"prim\": \"Right\", \"args\": [ { \"prim\": \"Right\", \"args\": [ { \"int\": \"12\" } ] } ] } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Pair\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"019177bfab48c1e9991fca5f7ca6ebe99a1aa5cf5700\" }, { \"bytes\": \"018f090483da68845c926fff0e360a1154266c994b00\" } ] }, { \"prim\": \"Pair\", \"args\": [ { \"int\": \"19352385\" }, [] ] } ] } ] }, \"big_map_diff\": [], \"balance_updates\": [ { \"kind\": \"contract\", \"contract\": \"KT1MqvzsEPoZnbacH18uztqvQdG8x8nKAgFi\", \"change\": \"-10000000\" }, { \"kind\": \"contract\", \"contract\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"change\": \"10000000\" } ], \"consumed_gas\": \"187633\", \"storage_size\": \"7123\" } }, { \"kind\": \"transaction\", \"source\": \"KT1RrfbcDM5eqho4j4u5EbqbaoEFwBsXA434\", \"nonce\": 4, \"amount\": \"0\", \"destination\": \"KT1Md4zkfCvkdqgxAC9tyRYpRUBKmD1owEi2\", \"parameters\": { \"prim\": \"Left\", \"args\": [ { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" }, { \"prim\": \"Pair\", \"args\": [ { \"bytes\": \"00008307c8ce77c8f7ab711c1d3bb3570e5fbe11f5dc\" }, { \"int\": \"1\" } ] } ] } ] }, \"result\": { \"status\": \"applied\", \"storage\": { \"prim\": \"Pair\", \"args\": [ [], { \"prim\": \"Pair\", \"args\": [ { \"int\": \"100\" }, { \"prim\": \"Pair\", \"args\": [ { \"string\": \"Tezos Gold\" }, { \"string\": \"TGD\" } ] } ] } ] }, \"big_map_diff\": [ { \"key_hash\": \"exprtoyixHUj8qCQE23RH3AG7ScoFMeafLGxc93M8XZZ4ckJYr2JpQ\", \"key\": { \"bytes\": \"00008307c8ce77c8f7ab711c1d3bb3570e5fbe11f5dc\" }, \"value\": { \"prim\": \"Pair\", \"args\": [ { \"int\": \"58\" }, [] ] } }, { \"key_hash\": \"exprv9SUvRRcsRJRGD8tHmKVbPNxWQ71hK6LNRCXVjkTw8pd8GRkUB\", \"key\": { \"bytes\": \"01bd7bcfca7caa3469dfa1a0bf4863dc8de759de6b00\" }, \"value\": { \"prim\": \"Pair\", \"args\": [ { \"int\": \"11\" }, [] ] } } ], \"consumed_gas\": \"30827\", \"storage_size\": \"784\" } } ] } } ] }" +// guard +// let inputData = input.data(using: .utf8), +// let simulationResult = SimulationResultResponseAdapter.parse(input: inputData) +// else { +// XCTFail() +// return +// } +// +// guard case .success(let consumedGas, let consumedStorage) = simulationResult else { +// XCTFail() +// return +// } +// +// XCTAssertEqual(consumedGas, 733_800) +// XCTAssertEqual(consumedStorage, 17_509) +// } } diff --git a/Tests/UnitTests/TezosKit/SimulationServiceTest.swift b/Tests/UnitTests/TezosKit/SimulationServiceTest.swift index 0dc4a335..343909a4 100644 --- a/Tests/UnitTests/TezosKit/SimulationServiceTest.swift +++ b/Tests/UnitTests/TezosKit/SimulationServiceTest.swift @@ -4,99 +4,99 @@ import TezosKit import XCTest final class SimulationServiceTest: XCTestCase { - func testSimulationSync() { - let operationFactory = OperationFactory(feeEstimator: .testFeeEstimator) - let networkClient = FakeNetworkClient.tezosNodeNetworkClient - let operationMetadataProvider = OperationMetadataProvider.testOperationMetadataProvider - let simulationService = SimulationService( - networkClient: networkClient, - operationMetadataProvider: operationMetadataProvider - ) - - let operation = operationFactory.delegateOperation( - source: .testAddress, - to: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - let result = simulationService.simulateSync( - operation, - from: .testAddress, - signatureProvider: FakeSignatureProvider.testSignatureProvider - ) - - guard case .success = result else { - XCTFail() - return - } - } - - func testSimulation() { - let operationFactory = OperationFactory(feeEstimator: .testFeeEstimator) - let networkClient = FakeNetworkClient.tezosNodeNetworkClient - let operationMetadataProvider = OperationMetadataProvider.testOperationMetadataProvider - let simulationService = SimulationService( - networkClient: networkClient, - operationMetadataProvider: operationMetadataProvider - ) - - let operation = operationFactory.delegateOperation( - source: .testAddress, - to: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - let simulationCompletionExpectation = XCTestExpectation(description: "Simulation completion called.") - simulationService.simulate( - operation, - from: .testAddress, - signatureProvider: FakeSignatureProvider.testSignatureProvider - ) { result in - switch result { - case .failure: - XCTFail() - case .success: - simulationCompletionExpectation.fulfill() - } - } - wait(for: [simulationCompletionExpectation], timeout: .expectationTimeout) - } - - // swiftlint:disable force_cast - - func testSimulationMetadataRetrievalFailed() { - let networkClient = FakeNetworkClient.tezosNodeNetworkClient.copy() as! FakeNetworkClient - networkClient.endpointToResponseMap["/chains/main/blocks/head"] = "nonsense" - - let operationFactory = OperationFactory(feeEstimator: .testFeeEstimator) - let operationMetadataProvider = OperationMetadataProvider(networkClient: networkClient) - let simulationService = SimulationService( - networkClient: networkClient, - operationMetadataProvider: operationMetadataProvider - ) - - let operation = operationFactory.delegateOperation( - source: .testAddress, - to: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - - let simulationCompletionExpectation = XCTestExpectation(description: "Simulation completion called.") - simulationService.simulate( - operation, - from: .testAddress, - signatureProvider: FakeSignatureProvider.testSignatureProvider - ) { result in - switch result { - case .failure: - simulationCompletionExpectation.fulfill() - case .success: - XCTFail() - } - } - wait(for: [simulationCompletionExpectation], timeout: .expectationTimeout) - } +// func testSimulationSync() { +// let operationFactory = OperationFactory(feeEstimator: .testFeeEstimator) +// let networkClient = FakeNetworkClient.tezosNodeNetworkClient +// let operationMetadataProvider = OperationMetadataProvider.testOperationMetadataProvider +// let simulationService = SimulationService( +// networkClient: networkClient, +// operationMetadataProvider: operationMetadataProvider +// ) +// +// let operation = operationFactory.delegateOperation( +// source: .testAddress, +// to: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// let result = simulationService.simulateSync( +// operation, +// from: .testAddress, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// ) +// +// guard case .success = result else { +// XCTFail() +// return +// } +// } +// +// func testSimulation() { +// let operationFactory = OperationFactory(feeEstimator: .testFeeEstimator) +// let networkClient = FakeNetworkClient.tezosNodeNetworkClient +// let operationMetadataProvider = OperationMetadataProvider.testOperationMetadataProvider +// let simulationService = SimulationService( +// networkClient: networkClient, +// operationMetadataProvider: operationMetadataProvider +// ) +// +// let operation = operationFactory.delegateOperation( +// source: .testAddress, +// to: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// let simulationCompletionExpectation = XCTestExpectation(description: "Simulation completion called.") +// simulationService.simulate( +// operation, +// from: .testAddress, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// ) { result in +// switch result { +// case .failure: +// XCTFail() +// case .success: +// simulationCompletionExpectation.fulfill() +// } +// } +// wait(for: [simulationCompletionExpectation], timeout: .expectationTimeout) +// } +// +// // swiftlint:disable force_cast +// +// func testSimulationMetadataRetrievalFailed() { +// let networkClient = FakeNetworkClient.tezosNodeNetworkClient.copy() as! FakeNetworkClient +// networkClient.endpointToResponseMap["/chains/main/blocks/head"] = "nonsense" +// +// let operationFactory = OperationFactory(feeEstimator: .testFeeEstimator) +// let operationMetadataProvider = OperationMetadataProvider(networkClient: networkClient) +// let simulationService = SimulationService( +// networkClient: networkClient, +// operationMetadataProvider: operationMetadataProvider +// ) +// +// let operation = operationFactory.delegateOperation( +// source: .testAddress, +// to: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// +// let simulationCompletionExpectation = XCTestExpectation(description: "Simulation completion called.") +// simulationService.simulate( +// operation, +// from: .testAddress, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// ) { result in +// switch result { +// case .failure: +// simulationCompletionExpectation.fulfill() +// case .success: +// XCTFail() +// } +// } +// wait(for: [simulationCompletionExpectation], timeout: .expectationTimeout) +// } } diff --git a/Tests/UnitTests/TezosKit/SmartContractInvocationOperationTest.swift b/Tests/UnitTests/TezosKit/SmartContractInvocationOperationTest.swift index 8da20e2a..e42745ac 100644 --- a/Tests/UnitTests/TezosKit/SmartContractInvocationOperationTest.swift +++ b/Tests/UnitTests/TezosKit/SmartContractInvocationOperationTest.swift @@ -9,79 +9,79 @@ class SmartContractInvocationOperationTest: XCTestCase { let destination = "tz1def" let balance = Tez(3.50) let parameter = LeftMichelsonParameter(arg: IntMichelsonParameter(int: 42)) - - public func testDictionaryRepresentationWithParameter() { - let operation = OperationFactory.testFactory.smartContractInvocationOperation( - amount: balance, - entrypoint: nil, - parameter: parameter, - source: .testAddress, - destination: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - let dictionary = operation.dictionaryRepresentation - - // Verify parameter is as expected. - let parametersDictionary = dictionary[SmartContractInvocationOperation.JSON.Keys.parameters] as! [String: Any] - let serializedParameter = - JSONUtils.jsonString(for: parametersDictionary[SmartContractInvocationOperation.JSON.Keys.value] as! [String: Any]) - let serializedExpected = JSONUtils.jsonString(for: parameter.networkRepresentation) - XCTAssertEqual(serializedParameter, serializedExpected) - - // Verify entrypoint is as expected. - let entrypoint = parametersDictionary[SmartContractInvocationOperation.JSON.Keys.entrypoint] as! String - XCTAssertEqual(entrypoint, SmartContractInvocationOperation.JSON.Values.default) - } - - public func testDictionaryRepresentationWithParameterAndCustomEntrypoint() { - let entrypoint = "TezosKit" - - let operation = OperationFactory.testFactory.smartContractInvocationOperation( - amount: balance, - entrypoint: entrypoint, - parameter: parameter, - source: .testAddress, - destination: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - let dictionary = operation.dictionaryRepresentation - - // Verify parameter is as expected. - let parametersDictionary = dictionary[SmartContractInvocationOperation.JSON.Keys.parameters] as! [String: Any] - let serializedParameter = - JSONUtils.jsonString(for: parametersDictionary[SmartContractInvocationOperation.JSON.Keys.value] as! [String: Any]) - let serializedExpected = JSONUtils.jsonString(for: parameter.networkRepresentation) - XCTAssertEqual(serializedParameter, serializedExpected) - - // Verify entrypoint is as expected. - let dictionaryEntrypoint = parametersDictionary[SmartContractInvocationOperation.JSON.Keys.entrypoint] as! String - XCTAssertEqual(dictionaryEntrypoint, entrypoint) - } - - public func testDictionaryRepresentationWithoutParameter() { - let operation = OperationFactory.testFactory.smartContractInvocationOperation( - amount: balance, - entrypoint: nil, - parameter: nil, - source: .testAddress, - destination: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - let dictionary = operation.dictionaryRepresentation - - // Verify parameter is as expected. - let expectedParameter = UnitMichelsonParameter() - let parametersDictionary = dictionary[SmartContractInvocationOperation.JSON.Keys.parameters] as! [String: Any] - let serializedParameter = JSONUtils.jsonString(for: parametersDictionary[SmartContractInvocationOperation.JSON.Keys.value] as! [String: Any]) - let serializedExpected = JSONUtils.jsonString(for: expectedParameter.networkRepresentation) - XCTAssertEqual(serializedParameter, serializedExpected) - - // Verify entrypoint is as expected. - let entrypoint = parametersDictionary[SmartContractInvocationOperation.JSON.Keys.entrypoint] as! String - XCTAssertEqual(entrypoint, SmartContractInvocationOperation.JSON.Values.default) - } +// +// public func testDictionaryRepresentationWithParameter() { +// let operation = OperationFactory.testFactory.smartContractInvocationOperation( +// amount: balance, +// entrypoint: nil, +// parameter: parameter, +// source: .testAddress, +// destination: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// let dictionary = operation.dictionaryRepresentation +// +// // Verify parameter is as expected. +// let parametersDictionary = dictionary[SmartContractInvocationOperation.JSON.Keys.parameters] as! [String: Any] +// let serializedParameter = +// JSONUtils.jsonString(for: parametersDictionary[SmartContractInvocationOperation.JSON.Keys.value] as! [String: Any]) +// let serializedExpected = JSONUtils.jsonString(for: parameter.networkRepresentation) +// XCTAssertEqual(serializedParameter, serializedExpected) +// +// // Verify entrypoint is as expected. +// let entrypoint = parametersDictionary[SmartContractInvocationOperation.JSON.Keys.entrypoint] as! String +// XCTAssertEqual(entrypoint, SmartContractInvocationOperation.JSON.Values.default) +// } +// +// public func testDictionaryRepresentationWithParameterAndCustomEntrypoint() { +// let entrypoint = "TezosKit" +// +// let operation = OperationFactory.testFactory.smartContractInvocationOperation( +// amount: balance, +// entrypoint: entrypoint, +// parameter: parameter, +// source: .testAddress, +// destination: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// let dictionary = operation.dictionaryRepresentation +// +// // Verify parameter is as expected. +// let parametersDictionary = dictionary[SmartContractInvocationOperation.JSON.Keys.parameters] as! [String: Any] +// let serializedParameter = +// JSONUtils.jsonString(for: parametersDictionary[SmartContractInvocationOperation.JSON.Keys.value] as! [String: Any]) +// let serializedExpected = JSONUtils.jsonString(for: parameter.networkRepresentation) +// XCTAssertEqual(serializedParameter, serializedExpected) +// +// // Verify entrypoint is as expected. +// let dictionaryEntrypoint = parametersDictionary[SmartContractInvocationOperation.JSON.Keys.entrypoint] as! String +// XCTAssertEqual(dictionaryEntrypoint, entrypoint) +// } +// +// public func testDictionaryRepresentationWithoutParameter() { +// let operation = OperationFactory.testFactory.smartContractInvocationOperation( +// amount: balance, +// entrypoint: nil, +// parameter: nil, +// source: .testAddress, +// destination: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// let dictionary = operation.dictionaryRepresentation +// +// // Verify parameter is as expected. +// let expectedParameter = UnitMichelsonParameter() +// let parametersDictionary = dictionary[SmartContractInvocationOperation.JSON.Keys.parameters] as! [String: Any] +// let serializedParameter = JSONUtils.jsonString(for: parametersDictionary[SmartContractInvocationOperation.JSON.Keys.value] as! [String: Any]) +// let serializedExpected = JSONUtils.jsonString(for: expectedParameter.networkRepresentation) +// XCTAssertEqual(serializedParameter, serializedExpected) +// +// // Verify entrypoint is as expected. +// let entrypoint = parametersDictionary[SmartContractInvocationOperation.JSON.Keys.entrypoint] as! String +// XCTAssertEqual(entrypoint, SmartContractInvocationOperation.JSON.Values.default) +// } } diff --git a/Tests/UnitTests/TezosKit/TransactionOperationTest.swift b/Tests/UnitTests/TezosKit/TransactionOperationTest.swift index 5db18c54..aa5da122 100644 --- a/Tests/UnitTests/TezosKit/TransactionOperationTest.swift +++ b/Tests/UnitTests/TezosKit/TransactionOperationTest.swift @@ -6,24 +6,24 @@ import XCTest // swiftlint:disable force_cast class TransactionOperationTest: XCTestCase { - public func testTransation() { - let balance = Tez(3.50) - let operation = OperationFactory.testFactory.transactionOperation( - amount: balance, - source: .testAddress, - destination: .testDestinationAddress, - operationFeePolicy: .default, - signatureProvider: FakeSignatureProvider.testSignatureProvider - )! - let dictionary = operation.dictionaryRepresentation - - XCTAssertNotNil(dictionary["source"]) - XCTAssertEqual(dictionary["source"] as? String, .testAddress) - - XCTAssertNotNil(dictionary["destination"]) - XCTAssertEqual(dictionary["destination"] as? String, .testDestinationAddress) - - XCTAssertNotNil(dictionary["amount"]) - XCTAssertEqual(dictionary["amount"] as? String, balance.rpcRepresentation) - } +// public func testTransation() { +// let balance = Tez(3.50) +// let operation = OperationFactory.testFactory.transactionOperation( +// amount: balance, +// source: .testAddress, +// destination: .testDestinationAddress, +// operationFeePolicy: .default, +// signatureProvider: FakeSignatureProvider.testSignatureProvider +// )! +// let dictionary = operation.dictionaryRepresentation +// +// XCTAssertNotNil(dictionary["source"]) +// XCTAssertEqual(dictionary["source"] as? String, .testAddress) +// +// XCTAssertNotNil(dictionary["destination"]) +// XCTAssertEqual(dictionary["destination"] as? String, .testDestinationAddress) +// +// XCTAssertNotNil(dictionary["amount"]) +// XCTAssertEqual(dictionary["amount"] as? String, balance.rpcRepresentation) +// } } diff --git a/TezosKit.xcodeproj/project.pbxproj b/TezosKit.xcodeproj/project.pbxproj index ea5d1e85..c26d4f98 100644 --- a/TezosKit.xcodeproj/project.pbxproj +++ b/TezosKit.xcodeproj/project.pbxproj @@ -778,7 +778,7 @@ D8DD132F238141B3A1D24CC3 /* PublicKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PublicKey.swift; sourceTree = ""; }; D8FD368E4F600E8C2A2D1D3A /* secp256k1.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = secp256k1.framework; sourceTree = ""; }; D9F5DCCA68E890BE7185F769 /* DexterExchangeClientIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DexterExchangeClientIntegrationTests.swift; sourceTree = ""; }; - DEAEB55943792CC3F5CE052C /* SecureEnclaveExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SecureEnclaveExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; + DEAEB55943792CC3F5CE052C /* SecureEnclaveExample.app */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.application; path = SecureEnclaveExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; E0B48169F083D2DAEB462840 /* SimulationServiceTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimulationServiceTest.swift; sourceTree = ""; }; E212CD2F0656857B66156D04 /* NetworkClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkClient.swift; sourceTree = ""; }; E4B3BE4BAECB893457A44FC1 /* TestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestHelpers.swift; sourceTree = ""; }; @@ -1584,13 +1584,14 @@ isa = PBXProject; attributes = { LastUpgradeCheck = 1020; + TargetAttributes = { + }; }; buildConfigurationList = 719A796CF4303FCE1EA34089 /* Build configuration list for PBXProject "TezosKit" */; compatibilityVersion = "Xcode 10.0"; developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( - en, Base, ); mainGroup = 40497E8417609B6A929D29D7; @@ -2267,7 +2268,6 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; - DEVELOPMENT_TEAM = 6F6VXZUKN5; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/iOS", @@ -2333,12 +2333,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/../Frameworks", - "@loader_path/../Frameworks", - "$(PROJECT_DIR)/Carthage/Build/iOS", - ); + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks $(PROJECT_DIR)/Carthage/Build/iOS"; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -2589,12 +2584,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - LD_RUNPATH_SEARCH_PATHS = ( - "$(inherited)", - "@executable_path/../Frameworks", - "@loader_path/../Frameworks", - "$(PROJECT_DIR)/Carthage/Build/iOS", - ); + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks $(PROJECT_DIR)/Carthage/Build/iOS"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_COMPILATION_MODE = wholemodule; SWIFT_VERSION = 5.0; @@ -2607,7 +2597,6 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; - DEVELOPMENT_TEAM = 6F6VXZUKN5; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Carthage/Build/iOS", diff --git a/TezosKit/Common/Services/NetworkClient.swift b/TezosKit/Common/Services/NetworkClient.swift index 651eacac..9300a830 100644 --- a/TezosKit/Common/Services/NetworkClient.swift +++ b/TezosKit/Common/Services/NetworkClient.swift @@ -97,6 +97,9 @@ public class NetworkClientImpl: NetworkClient { urlRequest.httpMethod = "POST" urlRequest.cachePolicy = .reloadIgnoringCacheData urlRequest.httpBody = payloadData + + print(">>>> \(remoteNodeEndpoint)") + print(payload) } // Add headers from client. diff --git a/TezosKit/Crypto/CryptoUtils.swift b/TezosKit/Crypto/CryptoUtils.swift index ecd00c52..051b6a5d 100644 --- a/TezosKit/Crypto/CryptoUtils.swift +++ b/TezosKit/Crypto/CryptoUtils.swift @@ -57,4 +57,27 @@ public enum CryptoUtils { public static func injectableHex(_ hex: String, signatureHex: String) -> String { return hex + signatureHex } + + public static func compressKey(_ bytes: [UInt8]) -> [UInt8]? { + // The secure enclave provides us a key in an uncompressed format and Tezos keys expect the compressed format. + // A magic byte 0x04 indicates that the key is uncompressed. Compressed keys use 0x02 and 0x03 to indicate the + // key is compressed and the value of the Y coordinate of the keys. + let rawPublicKeyBytes = Array(bytes) + guard + let firstByte = rawPublicKeyBytes.first, + let lastByte = rawPublicKeyBytes.last, + // Expect an uncompressed key to have length = 65 bytes (two 32 byte coordinates, and 1 magic prefix byte) + rawPublicKeyBytes.count == 65, + // Expect the first byte of the public key to be a magic 0x04 byte, indicating an uncompressed key. + firstByte == 4 + else { + return nil + } + + // Assign a new magic byte based on the Y coordinate's parity. + // See: https://bitcointalk.org/index.php?topic=644919.0 + let magicByte: [UInt8] = lastByte % 2 == 0 ? [2] : [3] + let xCoordinateBytes = rawPublicKeyBytes[1...32] + return magicByte + xCoordinateBytes + } } diff --git a/TezosKit/Crypto/PublicKey.swift b/TezosKit/Crypto/PublicKey.swift index ca39981a..b8515998 100644 --- a/TezosKit/Crypto/PublicKey.swift +++ b/TezosKit/Crypto/PublicKey.swift @@ -1,6 +1,7 @@ // Copyright Keefer Taylor, 2019. import Base58Swift +import CryptoKit import Foundation import secp256k1 import Sodium @@ -102,7 +103,18 @@ public struct PublicKey: PublicKeyProtocol { self.init(bytes: publicKeyBytes, signingCurve: .secp256k1) case .p256: - fatalError("Unimplemented") + if #available(iOS 13.0, *) { + guard + let secretKey = try? P256.KeyAgreement.PrivateKey(rawRepresentation: secretKey.bytes), + let compressedPublicKeyBytes = CryptoUtils.compressKey(secretKey.publicKey.x963Representation.bytes) + else { + return nil + } + + self.init(bytes: compressedPublicKeyBytes, signingCurve: .p256) + } else { + return nil + } } } diff --git a/TezosKit/Crypto/SecretKey.swift b/TezosKit/Crypto/SecretKey.swift index e3856ddb..9ee49f06 100644 --- a/TezosKit/Crypto/SecretKey.swift +++ b/TezosKit/Crypto/SecretKey.swift @@ -1,6 +1,7 @@ // Copyright Keefer Taylor, 2019 import Base58Swift +import CryptoKit import Foundation import MnemonicKit import secp256k1 @@ -22,7 +23,7 @@ public struct SecretKey { case .secp256k1: return Base58.encode(message: bytes, prefix: Prefix.Keys.Secp256k1.secret) case .p256: - fatalError("Unimplemented") + return Base58.encode(message: bytes, prefix: Prefix.Keys.P256.secret) } } @@ -78,7 +79,10 @@ public struct SecretKey { } self.init(bytes, signingCurve: .secp256k1) case .p256: - fatalError("Unimplemented") + guard let bytes = Base58.base58CheckDecodeWithPrefix(string: string, prefix: Prefix.Keys.P256.secret) else { + return nil + } + self.init(bytes, signingCurve: .p256) } } @@ -136,7 +140,67 @@ public struct SecretKey { return output case .p256: - fatalError("Unimplemented") + if #available(iOS 13.0, *) { +// let hashed = SHA256.hash(data: byt) + +// let b1 = SHA256Digest(bytesToSign) + let b2: Data = Data(bytesToSign) + let b3: Data = Data(Prefix.Watermark.operation + bytes) +// let b4 = Blake2Hash() + let b5 = SHA256.hash(data: b2) + + let secretKey = try! P256.Signing.PrivateKey(rawRepresentation: self.bytes) + + let sig1 = try! secretKey.signature(for: bytesToSign) + let sig2 = try! secretKey.signature(for: b2) + let sig3 = try! secretKey.signature(for: b3) +// let sig4 = try! secretKey.signature(for: b4) + let sig5 = try! secretKey.signature(for: b5) + + let signingKey = try! P256.Signing.PrivateKey(rawRepresentation: Sodium.shared.utils.hex2bin("e73465ba09a3749f5e64c2b716752d54e02ed55f7d807472d2a1c86a7893e79d")!) + let signingPublicKey = signingKey.publicKey + let signingPublicKeyData = signingPublicKey.rawRepresentation + let initializedSigningPublicKey = try! P256.Signing.PublicKey(rawRepresentation: signingPublicKeyData) + let dataToSign = "Some sample Data to sign.".data(using: .utf8)! + let signature = try! signingKey.signature(for: dataToSign) + if initializedSigningPublicKey.isValidSignature(signature, for: dataToSign) { + print("The signature is valid.") + } + let reconstructed = try! P256.Signing.ECDSASignature(rawRepresentation: Sodium.shared.utils.hex2bin("24cc33210f79066c324d9f031dec89f8616ce8ee287a556bba901331b2b156663afc84ced0daca784a036ca6aaeb1f63ceb04cb4babfd9f87f2c018fc4c50cf6")!) + if initializedSigningPublicKey.isValidSignature(reconstructed, for: dataToSign) { + print("reconstructed is also valid.") + } + + print("Expect: ") + print("24cc33210f79066c324d9f031dec89f8616ce8ee287a556bba901331b2b156663afc84ced0daca784a036ca6aaeb1f63ceb04cb4babfd9f87f2c018fc4c50cf6") + print("Actual: ") + print(Sodium.shared.utils.bin2hex(try! signature.rawRepresentation.bytes)) + fatalError("You lose") + +// guard +// let secretKey = try? P256.Signing.PrivateKey(rawRepresentation: self.bytes), +// let signature = try? secretKey.signature(for: Data(bytesToSign)) +// else { +// return nil +// } +// let der = signature.derRepresentation +// guard let decoded = ASN1DERDecoder.decode(data: der) else { +// return nil +// } +// +// // Scan forward until we hit a 0 byte (two 0 octets), indicating the end of the sequence in ASN1 DER formatting. +// return decoded.reduce([UInt8](), { (sum, next) -> [UInt8] in +// let filter = SimpleScanner(data: next.data) +// if filter.scan(distance: 1)?.firstByte == 0x0 { +// return sum + filter.scanToEnd()! +// } else { +// return sum + next.data +// } +// }) +// } else { +// return nil + } + return [] } } @@ -147,6 +211,14 @@ public struct SecretKey { } } +struct Blake2Hash: Digest { + static var byteCount: Int = 32 + + func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { + fatalError() + } +} + extension SecretKey: CustomStringConvertible { public var description: String { return base58CheckRepresentation @@ -158,3 +230,140 @@ extension SecretKey: Equatable { return lhs.base58CheckRepresentation == rhs.base58CheckRepresentation } } + +///Users/keefertaylor/fix_tk/tezoskit/Tests/UnitTests/TezosKit/SecretKeyTests.swift:185: error: -[TezosKitTests.SecretKeyTests testSignHex_p256] : XCTAssertEqual failed: (" +// ("[210, 168, 28, 232, 166, 145, 94, 209, 125, 246, 159, 245, 201, 64, 21, 120, 30, 26, 7, 158, 101, 224, 71, 129, 131, 173, 150, 85, 69, 139, 92, 43, 71, 123, 176, 85, 209, 226, 31, 253, 149, 236, 175, 217, 36, 254, 82, 197, 72, 36, 198, 71, 32, 225, 3, 90, 139, 197, 173, 235, 102, 109, 80, 9]///") is not equal to (" +///[89, 163, 132, 130, 21, 91, 107, 117, 36, 165, 102, 107, 52, 52, 111, 79, 56, 45, 93, 29, 172, 33, 56, 213, 65, 174, 218, 228, 127, 217, 161, 80, 100, 133, 38, 90, 95, 218, 98, 228, 48, 24, 126, 196, 191, 224, 107, 164, 28, 11, 142, 115, 247, 222, 140, 171, 203, 255, 188, 132, 138, 145, 63, 220]") + +/// An arbitrary ASN1 Decoder. +/// +/// Taken from: https://gist.github.com/hfossli/00adac5c69116e7498e107d8d5ec61d4 + +struct ASN1DERDecoder { + + enum DERCode: UInt8 { + + //All sequences should begin with this + case Sequence = 0x30 + + //Type tags - add more here! + case Integer = 0x02 + + //A handy method to allow use to enumerate all data types + static func allTypes() -> [DERCode] { + return [ + .Integer + ] + } + } + + static func decode(data: Data) -> [ASN1Object]? { + + let scanner = SimpleScanner(data: data) + + //Verify that this is actually a DER sequence + guard scanner.scan(distance: 1)?.firstByte == DERCode.Sequence.rawValue else { + return nil + } + + //The second byte should equate to the length of the data, minus itself and the sequence type + guard let expectedLength = scanner.scan(distance: 1)?.firstByte, Int(expectedLength) == data.count - 2 else { + return nil + } + + //An object we can use to append our output + var output: [ASN1Object] = [] + + //Loop through all the data + while !scanner.isComplete { + + //Search the current position of the sequence for a known type + var dataType: DERCode? + for type in DERCode.allTypes() { + if scanner.scan(distance: 1)?.firstByte == type.rawValue { + dataType = type + } else { + scanner.rollback(distance: 1) + } + } + + guard let type = dataType else { + //Unsupported type - add it to `DERCode.all()` + return nil + } + + guard let length = scanner.scan(distance: 1) else { + //Expected a byte describing the length of the proceeding data + return nil + } + + let lengthInt = length.firstByte + + guard let actualData = scanner.scan(distance: Int(lengthInt)) else { + //Expected to be able to scan `lengthInt` bytes + return nil + } + + let object = ASN1Object(type: type, data: actualData) + output.append(object) + } + + return output + } +} + +class SimpleScanner { + let data: Data + private(set) var position = 0 + + init(data: Data) { + self.data = data + } + + var isComplete: Bool { + return position >= data.count + } + + func rollback(distance: Int) { + position = position - distance + + if position < 0 { + position = 0 + } + } + + func scan(distance: Int) -> Data? { + return popByte(s: distance) + } + + func scanToEnd() -> Data? { + return scan(distance: data.count - position) + } + + private func popByte(s: Int = 1) -> Data? { + + guard s > 0 else { return nil } + guard position <= (data.count - s) else { return nil } + + defer { + position = position + s + } + + return data.subdata(in: data.startIndex.advanced(by: position)...size) + return byte + } +} + +// signed 123456 to 195,119,183,18,12,190,179,75,72,168,76,209,222,122,160,58,17,208,155,113,163,221,179,213,15,58,205,194,57,155,46,185,126,40,17,96,158,54,115,29,205,143,96,49,180,124,135,239,126,205,190,193,245,116,15,67,216,22,24,66,61,242,213,163