Created
November 1, 2017 15:35
-
-
Save genecyber/6b08bb41505ffaf3b8d4e32f7bf6ff0c to your computer and use it in GitHub Desktop.
Solidity Plugin Pattern using Interface Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.7; | |
import "Interface.Contract.sol"; | |
import "Interface.Record.sol"; | |
contract IPlugin is IContract, IRecord { | |
bool public initialized = false; | |
string public PluginName; | |
string public ContractName; | |
string public Description; | |
function Init() external returns (bool){ | |
if (initialized) { | |
return false; | |
} | |
_Init(); | |
return PostInit(); | |
} | |
function _Init() internal returns (bool){} | |
event Loaded(string, string, bool); | |
function PostInit() internal returns (bool) { | |
initialized = true; | |
Loaded("Plugin loading complete: ", PluginName, initialized); | |
return Ready(); | |
} | |
function DoWork(uint, bytes32[]) returns (bool){} | |
function Ready() constant returns (bool){ | |
return initialized; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.9; | |
import "Interface.Plugin.sol"; | |
contract Sample is IPlugin { | |
function StringValidate() {} | |
function _Init() internal returns (bool) { | |
PluginName = "Sample"; | |
ContractName = "Plugin.Sample.sol"; | |
Description = "Used to demonstrate how a plugin is implemented"; | |
return true; | |
} | |
function DoWork(uint _methodId, bytes32[] _parameters) returns (bool) { | |
// Operate logic here: | |
// _methodId is an index reference to the method of interest | |
// _parameters is an array of input parameters for the method specified | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.9; | |
import "Interface.Plugin.sol"; | |
contract StringValidate is IPlugin { | |
function StringValidate() { | |
} | |
event LoadedPlugin(string, string, bool); | |
function _Init() internal returns (bool) { | |
PluginName = "String Validate"; | |
ContractName = "Plugin.StringValidate.sol"; | |
Description = "Used for creating and validating Bitcoin based signatures"; | |
Loaded("String Validate", PluginName, initialized); | |
return true; | |
} | |
function DoWork(uint _methodId, bytes32[] _parameters) returns (bool) { | |
return true; | |
} | |
function internal_createChallenge(string btc_from, string sep, string amount, string tail) internal constant returns (string challenge) { | |
return strConcat(btc_from, sep, amount,tail, ""); | |
} | |
function internal_ValidateBSM(string payload, address key, uint8 v, bytes32 r, bytes32 s) constant returns (bool) { | |
return key == ecrecover(internal_CreateBSMHash(payload), v, r, s); | |
} | |
function internal_CreateBSMHash(string payload) internal constant returns (bytes32) { | |
string memory prefix = "\x18Bitcoin Signed Message:\n"; | |
return sha256(sha256(prefix, bytes1(bytes(payload).length), payload)); | |
} | |
function strConcat(string _a, string _b, string _c, string _d, string _e) internal returns (string){ | |
bytes memory _ba = bytes(_a); | |
bytes memory _bb = bytes(_b); | |
bytes memory _bc = bytes(_c); | |
bytes memory _bd = bytes(_d); | |
bytes memory _be = bytes(_e); | |
string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length); | |
bytes memory babcde = bytes(abcde); | |
uint k = 0; | |
for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i]; | |
for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i]; | |
for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i]; | |
for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i]; | |
for (i = 0; i < _be.length; i++) babcde[k++] = _be[i]; | |
return string(babcde); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
StringValidate is incomplete but shows how a plugin can be very unique