Skip to content

Commit

Permalink
Allow passing URLSession to StarknetProvider (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
DelevoXDG committed Dec 11, 2023
1 parent 99699cd commit 40e72e2
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 17 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
test_and_lint:
runs-on: macos-12
env:
DEVNET_SHA: a0d87f2f621adb6278c1bdba523ddb58268bf483
DEVNET_SHA: 78527decb3f76c4c808fa35f46228557af3df385
steps:
- uses: actions/checkout@v3

Expand Down Expand Up @@ -72,11 +72,14 @@ jobs:
with:
scarb-version: "0.7.0"

- name: Setup asdf
uses: asdf-vm/actions/setup@v2

- name: Install sncast 0.6.0
run: |
curl --proto '=https' --tlsv1.2 -LsSf https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | sh -s -- -v 0.6.0
source /Users/runner/.bashrc
echo "$(dirname $(which sncast))" >> $GITHUB_PATH
asdf plugin add starknet-foundry
asdf install starknet-foundry 0.6.0
asdf global starknet-foundry 0.6.0
- name: Run tests
run: |
Expand Down
6 changes: 3 additions & 3 deletions Sources/CryptoToolkit/Utils.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

internal func runWithBuffer(resultSize: Int, expectedReturnCode: Int, body: (_ buffer: UnsafeMutablePointer<CChar>) -> Int32) -> Data? {
func runWithBuffer(resultSize: Int, expectedReturnCode: Int, body: (_ buffer: UnsafeMutablePointer<CChar>) -> Int32) -> Data? {
let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: outBufferSize)
defer {
buffer.deallocate()
Expand All @@ -20,9 +20,9 @@ internal func runWithBuffer(resultSize: Int, expectedReturnCode: Int, body: (_ b
}

private let outBufferSize = 1024
internal let standardResultSize = 32
let standardResultSize = 32

internal extension Data {
extension Data {
func paddingLeft(toLength length: Int) -> Data {
let paddingLength = length - self.count

Expand Down
2 changes: 1 addition & 1 deletion Sources/Starknet/Crypto/StarknetCurve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class StarknetCurve {
/// Sign hash with StarknetPrivate key and given k value.
///
/// This method is internal, as using bad k parameter is very dangerous and may lead to exposing the private key.
internal class func sign(privateKey: Felt, hash: Felt, k: BigUInt) throws -> StarknetCurveSignature {
class func sign(privateKey: Felt, hash: Felt, k: BigUInt) throws -> StarknetCurveSignature {
let signatureData = try CryptoCpp.sign(privateKey: privateKey.serialize(), hash: hash.serialize(), k: k.serialize())

let r = try signatureData.subdata(in: 0 ..< 32).toFelt()
Expand Down
2 changes: 1 addition & 1 deletion Sources/Starknet/Data/Hex/NumAsHexProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public extension NumAsHexProtocol {
}
}

internal extension String {
extension String {
func components(withMaxLength length: Int) -> [String] {
stride(from: 0, to: self.count, by: length).map {
let start = self.index(self.startIndex, offsetBy: $0)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Starknet/Data/StarknetTypedData.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// TypedData.swift
// StarknetTypedData.swift
//
//
// Created by Bartosz Rybarski on 14/02/2023.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public struct StarknetSequencerDeployAccountTransaction: StarknetSequencerTransa

// Default deserializer doesn't check if the fields with default values match what is deserialized.
// It's an extension that resolves this.
internal extension StarknetSequencerTransaction {
extension StarknetSequencerTransaction {
func verifyTransactionType<T>(container: KeyedDecodingContainer<T>, codingKeysType _: T.Type) throws where T: CodingKey {
let type = try container.decode(StarknetTransactionType.self, forKey: T(stringValue: "type")!)

Expand Down
13 changes: 13 additions & 0 deletions Sources/Starknet/Providers/StarknetProvider/StarknetProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ public class StarknetProvider: StarknetProviderProtocol {
self.init(starknetChainId: starknetChainId, url: url)
}

public init(starknetChainId: StarknetChainId, url: URL, urlSession: URLSession) {
self.starknetChainId = starknetChainId
self.url = url
self.networkProvider = HttpNetworkProvider(session: urlSession)
}

public convenience init?(starknetChainId: StarknetChainId, url: String, urlSession: URLSession) {
guard let url = URL(string: url) else {
return nil
}
self.init(starknetChainId: starknetChainId, url: url, urlSession: urlSession)
}

private func makeRequest<U>(method: JsonRpcMethod, params: some Encodable = EmptyParams(), receive _: U.Type) async throws -> U where U: Decodable {
let rpcPayload = JsonRpcPayload(method: method, params: params)

Expand Down
2 changes: 1 addition & 1 deletion Tests/StarknetTests/Accounts/AccountTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class AccountTests: XCTestCase {
override class func tearDown() {
super.tearDown()

if let devnetClient = Self.devnetClient {
if let devnetClient {
devnetClient.close()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/StarknetTests/Data/ExecutionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class ExecutionTests: XCTestCase {
override class func tearDown() {
super.tearDown()

if let devnetClient = Self.devnetClient {
if let devnetClient {
devnetClient.close()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/StarknetTests/Devnet/DevnetClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class devnetClientTests: XCTestCase {
}

func testDeclareDeploy() async throws {
let contract = try await client.declareDeployContract(contractName: "Balance")
let contract = try await client.declareDeployContract(contractName: "Balance", constructorCalldata: [1000])
try await client.assertTransactionSucceeded(transactionHash: contract.declare.transactionHash)
try await client.assertTransactionSucceeded(transactionHash: contract.deploy.transactionHash)
}
Expand Down
15 changes: 13 additions & 2 deletions Tests/StarknetTests/Providers/ProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ final class ProviderTests: XCTestCase {
StarknetProvider(starknetChainId: .testnet, url: url)!
}

func testRequestWithCustomURLSession() {
let starknetChainId = StarknetChainId.testnet
let url = Self.devnetClient.rpcUrl
let customURLSession = URLSession(configuration: .ephemeral)
let starknetProvider = StarknetProvider(starknetChainId: starknetChainId, url: url, urlSession: customURLSession)

XCTAssertNotNil(starknetProvider)
}

// TODO: (#89): Re-enable once devnet-rs supports RPC 0.5.0
func disabledTestSpecVersion() async throws {
let result = try await provider.specVersion()
Expand Down Expand Up @@ -162,7 +171,8 @@ final class ProviderTests: XCTestCase {
}

// TODO: (#100) separate estimateFee tests based on transaction type
func testEstimateFee() async throws {
// TODO: (#89): Re-enable this test
func disabledTestEstimateFee() async throws {
let acc = try await ProviderTests.devnetClient.createDeployAccount(name: "test_estimate_fee")
let contract = try await ProviderTests.devnetClient.declareDeployContract(contractName: "Balance")

Expand All @@ -185,7 +195,8 @@ final class ProviderTests: XCTestCase {
XCTAssertEqual(fees.count, 2)
}

func testEstimateMessageFee() async throws {
// TODO: (#89): Re-enable this test
func disabledTestEstimateMessageFee() async throws {
let contract = try await ProviderTests.devnetClient.declareDeployContract(contractName: "Balance")

let message = StarknetMessageFromL1(
Expand Down
2 changes: 1 addition & 1 deletion Tests/StarknetTests/Utils/StarknetTypedData.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// TypedData.swift
// StarknetTypedData.swift
//
//
// Created by Bartosz Rybarski on 15/02/2023.
Expand Down

0 comments on commit 40e72e2

Please sign in to comment.