Skip to content

Commit

Permalink
v0.1.1 (#6)
Browse files Browse the repository at this point in the history
* add release-package.yml

* patch package scope

* patch repository url

* patch homepage

* Unstructured storage (#4)

* bump version

* UnstructuredStorageLib

* rename to keystore, add initializablekeystore

* rename InitializableKeyStored

* patch doc

* CloneFactory.sol

* Add ArrayLib helper functions

* change org name

* fix npm ignore files (#5)

* v0.1.1 + docs
  • Loading branch information
leovigna authored Mar 13, 2021
1 parent 8db056f commit 84a886f
Show file tree
Hide file tree
Showing 19 changed files with 684 additions and 67 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/release-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ on:
types: [created]

jobs:
build:
publish-npmjs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: "12.16"
registry-url: https://registry.npmjs.org/
# Set login for package registries
- run: npm set //registry.npmjs.org/:_authToken=${{secrets.NPMJS_TOKEN}}
- run: npm set //npm.pkg.github.com/:_authToken=${{secrets.GPR_TOKEN}}
- run: npm install
- run: npm test
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPMJS_TOKEN}}

publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: "12.16"
registry-url: https://npm.pkg.github.com/
# Set login for package registries (only required for private dependencies)
- run: npm set //registry.npmjs.org/:_authToken=${{secrets.NPMJS_TOKEN}}
- run: npm set //npm.pkg.github.com/:_authToken=${{secrets.GPR_TOKEN}}
- run: npm install
- run: npm publish
env:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# build output
build/
migrations/
test/
!contracts/test/
types/truffle-contracts/
Expand Down
42 changes: 41 additions & 1 deletion contracts/Array/ArrayLibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ library ArrayLibUtils {

/**
* @dev Concatenate byte arrays
* @param arrayOfArrays of uint256
* @param arrayOfArrays of bytes
* @return concatenated
*
* This function is especially useful when looking to concatenate a
Expand All @@ -89,4 +89,44 @@ library ArrayLibUtils {

return newArray;
}

/**
* @dev Concatenate byte arrays
* @param arrayOfArrays of uint256
* @return concatenated
*/
function concat(uint256[][] memory arrayOfArrays) internal pure returns (uint256[] memory) {
uint256 arrayCount = arrayOfArrays.length;
uint256 totalLength = 0;
for (uint256 i = 0; i < arrayCount; i++) {
totalLength += arrayOfArrays[i].length;
}

uint256[] memory newArray = new uint256[](totalLength);
uint256 k = 0;
for (uint256 i = 0; i < arrayCount; i++) {
for (uint256 j = 0; j < arrayOfArrays[i].length; j++) {
newArray[k] = arrayOfArrays[i][j];
k++;
}
}

return newArray;
}

/**
* @dev Return range array
* @param from start index
* @param to end index, exclusive
* @return rangeArray
*
*/
function range(uint256 from, uint256 to) internal pure returns (uint256[] memory rangeArray) {
require(from < to, 'from >= to');

rangeArray = new uint256[](to - from);
for (uint256 i = 0; i < rangeArray.length; i++) {
rangeArray[i] = from + i;
}
}
}
19 changes: 0 additions & 19 deletions contracts/Migrations.sol

This file was deleted.

78 changes: 78 additions & 0 deletions contracts/Proxy/CloneFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
pragma solidity ^0.6.0;

//from https://github.com/optionality/clone-factory/blob/master/contracts/CloneFactory.sol
//explainer at https://blog.openzeppelin.com/deep-dive-into-the-minimal-proxy-contract/
//also see https://eips.ethereum.org/EIPS/eip-1167

//WARNING: READ BELOW to understand differences with original contract
/**
* Modified from @optionality.io/clone-factory/contracts/CloneFactory.sol
* - Added support for CREATE2-deployed minimal proxy.
*/

/**
* @dev EIP1167 Minimal Proxy
*/

/*
The MIT License (MIT)
Copyright (c) 2018 Murray Software, LLC.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//solhint-disable max-line-length
//solhint-disable no-inline-assembly

contract CloneFactory {
//CHANGE from REFERENCE implementation by Leo VIGNA
//Added create2 support
function create2Clone(address target, bytes32 salt) internal returns (address result) {
bytes20 targetBytes = bytes20(target);
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone, 0x14), targetBytes)
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
result := create2(0, clone, 0x37, salt)
}
}

function createClone(address target) internal returns (address result) {
bytes20 targetBytes = bytes20(target);
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone, 0x14), targetBytes)
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
result := create(0, clone, 0x37)
}
}

function isClone(address target, address query) internal view returns (bool result) {
bytes20 targetBytes = bytes20(target);
assembly {
let clone := mload(0x40)
mstore(clone, 0x363d3d373d3d3d363d7300000000000000000000000000000000000000000000)
mstore(add(clone, 0xa), targetBytes)
mstore(add(clone, 0x1e), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)

let other := add(clone, 0x40)
extcodecopy(query, other, 0, 0x2d)
result := and(eq(mload(clone), mload(other)), eq(mload(add(clone, 0xd)), mload(add(other, 0xd))))
}
}
}
82 changes: 82 additions & 0 deletions contracts/Proxy/InitializableKeyStored.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: MIT

// solhint-disable-next-line compiler-version
pragma solidity >=0.4.24 <0.8.0;

import '../Storage/KeyStoreLib.sol';

//WARNING: READ BELOW to understand differences with original contract
/** Modified from @openzeppelin/contracts/proxy/Initializable.sol
* Contracts that inherit from a parent class often can't also
* be made Initializable because of conflicts with parent contract's
* storage layer. The solution until now was to make the parent contract
* Initializable but this cannot always be done (eg. using a library such as OpenGSN).
*
* This version of the Initializable contract uses KeyStoreLib to store the
* variables at InitializableKeyStored._initialized and
* InitializableKeyStored._initializing.
*/

/**
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
* behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
*
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
* possible by providing the encoded function call as the `_data` argument to {UpgradeableProxy-constructor}.
*
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
*/
abstract contract InitializableKeyStored {
using KeyStoreLib for bytes32;

/**
* @dev Indicates that the contract has been initialized.
*/
bytes32 private constant _initializedSlot = keccak256('InitializableKeyStored._initialized');

/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bytes32 private constant _initializingSlot = keccak256('InitializableKeyStored._initializing');

/**
* @dev Modifier to protect an initializer function from being invoked twice.
*/
modifier initializer() {
bool _initialized = _initializedSlot.getBool();
bool _initializing = _initializingSlot.getBool();

require(_initializing || _isConstructor() || !_initialized, 'Initializable: contract is already initialized');

bool isTopLevelCall = !_initializing;
if (isTopLevelCall) {
_initializing = true;
_initialized = true;
}

_;

if (isTopLevelCall) {
_initializing = false;
}
}

/// @dev Returns true if and only if the function is running in the constructor
function _isConstructor() private view returns (bool) {
// extcodesize checks the size of the code stored in an address, and
// address returns the current address. Since the code is still not
// deployed when running a constructor, any checks on its code size will
// yield zero, making it an effective way to detect if a contract is
// under construction or not.
address self = address(this);
uint256 cs;
// solhint-disable-next-line no-inline-assembly
assembly {
cs := extcodesize(self)
}
return cs == 0;
}
}
105 changes: 105 additions & 0 deletions contracts/Storage/KeyStore.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;

import './KeyStoreLib.sol';

/**
* @dev An example external facing contract using KeyStoreLib
*
* WARNING: Contracts should use KeyStoreLib directly (recommended) or override KeyStore setter methods.
*
*/
contract KeyStore {
using KeyStoreLib for bytes32;

/**
* @dev Get uint256
* @param slot storage slot
* @return v
*
*/
function getUInt256(bytes32 slot) public view returns (uint256 v) {
return slot.getUInt256();
}

/**
* @dev Set uint256
* @param slot storage slot
* @param v value
*
*/
function setUInt256(bytes32 slot, uint256 v) public {
slot.setUInt256(v);
}

/**
* @dev Get address
* @param slot storage slot
* @return v
*
*/
function getAddress(bytes32 slot) public view returns (address v) {
return slot.getAddress();
}

/**
* @dev Set address
* @param slot storage slot
* @param v value
*
*/
function setAddress(bytes32 slot, address v) public {
slot.setAddress(v);
}

/**
* @dev Get uint256[] storage
* @param slot storage slot
* @param k index
* @return v
*
*/
function getUInt256Array(bytes32 slot, uint256 k) public view returns (uint256) {
uint256[] storage m = slot.getUInt256Array();
return m[k];
}

/**
* @dev Push uint256[] storage
* @param slot storage slot
* @param v value
*
*/
function pushUInt256Array(bytes32 slot, uint256 v) public {
uint256[] storage m = slot.getUInt256Array();
m.push(v);
}

/**
* @dev Get mapping(uint256 => uint256) storage
* @param slot storage slot
* @param k key
* @return v
*
*/
function getUInt256Mapping(bytes32 slot, uint256 k) public view returns (uint256) {
mapping(uint256 => uint256) storage m = slot.getUInt256Mapping();
return m[k];
}

/**
* @dev Set mapping(uint256 => uint256) storage
* @param slot storage slot
* @param k key
* @param v value
*
*/
function setUInt256Mapping(
bytes32 slot,
uint256 k,
uint256 v
) public {
mapping(uint256 => uint256) storage m = slot.getUInt256Mapping();
m[k] = v;
}
}
Loading

0 comments on commit 84a886f

Please sign in to comment.