Simple off chain Data Storage Using Web3 Storage & NFT Storage. Store all Things on IPFS & Filecoin

ETHOnline 2021 · 11 Oct 2021 · 34:34

Yusef Napora of Protocol Labs explains how IPFS content addressing links NFTs to off-chain data, why pinning matters, and how Filecoin adds long-term storage. He introduces nft.storage and web3.storage, free services with an HTTP API and JavaScript client, and demonstrates uploading files with an API token. He also previews token management and plans for extensible NFT metadata.

Speakers

Topics

From ETHOnline 2021, hosted by ETHGlobal.

Transcript

Um so our next speaker is Yusef and he will be talking about simple blockchain data storage using web3.storage and nft.storage. So um without further ado, let's uh let's see how we can make NFT storage. Welcome. It's great to be here. Thanks. I'll share my screen here. Cool. So, I am Yusef and I work at Protocol Labs where we make IPFS and Filecoin. And these are two really interesting bits of technology that um we think can help everybody in this uh decentralized app ecosystem take advantage of uh

this vast network of storage that's now available. So, let me sort of get into the next here. So, it's kind of a mashup of two talks about web3.storage and nft.storage. And these are both services that we've built to make using IPFS and Filecoin really easy and accessible for especially for web two developers or really anybody that wants like sort of um friction a low friction path into this uh storage network. So, uh first I want to talk a little bit about NFTs. So, we became really interested in NFTs um you know, we have been for a while, but obviously this year they sort of started to take off in the public consciousness. That's when I

personally started getting into them. Um I was like loosely aware of them before that. But, basically it was really like encouraging for us that to see that IPFS was being used to store lots of NFTs. Um and we think it's a really great fit for this kind of application because first of all, like storing data on most blockchains is prohibitively expensive. And therefore, to have an NFT that represents something outside of itself, which is the case for many.

There needs to be a link between the NFT and data that lives off-chain, and we IPFS has some really great properties for that for creating that kind of link. So, we'll go into that in a little bit. Um but we started to, you know, realize that the developers uh that like OpenSea and Foundation and Zora and so on, they really care they care about users being able to access the data should they ever disappear. And then this is sort of a common thread in these kind of circles is that like nobody wants to trust any individual party to to stick around forever, but we combine, you know, if we all like play well together, we can hopefully design a system that outlives any of us individually, right? And

that's uh and it's good for for us to kind of build systems with that in mind, even though hopefully of course we'll be around for a while. Um but and it's something that users care about as especially like they became aware that the thing that they're or I don't know, I mean people are already aware of this, but I think that people that are new to NFTs don't always understand like what specifically lives on the blockchain and what lives elsewhere and like what's that relationship. So, there's like an educational piece to that that we've been involved in trying to kind of clue people into what, you know, what is that link and what's the strong way to make that link.

So, we've got we're IPFS within the mix. Um and if you're new to IPFS, it is the InterPlanetary File System. Uh it's been around for several years, and the gist of it is basically that you first you put your data onto an IPFS node, and that node participates in this big peer-to-peer content sharing network, where basically if somebody wants a piece of content, they'll ask for it, and if I have it, I'll provide it. And uh everybody does this, and it's actually a pretty efficient way to distribute data uh across the network in in many ways. But first so anyway, you first you put your data into the IPFS node and you get back this string, the content ID.

And it is a cryptographic hash. By default, it's SHA-2 256. But, there you can use lots of different hash functions and all the rest. It's a very customizable piece of software. Um but, the the fundamental point is that this CID will always identify one specific piece of content. And any changes to the content would have produced a different CID. So, given a CID, you can say, "Hey network, please give me this data." And when it comes back from anybody, you can always verify that it's correct because you can compare it against the CID that you used to retrieve it. So, this means that you don't need to really care who is providing the data or where they come from or, you know, anything. You don't need to know anything about them. Uh as long as the protocol works, as long as

somebody has the data and they see your request, they can provide it and you'll you'll get it. And it's a pretty cool feature for linking to things, uh which we'll get into in just a second. But, so here's the the pro- the requesting process. So, you request the data by the CID, original node responds, and now they both have a copy. And if a third node comes along asking for it, both of them can respond in parallel and say, "Oh, I have it. Uh here's a few pieces." Guy on the left gives a few pieces, guy on the right gives a few pieces. And now you get the file potentially faster than you could downloading the whole thing from one.

But, there's lots of variables there with respect to latency and so on. So, it's it's really hard to make like absolute claims about performance characteristics versus other systems. But, in general, there's potential for efficiency there. Um so, yeah, now this third node has it. And as a result, they can also participate in the providing process. Uh and that's great. The original node go away, uh but the content is still there. And so, we've kind of gotten close to this um dream of content outliving us and our, you know, organizational guidance and so on.

Um the data still retrievable from other nodes and you know, that's kind of the power of peer-to-peer network. Like that no individual peer is, you know, blessed or special. Um, but there's some caveats to this approach. With IPFS, uh, if all the nodes that have the content disappear from the network, then there's nobody there to provide it. Uh, there's the it's like an active process where your node needs to be online in order to respond to requests and if no node is online with that content, it's just not resolvable. Although, I should mention that the same is basically true of, yeah, HTTP servers like, yeah, facebook.com were suddenly to be unresolvable, uh, then nobody could

access it. But with the, um, with IPFS, you have kind of an interesting property where if anybody later adds the same content to the network, provided they use the same hashing algorithm everything, um, then they'll come up with the same CID and any links to that original content will be resolvable. So, it's Uh, well, I guess like a third party can sort of fix a broken link in a way that they can't with HTTP. So, that's kind of neat. But the, uh, the second caveat is that there is an automatic garbage collection process for these ephemeral copies that get made as content propagates throughout the network. Each node will keep a little copy for a minute, but eventually look, they may get disk pressure and will have to clean

up and throw stuff away. So, to prevent that, there's a thing called pinning, which is just signaling to your IPFS node, please keep this alive. Um, yeah, don't garbage collect your stuff. And, um, this has been one of the the the main ways for people to get, uh, practically speaking, get their data into IPFS and made accessible is to use a pinning service that will sort of provide this data for you on your behalf. So, you don't have to run your own infrastructure and so on. And, you know, as we were looking at the NFT space and you know, like what what can we contribute, we saw, well, we can make this process um a little easier for people by basically more or less becoming a pinning service, but doing so using um

so the some of other technology, I mean namely Filecoin, which we'll talk about in just a sec. So, the way we present this is as nft.storage. This is a website. It is free to use. You want again, you get an API token and then you can upload your stuff through us and where we provide it on IPFS. And also, um for long-term storage, it's all getting sent to Filecoin. So, the way this works is, yeah, you take your NFT, it's a very simple um upload interface. Uh it's just an HTTP request. There's There's a few different variations on that, uh depending on what your needs are, um which endpoint you might want to use.

But then anyway, we do your data through a system request and then NFT storage is going to store it on an IPFS cluster that we manage. And then, uh we're also we also redundantly store it with a third-party provider just for safekeeping. And behind the scenes though, we are also negotiating deals with Filecoin storage providers to store data for the long term. And they are actually getting paid by the network in the form of block rewards, which um is kind of an interesting uh sort of economic angle that we can talk about in a second. But the what for now, like the advantage of Filecoin is that

it fits this property of like allowing members of the network to disappear, but still giving you some solid assurances about your data. So, now all if any if nft.storage were to go away completely and all the IPFS nodes that we manage or that other people run lost all the content. The data is still just there on Filecoin being continually challenged by the protocol to like prove its existence. So, that's um basically if you had if like in the worst case scenario where we just fold up shop completely and can't pay the bills anymore um everybody has this sort of, you know, public verifiable escape hatch as it were, where and just like the or a seed vault

maybe is another way to think of it, where like the data is preserved and it's it's just right there and we provide all the information for retrieving it in our API and stuff. So, um yeah, to use it you go to nft.storage, which is a top-level domain now, so that's cool. And uh it's it's really simple and free. Um and you should take that take it out. It's a very it's this is the HTTP API. It's not very big. Um the uh the main endpoint is this upload endpoint for storing things, but there's also um yeah, stuff for retrieving status information about your upload, there's, you know, who is storing on Filecoin and IPFS and so and so on, so that you can

kind of get that granular information if you need it. Um and if you're in JavaScript, we have this JavaScript client that makes it uh makes things a little simpler and will help you format data according to ERC-1155 standards. So, if you are um building an ERC-1155 contract or one that, you know, can be compatible with one with that, um use this client.store method and what uh it will do some nice stuff for you, basically.

You can give it a file object and it will add the file to IPFS and create a hash link for you in your metadata JSON and then also it wraps the whole thing up into this um thing called a DAG Cbor graph. Uh it it's not super important what DAG Cbor is, but it's basically a just a compact binary JSON object. But, it's it's a nice way of addressing hash link data that we like to play with. Uh it's part of the IPLD project, if you're interested. Um so, I'm going to quickly just show you guys how to use NFT.Storage, and then we can talk a little bit about our other storage service. So.

Uh here's the site itself, and you log in with GitHub or email, it'll send you a magic link to click on. Uh so, I log in to my profile, you'll see a few things I've stored. Uh mostly just testing things out and so on. And we can go here and poke at it a little bit and get an API token that we can authenticate with. Um I should also uh mention that my home internet is is very slow, so things that are slow for me may not be slow for you when you're using them. So, especially when I'm uploading things.

It's kind of sad. Uh but anyway, so here we are at the API keys, and uh I made this one a few minutes ago. You can make and delete them here. And so, there's my key for later. I'll probably come back here, and I'm going to uh I've got an empty project. Uh I just have an empty folder here with some images that maybe we could store on NFT.Storage, and let's make an NPM project to write some code in. So, if I do NPM init, and I just going to accept all the defaults.

Now, I have a package.json file, and I can say NPM install uh nft.storage, and that gives me the JavaScript client library. Um yeah, I'm I'm also going to pull in a couple other libraries once this is done to make, you know, building a simple little example app easier. So, minimist is a for pulling out command line arguments, kind of nice. And then MIME types for figuring out what kind of file you got cuz the store function kind of likes you to provide it with a MIME type. And this this method that formats according to ERC-1155 expects it to be an image file type when you provided the image

field. Well, you can actually give it an MP4 now for compatibility with platforms that use videos in image field. Um so anyhow, we have now installed some dependencies and head over to an editor and make ourselves a file. Um I'll call this index.mjs. Um something that's relatively new in in Node land is if you name your file with a MJS extension or if you put type module in your package.json, you can use import syntax. Import NFT storage from NFT storage.

Um I'll also pull in the file object since uh the the API uses the file object that you would see in a web browser context and then it provides an implementation for Node if you're working on Node.js. So, I can import that here. Uh and then in order to read the file from my local disk, I'll import Node's FS module. So, import FS from FS/promises so I can use the async/await. And pull in Minimus that we installed earlier.

And MIME types. So, that's all the stuff we'll need in a second and let's do a function for storing an NFT. So, let's just call this async function store NFT and it'll take a image file name. And then send in the name and description metadata that we'll we'll tag our NFT with. Let's description.

So, to do this, we're going to make an NFT storage client, and for that we'll need a token, which I'm going to be a naughty boy and hard code into my source code. Uh but yeah. And obviously, this is actually uh uh area that we'll be improving upon very shortly. Like right um right now the best practice for protecting your API token for NFT storage is probably to protect to have it uh interact with NFT storage from a back-end service since bundling it into a purely decentralized app front-end would expose it to the client, but we're working on solutions for that, which would involve direct authentication through a wallet and using decentralized identity, and also um sort of user-scoped tokens so that you can

manage authorization on your user's behalf. I'm mentioning this because um it's just a common issue that we are heads down working on. So, stay tuned. Uh in the meantime, I'm going to grab a token from here and paste it in here. So, I've got got that, and now I can make a client. So, I'll say const uh client is new NFT storage, and you give it a token argument. And now I can do stuff with it, but first I'll need to read my file in. So, I can say file data equals fs.readFile.

Oh, sorry. I need to await this. It's async. And image file name. And type is going to be mime.lookup image file name because it just pulls the file type from the file extension. Not super fancy. And then we're going to say, "Okay, I have the data and the file types and I can make a file." A new file object that takes an array of blob parts, which is basically just you know, things that can be turned into binary data. So, for uh um this stuff we slurped up from our image file

name here will fit that just fine. Um actually yeah I I let's see. No. Let me quickly make this bigger so everybody can this is a little easier to read. There we go. So, I'm making a new file and I'm giving it the file data. The file name um maybe we'll trim that down to just be the last part of the file path. So, import base name. And now you can say this is base name image file name.

Okay, and then the the last part of the file constructor is the optional arguments where you can pass in the mime type. So, we looked that up earlier and now we can pass it in. And now we have a file object that I can I'm going to call this image, right? Because that's what the store method expects. Yeah. Image. Now, this is a proper file that we can hand off to the client. So, now I'm going to say uh I'm just going to return client.store. image name description Uh and there's a few other you can add optional properties to you know, if you have if you want you can have properties with any key value stuff you want in

there for custom use case, which if you are doing the the ERC-1155 we do recommend like sticking custom properties in properties if you can just because it makes it easier. We're trying to beat the drum of standardization a little bit. Um because I been on the receiving end, we're also you know, scraping Ethereum chains and finding all kinds of NFTs in the wild and seeing what people are doing. There's a lot of interesting metadata variations out there. But at any rate, now we have this and we can say we can call it. So let's make up a main function.

And and this is where we can grab our command line args. That's right. We don't need this. complete a little bit dirty. All right. So args equals minimist. And for this you pass in the process arguments. So process.argv Pull off the first two for node reasons. And now um Minimist is kind of simple. It just like it takes whatever arguments it gets and parses them out and gives you a dictionary or a JS object right here. So now I'm going to just assert that you

gave me the right stuff. So there's nothing We want there to be an image, a name, and description. So if there's no image, complain. Uh this may be a little fussy for live coding. But still, I mean, error handling is important. I'll let the description be optional, I guess.

I'll say that I'm going to say Let's pull them out of this object and I'll and then pass them to the store NFT function. So, image here is the uh image file name uh and we can just toss that over here and say metadata equals await uh store NFT and then give it this the image file name name and description. And now let's log into the console and see what happens. So.

All right. So, now if I call the main function, we should be able to call this thing as a node script and see what we see. So, let's see. Here we go. Uh if I do node index.js and then say dash dash image uh I can give it uh images and name etc.

And yeah, I probably should have added a little console log saying, "Hey, we're doing a thing now." But, there it goes. Uh it's already done. So, it So, what happens with the result you get back at this is um you have an IPFS URL, which is in the IPFS colon slash slash format. If you want, you can pretty easily turn this into a HTTP gateway URL. So, um this part here is the the CID and then there's a file path. So, if I wanted to make a gateway URL out of this, I can say CID Sorry.

Um copying things accidentally here. CID.ipfs.dweb.link. Uh there's lots of other gateways, but that's the one that sticks in my head. And then uh then add the file path at the end. So, I'll show you what that looks like real quick. I do CID that ipfs.io web.link /metadata.json. Yeah, I spelled that wrong. The metadata.

Yeah, so it cuz um Yeah, there's basically a little bundle and makes a little graph structure for you. And then um you can traverse that graph using this pathing syntax or whatever. But it basically is just Yeah. Yeah, here we go. So there's there's the metadata that we stored and you can see here if uh that's big enough that this is the uh the image field has this same IPFS colon slash slash dot reference to the image itself. So, you can make another gateway link and pull it out.

Uh I uh dude, if you have Brave installed, it'll follow these links automatically, but I for some reason didn't think to use it uh for this demo. Oh, well. the link So, that will chase out the image itself. Um and and if you want a sort of machine readable uh or sort of traversable form of this uh without having to parse links and so on, that wrapper object, like the root object of this graph, where if you just have the CID uh without the

metadata.json, it's actually a with that DAG-CBOR thing I was talking about earlier, where you can fetch this object and then use um our IP LD tooling to traverse into the metadata and find the image itself and sort of all bundled into this graph. So, not sure why it's taking so long to resolve, but I I blame my terrible internet. Did so, we'll see. Uh anyway, let's maybe take a break from all this stuff and uh we'll talk about web3.storage. We'll come back and hopefully it will have loaded by then.

All right. So, oh look, there's something I want to mention before we get on to web3.storage, which is um that we have uh this thing called There's a a format called a CAR, which is a content archive, and you can send us content archives. Uh and if you want to pre-format your IPFS data, like if you have specific layout that you want, or also um if you want to send big files over 100 megabytes, I should mention too that we're going to be doing this for you automatically soon. So, um you may not have to do it yourself, but it's kind of nice to understand what's going on behind under the scenes so you have a mental model what's working. So, uh the advantage of using a CAR is that you can know the CID

before you upload. So, you're not waiting on our server to tell you what the identifier is, and you can verify that it's correct, that we're like storing exactly what, you know, you're giving us. And it like I say, it's chunkable and it has this you can split the CARs into chunks here. So, I have one big graph uh and I can split it into little graphs that all have the same root, and our back end will piece them all together for you. Uh and yeah, it's pretty cool. So, I'll talk a little bit more about CARs in a second because we have this other service and uh I'm just quickly running low on time. So, uh I should get into the weeds of web3.storage. It's It works very much like NFT storage, especially under the

hood. They are very very similar. Um and it's So, if you remember this slide from before, it's the same thing, but for non-NFT data. So, it's it's actually any data that you have that you want to be stored on IPFS and Filecoin, you can just upload through web3.storage, and we will store it for you. Um and it has the CARs baked in. So, uh and this will be coming to NFT.Storage soon, but right now web3.storage has it already in the JavaScript client where essentially you give it a file of any size, and it will create the CAR file for you and do the chunking and send everything off, and it gives you access to the root CID um

as soon as we compute it on the client. So, you can display in your UI and say, "Oh, here's the thing. Now, in parallel, send it off to uh web3.storage, and it'll get stored for you." And you get progress callbacks and stuff like that. Um very briefly, here's kind of what the architecture looks. Um I'm not going to talk a a bunch about it, but there's some cloud workers. We maintain some state in a database, but then the interesting bit is the storage broker service. It will take your data and batch it up into use I think we usually do 32 GB chunks um and store those each with a Filecoin provider, since that's the most efficient format for getting data directly into a Filecoin sort of pipeline.

Um and then but parallel to that, we've already added it to our IPFS cluster and to our redundant Pinata cluster. So, it's available for retrieval pretty much as soon as you upload, um but then there's maybe I think we're at like uh 18 hours now in terms of delay for getting it onto Filecoin. Right? I'm sorry, it might actually be eight, but I I have to actually ask people what the real number is. Sorry. But it is a it'll be a few hours because we have um we have this aggregation process going on. Um and but as a result of this, by by meeting the miners where they are, sorry, the storage providers, that's what we're calling it.

We um Um we are able to offer the service for free. So, here's uh something that comes up a lot when we talk about the service is like, how is this free? Uh and it it basically is because um the primary economic driver for Filecoin providers right now is block rewards. So, they get rewarded first for committing capacity to the network. So, if you bring drives online uh into the network and prove that you've done so, um then you get potential block rewards relative to your overall storage power, the capacity that you're that you're adding. Uh but, um if you are storing real data, boom, you get a 10x uh multiplier, basically. It it And And the way that this works is

that there's a all a system called Filecoin Plus in which a notary allocates this resource called data cap. And data cap essentially is is more it's it's sort of like a voucher that says like, "We we believe this is real data and not just some random, you know, garbage that somebody um generated to pump up their numbers, I guess." Um so, the idea is that by flowing your data through Web3.Storage and NFT.Storage, like, we have a reasonable assurance that you're a real person or that there's a real like sort of social need behind the data. I mean, that's kind of what we're getting at cuz like fundamentally our mission is data preservation and all that. So, anyway, verified data

increases the chance of block rewards by 10x. And so, as a result, it's the actual cost of like storing the data and there's some collateral that the providers put up front to, you know, ensure that they're going to store it. That is all basically um kind of negligible compared to the increased reward. So, it works out for them to provide long-term storage for free, and so we're not charging users anything for storage as well. Um there's a minimal operational cost to us to keep the IPFS side of things, you know, running but that's in in grand scheme not a big deal and we're super, you know, into providing it to make things easier for people. So, that's that's what I got. Well, thanks

very much. And if if anybody has questions, please let me know. So, that was awesome. Um also nice recovery. I feel like we're going to go over time, but this worked out well. Yeah, yeah, totally. I pulled it in. Um I think two kind of common questions when you actually address it is the nuance between web 3 and NFT.storage just the formatting and then the car archive formats. Um A common question that I think we get especially during the hackathon is what are some best practices that you recommend when people want to think about storing metadata or even updating it? Like how do we think about that and how does NFT.storage kind of come in handy for for these types of things? Um and I want to

question in too how do you actually what's next after this? Like you are is this done? Like is this a great service you're offering it it works. Like how do you actually improve this thing or what are future features? Um actually I mean with the so there there are some reliability things that we want to bake in and right now like we're doing a database migration that should make everything faster and more, you know, solid. But feature-wise, I think those two questions are kind of related where like right now especially Michael and Gazelle, I don't know if you guys have met them yet, but they're heads down trying to figure out a sort of a workable plan for extensible NFTs or like not not exactly mutating, but keeping track of, you know, assertions about an NFT over time. And so, the way that we

kind of see that happening now is using events to send out basically, you know, entity attribute value triples where you would say like the entity is an identifier for the NFT itself and then an attribute is some assertion about it like, you know, it has an alternate representation here or or like this, you know, it was remixed into something that you'd imagine lots of scenarios, but then eventually you fold those into you basically compact a big log and then you have I mean, you know, a view of the all the NFT over time, but and that's all kind of like speculative right now.

We're still trying to figure out how practical can we make this and like how and also like you know, is this something that requires a lot of buy-in from people up front or is there a way we can kind of apply this to NFTs that already exist? And I think that there is but there's a lot of details. So I think that that's kind of where our focus is NFT wise right now for NFT storage is to try to figure I'm not sure if it'll actually come through NFT storage directly, but I mean we will support it there, but I think that that's kind of a broader effort of trying to figure out what the data model should look like.

Um in terms of the actual experience of using it, um we're right now so right now we have it two separate services and we're going to end up making NFT storage a client of Web3 Storage. So just for less stuff for us to manage. And for the token management stuff is the the big feature that's going to be coming to both platforms soon. I think it's a like So it'll be a way to have like a sort of admin token that you can then create a a user scope token and then then users will be able to manage and delete their own stuff without having access to anybody else's, but then we're also going to have like a fully decentralized path of using a decentralized identifier document to assert ownership of a key and then that gets you like we just want to know that there you know

be able to identify you somehow. We don't have to know who exactly specifically you are. So we're building that out and should be coming I mean I can't promise timelines, but we're feeling it. Awesome. Well, the ideas do sound pretty incredible here. That's great. Well, I think that's all the time we have today. Thank you so much for doing that presentation and also Yeah, thanks. That was great. Uh but uh perfect. So with that we are ready to move on to