Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ public class CoreWalletManager: ObservableObject {

// MARK: - Account Management

/// Build a signed transaction
/// - Parameters:
/// - accountIndex: The account index to use
/// - outputs: The transaction outputs
/// - Returns: The signed transaction bytes
public func buildSignedTransaction(for wallet: HDWallet, accIndex: UInt32, outputs: [Transaction.Output]) throws -> (Data, UInt64) {
try sdkWalletManager.buildSignedTransaction(for: wallet, accIndex: accIndex, outputs: outputs)
}

/// Get transactions for a wallet
/// - Parameters:
/// - wallet: The wallet to get transactions for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import Foundation
Comment thread
ZocoLini marked this conversation as resolved.
import DashSDKFFI

/// Result of building and signing a transaction
public struct BuildAndSignResult: Sendable {
/// The signed transaction bytes
public let transactionData: Data
/// The fee paid in duffs
public let fee: UInt64
}

/// Transaction utilities for wallet operations
public class Transaction {

Expand All @@ -23,9 +15,11 @@ public class Transaction {
}

func toFFI() -> FFITxOutput {
return address.withCString { addressCStr in
FFITxOutput(address: addressCStr, amount: amount)
}
// TODO: This memory is not being freed, FFI must free FFITxOutput
// or expose a method to do it
let cString = strdup(address)
Comment thread
ZocoLini marked this conversation as resolved.

Comment thread
ZocoLini marked this conversation as resolved.
return FFITxOutput(address: cString, amount: amount)
}
Comment thread
ZocoLini marked this conversation as resolved.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public class Wallet {

return count
}

// MARK: - Key Derivation

/// Get the extended public key for an account
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,64 @@ public class WalletManager {
return success
}

/// Build a signed transaction
/// - Parameters:
/// - accIndex: The account index to use
/// - outputs: The transaction outputs
/// - Returns: The signed transaction bytes and the fee
Comment thread
ZocoLini marked this conversation as resolved.
public func buildSignedTransaction(for wallet: HDWallet, accIndex: UInt32, outputs: [Transaction.Output]) throws -> (Data, UInt64) {
guard !outputs.isEmpty else {
throw KeyWalletError.invalidInput("Transaction must have at least one output")
}

var error = FFIError()
var txBytesPtr: UnsafeMutablePointer<UInt8>?
var txLen: size_t = 0

var fee: UInt64 = 0

guard let wallet = try self.getWallet(id: wallet.walletId) else {
throw KeyWalletError.walletError("Wallet not found in manager")
}

let ffiOutputs = outputs.map { $0.toFFI() }

let success = ffiOutputs.withUnsafeBufferPointer { outputsPtr in
wallet_build_and_sign_transaction(
Comment thread
ZocoLini marked this conversation as resolved.
self.handle,
wallet.ffiHandle,
accIndex,
outputsPtr.baseAddress,
outputs.count,
1000,
&fee,
&txBytesPtr,
&txLen,
&error)
}

defer {
if error.message != nil {
error_message_free(error.message)
}
for _ in ffiOutputs {
// TODO: Memory leak, FFI doesnt expose a way to free the address
}
Comment thread
ZocoLini marked this conversation as resolved.
if let ptr = txBytesPtr {
transaction_bytes_free(ptr)
}
}
Comment thread
ZocoLini marked this conversation as resolved.
Comment thread
ZocoLini marked this conversation as resolved.

guard success, let ptr = txBytesPtr else {
throw KeyWalletError(ffiError: error)
}

// Copy the transaction data before freeing
let txData = Data(bytes: ptr, count: txLen)

return (txData, fee)
}

// MARK: - Block Height Management

/// Get the current block height for a network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,20 @@ class SendViewModel: ObservableObject {
successMessage = "Transfer to Platform complete"

case .coreToCore:
// TODO: Implement standard Core → Core transaction
error = "Core to Core transfer not yet implemented"
let outputs = [
Transaction.Output(address: recipientAddress, amount: amount)
]

// TODO: The model is using hardoced estimated fees
let (tx, _) = try walletService.walletManager
.buildSignedTransaction(
for: wallet,
accIndex: 0,
outputs: outputs
)

try walletService.broadcastTransaction(tx)
successMessage = "Transfer to Core complete"
}

// Refresh shielded balance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ struct CreateWalletView: View {
Task {
do {
print("=== STARTING WALLET CREATION ===")

let mnemonic = (showImportOption ? importMnemonic : mnemonic)
print("PIN length: \(walletPin.count)")
print("Import option enabled: \(showImportOption)")
Expand Down
Loading