forked from rink1969/ckb-contract-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightning.rb
370 lines (284 loc) · 9.82 KB
/
lightning.rb
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
gem 'ckb-sdk-ruby'
require 'ckb'
include CKB
include Types
puts "0. prepare"
puts "transfer capacity to alice and bob"
api = API.new
god = Wallet.from_hex(api, "0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3")
bob = Wallet.from_hex(api, "0xa272f08f25809112aa8a8c42967418d8404e80f4313a8ae928c15beee3482012")
alice = Wallet.from_hex(api, "0xefc3957bc1d1ee67aaea83e54d7bdfc8069d0eba52e7a247bcdcb8b2d9705601")
alice2 = Wallet.from_hex(api, "0xdf51aa81f4e19c1c57ca764a7810400e68d899f5aebf8e46d89401a7ce568c7e")
alice3 = Wallet.from_hex(api, "0x70972471656087d365955112fa5f795be5c28ad342de64a79ac2530ce302cb7f")
bob2 = Wallet.from_hex(api, "0x2c1365696e17e37d277d44f6cef59eb9bf588974c103937ea52679a28702118c")
bob3 = Wallet.from_hex(api, "0x8665b2764dd353e3f63c86f261faf7b30a6a91ba6bbf546a2d6c4249a293f637")
puts god.send_capacity(bob.address, 300 * 10 ** 8)
while bob.get_balance < 300 * 10 ** 8
sleep 1
end
sleep 10
puts god.send_capacity(alice.address, 300 * 10 ** 8)
while alice.get_balance < 300 * 10 ** 8
sleep 1
end
sleep 10
puts "1. unsigned funding transaction"
god_c = CKB::Contract.new(god)
puts "1.1 deploy two-of-two multi signatures script"
puts " and revocable_maturity script"
god_c.deploy_contract("./two_of_two", :tot, [])
sleep 10
god_c.deploy_contract("./revocable_maturity", :revocable_maturity, [])
sleep 5
puts "1.2 create unsigned finding transaction"
capacity = 300 * 10 ** 8
inputs_from_alice = alice.send :gather_inputs, capacity, MIN_CELL_CAPACITY
inputs_from_bob = bob.send :gather_inputs, capacity, MIN_CELL_CAPACITY
tot_outpoint = Types::OutPoint.new(
cell: Types::CellOutPoint.new(
tx_hash: god_c.contracts[:tot][:tx_hash],
index: 0
)
)
revocable_maturity_outpoint = Types::OutPoint.new(
cell: Types::CellOutPoint.new(
tx_hash: god_c.contracts[:revocable_maturity][:tx_hash],
index: 0
)
)
outputs = [
Types::Output.new(
capacity: capacity * 2,
data: "0x",
lock: Types::Script.new(
args: [alice.blake160, bob.blake160],
code_hash: god_c.contracts[:tot][:binary_hash]
)
)
]
unsigned_funding_transaction = Transaction.new(
version: 0,
deps: [api.system_script_out_point],
inputs: inputs_from_alice.inputs + inputs_from_bob.inputs,
outputs: outputs
)
funding_tx_hash = api.compute_transaction_hash(unsigned_funding_transaction)
sleep 2
# P18: Figure 4
puts "2 create commit transaction (children transaction)"
puts "2.1 create commit transaction and sign"
puts "alice create commitment tx 1a (C1a)"
puts " Only alice can broadcast"
puts " Outputs:"
puts " 0: RSMC alice&bob 300 ckb"
puts " 1: no locktime for bob 300 ckb"
c1a_input = Types::Input.new(
previous_output: Types::OutPoint.new(cell: Types::CellOutPoint.new(tx_hash: funding_tx_hash, index: 0)),
args: [],
since: "0"
)
c1a_output_0 = Types::Output.new(
capacity: capacity,
data: "0x",
lock: Types::Script.new(
args: [alice2.blake160, bob.blake160, "0x"],
code_hash: god_c.contracts[:revocable_maturity][:binary_hash]
)
)
c1a_output_1 = Types::Output.new(
capacity: capacity,
data: "0x",
lock: bob.lock
)
c1a = Transaction.new(
version: 0,
deps: [api.system_script_out_point, tot_outpoint],
inputs: [c1a_input],
outputs: [c1a_output_0, c1a_output_1]
)
c1a_tx_hash = api.compute_transaction_hash(c1a)
c1a_alice_witnesses = c1a.sign(alice.key, c1a_tx_hash).witnesses
puts "bob create commitment tx 1b (C1b)"
puts " Only bob can broadcast"
puts " Outputs:"
puts " 0: RSMC alice&bob 300 ckb"
puts " 1: no locktime for alice 300 ckb"
c1b_input = Types::Input.new(
previous_output: Types::OutPoint.new(cell: Types::CellOutPoint.new(tx_hash: funding_tx_hash, index: 0)),
args: [],
since: "0"
)
c1b_output_0 = Types::Output.new(
capacity: capacity,
data: "0x",
lock: Types::Script.new(
args: [alice.blake160, bob2.blake160, "0x01"],
code_hash: god_c.contracts[:tot][:binary_hash]
)
)
c1b_output_1 = Types::Output.new(
capacity: capacity,
data: "0x",
lock: alice.lock
)
c1b = Transaction.new(
version: 0,
deps: [api.system_script_out_point, tot_outpoint],
inputs: [c1b_input],
outputs: [c1b_output_0, c1b_output_1]
)
c1b_tx_hash = api.compute_transaction_hash(c1b)
c1b_bob_witnesses = c1b.sign(bob.key, c1b_tx_hash).witnesses
puts "2.2 Exchange the signatures for the children"
c1a_bob_witnesses = c1a.sign(bob.key, c1a_tx_hash).witnesses
c1a_witnesses = []
c1a_alice_witnesses.each_with_index do |alice_witness, index|
c1a_witnesses.push(Witness.new(data: alice_witness.data + c1a_bob_witnesses[index].data))
end
c1b_alice_witnesses = c1b.sign(alice.key, c1b_tx_hash).witnesses
c1b_witnesses = []
c1b_bob_witnesses.each_with_index do |bob_witness, index|
c1b_witnesses.push(Witness.new(data: c1b_alice_witnesses[index].data + bob_witness.data))
end
puts "2.3 Sign the parent (Funding transaction)"
alice_funding_witnesses = unsigned_funding_transaction.sign(alice.key, funding_tx_hash).witnesses
bob_funding_witnesses = unsigned_funding_transaction.sign(bob.key, funding_tx_hash).witnesses
puts "2.4 Exchange the signatures for the parent"
funding_witnesses = alice_funding_witnesses[0..(inputs_from_alice.inputs.size - 1)] + bob_funding_witnesses[(inputs_from_alice.inputs.size)..-1]
unsigned_funding_transaction.witnesses = funding_witnesses
puts "2.5 Broadcast the parent on the blockchain"
puts api.send_transaction(unsigned_funding_transaction)
sleep 8
puts api.get_transaction(funding_tx_hash).tx_status.to_h
puts "3 new commit transaction"
puts "3.1 create commit transaction and sign"
puts "alice create commitment tx 2a (C2a)"
puts " Only alice can broadcast"
puts " Outputs:"
puts " 0: RSMC alice&bob 400 ckb"
puts " 1: no locktime for bob 200 ckb"
c2a_input = Types::Input.new(
previous_output: Types::OutPoint.new(cell: Types::CellOutPoint.new(tx_hash: funding_tx_hash, index: 0)),
args: [],
since: "0"
)
c2a_output_0 = Types::Output.new(
capacity: 400 * 10 ** 8,
data: "0x",
lock: Types::Script.new(
args: [alice3.blake160, bob.blake160, "0x"],
code_hash: god_c.contracts[:tot][:binary_hash]
)
)
c2a_output_1 = Types::Output.new(
capacity: 200 * 10 ** 8,
data: "0x",
lock: bob.lock
)
c2a = Transaction.new(
version: 0,
deps: [api.system_script_out_point, tot_outpoint],
inputs: [c2a_input],
outputs: [c2a_output_0, c2a_output_1]
)
c2a_tx_hash = api.compute_transaction_hash(c2a)
c2a_alice_witnesses = c2a.sign(alice.key, c2a_tx_hash).witnesses
puts "bob create commitment tx 2b (C2b)"
puts " Only bob can broadcast"
puts " Outputs:"
puts " 0: RSMC alice&bob 200 ckb"
puts " 1: no locktime for alice 400 ckb"
c2b_input = Types::Input.new(
previous_output: Types::OutPoint.new(cell: Types::CellOutPoint.new(tx_hash: funding_tx_hash, index: 0)),
args: [],
since: "0"
)
c2b_output_0 = Types::Output.new(
capacity: 200 * 10 ** 8,
data: "0x",
lock: Types::Script.new(
args: [alice.blake160, bob3.blake160, "0x01"],
code_hash: god_c.contracts[:tot][:binary_hash]
)
)
c2b_output_1 = Types::Output.new(
capacity: 400 * 10 ** 8,
data: "0x",
lock: alice.lock
)
c2b = Transaction.new(
version: 0,
deps: [api.system_script_out_point, tot_outpoint],
inputs: [c2b_input],
outputs: [c2b_output_0, c2b_output_1]
)
c2b_tx_hash = api.compute_transaction_hash(c2b)
c2b_bob_witnesses = c2b.sign(bob.key, c2b_tx_hash).witnesses
puts "3.2 Exchange the signatures for the children"
c2a_bob_witnesses = c2a.sign(bob.key, c2a_tx_hash).witnesses
c2a_witnesses = []
c2a_alice_witnesses.each_with_index do |alice_witness, index|
c2a_witnesses.push(Witness.new(data: alice_witness.data + c2a_bob_witnesses[index].data))
end
c2b_alice_witnesses = c2b.sign(alice.key, c2b_tx_hash).witnesses
c2b_witnesses = []
c2b_bob_witnesses.each_with_index do |bob_witness, index|
c2b_witnesses.push(Witness.new(data: c2b_alice_witnesses[index].data + bob_witness.data))
end
puts "3.3 Breach Remedy Transaction for parent commit transaction"
puts " alice discloses the alice2 private keys to the counterparty"
puts " bob discloses the bob2 private keys to the counterparty"
br2a_input = Types::Input.new(
previous_output: Types::OutPoint.new(cell: Types::CellOutPoint.new(tx_hash: c1a_tx_hash, index: 0)),
args: [],
since: "0"
)
br2a_output = Types::Output.new(
capacity: 300 * 10 ** 8,
data: "0x",
lock: Types::Script.new(
args: [bob.blake160],
code_hash: api.system_script_code_hash
)
)
br2a = Transaction.new(
version: 0,
deps: [api.system_script_out_point, revocable_maturity_outpoint],
inputs: [br2a_input],
outputs: [br2a_output]
)
br2a_tx_hash = api.compute_transaction_hash(br2a)
br2a_alice_witnesses = br2a.sign(alice2.key, br2a_tx_hash).witnesses
br2b_input = Types::Input.new(
previous_output: Types::OutPoint.new(cell: Types::CellOutPoint.new(tx_hash: c1b_tx_hash, index: 0)),
args: [],
since: "0"
)
br2b_output = Types::Output.new(
capacity: 300 * 10 ** 8,
data: "0x",
lock: Types::Script.new(
args: [alice.blake160],
code_hash: api.system_script_code_hash
)
)
br2b = Transaction.new(
version: 0,
deps: [api.system_script_out_point, revocable_maturity_outpoint],
inputs: [br2b_input],
outputs: [br2b_output]
)
br2b_tx_hash = api.compute_transaction_hash(br2b)
br2b_bob_witnesses = br2b.sign(bob2.key, br2b_tx_hash).witnesses
puts "3.4 Exchange the signatures for the Breach Remedy Transaction"
br2a_bob_witnesses = br2a.sign(bob.key, br2a_tx_hash).witnesses
br2b_alice_witnesses = br2b.sign(alice.key, br2b_tx_hash).witnesses
br2a_witnesses = []
br2a_alice_witnesses.each_with_index do |alice_witness, index|
br2a_witnesses.push(Witness.new(data: alice_witness.data + br2a_bob_witnesses[index].data))
end
br2b_witnesses = []
br2b_bob_witnesses.each_with_index do |bob_witness, index|
br2b_witnesses.push(Witness.new(data: br2b_alice_witnesses[index].data + bob_witness.data))
end
puts "Bob&Alice should destroy all old Commitment Transactions"