<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sundeep Charan Ramkumar</title>
    <description>The latest articles on DEV Community by Sundeep Charan Ramkumar (@sundeep_charan).</description>
    <link>https://dev.to/sundeep_charan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F175459%2Fe9c82071-b414-4aad-9481-498b0ef70e2d.jpeg</url>
      <title>DEV Community: Sundeep Charan Ramkumar</title>
      <link>https://dev.to/sundeep_charan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sundeep_charan"/>
    <language>en</language>
    <item>
      <title>Program Derived Addresses - A Deep Dive</title>
      <dc:creator>Sundeep Charan Ramkumar</dc:creator>
      <pubDate>Sun, 25 Feb 2024 05:52:55 +0000</pubDate>
      <link>https://dev.to/sundeep_charan/program-derived-addresses-a-deep-dive-1a2k</link>
      <guid>https://dev.to/sundeep_charan/program-derived-addresses-a-deep-dive-1a2k</guid>
      <description>&lt;h2&gt;
  
  
  Chapters
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction to cryptographic pairs&lt;/li&gt;
&lt;li&gt;Elliptic Curves&lt;/li&gt;
&lt;li&gt;Message Signing&lt;/li&gt;
&lt;li&gt;PDAs and how it differs from Ed25519&lt;/li&gt;
&lt;li&gt;Use cases of PDAs&lt;/li&gt;
&lt;li&gt;Ephemeral Signers&lt;/li&gt;
&lt;li&gt;Squads Protocol&lt;/li&gt;
&lt;li&gt;Utilisation of PDAs in Squads&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Introduction to cryptographic pairs
&lt;/h2&gt;

&lt;p&gt;Before diving deep into cryptographic pairs, one must understand what even is a cryptographic pair. Let's say you are living in a shared apartment where people have the same key for the lock that you are using to lock/unlock the door. Multiple key duplicates to access one encrypted data point. This is what we call &lt;strong&gt;symmetric cryptography&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Now again, let's say that you are living in the same shared apartment, but instead of multiple people, it's you and the apartment owner living together. The apartment owner lets you lock the door, but not being able to unlock it, for safety reasons. Only the owner who possesses the unlock key can be able to unlock the door for you. This is basically what &lt;strong&gt;asymmetric cryptography&lt;/strong&gt; is. The tenant has the benefits of living inside the house and accessing the data point, but the key to accessing it lies only under the apartment's owner. Similarly, in cryptography, the person who has the private key for a public address doesn't need to share the private key, but still be able to send/allow the intended message to another person. In real-world scenarios, people mix and match asymmetric and symmetric that fulfill their use cases.&lt;/p&gt;

&lt;p&gt;Now that we have a basic idea about what the basics of symmetrical/asymmetrical cryptography are, here is a well-known/used list of asymmetric cryptographic keypairs. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.ibm.com/docs/en/zos/2.1.0?topic=keys-rsa-private-public" rel="noopener noreferrer"&gt;RSA&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.google.com/search?q=DSA+keypair&amp;amp;sca_esv=130c2c2530734d82&amp;amp;sxsrf=ACQVn0-Aqg7A9q-l-uIDeDx2wdK2LNzARA%3A1708153850603&amp;amp;ei=-lvQZeW5JIyKseMP6-SG0Aw&amp;amp;ved=0ahUKEwjlnvL76LGEAxUMRWwGHWuyAcoQ4dUDCBA&amp;amp;uact=5&amp;amp;oq=DSA+keypair&amp;amp;gs_lp=Egxnd3Mtd2l6LXNlcnAiC0RTQSBrZXlwYWlyMgcQABiABBgNMggQABgFGB4YCjIGEAAYCBgeMggQABgFGB4YDTILEAAYgAQYigUYhgMyCxAAGIAEGIoFGIYDMgsQABiABBiKBRiGA0jBBlCmAljgBHABeAGQAQCYAW6gAdoBqgEDMC4yuAEDyAEA-AEBwgIKEAAYRxjWBBiwA8ICBhAAGB4YDYgGAZAGCA&amp;amp;sclient=gws-wiz-serp" rel="noopener noreferrer"&gt;DSA&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.hypr.com/security-encyclopedia/elliptic-curve-digital-signature-algorithm#:~:text=The%20Elliptic%20Curve%20Digital%20Signature%20Algorithm%20(ECDSA)%20is%20a%20Digital,elliptic%20curve%20cryptography%20(ECC)." rel="noopener noreferrer"&gt;ECDSA&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cryptobook.nakov.com/digital-signatures/eddsa-sign-verify-examples" rel="noopener noreferrer"&gt;EdDSA&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and much more ...&lt;/p&gt;

&lt;p&gt;We will focus on &lt;strong&gt;EdDSA&lt;/strong&gt;, specifically &lt;strong&gt;Ed25519&lt;/strong&gt;, and how we can create Program Derived Addresses in Solana.&lt;/p&gt;




&lt;h2&gt;
  
  
  Elliptic Curves
&lt;/h2&gt;

&lt;p&gt;The Edwards curve Digital Signature Algorithm or EdDSA in short, uses a mathematical elliptic curve. An elliptic curve is plotted over a graph via the function&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;y2 = x3 + ax + b&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
. It looks visually like the image beneath.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A1100%2Fformat%3Awebp%2F1%2A2PxXNwMceh1XC_waAP9NiA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A1100%2Fformat%3Awebp%2F1%2A2PxXNwMceh1XC_waAP9NiA.jpeg" alt="Elliptic Curve"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But why use an elliptic curve even? Not going to fry your brains for a technical approach, but basically, it's way harder to brute-force crack an EdDSA private key than an RSA private key. It's extremely popular and a common tool used in encryption-based requirements, like government internal communications and text message encryptions. The structure of an EdDSA is similar to RSA (having private and public keys), but the way the public key is derived is where the security is possessed. Whilst we do have many variants in EdDSA like &lt;strong&gt;Ed448-Goldilocks&lt;/strong&gt;, &lt;strong&gt;Ed25519ph&lt;/strong&gt;, and &lt;strong&gt;Ed25519ctx&lt;/strong&gt;, we are going to deal with &lt;em&gt;Ed25519&lt;/em&gt; as that is the keypair Solana and adopted globally for message communication (signatures and verification). If one requires just the asymmetric part, one can go ahead with &lt;strong&gt;curve 25519&lt;/strong&gt;. A deep technical walkthrough about EdDSA and Ed25519 by &lt;strong&gt;Cloudflare&lt;/strong&gt; is explained &lt;a href="https://blog.cloudflare.com/a-relatively-easy-to-understand-primer-on-elliptic-curve-cryptography" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Message Signing
&lt;/h2&gt;

&lt;p&gt;A message is nothing but the context the private key is used for. Signing a message essentially helps us verify that the person whom we expected to receive from is the one who sent it, and not spoofed by someone else. We can see some examples here. User A wants to send a text message to User B. User A uses their private key to &lt;em&gt;sign&lt;/em&gt; the text message, and as a result, the signature as well as the message is sent to User B. User B now wants to verify if the message is sent by User A, by simply checking the signature and the public key of User A. &lt;/p&gt;

&lt;p&gt;In Solana, users transfer messages for not just text purposes but also for financial purposes. NFT transfers, listing, and market orders are all examples of the simple scenario explained above. All regular consumers in Solana such as hot wallets, ledgers, and Solana seed vaults in Saga mobiles use this mechanism to sign messages. &lt;/p&gt;


&lt;h2&gt;
  
  
  PDAs and how it differs from Ed25519
&lt;/h2&gt;

&lt;p&gt;Now that we have a brief idea so far regarding how keypairs work and what it takes to sign a message that's valuable to the receiver, there is a hybrid version of keypairs in Solana called &lt;strong&gt;Program Derived Addresses&lt;/strong&gt; or &lt;strong&gt;PDA&lt;/strong&gt;s in short. In the &lt;em&gt;Elliptic Curves&lt;/em&gt; section, we have seen the graph that's plotted under a certain equation right? The normal Ed25519 keypairs lie on either curve point over that graph. These normal keypairs as we mentioned earlier, are usually owned by the &lt;strong&gt;&lt;em&gt;&lt;a href="https://explorer.solana.com/address/11111111111111111111111111111111" rel="noopener noreferrer"&gt;System Program&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt; and they are the wallets we use daily. However, where PDA differs is that they neither lie on the curve, nor they do not have a private key. So just to summarize, a PDA differs from a regular keypair via two things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does not possess a private key&lt;/li&gt;
&lt;li&gt;Provide an option to be owned by the creator's program (also through the System program but we will discuss this later)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But that brings us to the next question, if a PDA does not have a private key, how could it sign a transaction's message and securely send it over to the receiver? That's where we have a seeding/salting mechanism. The following process is how we derive/use a PDA in Solana&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We pass a random array of strings (usually defining what the PDA is for), that get converted to &lt;a href="https://dicom.nema.org/dicom/2013/output/chtml/part05/sect_7.3.html#:~:text=Little%20Endian%20byte%20ordering%20is,in%20increasing%20order%20of%20significance." rel="noopener noreferrer"&gt;little endian&lt;/a&gt; bytes. This is known as the &lt;strong&gt;seeds array&lt;/strong&gt;. The random array of strings doesn't mean it generates randomly every time, but the choice of seeds is random. But once the seeds are created/generated, one must choose those seeds in the same order it was created/generated, as they all get converted to &lt;strong&gt;Unsigned integer array of each number being under 8 bytes&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The hash function tries to derive an address and checks if the resultant public key is generated through the seeds and not lying inside the ed25519 curve.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we still aren't successful in keeping out from the curve, we &lt;em&gt;iterate&lt;/em&gt; through a range from 0 - 255 and apply this number alongside the &lt;strong&gt;seeds array&lt;/strong&gt;. This number variation is known as &lt;strong&gt;bump&lt;/strong&gt;. A PDA usually consists of the initial seeds and a numeric bump value. They together are referred to as the &lt;strong&gt;signer seeds&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When we require a PDA to sign for a transaction, instead of the private key we would be using normally, we would be using these &lt;strong&gt;signer_seeds&lt;/strong&gt; as a replacement, and then sign the transaction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The receiving user can now verify the same way how they would do so for a regular keypair signature.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The following core logic snippet is how a PDA is generated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;try_find_program_address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seeds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="n"&gt;program_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Pubkey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pubkey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// We start with the bump from 255 and iterate to 0&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;bump_seed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Collecting the seeds (the array of strings)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;seeds_with_bump&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;seeds&lt;/span&gt;&lt;span class="nf"&gt;.to_vec&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;// We then append the digit that is currently iterating through with the seeds array.&lt;/span&gt;
        &lt;span class="n"&gt;seeds_with_bump&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;bump_seed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// We try creating the pubkey through the current combination and the program ID &lt;/span&gt;
        &lt;span class="c1"&gt;// that becomes the owner of this address &lt;/span&gt;
        &lt;span class="c1"&gt;// and check if it's a valid off-curve address.&lt;/span&gt;
       &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_program_address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;seeds_with_bump&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;program_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bump_seed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// If we fail to find one, we continue through the next iteration, subtracting the current value by 1.&lt;/span&gt;
        &lt;span class="n"&gt;bump_seed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="nb"&gt;None&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this brings us to the next question. Why even spend all this effort to create/use/utilize a PDA? Why not just assign servers/bots random keypairs and store them in a secured place to sign them when it's needed? That brings us to the next topic of &lt;em&gt;Use cases&lt;/em&gt; of PDAs. But in short, signing transactions using keypairs is always less secure and anyone can spoof it when the keypair gets compromised.&lt;/p&gt;




&lt;h2&gt;
  
  
  Use Cases of PDAs
&lt;/h2&gt;

&lt;p&gt;Let's now take some of the use cases where PDAs might be useful. Automation and trustless patterns are the core usages used through PDAs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Escrow-based systems
&lt;/h3&gt;

&lt;p&gt;This is one of the classic examples where programmatic signatures are required. User A requires X amount of tokens for Y amount of currently holding tokens. User A then proceeds to deposit the Y amount of tokens to the escrow program. Here, the escrow program has a PDA which becomes the spokesperson on behalf of the program. This PDA now has the token account as its authority and receives the Y amount of tokens. When User B agrees to send X amount of tokens for the already deposited Y amount of tokens, the PDA now repeats the same actions for the X amount of tokens, and then automatically transfers the required X tokens to User A and Y tokens to User B.&lt;/p&gt;

&lt;p&gt;Manual transfers would lead to trust issues and compromises, but this would be the best advocate for to use of PDAs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Temporary Delegates
&lt;/h3&gt;

&lt;p&gt;Let's assume that you need a staking platform where the user is not willing to transfer the token, but still be able to receive the APY rewards. Scenarios like these help PDAs freeze the user's NFT in their wallet so that they won't be able to transfer the token, while still being able to get rewarded. &lt;/p&gt;

&lt;h3&gt;
  
  
  Programmatic investments through algorithmic findings
&lt;/h3&gt;

&lt;p&gt;Certain Decentralized finance applications split through the user's investments and get the maximum yields through depositing user funds to a PDA and then the PDA behaves as a vault to invest/withdraw funds from certain protocols based on the user's risk parameters. Applications like this allow users to not have the hassle of remembering more than 1 address and constantly having to monitor the markets' volatility.&lt;/p&gt;

&lt;p&gt;Another key functionality of PDA is something known as &lt;strong&gt;program derived ephemeral signers&lt;/strong&gt; which is what we are going to learn more about in the next section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dictionary-based data storage
&lt;/h3&gt;

&lt;p&gt;PDAs can also be used for on-chain storage of individual user details. For example, let's say an on-chain social platform is present. Each user profile can be made as a PDA with the following seeds&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;['user', 'social platform', user. pubkey, created_timestamp]&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 and then assign certain data points to that address, essentially making it a key-value pair (key being the PDA and value being the data object).&lt;/p&gt;


&lt;h2&gt;
  
  
  Ephemeral Signers
&lt;/h2&gt;

&lt;p&gt;Before talking about ephemeral signers, let's talk about a certain instruction in System Program called &lt;code&gt;CreateAccount&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cd"&gt;/// Create a new account&lt;/span&gt;
&lt;span class="cd"&gt;/// # Account references&lt;/span&gt;
&lt;span class="cd"&gt;///   0. `[WRITE, SIGNER]` Funding account&lt;/span&gt;
&lt;span class="cd"&gt;///   1. `[WRITE, SIGNER]` New account&lt;/span&gt;
&lt;span class="n"&gt;CreateAccount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// Number of imports to transfer to the new account&lt;/span&gt;
    &lt;span class="n"&gt;lamports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="cd"&gt;/// Number of bytes of memory to allocate&lt;/span&gt;
    &lt;span class="n"&gt;space&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="cd"&gt;/// Address of the program that will own the new account&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Pubkey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new account as well as the funding account would need to be signed. This means, that the account that we are creating needs to be signing the signing the transaction along with the user who is creating that account. The following way is how we would create the transaction from the client side. (We have ignored all the unnecessary code, and just focussed on the required part for this topic)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createAccount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;RPC_URL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userKeypair&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Keypair&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromSecretKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userKeypairBytes&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="c1"&gt;// Generating a keypair to create an account&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;accountKeypair&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Keypair&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createAccountIx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;SystemProgram&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createAccount&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;fromPubkey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;userKeypair&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;lamports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;space&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;SIZE_OF_THE_ACCOUNT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;newAccountPubkey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;accountKeypair&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;programID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DESTINATION_PROGRAM_ID&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// Creating transaction (cut short here due to context)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;createAccountIx&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// Signing part&lt;/span&gt;
    &lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userKeypair&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accountKeypair&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// The account that is created&lt;/span&gt;

    &lt;span class="c1"&gt;// Transaction being sent&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The line where &lt;code&gt;tx.sign(accountKeypair)&lt;/code&gt; is the part where the account needs to have its signature privileges. However, the moment when this transaction is created, we wouldn't need this keypair forever, because the instructions involving this account moving forward would just expect the authority of this account to be signing, and not this account's signing privileges. In other words, this account keypair's &lt;strong&gt;short-lived&lt;/strong&gt; is also known as &lt;strong&gt;ephemeral signer&lt;/strong&gt;. An ephemeral signer is just like a regular key pair, but since its purpose is short-lived and no one stores the key pair of the account, the resultant account would never be able to interact on its behalf after the initialization. &lt;/p&gt;

&lt;p&gt;We can have a question now, why even think about ephemeral signers in the first place? Well, in short, regular ephemeral signers cannot be called inside smart contracts to be invoked, as we have to either make a delegate to that account's keypair or make a &lt;strong&gt;programmable ephemeral signer&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;That is exactly what we are going to discuss about in the next topic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Squads protocol
&lt;/h2&gt;

&lt;p&gt;Squads protocol is a multi-sig smart wallet platform where users can create transactions and similar members can either approve or reject the transaction, resulting in maximum governance facilities and trustless methodologies. &lt;/p&gt;

&lt;p&gt;Sqauds protocol maximizes PDAs to its maximum to provide multiple features for consumers. The 2 main primary features of Squads protocols are leveraging the following concepts&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Creating a smart wallet using PDAs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ability to assign programmatic ephemeral signers through PDAs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assigning spending limits through PDAs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ability to batch transactions through PDAs&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will be discussing both of these points in the upcoming topic via referring to the &lt;a href="https://github.com/Squads-Protocol/v4" rel="noopener noreferrer"&gt;squads-v4&lt;/a&gt; protocol repository.&lt;/p&gt;




&lt;h2&gt;
  
  
  Utilisation of PDAs in Squads
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Creating a smart wallet using PDAs
&lt;/h3&gt;

&lt;p&gt;Here at Squads, A multi-sig is created through the following seeds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;multisig_seeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;b"multisig"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;b"multisig"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;create_key&lt;/span&gt;&lt;span class="nf"&gt;.key&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.as_ref&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// A random key that is used for seeding&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The multi-sig PDA acts as the key-value pair we discussed earlier, essentially storing important information like the signing threshold, the members that are within the multi-sig, the config authority who can change these settings, and much more. &lt;/p&gt;

&lt;p&gt;A smart wallet is essentially using a PDA with the following seeds. This is referred to as Vault.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;vault_seeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;b"multisig"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;multisig_key&lt;/span&gt;&lt;span class="nf"&gt;.as_ref&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// multi-sig's PDA address&lt;/span&gt;
    &lt;span class="s"&gt;b"vault"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.vault_index&lt;/span&gt;&lt;span class="nf"&gt;.to_le_bytes&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// defaulted to 0&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A key difference between Multisig's PDA and the Smart Wallet's PDA is that, in Multisig's PDA, the owner of the public key is the Squads program ID itself, whereas in Vault PDA, we &lt;em&gt;do not&lt;/em&gt; create the account itself, because if the account is created, then it won't be able to be under the System program to act as a normal wallet but become a restricted address to be used within the bounds of the Squads program. &lt;/p&gt;

&lt;p&gt;One good example is sending SOL to a random address. A PDA should have special instructions (such as Lamport arithmetic and checks for balance) to do a System Instruction, but where with this setting, we can simply create a System Program's transfer instruction, and it would work just like a regular Phantom/Backpack/Solflare wallet. If you remember during the introductory section of PDAs, we referred that a PDA can be declared either under the owned program or the system program. &lt;/p&gt;

&lt;p&gt;The smart wallet's PDA is like a hybrid version of being able to programmatically sign under the squads' program, as well as having the potential to hold assets just like a normal wallet.&lt;/p&gt;

&lt;p&gt;Another key reason why smart wallets are PDAs is that they can be executed at any point in time without worrying about the expiration of block hash and other client-side limitations. A transaction message is deserialized and sent as an instruction argument, which is stored under a &lt;strong&gt;transaction&lt;/strong&gt; PDA.&lt;/p&gt;

&lt;p&gt;The transaction PDA is then read during execution and the vault PDA executes the transaction wherever that account is marked as a signer.&lt;/p&gt;

&lt;p&gt;So, in summary, the vault PDA essentially becomes a normal wallet address, but with the additional feature of multi-sig approvals.&lt;/p&gt;




&lt;h3&gt;
  
  
  Program-derived Ephemeral Signers
&lt;/h3&gt;

&lt;p&gt;As we said earlier, ephemeral signers are short-lived and serve no purpose after the account responsible is created through the System program, but since we can't execute the transaction on a chain from just private key bytes, and have the need to execute in a delayed fashion (after the decision of voting approval), Squads once again leverage PDAs through &lt;strong&gt;program derived ephemeral signers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's say we have a transaction of 2 different instructions&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Creating a mint account&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating a nonassociated token account (plain keypair)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both mint accounts and normal token accounts require ephemeral keypairs to be added as signers. We create a transaction generating the above transaction and deserializing it. The general seed pattern for a program-derived ephemeral signer is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ephemeral_signer_seeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;b"multisig"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;transaction_key&lt;/span&gt;&lt;span class="nf"&gt;.as_ref&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// The transaction state PDA (key-value pair technique)&lt;/span&gt;
    &lt;span class="s"&gt;"ephemeral_signer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ephemeral_signer_index&lt;/span&gt;&lt;span class="nf"&gt;.to_le_bytes&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// The index order of the total ephemeral signer count&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we need about 2 ephemeral signers, we would start from index 0 to the count length - 1 as the &lt;strong&gt;ephemeral signer index&lt;/strong&gt;. &lt;br&gt;
In this way, after the decision amongst members has been made to approve it, the ephemeral signers are passed as signers inside the transaction, and sent as CPI instructions. &lt;/p&gt;

&lt;p&gt;By this, not only are we able to save the signer's keypair (basically the signer seeds) from executing at a later point in time, but we also allow the Squads program to sign as well programmatically. This allows complete automation of transaction executions. &lt;/p&gt;

&lt;p&gt;Program-derived ephemeral signers, in summary, help us to create any instruction that requires short-lived keypairs, through PDAs.&lt;/p&gt;
&lt;h3&gt;
  
  
  Spending limits
&lt;/h3&gt;

&lt;p&gt;In squads v4, either through the multisig config authority or through a config transaction, one can create a &lt;strong&gt;spending limit&lt;/strong&gt; PDA. This allows users inside the multisig, to define certain spending limits for certain tokens (including SOL and SPL). A spending limit PDA consists of the following seeds&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;spending_limit_seeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;b"multisig"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;multisig&lt;/span&gt;&lt;span class="nf"&gt;.key&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.as_ref&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// multisig PDA&lt;/span&gt;
    &lt;span class="s"&gt;b"spending_limit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="py"&gt;.create_key&lt;/span&gt;&lt;span class="nf"&gt;.as_ref&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// Random key to aid in random generation&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once this limit PDA is created, it has the necessary data to limit the spending usage either per day week or month. The spending limit is also created, to make sure the redundant vault transactions aren't created and directly using spending limit PDA just for token transfer transactions. &lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Thus, we have learned why PDA exists in Solana, and how we can leverage such powerful features through PDAs that help benefit the mass consumer market.&lt;/p&gt;

</description>
      <category>solana</category>
      <category>pda</category>
      <category>rust</category>
      <category>squads</category>
    </item>
  </channel>
</rss>
