-
Notifications
You must be signed in to change notification settings - Fork 31
/
IPFSStorage.sol
76 lines (68 loc) · 1.89 KB
/
IPFSStorage.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
76
pragma solidity ^0.4.18;
/**
* @title IPFSStorage
* @author Forest Fang (@saurfang)
* @dev Stores IPFS (multihash) hash by address. A multihash entry is in the format
* of <varint hash function code><varint digest size in bytes><hash function output>
* See https://github.com/multiformats/multihash
*
* Currently IPFS hash is 34 bytes long with first two segments represented as a single byte (uint8)
* The digest is 32 bytes long and can be stored using bytes32 efficiently.
*/
contract IPFSStorage {
struct Multihash {
bytes32 digest;
uint8 hashFunction;
uint8 size;
}
mapping (address => Multihash) private entries;
event EntrySet (
address indexed key,
bytes32 digest,
uint8 hashFunction,
uint8 size
);
event EntryDeleted (
address indexed key
);
/**
* @dev associate a multihash entry with the sender address
* @param _digest hash digest produced by hashing content using hash function
* @param _hashFunction hashFunction code for the hash function used
* @param _size length of the digest
*/
function setEntry(bytes32 _digest, uint8 _hashFunction, uint8 _size)
public
{
Multihash memory entry = Multihash(_digest, _hashFunction, _size);
entries[msg.sender] = entry;
EntrySet(
msg.sender,
_digest,
_hashFunction,
_size
);
}
/**
* @dev deassociate any multihash entry with the sender address
*/
function clearEntry()
public
{
require(entries[msg.sender].digest != 0);
delete entries[msg.sender];
EntryDeleted(msg.sender);
}
/**
* @dev retrieve multihash entry associated with an address
* @param _address address used as key
*/
function getEntry(address _address)
public
view
returns(bytes32 digest, uint8 hashfunction, uint8 size)
{
Multihash storage entry = entries[_address];
return (entry.digest, entry.hashFunction, entry.size);
}
}