Skip to content

Instantly share code, notes, and snippets.

@jpn0424
Last active May 19, 2019 08:05
Show Gist options
  • Save jpn0424/149a967f85de816245d84f343f924195 to your computer and use it in GitHub Desktop.
Save jpn0424/149a967f85de816245d84f343f924195 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.0+commit.1d4f565a.js&optimize=false&gist=
pragma solidity ^0.5.0;
/**
* @title BytesToTypes
* @dev The BytesToTypes contract converts the memory byte arrays to the standard solidity types
* @author [email protected]
*/
contract BytesToTypes {
function bytesToAddress(uint _offst, bytes memory _input) internal pure returns (address _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToBool(uint _offst, bytes memory _input) internal pure returns (bool _output) {
uint8 x;
assembly {
x := mload(add(_input, _offst))
}
x==0 ? _output = false : _output = true;
}
function getStringSize(uint _offst, bytes memory _input) internal pure returns(uint size){
assembly{
size := mload(add(_input,_offst))
let chunk_count := add(div(size,32),1) // chunk_count = size/32 + 1
if gt(mod(size,32),0) {// if size%32 > 0
chunk_count := add(chunk_count,1)
}
size := mul(chunk_count,32)// first 32 bytes reseves for size in strings
}
}
function bytesToString(uint _offst, bytes memory _input, bytes memory _output) internal pure {
uint size = 32;
assembly {
let chunk_count
size := mload(add(_input,_offst))
chunk_count := add(div(size,32),1) // chunk_count = size/32 + 1
if gt(mod(size,32),0) {
chunk_count := add(chunk_count,1) // chunk_count++
}
for { let index:= 0 } lt(index , chunk_count){ index := add(index,1) } {
mstore(add(_output,mul(index,32)),mload(add(_input,_offst)))
_offst := sub(_offst,32) // _offst -= 32
}
}
}
function bytesToBytes32(uint _offst, bytes memory _input, bytes32 _output) internal pure {
assembly {
mstore(_output , add(_input, _offst))
mstore(add(_output,32) , add(add(_input, _offst),32))
}
}
function bytesToInt8(uint _offst, bytes memory _input) internal pure returns (int8 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt16(uint _offst, bytes memory _input) internal pure returns (int16 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt24(uint _offst, bytes memory _input) internal pure returns (int24 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt32(uint _offst, bytes memory _input) internal pure returns (int32 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt40(uint _offst, bytes memory _input) internal pure returns (int40 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt48(uint _offst, bytes memory _input) internal pure returns (int48 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt56(uint _offst, bytes memory _input) internal pure returns (int56 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt64(uint _offst, bytes memory _input) internal pure returns (int64 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt72(uint _offst, bytes memory _input) internal pure returns (int72 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt80(uint _offst, bytes memory _input) internal pure returns (int80 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt88(uint _offst, bytes memory _input) internal pure returns (int88 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt96(uint _offst, bytes memory _input) internal pure returns (int96 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt104(uint _offst, bytes memory _input) internal pure returns (int104 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt112(uint _offst, bytes memory _input) internal pure returns (int112 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt120(uint _offst, bytes memory _input) internal pure returns (int120 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt128(uint _offst, bytes memory _input) internal pure returns (int128 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt136(uint _offst, bytes memory _input) internal pure returns (int136 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt144(uint _offst, bytes memory _input) internal pure returns (int144 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt152(uint _offst, bytes memory _input) internal pure returns (int152 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt160(uint _offst, bytes memory _input) internal pure returns (int160 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt168(uint _offst, bytes memory _input) internal pure returns (int168 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt176(uint _offst, bytes memory _input) internal pure returns (int176 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt184(uint _offst, bytes memory _input) internal pure returns (int184 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt192(uint _offst, bytes memory _input) internal pure returns (int192 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt200(uint _offst, bytes memory _input) internal pure returns (int200 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt208(uint _offst, bytes memory _input) internal pure returns (int208 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt216(uint _offst, bytes memory _input) internal pure returns (int216 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt224(uint _offst, bytes memory _input) internal pure returns (int224 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt232(uint _offst, bytes memory _input) internal pure returns (int232 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt240(uint _offst, bytes memory _input) internal pure returns (int240 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt248(uint _offst, bytes memory _input) internal pure returns (int248 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToInt256(uint _offst, bytes memory _input) internal pure returns (int256 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint8(uint _offst, bytes memory _input) internal pure returns (uint8 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint16(uint _offst, bytes memory _input) internal pure returns (uint16 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint24(uint _offst, bytes memory _input) internal pure returns (uint24 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint32(uint _offst, bytes memory _input) internal pure returns (uint32 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint40(uint _offst, bytes memory _input) internal pure returns (uint40 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint48(uint _offst, bytes memory _input) internal pure returns (uint48 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint56(uint _offst, bytes memory _input) internal pure returns (uint56 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint64(uint _offst, bytes memory _input) internal pure returns (uint64 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint72(uint _offst, bytes memory _input) internal pure returns (uint72 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint80(uint _offst, bytes memory _input) internal pure returns (uint80 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint88(uint _offst, bytes memory _input) internal pure returns (uint88 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint96(uint _offst, bytes memory _input) internal pure returns (uint96 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint104(uint _offst, bytes memory _input) internal pure returns (uint104 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint112(uint _offst, bytes memory _input) internal pure returns (uint112 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint120(uint _offst, bytes memory _input) internal pure returns (uint120 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint128(uint _offst, bytes memory _input) internal pure returns (uint128 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint136(uint _offst, bytes memory _input) internal pure returns (uint136 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint144(uint _offst, bytes memory _input) internal pure returns (uint144 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint152(uint _offst, bytes memory _input) internal pure returns (uint152 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint160(uint _offst, bytes memory _input) internal pure returns (uint160 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint168(uint _offst, bytes memory _input) internal pure returns (uint168 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint176(uint _offst, bytes memory _input) internal pure returns (uint176 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint184(uint _offst, bytes memory _input) internal pure returns (uint184 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint192(uint _offst, bytes memory _input) internal pure returns (uint192 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint200(uint _offst, bytes memory _input) internal pure returns (uint200 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint208(uint _offst, bytes memory _input) internal pure returns (uint208 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint216(uint _offst, bytes memory _input) internal pure returns (uint216 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint224(uint _offst, bytes memory _input) internal pure returns (uint224 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint232(uint _offst, bytes memory _input) internal pure returns (uint232 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint240(uint _offst, bytes memory _input) internal pure returns (uint240 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint248(uint _offst, bytes memory _input) internal pure returns (uint248 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
function bytesToUint256(uint _offst, bytes memory _input) internal pure returns (uint256 _output) {
assembly {
_output := mload(add(_input, _offst))
}
}
}
pragma solidity ^0.5.0;
// 提供權限管理
// 腳色:管理者、合約建立者
contract ManagePermission {
address private admin;
address private serverService;
constructor() public {
admin = msg.sender;
}
function isAdmin() internal view returns(bool) {
return msg.sender == admin;
}
modifier onlyAdmin() {
require(isAdmin());
_;
}
function isServer() internal view returns(bool) {
return msg.sender == serverService;
}
modifier onlyService() {
require(isServer());
_;
}
// Interface
// 設定後端服務帳戶
function setServerServiceAccount(address serverServiceAccount) external onlyAdmin {
serverService = serverServiceAccount;
}
}
// 基礎庫,數據儲存、數據結構、事件
// 後端服務會監控合約,當發生事件時,會將記錄拋上receipt。
contract FbBase is ManagePermission {
// Storage
// Data Structure
struct policyContract {
uint256 eventId; // 事件流水號
uint256 FKtransEvent;
uint256 createTime;
address creator;
bytes32 transHash; // 交易號
address contractAddress; // 合約地址
uint256 templateIndex; // 使用樣本索引
bool tableEffect; // 有效性
}
struct transEvent {
uint256 eventId; // 事件流水號
uint256 eventTime;
uint256 eventAtt; // 事件屬性
address driver; // 事件驅動者
bool tableEffect; // 有效性
}
// schema
struct userSchema {
uint256 eventId;
mapping (uint256 => policyContract) policyContractRepo;
mapping (uint256 => transEvent) transEventByRepo;
}
mapping (address => userSchema) userInfo;
// event trace
// arg1:事件屬性, arg2:傳送事件Info,
event _transEvent(uint256 indexed eventAtt, uint256 eventId, uint256 transEventId);
event _policyContract(uint256 indexed eventAtt, uint256 eventId, uint256 transEventId, uint256 policyContractId);
}
// Template Interface
contract TemplateInterface {
function createContract(bytes calldata arg) external;
}
// 父合約功能模組
contract ParentContract is FbBase {
// storage
// template Info
struct templateDetail {
string author;
address templateAddress;
bool effectiveness;
}
mapping (uint256 => templateDetail) templateInfo;
TemplateInterface templatecontract;
// setting
// 獲取樣板操作
function getTemplatePointer(uint256 id) internal {
templatecontract = TemplateInterface(templateInfo[id].templateAddress);
}
// internal operation
// 建立樣板
function templateCreate(uint256 id, bytes memory arg) internal {
getTemplatePointer(id);
templatecontract.createContract(arg);
}
// interface
// 父合約位置設定
function setTemplateAddress(uint256 id, address _address) external onlyAdmin() {
templateInfo[id].templateAddress = _address;
templateInfo[id].effectiveness = true;
}
}
contract FbLifeInterface is ParentContract {
address private admin;
/* Interface */
function createPolicyContract(uint256 templateId, bytes calldata arg) external {
templateCreate(templateId, arg);
}
}
pragma solidity ^0.5.0;
contract ManagePermission {
address private _admin;
address private serverService;
constructor() public {
_admin = msg.sender;
}
function isAdmin() internal view returns(bool) {
return msg.sender == _admin;
}
modifier onlyAdmin() {
require(isAdmin());
_;
}
function isServer() internal view returns(bool) {
return msg.sender == serverService;
}
modifier onlyService() {
require(isServer());
_;
}
// Interface
// 設定後端服務帳戶
function setServerServiceAccount(address serverServiceAccount) external onlyAdmin {
serverService = serverServiceAccount;
}
}
// Template Status
contract TemplateStatusQueue is ManagePermission {
}
// Template Event
contract TemplateEvent is TemplateStatusQueue {
}
// Template Rule
contract TemplateRule is TemplateEvent {
}
// policy contract interface
contract ContractI {
}
// Template Interface
contract Template is TemplateRule {
// Interface
// create Policy Contract
function createContract() public onlyService() {
// record event & arg
// create contract
new Contract();
}
// set policy contract
// set --> role check --> push
}
// Main content
contract Contract {
address private serviceAccount;
// Interface
constructor () public {
serviceAccount = msg.sender;
}
function setPolicyContract() external {
}
}
pragma solidity ^0.5.0;
/**
* @title Seriality
* @dev The Seriality contract is the main interface for serializing data using the TypeToBytes, BytesToType and SizeOf
* @author [email protected]
*/
import "./BytesToTypes.sol";
import "./TypesToBytes.sol";
import "./SizeOf.sol";
contract Seriality is BytesToTypes, TypesToBytes, SizeOf {
constructor() public {
}
}
pragma solidity ^0.5.0;
import "./Seriality.sol";
contract StringsReturn is Seriality {
function stringCaller() public pure returns( string memory out1,
string memory out2,
string memory out3,
string memory out4,
string memory out5)
{
bytes memory buffer = new bytes(320);
uint offset = stringCallee(buffer);
//deserializing
out1 = new string (getStringSize(offset, buffer));
bytesToString(offset, buffer, bytes(out1));
offset -= sizeOfString(out1);
out2 = new string (getStringSize(offset, buffer));
bytesToString(offset, buffer, bytes(out2));
offset -= sizeOfString(out2);
out3 = new string (getStringSize(offset, buffer));
bytesToString(offset, buffer, bytes(out3));
offset -= sizeOfString(out3);
out4 = new string (getStringSize(offset, buffer));
bytesToString(offset, buffer, bytes(out4));
offset -= sizeOfString(out4);
out5 = new string (getStringSize(offset, buffer));
bytesToString(offset, buffer, bytes(out5));
}
function stringCallee(bytes memory buffer) public pure returns (uint buffer_size) {
string memory out1 = new string(32);
string memory out2 = new string(32);
string memory out3 = new string(32);
string memory out4 = new string(32);
string memory out5 = new string(32);
out1 = "Come on baby lets dance!";
out2 = "May I buy you a drink?";
out3 = "I am an itinerant programmer";
out4 = "Inam javab lashi!";
out5 = "Bia inja dahan service";
// Serializing
buffer_size = sizeOfString(out5) +
sizeOfString(out4) +
sizeOfString(out3) +
sizeOfString(out2) +
sizeOfString(out1);
uint offset = buffer_size;
stringToBytes(offset, bytes(out1), buffer);
offset -= sizeOfString(out1);
stringToBytes(offset, bytes(out2), buffer);
offset -= sizeOfString(out2);
stringToBytes(offset, bytes(out3), buffer);
offset -= sizeOfString(out3);
stringToBytes(offset, bytes(out4), buffer);
offset -= sizeOfString(out4);
stringToBytes(offset, bytes(out5), buffer);
return buffer_size;
}
}
pragma solidity ^0.5.0;
/**
* @title SizeOf
* @dev The SizeOf return the size of the solidity types in byte
* @author [email protected]
*/
contract SizeOf {
function sizeOfString(string memory _in) internal pure returns(uint _size){
_size = bytes(_in).length / 32;
if(bytes(_in).length % 32 != 0)
_size++;
_size++; // first 32 bytes is reserved for the size of the string
_size *= 32;
}
function sizeOfInt(uint16 _postfix) internal pure returns(uint size){
assembly{
switch _postfix
case 8 { size := 1 }
case 16 { size := 2 }
case 24 { size := 3 }
case 32 { size := 4 }
case 40 { size := 5 }
case 48 { size := 6 }
case 56 { size := 7 }
case 64 { size := 8 }
case 72 { size := 9 }
case 80 { size := 10 }
case 88 { size := 11 }
case 96 { size := 12 }
case 104 { size := 13 }
case 112 { size := 14 }
case 120 { size := 15 }
case 128 { size := 16 }
case 136 { size := 17 }
case 144 { size := 18 }
case 152 { size := 19 }
case 160 { size := 20 }
case 168 { size := 21 }
case 176 { size := 22 }
case 184 { size := 23 }
case 192 { size := 24 }
case 200 { size := 25 }
case 208 { size := 26 }
case 216 { size := 27 }
case 224 { size := 28 }
case 232 { size := 29 }
case 240 { size := 30 }
case 248 { size := 31 }
case 256 { size := 32 }
default { size := 32 }
}
}
function sizeOfUint(uint16 _postfix) internal pure returns(uint size){
return sizeOfInt(_postfix);
}
function sizeOfAddress() internal pure returns(uint8){
return 20;
}
function sizeOfBool() internal pure returns(uint8){
return 1;
}
}
pragma solidity ^0.5.0;
/**
* @title TypesToBytes
* @dev The TypesToBytes contract converts the standard solidity types to the byte array
* @author [email protected]
*/
contract TypesToBytes {
constructor() internal {
}
function addressToBytes(uint _offst, address _input, bytes memory _output) internal pure {
assembly {
mstore(add(_output, _offst), _input)
}
}
function bytes32ToBytes(uint _offst, bytes32 _input, bytes memory _output) internal pure {
assembly {
mstore(add(_output, _offst), _input)
mstore(add(add(_output, _offst),32), add(_input,32))
}
}
function boolToBytes(uint _offst, bool _input, bytes memory _output) internal pure {
uint8 x = _input == false ? 0 : 1;
assembly {
mstore(add(_output, _offst), x)
}
}
function stringToBytes(uint _offst, bytes memory _input, bytes memory _output) internal pure {
uint256 stack_size = _input.length / 32;
if(_input.length % 32 > 0) stack_size++;
assembly {
stack_size := add(stack_size,1)//adding because of 32 first bytes memory as the length
for { let index := 0 } lt(index,stack_size){ index := add(index ,1) } {
mstore(add(_output, _offst), mload(add(_input,mul(index,32))))
_offst := sub(_offst , 32)
}
}
}
function intToBytes(uint _offst, int _input, bytes memory _output) internal pure {
assembly {
mstore(add(_output, _offst), _input)
}
}
function uintToBytes(uint _offst, uint _input, bytes memory _output) internal pure {
assembly {
mstore(add(_output, _offst), _input)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment