@idos-network/kwil-infra 1.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.
Files changed (58) hide show
  1. package/.turbo/turbo-build.log +47 -0
  2. package/.turbo/turbo-test.log +126 -0
  3. package/.turbo/turbo-typecheck.log +5 -0
  4. package/CHANGELOG.md +20 -0
  5. package/LICENSE +21 -0
  6. package/README.md +63 -0
  7. package/dist/actions/index.d.mts +2 -0
  8. package/dist/actions/index.mjs +1371 -0
  9. package/dist/actions/index.mjs.map +1 -0
  10. package/dist/facesign/index.d.mts +2 -0
  11. package/dist/facesign/index.mjs +2 -0
  12. package/dist/facesign-signer-ADd4EASX.d.mts +29 -0
  13. package/dist/facesign-signer-ADd4EASX.d.mts.map +1 -0
  14. package/dist/facesign-signer-BY9Nti9F.mjs +244 -0
  15. package/dist/facesign-signer-BY9Nti9F.mjs.map +1 -0
  16. package/dist/index-Ci6QhKJo.d.mts +644 -0
  17. package/dist/index-Ci6QhKJo.d.mts.map +1 -0
  18. package/dist/index.d.mts +2 -0
  19. package/dist/index.mjs +429 -0
  20. package/dist/index.mjs.map +1 -0
  21. package/dist/signature-verification/index.d.mts +14 -0
  22. package/dist/signature-verification/index.d.mts.map +1 -0
  23. package/dist/signature-verification/index.mjs +140 -0
  24. package/dist/signature-verification/index.mjs.map +1 -0
  25. package/dist/xrp/utils.d.mts +1165 -0
  26. package/dist/xrp/utils.d.mts.map +1 -0
  27. package/dist/xrp/utils.mjs +105 -0
  28. package/dist/xrp/utils.mjs.map +1 -0
  29. package/dist/xrpl-credentials/index.d.mts +179 -0
  30. package/dist/xrpl-credentials/index.d.mts.map +1 -0
  31. package/dist/xrpl-credentials/index.mjs +224 -0
  32. package/dist/xrpl-credentials/index.mjs.map +1 -0
  33. package/package.json +93 -0
  34. package/src/actions/actions.ts +2197 -0
  35. package/src/actions/index.ts +55 -0
  36. package/src/create-kwil-client.ts +144 -0
  37. package/src/create-kwil-signer.ts +270 -0
  38. package/src/facesign/facesign-signer.ts +327 -0
  39. package/src/facesign/index.ts +2 -0
  40. package/src/index.ts +7 -0
  41. package/src/near/create-near-wallet-kwil-signer.ts +245 -0
  42. package/src/near/get-config.ts +43 -0
  43. package/src/near/nep413.ts +62 -0
  44. package/src/near/signature-verification.ts +85 -0
  45. package/src/signature-verification/evm.ts +33 -0
  46. package/src/signature-verification/facesign.ts +19 -0
  47. package/src/signature-verification/index.test.ts +110 -0
  48. package/src/signature-verification/index.ts +72 -0
  49. package/src/signature-verification/stellar.ts +31 -0
  50. package/src/xrp/signature-verification.ts +18 -0
  51. package/src/xrp/signer.ts +43 -0
  52. package/src/xrp/utils.ts +178 -0
  53. package/src/xrpl-credentials/accept.ts +143 -0
  54. package/src/xrpl-credentials/create.ts +199 -0
  55. package/src/xrpl-credentials/index.ts +12 -0
  56. package/tsconfig.json +33 -0
  57. package/tsdown.config.ts +13 -0
  58. package/vitest.config.ts +6 -0
@@ -0,0 +1,143 @@
1
+ import { hexEncode, utf8Encode } from "@idos-network/utils/codecs";
2
+
3
+ /**
4
+ * Payload structure for XRPL CredentialAccept transaction
5
+ *
6
+ * This type defines the required fields for creating a credential acceptance
7
+ * transaction on the XRP Ledger. The CredentialType field is automatically
8
+ * encoded as a hex string from the UTF-8 representation of the credential type.
9
+ */
10
+ export type CredentialAcceptPayload = {
11
+ /** Transaction type identifier - must be "CredentialAccept" */
12
+ TransactionType: "CredentialAccept";
13
+ /** The issuer's XRPL address who created the Credential object */
14
+ Issuer: string;
15
+ /** The account's XRPL address who is receiving the credential */
16
+ Account: string;
17
+ /** The type of credential being accepted (encoded as hex string) */
18
+ CredentialType: string;
19
+ };
20
+
21
+ /**
22
+ * Creates a credential acceptance payload for XRPL transactions
23
+ *
24
+ * This function constructs a properly formatted payload for credential acceptance
25
+ * transactions. The credential type is automatically encoded from UTF-8 to hex
26
+ * format as required by XRPL specifications.
27
+ *
28
+ * @param issuerAddress - The XRPL address of the credential issuer
29
+ * @param accountAddress - The XRPL address of the account accepting the credential
30
+ * @param credentialType - The human-readable idOS credential type (e.g., "KYC")
31
+ * @returns A properly formatted CredentialAcceptPayload object
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const payload = OriginalCredentialAcceptPayload(
36
+ * "rIssuerAddress123...",
37
+ * "rAccountAddress456...",
38
+ * "KYC"
39
+ * );
40
+ * // Result: {
41
+ * // TransactionType: "CredentialAccept",
42
+ * // Issuer: "rIssuerAddress123...",
43
+ * // Account: "rAccountAddress456...",
44
+ * // CredentialType: "4B5943" // Hex encoded "KYC"
45
+ * // }
46
+ * ```
47
+ */
48
+ export function OriginalCredentialAcceptPayload(
49
+ issuerAddress: string,
50
+ accountAddress: string,
51
+ credentialType: string,
52
+ ): CredentialAcceptPayload {
53
+ // Encode the credential type from UTF-8 to hex format
54
+ // This is required by XRPL for proper transaction processing
55
+ const encodedCredentialType = hexEncode(utf8Encode(credentialType));
56
+
57
+ return {
58
+ TransactionType: "CredentialAccept",
59
+ Issuer: issuerAddress,
60
+ Account: accountAddress,
61
+ CredentialType: encodedCredentialType,
62
+ };
63
+ }
64
+
65
+ /**
66
+ * Creates a copy credential acceptance payload for XRPL transactions
67
+ *
68
+ * This function constructs a payload for accepting a copy of an existing credential
69
+ * with additional metadata including the original issuer and a timelock period.
70
+ * The CredentialType field is encoded as a composite string containing:
71
+ * - "AG-" prefix (indicating copy credential)
72
+ * - Original credential type
73
+ * - Original issuer address
74
+ * - Timelock period in years
75
+ *
76
+ * @param copyCredentialIssuerAddress - The XRPL address of the issuer created a XRPL Credential object related to idOS credential copy
77
+ * @param accountAddress - The XRPL address of the account accepting the credential copy
78
+ * @param originalCredentialIssuerAddress - The XRPL address of the issuer created a XRPL Credential object related to idOS original credential
79
+ * @param credentialType - The human-readable idOScredential type (e.g., "KYC")
80
+ * @param timelockYears - The number of years for the timelock period (must be non-negative integer)
81
+ * @returns A properly formatted CredentialAcceptPayload object for copy credentials
82
+ * @throws {Error} When timelockYears is not a non-negative integer
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const payload = await CopyCredentialAcceptPayload(
87
+ * "rCopyIssuerAddress123...",
88
+ * "rAccountAddress456...",
89
+ * "rOriginalIssuerAddress789...",
90
+ * "KYC",
91
+ * 2
92
+ * );
93
+ * // Result: {
94
+ * // TransactionType: "CredentialAccept",
95
+ * // Issuer: "rCopyIssuerAddress123...",
96
+ * // Account: "rAccountAddress456...",
97
+ * // CredentialType: "41472D4B59432D..." // Hex encoded "AG-KYC-{originalIssuer}-2Y"
98
+ * // }
99
+ * ```
100
+ */
101
+ export async function CopyCredentialAcceptPayload(
102
+ copyCredentialIssuerAddress: string,
103
+ accountAddress: string,
104
+ originalCredentialIssuerAddress: string,
105
+ credentialType: string,
106
+ timelockYears: number,
107
+ ): Promise<CredentialAcceptPayload> {
108
+ // Validate that timelockYears is a non-negative integer
109
+ if (!Number.isInteger(timelockYears) || timelockYears < 0) {
110
+ throw new Error("timelockYears must be a non-negative integer");
111
+ }
112
+
113
+ let decodeAccountID: typeof import("xrpl").decodeAccountID;
114
+ try {
115
+ ({ decodeAccountID } = await import("xrpl"));
116
+ } catch {
117
+ throw new Error(
118
+ 'Missing optional peer dependency "xrpl". Install it to use CopyCredentialAcceptPayload.',
119
+ );
120
+ }
121
+
122
+ // Create a composite credential type string that includes:
123
+ // - "AG-" prefix (indicating this is a copy credential)
124
+ // - Original credential type
125
+ // - Original issuer Account ID (decoded from XRPL address format)
126
+ // - Timelock period in years
127
+ const type = Buffer.concat([
128
+ utf8Encode("AG"),
129
+ utf8Encode("-"),
130
+ utf8Encode(credentialType),
131
+ utf8Encode("-"),
132
+ decodeAccountID(originalCredentialIssuerAddress),
133
+ utf8Encode("-"),
134
+ utf8Encode(`${timelockYears}Y`),
135
+ ]);
136
+
137
+ return {
138
+ TransactionType: "CredentialAccept",
139
+ Issuer: copyCredentialIssuerAddress,
140
+ Account: accountAddress,
141
+ CredentialType: hexEncode(type),
142
+ };
143
+ }
@@ -0,0 +1,199 @@
1
+ import type { Client, TxResponse, Wallet } from "xrpl";
2
+
3
+ import { hexEncode, utf8Encode } from "@idos-network/utils/codecs";
4
+
5
+ /**
6
+ * Parameters for creating an original credential on XRPL
7
+ */
8
+ export type CreateCredentialForOriginalParams = {
9
+ /** Unique identifier for the idOS credential, like `741a9caf-ec53-42c7-aed6-519950dcded5` */
10
+ credId: string;
11
+ /** Type/category of the credential, like `KYC` */
12
+ credType: string;
13
+ /** XRPL address of the user receiving the credential, like `rPT1Sjq2YGrBMTttX4GZHjKu9dyf6bpAYe` */
14
+ userAddress: string;
15
+ };
16
+
17
+ /**
18
+ * Parameters for creating a time-locked copy credential on XRPL
19
+ */
20
+ export type CreateCredentialForCopyParams = {
21
+ /** Unique identifier for the idOS credential */
22
+ credId: string;
23
+ /** Type/category of the credential */
24
+ credType: string;
25
+ /** XRPL address of the user receiving the credential */
26
+ userAddress: string;
27
+ /** Number of years to lock the credential (must be non-negative integer) */
28
+ timelockYears: number;
29
+ /** XRPL address of the original credential issuer */
30
+ origCredIssuerAddress: string;
31
+ };
32
+
33
+ /**
34
+ * Service class for creating credentials on the XRPL blockchain
35
+ *
36
+ * This service provides methods to create both original and copy credentials
37
+ * on the XRPL. It handles connection management, transaction submission,
38
+ * and proper encoding of credential data according to XRPL specifications.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const wallet = Wallet.fromSeed("s...");
43
+ * const xrplService = await XrplCredentialsCreate.init("wss://s.devnet.rippletest.net:51233", wallet);
44
+ *
45
+ * // Create original credential
46
+ * await xrplService.createCredentialForOriginal({
47
+ * credId: "741a9caf-ec53-42c7-aed6-519950dcded5",
48
+ * credType: "KYC",
49
+ * userAddress: "rPT1Sjq2YGrBMTttX4GZHjKu9dyf6bpAYe"
50
+ * });
51
+ * ```
52
+ */
53
+ export class XrplCredentialsCreate {
54
+ readonly #client: Client;
55
+ readonly #wallet: Wallet;
56
+ readonly #xrpl: typeof import("xrpl");
57
+
58
+ constructor(nodeUrl: string, wallet: Wallet, xrpl: typeof import("xrpl")) {
59
+ this.#xrpl = xrpl;
60
+ this.#wallet = wallet;
61
+ this.#client = new this.#xrpl.Client(nodeUrl);
62
+ }
63
+
64
+ /**
65
+ * Creates a new XRPL credentials service instance.
66
+ *
67
+ * @param nodeUrl - The WebSocket URL of the XRPL node to connect to
68
+ * @param wallet - The XRPL wallet to use for signing transactions
69
+ */
70
+ static async init(nodeUrl: string, wallet: Wallet): Promise<XrplCredentialsCreate> {
71
+ let xrpl: typeof import("xrpl");
72
+
73
+ try {
74
+ xrpl = await import("xrpl");
75
+ } catch {
76
+ throw new Error(
77
+ "Missing optional peer dependency 'xrpl'. Install it to use XrplCredentialsCreate.",
78
+ );
79
+ }
80
+
81
+ return new XrplCredentialsCreate(nodeUrl, wallet, xrpl);
82
+ }
83
+
84
+ /**
85
+ * Creates a credential on the XRPL that reflects an idOS original credential.
86
+ *
87
+ * This method creates a new credential with the specified type and assigns it to the given user.
88
+ * The credential type is encoded as a hex string, and the credential ID is encoded as a hex URI.
89
+ *
90
+ * @param params - Parameters for creating the original credential
91
+ * @returns Promise resolving to the XRPL transaction result
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * await xrplService.createCredentialForOriginal({
96
+ * credId: "741a9caf-ec53-42c7-aed6-519950dcded5",
97
+ * credType: "KYC",
98
+ * userAddress: "rPT1Sjq2YGrBMTttX4GZHjKu9dyf6bpAYe"
99
+ * });
100
+ * ```
101
+ */
102
+ async createCredentialForOriginal(
103
+ params: CreateCredentialForOriginalParams,
104
+ ): Promise<TxResponse> {
105
+ const { credId, credType, userAddress } = params;
106
+
107
+ // Connect to the XRPL node
108
+ await this.#client.connect();
109
+
110
+ // Submit the credential creation transaction and wait for confirmation
111
+ const res = await this.#client.submitAndWait(
112
+ {
113
+ TransactionType: "CredentialCreate",
114
+ Account: this.#wallet.address,
115
+ Subject: userAddress,
116
+ // Encode credential type as hex string (required by XRPL)
117
+ CredentialType: hexEncode(utf8Encode(credType)),
118
+ // Encode credential ID as hex URI (required by XRPL)
119
+ URI: hexEncode(utf8Encode(credId)),
120
+ },
121
+ {
122
+ wallet: this.#wallet,
123
+ },
124
+ );
125
+
126
+ // Disconnect from the XRPL node
127
+ await this.#client.disconnect();
128
+
129
+ return res;
130
+ }
131
+
132
+ /**
133
+ * Creates a time-locked credential copy on the XRPL.
134
+ *
135
+ * This method creates a Credential object in XRPL that reflects an idOS credential copy.
136
+ * The credential type includes metadata about the original issuer and time-lock duration.
137
+ * The composite credential type follows the format: "AG-{type}-{originalIssuer}-{timelock}Y"
138
+ *
139
+ * @param params - Parameters for creating the time-locked credential copy
140
+ * @returns Promise resolving to the XRPL transaction result
141
+ *
142
+ * @throws {Error} When timelockYears is not a non-negative integer
143
+ *
144
+ * @example
145
+ * ```typescript
146
+ * const result = await xrplService.createCredentialForCopy({
147
+ * credId: "741a9caf-ec53-42c7-aed6-519950dcded5",
148
+ * credType: "KYC",
149
+ * userAddress: "rPT1Sjq2YGrBMTttX4GZHjKu9dyf6bpAYe",
150
+ * timelockYears: 5,
151
+ * origCredIssuerAddress: "rU9C67bZ3ZvjXaJYwYqMqjMq1o4ZbwK3YN"
152
+ * });
153
+ * ```
154
+ */
155
+ async createCredentialForCopy(params: CreateCredentialForCopyParams): Promise<TxResponse> {
156
+ const { credId, credType, userAddress, timelockYears, origCredIssuerAddress } = params;
157
+
158
+ // Validate timelockYears is a non-negative integer
159
+ if (!Number.isInteger(timelockYears) || timelockYears < 0) {
160
+ throw new Error("timelockYears must be a non-negative integer");
161
+ }
162
+
163
+ // Connect to the XRPL node
164
+ await this.#client.connect();
165
+
166
+ // Create composite credential type with metadata:
167
+ // Format: "AG-{credType}-{originalIssuer}-{timelock}Y"
168
+ const type = Buffer.concat([
169
+ utf8Encode("AG"), // "AG" prefix for copy credentials
170
+ utf8Encode("-"), // Separator
171
+ utf8Encode(credType), // Original credential type
172
+ utf8Encode("-"), // Separator
173
+ this.#xrpl.decodeAccountID(origCredIssuerAddress), // Original issuer address (decoded)
174
+ utf8Encode("-"), // Separator
175
+ utf8Encode(`${timelockYears}Y`), // Timelock period in years
176
+ ]);
177
+
178
+ // Submit the credential copy creation transaction and wait for confirmation
179
+ const res = await this.#client.submitAndWait(
180
+ {
181
+ TransactionType: "CredentialCreate",
182
+ Account: this.#wallet.address,
183
+ Subject: userAddress,
184
+ // Use the composite credential type with metadata
185
+ CredentialType: hexEncode(type),
186
+ // Encode credential ID as hex URI (required by XRPL)
187
+ URI: hexEncode(utf8Encode(credId)),
188
+ },
189
+ {
190
+ wallet: this.#wallet,
191
+ },
192
+ );
193
+
194
+ // Disconnect from the XRPL node
195
+ await this.#client.disconnect();
196
+
197
+ return res;
198
+ }
199
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * XRPL Credentials Module
3
+ *
4
+ * This module provides utilities for creating and managing XRPL credential transactions.
5
+ * It includes functionality for:
6
+ * - Creating credentials on the XRPL blockchain
7
+ * - Generating credential acceptance payloads
8
+ * - Managing both original and copy credentials
9
+ */
10
+
11
+ export * from "./accept";
12
+ export * from "./create";
package/tsconfig.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "lib": ["DOM", "ESNext"],
5
+ "module": "ESNext",
6
+ "skipLibCheck": true,
7
+
8
+ // Bundler mode
9
+ "moduleResolution": "bundler",
10
+ "allowImportingTsExtensions": true,
11
+ "resolveJsonModule": true,
12
+ "isolatedModules": true,
13
+ "noEmit": false,
14
+ "emitDeclarationOnly": true,
15
+
16
+ // Output configuration
17
+ "declaration": true,
18
+ "declarationMap": true,
19
+ "declarationDir": "./dist/types",
20
+ "isolatedDeclarations": true,
21
+ "sourceMap": true,
22
+ "inlineSources": true,
23
+
24
+ // Linting
25
+ "strict": true,
26
+ "noUnusedLocals": true,
27
+ "noUnusedParameters": true,
28
+ "noFallthroughCasesInSwitch": true,
29
+ "forceConsistentCasingInFileNames": true,
30
+ "esModuleInterop": true
31
+ },
32
+ "include": ["./src/**/*"]
33
+ }
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from "tsdown";
2
+
3
+ export default defineConfig({
4
+ dts: true,
5
+ entry: [
6
+ "./src/index.ts",
7
+ "./src/actions/index.ts",
8
+ "./src/signature-verification/index.ts",
9
+ "./src/xrp/utils.ts",
10
+ "./src/facesign/index.ts",
11
+ "./src/xrpl-credentials/index.ts",
12
+ ],
13
+ });
@@ -0,0 +1,6 @@
1
+ import { defineConfig } from "vitest/config";
2
+ export default defineConfig({
3
+ test: {
4
+ environment: "jsdom",
5
+ },
6
+ });