Skip to content
Open
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
10 changes: 9 additions & 1 deletion packages/swift-sdk/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ let package = Package(
name: "SwiftDashSDK",
dependencies: ["DashSDKFFI"],
path: "Sources/SwiftDashSDK",
exclude: ["KeyWallet/README.md"]
exclude: ["KeyWallet/README.md"],
linkerSettings: [.linkedFramework("SystemConfiguration")]
),

// Tests
.testTarget(
name: "SwiftDashSDKTests",
dependencies: ["SwiftDashSDK"],
path: "Tests/SwiftDashSDKTests"
)
],
swiftLanguageModes: [.v6]
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PlatformWallet {
}

/// Create a new Platform Wallet from a 64-byte seed
public static func fromSeed(_ seed: Data) throws -> PlatformWallet {
public static func fromSeed(_ seed: Data, network: Network = .testnet) throws -> PlatformWallet {
guard seed.count == 64 else {
throw PlatformWalletError.invalidParameter
}
Expand All @@ -25,6 +25,7 @@ public class PlatformWallet {

let result = seed.withUnsafeBytes { seedPtr in
platform_wallet_info_create_from_seed(
network,
seedPtr.baseAddress?.assumingMemoryBound(to: UInt8.self),
seed.count,
&handle,
Expand All @@ -40,14 +41,19 @@ public class PlatformWallet {
}

/// Create a new Platform Wallet from a BIP39 mnemonic phrase
public static func fromMnemonic(_ mnemonic: String, passphrase: String? = nil) throws -> PlatformWallet {
public static func fromMnemonic(
_ mnemonic: String,
passphrase: String? = nil,
network: Network = .testnet
) throws -> PlatformWallet {
var handle: Handle = NULL_HANDLE
var error = PlatformWalletFFIError()

let mnemonicCStr = (mnemonic as NSString).utf8String
let passphraseCStr = passphrase != nil ? (passphrase! as NSString).utf8String : nil

let result = platform_wallet_info_create_from_mnemonic(
network,
mnemonicCStr,
passphraseCStr,
Comment on lines 18 to 58
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocking: Wallet creation now sends the wrong network enum into the platform-wallet FFI

The new network: overloads are typed as SwiftDashSDK.Network (DashSDKNetwork) and pass that value straight into platform_wallet_info_create_from_seed / platform_wallet_info_create_from_mnemonic. That is not the same enum the platform-wallet FFI expects. DashSDKNetwork uses mainnet=0, testnet=1, regtest=2, devnet=3, local=4 in packages/rs-sdk-ffi/src/types.rs, while the Rust platform-wallet side uses dashcore::Network, whose discriminants are Dash=0, Testnet=1, Devnet=2, Regtest=3 (see the checked-out dash-network dependency at ~/.cargo/git/checkouts/rust-dashcore-a35422abb966ea16/6affdaa/dash-network/src/lib.rs). So .regtest now creates a devnet wallet, .devnet creates a regtest wallet, and .local passes an invalid discriminant across the FFI boundary. Before this PR the Swift bridge used PlatformNetwork.ffiValue, which matched the platform-wallet network layout. The fix here is to keep using the platform-wallet network enum/mapping for these entry points instead of reusing the unified SDK enum directly.

source: ['codex']

🤖 Fix this with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWallet.swift`:
- [BLOCKING] lines 18-58: Wallet creation now sends the wrong network enum into the platform-wallet FFI
  The new `network:` overloads are typed as `SwiftDashSDK.Network` (`DashSDKNetwork`) and pass that value straight into `platform_wallet_info_create_from_seed` / `platform_wallet_info_create_from_mnemonic`. That is not the same enum the platform-wallet FFI expects. `DashSDKNetwork` uses `mainnet=0, testnet=1, regtest=2, devnet=3, local=4` in `packages/rs-sdk-ffi/src/types.rs`, while the Rust platform-wallet side uses `dashcore::Network`, whose discriminants are `Dash=0, Testnet=1, Devnet=2, Regtest=3` (see the checked-out dash-network dependency at `~/.cargo/git/checkouts/rust-dashcore-a35422abb966ea16/6affdaa/dash-network/src/lib.rs`). So `.regtest` now creates a devnet wallet, `.devnet` creates a regtest wallet, and `.local` passes an invalid discriminant across the FFI boundary. Before this PR the Swift bridge used `PlatformNetwork.ffiValue`, which matched the platform-wallet network layout. The fix here is to keep using the platform-wallet network enum/mapping for these entry points instead of reusing the unified SDK enum directly.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh, I will ignore this since I am working on a Network unification PR, in case that is possible

&handle,
Expand All @@ -73,7 +79,6 @@ public class PlatformWallet {

let result = platform_wallet_info_get_identity_manager(
handle,
network.ffiValue,
&managerHandle,
&error
)
Expand All @@ -93,7 +98,6 @@ public class PlatformWallet {

let result = platform_wallet_info_set_identity_manager(
handle,
network.ffiValue,
manager.handle,
&error
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation

@_silgen_name("platform_wallet_info_create_from_seed")
func platform_wallet_info_create_from_seed(
_ network: Network,
_ seed: UnsafePointer<UInt8>?,
_ seed_len: Int,
_ out_handle: UnsafeMutablePointer<Handle>,
Expand All @@ -15,6 +16,7 @@ func platform_wallet_info_create_from_seed(

@_silgen_name("platform_wallet_info_create_from_mnemonic")
func platform_wallet_info_create_from_mnemonic(
_ network: Network,
_ mnemonic: UnsafePointer<CChar>?,
_ passphrase: UnsafePointer<CChar>?,
_ out_handle: UnsafeMutablePointer<Handle>,
Expand All @@ -24,15 +26,13 @@ func platform_wallet_info_create_from_mnemonic(
@_silgen_name("platform_wallet_info_get_identity_manager")
func platform_wallet_info_get_identity_manager(
_ wallet_handle: Handle,
_ network: NetworkType,
_ out_manager_handle: UnsafeMutablePointer<Handle>,
_ out_error: UnsafeMutablePointer<PlatformWalletFFIError>
) -> PlatformWalletFFIResult

@_silgen_name("platform_wallet_info_set_identity_manager")
func platform_wallet_info_set_identity_manager(
_ wallet_handle: Handle,
_ network: NetworkType,
_ manager_handle: Handle,
_ out_error: UnsafeMutablePointer<PlatformWalletFFIError>
) -> PlatformWalletFFIResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ typealias NetworkType = UInt32
typealias PlatformWalletFFIResult = Int32

struct PlatformWalletFFIError {
var message: UnsafePointer<CChar>?
var code: PlatformWalletFFIResult = 0
var message: UnsafePointer<CChar>? = nil
}

struct FFIBlockTime {
Expand All @@ -33,19 +34,21 @@ struct FFIBlockTime {
var timestamp: UInt64
}

// Error result codes (must match Rust enum values)
// Error result codes (must match Rust enum PlatformWalletFFIResult in rs-platform-wallet-ffi/src/error.rs)
let Success: PlatformWalletFFIResult = 0
let ErrorNullPointer: PlatformWalletFFIResult = 1
let ErrorInvalidHandle: PlatformWalletFFIResult = 2
let ErrorInvalidParameter: PlatformWalletFFIResult = 3
let ErrorInvalidIdentifier: PlatformWalletFFIResult = 4
let ErrorInvalidNetwork: PlatformWalletFFIResult = 5
let ErrorInvalidHandle: PlatformWalletFFIResult = 1
let ErrorInvalidParameter: PlatformWalletFFIResult = 2
let ErrorNullPointer: PlatformWalletFFIResult = 3
let ErrorSerialization: PlatformWalletFFIResult = 4
let ErrorDeserialization: PlatformWalletFFIResult = 5
let ErrorWalletOperation: PlatformWalletFFIResult = 6
let ErrorIdentityNotFound: PlatformWalletFFIResult = 7
let ErrorContactNotFound: PlatformWalletFFIResult = 8
let ErrorUtf8Conversion: PlatformWalletFFIResult = 9
let ErrorSerialization: PlatformWalletFFIResult = 10
let ErrorDeserialization: PlatformWalletFFIResult = 11
let ErrorInvalidNetwork: PlatformWalletFFIResult = 9
let ErrorInvalidIdentifier: PlatformWalletFFIResult = 10
let ErrorMemoryAllocation: PlatformWalletFFIResult = 11
let ErrorUtf8Conversion: PlatformWalletFFIResult = 12
let ErrorUnknown: PlatformWalletFFIResult = 99

/// Platform Wallet error types
public enum PlatformWalletError: Error {
Expand Down
11 changes: 11 additions & 0 deletions packages/swift-sdk/Sources/SwiftDashSDK/SwiftDashSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@
public typealias Network = DashSDKNetwork
public typealias ErrorCode = DashSDKErrorCode
public typealias SDKConfig = DashSDKConfig

// Named constants for `Network` (imported from C as a raw-value struct
// without Swift case names). Raw values match the `DashSDKNetwork` C enum
// in rs-sdk-ffi.h.
public extension DashSDKNetwork {
static let mainnet = DashSDKNetwork(rawValue: 0)
static let testnet = DashSDKNetwork(rawValue: 1)
static let regtest = DashSDKNetwork(rawValue: 2)
static let devnet = DashSDKNetwork(rawValue: 3)
static let local = DashSDKNetwork(rawValue: 4)
}
31 changes: 0 additions & 31 deletions packages/swift-sdk/SwiftTests/Package.swift

This file was deleted.

Loading
Loading