Skip to content

Instantly share code, notes, and snippets.

@CowboyJim
Created January 31, 2017 15:13
Show Gist options
  • Save CowboyJim/75dfe85a7d67bcb1179687f045ea9016 to your computer and use it in GitHub Desktop.
Save CowboyJim/75dfe85a7d67bcb1179687f045ea9016 to your computer and use it in GitHub Desktop.
Payment Directory Distributed Contract
pragma solidity ^0.4.2;
/// @title Simple Alias Directory
/// @author Jim Boone
/*
Demonstration payment alias directory distributed application
solidity contract
jim.boone@levvel.io
Copyright: Levvel, LLC
http://levvel.io
*/
contract AliasDirectoryContract {
// Data structure that represents a bank or financial institution
struct Bank {
string name;
string routingNumber;
address nodeAddress;
}
// Data structure that represenets an alias
struct Alias {
uint ownerId;
string alias;
bool email;
string accountNumber;
address owningFI;
}
// Data structure that represents the owner of an alias
struct Owner {
uint id;
string name;
mapping (string => Alias) aliases;
}
/*
* Node address that is responsible for managing the banks
* that are permitted to add aliases to the directory
*/
address public adminstrator;
// State variable map that stores authorized network Banks
mapping(address => Bank) public banks;
// State variable map that stores alias owners
mapping(uint => Owner) public gOwners;
// State variable map that stores alias owners
// Note: the alias string value is convered to a hash prior to storing
mapping(bytes32 => uint) public gHashedOwnerIds;
// State variable map that stores customer's aliases
mapping (string => Alias) gAliases;
// State variable the is used to generate owner ID numbers
uint ownerNumber = 100;
// Event that is broadcast whenever an alias is created
event AliasCreated (string alias, bool email, string accountNumber,
string name, address owningFI);
// Event that is broadcast whenever an alias is deleted
event AliasDeleted (string alias, bool email, string accountNumber,
string name, address owningFI);
// Event that is broadcast whenever an owner is created
event OwnerCreated(string name, uint id);
// Event that is broadcast whenever a bank is authorized
event AuthorizedBankAdded(string bankName, string routingNumber, address nodeAddress);
// Event that is broadcast whenever an authorized bank is deleted
event AuthorizedBankDeleted(string bankName, string routingNumber, address nodeAddress);
// Function modifier that can be used to guard against unauthorized
// users from participating in the directory
modifier authorized {
if (msg.sender == address(0x0))
throw;
_;
}
// Contract constructor function. The node creating the contract
// will be the adminstrator
function AliasDirectoryContract(){
adminstrator = msg.sender;
banks[adminstrator] = Bank("Adminstrator Bank","not_set",adminstrator );
}
// Add new authorized bank nodes
function addAuthorizedBanks(string bankName, string routingNumber, address nodeAddress) authorized {
banks[nodeAddress] = Bank(bankName, routingNumber, nodeAddress);
AuthorizedBankAdded(bankName, routingNumber, nodeAddress);
}
// Remove authorized bank nodes
function deleteAuthorizedBanks(address nodeAddress) authorized {
delete banks[nodeAddress];
Bank bank = banks[nodeAddress];
AuthorizedBankDeleted(bank.name, bank.routingNumber, bank.nodeAddress);
}
// Create new alias for the distributed directory
function newOwnerAndAlias(string name, string alias, bool email, string accountNumber ) authorized {
uint ownerId = newOwner(name);
newAlias( ownerId, alias, email, accountNumber);
}
function getOwnerId(string name) constant returns (uint) {
return gHashedOwnerIds[sha3(name)];
}
// Creates a new Owner
function newOwner(string name) authorized returns (uint ownerId){
ownerId = ownerNumber++;
gHashedOwnerIds[sha3(name)] = ownerId;
gOwners[ownerId] = Owner(ownerId, name);
OwnerCreated(name, ownerId);
}
// Create new alias for the distributed directory
function newAlias(uint ownerId, string alias, bool email, string accountNumber ) authorized {
// Ensure that the alias doesn't currently exist in the directory
if (gAliases[alias].owningFI != address(0x0)){
throw;
}
// Grab the owner
Owner aliasOwner = gOwners[ownerId];
// Create the new alias data structure
gAliases[alias] = Alias(ownerId, alias, email, accountNumber, msg.sender);
// Assign the alias to the owner alias map
aliasOwner.aliases[alias] = gAliases[alias];
// Broadcast the alias creation event
AliasCreated(alias, email, accountNumber, aliasOwner.name, msg.sender);
}
// Delete the alias from the directory
function deleteAlias(string value) authorized returns(bool) {
Alias alias = gAliases[value];
if(alias.owningFI != msg.sender){
throw;
}
// Grab the owner
Owner aliasOwner = gOwners[alias.ownerId];
// Broadcast the alias deletion event
AliasDeleted(value, alias.email, alias.accountNumber, aliasOwner.name, alias.owningFI);
delete gAliases[value];
return true;
}
// Returns details for the alias
function getAlias(string alias) constant returns (bool email,
string accountNumber, string name, uint ownerId, address owningFI) {
Alias aliasData = gAliases[alias];
email = aliasData.email;
accountNumber = aliasData.accountNumber;
name = gOwners[aliasData.ownerId].name;
owningFI = owningFI;
ownerId = aliasData.ownerId;
}
// Remove the contract from the blockchain and send it's ether to the contract owner
// This will destroy the directory
function destroyDirectory(){
if(msg.sender == adminstrator){
selfdestruct(adminstrator);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment