Skip to content

Commit 4d95d28

Browse files
committed
update blockchain observer utilities
1 parent 2a7587f commit 4d95d28

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

xcode/DirectBindingsApp/DirectBindingsApp/app-batteries/BlockchainObserver.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,13 @@ class BlockchainObserver {
331331
assert(blockHeader.count == 80)
332332
return blockHeader
333333
}
334+
335+
public func getTransaction(hash: String) async throws -> [UInt8] {
336+
let response = try await self.callRpcMethod(method: "getrawtransaction", params: [hash])
337+
let txHex = response["result"] as! String
338+
let transaction = hexStringToBytes(hexString: txHex)!
339+
return transaction
340+
}
334341

335342
/**
336343
Get chain details such as the chaintip hash, the active soft forks, etc.
@@ -353,6 +360,21 @@ class BlockchainObserver {
353360
let result = response["result"] as! String
354361
return result
355362
}
363+
364+
public func submitTransaction(transaction: [UInt8]) async throws -> String {
365+
let txHex = bytesToHexString(bytes: transaction)
366+
let response = try await self.callRpcMethod(method: "sendrawtransaction", params: [txHex])
367+
// returns the txid
368+
let result = response["result"] as! String
369+
return result
370+
}
371+
372+
public func decodeScript(script: [UInt8]) async throws -> [String: Any] {
373+
let scriptHex = bytesToHexString(bytes: script)
374+
let response = try await self.callRpcMethod(method: "decodescript", params: [scriptHex])
375+
let result = response["result"] as! [String: Any]
376+
return result
377+
}
356378

357379
}
358380

xcode/DirectBindingsApp/DirectBindingsAppTests/test-batteries/bitcoin/BitcoinTests.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,20 @@ public class BitcoinTests: XCTestCase {
114114
let genesisBinary = try await rpcInterface.getBlockBinary(hash: genesisHash)
115115
let genesisHeader = try await rpcInterface.getBlockHeader(hash: genesisHash)
116116

117+
let genesisCoinbaseTxHash = genesisDetails.tx.first!
118+
let genesisCoinbaseTx = try await rpcInterface.getTransaction(hash: genesisCoinbaseTxHash)
119+
120+
121+
// try? await rpcInterface.submitTransaction(transaction: modifiedCoinbaseTx)
122+
117123
// let address = try await rpcInterface.generateAddress()
118124
// let details = try await rpcInterface.mineBlocks(number: 1, coinbaseDestinationAddress: address)
119125
let helpDetails = try await rpcInterface.getHelp()
120126
print(helpDetails)
127+
128+
let exampleFundingTx: [UInt8] = [1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 0, 255, 255, 255, 255, 1, 160, 134, 1, 0, 0, 0, 0, 0, 34, 0, 32, 15, 45, 153, 123, 98, 37, 242, 142, 113, 51, 126, 45, 206, 175, 66, 247, 173, 2, 141, 2, 27, 84, 38, 188, 34, 74, 82, 164, 28, 229, 39, 139, 1, 1, 1, 0, 0, 0, 0]
129+
// try? await rpcInterface.submitTransaction(transaction: exampleFundingTx)
130+
121131

122132
class Listener: BlockchainListener {
123133
var initializationComplete = false
@@ -143,7 +153,10 @@ public class BitcoinTests: XCTestCase {
143153
listener.initializationComplete = true
144154

145155
let testAddress = try await rpcInterface.generateAddress()
146-
try await rpcInterface.mineBlocks(number: 1, coinbaseDestinationAddress: testAddress)
156+
let exampleOutputScript: [UInt8] = [0, 32, 200, 194, 75, 55, 227, 33, 251, 71, 196, 33, 177, 196, 155, 145, 17, 78, 244, 226, 155, 141, 216, 230, 180, 183, 149, 172, 116, 249, 56, 6, 118, 255]
157+
let scriptInfo = try await rpcInterface.decodeScript(script: exampleOutputScript)
158+
let outputAddress = (scriptInfo["addresses"] as! [String]).first!
159+
try await rpcInterface.mineBlocks(number: 1, coinbaseDestinationAddress: outputAddress)
147160
// sleep for six seconds
148161
try await Task.sleep(nanoseconds: 6_000_000_000)
149162
XCTAssertEqual(listener.newBlockDetected, true)

0 commit comments

Comments
 (0)