|
| 1 | +--- |
| 2 | +title: "NEAR Smart Contract Security" |
| 3 | +date: 2024-07-18T00:00:00+09:00 |
| 4 | +draft: true |
| 5 | +# lastmod: 2024-07-17 |
| 6 | +description: "Good practices for writing NEAR smart contracts" |
| 7 | +author: Jacob Lindahl |
| 8 | +twitter: sudo_build |
| 9 | +math: true |
| 10 | +license: |
| 11 | + name: CC BY-SA 4.0 |
| 12 | + link: https://creativecommons.org/licenses/by-sa/4.0/ |
| 13 | +--- |
| 14 | + |
| 15 | +I have extensive experience with writing secure smart contracts for the NEAR blockchain. Throughout my tenure at [NEAR Foundation](https://near.foundation/) and [Pagoda](https://www.pagoda.co/), I have accumulated over three years of experience with the platform, having contributed to core libraries and security-critical features. |
| 16 | + |
| 17 | +Among my experience are: |
| 18 | + |
| 19 | +- Author of the library [near-sdk-contract-tools](https://github.com/near/near-sdk-contract-tools), a security-first library that implements many of the NEAR contract standards, [audited by Kudelski Security](https://github.com/near/near-sdk-contract-tools/blob/develop/documents/NEAR%20Contract%20Tools%20-%20Final%20-%2005.05.2023.pdf). |
| 20 | +- Technical lead on [the native USDC integration with NEAR Protocol](https://near.org/blog/usdc-launches-natively-on-the-near-protocol), where I assisted the Circle team in writing the USDC on NEAR smart contract and performed an internal audit. |
| 21 | +- Internal audits for several other partner smart contracts. |
| 22 | +- Primary author of the [Nuffle Labs](https://nuff.tech/) (formerly NEAR Data Availability) [blob store smart contract](https://github.com/Nuffle-Labs/data-availability/tree/main/contracts/blob-store). |
| 23 | +- Author of the NEAR Multichain [gas station](https://github.com/near/multichain-gas-station-contract/tree/master/gas_station) and [NFT chain key](https://github.com/near/multichain-gas-station-contract/tree/master/nft_key) smart contracts. |
| 24 | +- [Secureum](https://www.secureum.xyz/) graduate. |
| 25 | + |
| 26 | +Besides these, I have assisted countless community members and ecosystem projects with smart contract authorship and security-related topics, including holding seminars, performing code reviews, and providing technical support. |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +When writing smart contracts on NEAR, many of the same general principles apply as do when writing Solidity smart contracts: |
| 31 | + |
| 32 | +- Validate all your inputs. |
| 33 | +- Don't trust users to act honestly or smart contracts to be implemented correctly. |
| 34 | +- Beware of invalid intermediate state.[^cei] |
| 35 | +- […and so on.]({{< ref "blog/modern-dearth-of-intelligent-crypto#the-well-tempered-contract-developer" >}}) |
| 36 | + |
| 37 | +[^cei]: This is a generalization of the concept Solidity developers know as a "reentrancy" vulnerability, usually addressed by the [_Checks-Effects-Interactions_ order of operations](https://docs.soliditylang.org/en/latest/security-considerations.html#reentrancy). |
| 38 | + |
| 39 | +Therefore, rather than regurgitate tired principles well-visited elsewhere, I shall provide practical advice from my own experience in the effective development of NEAR smart contracts. |
| 40 | + |
| 41 | +## Security |
| 42 | + |
| 43 | +### Numbers |
| 44 | + |
| 45 | +- Arithmetic overflow |
| 46 | +- Large integer serialization |
| 47 | + |
| 48 | +### Cross-contract calls |
| 49 | + |
| 50 | +- Gas |
| 51 | +- Argument serialization in callbacks |
| 52 | +- Callback protection |
| 53 | +- Writing ext_\* traits to be maximally flexible |
| 54 | + |
| 55 | +### Serialization |
| 56 | + |
| 57 | +- Newtypes & wrappers |
| 58 | + |
| 59 | +### Working with NEP standards |
| 60 | + |
| 61 | +### Constructors |
| 62 | + |
| 63 | +### Storage |
| 64 | + |
| 65 | +- SDK collections |
| 66 | +- Prefixing & storage keys |
| 67 | +- Deleting collections |
| 68 | +- Avoiding soft-locking |
| 69 | + |
| 70 | +### Writing tests |
| 71 | + |
| 72 | +- `near-workspaces`, dynamic compilation |
| 73 | + |
| 74 | +### Enforcing before and after invariants with predicates |
| 75 | + |
| 76 | +### Calculating refunds |
| 77 | + |
| 78 | +### Feature flags |
| 79 | + |
| 80 | +### Forcing signatures from a full-access key |
| 81 | + |
| 82 | +- (or contract) |
| 83 | +- Smart contract VM doesn't currently provide access key inspection |
| 84 | + |
| 85 | +### ABI |
| 86 | + |
| 87 | +### Source code verification |
| 88 | + |
| 89 | +## Optimization |
| 90 | + |
| 91 | +### Avoiding `.unwrap()` |
| 92 | + |
| 93 | +### Being conscious of dynamic allocations |
| 94 | + |
| 95 | +- Thrashing the allocator |
| 96 | +- String operations |
| 97 | + |
| 98 | +### Ditching the SDK |
| 99 | + |
| 100 | +### Custom JSON deserialization |
| 101 | + |
| 102 | +### Post-compilation WASM blob |
0 commit comments