@arkecosystem/typescript-crypto 0.0.13 → 0.0.14

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.
package/dist/index.js CHANGED
@@ -27810,6 +27810,17 @@ var AbiDecoder = class extends AbiBase {
27810
27810
  args: this.interface.decodeFunctionData(functionFragment, data)
27811
27811
  };
27812
27812
  }
27813
+ decodeError(data) {
27814
+ if (!data.startsWith("0x")) {
27815
+ data = "0x" + data;
27816
+ }
27817
+ const errorSelector = data.slice(0, 10);
27818
+ const errorFragment = this.interface.getError(errorSelector);
27819
+ if (!errorFragment) {
27820
+ throw new Error(`Error selector not found in ABI: ${errorSelector}`);
27821
+ }
27822
+ return errorFragment.name;
27823
+ }
27813
27824
  };
27814
27825
 
27815
27826
  // src/utils/AbiEncoder.ts
@@ -2,5 +2,6 @@ import { AbiResult } from "@/types";
2
2
  import { AbiBase } from "./AbiBase";
3
3
  export declare class AbiDecoder extends AbiBase {
4
4
  decodeFunctionData(data: string): AbiResult;
5
+ decodeError(data: string): string;
5
6
  }
6
7
  //# sourceMappingURL=AbiDecoder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AbiDecoder.d.ts","sourceRoot":"","sources":["../../src/utils/AbiDecoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,qBAAa,UAAW,SAAQ,OAAO;IACtC,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;CAiB3C"}
1
+ {"version":3,"file":"AbiDecoder.d.ts","sourceRoot":"","sources":["../../src/utils/AbiDecoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,qBAAa,UAAW,SAAQ,OAAO;IACtC,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;IAkB3C,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;CAcjC"}
@@ -14,4 +14,15 @@ export class AbiDecoder extends AbiBase {
14
14
  args: this.interface.decodeFunctionData(functionFragment, data),
15
15
  };
16
16
  }
17
+ decodeError(data) {
18
+ if (!data.startsWith("0x")) {
19
+ data = "0x" + data;
20
+ }
21
+ const errorSelector = data.slice(0, 10);
22
+ const errorFragment = this.interface.getError(errorSelector);
23
+ if (!errorFragment) {
24
+ throw new Error(`Error selector not found in ABI: ${errorSelector}`);
25
+ }
26
+ return errorFragment.name;
27
+ }
17
28
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkecosystem/typescript-crypto",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "engines": {
5
5
  "node": ">=20.12.2"
6
6
  },
@@ -19,4 +19,19 @@ export class AbiDecoder extends AbiBase {
19
19
  args: this.interface.decodeFunctionData(functionFragment, data),
20
20
  };
21
21
  }
22
+
23
+ decodeError(data: string): string {
24
+ if (!data.startsWith("0x")) {
25
+ data = "0x" + data;
26
+ }
27
+
28
+ const errorSelector = data.slice(0, 10);
29
+
30
+ const errorFragment = this.interface.getError(errorSelector);
31
+ if (!errorFragment) {
32
+ throw new Error(`Error selector not found in ABI: ${errorSelector}`);
33
+ }
34
+
35
+ return errorFragment.name;
36
+ }
22
37
  }
@@ -44,3 +44,32 @@ it("should decode using a custom abi", () => {
44
44
  expect(decodedData.args[0]).toEqual(args[0]);
45
45
  expect(decodedData.args[1].map((value: BigInt) => value.toString())).toEqual(args[1]);
46
46
  });
47
+
48
+ it("should decode error data and return error name", () => {
49
+ const decoder = new AbiDecoder(ContractAbiType.USERNAMES);
50
+
51
+ const errorData = "0xa0ca2f4e";
52
+
53
+ const errorName = decoder.decodeError(errorData);
54
+
55
+ expect(errorName).toBe("TakenUsername");
56
+ });
57
+
58
+ it("should decode error data without 0x prefix", () => {
59
+ const decoder = new AbiDecoder(ContractAbiType.USERNAMES);
60
+
61
+ // Same as above but without 0x prefix
62
+ const errorData = "a0ca2f4e";
63
+
64
+ const errorName = decoder.decodeError(errorData);
65
+
66
+ expect(errorName).toBe("TakenUsername");
67
+ });
68
+
69
+ it("should throw error for unknown error selector", () => {
70
+ const decoder = new AbiDecoder(ContractAbiType.USERNAMES);
71
+
72
+ const errorData = "0x12345678";
73
+
74
+ expect(() => decoder.decodeError(errorData)).toThrow("Error selector not found in ABI: 0x12345678");
75
+ });