<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Barrick on Medium]]></title>
        <description><![CDATA[Stories by David Barrick on Medium]]></description>
        <link>https://medium.com/@davbarrick?source=rss-2d06552f9d63------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*E6O06jj2T4YtUD1TM9dzdg.jpeg</url>
            <title>Stories by David Barrick on Medium</title>
            <link>https://medium.com/@davbarrick?source=rss-2d06552f9d63------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 23:06:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@davbarrick/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How We Built the Buildspace NFT Contract ✨]]></title>
            <link>https://davbarrick.medium.com/how-we-built-the-buildspace-nft-contract-1dc8b81a3b4f?source=rss-2d06552f9d63------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dc8b81a3b4f</guid>
            <category><![CDATA[solana-network]]></category>
            <category><![CDATA[polygon-network]]></category>
            <category><![CDATA[nft]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[web3]]></category>
            <dc:creator><![CDATA[David Barrick]]></dc:creator>
            <pubDate>Wed, 22 Dec 2021 21:46:36 GMT</pubDate>
            <atom:updated>2021-12-28T14:52:40.375Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pFdHOS0LCH6XSPlbAgrenw.png" /></figure><h4>Inspiration</h4><p>Traditionally, when you complete a class / college degree / training etc, you receive a nice confirmation email, or a certificate at best.</p><p>Pretty boring right? At <a href="https://buildspace.so/">buildspace</a>, we had to spice things up. So naturally, we turned to the best crypto native certificate, ✨ <a href="https://en.wikipedia.org/wiki/Non-fungible_token">NFTs</a> ✨</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*2cvByo50Bm_1rCRBCCObCw.png" /></figure><p>Excellent. Now that we got our meme out of the way courtesy of the <a href="https://discord.gg/buildspace">buildspace discord</a> meme channel, let’s dive in, shall we?</p><p>NFTs and smart contracts in general require a lot of thought and planning upfront because of their immutable nature, so here was our spec:</p><ul><li><strong>⛔️ Non-Transferable</strong>: Users shouldn’t be able to transfer their proof of completion to someone else.</li><li><strong>🔮 Multiple Cohorts</strong>: Each cohort in buildspace has a unique number of users enrolled. So the mint limit would also need to be unique per cohort on the contract.</li><li><strong>✈️ Airdrop Support: </strong>$MATIC is pretty expensive to <a href="https://wallet.polygon.technology/bridge">bridge</a>, so we needed a way to mint and airdrop NFTs to users without requiring them to have $MATIC in their wallets.</li><li><strong>🔒 Access Control</strong>: We needed a way to grant certain people (buildspace team members and teaching assistants) privilege to mint and airdrop NFTs.</li></ul><p>We used the OpenZepplin ERC721 contract as our base, and modified it to fit our needs. Initially, we deployed our NFT contract to the ethereum mainnet at a cost of around $850 at the time, and paid the gas fees to mint on behalf of our users. But at $40-$60 per mint, this quickly became cost prohibitive at our scale. Enter <a href="https://polygon.technology/">Polygon</a>. Polygon is an EVM compatible layer 2 chain. Once we deployed the *same* contract on Polygon, we saw our mints go down to $0.01-$0.02 each. Much better!</p><p><em>Note: We use </em><a href="https://brilliant.org/wiki/merkle-tree/"><em>merkle trees</em></a><em> extensively in this contract. Essentially, merkle trees are an easy way to whitelist a bunch of addresses without storing an endlessly growing array of ‘allowed’ addresses on the contract. You can read more about them in relation to crypto </em><a href="https://thecibrax.com/using-merkle-trees-for-bulk-transfers-in-ethereum"><em>here</em></a><em>.</em></p><h4>The Contract</h4><p>Below is the full contract, but don’t worry I’ll break it down section by section:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f068b4d603f55f417a77e9e9e6c0a1fe/href">https://medium.com/media/f068b4d603f55f417a77e9e9e6c0a1fe/href</a></iframe><p>Initially, buildspace users would manually claim their token from the project page via the claimToken function on line 134. We found the merkle root update during the claim process to be pretty confusing to users, so we switched to an airdrop method and abandoned the claimToken function. Alright let’s look at the first spec requirement:</p><h4>⛔️ Non-Transferrable</h4><p>Making NFTs non-transferrable turns out to be pretty easy. Before a transfer event occurs, and internal ERC721 _beforeTokenTransfer function is called. So all we need to do is override this function on the contract and add in our requirements (line 181). Essentially, we only wan’t to allow mints: from address(0), burning an NFT: to address(0), or if a buildspace admin explicitly allowsTransfers in the future. We kept the allowsTransfers override option just in case we saw a large amount of incorrect wallet addresses linked to user’s profiles and wanted to let them transfer to their correct wallet.</p><h4>🔮 Multiple Cohorts</h4><p>Let’s take a look at line 13. Our cohorts are all a mapping of cohort_id as the key, and a Cohort struct defined on line 21 as the value. Whenever we create a new cohort for a buildspace project, a new cohort is also created on the contract by calling the createCohort function on line 152. There are 3 params in this function: cohortId, limit, and merkleRoot. The cohortId param corresponds to the cohort ID on buildspace, the limit params sets a max number of mints per cohort, and the merkleRoot params allows us to pass in an initial ‘whitelist’ of users who should be able to mint against a cohort.</p><h4>✈️ Airdrop Support</h4><p>Every hour, a cron job built by <a href="https://twitter.com/alec_dilanchian/">@alec_dilanchian</a> runs and fetches all the cohort IDs that have started within the past 7 days. For each cohort, we check to see if a completion event has happened. In our case, this means you’ve completed the last lesson in the project. Once a project completion is found for a user, and that user has linked their ETH wallet to buildspace, we generate a merkle tree in our backend, update the contract with the new merkle root with the setMerkleroot function on line 166, then run the adminClaimToken function on line 126 from the cron job with the merkle proof related to the address that is receiving the NFT. After that, we fire off a confirmation email to the users that an NFT was just delivered to their address! Here’s what this looks like on our stack:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZhyP2HTbdTnJf8qadOPRNQ.png" /></figure><h4>🔒 Access Control</h4><p>Initially, we believed we needed a way for the buildspace team to mint and airdrop NFTs to users. In order to do this, we needed an admin map on line 12 to store our team wallet addresses, and a way to update these admin addresses over time via the updateAdmin function on line 177. We haven’t really used this feature in the contract though, and probably won’t in the future because we now have the cron job detailed above. That turned out to be a lot easier!</p><h4>🖼 Bonus: Metadata</h4><p>It’s recommended to use something like <a href="https://ipfs.io/">IPFS</a> for a more permanent storage solutions for NFTs. <a href="https://leftasexercise.com/2021/10/03/using-nft-metadata-to-safely-store-digital-assets/">Here’s a great post</a> on why this is important. However under our time constraints, we opted to use a Cloudfront CDN solution backed by an S3 bucket for all of our metadata needs. Our contractBaseUri is https://tokens.buildspace.so/assets/{COHORT_ID}-{COHORT_TOKEN_NUMBER}/metadata.json, and serves up all relevant metadata to wallets like MetaMask, and marketplaces like OpenSea.</p><p>And that’s it! You can checkout the contract’s activity on Polygon <a href="https://polygonscan.com/token/0x3cd266509d127d0eac42f4474f57d0526804b44e">here</a>. If you have any questions on our method of implementing NFTs, or just web3 stuff in general, feel free to reach out to me on twitter: <a href="https://twitter.com/davbarrick">@DavBarrick</a> ✨</p><p>PS: Huge shoutout to <a href="https://twitter.com/jake_loo">@jake_loo</a>, <a href="https://twitter.com/monkeymeaning">@monkeymeaning</a>, and the team at <a href="https://thirdweb.com/">thirdweb</a> for helping us build and audit this contract :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dc8b81a3b4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Build a Serverless Web3 Wallet Login like OpenSea with MetaMask and Cognito]]></title>
            <link>https://davbarrick.medium.com/how-to-build-a-serverless-web3-wallet-login-like-opensea-with-metamask-and-cognito-eb93c723f4de?source=rss-2d06552f9d63------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb93c723f4de</guid>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[web3]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[metamask]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[David Barrick]]></dc:creator>
            <pubDate>Sun, 03 Oct 2021 21:11:17 GMT</pubDate>
            <atom:updated>2021-10-03T21:11:17.426Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gYEjgiFUFbFxRsXYV-qKLg.png" /></figure><p>Username / Password logins are the bane of our existence. Social logins? Just another tactic by big tech to collect our data.</p><p>So how do we improve this in a “Web3” way but still provide traditional JWT auth to your existing backend API?</p><p>Wallet sign in. This is the way.</p><p>The basic idea here is that it’s cryptographically easy to prove the ownership of an account by signing a message using a private key. If you manage to sign a message generated by a backend, then the backend will consider you the owner of that public address. Therefore, we can build a signature authentication mechanism with a user’s public address as their username.</p><p>If you don’t already have the <a href="https://www.serverless.com/framework/docs/getting-started/">Serverless CLI</a> and the <a href="https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-mac.html">AWS CLI</a> installed, make sure to set both of these set up by following the <a href="https://www.serverless.com/framework/docs/providers/aws/guide/installation">instructions here</a> before we get started 💪</p><h4><strong>Resources Service</strong></h4><p>To start, let’s create our <a href="https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools.html">Cognito User Pool</a>. Personally, I prefer to use the <a href="https://www.serverless.com/">Serverless framework</a> to deploy resources vs using the AWS Console.</p><p>We’ll create our Pool in a separate service from our triggers because our Serverless template needs to know about our Pool ahead of time in order to attach our triggers (more on this later).</p><p>First let’s create our project folder, then create our first Serverless template inside resources.yml</p><pre>mkdir web3-login<br>cd web3-login<br>touch resources.yml</pre><p>Then let’s edit our resources.yml to look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0590cbab263b1b7ff7462083e1ba248e/href">https://medium.com/media/0590cbab263b1b7ff7462083e1ba248e/href</a></iframe><p>This will create 2 resources for us. The first being our Cognito User Pool that will store all of our users and respond to auth events via triggers we’ll set up in the next section. The second resource is our Cognito User Pool Web Client. We’ll use this in our front end React app to communicate with our Pool.</p><p>When you’re ready, run this command to create the Pool resources:</p><pre>serverless deploy --config resources.yml</pre><p>Ok on to the next service! 🚀</p><h4><strong>Cognito Triggers Service</strong></h4><p>Cognito has a built in way to respond to authentication events via Lambda functions called ‘triggers’. The Serverless template below creates 4 lambda functions to respond to the following events: DefineAuthChallenge, CreateAuthChallenge, VerifyAuthChallenge, and PreSignUp. If you’d like to read more on when these triggers are called, check out the docs <a href="https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html">here</a>.</p><p>Let’s create a new file called triggers.yml in our project folder:</p><pre>touch triggers.yml</pre><p>We’ll walk though this template later but for now, edit your triggers.yml to look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd96a90eb248f1274289e0a464a680a4/href">https://medium.com/media/bd96a90eb248f1274289e0a464a680a4/href</a></iframe><p>Here we’re defining our 4 cognito triggers. When we deploy this service later, it will create 4 lambda functions, and attach them to our Pool for us.</p><p>Now let’s create these 4 trigger files in our project folder:</p><pre>touch defineAuthChallenge.js createAuthChallenge.js verifyAuthChallenge.js preSignUp.js</pre><p>Finally, let’s install a few npm modules to help us with signature verification:</p><pre>npm init --yes<br>npm install web3 ethereumjs-util</pre><p>Your folder contents should now look like this:</p><pre>web3-login/<br>  node_modules/<br>  resources.yml<br>  triggers.yml<br>  defineAuthChallenge.js<br>  createAuthChallenge.js<br>  verifyAuthChallenge.js<br>  preSignUp.js<br>  package.json</pre><h4>Trigger #1</h4><p>Lets dive in to our first trigger: defineAuthChallenge.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1d042b45085c8dc13eb2d39ac171751/href">https://medium.com/media/d1d042b45085c8dc13eb2d39ac171751/href</a></iframe><p>defineAuthChallenge.js will be triggered when our Pool receives a sign in request from our web client. On line 4, we’ll check to verify that this a valid sign in attempt type, `CUSTOM_CHALLENGE` , and not some other form of authentication like username/password or a social login. We only want to support signature login!</p><h4>Trigger #2</h4><p>Alright time for our second trigger: createAuthChallenge.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5a00b93de21722f94acc99c51ad9d75/href">https://medium.com/media/e5a00b93de21722f94acc99c51ad9d75/href</a></iframe><p>createAuthChallenge.js<em> </em>will be triggered when Cognito doesn’t have an auth challenge created yet for a sign in attempt. In this function, we’ll first need to verify that we have a user in this pool, and if not, return an error back to the client (more on this later). Lines 11 and 12 start the logic of signature verification. Essentially, we’ll need to create a message that our client will sign.</p><p>To prevent the user from logging in again with the same signature (in case it gets compromised), we make sure that the next time the same user wants to log in, they need to sign a new nonce. This is achieved by generating another random nonce for this user and attaching it to the session so we can verify it in the next trigger.</p><p>Note: Any data in publicChallengeParameters will be passed down to our client, and any data in privateChallengeParameters will only be available to our backend triggers.</p><h4>Trigger #3</h4><p>On to the third trigger! My personal favorite: verifyAuthChallenge.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa59bf4f9b3a935637478f047c240de6/href">https://medium.com/media/fa59bf4f9b3a935637478f047c240de6/href</a></iframe><p>This is the fun part. Cognito receives a the challenge answer input from our client which contains signature from our web3 provider. Now we need to verify if the address (the userName on our Cognito user), has signed the correct nonce.</p><p>Most providers will prepend \u0019Ethereum Signed Message:\n and the message length to every personal_sign request, so we’ll need to do the same here before we hash our message .</p><p>The next block is the verification itself. There is some cryptography involved. If you feel adventurous I recommend you reading more about <a href="https://blog.cloudflare.com/a-relatively-easy-to-understand-primer-on-elliptic-curve-cryptography/">elliptic curve</a> signatures.</p><p>To summarize this block, what it does is, given our privateChallengeParamters.message (containing the nonce) and our signature, the ecrecover function outputs the public address used to sign the privateChallengeParamters.message. If it matches our address on the Cognito user, then the user who made the request successfully proved their ownership of address. We consider them authenticated 🔥</p><h4>Trigger #4</h4><p>And last but not least, our last trigger: preSignUp.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd45906e6fd18638897bbba70cc0c39d/href">https://medium.com/media/bd45906e6fd18638897bbba70cc0c39d/href</a></iframe><p>This trigger is called immediately <em>before</em> a new cognito user is registered. This allows us to validate any info we need and even reject the sign up if we wanted to for any reason. In our case, we just want to auto confirm every user who signs up so that they can be issued JWTs after their signature is verified.</p><p>After you’ve got these files ready, lets run another deployment, but against our triggers service this time:</p><pre>serverless deploy --config triggers.yml</pre><p>And that’s our backend! Before we move to the front end, we need to grab a few IDs from our Serverless stack. In your project folder, run this command:</p><pre>serverless info --config resources.yml --verbose</pre><p>You’ll see something like this as a result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*V6QV0N7xTTaHxelANpOebA.png" /></figure><p>In the “Stack Outputs” section, copy down the UserPoolId<em> </em>and the UserPoolWebClient<em>.</em> We’ll be using both of those IDs in our front end ✍️</p><h4>Front End</h4><p>Check out the demo here: <a href="https://walletdemo.barrickapps.com/">walletdemo.barrickapps.com</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pt5btFVaBnmWDSmgeN9pDw.png" /></figure><p>Enter your UserPoolId , UserPoolWebClient , and Region where your Pool is deployed.</p><p>Next, you should see a “Connect Wallet” which will prompt the MetaMask dialog to pop and ask to connect a wallet, then sign your nonce.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*OxEdKfoQj-yFlqtcAm-rbw.png" /></figure><p>Once you sign, Cognito will run through your triggers, verify the MetaMask signature, and return back an authenticated Cognito user from your pool!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*3LZMfZu_0W9ka8BKih_djw.png" /></figure><p>If you made it this far, <em>congrats</em> on building a serverless wallet login flow! 🎉</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F14751159&amp;display_name=Tenor&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fcelebrate-party-the-office-kevin-gif-14751159&amp;image=https%3A%2F%2Fc.tenor.com%2F7KE3TH-N4IAAAAAM%2Fcelebrate-party.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4c3d16edf7e27c88dc09854603b3830/href">https://medium.com/media/d4c3d16edf7e27c88dc09854603b3830/href</a></iframe><p>Thanks for reading! If you’ve got any feedback or questions feel free to reach out to me on twitter: <a href="https://twitter.com/DavBarrick">@DavBarrick</a></p><p>The code for this demo and triggers above are here: <a href="https://github.com/DavidBarrick/cognito-wallet-login">https://github.com/DavidBarrick/cognito-wallet-login</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb93c723f4de" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>