Skip to content

Commit 84a886f

Browse files
authored
v0.1.1 (#6)
* 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
1 parent 8db056f commit 84a886f

19 files changed

+684
-67
lines changed

.github/workflows/release-package.yml

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,33 @@ on:
55
types: [created]
66

77
jobs:
8-
build:
8+
publish-npmjs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- uses: actions/checkout@v2
1212
- uses: actions/setup-node@v1
1313
with:
1414
node-version: "12.16"
15+
registry-url: https://registry.npmjs.org/
16+
# Set login for package registries
17+
- run: npm set //registry.npmjs.org/:_authToken=${{secrets.NPMJS_TOKEN}}
18+
- run: npm set //npm.pkg.github.com/:_authToken=${{secrets.GPR_TOKEN}}
1519
- run: npm install
16-
- run: npm test
20+
- run: npm publish --access public
21+
env:
22+
NODE_AUTH_TOKEN: ${{secrets.NPMJS_TOKEN}}
1723

1824
publish-gpr:
19-
needs: build
2025
runs-on: ubuntu-latest
2126
steps:
2227
- uses: actions/checkout@v2
2328
- uses: actions/setup-node@v1
2429
with:
2530
node-version: "12.16"
2631
registry-url: https://npm.pkg.github.com/
32+
# Set login for package registries (only required for private dependencies)
33+
- run: npm set //registry.npmjs.org/:_authToken=${{secrets.NPMJS_TOKEN}}
34+
- run: npm set //npm.pkg.github.com/:_authToken=${{secrets.GPR_TOKEN}}
2735
- run: npm install
2836
- run: npm publish
2937
env:

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
22
# build output
33
build/
4-
migrations/
54
test/
65
!contracts/test/
76
types/truffle-contracts/

contracts/Array/ArrayLibUtils.sol

+41-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ library ArrayLibUtils {
6565

6666
/**
6767
* @dev Concatenate byte arrays
68-
* @param arrayOfArrays of uint256
68+
* @param arrayOfArrays of bytes
6969
* @return concatenated
7070
*
7171
* This function is especially useful when looking to concatenate a
@@ -89,4 +89,44 @@ library ArrayLibUtils {
8989

9090
return newArray;
9191
}
92+
93+
/**
94+
* @dev Concatenate byte arrays
95+
* @param arrayOfArrays of uint256
96+
* @return concatenated
97+
*/
98+
function concat(uint256[][] memory arrayOfArrays) internal pure returns (uint256[] memory) {
99+
uint256 arrayCount = arrayOfArrays.length;
100+
uint256 totalLength = 0;
101+
for (uint256 i = 0; i < arrayCount; i++) {
102+
totalLength += arrayOfArrays[i].length;
103+
}
104+
105+
uint256[] memory newArray = new uint256[](totalLength);
106+
uint256 k = 0;
107+
for (uint256 i = 0; i < arrayCount; i++) {
108+
for (uint256 j = 0; j < arrayOfArrays[i].length; j++) {
109+
newArray[k] = arrayOfArrays[i][j];
110+
k++;
111+
}
112+
}
113+
114+
return newArray;
115+
}
116+
117+
/**
118+
* @dev Return range array
119+
* @param from start index
120+
* @param to end index, exclusive
121+
* @return rangeArray
122+
*
123+
*/
124+
function range(uint256 from, uint256 to) internal pure returns (uint256[] memory rangeArray) {
125+
require(from < to, 'from >= to');
126+
127+
rangeArray = new uint256[](to - from);
128+
for (uint256 i = 0; i < rangeArray.length; i++) {
129+
rangeArray[i] = from + i;
130+
}
131+
}
92132
}

contracts/Migrations.sol

-19
This file was deleted.

contracts/Proxy/CloneFactory.sol

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
pragma solidity ^0.6.0;
2+
3+
//from https://github.com/optionality/clone-factory/blob/master/contracts/CloneFactory.sol
4+
//explainer at https://blog.openzeppelin.com/deep-dive-into-the-minimal-proxy-contract/
5+
//also see https://eips.ethereum.org/EIPS/eip-1167
6+
7+
//WARNING: READ BELOW to understand differences with original contract
8+
/**
9+
* Modified from @optionality.io/clone-factory/contracts/CloneFactory.sol
10+
* - Added support for CREATE2-deployed minimal proxy.
11+
*/
12+
13+
/**
14+
* @dev EIP1167 Minimal Proxy
15+
*/
16+
17+
/*
18+
The MIT License (MIT)
19+
Copyright (c) 2018 Murray Software, LLC.
20+
Permission is hereby granted, free of charge, to any person obtaining
21+
a copy of this software and associated documentation files (the
22+
"Software"), to deal in the Software without restriction, including
23+
without limitation the rights to use, copy, modify, merge, publish,
24+
distribute, sublicense, and/or sell copies of the Software, and to
25+
permit persons to whom the Software is furnished to do so, subject to
26+
the following conditions:
27+
The above copyright notice and this permission notice shall be included
28+
in all copies or substantial portions of the Software.
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
*/
37+
//solhint-disable max-line-length
38+
//solhint-disable no-inline-assembly
39+
40+
contract CloneFactory {
41+
//CHANGE from REFERENCE implementation by Leo VIGNA
42+
//Added create2 support
43+
function create2Clone(address target, bytes32 salt) internal returns (address result) {
44+
bytes20 targetBytes = bytes20(target);
45+
assembly {
46+
let clone := mload(0x40)
47+
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
48+
mstore(add(clone, 0x14), targetBytes)
49+
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
50+
result := create2(0, clone, 0x37, salt)
51+
}
52+
}
53+
54+
function createClone(address target) internal returns (address result) {
55+
bytes20 targetBytes = bytes20(target);
56+
assembly {
57+
let clone := mload(0x40)
58+
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
59+
mstore(add(clone, 0x14), targetBytes)
60+
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
61+
result := create(0, clone, 0x37)
62+
}
63+
}
64+
65+
function isClone(address target, address query) internal view returns (bool result) {
66+
bytes20 targetBytes = bytes20(target);
67+
assembly {
68+
let clone := mload(0x40)
69+
mstore(clone, 0x363d3d373d3d3d363d7300000000000000000000000000000000000000000000)
70+
mstore(add(clone, 0xa), targetBytes)
71+
mstore(add(clone, 0x1e), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
72+
73+
let other := add(clone, 0x40)
74+
extcodecopy(query, other, 0, 0x2d)
75+
result := and(eq(mload(clone), mload(other)), eq(mload(add(clone, 0xd)), mload(add(other, 0xd))))
76+
}
77+
}
78+
}
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
// solhint-disable-next-line compiler-version
4+
pragma solidity >=0.4.24 <0.8.0;
5+
6+
import '../Storage/KeyStoreLib.sol';
7+
8+
//WARNING: READ BELOW to understand differences with original contract
9+
/** Modified from @openzeppelin/contracts/proxy/Initializable.sol
10+
* Contracts that inherit from a parent class often can't also
11+
* be made Initializable because of conflicts with parent contract's
12+
* storage layer. The solution until now was to make the parent contract
13+
* Initializable but this cannot always be done (eg. using a library such as OpenGSN).
14+
*
15+
* This version of the Initializable contract uses KeyStoreLib to store the
16+
* variables at InitializableKeyStored._initialized and
17+
* InitializableKeyStored._initializing.
18+
*/
19+
20+
/**
21+
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
22+
* behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
23+
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
24+
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
25+
*
26+
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
27+
* possible by providing the encoded function call as the `_data` argument to {UpgradeableProxy-constructor}.
28+
*
29+
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
30+
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
31+
*/
32+
abstract contract InitializableKeyStored {
33+
using KeyStoreLib for bytes32;
34+
35+
/**
36+
* @dev Indicates that the contract has been initialized.
37+
*/
38+
bytes32 private constant _initializedSlot = keccak256('InitializableKeyStored._initialized');
39+
40+
/**
41+
* @dev Indicates that the contract is in the process of being initialized.
42+
*/
43+
bytes32 private constant _initializingSlot = keccak256('InitializableKeyStored._initializing');
44+
45+
/**
46+
* @dev Modifier to protect an initializer function from being invoked twice.
47+
*/
48+
modifier initializer() {
49+
bool _initialized = _initializedSlot.getBool();
50+
bool _initializing = _initializingSlot.getBool();
51+
52+
require(_initializing || _isConstructor() || !_initialized, 'Initializable: contract is already initialized');
53+
54+
bool isTopLevelCall = !_initializing;
55+
if (isTopLevelCall) {
56+
_initializing = true;
57+
_initialized = true;
58+
}
59+
60+
_;
61+
62+
if (isTopLevelCall) {
63+
_initializing = false;
64+
}
65+
}
66+
67+
/// @dev Returns true if and only if the function is running in the constructor
68+
function _isConstructor() private view returns (bool) {
69+
// extcodesize checks the size of the code stored in an address, and
70+
// address returns the current address. Since the code is still not
71+
// deployed when running a constructor, any checks on its code size will
72+
// yield zero, making it an effective way to detect if a contract is
73+
// under construction or not.
74+
address self = address(this);
75+
uint256 cs;
76+
// solhint-disable-next-line no-inline-assembly
77+
assembly {
78+
cs := extcodesize(self)
79+
}
80+
return cs == 0;
81+
}
82+
}

contracts/Storage/KeyStore.sol

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//SPDX-License-Identifier: MIT
2+
pragma solidity >=0.6.0;
3+
4+
import './KeyStoreLib.sol';
5+
6+
/**
7+
* @dev An example external facing contract using KeyStoreLib
8+
*
9+
* WARNING: Contracts should use KeyStoreLib directly (recommended) or override KeyStore setter methods.
10+
*
11+
*/
12+
contract KeyStore {
13+
using KeyStoreLib for bytes32;
14+
15+
/**
16+
* @dev Get uint256
17+
* @param slot storage slot
18+
* @return v
19+
*
20+
*/
21+
function getUInt256(bytes32 slot) public view returns (uint256 v) {
22+
return slot.getUInt256();
23+
}
24+
25+
/**
26+
* @dev Set uint256
27+
* @param slot storage slot
28+
* @param v value
29+
*
30+
*/
31+
function setUInt256(bytes32 slot, uint256 v) public {
32+
slot.setUInt256(v);
33+
}
34+
35+
/**
36+
* @dev Get address
37+
* @param slot storage slot
38+
* @return v
39+
*
40+
*/
41+
function getAddress(bytes32 slot) public view returns (address v) {
42+
return slot.getAddress();
43+
}
44+
45+
/**
46+
* @dev Set address
47+
* @param slot storage slot
48+
* @param v value
49+
*
50+
*/
51+
function setAddress(bytes32 slot, address v) public {
52+
slot.setAddress(v);
53+
}
54+
55+
/**
56+
* @dev Get uint256[] storage
57+
* @param slot storage slot
58+
* @param k index
59+
* @return v
60+
*
61+
*/
62+
function getUInt256Array(bytes32 slot, uint256 k) public view returns (uint256) {
63+
uint256[] storage m = slot.getUInt256Array();
64+
return m[k];
65+
}
66+
67+
/**
68+
* @dev Push uint256[] storage
69+
* @param slot storage slot
70+
* @param v value
71+
*
72+
*/
73+
function pushUInt256Array(bytes32 slot, uint256 v) public {
74+
uint256[] storage m = slot.getUInt256Array();
75+
m.push(v);
76+
}
77+
78+
/**
79+
* @dev Get mapping(uint256 => uint256) storage
80+
* @param slot storage slot
81+
* @param k key
82+
* @return v
83+
*
84+
*/
85+
function getUInt256Mapping(bytes32 slot, uint256 k) public view returns (uint256) {
86+
mapping(uint256 => uint256) storage m = slot.getUInt256Mapping();
87+
return m[k];
88+
}
89+
90+
/**
91+
* @dev Set mapping(uint256 => uint256) storage
92+
* @param slot storage slot
93+
* @param k key
94+
* @param v value
95+
*
96+
*/
97+
function setUInt256Mapping(
98+
bytes32 slot,
99+
uint256 k,
100+
uint256 v
101+
) public {
102+
mapping(uint256 => uint256) storage m = slot.getUInt256Mapping();
103+
m[k] = v;
104+
}
105+
}

0 commit comments

Comments
 (0)