| 
									
										
										
										
											2018-10-09 15:00:30 -06:00
										 |  |  | -- M-N Multisig. Pass in a table "{m=M, n=N, tokens=T}" where M is the number | 
					
						
							|  |  |  | -- of signatures required, and N is a list of the pubkeys identifying | 
					
						
							|  |  |  | -- those signatures. Once M of len(N) signatures are collected, tokens T | 
					
						
							|  |  |  | -- are subtracted from account 1 and given to account 4. Note that unlike | 
					
						
							|  |  |  | -- Rust, Lua is one-based and that account 1 is the first account. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function find(t, x) | 
					
						
							|  |  |  |     for i, v in pairs(t) do | 
					
						
							|  |  |  |         if v == x then | 
					
						
							|  |  |  |             return i | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 20:29:05 -06:00
										 |  |  | function deserialize(bytes) | 
					
						
							|  |  |  |     return load("return" .. bytes)() | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | local from_account, | 
					
						
							|  |  |  |       serialize_account, | 
					
						
							|  |  |  |       state_account, | 
					
						
							|  |  |  |       to_account = table.unpack(accounts) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | local serialize = load(serialize_account.userdata)().serialize | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if #state_account.userdata == 0 then | 
					
						
							|  |  |  |     local cfg = deserialize(data) | 
					
						
							|  |  |  |     state_account.userdata = serialize(cfg, nil, "s") | 
					
						
							| 
									
										
										
										
											2018-10-09 15:00:30 -06:00
										 |  |  |     return | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 20:29:05 -06:00
										 |  |  | local cfg = deserialize(state_account.userdata) | 
					
						
							|  |  |  | local key = deserialize(data) | 
					
						
							| 
									
										
										
										
											2018-10-09 15:00:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | local i = find(cfg.n, key) | 
					
						
							|  |  |  | if i == nil then | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | table.remove(cfg.n, i) | 
					
						
							|  |  |  | cfg.m = cfg.m - 1 | 
					
						
							| 
									
										
										
										
											2018-10-09 20:29:05 -06:00
										 |  |  | state_account.userdata = serialize(cfg, nil, "s") | 
					
						
							| 
									
										
										
										
											2018-10-09 15:00:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | if cfg.m == 0 then | 
					
						
							| 
									
										
										
										
											2018-10-09 20:29:05 -06:00
										 |  |  |     from_account.tokens = from_account.tokens - cfg.tokens | 
					
						
							|  |  |  |     to_account.tokens = to_account.tokens + cfg.tokens | 
					
						
							| 
									
										
										
										
											2018-10-09 15:00:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     -- End of game. | 
					
						
							| 
									
										
										
										
											2018-10-09 20:29:05 -06:00
										 |  |  |     state_account.tokens = 0 | 
					
						
							| 
									
										
										
										
											2018-10-09 15:00:30 -06:00
										 |  |  | end |