|
| 1 | +--- |
| 2 | +id: access-keys |
| 3 | +title: Access Key Management |
| 4 | +sidebar_label: Access Key Management |
| 5 | +description: "Understand how function-call access keys enable gasless operations in NEAR Drop." |
| 6 | +--- |
| 7 | +import Tabs from '@theme/Tabs'; |
| 8 | +import TabItem from '@theme/TabItem'; |
| 9 | +import {Github} from "@site/src/components/codetabs" |
| 10 | + |
| 11 | +This is where NEAR gets really cool. Function-call access keys are what make gasless claiming possible - let's understand how they work! |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## The Problem NEAR Solves |
| 16 | + |
| 17 | +Traditional blockchains have a chicken-and-egg problem: |
| 18 | +- You need tokens to pay gas fees |
| 19 | +- But you need gas to receive tokens |
| 20 | +- New users are stuck! |
| 21 | + |
| 22 | +NEAR's solution: **Function-call access keys** that let you call specific functions without owning the account. |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## How Access Keys Work |
| 27 | + |
| 28 | +NEAR has two types of keys: |
| 29 | + |
| 30 | +**Full Access Keys** 🔑 |
| 31 | +- Complete control over an account |
| 32 | +- Can do anything: transfer tokens, deploy contracts, etc. |
| 33 | +- Like having admin access |
| 34 | + |
| 35 | +**Function-Call Keys** 🎫 |
| 36 | +- Limited permissions |
| 37 | +- Can only call specific functions |
| 38 | +- Like having a concert ticket - gets you in, but only to your seat |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## NEAR Drop's Key Magic |
| 43 | + |
| 44 | +Here's what happens when you create a drop: |
| 45 | + |
| 46 | +<Github fname="lib.rs" language="rust" |
| 47 | + url="https://github.com/Festivemena/Near-drop/blob/main/contract/src/lib.rs" |
| 48 | + start="140" end="170" /> |
| 49 | + |
| 50 | +**The result**: Recipients can claim tokens without having NEAR accounts or paying gas! |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Key Permissions Breakdown |
| 55 | + |
| 56 | +Function-call keys in NEAR Drop have strict limits: |
| 57 | + |
| 58 | +<Github fname="lib.rs" language="rust" |
| 59 | + url="https://github.com/Festivemena/Near-drop/blob/main/contract/src/lib.rs" |
| 60 | + start="8" end="20" /> |
| 61 | + |
| 62 | +**What keys CAN do:** |
| 63 | +- Call `claim_for` to claim to existing accounts |
| 64 | +- Call `create_account_and_claim` to create new accounts |
| 65 | +- Use up to 0.005 NEAR worth of gas |
| 66 | + |
| 67 | +**What keys CANNOT do:** |
| 68 | +- Transfer tokens from the contract |
| 69 | +- Call any other functions |
| 70 | +- Deploy contracts or change state maliciously |
| 71 | +- Exceed their gas allowance |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## Key Lifecycle |
| 76 | + |
| 77 | +The lifecycle is simple and secure: |
| 78 | + |
| 79 | +``` |
| 80 | +1. CREATE → Add key with limited permissions |
| 81 | +2. SHARE → Give private key to recipient |
| 82 | +3. CLAIM → Recipient uses key to claim tokens |
| 83 | +4. CLEANUP → Remove key after use (prevents reuse) |
| 84 | +``` |
| 85 | + |
| 86 | +Here's the cleanup code: |
| 87 | + |
| 88 | +<Github fname="claim.rs" language="rust" |
| 89 | + url="https://github.com/Festivemena/Near-drop/blob/main/contract/src/claim.rs" |
| 90 | + start="200" end="220" /> |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## Advanced Key Patterns |
| 95 | + |
| 96 | +### Time-Limited Keys |
| 97 | + |
| 98 | +You can make keys that expire: |
| 99 | + |
| 100 | +<Github fname="lib.rs" language="rust" |
| 101 | + url="https://github.com/Festivemena/Near-drop/blob/main/contract/src/lib.rs" |
| 102 | + start="300" end="330" /> |
| 103 | + |
| 104 | +### Key Rotation |
| 105 | + |
| 106 | +For extra security, you can rotate keys: |
| 107 | + |
| 108 | +<Github fname="lib.rs" language="rust" |
| 109 | + url="https://github.com/Festivemena/Near-drop/blob/main/contract/src/lib.rs" |
| 110 | + start="350" end="380" /> |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Security Best Practices |
| 115 | + |
| 116 | +**✅ DO:** |
| 117 | +- Use minimal gas allowances (0.005 NEAR is plenty) |
| 118 | +- Remove keys immediately after use |
| 119 | +- Validate key formats before adding |
| 120 | +- Monitor key usage patterns |
| 121 | + |
| 122 | +**❌ DON'T:** |
| 123 | +- Give keys excessive gas allowances |
| 124 | +- Reuse keys for multiple drops |
| 125 | +- Skip cleanup after claims |
| 126 | +- Log private keys anywhere |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## Gas Usage Monitoring |
| 131 | + |
| 132 | +Track how much gas your keys use: |
| 133 | + |
| 134 | +<Github fname="lib.rs" language="rust" |
| 135 | + url="https://github.com/Festivemena/Near-drop/blob/main/contract/src/lib.rs" |
| 136 | + start="400" end="420" /> |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## Integration with Frontend |
| 141 | + |
| 142 | +Your frontend can generate keys securely: |
| 143 | + |
| 144 | +<Github fname="crypto.js" language="javascript" |
| 145 | + url="https://github.com/Festivemena/Drop/blob/main/src/utils/crypto.js" |
| 146 | + start="1" end="30" /> |
| 147 | + |
| 148 | +Create claim URLs: |
| 149 | + |
| 150 | +<Github fname="crypto.js" language="javascript" |
| 151 | + url="https://github.com/Festivemena/Drop/blob/main/src/utils/crypto.js" |
| 152 | + start="32" end="45" /> |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## Troubleshooting Common Issues |
| 157 | + |
| 158 | +**"Access key not found"** |
| 159 | +- Key wasn't added properly to the contract |
| 160 | +- Key was already used and cleaned up |
| 161 | +- Check the public key format |
| 162 | + |
| 163 | +**"Method not allowed"** |
| 164 | +- Trying to call a function not in the allowed methods list |
| 165 | +- Our keys only allow `claim_for` and `create_account_and_claim` |
| 166 | + |
| 167 | +**"Insufficient allowance"** |
| 168 | +- Key ran out of gas budget |
| 169 | +- Increase `FUNCTION_CALL_ALLOWANCE` if needed |
| 170 | + |
| 171 | +**"Key already exists"** |
| 172 | +- Trying to add a duplicate key |
| 173 | +- Generate new unique keys for each drop |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## Why This Matters |
| 178 | + |
| 179 | +Function-call access keys are NEAR's superpower for user experience: |
| 180 | + |
| 181 | +🎯 **No Onboarding Friction**: New users can interact immediately |
| 182 | +⚡ **Gasless Operations**: Recipients don't pay anything |
| 183 | +🔒 **Still Secure**: Keys have minimal, specific permissions |
| 184 | +🚀 **Scalable**: Works for any number of recipients |
| 185 | + |
| 186 | +This is what makes NEAR Drop possible - without function-call keys, you'd need a completely different (and much more complex) approach. |
| 187 | + |
| 188 | +--- |
| 189 | + |
| 190 | +## Next Steps |
| 191 | + |
| 192 | +Now that you understand how the gasless magic works, let's see how to create new NEAR accounts during the claiming process. |
| 193 | + |
| 194 | +[Continue to Account Creation →](./account-creation.md) |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +:::tip Key Insight |
| 199 | +Function-call access keys are like giving someone a specific key to your house that only opens one room and only works once. It's secure, limited, and perfect for token distribution! |
| 200 | +::: |
0 commit comments