dhali-js 2.0.0 → 2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dhali-js",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A JavaScript library for managing XRPL payment channels and generating auth tokens for Dhali APIs",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Wraps a base64 encoded claim and a base64 encoded requirement into an x402 compliant payload.
3
+ *
4
+ * @param {string} claimBase64 - The base64 encoded claim object.
5
+ * @param {string} paymentRequirementBase64 - The base64 encoded payment requirement object.
6
+ * @returns {string} The base64 encoded x402 payment payload.
7
+ */
8
+ function wrapAsX402PaymentPayload(claimBase64, paymentRequirementBase64) {
9
+ const decodedClaim = JSON.parse(
10
+ Buffer.from(claimBase64, "base64").toString("utf-8")
11
+ );
12
+ let req = JSON.parse(
13
+ Buffer.from(paymentRequirementBase64, "base64").toString("utf-8")
14
+ );
15
+
16
+ if (req.accepts) {
17
+ req = Array.isArray(req.accepts) ? req.accepts[0] : req.accepts;
18
+ }
19
+
20
+ // Normalize fields to match Dhali-wallet's PaymentRequirements defaults (camelCase)
21
+ const normalizedReq = {
22
+ scheme: req.scheme || "",
23
+ network: req.network || "",
24
+ asset: req.asset || (req.price ? req.price.asset : "") || "",
25
+ amount: String(req.amount || (req.price ? req.price.amount : "0")),
26
+ payTo: req.payTo || req.pay_to || "",
27
+ maxTimeoutSeconds: parseInt(
28
+ req.maxTimeoutSeconds || req.max_timeout_seconds || 1209600
29
+ ),
30
+ extra: req.extra || {},
31
+ };
32
+
33
+ const x402Payload = {
34
+ x402Version: 2,
35
+ payload: decodedClaim,
36
+ accepted: normalizedReq,
37
+ };
38
+
39
+ return Buffer.from(JSON.stringify(x402Payload)).toString("base64");
40
+ }
41
+
42
+ module.exports = {
43
+ wrapAsX402PaymentPayload,
44
+ };
package/src/index.js CHANGED
@@ -3,6 +3,7 @@ const { DhaliXrplChannelManager, ChannelNotFound } = require("./dhali/DhaliXrplC
3
3
  const { DhaliEthChannelManager } = require("./dhali/DhaliEthChannelManager");
4
4
  const { Currency } = require("./dhali/Currency");
5
5
  const { getAvailableDhaliCurrencies } = require("./dhali/configUtils");
6
+ const { wrapAsX402PaymentPayload } = require("./dhali/utils");
6
7
 
7
8
  module.exports = {
8
9
  DhaliChannelManager,
@@ -10,5 +11,6 @@ module.exports = {
10
11
  DhaliEthChannelManager,
11
12
  ChannelNotFound,
12
13
  Currency,
13
- getAvailableDhaliCurrencies
14
+ getAvailableDhaliCurrencies,
15
+ wrapAsX402PaymentPayload
14
16
  };
@@ -0,0 +1,70 @@
1
+ const { wrapAsX402PaymentPayload } = require("../src/dhali/utils");
2
+
3
+ describe("wrapAsX402PaymentPayload", () => {
4
+ const mockClaim = {
5
+ version: "2",
6
+ account: "rAccount",
7
+ protocol: "XRPL.MAINNET",
8
+ currency: { code: "XRP", scale: 6 },
9
+ destination_account: "rDest",
10
+ authorized_to_claim: "1000000",
11
+ channel_id: "chan123",
12
+ signature: "sig123",
13
+ };
14
+ const claimBase64 = Buffer.from(JSON.stringify(mockClaim)).toString("base64");
15
+
16
+ const mockReqFull = {
17
+ accepts: [
18
+ {
19
+ scheme: "dhali",
20
+ network: "xrpl:0",
21
+ asset: "xrpl:0/native:xrp",
22
+ amount: "70",
23
+ pay_to: "rLggTEwmTe3eJgyQbCSk4wQazow2TeKrtR",
24
+ max_timeout_seconds: 1209600,
25
+ extra: {},
26
+ },
27
+ ],
28
+ };
29
+ const reqFullBase64 = Buffer.from(JSON.stringify(mockReqFull)).toString(
30
+ "base64"
31
+ );
32
+
33
+ const mockReqDhaliApp = {
34
+ scheme: "dhali",
35
+ network: "eip155:1",
36
+ payTo: "0x3D85634D9EA2854F4276eE5372Bf32Eb4ACDbf77",
37
+ price: {
38
+ amount: "10000000000",
39
+ asset: "eip155:1/erc20:0x8292Bb45bf1Ee4d140127049757C2E0fF06317eD",
40
+ },
41
+ maxTimeoutSeconds: 1209600,
42
+ extra: {},
43
+ };
44
+ const reqDhaliAppBase64 = Buffer.from(JSON.stringify(mockReqDhaliApp)).toString(
45
+ "base64"
46
+ );
47
+
48
+ test("should wrap claim and full requirements correctly", () => {
49
+ const resultBase64 = wrapAsX402PaymentPayload(claimBase64, reqFullBase64);
50
+ const result = JSON.parse(Buffer.from(resultBase64, "base64").toString());
51
+
52
+ expect(result.x402Version).toBe(2);
53
+ expect(result.payload).toEqual(mockClaim);
54
+ expect(result.accepted.amount).toBe("70");
55
+ expect(result.accepted.payTo).toBe("rLggTEwmTe3eJgyQbCSk4wQazow2TeKrtR");
56
+ });
57
+
58
+ test("should wrap and normalize Dhali-app style requirements correctly", () => {
59
+ const resultBase64 = wrapAsX402PaymentPayload(claimBase64, reqDhaliAppBase64);
60
+ const result = JSON.parse(Buffer.from(resultBase64, "base64").toString());
61
+
62
+ expect(result.accepted.amount).toBe("10000000000");
63
+ expect(result.accepted.asset).toBe(
64
+ "eip155:1/erc20:0x8292Bb45bf1Ee4d140127049757C2E0fF06317eD"
65
+ );
66
+ expect(result.accepted.payTo).toBe(
67
+ "0x3D85634D9EA2854F4276eE5372Bf32Eb4ACDbf77"
68
+ );
69
+ });
70
+ });