-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathURLShortner.sol
75 lines (63 loc) · 2.07 KB
/
URLShortner.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract URLShortner {
struct URLStruct {
address owner;
string url;
bool exists;
}
// Generated Hash to URL Struct Mapping
mapping (bytes => URLStruct) lookupTable;
// User to Shortened Hash mapping
mapping (address => bytes[]) public shortenedURLs;
address payable owner;
// URL Shortened event
event URLShortened(string url, bytes _short, address owner);
constructor() public { owner = msg.sender; }
// Set URL To Shorten With User Supplied Hash
function shortenURLWithHash(string memory _url, bytes memory _short) public payable {
if (!lookupTable[_short].exists){
lookupTable[_short] = URLStruct(msg.sender, _url, true);
shortenedURLs[msg.sender].push(_short);
// Emit Event on Blockchain
emit URLShortened(_url, _short, msg.sender);
}
}
// Set URL to shorten with Generated Hash
function shortenURL(string memory url) public payable {
bytes memory shortHash = getUrlShortHash(url);
return shortenURLWithHash(url, shortHash);
}
// Get URL for input hash
function getURL(bytes memory _short) public view returns (string memory) {
URLStruct storage result = lookupTable[_short];
if(result.exists){
return result.url;
}
return "FAIL";
}
// Kill Contract
function kill()
public
onlyOwner
{
if (msg.sender == owner) selfdestruct(owner);
}
// Modifier to check that the caller is the owner of
// the contract.
modifier onlyOwner()
{
require(msg.sender == owner, "Not owner");
// Underscore is a special character only used inside
// a function modifier and it tells Solidity to
// execute the rest of the code.
_;
}
// private method to generate hash
function getUrlShortHash(string memory str) internal pure returns (bytes memory) {
bytes32 hash = sha256(abi.encodePacked(str));
uint main_shift = 15;
bytes32 mask = 0xffffff0000000000000000000000000000000000000000000000000000000000;
return abi.encodePacked(bytes3(hash<<(main_shift*6)&mask));
}
}