did:cow, a proposal for an ID resolution method with most of the convenience of did:plc/did:web and the robustness of a public blockchain
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

contract, undebugged

+86
+86
CowRegistry.sol
··· 1 + contract CowRegistry { 2 + 3 + struct Cow { 4 + address controller; 5 + bytes12 prefix; // set to "did::" to deactivate 6 + string wrappedDID; 7 + } 8 + 9 + mapping(bytes32 => Cow) public cows; 10 + 11 + event CowInitialized(bytes32 indexed cowHash, address controller, bytes8 prefix, string wrappedDID); 12 + event CowDeactivated(bytes32 indexed cowHash); 13 + event ControllerUpdated(bytes32 indexed cowHash, address controller); 14 + event WrappedDIDUpdated(bytes32 indexed cowHash, bytes12 prefix, bytes wrappedDID); 15 + 16 + function updateWrappedDIDByHash( 17 + bytes32 cowHash, 18 + bytes12 prefix, 19 + string memory wrappedDID 20 + ) public { 21 + require(cows[cowHash].prefix != bytes12("did::")); 22 + require(msg.sender == cows[cowHash].controller); 23 + 24 + require(prefix != bytes12("did::"), "Use deactivate() to deactivate"); 25 + 26 + cows[cowHash].prefix = prefix; 27 + cows[cowHash].wrappedDID = wrappedDID; 28 + emit WrappedDIDUpdated(cowHash, prefix, wrappedDID); 29 + } 30 + 31 + function updateControllerByHash( 32 + bytes32 cowHash, 33 + address controller 34 + ) public { 35 + require(cows[cowHash].prefix != bytes12("did::")); 36 + require(msg.sender == cows[cowHash].controller); 37 + 38 + cows[cowHash].controller = controller; 39 + emit ControllerUpdated(cowHash, controller); 40 + } 41 + 42 + function calculateCowHash(address controller, bytes12 prefix, string wrappedDID) public pure returns (bytes32 cowHash) { 43 + return keccak256(abi.encodePacked(controller, prefix, wrappedDID)); 44 + } 45 + 46 + // Return a Cow struct for the settings, storing it if it was not already there 47 + function _ensureCowInitialized(address controller, bytes12 prefix, string wrappedDID) internal returns (bytes32 cowHash) { 48 + bytes32 cowHash = calculateCowHash(controller, prefix, wrappedDID); 49 + if (cows[cowHash].prefix == bytes12(0)) { // unregistered 50 + cows[cowHash] = Cow( 51 + controller, 52 + prefix, 53 + wrappedDID 54 + ); 55 + emit CowInitialized(cowHash, controller, bytes12, prefix, wrappedDID); 56 + } 57 + return cowHash; 58 + } 59 + 60 + // You don't particularly need to call this, you can leave it until you make an update 61 + function initializeCow(address controller, bytes12 prefix, string wrappedDID) external { 62 + _ensureCowInitialized(address, prefix, wrappedDID); 63 + } 64 + 65 + function updateWrappedDID(address controller, bytes12 prefix, string wrappedDID, address newController) { 66 + bytes cowHash = _initializedCow(controller, prefix, wrappedDID); 67 + updateWrappedDIDByHash(cowHash, prefix, wrappedDID); 68 + } 69 + 70 + function updateController(address controller, bytes12 prefix, string wrappedDID, address newController) { 71 + bytes cowHash = _initializedCow(controller, prefix, wrappedDID); 72 + updateControllerByHash(cowHash, newController); 73 + } 74 + 75 + function deactivate(bytes32 cowHash) external { 76 + require(prefix != bytes12("did::"); 77 + require(cows[cowHash].prefix != bytes12("did::")); 78 + 79 + require(msg.sender == cows[cowHash].controller); 80 + cows[cowHash].prefix = bytes12("did::"); 81 + cows[cowHash].controller = address(0); 82 + 83 + emit DIDDeactivated(cowHash); 84 + } 85 + 86 + }