-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18_6_sendtx.playground
137 lines (81 loc) · 3.87 KB
/
18_6_sendtx.playground
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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()
}
// 0. Generate a Receiving Address
var method = "getnewaddress"
var param = "\"learning-bitcoin\", \"legacy\""
makeCommand(method: method,param: param) { result in
let address = result as! NSString
// 1. Find an Unspent UTXO
method = "listunspent"
param = ""
makeCommand(method: method,param: param) { result in
let unspent = result! as! NSArray
let utxo = unspent[0] as! NSDictionary
let txid = utxo["txid"] as! NSString
let vout = utxo["vout"] as! NSInteger
let amount = utxo["amount"] as! NSNumber
let new_amount = amount.floatValue - 0.0001
// 2. Create a Raw Transaction
method = "createrawtransaction"
param="[ { \"txid\": \"\(txid)\", \"vout\": \(vout) } ], { \"\(address)\": \(new_amount)}"
makeCommand(method: method,param: param) { result in
let hex = result as! NSString
// 3. Sign the Raw Transaction
method = "signrawtransactionwithwallet"
param = "\"\(hex)\""
makeCommand(method: method,param: param) { result in
let signedhexinfo = result as! NSDictionary
let signedhex = signedhexinfo["hex"] as! NSString
// 4. Send the Raw Transaction
method = "sendrawtransaction"
param = "\"\(signedhex)\""
makeCommand(method: method,param: param) { result in
let new_txid = result as! NSString
print("TXID: \(new_txid)")
}
}
}
}
}