Skip to content

Instantly share code, notes, and snippets.

@tracy-codes
Created June 30, 2022 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tracy-codes/3aab96f4c23086d6124835436d4ca6f4 to your computer and use it in GitHub Desktop.
Save tracy-codes/3aab96f4c23086d6124835436d4ca6f4 to your computer and use it in GitHub Desktop.
Javascript implementation of the new edit file method
import bs58 from 'bs58'
import nacl from 'tweetnacl'
// `storageAccount` is the string representation of a storage account pubkey
// `fileName` is the name of the file to be edited
// `sha256Hash` is the sha256 hash of the new file's contents
const message = `Shadow Drive Signed Message:\n StorageAccount: ${storageAccount}\nFile to edit: ${fileName}\nNew file hash: ${sha256Hash}`
// Expect the final message string to look something like this if you were to output it
// Shadow Drive Signed Message:
// Storage Acount: ABC123
// File to delete: https://shadow-drive.genesysgo.net/ABC123/file.png
// If the message is not formatted like above exactly, it will fail message signature verification
// on the Shadow Drive Network side.
const encodedMessage = new TextEncoder().encode(message);
// Uses https://github.com/dchest/tweetnacl-js to sign the message. If it's not signed in the same manor,
// the message will fail signature verification on the Shadow Network side.
// This will return a base58 byte array of the signature.
const signedMessage = nacl.sign.detached(encodedMessage, keypair.secretKey);
// Convert the byte array to a bs58-encoded string
const signature = bs58.encode(signedMessage)
const fd = new FormData();
fd.append("file", fileData, {
contentType: fileContentType as string,
filename: fileName,
});
fd.append("signer", keypair.publicKey.toString())
fd.append("message", signature)
fd.append("storage_account", storageAccount)
const uploadResponse = await fetch(`${SHDW_DRIVE_ENDPOINT}/edit`, {
method: "POST",
body: fd,
});
@0xPratik
Copy link

There is alot of things not explained in this example, is this code part of some repo cause i can see incomplete definitions of some variables

@tracy-codes
Copy link
Author

tracy-codes commented Jan 21, 2023

@0xPratik This code is an example, not a full implementation.

I recommend looking at the other open source GenesysGo repositories for full examples if you aren’t sure what to do with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment