Repository: summa-tx/bitcoin-spv (language: rust)

Issue:
Unnecessary logic in BytesLib.toBytes32()
#### Description

The heavily used library function `BytesLib.toBytes32()` unnecessarily casts `_source` to `bytes` (same type) and creates a copy of the dynamic byte array to check it's length, while this can be done directly on the user-provided `bytes _source`.

#### Examples

https://github.com/summa-tx/bitcoin-spv/blob/f5e4da091a1c97e6432c2d70eba434edb189f919/solidity/contracts/BytesLib.sol#L399-L408

#### Recommendation

```solidity
function toBytes32(bytes memory _source) pure internal returns (bytes32 result) {
        if (_source.length == 0) {
            return 0x0;
        }

        assembly {
            result := mload(add(_source, 32))
        }
    }
```



Discussion / hints:

Fix patch:
diff --git a/solidity/contracts/BytesLib.sol b/solidity/contracts/BytesLib.sol
index bf409800..e37936d6 100644
--- a/solidity/contracts/BytesLib.sol
+++ b/solidity/contracts/BytesLib.sol
@@ -400,8 +400,7 @@ library BytesLib {
     }
 
     function toBytes32(bytes memory _source) pure internal returns (bytes32 result) {
-        bytes memory tempEmptyStringTest = bytes(_source);
-        if (tempEmptyStringTest.length == 0) {
+        if (_source.length == 0) {
             return 0x0;
         }
 


Test patch: