mirror of
				https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line.git
				synced 2025-10-31 02:17:24 +00:00 
			
		
		
		
	Create 17_6_getinfo.playground
This commit is contained in:
		
							parent
							
								
									8249ac8f8c
								
							
						
					
					
						commit
						606dc8b98a
					
				
							
								
								
									
										95
									
								
								src/17_6_getinfo.playground
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								src/17_6_getinfo.playground
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,95 @@ | |||||||
|  | import Foundation | ||||||
|  | 
 | ||||||
|  | func makeCommand(method: String, param: Any, completionHandler: @escaping (Any?) -> Void) -> Void { | ||||||
|  | 
 | ||||||
|  | // 1. Build a URL | ||||||
|  |      | ||||||
|  |     let testnetRpcPort = "18332" | ||||||
|  |     let nodeIp = "127.0.0.1:\(testnetRpcPort)" | ||||||
|  |     let rpcusername = "oIjA53JC2u" | ||||||
|  |     let rpcpassword = "ebVCeSyyM0LurvgQyi0exWTqm4oU0rZU" | ||||||
|  |     let walletName = "" | ||||||
|  |     let walletUrl = "http://\(rpcusername):\(rpcpassword)@\(nodeIp)/\(walletName)" | ||||||
|  | 
 | ||||||
|  |     let url = URL(string: walletUrl) | ||||||
|  | 
 | ||||||
|  | // 2. Build a Request | ||||||
|  |      | ||||||
|  |     var request = URLRequest(url: url!) | ||||||
|  |     request.httpMethod = "POST" | ||||||
|  |     request.setValue("text/plain", forHTTPHeaderField: "Content-Type") | ||||||
|  |     request.httpBody = "{\"jsonrpc\":\"1.0\",\"id\":\"curltest\",\"method\":\"\(method)\",\"params\":[\(param)]}".data(using: .utf8) | ||||||
|  | 
 | ||||||
|  | // 3. Build a Session | ||||||
|  |      | ||||||
|  |     let session = URLSession(configuration: .default) | ||||||
|  |     let task = session.dataTask(with: request as URLRequest) { data, response, error in | ||||||
|  | 
 | ||||||
|  |         do { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |             if error != nil { | ||||||
|  | 
 | ||||||
|  |                     //Handle the error | ||||||
|  |                      | ||||||
|  |             } else { | ||||||
|  |                      | ||||||
|  |                 if let urlContent = data { | ||||||
|  |                          | ||||||
|  |                     do { | ||||||
|  |                              | ||||||
|  |                         let json = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableLeaves) as! NSDictionary | ||||||
|  |                         if let errorCheck = json["error"] as? NSDictionary { | ||||||
|  |                                                                  | ||||||
|  |                             if let errorMessage = errorCheck["message"] as? String { | ||||||
|  | 
 | ||||||
|  |                                 print("FAILED") | ||||||
|  |                                 print(errorMessage) | ||||||
|  | 
 | ||||||
|  |                             } | ||||||
|  |                                  | ||||||
|  |                         } else { | ||||||
|  |                                  | ||||||
|  |                             let result = json["result"] | ||||||
|  |                             completionHandler(result) | ||||||
|  |                              | ||||||
|  |                         } | ||||||
|  |                              | ||||||
|  |                     } catch { | ||||||
|  |                              | ||||||
|  |                             //Handle error here | ||||||
|  |                              | ||||||
|  |                     } | ||||||
|  |                          | ||||||
|  |                 } | ||||||
|  |                      | ||||||
|  |             } | ||||||
|  |                  | ||||||
|  |         } | ||||||
|  |              | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | //    return response | ||||||
|  |     task.resume() | ||||||
|  |      | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Test getblockchaininfo | ||||||
|  | 
 | ||||||
|  | var method = "getblockchaininfo" | ||||||
|  | var param = "" | ||||||
|  | 
 | ||||||
|  | makeCommand(method: method,param: param) { result in | ||||||
|  |      | ||||||
|  |     print(result!) | ||||||
|  |     let blockinfo = result as! NSDictionary | ||||||
|  |     let block = blockinfo["blocks"] as! NSNumber | ||||||
|  | 
 | ||||||
|  |     // Test getblockchash | ||||||
|  |      | ||||||
|  |     method = "getblockhash" | ||||||
|  |     makeCommand(method: method,param: block) { result in | ||||||
|  |         print("Blockhash for \(block) is \(result!)") | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user