Created
December 18, 2021 17:27
-
-
Save sunguru98/bcd6e9473e477f2c77d55b50e9828dca to your computer and use it in GitHub Desktop.
Arweave Upload Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// newJSON -> metadataJSON | |
// imageBuffer -> Buffer created from canvas | |
async function uploadToArweave(newJSON: any, imageBuffer: Buffer) { | |
const sundeepPrivateKey = process.env.MY_WALLET_PRIVATE_KEY!; | |
if (!sundeepPrivateKey) { | |
throw new Error('Private Key not found'); | |
} | |
const solanaSigner = new signers.SolanaSigner( | |
base58.encode(JSON.parse(sundeepPrivateKey)) | |
); | |
const imageType = 'image/png'; | |
const jsonType = 'application/json'; | |
const manifestType = 'application/x.arweave-manifest+json'; | |
// Image | |
const imageDataItem = createData(imageBuffer, solanaSigner, { | |
tags: [ | |
{ name: 'App-Name', value: 'Asgard Army' }, | |
{ name: 'Content-Type', value: imageType }, | |
], | |
}); | |
await imageDataItem.sign(solanaSigner); | |
const newImageLink = `https://arweave.net/${imageDataItem.id}`; | |
// JSON | |
const updatedJSON = { | |
...newJSON, | |
image: newImageLink, | |
properties: { | |
...newJSON.properties, | |
files: [{ type: imageType, uri: newImageLink }], | |
}, | |
}; | |
const jsonDataItem = createData(JSON.stringify(updatedJSON), solanaSigner, { | |
tags: [ | |
{ name: 'App-Name', value: 'Asgard Army' }, | |
{ name: 'Content-Type', value: jsonType }, | |
], | |
}); | |
await jsonDataItem.sign(solanaSigner); | |
// Manifest (Both image and JSON info combined) | |
const arweavePathManifest = createArweavePathManifest( | |
imageDataItem.id, | |
jsonDataItem.id | |
); | |
const pathManifestDataItem = createData( | |
JSON.stringify(arweavePathManifest), | |
solanaSigner, | |
{ | |
tags: [ | |
{ name: 'App-Name', value: 'Asgard Army' }, | |
{ name: 'Content-Type', value: manifestType }, | |
], | |
} | |
); | |
await pathManifestDataItem.sign(solanaSigner); | |
// Real upload starts here | |
const bundlrInstance = new Bundlr( | |
'https://node1.bundlr.network/', | |
'solana', | |
JSON.parse(sundeepPrivateKey) | |
); | |
const uploadContentSize = | |
imageDataItem.data.length + | |
jsonDataItem.data.length + | |
pathManifestDataItem.data.length; | |
console.log('TOTAL CONTENT SIZE', uploadContentSize, 'BYTES'); | |
const costInSOL = await bundlrInstance.utils.getPrice( | |
'solana', | |
uploadContentSize | |
); | |
console.log('SOL Cost to Upload:', costInSOL.toNumber() / LAMPORTS_PER_SOL); | |
await bundlrInstance.fund(costInSOL.toNumber()); | |
for (let dataItem of [imageDataItem, jsonDataItem, pathManifestDataItem]) { | |
const transaction = bundlrInstance.createTransaction(dataItem.rawData, { | |
tags: dataItem.tags, | |
}); | |
await transaction.sign(); | |
const { data, ...rest } = await transaction.upload(); | |
console.log(data, rest); | |
} | |
console.log(` | |
IMAGE URL: https://arweave.net/${imageDataItem.id} | |
JSON URL: https://arweave.net/${jsonDataItem.id} | |
PATH MANIFEST URL: https://arweave.net/${pathManifestDataItem.id} | |
`); | |
return { | |
imageURL: `https://arweave.net/${imageDataItem.id}`, | |
jsonURL: `https://arweave.net/${jsonDataItem.id}`, | |
pathManifestURL: `https://arweave.net/${pathManifestDataItem.id}`, | |
}; | |
} | |
function createArweavePathManifest(imageDataId: string, jsonDataId: string) { | |
return { | |
manifest: 'arweave/paths', | |
version: '0.1.0', | |
paths: { | |
'image.png': { | |
id: imageDataId, | |
}, | |
'metadata.json': { | |
id: jsonDataId, | |
}, | |
}, | |
index: { | |
path: 'metadata.json', | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment