-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
2059 lines (1756 loc) · 66.3 KB
/
app.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'base64'
require 'sinatra'
require 'chartkick'
require 'cardano_wallet'
require 'sys/proctable'
require 'json2table'
require_relative 'helpers/app_helpers'
require_relative 'helpers/discovery_helpers'
ENV['ICARUS_PORT'] ||= '4444'
ENV['ICARUS_BIND_ADDR'] ||= '0.0.0.0'
set :port, ENV['ICARUS_PORT'].to_i
set :bind, ENV['ICARUS_BIND_ADDR']
set :root, File.dirname(__FILE__)
# enable :sessions
use Rack::Session::Pool
helpers Helpers::App
helpers Helpers::Discovery
before do
@tx_max_count = 300 # max number of transactions to display by default on wallet page
@timeout = 3600
session[:opt] ||= {port: 8090, timeout: @timeout}
@cw ||= CardanoWallet.new session[:opt]
session[:opt] ||= @cw.opt
session[:platform] ||= os
# puts session[:opt]
end
get "/" do
erb :index
end
get "/connect" do
redirect "/"
end
post "/connect" do
begin
@cw = CardanoWallet.new({ port: params[:wallet_port].to_i,
protocol: params[:protocol],
cacert: params[:cacert],
pem: params[:pem],
timeout: @timeout })
rescue
session[:error] = "Failed to initialize CardanoWallet! (Hint: Make sure Cacert and Pem point to real files)."
end
session[:opt] = @cw.opt
session[:w_connected] = is_connected? @cw
redirect "/"
end
get "/discovery" do
wallet_servers = Sys::ProcTable.ps.select{|p| p.cmdline.include?("cardano-wallet") if p.cmdline}
erb :discovery, { :locals => { :wallet_servers => wallet_servers } }
end
# MISC
get "/get-acc-pub-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
wid = params[:wid]
erb :form_get_acc_public_key, { :locals => { :wallets => wallets,
:wid => wid,
:format => params[:format],
:acc_pub_key => nil
} }
end
post "/get-acc-pub-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
acc_pub_key = @cw.shelley.keys.get_acc_public_key(params[:wid], {format: params[:format]})
handle_api_err acc_pub_key, session
erb :form_get_acc_public_key, { :locals => { :wallets => wallets,
:wid => params[:wid],
:format => params[:format],
:acc_pub_key => acc_pub_key
} }
end
get "/create-acc-pub-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
params[:wid] ? wid = params[:wid] : wid = wallets.first['id']
erb :form_create_acc_public_key, { :locals => { :wallets => wallets,
:wid => wid,
:index => params[:index],
:purpose => params[:purpose],
:format => params[:format],
:acc_pub_key => nil
} }
end
post "/create-acc-pub-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
begin
payload = { "passphrase" => params[:pass],
"format" => params[:format]}
payload["purpose"] = params[:purpose] if params[:purpose] != ''
acc_pub_key = @cw.shelley.keys.create_acc_public_key(params[:wid],
params[:index],
payload)
rescue
session[:error] = "Make sure parameters are valid. "
redirect "/get-pub-key"
end
handle_api_err acc_pub_key, session
erb :form_create_acc_public_key, { :locals => { :wallets => wallets,
:wid => params[:wid],
:index => params[:index],
:purpose => params[:purpose],
:format => params[:format],
:acc_pub_key => acc_pub_key
} }
end
get "/get-pub-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
params[:wid] ? wid = params[:wid] : wid = wallets.first['id']
erb :form_get_public_key, { :locals => { :wallets => wallets,
:wid => wid,
:role => params[:role],
:index => params[:index],
:pub_key => nil,
:hash => nil
} }
end
post "/get-pub-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
hash = params[:hash] == 'true' ? { hash: true } : {}
begin
pub_key = @cw.shelley.keys.get_public_key(params[:wid],
params[:role],
params[:index],
hash)
rescue
session[:error] = "Make sure parameters are valid. "
redirect "/get-pub-key"
end
handle_api_err pub_key, session
erb :form_get_public_key, { :locals => { :wallets => wallets,
:wid => params[:wid],
:role => params[:role],
:index => params[:index],
:pub_key => pub_key,
:hash => params[:hash]
} }
end
get "/get-policy-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
params[:wid] ? wid = params[:wid] : wid = wallets.first['id']
erb :form_get_policy_key, { :locals => { :wallets => wallets,
:wid => wid,
:policy_key => nil,
:hash => nil } }
end
post "/get-policy-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
hash = params[:hash] == 'true' ? { hash: true } : {}
begin
policy_key = @cw.shelley.keys.get_policy_key(params[:wid], hash)
rescue
session[:error] = "Make sure parameters are valid. "
redirect "/get-policy-key"
end
handle_api_err policy_key, session
erb :form_get_policy_key, { :locals => { :wallets => wallets,
:wid => params[:wid],
:policy_key => policy_key,
:hash => params[:hash]
} }
end
get "/create-policy-id" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
params[:wid] ? wid = params[:wid] : wid = wallets.first['id']
erb :form_create_policy_id, { :locals => { :wallets => wallets,
:wid => wid,
:policy_script_template => nil,
:policy_id => nil } }
end
post "/create-policy-id" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
begin
policy_script_template = JSON.parse(params[:mint_policy_script].strip)
rescue
policy_script_template = params[:mint_policy_script]
end
policy_id = @cw.shelley.keys.create_policy_id(params[:wid], policy_script_template)
handle_api_err policy_id, session
erb :form_create_policy_id, { :locals => { :wallets => wallets,
:wid => params[:wid],
:policy_script_template => params[:mint_policy_script],
:policy_id => policy_id } }
end
get "/create-policy-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
params[:wid] ? wid = params[:wid] : wid = wallets.first['id']
erb :form_create_policy_key, { :locals => { :wallets => wallets,
:wid => wid,
:pass => 'Secure Passphrase',
:policy_key => nil,
:hash => nil } }
end
post "/create-policy-key" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
hash = params[:hash] == 'true' ? { hash: true } : {}
begin
policy_key = @cw.shelley.keys.create_policy_key(params[:wid], params[:pass], hash)
rescue
session[:error] = "Make sure parameters are valid. "
redirect "/create-policy-key"
end
handle_api_err policy_key, session
erb :form_create_policy_key, { :locals => { :wallets => wallets,
:wid => params[:wid],
:pass => params[:pass],
:policy_key => policy_key,
:hash => params[:hash] } }
end
get "/sign-metadata" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
params[:wid] ? wid = params[:wid] : wid = wallets.first['id']
erb :form_sign_metadata, { :locals => { :wallets => wallets,
:wid => wid,
:role => params[:role],
:index => params[:index],
:pass => params[:pass],
:metadata => params[:metadata],
:signed_metadata => nil
} }
end
post "/sign-metadata" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
m = parse_metadata(params[:metadata])
begin
signed_metadata = @cw.shelley.keys.sign_metadata(params[:wid],
params[:role],
params[:index],
params[:pass],
m
)
rescue
session[:error] = "Make sure parameters are valid. "
redirect "/sign-metadata"
end
handle_api_err signed_metadata, session
erb :form_sign_metadata, { :locals => { :wallets => wallets,
:wid => params[:wid],
:role => params[:role],
:index => params[:index],
:pass => params[:pass],
:metadata => m,
:signed_metadata => signed_metadata
} }
end
get "/settings" do
settings = @cw.misc.settings.get
erb :form_settings, { :locals => { :settings => settings,
:update_settings => nil,
:pool_strategy => nil } }
end
post "/settings" do
set = { "pool_metadata_source" => params['pool_strategy'] }
r = @cw.misc.settings.update(set)
handle_api_err r, session
settings = @cw.misc.settings.get
erb :form_settings, { :locals => { :settings => settings,
:update_settings => r,
:pool_strategy => params['pool_strategy'] } }
end
get "/smash-health-check" do
(params[:url] == '' or params[:url].nil?) ? q = {} : q = {url: params[:url]}
health_check = @cw.misc.utils.smash_health(q)
smash = @cw.misc.settings.get['pool_metadata_source']
erb :form_smash_health_check, { :locals => { :health_check => health_check, :smash => smash } }
end
get "/network-params" do
r = @cw.misc.network.parameters
handle_api_err r, session
erb :network_params, :locals => { :network_params => r }
end
get "/network-info" do
r = @cw.misc.network.information
handle_api_err r, session
erb :network_info, :locals => { :network_info => r }
end
get "/network-clock" do
r = @cw.misc.network.clock
handle_api_err r, session
erb :network_clock, :locals => { :network_clock => r }
end
get "/block-header" do
r = @cw.misc.node.block_header
handle_api_err r, session
erb :block_header, :locals => { :block_header => r }
end
get "/inspect-address" do
erb :form_inspect_address, { :locals => { :address_details => nil, :id => nil } }
end
get "/inspect-address-now" do
address_details = @cw.misc.utils.addresses(params[:addr_id])
erb :form_inspect_address, { :locals => { :address_details => address_details,
:id => params[:addr_id]} }
end
get "/construct-address" do
erb :form_construct_address, { :locals => { :script => nil, :address => nil } }
end
post "/construct-address" do
begin
script = JSON.parse params[:script]
address = @cw.misc.utils.post_address(script)
rescue
session[:error] = "Make sure the 'script' has correct JSON format."
end
erb :form_construct_address, { :locals => { :script => params[:script],
:address => address} }
end
get "/submit-external-tx" do
erb :form_tx_external, { :locals => { :tx => nil, :blob => nil } }
end
post "/submit-external-tx" do
serialized_tx = Base64.decode64(params['blob'].strip)
r = @cw.misc.proxy.submit_external_transaction(serialized_tx)
handle_api_err r, session
erb :form_tx_external, { :locals => { :tx => r, :blob => params['blob'].strip } }
end
# SHARED WALLETS
get "/shared-wallets-utxo" do
utxo = @cw.shared.wallets.utxo params[:wid]
utxo_snapshot = @cw.shared.wallets.utxo_snapshot params[:wid]
wal = @cw.shared.wallets.get params[:wid]
wallets = @cw.shared.wallets.list
erb :utxo_details, { :locals => { :wal => wal,
:utxo => utxo,
:utxo_snapshot => utxo_snapshot,
:wallets => wallets } }
end
get "/shared-get-acc-pub-key" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
wid = params[:wid]
erb :form_get_acc_public_key, { :locals => { :wallets => wallets,
:wid => wid,
:format => params[:format],
:acc_pub_key => nil
} }
end
post "/shared-get-acc-pub-key" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
acc_pub_key = @cw.shared.keys.get_acc_public_key(params[:wid], {format: params[:format]})
handle_api_err acc_pub_key, session
erb :form_get_acc_public_key, { :locals => { :wallets => wallets,
:wid => params[:wid],
:format => params[:format],
:acc_pub_key => acc_pub_key
} }
end
get "/shared-create-acc-pub-key" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
# params[:wid] ? wid = params[:wid] : wid = wallets.first['id']
wid = params[:wid]
erb :form_create_acc_public_key, { :locals => { :wallets => wallets,
:wid => wid,
:index => params[:index],
:format => params[:format],
:acc_pub_key => nil
} }
end
post "/shared-create-acc-pub-key" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
begin
payload = { "passphrase" => params[:pass],
"format" => params[:format]}
acc_pub_key = @cw.shared.keys.create_acc_public_key(params[:wid],
params[:index],
payload)
rescue
session[:error] = "Make sure parameters are valid. "
redirect "/shared-create-acc-pub-key"
end
handle_api_err acc_pub_key, session
erb :form_create_acc_public_key, { :locals => { :wallets => wallets,
:wid => params[:wid],
:index => params[:index],
:format => params[:format],
:acc_pub_key => acc_pub_key
} }
end
get "/shared-get-pub-key" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
# params[:wid] ? wid = params[:wid] : wid = wallets.first['id']
wid = params[:wid]
erb :form_get_public_key, { :locals => { :wallets => wallets,
:wid => wid,
:role => params[:role],
:index => params[:index],
:pub_key => nil,
:hash => nil
} }
end
post "/shared-get-pub-key" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
hash = params[:hash] == 'true' ? { hash: true } : {}
begin
pub_key = @cw.shared.keys.get_public_key(params[:wid],
params[:role],
params[:index],
hash)
rescue
session[:error] = "Make sure parameters are valid. "
redirect "/shared-get-pub-key"
end
handle_api_err pub_key, session
erb :form_get_public_key, { :locals => { :wallets => wallets,
:wid => params[:wid],
:role => params[:role],
:index => params[:index],
:pub_key => pub_key,
:hash => params[:hash]
} }
end
get "/shared-wallets/:wal_id/patch-payment" do
wal = @cw.shared.wallets.get params[:wal_id]
handle_api_err wal, session
erb :form_shared_wallet_patch, { :locals => { :wal => wal } }
end
post "/shared-wallets/:wal_id/patch-payment" do
up = @cw.shared.wallets.update_payment_script(params[:wal_id],
params[:cosigner],
params[:acc_pub_key])
handle_api_err up, session
redirect "/shared-wallets/#{params[:wal_id]}"
end
get "/shared-wallets/:wal_id/patch-delegation" do
wal = @cw.shared.wallets.get params[:wal_id]
handle_api_err wal, session
erb :form_shared_wallet_patch, { :locals => { :wal => wal} }
end
post "/shared-wallets/:wal_id/patch-delegation" do
up = @cw.shared.wallets.update_delegation_script(params[:wal_id],
params[:cosigner],
params[:acc_pub_key])
handle_api_err up, session
redirect "/shared-wallets/#{params[:wal_id]}"
end
get "/shared-wallets" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
erb :shared_wallets, { :locals => { :wallets => wallets } }
end
get "/shared-wallets/:wal_id" do
wal = @cw.shared.wallets.get params[:wal_id]
handle_api_err wal, session
txs = @cw.shared.transactions.list(params[:wal_id], {max_count: @tx_max_count})
handle_api_err txs, session
addrs = @cw.shared.addresses.list params[:wal_id]
handle_api_err addrs, session
erb :shared_wallet, { :locals => { :wal => wal, :txs => txs, :addrs => addrs} }
end
get "/shared-wallets-delete/:wal_id" do
@cw.shared.wallets.delete params[:wal_id]
redirect "/shared-wallets"
end
get "/shared-wallets-delete-all" do
erb :form_del_all
end
post "/shared-wallets-delete-all" do
s = @cw.shared.wallets.list
handle_api_err s, session
s.each do |wal|
r = @cw.shared.wallets.delete wal['id']
handle_api_err r, session
end
redirect "/shared-wallets"
end
get "/shared-wallets-create-from-pub-key" do
erb :form_create_wallet_from_pub_key
end
post "/shared-wallets-create-from-pub-key" do
name = params[:wal_name]
pub_key = params[:pub_key]
account_index = params[:account_index]
begin
payment_script_template = JSON.parse(params[:payment_script_template].strip)
rescue
session[:error] = "Make sure the 'payment_script_template' has correct JSON format."
redirect '/shared-wallets-create-from-pub-key'
end
payload = {name: name,
account_public_key: pub_key,
account_index: account_index,
payment_script_template: payment_script_template
}
if params[:delegation_script_template] != ''
begin
delegation_script_template = JSON.parse(params[:delegation_script_template].strip)
rescue
session[:error] = "Make sure the 'delegation_script_template' has correct JSON format."
redirect '/shared-wallets-create-from-pub-key'
end
payload[:delegation_script_template] = delegation_script_template
end
wal = @cw.shared.wallets.create(payload)
handle_api_err wal, session
redirect "/shared-wallets/#{wal['id']}"
end
get "/shared-wallets-create" do
# 24-word mnemonics
mnemonics = @cw.utils.mnemonic_sentence(24).join(' ')
erb :form_create_wallet, { :locals => { :mnemonics => mnemonics} }
end
post "/shared-wallets-create" do
m = prepare_mnemonics params[:mnemonics]
pass = params[:pass]
name = params[:wal_name]
account_index = params[:account_index]
begin
payment_script_template = JSON.parse(params[:payment_script_template].strip)
rescue
session[:error] = "Make sure the 'payment_script_template' has correct JSON format."
redirect '/shared-wallets-create'
end
payload = { mnemonic_sentence: m,
passphrase: pass,
name: name,
account_index: account_index,
payment_script_template: payment_script_template
}
if params[:delegation_script_template] != ''
begin
delegation_script_template = JSON.parse(params[:delegation_script_template].strip)
rescue
session[:error] = "Make sure the 'delegation_script_template' has correct JSON format."
redirect '/shared-wallets-create'
end
payload[:delegation_script_template] = delegation_script_template
end
wal = @cw.shared.wallets.create(payload)
handle_api_err wal, session
session[:wal] = wal
handle_api_err wal, session
redirect "/shared-wallets/#{wal['id']}"
end
# SHELLEY WALLETS
get "/wallets-get-assets" do
wallets = @cw.shelley.wallets.list
handle_api_err(wallets, session)
erb :form_assets_get, { :locals => { :wallets => wallets,
:asset_name => params[:asset_name],
:policy_id => params[:policy_id],
:asset => nil } }
end
post "/wallets-get-assets" do
wallets = @cw.shelley.wallets.list
handle_api_err(wallets, session)
asset_name = (params[:asset_name] == '') ? nil : params[:asset_name].strip
policy_id = (params[:policy_id] == '') ? nil : params[:policy_id].strip
begin
asset = @cw.shelley.assets.get(params[:wid], policy_id, asset_name)
rescue
session[:error] = "Make sure policy ID or asset name do not contain spaces."
end
erb :form_assets_get, { :locals => { :wallets => wallets,
:asset_name => asset_name,
:policy_id => policy_id,
:asset => asset } }
end
get "/wallets/coin-selection/delegation" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
erb :form_coin_selection_deleg_action, { :locals => { :deleg_action => nil,
:coin_selection => nil,
:wallets => wallets } }
end
post "/wallets/coin-selection/delegation" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
begin
deleg_action = JSON.parse params[:deleg_action]
coin_selection = @cw.shelley.coin_selections.random_deleg(params[:wid], deleg_action)
# handle_api_err coin_selection, session
rescue
session[:error] = "Make sure the 'action' has correct JSON format."
end
erb :form_coin_selection_deleg_action, { :locals => { :deleg_action => params[:deleg_action],
:coin_selection => coin_selection,
:wallets => wallets } }
end
get "/wallets/coin-selection/random" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
erb :form_coin_selection, { :locals => { :addr_amt => nil,
:assets => nil,
:withdrawal => nil,
:metadata => nil,
:coin_selection => nil,
:wallets => wallets } }
end
post "/wallets/coin-selection/random" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
payload = prepare_payload(params)
case params[:withdrawal]
when ''
w = nil
when 'self'
w = 'self'
else
w = params[:withdrawal].split
end
m = parse_metadata(params[:metadata])
coin_selection = @cw.shelley.coin_selections.random(params[:wid], payload, w, m)
erb :form_coin_selection, { :locals => { :addr_amt => params[:addr_amt],
:assets => params[:assets],
:withdrawal => params[:withdrawal],
:metadata => params[:metadata],
:coin_selection => coin_selection,
:wallets => wallets } }
end
get "/wallets" do
wallets = @cw.shelley.wallets.list
handle_api_err wallets, session
erb :wallets, { :locals => { :wallets => wallets } }
end
get "/wallets/:wal_id" do
wal = @cw.shelley.wallets.get params[:wal_id]
handle_api_err wal, session
txs = @cw.shelley.transactions.list(params[:wal_id], {max_count: @tx_max_count})
handle_api_err txs, session
addrs = @cw.shelley.addresses.list params[:wal_id]
handle_api_err addrs, session
erb :wallet, { :locals => { :wal => wal, :txs => txs, :addrs => addrs} }
end
get "/wallets/:wal_id/update" do
wal = @cw.shelley.wallets.get params[:wal_id]
handle_api_err wal, session
erb :form_wallet_update, { :locals => { :wal => wal } }
end
post "/wallets/:wal_id/update" do
r = @cw.shelley.wallets.update_metadata(params[:wal_id], {name: params[:name]})
handle_api_err r, session
redirect "/wallets/#{params[:wal_id]}"
end
get "/wallets/:wal_id/update-pass" do
wal = @cw.shelley.wallets.get params[:wal_id]
handle_api_err wal, session
erb :form_wallet_update_pass, { :locals => { :wal => wal } }
end
post "/wallets/:wal_id/update-pass" do
r = @cw.shelley.wallets.update_passphrase(params[:wal_id],
{old_passphrase: params[:old_pass],
new_passphrase: params[:new_pass]})
handle_api_err r, session
redirect "/wallets/#{params[:wal_id]}"
end
get "/wallets/:wal_id/update-pass-mnem" do
wal = @cw.shelley.wallets.get params[:wal_id]
handle_api_err wal, session
erb :form_wallet_update_pass_mnem, { :locals => { :wal => wal } }
end
post "/wallets/:wal_id/update-pass-mnem" do
m = prepare_mnemonics params[:mnemonic_sentence]
r = @cw.shelley.wallets.update_passphrase(params[:wal_id],
{mnemonic_sentence: m,
new_passphrase: params[:new_pass]})
handle_api_err r, session
redirect "/wallets/#{params[:wal_id]}"
end
get "/wallets-delete/:wal_id" do
@cw.shelley.wallets.delete params[:wal_id]
redirect "/wallets"
end
get "/wallets-create" do
# 24-word mnemonics
mnemonics = @cw.utils.mnemonic_sentence(24).join(' ')
erb :form_create_wallet, { :locals => { :mnemonics => mnemonics} }
end
post "/wallets-create" do
m = prepare_mnemonics params[:mnemonics]
pass = params[:pass]
name = params[:wal_name]
pool_gap = params[:pool_gap].to_i
wal = @cw.shelley.wallets.create({mnemonic_sentence: m,
passphrase: pass,
name: name,
address_pool_gap: pool_gap})
handle_api_err wal, session
session[:wal] = wal
handle_api_err wal, session
redirect "/wallets/#{wal['id']}"
end
get "/wallets-create-from-pub-key" do
# 15-word mnemonics
erb :form_create_wallet_from_pub_key
end
post "/wallets-create-from-pub-key" do
pub_key = params[:pub_key]
name = params[:wal_name]
pool_gap = params[:pool_gap].to_i
wal = @cw.shelley.wallets.create({name: name,
account_public_key: pub_key,
address_pool_gap: pool_gap,
})
handle_api_err wal, session
redirect "/wallets/#{wal['id']}"
end
get "/wallets-create-many-from-mnemonics" do
erb :form_create_many_from_mnemonics
end
post "/wallets-create-many-from-mnemonics" do
pass = params[:pass]
name = params[:wal_name]
pool_gap = params[:pool_gap].to_i
mnemonics_array = params[:mnemonics].split("\n").map{|a| a.strip}
mnemonics_array.each_with_index do |m,i|
wal = @cw.shelley.wallets.create({mnemonic_sentence: m.split,
passphrase: pass,
name: "#{name} #{i + 1}",
address_pool_gap: pool_gap})
handle_api_err wal, session
end
redirect "/wallets"
end
get "/wallets-create-many" do
erb :form_create_many
end
post "/wallets-create-many" do
pass = params[:pass]
name = params[:wal_name]
pool_gap = params[:pool_gap].to_i
how_many = params[:how_many].to_i
1.upto how_many do |i|
mnemonics = @cw.utils.mnemonic_sentence(params[:words_count].to_i)
wal = @cw.shelley.wallets.create({mnemonic_sentence: mnemonics,
passphrase: pass,
name: "#{name} #{i}",
address_pool_gap: pool_gap})
handle_api_err wal, session
end
redirect "/wallets"
end
get "/wallets-delete-all" do
erb :form_del_all
end
post "/wallets-delete-all" do
s = @cw.shelley.wallets.list
handle_api_err s, session
s.each do |wal|
r = @cw.shelley.wallets.delete wal['id']
handle_api_err r, session
end
redirect "/wallets"
end
get "/wallets-utxo" do
utxo = @cw.shelley.wallets.utxo params[:wid]
utxo_snapshot = @cw.shelley.wallets.utxo_snapshot params[:wid]
wal = @cw.shelley.wallets.get params[:wid]
wallets = @cw.shelley.wallets.list
erb :utxo_details, { :locals => { :wal => wal,
:utxo => utxo,
:utxo_snapshot => utxo_snapshot,
:wallets => wallets } }
end
get "/wallets-migrate" do
wallets = @cw.shelley.wallets.list
handle_api_err(wallets, session)
erb :form_migrate, { :locals => { :wallets => wallets} }
end
post "/wallets-migrate" do
wid_src = params[:wid_src]
addresses = params[:addresses].split("\n").map{|a| a.strip}
pass = params[:pass]
r = @cw.shelley.migrations.migrate(wid_src, pass, addresses)
handle_api_err(r, session)
erb :show_migrated, { :locals => { transactions: r, wid_src: wid_src} }
end
get "/wallets-migration-plan" do
wid = params[:wid]
wallets = @cw.shelley.wallets.list
erb :show_migration_plan, { :locals => { :migration_plan => nil,
:addresses => nil,
:wid => wid,
:wallets => wallets } }
end
post "/wallets-migration-plan" do
wid = params[:wid]
addresses = params[:addresses].split("\n").map{|a| a.strip}
wallets = @cw.shelley.wallets.list
r = @cw.shelley.migrations.plan(wid, addresses)
handle_api_err(r, session)
erb :show_migration_plan, { :locals => { :migration_plan => r,
:addresses => params[:addresses],
:wid => wid,
:wallets => wallets } }
end
# Transactions SHARED
get "/shared-wallets-transactions" do
query = toListTransactionsQuery(params)
r = @cw.shared.transactions.list(params[:wid], query)
handle_api_err r, session
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
erb :transactions, { :locals => { :wallets => wallets,
:transactions => r,
:query => query } }
end
# show tx details
get "/shared-wallets/:wal_id/txs/:tx_id" do
wid = params[:wal_id]
txid = params[:tx_id]
tx = @cw.shared.transactions.get(wid, txid, {"simple-metadata" => true})
handle_api_err tx, session
erb :tx_details, { :locals => { :tx => tx, :wid => wid } }
end
# Decode shared
get "/decode-tx-shared" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
erb :form_tx_new_decode, {:locals => { :wallets => wallets,
:tx => nil,
:serialized_tx => nil } }
end
post "/decode-tx-shared" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
serialized_tx = params[:serialized_tx].strip
tx = @cw.shared.transactions.decode(params[:wid], serialized_tx)
handle_api_err tx, session
erb :form_tx_new_decode, {:locals => { :wallets => wallets,
:tx => tx,
:serialized_tx => serialized_tx } }
end
# Construct shared
get "/construct-tx-shared" do
wallets = @cw.shared.wallets.list
handle_api_err wallets, session
erb :form_tx_new_construct, {:locals => { :wallets => wallets, :tx => nil } }
end
post "/construct-tx-shared" do
wid = params[:wid]
if params[:payments_check]
case params[:payments_mode]
when 'single_output', 'multi_output'
payload = prepare_payload_new_tx(params)
when 'between_wallets'
wid_dst = params[:wid_dst]
amount = params[:amount_wallet]
address_dst = @cw.shared.addresses.list(wid_dst,
{state: "unused"}).
first['id']
if params[:assets_wallet] == ''
payload = [ { "address": address_dst,
"amount": { "quantity": amount.to_i, "unit": "lovelace" }
}
]
else
assets = parse_assets(params[:assets_wallet])
payload = [ { "address": address_dst,
"amount": { "quantity": amount.to_i, "unit": "lovelace" },