A rudimentary file storage for Radix, just for fun as an experiment. Does nothing more than storing and retrieving bytes to and from a KeyValueStore.
Note that Radix was not made for this.
Example site that allows uploading of files and serves them based on their hashes: https://radix-files.vercel.app/ (Stokenet)
A simple setup for serving files, made in Svelte, can be found here: https://github.com/yr12345678/radix-file-serving.
Stores a file's bytes in the KeyValueStore with the file's hash as the key.
By default, files up to 500KB are allowed, depending on the transaction method used (see manifests below). The mobile-to-mobile connection seems to have trouble processing larger files, but at least about 150KB should work there.
Accepts:
- Vec<u8>: hex-encoded file bytes
- String: file name
Returns:
- String: the file's hash
Gets a file's bytes from the KeyValueStore via the provided hash.
Accepts:
- String: file hash
Returns:
- (String, Vec<u8>): a tuple containing the file name and file bytes
Emitted by the store_file
method.
Fields:
file_hash: String
: the file's hashfile_name: String
: the file's name
Emitted by the get_file
method.
Fields:
file_hash: String
: the file's hashfile_name: String
: the file's name
component_tdx_2_1cpd8dr5lza00jyk28npcu9qknn4j7ug26nmnhzwtsa6qhmr99enex6
component_rdx1crlx9t5hdz2yx494zhcqyquyhdmwvnuryqt4lty2d6tcng3elxtuee
You can use the deployed version, but can also instantiate your own, of course:
CALL_FUNCTION
Address("package_tdx_2_1ph9m4alsknm4zyn0azwf6k2ejmh474075r3k5mpp7ze0vk068duvpy")
"FileStorage"
"instantiate"
For this approach, you will have to add a blob to the transaction. This happens outside of the manifest, and instead you refer to its hash in the manifest. Tools you can use to do this are for example the dApp Toolkit and the Radix Engine Toolkit.
Using this method, you can store files using the full file size limit.
CALL_METHOD
Address("COMPONENT_ADDRESS") # Replace with component address
"store_file"
Blob("BLOB_HASH") # Replace with a hash reference to the blob
"filename.png"
You can use something like https://tomeko.net/online_tools/file_to_hex.php?lang=en to get the hex of a file for testing purposes.
Uncheck the two options:
- Use 0x and comma as separator (C-like)
- Insert newlines after each 16B
Note: if you use this method, you can only use half the file size limit, as each byte is hex-encoded to two characters, which doubles the transaction size.
CALL_METHOD
Address("COMPONENT_ADDRESS") # Replace with component address
"store_file"
Bytes("HEX_ENCODED_BYTES") # Replace with hex-encoded bytes
"filename.png"
Address("COMPONENT_ADDRESS") # Replace with component address
"get_file"
"HASH" # Replace with file hash