add owner permission control

main
Harry 2 years ago
parent e4f885a3ac
commit f829e2baf6

@ -20,6 +20,7 @@ contract Git3 {
string name;
}
mapping(bytes => address) public repoNameToOwner;
mapping(string => refInfo) public nameToRefInfo; // dev => {hash: 0x1234..., index: 1 }
string[] public refs; // [main, dev, test, staging]
@ -34,28 +35,38 @@ contract Git3 {
storageManager = IFileOperator(address(new FlatDirectory(220)));
}
modifier onlyOwner(bytes memory repoName, address memory user) {
require(repoNameToOwner[repoName] == user || repoNameToOwner[repoName] == address(0));
_;
}
function download(
bytes memory repoName,
bytes memory path
) external view returns (bytes memory, bool) {
// call flat directory(FD)
return storageManager.read(path);
return storageManager.read(bytes.concat(repoName, '/', path));
}
function upload(bytes memory path, bytes memory data) external payable {
storageManager.writeChunk{value: msg.value}(path, 0, data);
function upload(bytes memory repoName, bytes memory path, bytes memory data)
external payable onlyOwner(repoName, msg.sender)
{
storageManager.writeChunk{value: msg.value}(bytes.concat(repoName, '/', path), 0, data);
}
function uploadChunk(
bytes memory repoName,
bytes memory path,
uint256 chunkId,
bytes memory data
) external payable {
storageManager.writeChunk{value: msg.value}(path, chunkId, data);
) external payable onlyOwner(repoName, msg.sender) {
storageManager.writeChunk{value: msg.value}(bytes.concat(repoName, '/', path), chunkId, data);
}
function remove(bytes memory path) external {
function remove(bytes memory repoName, bytes memory path) external onlyOwner(repoName, msg.sender) {
// The actually process of remove will remove all the chunks
storageManager.remove(path);
storageManager.remove(bytes.concat(repoName, '/', path));
}
function size(bytes memory name) external view returns (uint256, uint256) {
@ -73,7 +84,7 @@ contract Git3 {
}
}
function setRef(string memory name, bytes20 refHash) public {
function setRef(bytes memory repoName, bytes memory name, bytes20 refHash) public onlyOwner(repoName, msg.sender) {
// only execute `sload` once to reduce gas consumption
refInfo memory srs;
srs = nameToRefInfo[name];
@ -86,6 +97,7 @@ contract Git3 {
"Refs exceed valid length"
);
repoToOwner
nameToRefInfo[name].hash = refHash;
nameToRefInfo[name].index = uint96(refsLen);
@ -96,7 +108,7 @@ contract Git3 {
}
}
function delRef(string memory name) public {
function delRef(bytes memory repoName, bytes memory name) public onlyOwner(repoName, msg.sender) {
// only execute `sload` once to reduce gas consumption
refInfo memory srs;
srs = nameToRefInfo[name];

13852
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -18,11 +18,19 @@
"homepage": "https://github.com/cyl19970726/dGithub#readme",
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"@typechain/hardhat": "^6.1.5",
"dotenv": "^16.0.3",
"hardhat": "^2.12.4"
"hardhat": "^2.12.4",
"hardhat-gas-reporter": "^1.0.9",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"dependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^1.0.5",
"@nomiclabs/hardhat-ethers": "^2.2.1",
"@nomiclabs/hardhat-etherscan": "^3.1.3",
"@openzeppelin/contracts": "^4.8.0",
"chai": "^4.3.7",
"evm-large-storage": "^1.0.0"
}
}

Loading…
Cancel
Save