Skip to content

Commit

Permalink
feat(send): implement send_transaction, bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpooleywc committed Sep 10, 2024
1 parent 904c24f commit 11b4145
Show file tree
Hide file tree
Showing 36 changed files with 1,601 additions and 311 deletions.
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 11b4145

Please sign in to comment.