How we launched Pixel Dev NFT on Polygon - Rasmus Nielsen
web3con 2022 · 5 Mar 2022 · 20:01
Rasmus Nielsen, a full stack developer and core contributor to Developer DAO's Derivatives project, explains how the Pixel Devs NFT collection launched on Polygon while requiring holders to own a Genesis NFT on Ethereum. A small authorization server checks Ethereum ownership and signs a short-lived message, and the Polygon contract verifies that signature before minting. The talk walks through the Express server, the Solidity modifier, the trade-offs, and an encoding pitfall.
Speakers
Topics
From web3con 2022, hosted by Developer DAO.
Transcript
this talk is sponsored by live pier live pier is a decentralized video streaming network that provides the necessary infrastructure for broadcasting video over the internet at a fraction of the cost to today's cloud infrastructure learn more at livepeer.org okay so hello everyone my name is rasmus and today i want to talk about how we launched the pixeldevs nft collection on polygon so first a little bit about me i'm a full stack developer and consultant based in denmark i have worked with web development for more than 15 years and my journey into work free first got started last year when i became a developer member
and without further ado let's just jump straight into it so pixel devs what is pixel devs it is a nft collection of of of nft it is a pnft collection launched recently just by developer data and it is a derived nft from the original genesis nft also known as developers for revolution it's a visualization of the eight traits and what you see here on the left is my personal genesis nft and on the right you have the pixel dev nft so i first got involved with the project
when i built a simple preview app that allows you to put in your developer token id and see a preview of your pixel dev later on that very same web app laid the foundation for the minting site that is today public at pixeldevs.developer.com so what i want to talk about today though is how is is the actual underlying workings of the code which is we decided to we decided quite early on to launch the nft collection on a layer 2 option and we chose polygon now our motivation for that was quite straightforward to save a ton of
gas money for our members making it more affordable and accessible to claim your personal pixel dev now it also came with a further complication which is in order to mint your pixel depth you must also hold a genesis nft on ethereum but how do we make that a check a crush chain so if you're anything like me a savvy web 2 developer you might think hey that that's not too difficult what we'll do is just the web client will send a minting requested polygon and polygon will then check in with ethereum and see hey does this
uh does this address hold this token on in the genesis token there's just one problem which is it's not how smart contracts and and and blockchains work so a smart contract runs in a deterministic environment meaning that a certain input must always provide a certain output and with that it comes to restrictions of not being able to make any kind of randomness and not making any kind of outbound network requests it can only interact with itself and the blockchain that it's hosted on so what can we do instead
actually we have a few different uh we have quite a few different ways that to to do this but what i'll share with you today is specifically how we ended up doing it so basically we took that we took that responsibility of and put it into what i like to call a authorization server so the the flow now looks like this a the browse a user logins with the wallet with the wallet in in the browser in the web client and from the web client it will then make a authorization request to the authorization server that request contains the address of the
user that's offer that's authenticated and the token id they're trying to mint now the server will then check in with ethereum and ask hey who is the current owner of this token id and if that address that comes back matches the address in the request it will create a it will create a response containing the address token id and a expiration which is how long and and then it will create a signature with those three parameters and the expiration is simply how long is the signature valid for next the rep plan will then
send the actual minting request containing this payload from this response from the server and all polygon has to do now is see is does this signature look like a legit authorization server signature and if it does then i can go ahead and admit the nft but there's one last piece to the puzzle which is how does the polygon contract know what a server signature looks like for this to work the authorization server will need to have an identity on its own of its own it which is basically a wallet address with an underlying private key it can then use the private key to
create the signature and the polygon contract can then see if the the entity that's created the signature came from the known server address and if it did we know that we know that the user holds a genesis token on ethereum so there's a few benefits to this approach one of them being that it's super flexible in in in the sense that it works with any kind of evm compatible blockchain being any layer 2 option or even can even check against it can even run on say polka dot power chain
as long as it's an evm compatible blockchain it can also incorporate any kind of logic you you may wish basically if you can write it in code you can build it it's super flexible because you can you can you can you can push updates to the to the server bug fixes new features and it's also very low cost all you have to pay for is hosting off of that stateless server there's also a few things to keep in mind which is it does have a private key that needs to be kept safe if that private key is somehow exposed or exploited then it can be used to to forge the server
signatures and and your solution is no longer secure and yeah and you oh there's also the extra dependency that you add to the to the contract which is the server so that's of course something you want to keep in mind if you're interested in knowing more about our considerations and also some of the other solutions we did explore before heading down this path i highly recommend you to go check out a blog post i published recently it's on the official developer. blog and it's called launching pixel devs nft on polygon so go check that out what i want to spend the rest of my talk on is
showing you the actual code that powers our solution so let me just share my editor here and and we'll actually do a um a code dive so what i have here is the git repo of the pixel dash project it is called pixel avatar so that was our code name for the project while developing it so just keep that in mind whenever you see pixel avatars it actually means pixel devs you'll see that our project consists of three main parts which is the contract the web client and the server so we will zoom in on the actual communication between the server and the contract
and let's try and start by opening up app.js and of the server and you'll see that it's a very basic express server if we scroll down here there is two endpoints this inventory which returns an array of token ids that the provided address holds on ethereum and then we have the authorized method and let's try and open that so at the top of the method we are we are fetching the address and the token id from the request
we'll do some very light validation that a token id was actually provided and next we will send a request to ethereum getting asking who is the current owner of this token id with a little bit of lock that address should match the address provided in the request and if it didn't we're going to error out saying that only the owner of the genesis token can claim this token we're then ready to create our signature we'll start by creating a deadline and the deadline is basically just a sanity check saying that you can't just obtain
a signature and have it lying around for forever you need to use it fairly soon and basically we want to keep it as that window as short as possible but long enough that a user can actually make a successful transaction on polygon i think right now we have it set to 5 10 15 minutes something like that we'll then construct a message and the message is a concatenation of the different parameters so it basically reads token id colon the actual token id address colon the address deadline colon the deadline next what we'll do is that we will hash it and for the hashing we use a
algorithm i believe it's called cac 256 and that is the hashing algorithm that solidity also uses in the contract finally we will create our signature now i won't go too much into how we are constructing the signature but basically we give it a hash and a private key we will then sign it and finally the outcome will be three part which is consisting of v r and s and that together forms the signature
now if you wanna know more about how this works i recommend you to go google it and find it on stackoverflow there will be someone explaining it much better than i can also what i'll mention is that the private key of the server is so we basically we have a cli script that generates random key pairs so we can we can just run the script and it will spit out a private key and a wallet address derived from that private key the inner workings is exactly the same as when you download metamask and it will provide you with a with a wallet address with an underlying
private key so it's exactly the same basically in our response we'll then include the token id the deadline and the signature now you'll notice that we're not actually returning the address and that's because when when the rep when the user then creates the minting request it it will already the payload of that request will already include the authenticated wallet address so that's what we'll use instead let's go and check out on the contract part of things so
the contract here is you'll see that it's an erc 721 innumerable upgradeable contract from openstepperlin and if you don't know what that means it it basically just means we we didn't go and create our write our contract from scratch instead we we built upon the tried and tested open settling contract so we only have to implement a few details that makes our that makes our contract unique and you see that we have a base uri which is the ipfs address where the actual images are hosted along with the metadata
we have the minting price and then we have the server address and mind you again this server address is the public address of that server derived from its private key so those two should align with each other i'll skip through these fairly quickly we we have some some events that are being emitted and we then initialize the contract and then we have some get us instead of here now i'll scroll down to the actual method that we call from the web client and it's called mint with signature again you'll see it in it accepts the exact same parameters
from returned from the server we have the token id deadline and the signature the the body of the mints of the minting method is very simple we just check that enough matic was sent we will then we will then mint the actual nft and emit an event saying that it was minted now what you'll notice is that we have this modifier here called valid server signature where we pass in the arguments and this tells solidity that before you can jump down and call this code you must make sure that this code up here passes first
so let's have a look at what that is so this is the modifier valid server signature and the first thing you'll notice is that we are reconstructing the message exactly the same way that we did on the server side of things so we are concatenating these parameters token id address you'll notice that the address is the message sender which is which is the actual wallet address that initiated this transaction and finally the deadline we then use the same hashing algorithm and we pass it into a function called easyrecover now ease what easy recover is
it text the hash method and the signature and what it will spit out is the wallet is the address that created the signature and with a little bit of luck and no foul play that signer should match the should match the server address known to the contract if it didn't we we're gonna error out with invalid server signature now if it did match the last thing we'll check is that the deadline hasn't expired and finally we're telling solidity if you can make it to this point you're good to go you can go ahead and submit the nft
now there's just one more thing i'd like to address which is there's something that tripped me up a bit while building this and it has to do with the way solidity works fairly close to the metal if if you will in the sense that the way that we concatenate these different parameters is that some of these are strings and some of them for instance the address is actually a hex code so you can't just concatenate those like one big string instead what you use is a function called api and code packed and what that does is that it loops
through each of these different parts it will then hex code them until they're all hex codes and then it's gonna concatenate those hex codes now just hopping back to the to the server if you're anything like me what you would do is just uh bit optimistically concatenate the strings so it reads the actual like token colon token address colon address until it's one long string and then for the sake of it you can hex code that the problem is that these two different approaches does not produce the same output
basically concatenating hex codes is not the same as concatenating a string and then hex coding that so instead what i ended up doing here was create a little helper called hex concat that loops through each of the different parts each of the different strings ensures that it's a hex code like hex encodes it and then by the end of that concatenate the individual hex codes and that will produce the same result as this in as what we get in solidity so i hope that i just saved you a few hours of debugging because because i sure it sure did trip me up and
and and hopefully you won't have to to spend those hours now okay so finally i just want to say special thanks to um to the other call members of the uh of the derivatives team gold sierra lebron and eric obviously who is the great artist behind all of these pixel devs and also to the countless other contributors to the project it indeed was a team effort and it's been a blast working on it together if you're a developer down member do go check out the pixel devs on on the website and
and yeah my name is rasmus and i hope you found this talk interesting do feel free to feel feel free to reach out to me on twitter or github or discord if you're a down member and and yeah oh and by the way if you notice if you if you've been wondering what this behind me is it's actually my wife's nft collection it's an open c it's called butter goods so it also just goes to show that um you know we are pretty repealed on on anything crypto web-free nfts here in the house so uh so yeah thank you so much for tuning in and have a great day i'll be seeing you bye
