@arkecosystem/typescript-crypto 0.0.16 → 0.0.18

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.
Files changed (44) hide show
  1. package/dist/enums/AbiFunction.d.ts +3 -1
  2. package/dist/enums/AbiFunction.d.ts.map +1 -1
  3. package/dist/enums/AbiFunction.js +2 -0
  4. package/dist/index.js +232 -96
  5. package/dist/transactions/builders/TokenTransferBuilder.d.ts +10 -0
  6. package/dist/transactions/builders/TokenTransferBuilder.d.ts.map +1 -0
  7. package/dist/transactions/builders/TokenTransferBuilder.js +25 -0
  8. package/dist/transactions/builders/index.d.ts +1 -0
  9. package/dist/transactions/builders/index.d.ts.map +1 -1
  10. package/dist/transactions/builders/index.js +1 -0
  11. package/dist/types.d.ts +5 -0
  12. package/dist/types.d.ts.map +1 -1
  13. package/dist/utils/AbiEncoder.d.ts +2 -0
  14. package/dist/utils/AbiEncoder.d.ts.map +1 -1
  15. package/dist/utils/AbiEncoder.js +3 -0
  16. package/dist/utils/Helpers.d.ts +8 -0
  17. package/dist/utils/Helpers.d.ts.map +1 -1
  18. package/dist/utils/Helpers.js +12 -0
  19. package/dist/utils/TransactionDataEncoder.d.ts +13 -0
  20. package/dist/utils/TransactionDataEncoder.d.ts.map +1 -0
  21. package/dist/utils/TransactionDataEncoder.js +44 -0
  22. package/dist/utils/TransactionTypeIdentifier.d.ts +24 -0
  23. package/dist/utils/TransactionTypeIdentifier.d.ts.map +1 -0
  24. package/dist/utils/TransactionTypeIdentifier.js +54 -0
  25. package/dist/utils/index.d.ts +2 -0
  26. package/dist/utils/index.d.ts.map +1 -1
  27. package/dist/utils/index.js +2 -0
  28. package/package.json +1 -1
  29. package/src/enums/AbiFunction.ts +2 -0
  30. package/src/transactions/builders/TokenTransferBuilder.ts +35 -0
  31. package/src/transactions/builders/index.ts +1 -0
  32. package/src/types.ts +7 -0
  33. package/src/utils/AbiEncoder.ts +5 -0
  34. package/src/utils/Helpers.ts +15 -0
  35. package/src/utils/TransactionDataEncoder.ts +54 -0
  36. package/src/utils/TransactionTypeIdentifier.ts +65 -0
  37. package/src/utils/index.ts +2 -0
  38. package/tests/fixtures/transactions/token-transfer.json +16 -0
  39. package/tests/fixtures/transactions/transaction-data-encoder.json +15 -0
  40. package/tests/unit/transactions/builders/TokenTransferBuilder.test.ts +138 -0
  41. package/tests/unit/utils/AbiEncoder.test.ts +12 -0
  42. package/tests/unit/utils/Helpers.test.ts +10 -0
  43. package/tests/unit/utils/TransactionDataEncoder.test.ts +51 -0
  44. package/tests/unit/utils/TransactionTypeIdentifier.test.ts +199 -0
@@ -0,0 +1,199 @@
1
+ import { jest } from "@jest/globals";
2
+ import { TransactionTypeIdentifier } from "@/utils/TransactionTypeIdentifier";
3
+ import { ConsensusContract, MultipaymentContract, UsernamesContract } from "@/utils/Abi/json";
4
+ import { Helpers } from "@/utils/Helpers";
5
+
6
+ describe("TransactionTypeIdentifier", () => {
7
+ const TransactionFunctionSigs = {
8
+ MultiPayment: MultipaymentContract.methodIdentifiers["pay(address[],uint256[])"],
9
+ RegisterUsername: UsernamesContract.methodIdentifiers["registerUsername(string)"],
10
+ ResignUsername: UsernamesContract.methodIdentifiers["resignUsername()"],
11
+ RegisterValidator: ConsensusContract.methodIdentifiers["registerValidator(bytes)"],
12
+ ResignValidator: ConsensusContract.methodIdentifiers["resignValidator()"],
13
+ Vote: ConsensusContract.methodIdentifiers["vote(address)"],
14
+ Unvote: ConsensusContract.methodIdentifiers["unvote()"],
15
+ UpdateValidator: ConsensusContract.methodIdentifiers["updateValidator(bytes)"],
16
+ };
17
+
18
+ const InvalidFunctionSignature = "0x1234567";
19
+
20
+ beforeAll(() => {
21
+ jest.mock("@/utils/AbiDecoder", () => {
22
+ return {
23
+ AbiDecoder: jest.fn().mockImplementation(() => ({
24
+ decodeFunctionData: jest.fn().mockReturnValue({ functionName: "transfer" }),
25
+ })),
26
+ };
27
+ });
28
+ });
29
+
30
+ afterAll(() => {
31
+ jest.clearAllMocks();
32
+ });
33
+
34
+ describe("#isTransfer", () => {
35
+ it("should return true for empty transfer string", () => {
36
+ expect(TransactionTypeIdentifier.isTransfer("")).toBe(true);
37
+ });
38
+
39
+ it("should return false for non-empty data", () => {
40
+ expect(TransactionTypeIdentifier.isTransfer(InvalidFunctionSignature)).toBe(false);
41
+ });
42
+ });
43
+
44
+ describe("#isVote", () => {
45
+ it("should return true for matching vote signature", () => {
46
+ expect(TransactionTypeIdentifier.isVote(Helpers.addLeadingHexZero(TransactionFunctionSigs.Vote))).toBe(
47
+ true,
48
+ );
49
+ });
50
+
51
+ it("should return true for matching vote signature without 0x prefix", () => {
52
+ expect(TransactionTypeIdentifier.isVote(TransactionFunctionSigs.Vote)).toBe(true);
53
+ });
54
+
55
+ it("should return false for non-matching signature", () => {
56
+ expect(TransactionTypeIdentifier.isVote(InvalidFunctionSignature)).toBe(false);
57
+ });
58
+ });
59
+
60
+ describe("#isUnvote", () => {
61
+ it("should return true for matching unvote signature", () => {
62
+ expect(TransactionTypeIdentifier.isUnvote(Helpers.addLeadingHexZero(TransactionFunctionSigs.Unvote))).toBe(
63
+ true,
64
+ );
65
+ });
66
+
67
+ it("should return true for matching unvote signature without 0x prefix", () => {
68
+ expect(TransactionTypeIdentifier.isUnvote(TransactionFunctionSigs.Unvote)).toBe(true);
69
+ });
70
+
71
+ it("should return false for non-matching signature", () => {
72
+ expect(TransactionTypeIdentifier.isUnvote(InvalidFunctionSignature)).toBe(false);
73
+ });
74
+ });
75
+
76
+ describe("#isMultiPayment", () => {
77
+ it("should return true for matching multi-payment signature", () => {
78
+ expect(
79
+ TransactionTypeIdentifier.isMultiPayment(
80
+ Helpers.addLeadingHexZero(TransactionFunctionSigs.MultiPayment),
81
+ ),
82
+ ).toBe(true);
83
+ });
84
+
85
+ it("should return true for matching multi-payment signature without 0x prefix", () => {
86
+ expect(TransactionTypeIdentifier.isMultiPayment(TransactionFunctionSigs.MultiPayment)).toBe(true);
87
+ });
88
+
89
+ it("should return false for non-matching signature", () => {
90
+ expect(TransactionTypeIdentifier.isMultiPayment(InvalidFunctionSignature)).toBe(false);
91
+ });
92
+ });
93
+
94
+ describe("#isUsernameRegistration", () => {
95
+ it("should return true for matching registration signature", () => {
96
+ expect(
97
+ TransactionTypeIdentifier.isUsernameRegistration(
98
+ Helpers.addLeadingHexZero(TransactionFunctionSigs.RegisterUsername),
99
+ ),
100
+ ).toBe(true);
101
+ });
102
+
103
+ it("should return true for matching registration signature without 0x prefix", () => {
104
+ expect(TransactionTypeIdentifier.isUsernameRegistration(TransactionFunctionSigs.RegisterUsername)).toBe(
105
+ true,
106
+ );
107
+ });
108
+
109
+ it("should return false for non-matching signature", () => {
110
+ expect(TransactionTypeIdentifier.isUsernameRegistration(InvalidFunctionSignature)).toBe(false);
111
+ });
112
+ });
113
+
114
+ describe("#isUsernameResignation", () => {
115
+ it("should return true for matching resignation signature", () => {
116
+ expect(
117
+ TransactionTypeIdentifier.isUsernameResignation(
118
+ Helpers.addLeadingHexZero(TransactionFunctionSigs.ResignUsername),
119
+ ),
120
+ ).toBe(true);
121
+ });
122
+
123
+ it("should return true for matching resignation signature without 0x prefix", () => {
124
+ expect(TransactionTypeIdentifier.isUsernameResignation(TransactionFunctionSigs.ResignUsername)).toBe(true);
125
+ });
126
+
127
+ it("should return false for non-matching signature", () => {
128
+ expect(TransactionTypeIdentifier.isUsernameResignation(InvalidFunctionSignature)).toBe(false);
129
+ });
130
+ });
131
+
132
+ describe("#isValidatorRegistration", () => {
133
+ it("should return true for matching validator registration signature", () => {
134
+ expect(
135
+ TransactionTypeIdentifier.isValidatorRegistration(
136
+ Helpers.addLeadingHexZero(TransactionFunctionSigs.RegisterValidator),
137
+ ),
138
+ ).toBe(true);
139
+ });
140
+
141
+ it("should return true for matching validator registration signature without 0x prefix", () => {
142
+ expect(TransactionTypeIdentifier.isValidatorRegistration(TransactionFunctionSigs.RegisterValidator)).toBe(
143
+ true,
144
+ );
145
+ });
146
+
147
+ it("should return false for non-matching signature", () => {
148
+ expect(TransactionTypeIdentifier.isValidatorRegistration(InvalidFunctionSignature)).toBe(false);
149
+ });
150
+ });
151
+
152
+ describe("#isValidatorResignation", () => {
153
+ it("should return true for matching validator resignation signature", () => {
154
+ expect(
155
+ TransactionTypeIdentifier.isValidatorResignation(
156
+ Helpers.addLeadingHexZero(TransactionFunctionSigs.ResignValidator),
157
+ ),
158
+ ).toBe(true);
159
+ });
160
+
161
+ it("should return true for matching validator resignation signature without 0x prefix", () => {
162
+ expect(TransactionTypeIdentifier.isValidatorResignation(TransactionFunctionSigs.ResignValidator)).toBe(
163
+ true,
164
+ );
165
+ });
166
+
167
+ it("should return false for non-matching signature", () => {
168
+ expect(TransactionTypeIdentifier.isValidatorResignation(InvalidFunctionSignature)).toBe(false);
169
+ });
170
+ });
171
+
172
+ describe("#isUpdateValidator", () => {
173
+ it("should return true for matching update validator signature", () => {
174
+ expect(
175
+ TransactionTypeIdentifier.isUpdateValidator(
176
+ Helpers.addLeadingHexZero(TransactionFunctionSigs.UpdateValidator),
177
+ ),
178
+ ).toBe(true);
179
+ });
180
+
181
+ it("should return true for matching update validator signature without 0x prefix", () => {
182
+ expect(TransactionTypeIdentifier.isUpdateValidator(TransactionFunctionSigs.UpdateValidator)).toBe(true);
183
+ });
184
+
185
+ it("should return false for non-matching signature", () => {
186
+ expect(TransactionTypeIdentifier.isUpdateValidator(InvalidFunctionSignature)).toBe(false);
187
+ });
188
+ });
189
+
190
+ describe("#isTokenTransfer", () => {
191
+ it("should return true for valid token transfer data", () => {
192
+ expect(TransactionTypeIdentifier.isTokenTransfer("0x" + "0".repeat(64))).toBe(false);
193
+ });
194
+
195
+ it("should return false for invalid data", () => {
196
+ expect(TransactionTypeIdentifier.isTokenTransfer(InvalidFunctionSignature)).toBe(false);
197
+ });
198
+ });
199
+ });