Description:
Multi-signature wallet contract requiring multiple confirmations for transaction execution.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
// SPDX-License-Identifier: MIT
// File: @openzeppelin/contracts/utils/Context.sol
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// File: @openzeppelin/contracts/access/Ownable.sol
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// File: contract/NFTStorage.sol
pragma solidity ^0.8.19;
contract NFTStorage is Ownable{
string public name;
string public symbol;
struct Image {
address owner;
string imageName;
string description;
uint256 totalChunks;
uint256 uploadedChunks;
uint256 chunkSize;
uint256 timestamp;
bool exists;
bool completed;
}
struct Chunk {
string data;
bool exists;
}
mapping(address => bool) public whitelistedUsers;
mapping(string => Image) public images;
mapping(string => mapping(uint256 => Chunk)) public imageChunks;
mapping(address => string[]) public userImages;
event ImageCreated(string indexed imageId, address indexed owner, uint256 totalChunks);
event ChunkUploaded(string indexed imageId, uint256 chunkIndex, address indexed uploader);
event ImageCompleted(string indexed imageId, address indexed owner);
event UserWhitelisted(address indexed user, address indexed by);
event UserRemovedFromWhitelist(address indexed user, address indexed by);
modifier onlyWhitelisted() {
require(msg.sender == owner() || whitelistedUsers[msg.sender], "Not whitelisted");
_;
}
constructor(uint256 id) Ownable(msg.sender)payable {
whitelistedUsers[msg.sender] = true;
// Create name/symbol like Name: NFTStorage_0000000000001 symbol: NFS0000000000001
// Pad the number to 13 digits: 0000000000001, etc.
string memory paddedId = _padId(id, 13);
name = string(abi.encodePacked("NFTStorage_", paddedId));
symbol = string(abi.encodePacked("NFS", paddedId));
}
// Pads uint256 ID with leading zeros to reach a fixed length
function _padId(uint256 id, uint256 length) internal pure returns (string memory) {
string memory idStr = _uintToString(id);
uint256 idLen = bytes(idStr).length;
if (idLen >= length) {
return idStr;
}
bytes memory padded = new bytes(length);
uint256 padLen = length - idLen;
// Fill with leading zeros
for (uint256 i = 0; i < padLen; i++) {
padded[i] = bytes1("0");
}
// Copy the original digits
for (uint256 i = 0; i < idLen; i++) {
padded[padLen + i] = bytes(idStr)[i];
}
return string(padded);
}
// Converts uint256 to string
function _uintToString(uint256 value) internal pure returns (string memory) {
if (value == 0) return "0";
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
function createImage(
string memory _imageId,
string memory _imageName,
string memory _description, // Added description parameter
uint256 _totalChunks,
uint256 _chunkSize
) external onlyWhitelisted {
require(!images[_imageId].exists, "Image ID already exists");
// Check if user has any incomplete images
for (uint i = 0; i < userImages[msg.sender].length; i++) {
string memory existingImageId = userImages[msg.sender][i];
if (!images[existingImageId].completed) {
revert("Complete or cancel current image before creating new one");
}
}
images[_imageId] = Image({
owner: msg.sender,
imageName: _imageName,
description: _description, // Set description field
totalChunks: _totalChunks,
uploadedChunks: 0,
chunkSize: _chunkSize,
timestamp: block.timestamp,
exists: true,
completed: false
});
userImages[msg.sender].push(_imageId);
emit ImageCreated(_imageId, msg.sender, _totalChunks);
}
function uploadChunk(
string memory _imageId,
uint256 _chunkIndex,
string memory _chunkData
) external onlyWhitelisted {
require(images[_imageId].exists, "Image does not exist");
require(images[_imageId].owner == msg.sender, "Not image owner");
require(!images[_imageId].completed, "Image already completed");
require(!imageChunks[_imageId][_chunkIndex].exists, "Chunk already uploaded");
require(_chunkIndex < images[_imageId].totalChunks, "Invalid chunk index");
imageChunks[_imageId][_chunkIndex] = Chunk({
data: _chunkData,
exists: true
});
images[_imageId].uploadedChunks++;
// Check if image is now complete
if (images[_imageId].uploadedChunks == images[_imageId].totalChunks) {
images[_imageId].completed = true;
emit ImageCompleted(_imageId, msg.sender);
}
emit ChunkUploaded(_imageId, _chunkIndex, msg.sender);
}
function getChunk(string memory _imageId, uint256 _chunkIndex) external view returns (string memory) {
require(imageChunks[_imageId][_chunkIndex].exists, "Chunk does not exist");
return imageChunks[_imageId][_chunkIndex].data;
}
function getImageInfo(string memory _imageId) external view returns (Image memory) {
require(images[_imageId].exists, "Image does not exist");
return images[_imageId];
}
function getRemainingChunks(string memory _imageId) external view returns (uint256) {
require(images[_imageId].exists, "Image does not exist");
return images[_imageId].totalChunks - images[_imageId].uploadedChunks;
}
function isImageComplete(string memory _imageId) external view returns (bool) {
require(images[_imageId].exists, "Image does not exist");
return images[_imageId].completed;
}
function getUserImages(address _user) external view returns (string[] memory) {
return userImages[_user];
}
function getUserIncompleteImage(address _user) external view returns (string memory) {
for (uint i = 0; i < userImages[_user].length; i++) {
string memory imageId = userImages[_user][i];
if (!images[imageId].completed) {
return imageId;
}
}
return "";
}
function cancelImage(string memory _imageId) external onlyWhitelisted {
require(images[_imageId].exists, "Image does not exist");
require(images[_imageId].owner == msg.sender, "Not image owner");
require(!images[_imageId].completed, "Image already completed");
// Mark as completed to free up the slot
images[_imageId].completed = true;
}
function addToWhitelist(address _user) external onlyOwner {
whitelistedUsers[_user] = true;
emit UserWhitelisted(_user, msg.sender);
}
function removeFromWhitelist(address _user) external onlyOwner {
whitelistedUsers[_user] = false;
emit UserRemovedFromWhitelist(_user, msg.sender);
}
function isWhitelisted(address _user) external view returns (bool) {
return whitelistedUsers[_user];
}
}
Submitted on: 2025-10-16 10:51:07
Comments
Log in to comment.
No comments yet.