The COSMOS team asked us to review and audit their new COSMOS Fundraiser code. We looked at their contracts and tools and now publish our results.
The audited projects are at their cosmos GitHub organization. The list of projects included in this audit are:
Our overall impression of the projects is that code quality is good, and that most security best-practices were followed.
Update: The COSMOS team implemented most of our recommendations in the latest version of their projects.
Here’s our assessment and recommendations, in order of importance:
We haven’t found any severe security problems with the code.
Use of send and call primitives is always risky and should be analyzed in detail. One occurrence of call found in line 91 of Fundraiser.sol.
Remove unneeded call.value()
Using call.value()
is potentially dangerous, and was responsible for the TheDAO hack. We couldn’t find a reason to use treasury.call.value(msg.value)()
instead of the simplertreasury.send(msg.value)
. We recommend making this change.
For more info on this problem, see this note.
There are many unchecked math operations in the code. It’s always better to be safe and perform checked operations. Consider using a safe math library, or performing pre-condition checks on any math operation.
blockchain.info has had problems in the past, and as the COSMOS team mentions on this note, their nodes don’t recognize OP_RETURN outputs as standard. Consider running your own nodes of insight-api or bitcoin-abe and connecting to those, to reduce the risk of fundraiser problems because of third party problems.
Line 10 in bitcoin.js of cosmos/fundraiser-lib defines a fee rate of 200 satoshis per byte. That’s the current cheapest and fastest fee rate according to https://bitcoinfees.21.co/, but it’s still not recommended to use a static fee rate. If there’s network congestion during the fundraiser, transactions could be stuck. Consider integrating bitcoinfees’ API to get recommended fee rate dynamically.
Some functions could use more precondition checking. This helps prevent critical bugs coming from programming or user input handling errors. For example, the function getAddress in line 20 of bitcoin.js of fundraiser-lib performs no checks on the pub
argument. If there’s a bug in the caller code and and invalid value is sent, an address will be returned, corresponding to no public key. Example:
waitForPayment function of bitcoin.js module in fundraiser-lib doesn’t handle correctly requests that take longer than 6 seconds, and could call the callback multiple times. Consider adding a reentrancy guard, as shown on this sample code:
maraoz/fundraiser-lib
fundraiser-lib – JS module for participating in Cosmos Fundraiser github.com
Fundraiser.sol is written for an old version of solc (0.4.7). We recommend changing the solidity version pragma for the latest version (pragma solidity ^0.4.10;
) to enforce latest compiler version to be used.
The rate of ATOMS per BTC is fixed in line 12 of bitcoin.js of fundraiser-lib. If fundraiser lasts the full length (14 days), price could change a significant amount. Consider adding dynamic ATOMS/BTC rate based on USD/BTC rate from a public API.
The key generation scheme proposed is not standard, consider using BIP32 and BIP44 to derive COSMOS, Bitcoin and Ethereum keys using standard paths. See how DFINITY did their key derivations for more info.
As mentioned in these notes, COSMOS address recording mechanism uses pay-to-pubkey-hash outputs, which can be seen in lines 102 to 104 of bitcoin.js of fundraiser-lib. This is a very inefficient method, because it creates unspendable outputs, bloating the UTXO set forever. A better approach is to use OP_RETURN outputs, as mentioned in the notes linked above. See how DFINITY did their address recording for more info.
COSMOS team asked us to further review their latest commits. The reviewed changes are:
We have the following observations on those changes:
No severe security issues were found. Some changes were recommended to follow best practices and reduce potential attack surface.
Update: The COSMOS team implemented most of our recommendations in the latest version of their projects.
Overall, code quality is good, it’s well commented, and most well-known security good practices were followed. 👍
Note that as of the date of publishing, the above review reflects the current understanding of known security patterns as they relate to the COSMOS fundraiser. We have not reviewed the related COSMOS network project. The above should not be construed as investment advice or an offering of tokens. For general information about smart contract security, check out our thoughts here.