Skip to content

Commit

Permalink
better substrate service
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Nov 7, 2023
1 parent ac80b9b commit 5bc6849
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 55 deletions.
10 changes: 9 additions & 1 deletion Rust/tesseract/src/protocols/substrate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use tesseract_protocol_substrate::{AccountType, GetAccountResponse};
use tesseract_swift_utils::{data::CData, string::CString, error::CError};
use std::mem::ManuallyDrop;

#[cfg(feature="protocol-substrate-service")]
pub mod service;
Expand Down Expand Up @@ -43,4 +44,11 @@ impl TryFrom<SubstrateGetAccountResponse> for GetAccountResponse {
.and_then(|pk| value.path.try_into().map(|pt| (pk, pt)))
.map(|(pk, pt)|Self{ public_key: pk, path: pt })
}
}
}

#[no_mangle]
pub unsafe extern "C" fn tesseract_substrate_get_account_response_free(
res: &mut ManuallyDrop<SubstrateGetAccountResponse>
) {
ManuallyDrop::drop(res);
}
2 changes: 2 additions & 0 deletions Sources/CTesseract/include/tesseract-swift.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ void tesseract_service_free(struct ServiceTesseract *tesseract);
struct ServiceTesseract tesseract_service_add_test_service(struct ServiceTesseract *tesseract,
struct TestService service);

void tesseract_substrate_get_account_response_free(struct SubstrateGetAccountResponse *res);

struct ServiceTesseract tesseract_service_add_substrate_service(struct ServiceTesseract *tesseract,
struct SubstrateService service);

Expand Down
62 changes: 11 additions & 51 deletions Sources/TesseractService/SubstrateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,8 @@ import Foundation
import CTesseractBin
import TesseractShared

extension SubstrateGetAccountResponse: CValue, CType {
public typealias CVal = Self
}

extension CFuture_SubstrateGetAccountResponse: CFuturePtr {
public typealias CVal = SubstrateGetAccountResponse
public typealias Val = (pubKey: Data, path: String)

public mutating func _onComplete(cb: @escaping (CResult<CVal>) -> Void) -> CResult<CVal>? {
_withOnCompleteContext(cb) { ctx, value, error in
self.set_on_complete(&self, ctx, value, error) { ctx, val, err in
Self._onCompleteCallback(ctx, val, err)
}
}
}

public mutating func _setupSetOnCompleteFunc() {
self.set_on_complete = { this, ctx, value, error, cb in
Self._setOnCompleteFunc(this, ctx, value, error) { this, val, err in
cb?(this, val, err)
}
}
}

public static func convert(
cvalue: inout SubstrateGetAccountResponse
) -> CResult<(pubKey: Data, path: String)> {
.success((pubKey: cvalue.public_key.owned(), path: cvalue.path.owned()))
}

public static func convert(
value: inout (pubKey: Data, path: String)
) -> CResult<SubstrateGetAccountResponse> {
.success(SubstrateGetAccountResponse(public_key: value.pubKey.copiedPtr(),
path: value.path.copiedPtr()))
}
}
@_exported import enum TesseractShared.SubstrateAccountType
@_exported import struct TesseractShared.SubstrateGetAccountResponse

extension CTesseract.SubstrateService: CoreService {
public func register(
Expand All @@ -55,16 +20,8 @@ extension CTesseract.SubstrateService: CoreService {
}
}

public protocol SubstrateService: Service where Core == CTesseract.SubstrateService {
func getAccount(
type: SubstrateAccountType
) async -> Result<(pubKey: Data, path: String), TesseractError>

func signTransation(
type: SubstrateAccountType, path: String,
extrinsic: Data, metadata: Data, types: Data
) async -> Result<Data, TesseractError>
}
public protocol SubstrateService: TesseractShared.SubstrateService, Service
where Core == CTesseract.SubstrateService {}

public extension SubstrateService {
func toCore() -> Core {
Expand All @@ -77,18 +34,20 @@ public extension SubstrateService {

private func substrate_service_get_account(
this: UnsafePointer<CTesseract.SubstrateService>!,
accountType: SubstrateAccountType
accountType: CTesseract.SubstrateAccountType
) -> CFuture_SubstrateGetAccountResponse {
CFuture_SubstrateGetAccountResponse {
await this.unowned((any SubstrateService).self).castError().asyncFlatMap {
await $0.getAccount(type: accountType)
await $0.getAccount(
type: TesseractShared.SubstrateAccountType(cvalue: accountType)
)
}
}
}

private func substrate_service_sign(
this: UnsafePointer<CTesseract.SubstrateService>!,
type: SubstrateAccountType, path: CStringRef!,
type: CTesseract.SubstrateAccountType, path: CStringRef!,
extrinsic: CDataRef, metadata: CDataRef, types: CDataRef
) -> CFutureData {
let path = path.copied()
Expand All @@ -97,7 +56,8 @@ private func substrate_service_sign(
let types = types.copied()
return CFutureData {
await this.unowned((any SubstrateService).self).castError().asyncFlatMap {
await $0.signTransation(type: type, path: path, extrinsic: extrinsic,
await $0.signTransation(type: TesseractShared.SubstrateAccountType(cvalue: type),
path: path, extrinsic: extrinsic,
metadata: metadata, types: types)
}
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/TesseractService/TestService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ extension CTesseract.TestService: CoreService {
}
}

public protocol TestService: Service where Core == CTesseract.TestService {
func signTransation(req: String) async -> Result<String, TesseractError>
}
public protocol TestService: TesseractShared.TestService, Service
where Core == CTesseract.TestService {}

public extension TestService {
func toCore() -> Core {
Expand Down
112 changes: 112 additions & 0 deletions Sources/TesseractShared/SubstrateService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// SubstrateService.swift
//
//
// Created by Yehor Popovych on 07/11/2023.
//

import Foundation
import CTesseractBin
#if !COCOAPODS
import TesseractTransportsShared
#endif

public enum SubstrateAccountType {
case sr25519
case ed25519
case ecdsa
}

public struct SubstrateGetAccountResponse {
public let pubKey: Data
public let path: String

public init(pubKey: Data, path: String) {
self.pubKey = pubKey
self.path = path
}
}

public protocol SubstrateService {
func getAccount(
type: SubstrateAccountType
) async -> Result<SubstrateGetAccountResponse, TesseractError>

func signTransation(
type: SubstrateAccountType, path: String,
extrinsic: Data, metadata: Data, types: Data
) async -> Result<Data, TesseractError>
}

extension CTesseract.SubstrateAccountType: CType {
public init() { self.init(0) }
}

extension CTesseract.SubstrateGetAccountResponse: CType, CPtr {
public typealias Val = SubstrateGetAccountResponse

public func copied() -> SubstrateGetAccountResponse {
SubstrateGetAccountResponse(pubKey: public_key.copied(),
path: path.copied())
}

public mutating func owned() -> SubstrateGetAccountResponse {
defer { self.free() }
return self.copied()
}

public mutating func free() {
tesseract_substrate_get_account_response_free(&self)
}
}

extension SubstrateGetAccountResponse: AsCPtrCopy {
public typealias CopyPtr = CTesseract.SubstrateGetAccountResponse

public func copiedPtr() -> CTesseract.SubstrateGetAccountResponse {
CTesseract.SubstrateGetAccountResponse(public_key: pubKey.copiedPtr(),
path: path.copiedPtr())
}
}

extension SubstrateAccountType: CValue {
public typealias CVal = CTesseract.SubstrateAccountType

public init(cvalue val: CTesseract.SubstrateAccountType) {
switch val {
case SubstrateAccountType_Ed25519: self = .ed25519
case SubstrateAccountType_Sr25519: self = .sr25519
case SubstrateAccountType_Ecdsa: self = .ecdsa
default: fatalError("Unsupported account type: \(val)")
}
}

public var asCValue: CTesseract.SubstrateAccountType {
switch self {
case .sr25519: return SubstrateAccountType_Sr25519
case .ed25519: return SubstrateAccountType_Ed25519
case .ecdsa: return SubstrateAccountType_Ecdsa
}
}
}

extension CFuture_SubstrateGetAccountResponse: CFuturePtr {
public typealias CVal = CTesseract.SubstrateGetAccountResponse
public typealias Val = SubstrateGetAccountResponse

public mutating func _onComplete(cb: @escaping (CResult<CVal>) -> Void) -> CResult<CVal>? {
_withOnCompleteContext(cb) { ctx, value, error in
self.set_on_complete(&self, ctx, value, error) { ctx, val, err in
Self._onCompleteCallback(ctx, val, err)
}
}
}

public mutating func _setupSetOnCompleteFunc() {
self.set_on_complete = { this, ctx, value, error, cb in
Self._setOnCompleteFunc(this, ctx, value, error) { this, val, err in
cb?(this, val, err)
}
}
}
}
15 changes: 15 additions & 0 deletions Sources/TesseractShared/TestService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// TestService.swift
//
//
// Created by Yehor Popovych on 07/11/2023.
//

import Foundation
#if !COCOAPODS
import TesseractTransportsShared
#endif

public protocol TestService {
func signTransation(req: String) async -> Result<String, TesseractError>
}

0 comments on commit 5bc6849

Please sign in to comment.