2023-10-27 14:13:44 -04:00
|
|
|
import XCTest
|
|
|
|
@testable import BitcoinDevKit
|
|
|
|
|
2024-05-10 10:51:07 -04:00
|
|
|
let SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
|
|
|
|
let TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
|
|
|
|
|
2023-10-27 14:13:44 -04:00
|
|
|
final class LiveWalletTests: XCTestCase {
|
2024-02-28 13:34:23 -06:00
|
|
|
var dbFilePath: URL!
|
|
|
|
|
|
|
|
override func setUpWithError() throws {
|
|
|
|
super.setUp()
|
2024-02-08 16:02:15 -05:00
|
|
|
let fileManager = FileManager.default
|
|
|
|
let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
|
2024-02-28 13:34:23 -06:00
|
|
|
let uniqueDbFileName = "bdk_persistence_\(UUID().uuidString).db"
|
|
|
|
dbFilePath = documentDirectory.appendingPathComponent(uniqueDbFileName)
|
2024-02-08 16:02:15 -05:00
|
|
|
|
|
|
|
if fileManager.fileExists(atPath: dbFilePath.path) {
|
|
|
|
try fileManager.removeItem(at: dbFilePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:34:23 -06:00
|
|
|
override func tearDownWithError() throws {
|
|
|
|
let fileManager = FileManager.default
|
|
|
|
if fileManager.fileExists(atPath: dbFilePath.path) {
|
|
|
|
try fileManager.removeItem(at: dbFilePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 14:13:44 -04:00
|
|
|
func testSyncedBalance() throws {
|
|
|
|
let descriptor = try Descriptor(
|
2023-11-17 13:25:33 -05:00
|
|
|
descriptor: "wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)",
|
2024-05-10 10:51:07 -04:00
|
|
|
network: Network.signet
|
2023-10-27 14:13:44 -04:00
|
|
|
)
|
2024-02-08 16:02:15 -05:00
|
|
|
let wallet = try Wallet(
|
|
|
|
descriptor: descriptor,
|
|
|
|
changeDescriptor: nil,
|
2024-02-28 13:34:23 -06:00
|
|
|
persistenceBackendPath: dbFilePath.path,
|
2024-05-10 10:51:07 -04:00
|
|
|
network: .signet
|
2024-02-08 16:02:15 -05:00
|
|
|
)
|
2024-05-10 10:51:07 -04:00
|
|
|
let esploraClient = EsploraClient(url: SIGNET_ESPLORA_URL)
|
2024-05-05 20:08:33 -04:00
|
|
|
let fullScanRequest: FullScanRequest = wallet.startFullScan()
|
2024-02-08 16:02:15 -05:00
|
|
|
let update = try esploraClient.fullScan(
|
2024-05-05 20:08:33 -04:00
|
|
|
fullScanRequest: fullScanRequest,
|
2024-02-08 16:02:15 -05:00
|
|
|
stopGap: 10,
|
|
|
|
parallelRequests: 1
|
|
|
|
)
|
|
|
|
try wallet.applyUpdate(update: update)
|
2024-05-08 11:33:35 -04:00
|
|
|
try wallet.commit()
|
2024-02-08 16:02:15 -05:00
|
|
|
|
|
|
|
XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0))
|
|
|
|
|
|
|
|
print("Transactions count: \(wallet.transactions().count)")
|
|
|
|
let transactions = wallet.transactions().prefix(3)
|
|
|
|
for tx in transactions {
|
2024-04-06 21:25:43 -05:00
|
|
|
let sentAndReceived = wallet.sentAndReceived(tx: tx.transaction)
|
|
|
|
print("Transaction: \(tx.transaction.txid())")
|
2024-02-08 16:02:15 -05:00
|
|
|
print("Sent \(sentAndReceived.sent)")
|
|
|
|
print("Received \(sentAndReceived.received)")
|
|
|
|
}
|
2023-10-27 14:13:44 -04:00
|
|
|
}
|
2023-11-17 13:25:33 -05:00
|
|
|
|
|
|
|
func testBroadcastTransaction() throws {
|
|
|
|
let descriptor = try Descriptor(
|
|
|
|
descriptor: "wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)",
|
2024-05-10 10:51:07 -04:00
|
|
|
network: Network.signet
|
2023-11-17 13:25:33 -05:00
|
|
|
)
|
2024-02-08 16:02:15 -05:00
|
|
|
let wallet = try Wallet(
|
|
|
|
descriptor: descriptor,
|
|
|
|
changeDescriptor: nil,
|
2024-02-28 13:34:23 -06:00
|
|
|
persistenceBackendPath: dbFilePath.path,
|
2024-05-10 10:51:07 -04:00
|
|
|
network: .signet
|
2024-02-08 16:02:15 -05:00
|
|
|
)
|
2024-05-10 10:51:07 -04:00
|
|
|
let esploraClient = EsploraClient(url: SIGNET_ESPLORA_URL)
|
2024-05-05 20:08:33 -04:00
|
|
|
let fullScanRequest: FullScanRequest = wallet.startFullScan()
|
2024-02-08 16:02:15 -05:00
|
|
|
let update = try esploraClient.fullScan(
|
2024-05-05 20:08:33 -04:00
|
|
|
fullScanRequest: fullScanRequest,
|
2024-02-08 16:02:15 -05:00
|
|
|
stopGap: 10,
|
|
|
|
parallelRequests: 1
|
|
|
|
)
|
|
|
|
try wallet.applyUpdate(update: update)
|
2024-05-08 11:33:35 -04:00
|
|
|
try wallet.commit()
|
2024-02-08 16:02:15 -05:00
|
|
|
|
|
|
|
XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0), "Wallet must have positive balance, please add funds")
|
|
|
|
|
|
|
|
print("Balance: \(wallet.getBalance().total)")
|
|
|
|
|
2024-05-10 10:51:07 -04:00
|
|
|
let recipient: Address = try Address(address: "tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", network: .signet)
|
2024-04-17 11:34:25 -04:00
|
|
|
let psbt: Psbt = try
|
2024-02-08 16:02:15 -05:00
|
|
|
TxBuilder()
|
|
|
|
.addRecipient(script: recipient.scriptPubkey(), amount: 4200)
|
2024-04-01 14:56:19 -04:00
|
|
|
.feeRate(feeRate: FeeRate.fromSatPerVb(satPerVb: 2))
|
2024-02-08 16:02:15 -05:00
|
|
|
.finish(wallet: wallet)
|
|
|
|
|
|
|
|
print(psbt.serialize())
|
|
|
|
XCTAssertTrue(psbt.serialize().hasPrefix("cHNi"), "PSBT should start with cHNI")
|
|
|
|
|
|
|
|
let walletDidSign: Bool = try wallet.sign(psbt: psbt)
|
|
|
|
XCTAssertTrue(walletDidSign, "Wallet did not sign transaction")
|
|
|
|
|
2024-04-17 11:34:25 -04:00
|
|
|
let tx: Transaction = try! psbt.extractTx()
|
2024-02-08 16:02:15 -05:00
|
|
|
print(tx.txid())
|
|
|
|
let fee: UInt64 = try wallet.calculateFee(tx: tx)
|
|
|
|
print("Transaction Fee: \(fee)")
|
|
|
|
let feeRate: FeeRate = try wallet.calculateFeeRate(tx: tx)
|
2024-04-01 14:56:19 -04:00
|
|
|
print("Transaction Fee Rate: \(feeRate.toSatPerVbCeil()) sat/vB")
|
2024-02-08 16:02:15 -05:00
|
|
|
|
|
|
|
try esploraClient.broadcast(transaction: tx)
|
2023-11-17 13:25:33 -05:00
|
|
|
}
|
2023-10-27 14:13:44 -04:00
|
|
|
}
|