Skip to content

Commit

Permalink
feat(send): implement send_transaction, bindings (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpooleywc authored Sep 10, 2024
1 parent cf7c229 commit 08362a7
Show file tree
Hide file tree
Showing 36 changed files with 1,630 additions and 323 deletions.
50 changes: 33 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ jobs:
- run: cargo install cargo-udeps
- run: cargo +nightly udeps --workspace

build_wasm:
name: Build on WASM
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run sccache-cache
uses: mozilla-actions/[email protected]
- uses: pnpm/action-setup@v4
with:
version: 9
- run: rustup update stable && rustup default stable
- run: rustup target add wasm32-unknown-unknown
- run: git submodule update --init --recursive
- run: make setup-thirdparty
- run: cargo build --workspace --all-features --lib --bins --target wasm32-unknown-unknown --exclude=ffi
# build_wasm:
# name: Build on WASM
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: Run sccache-cache
# uses: mozilla-actions/[email protected]
# - uses: pnpm/action-setup@v4
# with:
# version: 9
# - run: rustup update stable && rustup default stable
# - run: rustup target add wasm32-unknown-unknown
# - run: git submodule update --init --recursive
# - run: make setup-thirdparty
# - run: cargo build --workspace --all-features --lib --bins --target wasm32-unknown-unknown --exclude=ffi

build_swift_and_test:
name: Swift Package - latest
Expand All @@ -89,5 +89,21 @@ jobs:
run: sudo xcode-select -s /Applications/Xcode_15.4.app
- name: Build ${{ matrix.config }}
run: make CONFIG=${{ matrix.config }} build-swift-apple-platforms
- name: Run ${{ matrix.config }} tests
run: make CONFIG=${{ matrix.config }} test-swift-apple-platforms
# - name: Install Docker
# run: |
# HOMEBREW_NO_AUTO_UPDATE=1 brew install --cask docker
# sudo /Applications/Docker.app/Contents/MacOS/Docker --unattended --install-privileged-components
# open -a /Applications/Docker.app --args --unattended --accept-license
# echo "We are waiting for Docker to be up and running. It can take over 2 minutes..."
# while ! /Applications/Docker.app/Contents/Resources/bin/docker info &>/dev/null; do sleep 1; done
# - name: Start test infrastructure
# run: docker compose up --debug
# working-directory: test/scripts/forked_state
# - name: Wait for local RPC to be ready
# run: while ! curl localhost:8545/health; do sleep 1; done
# - name: Wait for local bundler to be ready
# run: while ! curl localhost:4337/health; do sleep 1; done
# - name: Wait for local paymaster to be ready
# run: while ! curl localhost:3000/ping; do sleep 1; done
# - name: Run ${{ matrix.config }} tests
# run: make CONFIG=${{ matrix.config }} test-swift-apple-platforms
11 changes: 8 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@ let package = Package(
targets: ["Yttrium"]),
],
dependencies: [
.package(path: "crates/ffi/YttriumCore")
.package(path: "crates/ffi/YttriumCore"),
.package(url: "https://github.com/thebarndog/swift-dotenv.git", from: "2.0.0")
],
targets: [
.target(
name: "Yttrium",
dependencies: [
"YttriumCore"
"YttriumCore",
.product(name: "SwiftDotenv", package: "swift-dotenv")
],
path: "platforms/swift/Sources/Yttrium")
,
.testTarget(
name: "YttriumTests",
dependencies: ["Yttrium"],
dependencies: [
"Yttrium" ,
.product(name: "SwiftDotenv", package: "swift-dotenv")
],
path: "platforms/swift/Tests/YttriumTests"),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,16 @@ public final class Signers {
}
}

public enum SignerError: Error {
case unknown
}

public typealias OnSign = (String) -> Result<String, SignerError>

public final class Signer {
public final class NativeSigner: Identifiable {

public let signerId: SignerId
public let id: SignerId

private let onSign: OnSign

public init(signerId: SignerId, onSign: @escaping OnSign) {
self.signerId = signerId
public init(id: SignerId, onSign: @escaping OnSign) {
self.id = id
self.onSign = onSign
}

Expand All @@ -51,43 +47,49 @@ public final class Signer {
public struct SignerId: Hashable, CustomStringConvertible, RawRepresentable {

public var rawValue: String {
"\(account)-\(chainId)"
"\(signerType)-\(account)-\(chainId)"
}

public var description: String {
rawValue
}

public let signerType: SignerType
public let account: String
public let chainId: Int

public init(account: String, chainId: Int) {
public init(signerType: SignerType, account: String, chainId: Int) {
self.signerType = signerType
self.account = account
self.chainId = chainId
}

public init?(rawValue: String) {
let idComponents = rawValue.split(separator: "-")
guard idComponents.count == 2 else {
guard idComponents.count == 3 else {
return nil
}
let account = String(idComponents[0])
guard let chainId = Int(idComponents[1]) else {
guard let signerType = SignerType(rawValue: String(idComponents[0])) else {
return nil
}
let account = String(idComponents[1])
guard let chainId = Int(idComponents[2]) else {
return nil
}
self.signerType = signerType
self.account = account
self.chainId = chainId
}
}

public final class SignerServiceFFI {
public final class NativeSignerFFI {

public let signer: Signer
public let signer: NativeSigner

public init(signer_id: RustString) {
let idString = signer_id.toString()
let signerId = SignerId(rawValue: idString)!
self.signer = Signers.shared.signer(id: signerId)!
self.signer = Signers.shared.signer(id: signerId)!.nativeSigner!
}

public func sign(message: RustString) -> FFIStringResult {
Expand All @@ -96,17 +98,3 @@ public final class SignerServiceFFI {
.ffi
}
}

extension String: Error {}

extension Result where Success == String, Failure == String {

public var ffi: FFIStringResult {
switch self {
case .success(let value):
return .Ok(value.intoRustString())
case .failure(let error):
return .Err(error.localizedDescription.intoRustString())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

public final class PrivateKeySignerFFI {

public let signerId: SignerId

private let pK: String

public init(signer_id: RustString) {
let idString = signer_id.toString()
let signerId = SignerId(rawValue: idString)!
self.signerId = signerId
self.pK = Signers.shared.signer(id: signerId)!.privateKeySigner!.privateKey
}

public func private_key() -> FFIStringResult {
.Ok(pK.intoRustString())
}
}

public struct PrivateKeySigner: Identifiable {

public let id: SignerId

public let privateKey: String

public init(id: SignerId, privateKey: String) {
self.id = id
self.privateKey = privateKey
}
}
69 changes: 69 additions & 0 deletions crates/ffi/YttriumCore/Sources/YttriumCore/SwiftFFI/Signer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation

public enum Signer {
case native(NativeSigner)
case privateKey(PrivateKeySigner)

public var signerType: SignerType {
switch self {
case .native:
return .native
case .privateKey:
return .privateKey
}
}

public var signerId: SignerId {
switch self {
case .native(let native):
return native.id
case .privateKey(let privateKey):
return privateKey.id
}
}

public var privateKeySigner: PrivateKeySigner? {
switch self {
case .native:
return nil
case .privateKey(let privateKey):
return privateKey
}
}

public var nativeSigner: NativeSigner? {
switch self {
case .native(let native):
return native
case .privateKey:
return nil
}
}
}

public enum SignerType: String, Codable {
case native = "Native"
case privateKey = "PrivateKey"

public func toRustString() -> RustString {
rawValue.intoRustString()
}
}

public enum SignerError: Error {
case unknown
}

extension String: Error {}

extension Result where Success == String, Failure == String {

public var ffi: FFIStringResult {
switch self {
case .success(let value):
return .Ok(value.intoRustString())
case .failure(let error):
return .Err(error.localizedDescription.intoRustString())
}
}
}
Loading

0 comments on commit 08362a7

Please sign in to comment.