event-msg

Simple, embedded news post vehicle for EVM smart contracts
Log | Files | Refs

commit aadd6d2346f4b9f1f976db7ffb71466b13af2ae9
Author: lash <dev@holbrook.no>
Date:   Fri, 17 Feb 2023 22:53:00 +0000

Initial commit

Diffstat:
Apython/eth_event_msg/__init__.py | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apython/eth_event_msg/data/Msg.bin | 2++
Apython/eth_event_msg/data/Msg.json | 1+
Apython/requirements.txt | 1+
Apython/test_requirements.txt | 2++
Apython/tests/test_basic.py | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Asolidity/Makefile | 11+++++++++++
Asolidity/Msg.sol | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 250 insertions(+), 0 deletions(-)

diff --git a/python/eth_event_msg/__init__.py b/python/eth_event_msg/__init__.py @@ -0,0 +1,84 @@ +# standard imports +import os +import logging + +# external imports +from chainlib.eth.tx import TxFormat +from chainlib.eth.tx import TxFactory +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.jsonrpc import JSONRPCRequest +from chainlib.eth.contract import ABIContractEncoder +from chainlib.eth.contract import ABIContractType +from hexathon import strip_0x +from hexathon import add_0x + +moddir = os.path.dirname(__file__) +datadir = os.path.join(moddir, 'data') + +logg = logging.getLogger(__name__) + + +class EventMsg(TxFactory): + + __abi = None + __bytecode = None + + @staticmethod + def abi(): + if EventMsg.__abi == None: + f = open(os.path.join(datadir, 'Msg.json'), 'r') + EventMsg.__abi = json.load(f) + f.close() + return EventMsg.__abi + + + @staticmethod + def bytecode(version=None): + if EventMsg.__bytecode == None: + f = open(os.path.join(datadir, 'Msg.bin')) + EventMsg.__bytecode = f.read() + f.close() + return EventMsg.__bytecode + + + @staticmethod + def gas(code=None): + return 4000000 + + + def constructor(self, sender_address, tx_format=TxFormat.JSONRPC, version=None): + code = self.cargs(version=version) + tx = self.template(sender_address, None, use_nonce=True) + tx = self.set_code(tx, code) + return self.finalize(tx, tx_format) + + + @staticmethod + def cargs(version=None): + return EventMsg.bytecode() + + + def to_hash(self, contract_address, v, sender_address=ZERO_ADDRESS, id_generator=None): + j = JSONRPCRequest(id_generator) + o = j.template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('toHash') + enc.typ(ABIContractType.BYTES) + enc.bytes(v) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address) + tx = self.set_code(tx, data) + o['params'].append(self.normalize(tx)) + o['params'].append('latest') + o = j.finalize(o) + return o + + + @classmethod + def parse_token_spec(self, v): + v = strip_0x(v) + l = int(v[64:128], 16) + logg.debug('llll {} {}'.format(l, v[64:128])) + r = v[128:128+(l*2)] + return r diff --git a/python/eth_event_msg/data/Msg.bin b/python/eth_event_msg/data/Msg.bin @@ -0,0 +1 @@ +60806040523480156200001157600080fd5b5062000065602060126040518060400160405280600681526020017f736861323536000000000000000000000000000000000000000000000000000081525062000086640100000000026401000000009004565b6200008060126200035c640100000000026401000000009004565b620007db565b6060819050601081511115620000d3576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401620000ca90620005b3565b60405180910390fd5b620000dd620004ec565b6000600790505b60008160ff16106200014157600060088262000101919062000611565b60ff1660ff9060020a029050600086821667ffffffffffffffff1611156200012a575062000141565b8180620001379062000657565b92505050620000e4565b60018162000150919062000685565b826020019060ff16908160ff16815250506008816007620001729190620006c1565b6200017e919062000611565b60ff168567ffffffffffffffff169060020a02780100000000000000000000000000000000000000000000000002826080019077ffffffffffffffffffffffffffffffffffffffffffffffff1916908177ffffffffffffffffffffffffffffffffffffffffffffffff1916815250508251826040019060ff16908160ff1681525050826200020c906200076b565b82606001906fffffffffffffffffffffffffffffffff191690816fffffffffffffffffffffffffffffffff19168152505085826000019060ff16908160ff1681525050816000808767ffffffffffffffff16815260200190815260200160002060008201518160000160006101000a81548160ff021916908360ff16021790555060208201518160000160016101000a81548160ff021916908360ff16021790555060408201518160000160026101000a81548160ff021916908360ff16021790555060608201518160000160036101000a8154816fffffffffffffffffffffffffffffffff02191690837001000000000000000000000000000000009004021790555060808201518160000160136101000a81548167ffffffffffffffff0219169083780100000000000000000000000000000000000000000000000090040217905550905050505050505050565b6000806000838152602001908152602001600020905060008160000160009054906101000a900460ff1660ff16116200039457600080fd5b8060016000820160009054906101000a900460ff168160000160006101000a81548160ff021916908360ff1602179055506000820160019054906101000a900460ff168160000160016101000a81548160ff021916908360ff1602179055506000820160029054906101000a900460ff168160000160026101000a81548160ff021916908360ff1602179055506000820160039054906101000a9004700100000000000000000000000000000000028160000160036101000a8154816fffffffffffffffffffffffffffffffff0219169083700100000000000000000000000000000000900402179055506000820160139054906101000a90047801000000000000000000000000000000000000000000000000028160000160136101000a81548167ffffffffffffffff02191690837801000000000000000000000000000000000000000000000000900402179055509050505050565b6040518060a00160405280600060ff168152602001600060ff168152602001600060ff16815260200160006fffffffffffffffffffffffffffffffff19168152602001600077ffffffffffffffffffffffffffffffffffffffffffffffff191681525090565b600082825260208201905092915050565b7f4552525f5052454649585f544f4f5f4c4f4e4700000000000000000000000000600082015250565b60006200059b60138362000552565b9150620005a88262000563565b602082019050919050565b60006020820190508181036000830152620005ce816200058c565b9050919050565b600060ff82169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006200061e82620005d5565b91506200062b83620005d5565b92508282026200063b81620005d5565b915080821462000650576200064f620005e2565b5b5092915050565b60006200066482620005d5565b9150600082036200067a5762000679620005e2565b5b600182039050919050565b60006200069282620005d5565b91506200069f83620005d5565b9250828201905060ff811115620006bb57620006ba620005e2565b5b92915050565b6000620006ce82620005d5565b9150620006db83620005d5565b9250828203905060ff811115620006f757620006f6620005e2565b5b92915050565b600081519050919050565b6000819050602082019050919050565b60007fffffffffffffffffffffffffffffffff0000000000000000000000000000000082169050919050565b600062000752825162000718565b80915050919050565b60008160020a8302905092915050565b60006200077882620006fd565b82620007848462000708565b9050620007918162000744565b92506010821015620007d457620007cf7fffffffffffffffffffffffffffffffff00000000000000000000000000000000836010036008026200075b565b831692505b5050919050565b61145780620007eb6000396000f3fe608060405234801561001057600080fd5b50600436106100a5576000357c0100000000000000000000000000000000000000000000000000000000900480639a7c5e9f116100785780639a7c5e9f14610146578063ad0dec2f14610162578063b238d60314610192578063ccc094c2146101c6576100a5565b806318cbbcfc146100aa5780632c1758c1146100c857806331a5995d146100f857806386fe212d14610116575b600080fd5b6100b26101e2565b6040516100bf9190610b37565b60405180910390f35b6100e260048036038101906100dd9190610b92565b6101e7565b6040516100ef9190610bda565b60405180910390f35b61010061021a565b60405161010d9190610b37565b60405180910390f35b610130600480360381019061012b9190610b92565b610220565b60405161013d9190610b37565b60405180910390f35b610160600480360381019061015b9190610b92565b610307565b005b61017c60048036038101906101779190610d3b565b610496565b6040516101899190610e03565b60405180910390f35b6101ac60048036038101906101a79190610b92565b610758565b6040516101bd959493929190610eb7565b60405180910390f35b6101e060048036038101906101db9190611017565b6107f7565b005b600381565b6000600382106101f657600080fd5b6000820361020b576003600254149050610215565b8160025483161490505b919050565b60025481565b600060108210610265576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161025c906110e3565b60405180910390fd5b60006002548316146102ac576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016102a39061114f565b60405180910390fd5b8160026000828254179250508190555060036002541415157f6b7e2e653f93b645d4ed7292d6429f96637084363e477c8aaea1a43ed13c284e6002546040516102f59190610b37565b60405180910390a26002549050919050565b6000806000838152602001908152602001600020905060008160000160009054906101000a900460ff1660ff161161033e57600080fd5b8060016000820160009054906101000a900460ff168160000160006101000a81548160ff021916908360ff1602179055506000820160019054906101000a900460ff168160000160016101000a81548160ff021916908360ff1602179055506000820160029054906101000a900460ff168160000160026101000a81548160ff021916908360ff1602179055506000820160039054906101000a9004700100000000000000000000000000000000028160000160036101000a8154816fffffffffffffffffffffffffffffffff0219169083700100000000000000000000000000000000900402179055506000820160139054906101000a90047801000000000000000000000000000000000000000000000000028160000160136101000a81548167ffffffffffffffff02191690837801000000000000000000000000000000000000000000000000900402179055509050505050565b60606000600160000160019054906101000a900460ff1660ff16600160000160009054906101000a900460ff1660ff1684516104d2919061119e565b6104dc919061119e565b67ffffffffffffffff8111156104f5576104f4610c10565b5b6040519080825280601f01601f1916602001820160405280156105275781602001600182028036833780820191505090505b50905060005b600160000160019054906101000a900460ff1660ff168110156105ff57600160000160139054906101000a90047801000000000000000000000000000000000000000000000000028160088110610587576105866111d2565b5b1a7f0100000000000000000000000000000000000000000000000000000000000000028282815181106105bd576105bc6111d2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535080806105f790611201565b91505061052d565b600160000160009054906101000a900460ff167f010000000000000000000000000000000000000000000000000000000000000002828281518110610647576106466111d2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061068190611201565b91505060005b845181101561074d578481815181106106a3576106a26111d2565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f0100000000000000000000000000000000000000000000000000000000000000028382846106fa919061119e565b8151811061070b5761070a6111d2565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061074590611201565b915050610687565b508192505050919050565b60006020528060005260406000206000915090508060000160009054906101000a900460ff16908060000160019054906101000a900460ff16908060000160029054906101000a900460ff16908060000160039054906101000a900470010000000000000000000000000000000002908060000160139054906101000a9004780100000000000000000000000000000000000000000000000002905085565b6060819050601081511115610841576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161083890611295565b60405180910390fd5b610849610ab8565b6000600790505b60008160ff16106108a557600060088261086a91906112b5565b60ff1660ff9060020a029050600086821667ffffffffffffffff16111561089157506108a5565b818061089c906112f2565b92505050610850565b6001816108b2919061131b565b826020019060ff16908160ff168152505060088160076108d29190611350565b6108dc91906112b5565b60ff168567ffffffffffffffff169060020a02780100000000000000000000000000000000000000000000000002826080019077ffffffffffffffffffffffffffffffffffffffffffffffff1916908177ffffffffffffffffffffffffffffffffffffffffffffffff1916815250508251826040019060ff16908160ff168152505082610968906113ba565b82606001906fffffffffffffffffffffffffffffffff191690816fffffffffffffffffffffffffffffffff19168152505085826000019060ff16908160ff1681525050816000808767ffffffffffffffff16815260200190815260200160002060008201518160000160006101000a81548160ff021916908360ff16021790555060208201518160000160016101000a81548160ff021916908360ff16021790555060408201518160000160026101000a81548160ff021916908360ff16021790555060608201518160000160036101000a8154816fffffffffffffffffffffffffffffffff02191690837001000000000000000000000000000000009004021790555060808201518160000160136101000a81548167ffffffffffffffff0219169083780100000000000000000000000000000000000000000000000090040217905550905050505050505050565b6040518060a00160405280600060ff168152602001600060ff168152602001600060ff16815260200160006fffffffffffffffffffffffffffffffff19168152602001600077ffffffffffffffffffffffffffffffffffffffffffffffff191681525090565b6000819050919050565b610b3181610b1e565b82525050565b6000602082019050610b4c6000830184610b28565b92915050565b6000604051905090565b600080fd5b600080fd5b610b6f81610b1e565b8114610b7a57600080fd5b50565b600081359050610b8c81610b66565b92915050565b600060208284031215610ba857610ba7610b5c565b5b6000610bb684828501610b7d565b91505092915050565b60008115159050919050565b610bd481610bbf565b82525050565b6000602082019050610bef6000830184610bcb565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b610c4882610bff565b810181811067ffffffffffffffff82111715610c6757610c66610c10565b5b80604052505050565b6000610c7a610b52565b9050610c868282610c3f565b919050565b600067ffffffffffffffff821115610ca657610ca5610c10565b5b610caf82610bff565b9050602081019050919050565b82818337600083830152505050565b6000610cde610cd984610c8b565b610c70565b905082815260208101848484011115610cfa57610cf9610bfa565b5b610d05848285610cbc565b509392505050565b600082601f830112610d2257610d21610bf5565b5b8135610d32848260208601610ccb565b91505092915050565b600060208284031215610d5157610d50610b5c565b5b600082013567ffffffffffffffff811115610d6f57610d6e610b61565b5b610d7b84828501610d0d565b91505092915050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610dbe578082015181840152602081019050610da3565b60008484015250505050565b6000610dd582610d84565b610ddf8185610d8f565b9350610def818560208601610da0565b610df881610bff565b840191505092915050565b60006020820190508181036000830152610e1d8184610dca565b905092915050565b600060ff82169050919050565b610e3b81610e25565b82525050565b60007fffffffffffffffffffffffffffffffff0000000000000000000000000000000082169050919050565b610e7681610e41565b82525050565b60007fffffffffffffffff00000000000000000000000000000000000000000000000082169050919050565b610eb181610e7c565b82525050565b600060a082019050610ecc6000830188610e32565b610ed96020830187610e32565b610ee66040830186610e32565b610ef36060830185610e6d565b610f006080830184610ea8565b9695505050505050565b610f1381610e25565b8114610f1e57600080fd5b50565b600081359050610f3081610f0a565b92915050565b600067ffffffffffffffff82169050919050565b610f5381610f36565b8114610f5e57600080fd5b50565b600081359050610f7081610f4a565b92915050565b600067ffffffffffffffff821115610f9157610f90610c10565b5b610f9a82610bff565b9050602081019050919050565b6000610fba610fb584610f76565b610c70565b905082815260208101848484011115610fd657610fd5610bfa565b5b610fe1848285610cbc565b509392505050565b600082601f830112610ffe57610ffd610bf5565b5b813561100e848260208601610fa7565b91505092915050565b6000806000606084860312156110305761102f610b5c565b5b600061103e86828701610f21565b935050602061104f86828701610f61565b925050604084013567ffffffffffffffff8111156110705761106f610b61565b5b61107c86828701610fe9565b9150509250925092565b600082825260208201905092915050565b7f4552525f494e56414c49445f5354415445000000000000000000000000000000600082015250565b60006110cd601183611086565b91506110d882611097565b602082019050919050565b600060208201905081810360008301526110fc816110c0565b9050919050565b7f4552525f414c52454144595f4c4f434b45440000000000000000000000000000600082015250565b6000611139601283611086565b915061114482611103565b602082019050919050565b600060208201905081810360008301526111688161112c565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006111a982610b1e565b91506111b483610b1e565b92508282019050808211156111cc576111cb61116f565b5b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b600061120c82610b1e565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff820361123e5761123d61116f565b5b600182019050919050565b7f4552525f5052454649585f544f4f5f4c4f4e4700000000000000000000000000600082015250565b600061127f601383611086565b915061128a82611249565b602082019050919050565b600060208201905081810360008301526112ae81611272565b9050919050565b60006112c082610e25565b91506112cb83610e25565b92508282026112d981610e25565b91508082146112eb576112ea61116f565b5b5092915050565b60006112fd82610e25565b9150600082036113105761130f61116f565b5b600182039050919050565b600061132682610e25565b915061133183610e25565b9250828201905060ff81111561134a5761134961116f565b5b92915050565b600061135b82610e25565b915061136683610e25565b9250828203905060ff81111561137f5761137e61116f565b5b92915050565b6000819050602082019050919050565b60006113a18251610e41565b80915050919050565b60008160020a8302905092915050565b60006113c582610d84565b826113cf84611385565b90506113da81611395565b9250601082101561141a576114157fffffffffffffffffffffffffffffffff00000000000000000000000000000000836010036008026113aa565b831692505b505091905056fea26469706673582212209f52446ae313333c552aa40888a89c4e202d49749cdbdfed323b694c995852a164736f6c63430008110033 +\ No newline at end of file diff --git a/python/eth_event_msg/data/Msg.json b/python/eth_event_msg/data/Msg.json @@ -0,0 +1 @@ +[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bool","name":"_final","type":"bool"},{"indexed":false,"internalType":"uint256","name":"_sealState","type":"uint256"}],"name":"SealStateChange","type":"event"},{"inputs":[{"internalType":"uint8","name":"_length","type":"uint8"},{"internalType":"uint64","name":"_codecId","type":"uint64"},{"internalType":"string","name":"_uriPrefix","type":"string"}],"name":"addCodec","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_state","type":"uint256"}],"name":"isSealed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxSealState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"multiCodecs","outputs":[{"internalType":"uint8","name":"l","type":"uint8"},{"internalType":"uint8","name":"codecRLength","type":"uint8"},{"internalType":"uint8","name":"prefixRLength","type":"uint8"},{"internalType":"bytes16","name":"prefix","type":"bytes16"},{"internalType":"bytes8","name":"codec","type":"bytes8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_state","type":"uint256"}],"name":"seal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"sealState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_codec","type":"uint256"}],"name":"setMsgCodec","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"_digest","type":"bytes"}],"name":"toHash","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"}] diff --git a/python/requirements.txt b/python/requirements.txt @@ -0,0 +1 @@ +chainlib-eth~=0.4.16 diff --git a/python/test_requirements.txt b/python/test_requirements.txt @@ -0,0 +1,2 @@ +eth_tester==0.5.0b3 +py-evm==0.3.0a20 diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py @@ -0,0 +1,52 @@ +# standard imports +import os +import unittest +import json +import logging + +# external imports +from chainlib.eth.unittest.ethtester import EthTesterCase +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.connection import RPCConnection +from chainlib.eth.tx import receipt +from chainlib.eth.address import to_checksum_address + +# local imports +from eth_event_msg import EventMsg + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + + +hash_of_foo = '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' + + +class Test(EthTesterCase): + + def setUp(self): + super(Test, self).setUp() + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = EventMsg(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.constructor(self.accounts[0]) + self.conn = RPCConnection.connect(self.chain_spec, 'default') + r = self.conn.do(o) + o = receipt(tx_hash) + rr = self.conn.do(o) + self.assertEqual(rr['status'], 1) + logg.debug('smart contract published with hash {}'.format(r)) + self.address = to_checksum_address(rr['contract_address']) + + + def test_set_codec(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = EventMsg(self.chain_spec) #, signer=self.signer, nonce_oracle=nonce_oracle) + o = c.to_hash(self.address, hash_of_foo, sender_address=self.accounts[0]) + r = self.conn.do(o) + mh = c.parse_token_spec(r) + self.assertEqual(mh[:4], '1220') + self.assertEqual(mh[4:68], hash_of_foo) + + +if __name__ == '__main__': + unittest.main() + diff --git a/solidity/Makefile b/solidity/Makefile @@ -0,0 +1,11 @@ +SOLC = /usr/bin/solc + +all: + $(SOLC) --bin Msg.sol --evm-version byzantium | awk 'NR>3' > Msg.bin + truncate -s -1 Msg.bin + $(SOLC) --abi Msg.sol --evm-version byzantium | awk 'NR>3' > Msg.json + +install-py: all + cp -v Msg*{json,bin} ../python/eth_event_msg/data/ + +install: install-py diff --git a/solidity/Msg.sol b/solidity/Msg.sol @@ -0,0 +1,97 @@ +pragma solidity >=0.6.0; + +contract Msg { + + struct MultiHash { + uint8 l; + uint8 codecRLength; + uint8 prefixRLength; + bytes16 prefix; + bytes8 codec; + } + + mapping (uint256 => MultiHash) public multiCodecs; + MultiHash msgCodec; + + // Implements Sealer + uint256 public sealState; + uint8 constant CODECLIST_STATE = 1; + uint8 constant CODEC_STATE = 2; + uint256 constant public maxSealState = 3; + event SealStateChange(bool indexed _final, uint256 _sealState); + + constructor() { + addCodec(32, 0x12, "sha256"); + setMsgCodec(0x12); + } + + function addCodec(uint8 _length, uint64 _codecId, string memory _uriPrefix) public { + bytes memory prefixBytes; + + prefixBytes = bytes(_uriPrefix); + require(prefixBytes.length <= 16, 'ERR_PREFIX_TOO_LONG'); + MultiHash memory _hsh; + uint8 c; + + c = 7; + while (c >= 0) { + uint64 mask = uint64(0xff << (c * 8)); + if ((mask & _codecId) > 0) { + break; + } + c--; + } + _hsh.codecRLength = c + 1; + _hsh.codec = bytes8(_codecId << ((7 - c) * 8)); + _hsh.prefixRLength = uint8(prefixBytes.length); + _hsh.prefix = bytes16(prefixBytes); + _hsh.l = _length; + + multiCodecs[uint256(_codecId)] = _hsh; + } + + function setMsgCodec(uint256 _codec) public { + MultiHash storage _hsh; + + _hsh = multiCodecs[_codec]; + require(_hsh.l > 0); + + msgCodec = _hsh; + } + + // Implements Sealer + function seal(uint256 _state) public returns(uint256) { + require(_state < 16, 'ERR_INVALID_STATE'); + require(_state & sealState == 0, 'ERR_ALREADY_LOCKED'); + sealState |= _state; + emit SealStateChange(sealState == maxSealState, sealState); + return uint256(sealState); + } + + // Implements Sealer + function isSealed(uint256 _state) public view returns(bool) { + require(_state < maxSealState); + if (_state == 0) { + return sealState == maxSealState; + } + return _state & sealState == _state; + } + + function toHash(bytes memory _digest) public view returns(bytes memory) { + bytes memory r = new bytes(_digest.length + msgCodec.l + msgCodec.codecRLength); + + + uint256 i = 0; + for (i; i < msgCodec.codecRLength; i++) { + r[i] = msgCodec.codec[i]; + } + r[i] = bytes1(msgCodec.l); + i++; + for (uint256 j = 0; j < _digest.length; j++) { + r[i+j] = _digest[j]; + } + + return r; + } + +}