@xandeum/web3.js 0.4.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 (119) hide show
  1. package/LICENSE +51 -0
  2. package/README.md +167 -0
  3. package/archbee.yaml +55 -0
  4. package/dist/armageddon.d.ts +10 -0
  5. package/dist/armageddon.js +77 -0
  6. package/dist/assignCoowner.d.ts +12 -0
  7. package/dist/assignCoowner.js +81 -0
  8. package/dist/bigbang.d.ts +9 -0
  9. package/dist/bigbang.js +76 -0
  10. package/dist/const.d.ts +1 -0
  11. package/dist/const.js +4 -0
  12. package/dist/copyPath.d.ts +13 -0
  13. package/dist/copyPath.js +86 -0
  14. package/dist/createDirectory.d.ts +12 -0
  15. package/dist/createDirectory.js +84 -0
  16. package/dist/createFile.d.ts +13 -0
  17. package/dist/createFile.js +85 -0
  18. package/dist/exists.d.ts +20 -0
  19. package/dist/exists.js +86 -0
  20. package/dist/find.d.ts +22 -0
  21. package/dist/find.js +88 -0
  22. package/dist/getInfo.d.ts +12 -0
  23. package/dist/getInfo.js +77 -0
  24. package/dist/getMetadata.d.ts +23 -0
  25. package/dist/getMetadata.js +89 -0
  26. package/dist/getXandeumResult.d.ts +15 -0
  27. package/dist/getXandeumResult.js +95 -0
  28. package/dist/index.d.ts +16 -0
  29. package/dist/index.js +37 -0
  30. package/dist/listDirectoryEntery.d.ts +21 -0
  31. package/dist/listDirectoryEntery.js +87 -0
  32. package/dist/move.d.ts +14 -0
  33. package/dist/move.js +88 -0
  34. package/dist/peek.d.ts +15 -0
  35. package/dist/peek.js +88 -0
  36. package/dist/poke.d.ts +14 -0
  37. package/dist/poke.js +93 -0
  38. package/dist/removeDirectory.d.ts +12 -0
  39. package/dist/removeDirectory.js +82 -0
  40. package/dist/removeFile.d.ts +12 -0
  41. package/dist/removeFile.js +82 -0
  42. package/dist/renamePath.d.ts +13 -0
  43. package/dist/renamePath.js +85 -0
  44. package/dist/sanitizePath.d.ts +14 -0
  45. package/dist/sanitizePath.js +60 -0
  46. package/dist/webSocket.d.ts +47 -0
  47. package/dist/webSocket.js +103 -0
  48. package/docs/html/.nojekyll +1 -0
  49. package/docs/html/assets/hierarchy.js +1 -0
  50. package/docs/html/assets/highlight.css +92 -0
  51. package/docs/html/assets/icons.js +18 -0
  52. package/docs/html/assets/icons.svg +1 -0
  53. package/docs/html/assets/main.js +60 -0
  54. package/docs/html/assets/navigation.js +1 -0
  55. package/docs/html/assets/search.js +1 -0
  56. package/docs/html/assets/style.css +1640 -0
  57. package/docs/html/functions/armageddon.html +6 -0
  58. package/docs/html/functions/bigbang.html +5 -0
  59. package/docs/html/functions/copyPath.html +8 -0
  60. package/docs/html/functions/createDirectory.html +8 -0
  61. package/docs/html/functions/createFile.html +9 -0
  62. package/docs/html/functions/exists.html +8 -0
  63. package/docs/html/functions/getMetadata.html +10 -0
  64. package/docs/html/functions/getXandeumResult.html +9 -0
  65. package/docs/html/functions/listDirectoryEntry.html +9 -0
  66. package/docs/html/functions/move.html +9 -0
  67. package/docs/html/functions/peek.html +10 -0
  68. package/docs/html/functions/poke.html +10 -0
  69. package/docs/html/functions/removeDirectory.html +8 -0
  70. package/docs/html/functions/removeFile.html +8 -0
  71. package/docs/html/functions/renamePath.html +9 -0
  72. package/docs/html/functions/subscribeResult.html +17 -0
  73. package/docs/html/functions/unsubscribeResult.html +6 -0
  74. package/docs/html/hierarchy.html +1 -0
  75. package/docs/html/index.html +62 -0
  76. package/docs/html/modules.html +1 -0
  77. package/docs/markdown/.nojekyll +0 -0
  78. package/docs/markdown/README.md +169 -0
  79. package/docs/markdown/functions/armageddon.md +34 -0
  80. package/docs/markdown/functions/bigbang.md +27 -0
  81. package/docs/markdown/functions/copyPath.md +49 -0
  82. package/docs/markdown/functions/createDirectory.md +49 -0
  83. package/docs/markdown/functions/createFile.md +50 -0
  84. package/docs/markdown/functions/exists.md +37 -0
  85. package/docs/markdown/functions/getMetadata.md +39 -0
  86. package/docs/markdown/functions/getXandeumResult.md +38 -0
  87. package/docs/markdown/functions/listDirectoryEntry.md +38 -0
  88. package/docs/markdown/functions/move.md +55 -0
  89. package/docs/markdown/functions/peek.md +57 -0
  90. package/docs/markdown/functions/poke.md +56 -0
  91. package/docs/markdown/functions/removeDirectory.md +44 -0
  92. package/docs/markdown/functions/removeFile.md +44 -0
  93. package/docs/markdown/functions/renamePath.md +50 -0
  94. package/docs/markdown/functions/subscribeResult.md +60 -0
  95. package/docs/markdown/functions/unsubscribeResult.md +34 -0
  96. package/docs/markdown/globals.md +25 -0
  97. package/docs/markdown/summary.md +44 -0
  98. package/package.json +26 -0
  99. package/src/armageddon.ts +36 -0
  100. package/src/bigbang.ts +32 -0
  101. package/src/const.ts +1 -0
  102. package/src/copyPath.ts +51 -0
  103. package/src/createDirectory.ts +47 -0
  104. package/src/createFile.ts +47 -0
  105. package/src/exists.ts +47 -0
  106. package/src/getMetadata.ts +50 -0
  107. package/src/getXandeumResult.ts +48 -0
  108. package/src/index.ts +16 -0
  109. package/src/listDirectoryEntery.ts +51 -0
  110. package/src/move.ts +55 -0
  111. package/src/peek.ts +52 -0
  112. package/src/poke.ts +59 -0
  113. package/src/removeDirectory.ts +43 -0
  114. package/src/removeFile.ts +44 -0
  115. package/src/renamePath.ts +50 -0
  116. package/src/sanitizePath.ts +69 -0
  117. package/src/webSocket.ts +131 -0
  118. package/tsconfig.json +14 -0
  119. package/typedoc.json +26 -0
@@ -0,0 +1,44 @@
1
+ import { Transaction, TransactionInstruction, PublicKey } from '@solana/web3.js'
2
+ import BN from 'bn.js'
3
+ import { programId } from './const'
4
+ import { sanitizePath } from './sanitizePath'
5
+
6
+ /**
7
+ * Constructs a Solana transaction to remove a file from a file system,
8
+ * identified by a file system ID (`fsid`) and a UTF-8 encoded file path.
9
+ *
10
+ * @param fsid - A stringified integer representing the file system ID in which the file resides.
11
+ * @param path - The full path to the file to be deleted.
12
+ * @param wallet - The public key of the wallet that signs and authorizes the transaction.
13
+ * @returns A Promise that resolves to a Solana `Transaction` object containing the remove file instruction.
14
+ * @throws May throw an error if `path` is invalid per `sanitizePath`.
15
+ */
16
+
17
+ export async function removeFile (
18
+ fsid: string,
19
+ path: string,
20
+ wallet: PublicKey
21
+ ): Promise<Transaction> {
22
+ sanitizePath(path)
23
+
24
+ const instructionData = Buffer.concat([
25
+ Buffer.from(Int8Array.from([5]).buffer),
26
+ Buffer.from(Uint8Array.of(...new BN(fsid).toArray('le', 8))),
27
+ Buffer.from(`${path}`, 'utf-8')
28
+ ])
29
+
30
+ const instruction = new TransactionInstruction({
31
+ keys: [
32
+ {
33
+ pubkey: wallet,
34
+ isSigner: true,
35
+ isWritable: true
36
+ }
37
+ ],
38
+ programId: new PublicKey(programId),
39
+ data: instructionData
40
+ })
41
+
42
+ const tx = new Transaction().add(instruction)
43
+ return tx
44
+ }
@@ -0,0 +1,50 @@
1
+ import { Transaction, TransactionInstruction, PublicKey } from '@solana/web3.js'
2
+ import BN from 'bn.js'
3
+ import { programId } from './const'
4
+ import { sanitizePath } from './sanitizePath'
5
+
6
+ /**
7
+ * Constructs a Solana transaction to rename (or move) a file or directory
8
+ * within a file system, based on a provided file system ID (`fsid`).
9
+ *
10
+ * @param fsid - A stringified integer representing the file system ID where the path exists.
11
+ * @param oldPath - The current path of the file or directory to be renamed or moved.
12
+ * @param name - The new name to assign to the file or directory.
13
+ * @param wallet - The public key of the wallet that signs and authorizes the transaction.
14
+ * @returns A Promise that resolves to a Solana `Transaction` object containing the rename path instruction.
15
+ * @throws May throw an error if either `oldPath` or `newPath` is invalid per `sanitizePath`.
16
+ */
17
+
18
+ export async function renamePath (
19
+ fsid: string,
20
+ oldPath: string,
21
+ name: string,
22
+ wallet: PublicKey
23
+ ): Promise<Transaction> {
24
+ sanitizePath(oldPath)
25
+
26
+ sanitizePath(name)
27
+
28
+ const rest = Buffer.from(`${oldPath}\0${name}`, 'utf-8')
29
+
30
+ const instructionData = Buffer.concat([
31
+ Buffer.from(Int8Array.from([8]).buffer),
32
+ Buffer.from(Uint8Array.of(...new BN(fsid).toArray('le', 8))),
33
+ rest
34
+ ])
35
+
36
+ const instruction = new TransactionInstruction({
37
+ keys: [
38
+ {
39
+ pubkey: wallet,
40
+ isSigner: true,
41
+ isWritable: true
42
+ }
43
+ ],
44
+ programId: new PublicKey(programId),
45
+ data: instructionData
46
+ })
47
+
48
+ const tx = new Transaction().add(instruction)
49
+ return tx
50
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Sanitizes file path for validate path.
3
+ * Ensures the path contains only allowed characters, prevents path traversal,
4
+ * and normalizes the path to start with a single slash.
5
+ *
6
+ * Allowed characters: alphanumeric, '/', '_', '-', '.'
7
+ * Disallowed: consecutive slashes (//), path traversal (..), control characters,
8
+ * invalid filesystem characters (<, >, :, ", \, |, ?, *),
9
+ * empty or whitespace-only paths.
10
+ *
11
+ * @param path - The file path to sanitize.
12
+ * @throws Error if the path is invalid.
13
+ */
14
+ export function sanitizePath(path: string): void {
15
+ // Check for empty or whitespace-only paths
16
+ if (!path || path.trim() === "") {
17
+ throw new Error("Path cannot be empty or whitespace-only");
18
+ }
19
+
20
+ // Check for consecutive slashes (// or more)
21
+ if (/\/\/+/.test(path)) {
22
+ throw new Error("Consecutive slashes (//) are not allowed");
23
+ }
24
+
25
+ // Check for path traversal sequences (..)
26
+ if (path.includes("..")) {
27
+ throw new Error("Path traversal sequences (..) are not allowed");
28
+ }
29
+
30
+ // Check for control characters (ASCII 0-31 and 127)
31
+ for (let i = 0; i < path.length; i++) {
32
+ const code = path.charCodeAt(i);
33
+ if (code <= 31 || code === 127) {
34
+ throw new Error("Control characters are not allowed in paths");
35
+ }
36
+ }
37
+
38
+ // Check for allowed characters: alphanumeric, /, _, -, .
39
+ if (!/^[a-zA-Z0-9\/_\-\.]*$/.test(path)) {
40
+ throw new Error(
41
+ "Path must contain only alphanumeric characters, single slashes, underscores (_), hyphens (-), or periods (.)"
42
+ );
43
+ }
44
+
45
+ // Check for invalid filesystem characters
46
+ const invalidChars = ["<", ">", ":", '"', "\\", "|", "?", "*"];
47
+ for (let i = 0; i < path.length; i++) {
48
+ if (invalidChars.includes(path[i])) {
49
+ throw new Error(
50
+ 'Path contains invalid filesystem characters (<, >, :, ", \\, |, ?, *)'
51
+ );
52
+ }
53
+ }
54
+
55
+
56
+ // Check path component length (max 255 bytes)
57
+ const components = path.split("/").filter((c) => c.length > 0);
58
+ for (const component of components) {
59
+ if (new TextEncoder().encode(component).length > 255) {
60
+ throw new Error("Path component exceeds 255 bytes");
61
+ }
62
+ }
63
+
64
+ // Check total path length (max 4096 bytes)
65
+ if (new TextEncoder().encode(path).length > 4096) {
66
+ throw new Error("Total path length exceeds 4096 bytes");
67
+ }
68
+
69
+ }
@@ -0,0 +1,131 @@
1
+ import WebSocket from 'ws'
2
+ import { PublicKey, Transaction, TransactionInstruction, Keypair, Connection, clusterApiUrl,ComputeBudgetProgram } from "@solana/web3.js"
3
+
4
+ /**
5
+ * Represents the structure of result data received from a WebSocket subscription.
6
+ *
7
+ * - `fsid` (optional): The file system ID returned from the result.
8
+ * - `status` (optional): The status of the transaction or operation.
9
+ * - `data` (optional): Any custom payload or message associated with the result.
10
+ * - `subscription` (optional): The ID of the WebSocket subscription.
11
+ */
12
+ type ResultValue = {
13
+ fsid?: string
14
+ status?: string
15
+ data?: any
16
+ subscription?: string
17
+ }
18
+
19
+ /**
20
+ * Opens a WebSocket connection and subscribes to the result of a transaction
21
+ * via the custom `xandeumResultSubscribe` method.
22
+ *
23
+ * This is useful for receiving asynchronous results tied to an on-chain operation,
24
+ * such as file creation, modification, or deletion.
25
+ *
26
+ * The subscription sends a JSON-RPC request with:
27
+ * - `method`: "xandeumResultSubscribe"
28
+ * - `params`: [txId, { commitment: "finalized" }]
29
+ *
30
+ * The WebSocket listens for result messages and invokes the `onResult` callback
31
+ * if a valid result with `fsid`, `status`, or `data` is received.
32
+ *
33
+ * @param tx - The transaction ID you want to listen for results from.
34
+ * @param connection - The solana web3 connection with Xandeum-compatible JSON-RPC endpoint (e.g., `'https://api.devnet.solana.com'`).
35
+ * @param onResult - Callback to handle incoming result messages. Triggered when a valid response is received.
36
+ * @param onError - (Optional) Callback triggered if a WebSocket error occurs.
37
+ * @param onClose - (Optional) Callback triggered when the WebSocket connection closes.
38
+ */
39
+
40
+ export function subscribeResult (
41
+ connection: Connection,
42
+ tx: string,
43
+ onResult: (value: ResultValue) => void,
44
+ onError?: (err: any) => void,
45
+ onClose?: () => void
46
+ ): void {
47
+ let rpcEndpoint = connection.rpcEndpoint;
48
+ const url = new URL(rpcEndpoint);
49
+ // url.port = '8900'; // Set the port to 8900
50
+ if (url.port) {
51
+ url.port = '8900';
52
+ }
53
+ url.protocol = url.protocol.replace('http', 'ws');
54
+ const wsEndpoint = url.toString();
55
+ const ws = new WebSocket(wsEndpoint);
56
+
57
+ ws.addEventListener('open', () => {
58
+ const subscriptionMessage = {
59
+ jsonrpc: '2.0',
60
+ id: 1,
61
+ method: 'xandeumResultSubscribe',
62
+ params: [tx, { commitment: 'finalized' }]
63
+ }
64
+ ws.send(JSON.stringify(subscriptionMessage))
65
+ })
66
+
67
+ ws.addEventListener('message', event => {
68
+ const d = JSON.parse(String(event.data).replace(/:\s*(\d{16,})/g, ': "$1"'))
69
+
70
+ const value = d?.params?.result?.value
71
+ if (
72
+ (value?.fsid || value?.status || value?.data) &&
73
+ d?.params?.subscription
74
+ ) {
75
+ onResult({
76
+ fsid: value.fsid,
77
+ status: value.status,
78
+ data: value.message,
79
+ subscription: d.params.subscription
80
+ })
81
+ }
82
+ })
83
+
84
+ ws.addEventListener('error', error => {
85
+ if (onError) onError(error)
86
+ })
87
+
88
+ ws.addEventListener('close', () => {
89
+ if (onClose) onClose()
90
+ })
91
+ }
92
+
93
+ /**
94
+ * Sends a WebSocket JSON-RPC message to unsubscribe from a previously subscribed transaction result
95
+ * using the `xandeumResultUnsubscribed` method (note: custom method, ensure server-side implementation matches).
96
+ *
97
+ * This function automatically closes the WebSocket connection after sending the unsubscribe request.
98
+ *
99
+ * @param connection - The solana web3 connection with Xandeum-compatible JSON-RPC endpoint (e.g., `'https://api.devnet.solana.com'`).
100
+ * @param subscriptionId - The ID of the active subscription you want to cancel.
101
+ */
102
+
103
+ export function unsubscribeResult(connection: Connection,subscriptionId: string): void {
104
+ let rpcEndpoint = connection.rpcEndpoint;
105
+ const url = new URL(rpcEndpoint);
106
+ if (url.port) {
107
+ url.port = '8900';
108
+ }
109
+ url.protocol = url.protocol.replace('http', 'ws');
110
+ const wsEndpoint = url.toString();
111
+ const ws = new WebSocket(wsEndpoint);
112
+
113
+ ws.addEventListener('open', () => {
114
+ const unsubscribeMessage = {
115
+ jsonrpc: '2.0',
116
+ id: 2,
117
+ method: 'xandeumResultUnsubscribe',
118
+ params: [subscriptionId]
119
+ }
120
+
121
+ ws.send(JSON.stringify(unsubscribeMessage))
122
+ console.log(
123
+ `Sent xandeumResultUnsubscribe for subscription ID: ${subscriptionId}`
124
+ )
125
+ ws.close()
126
+ })
127
+
128
+ ws.addEventListener('error', err => {
129
+ console.error('WebSocket error during unsubscribe:', err)
130
+ })
131
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES5",
4
+ "module": "CommonJS",
5
+ "outDir": "dist",
6
+ "rootDir": "src",
7
+ "declaration": true,
8
+ "esModuleInterop": true,
9
+ "strict": true
10
+ },
11
+ "include": ["src/**/*.ts"],
12
+ "exclude": ["node_modules", "dist"]
13
+ }
14
+
package/typedoc.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "$schema": "https://typedoc.org/schema.json",
3
+ "entryPoints": ["src/index.ts"],
4
+ "out": "docs/html",
5
+ "tsconfig": "tsconfig.json",
6
+ "name": "Xandeum Web3 Library",
7
+ "readme": "README.md",
8
+ "theme": "default",
9
+ "includeVersion": true,
10
+ "hideGenerator": false,
11
+ "githubPages": true,
12
+ "cleanOutputDir": true,
13
+ "excludePrivate": true,
14
+ "excludeProtected": true,
15
+ "excludeInternal": true,
16
+ "excludeNotDocumented": false,
17
+ "gitRemote": "origin",
18
+ "gitRevision": "master",
19
+ "sourceLinkTemplate": "https://github.com/Xandeum/xandeum-web3.js/blob/{gitRevision}/{path}#L{line}",
20
+ "visibilityFilters": {
21
+ "protected": false,
22
+ "private": false,
23
+ "inherited": true,
24
+ "external": true
25
+ }
26
+ }