Manually creating a Tx

Manually creating a transaction

In addition to uploading using the SDK functions irys.upload(),irys.uploadFile(), and irys.uploadFolder(), you can also manually create, sign, and upload a transaction in separate steps.

Workflow

  1. const tx = irys.createTransaction()
  2. await tx.sign()
  3. await tx.upload()

After calling tx.sign(), you can access the transaction ID via tx.id, this facilitates use cases where you need access to the ID before uploading the full transaction.

ℹ️

You must call tx.sign() before using the value of tx.id.

Creating, signing, uploading

Basic workflow.

const createSignUpload = async () => {
	// Get a reference to a pre-configured Irys object
	// See: https://docs.irys.xyz/irys-sdk#connecting-to-irys
	const irys = await getIrys();
 
	// Create the transaction
	const tx = irys.createTransaction("GM World!", { tags: [{ name: "Content-Type", value: "text/plain" }] });
 
	// Sign the transaction
	await tx.sign(); // ID is now set
	console.log(`Tx created and signed, ID=${tx.id}`);
 
	// Upload the transaction
	const receipt = await tx.upload();
	console.log(`Tx uploaded. https://gateway.irys.xyz/${receipt.id}`);
};

Serializing a transaction

Serialize a transaction and recreate it later.

const serializationUpload = async () => {
	// Get a reference to a pre-configured Irys object
	// See: https://docs.irys.xyz/irys-sdk#connecting-to-irys
	const irys = await getIrys();
 
	// Create the transaction
	const tx1 = irys.createTransaction("GM World!", { tags: [{ name: "Content-Type", value: "text/plain" }] });
	// Note: You can sign before *or* after serializing
	await tx1.sign(); // ID is now set
	console.log(`Tx created and signed, ID=${tx1.id}`);
 
	// Serialize the transaction
	const txSerialized = tx1.getRaw();
 
	// Recreate the transaction from the serialized version
	const tx2 = irys.transaction.fromRaw(txSerialized);
	// ID is the same as before
	console.log(`Tx re-created from serialized, ID=${tx2.id}`);
 
	// Upload the tx
	const receipt = await tx2.upload();
 
	console.log(`Tx uploaded. https://gateway.irys.xyz/${receipt.id}`);
};

Deterministic ID

Use a deterministic ID in cases where you need access to a transaction ID before uploading, but can't or don't want to store a reference to the transaction object.

First, generate an anchor and use that to create a transaction with your data. Then, sign the transaction and you can access the ID. Finally, you can recreate the transaction using the same anchor and data and your ID will be the same.

const deterministicIDUpload = async () => {
	// Get a reference to a pre-configured Irys object
	// See: https://docs.irys.xyz/irys-sdk#connecting-to-irys
	const irys = await getIrys();
 
	// Generate 32 bytes through Buffer.from(anchor)
	const anchor = randomBytes(16).toString("hex");
	const tx1 = irys.createTransaction("GM Irys!", {
		tags: [{ name: "content-type", value: "text/plain" }],
		anchor,
	});
	await tx1.sign();
	console.log(`Tx1 ID ${tx1.id}`); // ID is now set
 
	const tx2 = irys.createTransaction("GM Irys!", {
		tags: [{ name: "content-type", value: "text/plain" }],
		anchor,
	});
	await tx2.sign();
	console.log(`Tx2 ID ${tx2.id}`); // ID is the same
	const receipt = await tx2.upload();
	console.log(`Tx uploaded. https://gateway.irys.xyz/${receipt.id}`);
};