@locuschain/lib 0.0.16-beta.9 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,162 +1,6 @@
1
1
  import { A as AddressClass } from './address-class-CHAZYhdZ.js';
2
2
  import ___default from 'lodash';
3
3
 
4
- // Automatically generated file. DO NOT EDIT.
5
- const callWasm = (cmd, params) => {
6
- // @ts-ignore
7
- let callLclib = globalThis['CallLclib'];
8
- if (LCLIB_NAMESPACE) {
9
- // @ts-ignore
10
- callLclib = globalThis[LCLIB_NAMESPACE]['CallLclib'];
11
- }
12
- // @ts-ignore
13
- if (typeof callLclib !== 'function')
14
- throw new Error('Wasm module is not loaded. Please call loadWasm or loadWasmSync first.');
15
- // @ts-ignore
16
- const resultString = callLclib(cmd, JSON.stringify(params));
17
- const result = JSON.parse(resultString);
18
- if (result.error && result.error.code != 0) {
19
- const error = Object.assign(Object.assign({}, result.error), { message: result.error.message });
20
- delete error.message;
21
- throw error;
22
- }
23
- else {
24
- return result.result;
25
- }
26
- };
27
- const calculateTxLinkHash = (params) => {
28
- return callWasm('calculateTxLinkHash', [params]);
29
- };
30
- const compileCoreScript = (params) => {
31
- return callWasm('compileCoreScript', [params]);
32
- };
33
- const convertAddressToData = (addrEncoded) => {
34
- return callWasm('convertAddressToData', [addrEncoded]);
35
- };
36
- const convertAddressToHex = (addrEncoded) => {
37
- return callWasm('convertAddressToHex', [addrEncoded]);
38
- };
39
- const convertBase32ToData = (encoded) => {
40
- return callWasm('convertBase32ToData', [encoded]);
41
- };
42
- const convertBase32ToHex = (encoded) => {
43
- return callWasm('convertBase32ToHex', [encoded]);
44
- };
45
- const convertCurrency = (params) => {
46
- return callWasm('convertCurrency', [params]);
47
- };
48
- const convertDataTo = (value, retType) => {
49
- return callWasm('convertDataTo', [value, retType]);
50
- };
51
- const convertDataToAddress = (str) => {
52
- return callWasm('convertDataToAddress', [str]);
53
- };
54
- const convertDataToBase32 = (str) => {
55
- return callWasm('convertDataToBase32', [str]);
56
- };
57
- const convertDataToHex = (str) => {
58
- return callWasm('convertDataToHex', [str]);
59
- };
60
- const convertDataToString = (str) => {
61
- return callWasm('convertDataToString', [str]);
62
- };
63
- const convertHexToAddress = (hexString) => {
64
- return callWasm('convertHexToAddress', [hexString]);
65
- };
66
- const convertHexToBase32 = (hexString) => {
67
- return callWasm('convertHexToBase32', [hexString]);
68
- };
69
- const convertHexToData = (encoded) => {
70
- return callWasm('convertHexToData', [encoded]);
71
- };
72
- const convertStringToData = (raw) => {
73
- return callWasm('convertStringToData', [raw]);
74
- };
75
- const convertToData = (value, valueType) => {
76
- return callWasm('convertToData', [value, valueType]);
77
- };
78
- const createAccountAndKeystore = (params) => {
79
- return callWasm('createAccountAndKeystore', [params]);
80
- };
81
- const createMasterKeystore = (params) => {
82
- return callWasm('createMasterKeystore', [params]);
83
- };
84
- const createNormalKey = (params) => {
85
- return callWasm('createNormalKey', [params]);
86
- };
87
- const createNormalKeystore = (params) => {
88
- return callWasm('createNormalKeystore', [params]);
89
- };
90
- const decodeTxs = (params) => {
91
- return callWasm('decodeTxs', [params]);
92
- };
93
- const deriveKeysFromMnemonic = (params) => {
94
- return callWasm('deriveKeysFromMnemonic', [params]);
95
- };
96
- const disCompileCoreScript = (params) => {
97
- return callWasm('disCompileCoreScript', [params]);
98
- };
99
- const encodeTxCurrency = (params) => {
100
- return callWasm('encodeTxCurrency', [params]);
101
- };
102
- const encodeTxNumber = (params) => {
103
- return callWasm('encodeTxNumber', [params]);
104
- };
105
- const generateMnemonic = (params) => {
106
- return callWasm('generateMnemonic', [params]);
107
- };
108
- const generateMnemonicBySeed = (params) => {
109
- return callWasm('generateMnemonicBySeed', [params]);
110
- };
111
- const getDefFromCoreScript = (params) => {
112
- return callWasm('getDefFromCoreScript', [params]);
113
- };
114
- const getHomeShard = (params) => {
115
- return callWasm('getHomeShard', [params]);
116
- };
117
- const getLibraryVersions = () => {
118
- return callWasm('getLibraryVersions', []);
119
- };
120
- const gzipAndEncode = (str) => {
121
- return callWasm('gzipAndEncode', [str]);
122
- };
123
- const hash = (params) => {
124
- return callWasm('hash', [params]);
125
- };
126
- const isGrantConsumingTx = (txTypeStr) => {
127
- return callWasm('isGrantConsumingTx', [txTypeStr]);
128
- };
129
- const loadMasterKeystore = (params) => {
130
- return callWasm('loadMasterKeystore', [params]);
131
- };
132
- const loadNormalKeystore = (params) => {
133
- return callWasm('loadNormalKeystore', [params]);
134
- };
135
- const makeCurrency = (params) => {
136
- return callWasm('makeCurrency', [params]);
137
- };
138
- const sign = (param) => {
139
- return callWasm('sign', [param]);
140
- };
141
- const signByMasterKey = (params) => {
142
- return callWasm('signByMasterKey', [params]);
143
- };
144
- const testCoreScript = (params) => {
145
- return callWasm('testCoreScript', [params]);
146
- };
147
- const verify = (params) => {
148
- return callWasm('verify', [params]);
149
- };
150
- const verifyByMasterKey = (params) => {
151
- return callWasm('verifyByMasterKey', [params]);
152
- };
153
- const verifyMerkleProof = (params) => {
154
- return callWasm('verifyMerkleProof', [params]);
155
- };
156
- const verifyTx = (jsonTx) => {
157
- return callWasm('verifyTx', [jsonTx]);
158
- };
159
-
160
4
  /******************************************************************************
161
5
  Copyright (c) Microsoft Corporation.
162
6
 
@@ -815,215 +659,161 @@ function load() {
815
659
  });
816
660
  }
817
661
 
818
- class TxValidator {
819
- constructor() {
820
- this.verifyTxParams = (rpcResult, params, options) => {
821
- try {
822
- if (options.targetTxHash) {
823
- const genHash = JSON.parse(verifyTx(JSON.stringify(rpcResult.tx))).hash;
824
- if (options.targetTxHash !== genHash) {
825
- console.warn(`[TxnIntegrityChecker] Tx hash not matched.`, options.targetTxHash, '!==', genHash);
826
- return { result: false, reason: 'tx verify fail' };
827
- }
828
- }
829
- const checks = [];
830
- const tx = rpcResult.tx;
831
- switch (tx.type) {
832
- case 'TX_TRANSFER_COIN':
833
- case 'TX_TRANSFER_COIN_FEE':
834
- checks.push(() => this.checkFields(params, tx, { to: 'target', amount: 'amount' }));
835
- break;
836
- case 'TX_TRANSFER_COIN_EXPRESS':
837
- checks.push(() => this.checkFields(params, tx, {
838
- to: 'target',
839
- amount: 'amount',
840
- 'source.owner': 'source.addr',
841
- 'source.height': 'source.index'
842
- }));
843
- break;
844
- case 'TX_CREATE_TOKEN':
845
- case 'TX_CREATE_TOKEN_FEE':
846
- checks.push(() => this.checkFields(params, tx, { tokenBalance: 'tokenBalance.v' }));
847
- break;
848
- case 'TX_TRANSFER_TOKEN':
849
- case 'TX_TRANSFER_TOKEN_FEE':
850
- checks.push(() => this.checkFields(params, tx, { to: 'target', amount: 'amount' }), () => this.checkComplexField(params, tx, {
851
- paramPath: 'tokenAmounts',
852
- txPath: 'tokens',
853
- paramTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.tokenId, a: d.amountToken })),
854
- txTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.t, a: d.a }))
855
- }));
856
- break;
857
- case 'TX_CREATE_ASSETOBJECT':
858
- case 'TX_CREATE_ASSETOBJECT_FEE':
859
- checks.push(() => this.checkFields(params, tx, Object.assign({
860
- assetType: 'object.type',
861
- metaData: 'object.metaData'
862
- }, (params.amount === '0' ? {} : { amount: 'object.amount' })))
863
- // TODO: params.operator !== tx.Object.Operator ("" !== "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHF3E")
864
- );
865
- break;
866
- case 'TX_TRANSFER_ASSETOBJECT':
867
- case 'TX_TRANSFER_ASSETOBJECT_FEE':
868
- checks.push(() => this.checkFields(params, tx, { to: 'target' }), () => this.checkFields(params, tx, {
869
- to: 'target',
870
- amount: 'assetObjects.assetObjects[0].targetObject.amount',
871
- assetId: 'assetObjects.assetObjects[0].targetObject.assetId'
872
- }));
873
- break;
874
- case 'TX_CREATE_CONTRACT':
875
- case 'TX_CREATE_CONTRACT_FEE':
876
- checks.push(() => this.checkFields(params, tx, {
877
- code: 'code',
878
- amount: 'amount',
879
- fuelLimit: 'fuelLimit',
880
- sysId: 'sysId',
881
- until: 'until'
882
- }, {
883
- contractAccount: 'target'
884
- }), () => this.checkComplexField(params, tx, {
885
- paramPath: 'tokenAmounts',
886
- txPath: 'tokens',
887
- paramTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.tokenId, a: d.amountToken })),
888
- txTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.t, a: d.a }))
889
- }));
890
- break;
891
- case 'TX_CALL_CONTRACT':
892
- case 'TX_CALL_CONTRACT_FEE':
893
- checks.push(() => this.checkFields(params, tx, {
894
- contractAccount: 'target',
895
- amount: 'amount',
896
- fuelLimit: 'fuelLimit',
897
- func: 'func',
898
- argData: 'argData'
899
- }), () => this.checkComplexField(params, tx, {
900
- paramPath: 'tokenAmounts',
901
- txPath: 'tokens',
902
- paramTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.tokenId, a: d.amountToken })),
903
- txTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.t, a: d.a }))
904
- })
905
- // TODO: tx.AssetObjects 는 아직 rpc가 만들어지지 않아서 확인을 하지 못했다
906
- );
907
- break;
908
- case 'TX_POST_DATA':
909
- case 'TX_POST_DATA_FEE':
910
- checks.push(() => this.checkFields(params, tx, { data: 'data', label: 'label' }));
911
- break;
912
- case 'TX_OPEN_ACCOUNT':
913
- checks.push(() => this.checkFields(params, tx, { account: 'addr', pk: 'key' }, {
914
- sender: 'refLink.addr',
915
- index: 'refLink.index'
916
- }));
917
- break;
918
- case 'TX_PROVIDE_SCRIPT':
919
- checks.push(() => this.checkFields(params, tx, {
920
- target: 'target',
921
- targetAfterTimeLock: 'targetAfterTimeLock',
922
- amount: 'amount'
923
- }), () => this.checkComplexField(params, tx, {
924
- paramPath: 'scriptProvide',
925
- txPath: 'scriptProvide',
926
- paramTransform: code => compileCoreScript(code),
927
- txTransform: code => code
928
- }), () => this.checkComplexField(params, tx, {
929
- paramPath: 'scriptAccept',
930
- txPath: 'scriptAccept',
931
- paramTransform: code => compileCoreScript(code),
932
- txTransform: code => code
933
- }), () => this.checkComplexField(params, tx, {
934
- paramPath: 'args',
935
- txPath: 'args',
936
- paramTransform: args => args !== null && args !== void 0 ? args : [],
937
- txTransform: args => args !== null && args !== void 0 ? args : []
938
- }));
939
- break;
940
- case 'TX_ACCEPT_SCRIPT':
941
- checks.push(() => this.checkFields(params, tx, { provider: 'refLink.addr', index: 'refLink.index' }), () => this.checkComplexField(params, tx, {
942
- paramPath: 'args',
943
- txPath: 'args',
944
- paramTransform: args => args !== null && args !== void 0 ? args : [],
945
- txTransform: args => args !== null && args !== void 0 ? args : []
946
- }) // 배열 비교라서..
947
- );
948
- break;
949
- case 'TX_CLOSE_ACCOUNT':
950
- checks.push(() => this.checkFields(params, tx, { to: 'target' }));
951
- break;
952
- case 'TX_BECOME_HOST':
953
- case 'TX_BECOME_GUEST':
954
- break;
955
- default:
956
- break;
957
- }
958
- for (const check of checks) {
959
- const result = check();
960
- if (result !== true)
961
- return result;
962
- }
963
- return { result: true, reason: 'success' };
964
- }
965
- catch (e) {
966
- console.error(e);
967
- return { result: false, reason: `tx verify fail. error: ${e.message}` };
968
- }
969
- };
970
- /**
971
- * params 객체와 tx 객체의 필드를 주어진 매핑에 따라 비교합니다.
972
- * @param params 파라미터 객체.
973
- * @param tx 검증할 트랜잭션 객체.
974
- * @param mapping 키는 `params`의 속성 경로, 값은 `tx`의 속성 경로인 객체.
975
- * @param optional mapping과 동일한 구조이나, `params` 값이 있을경우만 체크합니다. string은 빈문자열이 아닌 문자열, number는 0을 없다고 판단합니다
976
- * @returns 모든 필드가 일치하면 `true`, 실패 시 `IntegrityCheckResult` 객체를 반환합니다.
977
- */
978
- this.checkFields = (params, tx, mapping, optional) => {
979
- for (const paramKey in mapping) {
980
- const txKey = mapping[paramKey];
981
- const paramValue = this.getByPathIgnoreCase(params, paramKey);
982
- const txValue = ___default.get(tx, txKey);
983
- if (paramValue !== txValue) {
984
- return this.logAndFail(tx.type, paramKey, paramValue, txValue);
985
- }
986
- }
987
- for (const paramKey in optional !== null && optional !== void 0 ? optional : {}) {
988
- const txKey = optional[paramKey];
989
- const paramValue = this.getByPathIgnoreCase(params, paramKey);
990
- if (paramValue && paramValue !== '' && paramValue !== 0) {
991
- const txValue = ___default.get(tx, txKey);
992
- if (paramValue !== txValue) {
993
- return this.logAndFail(tx.type, paramKey, paramValue, txValue);
994
- }
995
- }
996
- }
997
- return true;
998
- };
999
- this.checkComplexField = (params, tx, { paramPath, txPath, paramTransform, txTransform }) => {
1000
- const paramValue = paramTransform(this.getByPathIgnoreCase(params, paramPath));
1001
- const txValue = txTransform(___default.get(tx, txPath));
1002
- if (!___default.isEqual(paramValue, txValue)) {
1003
- return this.logAndFail(tx.type, paramPath, paramValue, txValue);
1004
- }
1005
- return true;
1006
- };
662
+ // Automatically generated file. DO NOT EDIT.
663
+ const callWasm = (cmd, params) => {
664
+ // @ts-ignore
665
+ let callLclib = globalThis['CallLclib'];
666
+ if (namespace) {
667
+ // @ts-ignore
668
+ callLclib = globalThis[namespace]['CallLclib'];
1007
669
  }
1008
- logAndFail(txType, field, expected, actual) {
1009
- console.warn(`[TxnIntegrityChecker] ${txType} ${field}:`, expected, actual);
1010
- return { result: false, reason: 'tx integrity check fail', field };
670
+ // @ts-ignore
671
+ if (typeof callLclib !== 'function')
672
+ throw new Error('Wasm module is not loaded. Please call loadWasm or loadWasmSync first.');
673
+ // @ts-ignore
674
+ const resultString = callLclib(cmd, JSON.stringify(params));
675
+ const result = JSON.parse(resultString);
676
+ if (result.error && result.error.code != 0) {
677
+ const error = Object.assign(Object.assign({}, result.error), { message: result.error.message });
678
+ delete error.message;
679
+ throw error;
1011
680
  }
1012
- // 속성명의 대소문자를 무시하고 값을 구한다
1013
- getByPathIgnoreCase(obj, path) {
1014
- const keys = path.split('.');
1015
- let current = obj;
1016
- for (const key of keys) {
1017
- if (current === null || typeof current !== 'object')
1018
- return undefined;
1019
- const actualKey = Object.keys(current).find(k => k.toLowerCase() === key.toLowerCase());
1020
- if (actualKey === undefined)
1021
- return undefined;
1022
- current = current[actualKey];
1023
- }
1024
- return current;
681
+ else {
682
+ return result.result;
1025
683
  }
1026
- }
684
+ };
685
+ const calculateTxLinkHash = (params) => {
686
+ return callWasm('calculateTxLinkHash', [params]);
687
+ };
688
+ const compileCoreScript = (params) => {
689
+ return callWasm('compileCoreScript', [params]);
690
+ };
691
+ const convertAddressToData = (addrEncoded) => {
692
+ return callWasm('convertAddressToData', [addrEncoded]);
693
+ };
694
+ const convertAddressToHex = (addrEncoded) => {
695
+ return callWasm('convertAddressToHex', [addrEncoded]);
696
+ };
697
+ const convertBase32ToData = (encoded) => {
698
+ return callWasm('convertBase32ToData', [encoded]);
699
+ };
700
+ const convertBase32ToHex = (encoded) => {
701
+ return callWasm('convertBase32ToHex', [encoded]);
702
+ };
703
+ const convertCurrency = (params) => {
704
+ return callWasm('convertCurrency', [params]);
705
+ };
706
+ const convertDataTo = (value, retType) => {
707
+ return callWasm('convertDataTo', [value, retType]);
708
+ };
709
+ const convertDataToAddress = (str) => {
710
+ return callWasm('convertDataToAddress', [str]);
711
+ };
712
+ const convertDataToBase32 = (str) => {
713
+ return callWasm('convertDataToBase32', [str]);
714
+ };
715
+ const convertDataToHex = (str) => {
716
+ return callWasm('convertDataToHex', [str]);
717
+ };
718
+ const convertDataToString = (str) => {
719
+ return callWasm('convertDataToString', [str]);
720
+ };
721
+ const convertHexToAddress = (hexString) => {
722
+ return callWasm('convertHexToAddress', [hexString]);
723
+ };
724
+ const convertHexToBase32 = (hexString) => {
725
+ return callWasm('convertHexToBase32', [hexString]);
726
+ };
727
+ const convertHexToData = (encoded) => {
728
+ return callWasm('convertHexToData', [encoded]);
729
+ };
730
+ const convertStringToData = (raw) => {
731
+ return callWasm('convertStringToData', [raw]);
732
+ };
733
+ const convertToData = (value, valueType) => {
734
+ return callWasm('convertToData', [value, valueType]);
735
+ };
736
+ const createAccountAndKeystore = (params) => {
737
+ return callWasm('createAccountAndKeystore', [params]);
738
+ };
739
+ const createMasterKeystore = (params) => {
740
+ return callWasm('createMasterKeystore', [params]);
741
+ };
742
+ const createNormalKey = (params) => {
743
+ return callWasm('createNormalKey', [params]);
744
+ };
745
+ const createNormalKeystore = (params) => {
746
+ return callWasm('createNormalKeystore', [params]);
747
+ };
748
+ const decodeTxs = (params) => {
749
+ return callWasm('decodeTxs', [params]);
750
+ };
751
+ const deriveKeysFromMnemonic = (params) => {
752
+ return callWasm('deriveKeysFromMnemonic', [params]);
753
+ };
754
+ const disCompileCoreScript = (params) => {
755
+ return callWasm('disCompileCoreScript', [params]);
756
+ };
757
+ const encodeTxCurrency = (params) => {
758
+ return callWasm('encodeTxCurrency', [params]);
759
+ };
760
+ const encodeTxNumber = (params) => {
761
+ return callWasm('encodeTxNumber', [params]);
762
+ };
763
+ const generateMnemonic = (params) => {
764
+ return callWasm('generateMnemonic', [params]);
765
+ };
766
+ const generateMnemonicBySeed = (params) => {
767
+ return callWasm('generateMnemonicBySeed', [params]);
768
+ };
769
+ const getDefFromCoreScript = (params) => {
770
+ return callWasm('getDefFromCoreScript', [params]);
771
+ };
772
+ const getHomeShard = (params) => {
773
+ return callWasm('getHomeShard', [params]);
774
+ };
775
+ const getLibraryVersions = () => {
776
+ return callWasm('getLibraryVersions', []);
777
+ };
778
+ const gzipAndEncode = (str) => {
779
+ return callWasm('gzipAndEncode', [str]);
780
+ };
781
+ const hash = (params) => {
782
+ return callWasm('hash', [params]);
783
+ };
784
+ const isGrantConsumingTx = (txTypeStr) => {
785
+ return callWasm('isGrantConsumingTx', [txTypeStr]);
786
+ };
787
+ const loadMasterKeystore = (params) => {
788
+ return callWasm('loadMasterKeystore', [params]);
789
+ };
790
+ const loadNormalKeystore = (params) => {
791
+ return callWasm('loadNormalKeystore', [params]);
792
+ };
793
+ const makeCurrency = (params) => {
794
+ return callWasm('makeCurrency', [params]);
795
+ };
796
+ const sign = (param) => {
797
+ return callWasm('sign', [param]);
798
+ };
799
+ const signByMasterKey = (params) => {
800
+ return callWasm('signByMasterKey', [params]);
801
+ };
802
+ const testCoreScript = (params) => {
803
+ return callWasm('testCoreScript', [params]);
804
+ };
805
+ const verify = (params) => {
806
+ return callWasm('verify', [params]);
807
+ };
808
+ const verifyByMasterKey = (params) => {
809
+ return callWasm('verifyByMasterKey', [params]);
810
+ };
811
+ const verifyMerkleProof = (params) => {
812
+ return callWasm('verifyMerkleProof', [params]);
813
+ };
814
+ const verifyTx = (jsonTx) => {
815
+ return callWasm('verifyTx', [jsonTx]);
816
+ };
1027
817
 
1028
818
  // Automatically generated file. DO NOT EDIT.
1029
819
  const TX_CREATION_RPC_LIST = [
@@ -1512,12 +1302,220 @@ class Rpc {
1512
1302
  }
1513
1303
  }
1514
1304
 
1305
+ class TxValidator {
1306
+ constructor() {
1307
+ this.verifyTxParams = (rpcResult, params, options) => {
1308
+ try {
1309
+ if (options.targetTxHash) {
1310
+ const genHash = JSON.parse(verifyTx(JSON.stringify(rpcResult.tx))).hash;
1311
+ if (options.targetTxHash !== genHash) {
1312
+ console.warn(`[TxnIntegrityChecker] Tx hash not matched.`, options.targetTxHash, '!==', genHash);
1313
+ return { result: false, reason: 'tx verify fail' };
1314
+ }
1315
+ }
1316
+ const checks = [];
1317
+ const tx = rpcResult.tx;
1318
+ switch (tx.type) {
1319
+ case 'TX_TRANSFER_COIN':
1320
+ case 'TX_TRANSFER_COIN_FEE':
1321
+ checks.push(() => this.checkFields(params, tx, { to: 'target', amount: 'amount' }));
1322
+ break;
1323
+ case 'TX_TRANSFER_COIN_EXPRESS':
1324
+ checks.push(() => this.checkFields(params, tx, {
1325
+ to: 'target',
1326
+ amount: 'amount',
1327
+ 'source.owner': 'source.addr',
1328
+ 'source.height': 'source.index'
1329
+ }));
1330
+ break;
1331
+ case 'TX_CREATE_TOKEN':
1332
+ case 'TX_CREATE_TOKEN_FEE':
1333
+ checks.push(() => this.checkFields(params, tx, { tokenBalance: 'tokenBalance.v' }));
1334
+ break;
1335
+ case 'TX_TRANSFER_TOKEN':
1336
+ case 'TX_TRANSFER_TOKEN_FEE':
1337
+ checks.push(() => this.checkFields(params, tx, { to: 'target', amount: 'amount' }), () => this.checkComplexField(params, tx, {
1338
+ paramPath: 'tokenAmounts',
1339
+ txPath: 'tokens',
1340
+ paramTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.tokenId, a: d.amountToken })),
1341
+ txTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.t, a: d.a }))
1342
+ }));
1343
+ break;
1344
+ case 'TX_CREATE_ASSETOBJECT':
1345
+ case 'TX_CREATE_ASSETOBJECT_FEE':
1346
+ checks.push(() => this.checkFields(params, tx, Object.assign({
1347
+ assetType: 'object.type',
1348
+ metaData: 'object.metaData'
1349
+ }, (params.amount === '0' ? {} : { amount: 'object.amount' })))
1350
+ // TODO: params.operator !== tx.Object.Operator ("" !== "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHF3E")
1351
+ );
1352
+ break;
1353
+ case 'TX_TRANSFER_ASSETOBJECT':
1354
+ case 'TX_TRANSFER_ASSETOBJECT_FEE':
1355
+ checks.push(() => this.checkFields(params, tx, { to: 'target' }), () => this.checkFields(params, tx, {
1356
+ to: 'target',
1357
+ amount: 'assetObjects.assetObjects[0].targetObject.amount',
1358
+ assetId: 'assetObjects.assetObjects[0].targetObject.assetId'
1359
+ }));
1360
+ break;
1361
+ case 'TX_CREATE_CONTRACT':
1362
+ case 'TX_CREATE_CONTRACT_FEE':
1363
+ checks.push(() => this.checkFields(params, tx, {
1364
+ code: 'code',
1365
+ amount: 'amount',
1366
+ fuelLimit: 'fuelLimit',
1367
+ sysId: 'sysId',
1368
+ until: 'until'
1369
+ }, {
1370
+ contractAccount: 'target'
1371
+ }), () => this.checkComplexField(params, tx, {
1372
+ paramPath: 'tokenAmounts',
1373
+ txPath: 'tokens',
1374
+ paramTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.tokenId, a: d.amountToken })),
1375
+ txTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.t, a: d.a }))
1376
+ }));
1377
+ break;
1378
+ case 'TX_CALL_CONTRACT':
1379
+ case 'TX_CALL_CONTRACT_FEE':
1380
+ checks.push(() => this.checkFields(params, tx, {
1381
+ contractAccount: 'target',
1382
+ amount: 'amount',
1383
+ fuelLimit: 'fuelLimit',
1384
+ func: 'func',
1385
+ argData: 'argData'
1386
+ }), () => this.checkComplexField(params, tx, {
1387
+ paramPath: 'tokenAmounts',
1388
+ txPath: 'tokens',
1389
+ paramTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.tokenId, a: d.amountToken })),
1390
+ txTransform: tokens => (tokens !== null && tokens !== void 0 ? tokens : []).map((d) => ({ t: d.t, a: d.a }))
1391
+ })
1392
+ // TODO: tx.AssetObjects 는 아직 rpc가 만들어지지 않아서 확인을 하지 못했다
1393
+ );
1394
+ break;
1395
+ case 'TX_POST_DATA':
1396
+ case 'TX_POST_DATA_FEE':
1397
+ checks.push(() => this.checkFields(params, tx, { data: 'data', label: 'label' }));
1398
+ break;
1399
+ case 'TX_OPEN_ACCOUNT':
1400
+ checks.push(() => this.checkFields(params, tx, { account: 'addr', pk: 'key' }, {
1401
+ sender: 'refLink.addr',
1402
+ index: 'refLink.index'
1403
+ }));
1404
+ break;
1405
+ case 'TX_PROVIDE_SCRIPT':
1406
+ checks.push(() => this.checkFields(params, tx, {
1407
+ target: 'target',
1408
+ targetAfterTimeLock: 'targetAfterTimeLock',
1409
+ amount: 'amount'
1410
+ }), () => this.checkComplexField(params, tx, {
1411
+ paramPath: 'scriptProvide',
1412
+ txPath: 'scriptProvide',
1413
+ paramTransform: code => compileCoreScript(code),
1414
+ txTransform: code => code
1415
+ }), () => this.checkComplexField(params, tx, {
1416
+ paramPath: 'scriptAccept',
1417
+ txPath: 'scriptAccept',
1418
+ paramTransform: code => compileCoreScript(code),
1419
+ txTransform: code => code
1420
+ }), () => this.checkComplexField(params, tx, {
1421
+ paramPath: 'args',
1422
+ txPath: 'args',
1423
+ paramTransform: args => args !== null && args !== void 0 ? args : [],
1424
+ txTransform: args => args !== null && args !== void 0 ? args : []
1425
+ }));
1426
+ break;
1427
+ case 'TX_ACCEPT_SCRIPT':
1428
+ checks.push(() => this.checkFields(params, tx, { provider: 'refLink.addr', index: 'refLink.index' }), () => this.checkComplexField(params, tx, {
1429
+ paramPath: 'args',
1430
+ txPath: 'args',
1431
+ paramTransform: args => args !== null && args !== void 0 ? args : [],
1432
+ txTransform: args => args !== null && args !== void 0 ? args : []
1433
+ }) // 배열 비교라서..
1434
+ );
1435
+ break;
1436
+ case 'TX_CLOSE_ACCOUNT':
1437
+ checks.push(() => this.checkFields(params, tx, { to: 'target' }));
1438
+ break;
1439
+ case 'TX_BECOME_HOST':
1440
+ case 'TX_BECOME_GUEST':
1441
+ break;
1442
+ default:
1443
+ break;
1444
+ }
1445
+ for (const check of checks) {
1446
+ const result = check();
1447
+ if (result !== true)
1448
+ return result;
1449
+ }
1450
+ return { result: true, reason: 'success' };
1451
+ }
1452
+ catch (e) {
1453
+ console.error(e);
1454
+ return { result: false, reason: `tx verify fail. error: ${e.message}` };
1455
+ }
1456
+ };
1457
+ /**
1458
+ * params 객체와 tx 객체의 필드를 주어진 매핑에 따라 비교합니다.
1459
+ * @param params 파라미터 객체.
1460
+ * @param tx 검증할 트랜잭션 객체.
1461
+ * @param mapping 키는 `params`의 속성 경로, 값은 `tx`의 속성 경로인 객체.
1462
+ * @param optional mapping과 동일한 구조이나, `params` 값이 있을경우만 체크합니다. string은 빈문자열이 아닌 문자열, number는 0을 없다고 판단합니다
1463
+ * @returns 모든 필드가 일치하면 `true`, 실패 시 `IntegrityCheckResult` 객체를 반환합니다.
1464
+ */
1465
+ this.checkFields = (params, tx, mapping, optional) => {
1466
+ for (const paramKey in mapping) {
1467
+ const txKey = mapping[paramKey];
1468
+ const paramValue = this.getByPathIgnoreCase(params, paramKey);
1469
+ const txValue = ___default.get(tx, txKey);
1470
+ if (paramValue !== txValue) {
1471
+ return this.logAndFail(tx.type, paramKey, paramValue, txValue);
1472
+ }
1473
+ }
1474
+ for (const paramKey in optional !== null && optional !== void 0 ? optional : {}) {
1475
+ const txKey = optional[paramKey];
1476
+ const paramValue = this.getByPathIgnoreCase(params, paramKey);
1477
+ if (paramValue && paramValue !== '' && paramValue !== 0) {
1478
+ const txValue = ___default.get(tx, txKey);
1479
+ if (paramValue !== txValue) {
1480
+ return this.logAndFail(tx.type, paramKey, paramValue, txValue);
1481
+ }
1482
+ }
1483
+ }
1484
+ return true;
1485
+ };
1486
+ this.checkComplexField = (params, tx, { paramPath, txPath, paramTransform, txTransform }) => {
1487
+ const paramValue = paramTransform(this.getByPathIgnoreCase(params, paramPath));
1488
+ const txValue = txTransform(___default.get(tx, txPath));
1489
+ if (!___default.isEqual(paramValue, txValue)) {
1490
+ return this.logAndFail(tx.type, paramPath, paramValue, txValue);
1491
+ }
1492
+ return true;
1493
+ };
1494
+ }
1495
+ logAndFail(txType, field, expected, actual) {
1496
+ console.warn(`[TxnIntegrityChecker] ${txType} ${field}:`, expected, actual);
1497
+ return { result: false, reason: 'tx integrity check fail', field };
1498
+ }
1499
+ // 속성명의 대소문자를 무시하고 값을 구한다
1500
+ getByPathIgnoreCase(obj, path) {
1501
+ const keys = path.split('.');
1502
+ let current = obj;
1503
+ for (const key of keys) {
1504
+ if (current === null || typeof current !== 'object')
1505
+ return undefined;
1506
+ const actualKey = Object.keys(current).find(k => k.toLowerCase() === key.toLowerCase());
1507
+ if (actualKey === undefined)
1508
+ return undefined;
1509
+ current = current[actualKey];
1510
+ }
1511
+ return current;
1512
+ }
1513
+ }
1514
+
1515
1515
  const loadLocusWasm = load;
1516
1516
  const loadLocusWasmSync = loadSync;
1517
- const LCLIB_NAMESPACE = namespace;
1518
1517
  const txValidator = new TxValidator();
1519
1518
  const verifyTxParams = txValidator.verifyTxParams;
1520
- console.log('LCLIB_NAMESPACE:', LCLIB_NAMESPACE);
1521
1519
  const checkAddressType = (address) => {
1522
1520
  try {
1523
1521
  const hex = convertAddressToHex(address);
@@ -1543,4 +1541,4 @@ const isToken = (addressType) => addressType === AddressClass.UserToken ||
1543
1541
  addressType === AddressClass.AssetNFT;
1544
1542
  const isSystemToken = (addressType) => addressType === AddressClass.UserToken || addressType === AddressClass.ContractToken;
1545
1543
 
1546
- export { verifyMerkleProof as $, encodeTxNumber as A, generateMnemonic as B, generateMnemonicBySeed as C, getDefFromCoreScript as D, getHomeShard as E, getLibraryVersions as F, gzipAndEncode as G, hash as H, isAccount as I, isContract as J, isGrantConsumingTx as K, LCLIB_NAMESPACE as L, isSystemToken as M, isToken as N, loadLocusWasm as O, loadLocusWasmSync as P, loadMasterKeystore as Q, Rpc as R, loadNormalKeystore as S, TX_CREATION_RPC_LIST as T, makeCurrency as U, sign as V, signByMasterKey as W, testCoreScript as X, txValidator as Y, verify as Z, verifyByMasterKey as _, checkAddressType as a, verifyTx as a0, verifyTxParams as a1, __awaiter as a2, compileCoreScript as b, calculateTxLinkHash as c, convertAddressToData as d, convertAddressToHex as e, convertBase32ToData as f, convertBase32ToHex as g, convertCurrency as h, convertDataTo as i, convertDataToAddress as j, convertDataToBase32 as k, convertDataToHex as l, convertDataToString as m, convertHexToAddress as n, convertHexToBase32 as o, convertHexToData as p, convertStringToData as q, convertToData as r, createAccountAndKeystore as s, createMasterKeystore as t, createNormalKey as u, createNormalKeystore as v, decodeTxs as w, deriveKeysFromMnemonic as x, disCompileCoreScript as y, encodeTxCurrency as z };
1544
+ export { verifyTx as $, encodeTxNumber as A, generateMnemonic as B, generateMnemonicBySeed as C, getDefFromCoreScript as D, getHomeShard as E, getLibraryVersions as F, gzipAndEncode as G, hash as H, isAccount as I, isContract as J, isGrantConsumingTx as K, isSystemToken as L, isToken as M, loadLocusWasm as N, loadLocusWasmSync as O, loadMasterKeystore as P, loadNormalKeystore as Q, Rpc as R, makeCurrency as S, TX_CREATION_RPC_LIST as T, sign as U, signByMasterKey as V, testCoreScript as W, txValidator as X, verify as Y, verifyByMasterKey as Z, verifyMerkleProof as _, checkAddressType as a, verifyTxParams as a0, __awaiter as a1, compileCoreScript as b, calculateTxLinkHash as c, convertAddressToData as d, convertAddressToHex as e, convertBase32ToData as f, convertBase32ToHex as g, convertCurrency as h, convertDataTo as i, convertDataToAddress as j, convertDataToBase32 as k, convertDataToHex as l, convertDataToString as m, convertHexToAddress as n, convertHexToBase32 as o, convertHexToData as p, convertStringToData as q, convertToData as r, createAccountAndKeystore as s, createMasterKeystore as t, createNormalKey as u, createNormalKeystore as v, decodeTxs as w, deriveKeysFromMnemonic as x, disCompileCoreScript as y, encodeTxCurrency as z };