import web3swift
func sendRawTransaction(rawTransactionData: Data) {
let infuraUrl = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
let web3 = try! Web3.new(URL(string: infuraUrl)!)
// Create a raw transaction from data
let rawTxBytes = rawTransactionData
// Send the raw transaction
web3.eth.sendRawTransaction(rawTx: rawTxBytes) { result in
switch result {
case .success(let transactionHash):
print("Transaction successfully sent. Hash: \(transactionHash)")
case .failure(let error):
print("Failed to send transaction: \(error.localizedDescription)")
}
}
}
SOLANA Transaction
1. Build Solana Transaction
create a new Build BuildSolTransactionRequest should include the following attributes:
chainID: Chain ID (e.g., Solana mainnet is 501).
fromAddress: Sender's wallet address.
toAddress: Recipient's wallet address.
Amount: Amount to transfer.
paillierGroupID: User's Paillier group ID.
keyGenGroupID: Master wallet's key generation group ID.
KkeyIdentity: the keyIdentity of wallet
eCType: Cryptographic algorithm type (e.g., eddsa, ecdsa).
Use a BTC library to send the raw transaction Example:
import Foundation
extension Data {
/// Returns a lowercase hex string representation of the Data.
func toHexString() -> String {
return self.map { String(format: "%02x", $0) }.joined()
}
}
func sendRawTransaction(rawTxBytes: Data) {
// 1. Define the URL
guard let url = URL(string: "https://blockstream.info/testnet/api/tx") else {
print("Invalid URL")
return
}
// 2. Prepare the URLRequest
var request = URLRequest(url: url)
request.httpMethod = "POST"
// Blockstream’s API expects `text/plain`
request.setValue("text/plain", forHTTPHeaderField: "Content-Type")
// 3. The raw transaction in hex (same as in your curl --data)
let rawTransactionHex = rawTxBytes.toHexString()
// 4. Convert the hex string to Data and assign to request's body
request.httpBody = rawTransactionHex.data(using: .utf8)
// 5. Send the request
let task = URLSession.shared.dataTask(with: request) { data, response, error in
// Handle client-side error
if let error = error {
print("Error: \(error)")
return
}
// Check server response
guard let httpResponse = response as? HTTPURLResponse else {
print("No response from server.")
return
}
// Print status for debugging
print("Status code: \(httpResponse.statusCode)")
// Handle response data
if let data = data, let responseString = String(data: data, encoding: .utf8) {
// The API typically returns the transaction ID if successful,
// or an error message otherwise.
print("Response body: \(responseString)")
} else {
print("No data in response.")
}
}
task.resume()
}