This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
BlockSig.sol
105 lines (92 loc) · 4.04 KB
/
BlockSig.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* BlockSig Smart Contract.
* Copyright BlockVigil, Inc..
* Code released under the MIT license.
*/
pragma solidity ^0.5.17;
import "./Ownable.sol";
contract BlockSig is Ownable {
enum currentState {pending, signed, notarized, canceled}
struct Doc {
uint256 createdTime;
uint256 remainingSignatures;
uint256 completedTime;
bytes32 completedHash;
currentState status;
mapping (address => uint256) signers;
}
mapping (bytes32 => Doc) documents;
mapping (bytes32 => bytes32) finalDocs;
event Created(bytes32 hash);
event Signature(bytes32 hash, bytes32 signatureHash, address signer);
event Signed(bytes32 hash);
event Notarized(bytes32 hash, bytes32 signatureHash, address signer);
event Completed(bytes32 hash, bytes32 finalHash);
function createDoc(bytes32 hash, address[] memory signers, bool notarize) onlyOwner public {
require(!checkDoc(hash));
if (signers.length == 0 && !notarize){
documents[hash] = Doc(now, 0, now, hash, currentState.signed);
finalDocs[hash] = hash;
} else {
documents[hash] = Doc(now, signers.length, 0, "", currentState.pending);
for (uint8 i=0; i<signers.length; i++){
documents[hash].signers[signers[i]] = 1;
}
}
emit Created(hash);
}
function finalizeDoc(bytes32 originalHash, bytes32 finalHash) onlyOwner public {
require(documents[originalHash].status == currentState.notarized || documents[originalHash].status == currentState.signed || documents[originalHash].status == currentState.pending);
require(documents[originalHash].completedHash[0] == 0);
documents[originalHash].completedHash = finalHash;
finalDocs[finalHash] = originalHash;
emit Completed(originalHash, finalHash);
documents[originalHash].completedTime = now;
}
function sign(bytes32 hash, bytes32 signatureHash, address signer, bool notarized) onlyOwner public {
require(checkDoc(hash));
if (notarized){
documents[hash].status = currentState.notarized;
documents[hash].signers[signer] = now;
emit Signature(hash, signatureHash, signer);
emit Notarized(hash, signatureHash, signer);
} else {
require(documents[hash].signers[signer] == 1);
documents[hash].remainingSignatures--;
documents[hash].signers[signer] = now;
emit Signature(hash, signatureHash, signer);
if (documents[hash].remainingSignatures == 0){
documents[hash].status = currentState.signed;
emit Signed(hash);
}
}
}
function getFinalDoc(bytes32 hash) view public returns (bytes32 originalHash) {
require(checkDoc(finalDocs[hash]));
originalHash = finalDocs[hash];
}
function getDocument(bytes32 hash) view public returns (uint256 createdTime, uint256 remainingSignatures, uint256 completedTime, bytes32 completedHash, string memory status){
require(checkDoc(hash));
createdTime = documents[hash].createdTime;
if (documents[hash].status == currentState.pending){
status = 'pending';
} else if (documents[hash].status == currentState.signed){
status = 'signed';
} else if (documents[hash].status == currentState.notarized){
status = 'notarized';
} else if (documents[hash].status == currentState.canceled){
status = 'canceled';
}
remainingSignatures = documents[hash].remainingSignatures;
completedTime = documents[hash].completedTime;
completedHash = documents[hash].completedHash;
}
function getSignerTime(bytes32 hash, address signer) view public returns (uint256) {
require(checkDoc(hash));
require(documents[hash].signers[signer] > 1);
return documents[hash].signers[signer];
}
function checkDoc(bytes32 hash) view internal returns (bool){
return documents[hash].createdTime > 0;
}
}