-
Notifications
You must be signed in to change notification settings - Fork 56
feat(swift-sdk): transaction decoder over upstream key-wallet-ffi transaction_decode #3981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7cb62fe
feat(platform-wallet): add pure transaction decoder + thin FFI/Swift …
llbartekll 4eaa825
refactor(platform-wallet): map dashcore::Transaction directly to FFI …
llbartekll 225c1ae
Merge remote-tracking branch 'origin/v4.1-dev' into feat/core-tx-deco…
llbartekll ffa8c94
fix(platform-wallet): null out_decoded before validating tx_bytes
llbartekll 195e284
Merge remote-tracking branch 'origin/v4.1-dev' into feat/core-tx-deco…
QuantumExplorer 1c18092
test(swift-sdk): fix mis-transcribed tx-decode fixture hex
QuantumExplorer edc0be7
refactor(swift-sdk): consume upstream transaction_decode from key-wal…
QuantumExplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
packages/swift-sdk/Sources/SwiftDashSDK/KeyWallet/TransactionDecoder.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import Foundation | ||
| import DashSDKFFI | ||
|
|
||
| /// A consensus-decoded Dash transaction: per-output `(address, value, script)` | ||
| /// plus per-input previous outpoints with a best-effort P2PKH sender address. | ||
| /// | ||
| /// Wallet persistence stores every transaction's raw bytes but only | ||
| /// materializes TXO rows for the wallet's own outputs, so matching arbitrary | ||
| /// transactions against external addresses (e.g. the CrowdNode on-chain API: | ||
| /// "did a tx pay amount X to address Y?") requires decoding the stored bytes. | ||
| /// `TransactionDecoder.decode` is that opener. | ||
| public struct DecodedTransaction: Sendable, Equatable { | ||
| public struct Input: Sendable, Equatable { | ||
| /// Previous output's txid in consensus (internal) byte order — | ||
| /// reverse for explorer-style display (see `prevTxidDisplayHex`). | ||
| public let prevTxid: Data | ||
| /// Previous output's index. | ||
| public let prevVout: UInt32 | ||
| /// Sender address recovered from a P2PKH-shaped scriptSig; nil for | ||
| /// coinbase / non-P2PKH / unparseable script sigs. Unauthenticated — | ||
| /// derived from a script push the spender fully controls, with no | ||
| /// signature verification against the spent UTXO. Use it for display | ||
| /// or as a matching hint only, never for authentication or | ||
| /// authorization; the attacker-resistant matching primitive is the | ||
| /// per-output `(address, valueDuffs)` pair. | ||
| public let address: String? | ||
|
|
||
| /// Explorer-style (reversed, hex) rendering of `prevTxid`. | ||
| public var prevTxidDisplayHex: String { | ||
| prevTxid.reversed().map { String(format: "%02x", $0) }.joined() | ||
| } | ||
| } | ||
|
|
||
| public struct Output: Sendable, Equatable { | ||
| /// Destination address for standard scripts (P2PKH / P2SH); nil for | ||
| /// non-standard scripts (OP_RETURN, bare multisig, …). | ||
| public let address: String? | ||
| /// Output value in duffs. | ||
| public let valueDuffs: UInt64 | ||
| /// Raw scriptPubKey bytes. | ||
| public let scriptPubkey: Data | ||
| } | ||
|
|
||
| /// Transaction id in consensus (internal) byte order — reverse for | ||
| /// explorer-style display (see `txidDisplayHex`). | ||
| public let txid: Data | ||
| public let inputs: [Input] | ||
| public let outputs: [Output] | ||
|
|
||
| /// Explorer-style (reversed, hex) rendering of `txid`. | ||
| public var txidDisplayHex: String { | ||
| txid.reversed().map { String(format: "%02x", $0) }.joined() | ||
| } | ||
| } | ||
|
|
||
| /// Pure utility — decodes raw transaction bytes via `transaction_decode` | ||
| /// (key-wallet-ffi). No wallet handle or state involved. | ||
| public enum TransactionDecoder { | ||
| /// Consensus-decode raw transaction bytes. | ||
| /// - Parameters: | ||
| /// - txData: Serialized transaction bytes (e.g. from persisted rows). | ||
| /// - network: Network used to render addresses (base58 version bytes). | ||
| /// - Returns: The decoded transaction. | ||
| /// - Throws: `KeyWalletError.invalidInput` for malformed or empty bytes | ||
| /// (including trailing garbage). | ||
| public static func decode(_ txData: Data, network: Network) throws -> DecodedTransaction { | ||
| var error = FFIError() | ||
| var outDecoded: UnsafeMutablePointer<DecodedTransactionFFI>? = nil | ||
| let success = txData.withUnsafeBytes { (raw: UnsafeRawBufferPointer) -> Bool in | ||
| transaction_decode( | ||
| raw.baseAddress?.assumingMemoryBound(to: UInt8.self), | ||
| txData.count, | ||
| network.ffiValue, | ||
| &outDecoded, | ||
| &error | ||
| ) | ||
| } | ||
|
|
||
| defer { | ||
| if error.message != nil { | ||
| error_message_free(error.message) | ||
| } | ||
| } | ||
|
|
||
| guard success, let decoded = outDecoded else { | ||
| throw KeyWalletError(ffiError: error) | ||
| } | ||
| defer { decoded_transaction_free(decoded) } | ||
|
|
||
| var entry = decoded.pointee | ||
| let txid = withUnsafeBytes(of: &entry.txid) { Data($0) } | ||
|
|
||
| var inputs: [DecodedTransaction.Input] = [] | ||
| if let ptr = entry.inputs, entry.inputs_count > 0 { | ||
| inputs = (0..<Int(entry.inputs_count)).map { i in | ||
| var input = ptr[i] | ||
| let prevTxid = withUnsafeBytes(of: &input.prev_txid) { Data($0) } | ||
| let address = input.address.map { String(cString: $0) } | ||
| return DecodedTransaction.Input( | ||
| prevTxid: prevTxid, | ||
| prevVout: input.prev_vout, | ||
| address: address | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| var outputs: [DecodedTransaction.Output] = [] | ||
| if let ptr = entry.outputs, entry.outputs_count > 0 { | ||
| outputs = (0..<Int(entry.outputs_count)).map { i in | ||
| let output = ptr[i] | ||
| let address = output.address.map { String(cString: $0) } | ||
| let script: Data | ||
| if let sptr = output.script_pubkey, output.script_pubkey_len > 0 { | ||
| script = Data(bytes: sptr, count: Int(output.script_pubkey_len)) | ||
| } else { | ||
| script = Data() | ||
| } | ||
| return DecodedTransaction.Output( | ||
| address: address, | ||
| valueDuffs: output.value_duffs, | ||
| scriptPubkey: script | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return DecodedTransaction(txid: txid, inputs: inputs, outputs: outputs) | ||
| } | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionDecoderTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import XCTest | ||
| @testable import SwiftDashSDK | ||
|
|
||
| /// Pure-function tests for `TransactionDecoder` — no wallet handle or network | ||
| /// access. The fixture is the same synthetic transaction exercised by the | ||
| /// Rust-side unit tests in `key-wallet-ffi/src/tx_decode.rs` (rust-dashcore): | ||
| /// one P2PKH input (spending 1111…11:3, scriptSig = push(sig) push(pubkey)), | ||
| /// one P2PKH output of 151 072 duffs, and one OP_RETURN output. | ||
| final class TransactionDecoderTests: XCTestCase { | ||
| private static let fixtureHex = | ||
| "01000000011111111111111111111111111111111111111111111111111111111111111111" | ||
| + "030000006a4730303030303030303030303030303030303030303030303030303030303030" | ||
| + "30303030303030303030303030303030303030303030303030303030303030303030303030" | ||
| + "303030210324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c" | ||
| + "ffffffff02204e0200000000001976a91414db4138d56a2ecfb10881a9be394d9f321985b2" | ||
| + "88ac0000000000000000066a04aaaaaaaa00000000" | ||
|
|
||
| private static let fixtureAddress = "yNDj28QBMm5sY6bLjFcNdWRNef24KLQNuQ" | ||
| private static let fixtureTxidDisplay = | ||
| "bf7479216e5ba76f60bf11654c881824c6f9cdbb64eebe332cf835a3391cb5d5" | ||
|
|
||
| private var fixtureData: Data { | ||
| var data = Data() | ||
| let hex = Self.fixtureHex | ||
| var index = hex.startIndex | ||
| while index < hex.endIndex { | ||
| let next = hex.index(index, offsetBy: 2) | ||
| data.append(UInt8(hex[index..<next], radix: 16)!) | ||
| index = next | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| func testDecodesOutputsWithAddressesAndValues() throws { | ||
| let decoded = try TransactionDecoder.decode(fixtureData, network: .testnet) | ||
|
|
||
| XCTAssertEqual(decoded.outputs.count, 2) | ||
|
|
||
| // P2PKH output: exact signal amount, testnet address. | ||
| XCTAssertEqual(decoded.outputs[0].address, Self.fixtureAddress) | ||
| XCTAssertEqual(decoded.outputs[0].valueDuffs, 151_072) | ||
| XCTAssertFalse(decoded.outputs[0].scriptPubkey.isEmpty) | ||
|
|
||
| // OP_RETURN output: no address, script still exposed. | ||
| XCTAssertNil(decoded.outputs[1].address) | ||
| XCTAssertFalse(decoded.outputs[1].scriptPubkey.isEmpty) | ||
| } | ||
|
|
||
| func testRecoversP2PKHInputAddressAndOutpoint() throws { | ||
| let decoded = try TransactionDecoder.decode(fixtureData, network: .testnet) | ||
|
|
||
| XCTAssertEqual(decoded.inputs.count, 1) | ||
| XCTAssertEqual(decoded.inputs[0].prevTxid, Data(repeating: 0x11, count: 32)) | ||
| XCTAssertEqual(decoded.inputs[0].prevVout, 3) | ||
| XCTAssertEqual(decoded.inputs[0].address, Self.fixtureAddress) | ||
| } | ||
|
|
||
| func testTxidDisplayHexMatchesExplorerOrder() throws { | ||
| let decoded = try TransactionDecoder.decode(fixtureData, network: .testnet) | ||
| XCTAssertEqual(decoded.txidDisplayHex, Self.fixtureTxidDisplay) | ||
| XCTAssertEqual(decoded.txid, Data(decoded.txid.reversed().reversed())) | ||
| } | ||
|
|
||
| func testNetworkChangesRenderedAddresses() throws { | ||
| let mainnet = try TransactionDecoder.decode(fixtureData, network: .mainnet) | ||
| XCTAssertNotEqual(mainnet.outputs[0].address, Self.fixtureAddress) | ||
| XCTAssertTrue(mainnet.outputs[0].address?.hasPrefix("X") ?? false) | ||
| } | ||
|
|
||
| func testMalformedBytesThrowDeserialization() { | ||
| var bytes = fixtureData | ||
| bytes.append(contentsOf: [0xDE, 0xAD]) // trailing garbage | ||
| XCTAssertThrowsError(try TransactionDecoder.decode(bytes, network: .testnet)) | ||
| XCTAssertThrowsError(try TransactionDecoder.decode(Data([0xFF, 0xFF, 0xFF]), network: .testnet)) | ||
| } | ||
|
|
||
| func testEmptyInputThrowsInvalidParameter() { | ||
| XCTAssertThrowsError(try TransactionDecoder.decode(Data(), network: .testnet)) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.