This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lino-js.cjs.js
2858 lines (2836 loc) · 112 KB
/
lino-js.cjs.js
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
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var ByteBuffer = _interopDefault(require('bytebuffer'));
var shajs = _interopDefault(require('sha.js'));
var fetch = _interopDefault(require('cross-fetch'));
var elliptic = require('elliptic');
// return a new zero fee object
const getZeroFee = () => ({
amount: [],
gas: '0'
});
function encodeTx(msgs, rawPubKey, rawSigDER, seq) {
const stdSig = {
pub_key: convertToInternalPubKey(rawPubKey, _TYPE.PubKeySecp256k1),
signature: ByteBuffer.fromHex(rawSigDER).toString('base64'),
account_number: '0',
sequence: String(seq)
};
const stdTx = {
msg: msgs,
signatures: [stdSig],
fee: getZeroFee()
};
const authStdTx = {
type: 'auth/StdTx',
value: number2StringInObject(stdTx)
};
const jsonStr = JSON.stringify(authStdTx);
return ByteBuffer.btoa(jsonStr);
}
function decodeObject(result) {
var decodedResult = Object.assign({}, result);
var keys = Object.keys(result);
if (keys.length === 1 && keys[0] === 'amount' && !isNaN(decodedResult.amount)) {
decodedResult.amount = String(Number(decodedResult.amount) / 100000);
return decodedResult;
}
if (typeof result === 'string') {
return String(result);
}
if (result instanceof Array) {
decodedResult = [];
result.forEach(element => {
decodedResult.push(decodeObject(element));
});
}
else {
for (var index in keys) {
var key = keys[index];
if (key === 'memo' || key === 'title' || key === 'content' || key === 'description') {
if (decodedResult[key] !== null) {
decodedResult[key] = decodeURIComponent(escape(result[key]));
}
}
if (decodedResult[key] !== null &&
typeof decodedResult[key] !== 'string' &&
typeof decodedResult[key] !== 'boolean' &&
typeof decodedResult[key] !== 'number') {
decodedResult[key] = decodeObject(result[key]);
}
}
}
return decodedResult;
}
function encodeObject(result) {
var encodeResult = Object.assign({}, result);
var keys = Object.keys(result);
if (keys.length === 1 && keys[0] === 'amount' && !isNaN(encodeResult.amount)) {
encodeResult.amount = String(Number(encodeResult.amount) * 100000);
return encodeResult;
}
if (typeof result === 'string') {
return String(result);
}
if (result instanceof Array) {
encodeResult = [];
result.forEach(element => {
encodeResult.push(encodeObject(element));
});
}
else {
for (var index in keys) {
var key = keys[index];
if (encodeResult[key] !== null &&
typeof encodeResult[key] !== 'string' &&
typeof encodeResult[key] !== 'boolean') {
encodeResult[key] = encodeObject(result[key]);
}
}
}
return encodeResult;
}
function encodeMsg(msg) {
var encodedMsg = Object.assign({}, msg);
if ('new_reset_public_key' in msg) {
encodedMsg.new_reset_public_key = convertToInternalPubKey(msg.new_reset_public_key, _TYPE.PubKeySecp256k1);
}
if ('new_transaction_public_key' in msg) {
encodedMsg.new_transaction_public_key = convertToInternalPubKey(msg.new_transaction_public_key, _TYPE.PubKeySecp256k1);
}
if ('new_app_public_key' in msg) {
encodedMsg.new_app_public_key = convertToInternalPubKey(msg.new_app_public_key, _TYPE.PubKeySecp256k1);
}
if ('validator_public_key' in msg) {
encodedMsg.validator_public_key = convertToInternalPubKey(msg.validator_public_key, _TYPE.PubKeyEd25519);
}
if ('public_key' in msg) {
encodedMsg.public_key = convertToInternalPubKey(msg.public_key, _TYPE.PubKeySecp256k1);
}
return encodedMsg;
}
function encodeSignMsg(stdMsg, chainId, seq) {
const fee = getZeroFee();
const stdSignMsg = {
account_number: '0',
chain_id: chainId,
fee: fee,
memo: '',
msgs: stdMsg,
sequence: String(seq)
};
const jsonStr = JSON.stringify(number2StringInObject(sortObject(stdSignMsg)));
console.log('sign byte:', jsonStr);
const signMsgHash = shajs('sha256')
.update(jsonStr)
.digest();
return jsonStr;
}
function convertMsg(msg) {
var encodedMsg = Object.assign({}, msg);
if ('new_reset_public_key' in msg) {
var buffer = ByteBuffer.fromHex(msg.new_reset_public_key);
encodedMsg.new_reset_public_key = getByteArray(buffer);
}
if ('new_transaction_public_key' in msg) {
var buffer = ByteBuffer.fromHex(msg.new_transaction_public_key);
encodedMsg.new_transaction_public_key = getByteArray(buffer);
}
if ('new_app_public_key' in msg) {
var buffer = ByteBuffer.fromHex(msg.new_app_public_key);
encodedMsg.new_app_public_key = getByteArray(buffer);
}
if ('validator_public_key' in msg) {
var buffer = ByteBuffer.fromHex(msg.validator_public_key);
encodedMsg.validator_public_key = getByteArray(buffer);
}
return encodedMsg;
}
function getByteArray(buffer) {
let res = [];
for (var i = 0; i < buffer.limit; ++i) {
res.push(buffer.readUint8());
}
return res;
}
//decode std key to raw key, only support secp256k1 for now
function decodePrivKey(privKeyHex) {
privKeyHex = privKeyHex.toUpperCase();
if (privKeyHex.startsWith(_PREFIX.PrefixPrivKeySecp256k1)) {
return privKeyHex.slice(_PREFIX.PrefixPrivKeySecp256k1.length);
}
else if (privKeyHex.startsWith(_PREFIX.PrefixPrivKeyEd25519)) {
return privKeyHex.slice(_PREFIX.PrefixPrivKeyEd25519.length);
}
throw new Error(`Decode priv key failed: ${privKeyHex}\n`);
}
function decodePubKey(pubKeyHex) {
pubKeyHex = pubKeyHex.toUpperCase();
if (pubKeyHex.startsWith(_PREFIX.PrefixPubKeySecp256k1)) {
return pubKeyHex.slice(_PREFIX.PrefixPubKeySecp256k1.length);
}
else if (pubKeyHex.startsWith(_PREFIX.PrefixPubKeyEd25519)) {
return pubKeyHex.slice(_PREFIX.PrefixPubKeyEd25519.length);
}
throw new Error(`Decode pub key failed: ${pubKeyHex}\n`);
}
//eoncde raw key to std key, only support secp256k1 for now
function encodePrivKey(privKeyHex) {
return _PREFIX.PrefixPrivKeySecp256k1.concat(privKeyHex).toUpperCase();
}
function encodePubKey(pubKeyHex) {
return _PREFIX.PrefixPubKeySecp256k1.concat(pubKeyHex).toUpperCase();
}
// convert raw pub key to internal pub key format
function convertToInternalPubKey(rawPubKey, type) {
const res = {
type: type,
value: ByteBuffer.fromHex(rawPubKey).toString('base64')
};
return res;
}
// convert internal pub key to raw pub key
function convertToRawPubKey(internalPubKey) {
return ByteBuffer.fromBase64(internalPubKey.value).toString('hex');
}
function sortObject(object) {
var sortedObj = {}, keys = Object.keys(object);
keys.sort(function (key1, key2) {
(key1 = key1.toLowerCase()), (key2 = key2.toLowerCase());
if (key1 < key2)
return -1;
if (key1 > key2)
return 1;
return 0;
});
for (var index in keys) {
var key = keys[index];
if (typeof object[key] == 'object' && !(object[key] instanceof Array)) {
sortedObj[key] = sortObject(object[key]);
}
else if (typeof object[key] == 'object' && object[key] instanceof Array) {
sortedObj[key] = [];
object[key].forEach(element => {
sortedObj[key].push(sortObject(element));
});
}
else {
sortedObj[key] = object[key];
}
}
return sortedObj;
}
function number2StringInObject(object) {
var resultObj = {}, keys = Object.keys(object);
for (var index in keys) {
var key = keys[index];
if (typeof object[key] == 'object' && !(object[key] instanceof Array)) {
resultObj[key] = number2StringInObject(object[key]);
}
else if (typeof object[key] == 'object' && object[key] instanceof Array) {
resultObj[key] = [];
object[key].forEach(element => {
resultObj[key].push(number2StringInObject(element));
});
}
else {
if (typeof object[key] == 'number') {
resultObj[key] = String(object[key]);
}
else {
resultObj[key] = object[key];
}
}
}
return resultObj;
}
const _TYPE = {
PubKeyEd25519: 'tendermint/PubKeyEd25519',
PubKeySecp256k1: 'tendermint/PubKeySecp256k1',
PrivKeyEd25519: 'tendermint/PrivKeyEd25519',
PrivKeySecp256k1: 'tendermint/PrivKeySecp256k1',
SignatureKeyEd25519: 'tendermint/SignatureEd25519',
SignatureKeySecp256k1: 'tendermint/SignatureSecp256k1'
};
const _PREFIX = {
PrefixPubKeyEd25519: '1624DE6420',
PrefixPubKeySecp256k1: 'EB5AE98721',
PrefixPrivKeyEd25519: 'A328891040',
PrefixPrivKeySecp256k1: 'E1B0F79B20'
};
class Broadcast {
constructor(transport) {
this._transport = transport;
}
//account related
/**
* Register registers a new user on blockchain.
* It composes RegisterMsg and then broadcasts the transaction to blockchain.
*
* @param referrer: the user who refers the new user
* @param register_fee: the amount of money used for registering
* @param username: new username
* @param resetPubKey: new user's reset key
* @param transactionPubKeyHex: new user's transaction key
* @param appPubKeyHex: new user's app key
* @param referrerPrivKeyHex: referrer's private key
* @param seq: the sequence number of referrer for the next transaction
*/
register(referrer, register_fee, username, resetPubKey, transactionPubKeyHex, appPubKeyHex, referrerPrivKeyHex, seq) {
const msg = {
referrer: referrer,
register_fee: register_fee,
new_username: username,
new_reset_public_key: decodePubKey(resetPubKey),
new_transaction_public_key: decodePubKey(transactionPubKeyHex),
new_app_public_key: decodePubKey(appPubKeyHex)
};
return this._broadcastTransaction(msg, _MSGTYPE.RegisterMsgType, referrerPrivKeyHex, seq);
}
/**
* Transfer sends a certain amount of LINO token from the sender to the receiver.
* It composes TransferMsg and then broadcasts the transaction to blockchain.
*
* @param sender: the user who wants to send money
* @param receiver: the receiver whom the sender sends money to
* @param amount: the amount LINO token in the transfer
* @param memo: memos inthe transfer
* @param privKeyHex: the private key of sender
* @param seq: the sequence number of sender for the next transaction
*/
transfer(sender, receiver, amount, memo, privKeyHex, seq) {
const msg = {
amount: amount,
memo: memo,
receiver: receiver,
sender: sender
};
return this._broadcastTransaction(msg, _MSGTYPE.TransferMsgType, privKeyHex, seq);
}
/**
* Follow creates a social relationship between follower and followee.
* It composes FollowMsg and then broadcasts the transaction to blockchain.
*
* @param follower: follower
* @param followee: followee
* @param privKeyHex: the private key of follower
* @param seq: the sequence number of follower for the next transaction
*/
follow(follower, followee, privKeyHex, seq) {
const msg = {
followee: followee,
follower: follower
};
return this._broadcastTransaction(msg, _MSGTYPE.FollowMsgType, privKeyHex, seq);
}
/**
* Unfollow revokes the social relationship between follower and followee.
* It composes UnfollowMsg and then broadcasts the transaction to blockchain.
*
* @param follower: follower
* @param followee: followee
* @param privKeyHex: the private key of follower
* @param seq: the sequence number of follower for the next transaction
*/
unfollow(follower, followee, privKeyHex, seq) {
const msg = {
followee: followee,
follower: follower
};
return this._broadcastTransaction(msg, _MSGTYPE.UnfollowMsgType, privKeyHex, seq);
}
/**
* Claim claims rewards of a certain user.
* It composes ClaimMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who wants to claim reward
* @param privKeyHex: the private key of username
* @param seq: the sequence number of user for the next transaction
*/
claim(username, privKeyHex, seq) {
const msg = {
username
};
return this._broadcastTransaction(msg, _MSGTYPE.ClaimMsgType, privKeyHex, seq);
}
/**
* ClaimInterest claims interest of a certain user.
* It composes ClaimInterestMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who wants to claim interest
* @param privKeyHex: the private key of username
* @param seq: the sequence number of user for the next transaction
*/
claimInterest(username, privKeyHex, seq) {
const msg = {
username
};
return this._broadcastTransaction(msg, _MSGTYPE.ClaimInterestMsgType, privKeyHex, seq);
}
/**
* UpdateAccount updates account related info in jsonMeta which are not
* included in AccountInfo or AccountBank.
* It composes UpdateAccountMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who wants to update account meta
* @param json_meta: the newly updated meta
* @param privKeyHex: the private key of user
* @param seq: the sequence number of user for the next transaction
*/
updateAccount(username, json_meta, privKeyHex, seq) {
const msg = {
json_meta: json_meta,
username: username
};
return this._broadcastTransaction(msg, _MSGTYPE.UpdateAccMsgType, privKeyHex, seq);
}
/**
* Recover resets all keys of a user in case of losing or compromising.
* It composes RecoverMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who wants to recover account
* @param new_reset_public_key: new reset public key for user
* @param new_transaction_public_key: new transaction public key for user
* @param new_app_public_key: new app public key for user
* @param privKeyHex: the old private key of user
* @param seq: the sequence number of user for the next transaction
*/
recover(username, new_reset_public_key, new_transaction_public_key, new_app_public_key, privKeyHex, seq) {
const msg = {
username: username,
new_reset_public_key: decodePubKey(new_reset_public_key),
new_transaction_public_key: decodePubKey(new_transaction_public_key),
new_app_public_key: decodePubKey(new_app_public_key)
};
return this._broadcastTransaction(msg, _MSGTYPE.RecoverMsgType, privKeyHex, seq);
}
// post related
/**
* CreatePost creates a new post on blockchain.
* It composes CreatePostMsg and then broadcasts the transaction to blockchain.
*
* @param author: the user who creates the new post
* @param postID: the post id of the new post
* @param title: the title of the new post
* @param content: the content of the new post
* @param parentAuthor: if this is a comment, parentAuthor is the author of post that this comment is added to
* @param parentPostID: if this is a comment, parentPostID is the id of post that this comment is added to
* @param sourceAuthor: if this is a re-post, sourceAuthor should be the original post author
* @param sourcePostID: if this is a re-post, sourcePostID should be the original post id
* @param redistributionSplitRate: how much percentage the source post wants to split for re-post
* @param links: the links of the new post
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of user for the next transaction
*/
createPost(author, postID, title, content, parentAuthor, parentPostID, sourceAuthor, sourcePostID, redistributionSplitRate, links, privKeyHex, seq) {
let mLinks = null;
if (links != null) {
mLinks = [];
for (let entry of links.entries()) {
const mapping = {
identifier: entry[0],
url: entry[1]
};
mLinks.push(mapping);
}
}
const msg = {
author: author,
content: content,
links: mLinks,
parent_author: parentAuthor,
parent_postID: parentPostID,
post_id: postID,
redistribution_split_rate: redistributionSplitRate,
source_author: sourceAuthor,
source_postID: sourcePostID,
title: title
};
return this._broadcastTransaction(msg, _MSGTYPE.CreatePostMsgType, privKeyHex, seq);
}
/**
* Donate adds a money donation to a post by a user.
* It composes DonateMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who wants to donate to the post
* @param author: the author of the post
* @param amount: the amount LINO token that the user wants to donate
* @param post_id: the id of the post
* @param from_app: which app that the donation is from
* @param memo: memo of the donation
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of user for the next transaction
*/
donate(username, author, amount, post_id, from_app, memo, privKeyHex, seq) {
const msg = {
amount,
author,
from_app,
memo,
post_id,
username
};
return this._broadcastTransaction(msg, _MSGTYPE.DonateMsgType, privKeyHex, seq);
}
/**
* ReportOrUpvote adds a report or upvote action to a post.
* It composes ReportOrUpvoteMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who report or upvote the post
* @param author: the author of the post
* @param post_id: the id of the post
* @param is_report: indicates this is a report if set to true
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
reportOrUpvote(username, author, post_id, is_report, privKeyHex, seq) {
const msg = {
author,
is_report,
post_id,
username
};
return this._broadcastTransaction(msg, _MSGTYPE.ReportOrUpvoteMsgType, privKeyHex, seq);
}
/**
* DeletePost deletes a post from the blockchain. It doesn't actually
* remove the post from the blockchain, instead it sets IsDeleted to true
* and clears all the other data.
* It composes DeletePostMsg and then broadcasts the transaction to blockchain.
*
* @param author: the author of the post
* @param post_id: the id of the post
* @param privKeyHex: the private key of the author
* @param seq: the sequence number of the author for the next transaction
*/
deletePost(author, post_id, privKeyHex, seq) {
const msg = {
author,
post_id
};
return this._broadcastTransaction(msg, _MSGTYPE.DeletePostMsgType, privKeyHex, seq);
}
/**
* View increases the view count of a post by one.
* It composes ViewMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who view the post
* @param author: The author of the post
* @param post_id: the id of the post
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the author for the next transaction
*/
view(username, author, post_id, privKeyHex, seq) {
const msg = {
author,
post_id,
username
};
return this._broadcastTransaction(msg, _MSGTYPE.ViewMsgType, privKeyHex, seq);
}
/**
* UpdatePost updates post info with new data.
* It composes UpdatePostMsg and then broadcasts the transaction to blockchain.
*
* @param author: the author of the post
* @param title: new titile of the post
* @param post_id: the id of the post
* @param content: new content of the post
* @param links: new links of the post
* @param privKeyHex: the private key of the author
* @param seq: the sequence number of the author for the next transaction
*/
updatePost(author, title, post_id, content, links, privKeyHex, seq) {
let mLinks = null;
if (links != null) {
mLinks = [];
for (let entry of links.entries()) {
const mapping = {
identifier: entry[0],
url: entry[1]
};
mLinks.push(mapping);
}
}
const msg = {
author: author,
content: content,
links: mLinks,
post_id: post_id,
title: title
};
return this._broadcastTransaction(msg, _MSGTYPE.UpdatePostMsgType, privKeyHex, seq);
}
// validator related
/**
* ValidatorDeposit deposits a certain amount of LINO token for a user
* in order to become a validator. Before becoming a validator, the user
* has to be a voter.
* It composes ValidatorDepositMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who wants to deposit money for being a validator
* @param deposit: the amount of LINO token the user wants to deposit
* @param validator_public_key: the validator public key given by Tendermint
* @param link: the link of the user
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
validatorDeposit(username, deposit, validator_public_key, link, privKeyHex, seq) {
const msg = {
deposit: deposit,
link: link,
username: username,
validator_public_key: decodePubKey(validator_public_key)
};
return this._broadcastTransaction(msg, _MSGTYPE.ValDepositMsgType, privKeyHex, seq);
}
/**
* ValidatorWithdraw withdraws part of LINO token from a validator's deposit,
* while still keep being a validator.
* It composes ValidatorDepositMsg and then broadcasts the transaction to blockchain.
*
* @param username: the validator username
* @param amount: the amount of LINO token the validator wants to withdraw
* @param privKeyHex: the private key of the validator
* @param seq: the sequence number of the validator for the next transaction
*/
validatorWithdraw(username, amount, privKeyHex, seq) {
const msg = {
amount: amount,
username: username
};
return this._broadcastTransaction(msg, _MSGTYPE.ValWithdrawMsgType, privKeyHex, seq);
}
/**
* ValidatorRevoke revokes all deposited LINO token of a validator
* so that the user will not be a validator anymore.
* It composes ValidatorRevokeMsg and then broadcasts the transaction to blockchain.
*
* @param username: the validator username
* @param privKeyHex: the private key of the validator
* @param seq: the sequence number of the validator
*/
validatorRevoke(username, privKeyHex, seq) {
const msg = {
username
};
return this._broadcastTransaction(msg, _MSGTYPE.ValRevokeMsgType, privKeyHex, seq);
}
// vote related
/**
* StakeIn deposits a certain amount of LINO token for a user
* in order to become a voter.
* It composes model.StakeInMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user whot wants to deposit money for being a voter
* @param deposit: the amount of LINO token the user wants to deposit
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
stakeIn(username, deposit, privKeyHex, seq) {
const msg = {
deposit: deposit,
username: username
};
return this._broadcastTransaction(msg, _MSGTYPE.StakeInMsgType, privKeyHex, seq);
}
/**
* StakeOut withdraws part of LINO token from a voter's deposit,
* while still keep being a voter.
* It composes StakeOutMsg and then broadcasts the transaction to blockchain.
*
* @param username: the voter username
* @param amount: the amount of LINO token the voter wants to withdraw
* @param privKeyHex: the private key of the voter
* @param seq: the sequence number of the voter for the next transaction
*/
stakeOut(username, amount, privKeyHex, seq) {
const msg = {
amount: amount,
username: username
};
return this._broadcastTransaction(msg, _MSGTYPE.StakeOutMsgType, privKeyHex, seq);
}
/**
* Delegate delegates a certain amount of LINO token of delegator to a voter, so
* the voter will have more voting power.
* It composes DelegateMsg and then broadcasts the transaction to blockchain.
*
* @param delegator: the user who wants to delegate money
* @param voter: the voter that the delegator wants to delegate moeny to
* @param amount: the amount of LINO token that the delegator wants to delegate
* @param privKeyHex: the private key of the delegator
* @param seq: the sequence number of the delegator for the next transaction
*/
delegate(delegator, voter, amount, privKeyHex, seq) {
const msg = {
amount,
delegator,
voter
};
return this._broadcastTransaction(msg, _MSGTYPE.DelegateMsgType, privKeyHex, seq);
}
/**
* DelegatorWithdraw withdraws part of delegated LINO token of a delegator
* to a voter, while the delegation still exists.
* It composes DelegatorWithdrawMsg and then broadcasts the transaction to blockchain.
*
* @param delegator: the delegator username
* @param voter: the voter username
* @param amount: the amount of money that the delegator wants to withdraw
* @param privKeyHex: the private key of the delegator
* @param seq: the sequence number of the delegator for the next transaction
*/
delegatorWithdraw(delegator, voter, amount, privKeyHex, seq) {
const msg = {
delegator,
voter,
amount
};
return this._broadcastTransaction(msg, _MSGTYPE.DelegateWithdrawMsgType, privKeyHex, seq);
}
// developer related
/**
* DeveloperRegsiter registers a developer with a certain amount of LINO token on blockchain.
* It composes DeveloperRegisterMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who wants to become a developer
* @param deposit: the amount of money the user wants to deposit
* @param website: developer's website
* @param description: developer's description
* @param app_meta_data: developer's app meta data
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
developerRegister(username, deposit, website, description, app_meta_data, privKeyHex, seq) {
const msg = {
app_meta_data,
deposit,
description,
username,
website
};
return this._broadcastTransaction(msg, _MSGTYPE.DevRegisterMsgType, privKeyHex, seq);
}
/**
* DeveloperUpdate updates a developer info on blockchain.
* It composes DeveloperUpdateMsg and then broadcasts the transaction to blockchain.
*
* @param username: the developer's username
* @param website: new developer's website
* @param description: new developer's description
* @param app_meta_data: new developer's app meta data
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
developerUpdate(username, website, description, app_meta_data, privKeyHex, seq) {
const msg = {
username,
website,
description,
app_meta_data
};
return this._broadcastTransaction(msg, _MSGTYPE.DevUpdateMsgType, privKeyHex, seq);
}
/**
* DeveloperRevoke reovkes all deposited LINO token of a developer
* so the user will not be a developer anymore.
* It composes DeveloperRevokeMsg and then broadcasts the transaction to blockchain.
*
* @param username: the developer username
* @param privKeyHex: the private key of the developer
* @param seq: the sequence number of the developer for the next transaction
*/
developerRevoke(username, privKeyHex, seq) {
const msg = {
username
};
return this._broadcastTransaction(msg, _MSGTYPE.DevRevokeMsgType, privKeyHex, seq);
}
/**
* GrantPermission grants a certain (e.g. App) permission to
* an authorized app with a certain period of time.
* It composes GrantPermissionMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who grants the permission
* @param authorized_app: the authenticated app of the developer
* @param validity_period_second: how long does this app is valid
* @param grant_level: the permission level granted
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
grantPermission(username, authorized_app, validity_period_second, grant_level, amount, privKeyHex, seq) {
const msg = {
username,
authorized_app,
validity_period_second,
grant_level,
amount
};
return this._broadcastTransaction(msg, _MSGTYPE.GrantPermissionMsgType, privKeyHex, seq);
}
/**
* RevokePermission revokes the permission given previously to a app.
* It composes RevokePermissionMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who wants to revoke permission
* @param public_key: the user's public key that will be revoked
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
revokePermission(username, appName, permission, privKeyHex, seq) {
const msg = {
username: username,
revoke_from: appName,
permission: permission
};
return this._broadcastTransaction(msg, _MSGTYPE.RevokePermissionMsgType, privKeyHex, seq);
}
/**
* preAuthorizationPermission grants pre authorization permission to
* an authorized app with a certain period of time and amount.
* It composes GrantPermissionMsg and then broadcasts the transaction to blockchain.
*
* @param username: the user who grants the permission
* @param authorized_app: the authenticated app of the developer
* @param validity_period_second: how long does this app is valid
* @param grant_level: the permission level granted
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
preAuthorizationPermission(username, authorized_app, validity_period_second, amount, privKeyHex, seq) {
const msg = {
username,
authorized_app,
validity_period_second,
amount
};
return this._broadcastTransaction(msg, _MSGTYPE.PreAuthorizationMsgType, privKeyHex, seq);
}
// infra related
/**
* ProviderReport reports infra usage of a infra provider in order to get infra inflation.
* It composes ProviderReportMsg and then broadcasts the transaction to blockchain.
*
* @param username: the username of the infra provider
* @param usage: the amount of data traffic consumed
* @param privKeyHex: the private key of the user
* @param seq: the sequence number of the user for the next transaction
*/
providerReport(username, usage, privKeyHex, seq) {
const msg = {
usage,
username
};
return this._broadcastTransaction(msg, _MSGTYPE.ProviderReportMsgType, privKeyHex, seq);
}
// proposal related
/**
* VoteProposal adds a vote to a certain proposal with agree/disagree.
* It composes VoteProposalMsg and then broadcasts the transaction to blockchain.
*
* @param voter: the voter username
* @param proposal_id: the proposal id
* @param result: agree or disagree
* @param privKeyHex: the private key of the voter
* @param seq: the sequence number of the voter for the next transaction
*/
voteProposal(voter, proposal_id, result, privKeyHex, seq) {
const msg = {
proposal_id,
result,
voter
};
return this._broadcastTransaction(msg, _MSGTYPE.VoteProposalMsgType, privKeyHex, seq);
}
/**
* ChangeGlobalAllocationParam changes GlobalAllocationParam with new value.
* It composes ChangeGlobalAllocationParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal
* @param parameter: the GlobalAllocationParam
* @param reason: the reason to make such change
* @param privKeyHex: the private key of the creator
* @param seq: the sequence number of the creator for the next transaction
*/
changeGlobalAllocationParam(creator, parameter, reason, privKeyHex, seq) {
const msg = {
creator,
parameter: encodeObject(parameter),
reason
};
return this._broadcastTransaction(msg, _MSGTYPE.ChangeGlobalAllocationMsgType, privKeyHex, seq);
}
/**
* changeEvaluateOfContentValueParam changes EvaluateOfContentValueParam with new value.
* It composes ChangeEvaluateOfContentValueParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal
* @param parameter: the EvaluateOfContentValueParam
* @param reason: the reason to make such change
* @param privKeyHex: the private key of the creator
* @param seq: the sequence number of the creator for the next transaction
*/
changeEvaluateOfContentValueParam(creator, parameter, reason, privKeyHex, seq) {
const msg = {
creator,
parameter: encodeObject(parameter),
reason
};
return this._broadcastTransaction(msg, _MSGTYPE.ChangeEvaluationMsgType, privKeyHex, seq);
}
/**
* changeInfraInternalAllocationParam changes InfraInternalAllocationParam with new value.
* It composes ChangeInfraInternalAllocationParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal
* @param parameter: the InfraInternalAllocationParam
* @param reason: the reason to make such change
* @param privKeyHex: the private key of the creator
* @param seq: the sequence number of the creator for the next transaction
*/
changeInfraInternalAllocationParam(creator, parameter, reason, privKeyHex, seq) {
const msg = {
creator,
parameter: encodeObject(parameter),
reason
};
return this._broadcastTransaction(msg, _MSGTYPE.ChangeInfraAllocationMsgType, privKeyHex, seq);
}
/**
* changeVoteParam changes VoteParam with new value.
* It composes ChangeVoteParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal
* @param parameter: the VoteParam
* @param reason: the reason to make such change
* @param privKeyHex: the private key of the creator
* @param seq: the sequence number of the creator for the next transaction
*/
changeVoteParam(creator, parameter, reason, privKeyHex, seq) {
const msg = {
creator,
parameter: encodeObject(parameter),
reason
};
return this._broadcastTransaction(msg, _MSGTYPE.ChangeVoteParamMsgType, privKeyHex, seq);
}
/**
* changeProposalParam changes ProposalParam with new value.
* It composes ChangeProposalParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal
* @param parameter: the ProposalParam
* @param reason: the reason to make such change
* @param privKeyHex: the private key of the creator
* @param seq: the sequence number of the creator for the next transaction
*/
changeProposalParam(creator, parameter, reason, privKeyHex, seq) {
const msg = {
creator,
parameter: encodeObject(parameter),
reason
};
return this._broadcastTransaction(msg, _MSGTYPE.ChangeProposalParamMsgType, privKeyHex, seq);
}
/**
* changeDeveloperParam changes DeveloperParam with new value.
* It composes ChangeDeveloperParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal
* @param parameter: the DeveloperParam
* @param reason: the reason to make such change
* @param privKeyHex: the private key of the creator
* @param seq: the sequence number of the creator for the next transaction
*/
changeDeveloperParam(creator, parameter, reason, privKeyHex, seq) {
const msg = {
creator,
parameter: encodeObject(parameter),
reason
};
return this._broadcastTransaction(msg, _MSGTYPE.ChangeDeveloperParamMsgType, privKeyHex, seq);
}
/**
* changeValidatorParam changes ValidatorParam with new value.
* It composes ChangeValidatorParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal
* @param parameter: the ValidatorParam
* @param reason: the reason to make such change
* @param privKeyHex: the private key of the creator
* @param seq: the sequence number of the creator for the next transaction
*/
changeValidatorParam(creator, parameter, reason, privKeyHex, seq) {
const msg = {
creator,
parameter: encodeObject(parameter),
reason
};
return this._broadcastTransaction(msg, _MSGTYPE.ChangeValidatorParamMsgType, privKeyHex, seq);
}
/**
* changeBandwidthParam changes BandwidthParam with new value.
* It composes ChangeBandwidthParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal
* @param parameter: the BandwidthParam
* @param reason: the reason to make such change
* @param privKeyHex: the private key of the creator
* @param seq: the sequence number of the creator for the next transaction
*/
changeBandwidthParam(creator, parameter, reason, privKeyHex, seq) {
const msg = {
creator,
parameter: encodeObject(parameter),
reason
};
return this._broadcastTransaction(msg, _MSGTYPE.ChangeBandwidthParamMsgType, privKeyHex, seq);
}
/**
* changeAccountParam changes AccountParam with new value.
* It composes ChangeAccountParamMsg and then broadcasts the transaction to blockchain.
*
* @param creator: the user who creates the proposal