commit 60bc54933946b1c34bc5a0fd1b85df7b9877823b
parent c9232020d98a19ff918b8a19263d4ce361167b3b
Author: lash <dev@holbrook.no>
Date: Mon, 27 Mar 2023 20:26:37 +0100
Limit visibility of internal contract methods
Diffstat:
5 files changed, 65 insertions(+), 49 deletions(-)
diff --git a/python/eth_offline/data/Offline.bin b/python/eth_offline/data/Offline.bin
@@ -1 +1 @@
-608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550611006806100606000396000f3fe608060405234801561001057600080fd5b506004361061005e576000357c0100000000000000000000000000000000000000000000000000000000900480631ecc95c814610063578063772295f6146100935780638da5cb5b146100c3575b600080fd5b61007d60048036038101906100789190610a5d565b6100e1565b60405161008a9190610af0565b60405180910390f35b6100ad60048036038101906100a89190610b0b565b61014a565b6040516100ba9190610c09565b60405180910390f35b6100cb610563565b6040516100d89190610c33565b60405180910390f35b600060606000806100f0610563565b90506100fb8661058c565b92508280519060200120915061011182866107f2565b73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1614935050505092915050565b6101526108c9565b6060806060605485511461019b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161019290610cab565b60405180910390fd5b601467ffffffffffffffff8111156101b6576101b5610932565b5b6040519080825280601f01601f1916602001820160405280156101e85781602001600182028036833780820191505090505b509250602067ffffffffffffffff81111561020657610205610932565b5b6040519080825280601f01601f1916602001820160405280156102385781602001600182028036833780820191505090505b509050602067ffffffffffffffff81111561025657610255610932565b5b6040519080825280601f01601f1916602001820160405280156102885781602001600182028036833780820191505090505b50915060005b8351811015610349578581815181106102aa576102a9610ccb565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f01000000000000000000000000000000000000000000000000000000000000000284828151811061030757610306610ccb565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061034190610d29565b91505061028e565b5060005b602081101561041457856014826103649190610d71565b8151811061037557610374610ccb565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f0100000000000000000000000000000000000000000000000000000000000000028382815181106103d2576103d1610ccb565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061040c90610d29565b91505061034d565b5060005b60208110156104df578560348261042f9190610d71565b815181106104405761043f610ccb565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f01000000000000000000000000000000000000000000000000000000000000000282828151811061049d5761049c610ccb565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535080806104d790610d29565b915050610418565b50826104ea90610e11565b6c010000000000000000000000009004846000019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff16815250508161053b90610e8d565b8460200181815250508061054e90610e8d565b60019004846040018181525050505050919050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b606080600080845160166105a09190610d71565b67ffffffffffffffff8111156105b9576105b8610932565b5b6040519080825280601f01601f1916602001820160405280156105eb5781602001600182028036833780820191505090505b50925060197f0100000000000000000000000000000000000000000000000000000000000000028360008151811061062657610625610ccb565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535060029150306c0100000000000000000000000002905060005b601481101561070d5781816014811061068a57610689610ccb565b5b1a7f0100000000000000000000000000000000000000000000000000000000000000028484836106ba9190610d71565b815181106106cb576106ca610ccb565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061070590610d29565b91505061066e565b5060148261071b9190610d71565b915060005b85518110156107e65785818151811061073c5761073b610ccb565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f0100000000000000000000000000000000000000000000000000000000000000028484836107939190610d71565b815181106107a4576107a3610ccb565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535080806107de90610d29565b915050610720565b50829350505050919050565b60008060008061080185610861565b9250925092506001868285856040516000815260200160405260405161082a9493929190610f1f565b6020604051602081039080840390855afa15801561084c573d6000803e3d6000fd5b50505060206040510351935050505092915050565b600080600060418451146108aa576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108a190610fb0565b60405180910390fd5b6020840151925060408401519150606084015160001a90509193909250565b6040518060600160405280600073ffffffffffffffffffffffffffffffffffffffff16815260200160008019168152602001600081525090565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b61096a82610921565b810181811067ffffffffffffffff8211171561098957610988610932565b5b80604052505050565b600061099c610903565b90506109a88282610961565b919050565b600067ffffffffffffffff8211156109c8576109c7610932565b5b6109d182610921565b9050602081019050919050565b82818337600083830152505050565b6000610a006109fb846109ad565b610992565b905082815260208101848484011115610a1c57610a1b61091c565b5b610a278482856109de565b509392505050565b600082601f830112610a4457610a43610917565b5b8135610a548482602086016109ed565b91505092915050565b60008060408385031215610a7457610a7361090d565b5b600083013567ffffffffffffffff811115610a9257610a91610912565b5b610a9e85828601610a2f565b925050602083013567ffffffffffffffff811115610abf57610abe610912565b5b610acb85828601610a2f565b9150509250929050565b60008115159050919050565b610aea81610ad5565b82525050565b6000602082019050610b056000830184610ae1565b92915050565b600060208284031215610b2157610b2061090d565b5b600082013567ffffffffffffffff811115610b3f57610b3e610912565b5b610b4b84828501610a2f565b91505092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610b7f82610b54565b9050919050565b610b8f81610b74565b82525050565b6000819050919050565b610ba881610b95565b82525050565b6000819050919050565b610bc181610bae565b82525050565b606082016000820151610bdd6000850182610b86565b506020820151610bf06020850182610b9f565b506040820151610c036040850182610bb8565b50505050565b6000606082019050610c1e6000830184610bc7565b92915050565b610c2d81610b74565b82525050565b6000602082019050610c486000830184610c24565b92915050565b600082825260208201905092915050565b7f4552525f434f4e54454e545f4c454e4754480000000000000000000000000000600082015250565b6000610c95601283610c4e565b9150610ca082610c5f565b602082019050919050565b60006020820190508181036000830152610cc481610c88565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610d3482610bae565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610d6657610d65610cfa565b5b600182019050919050565b6000610d7c82610bae565b9150610d8783610bae565b9250828201905080821115610d9f57610d9e610cfa565b5b92915050565b600081519050919050565b6000819050602082019050919050565b60007fffffffffffffffffffffffffffffffffffffffff00000000000000000000000082169050919050565b6000610df88251610dc0565b80915050919050565b60008160020a8302905092915050565b6000610e1c82610da5565b82610e2684610db0565b9050610e3181610dec565b92506014821015610e7157610e6c7fffffffffffffffffffffffffffffffffffffffff00000000000000000000000083601403600802610e01565b831692505b5050919050565b6000610e848251610b95565b80915050919050565b6000610e9882610da5565b82610ea284610db0565b9050610ead81610e78565b92506020821015610eed57610ee87fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff83602003600802610e01565b831692505b5050919050565b610efd81610b95565b82525050565b600060ff82169050919050565b610f1981610f03565b82525050565b6000608082019050610f346000830187610ef4565b610f416020830186610f10565b610f4e6040830185610ef4565b610f5b6060830184610ef4565b95945050505050565b7f696e76616c6964207369676e6174757265206c656e6774680000000000000000600082015250565b6000610f9a601883610c4e565b9150610fa582610f64565b602082019050919050565b60006020820190508181036000830152610fc981610f8d565b905091905056fea264697066735822122002c8390b5ff45e86ce27de443185447f0eba191ffb863ee2a0f6a649827aaf6364736f6c63430008130033
-\ No newline at end of file
+608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550611202806100606000396000f3fe608060405234801561001057600080fd5b506004361061005e576000357c0100000000000000000000000000000000000000000000000000000000900480631ecc95c8146100635780633859b9fb14610093578063ce606ee0146100c3575b600080fd5b61007d60048036038101906100789190610ba7565b6100e1565b60405161008a9190610c3a565b60405180910390f35b6100ad60048036038101906100a89190610cb3565b6101ac565b6040516100ba9190610c3a565b60405180910390f35b6100cb6102cb565b6040516100d89190610d1e565b60405180910390f35b600060606000806100f1866102ef565b9250828051906020012091506101078286610555565b90503073ffffffffffffffffffffffffffffffffffffffff16633859b9fb82886040518363ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401610160929190610db8565b602060405180830381865afa15801561017d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101a19190610e14565b935050505092915050565b60006101b6610a13565b6101bf836105c4565b9050600073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1603610230576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161022790610e9e565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff16816000015173ffffffffffffffffffffffffffffffffffffffff16036102725760009150506102c5565b8373ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16149150505b92915050565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b606080600080845160166103039190610ef7565b67ffffffffffffffff81111561031c5761031b610a7c565b5b6040519080825280601f01601f19166020018201604052801561034e5781602001600182028036833780820191505090505b50925060197f0100000000000000000000000000000000000000000000000000000000000000028360008151811061038957610388610f2b565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535060029150306c0100000000000000000000000002905060005b6014811015610470578181601481106103ed576103ec610f2b565b5b1a7f01000000000000000000000000000000000000000000000000000000000000000284848361041d9190610ef7565b8151811061042e5761042d610f2b565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061046890610f5a565b9150506103d1565b5060148261047e9190610ef7565b915060005b85518110156105495785818151811061049f5761049e610f2b565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f0100000000000000000000000000000000000000000000000000000000000000028484836104f69190610ef7565b8151811061050757610506610f2b565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061054190610f5a565b915050610483565b50829350505050919050565b600080600080610564856109ab565b9250925092506001868285856040516000815260200160405260405161058d9493929190610fd7565b6020604051602081039080840390855afa1580156105af573d6000803e3d6000fd5b50505060206040510351935050505092915050565b6105cc610a13565b606080606060548551146105e2575050506109a6565b601467ffffffffffffffff8111156105fd576105fc610a7c565b5b6040519080825280601f01601f19166020018201604052801561062f5781602001600182028036833780820191505090505b509250602067ffffffffffffffff81111561064d5761064c610a7c565b5b6040519080825280601f01601f19166020018201604052801561067f5781602001600182028036833780820191505090505b509050602067ffffffffffffffff81111561069d5761069c610a7c565b5b6040519080825280601f01601f1916602001820160405280156106cf5781602001600182028036833780820191505090505b50915060005b8351811015610790578581815181106106f1576106f0610f2b565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f01000000000000000000000000000000000000000000000000000000000000000284828151811061074e5761074d610f2b565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061078890610f5a565b9150506106d5565b5060005b602081101561085b57856014826107ab9190610ef7565b815181106107bc576107bb610f2b565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f01000000000000000000000000000000000000000000000000000000000000000283828151811061081957610818610f2b565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061085390610f5a565b915050610794565b5060005b602081101561092657856034826108769190610ef7565b8151811061088757610886610f2b565b5b60200101517f010000000000000000000000000000000000000000000000000000000000000090047f0100000000000000000000000000000000000000000000000000000000000000028282815181106108e4576108e3610f2b565b5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061091e90610f5a565b91505061085f565b50826109319061107d565b6c010000000000000000000000009004846000019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff168152505081610982906110f9565b84602001818152505080610995906110f9565b600190048460400181815250505050505b919050565b600080600060418451146109f4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016109eb906111ac565b60405180910390fd5b6020840151925060408401519150606084015160001a90509193909250565b6040518060600160405280600073ffffffffffffffffffffffffffffffffffffffff16815260200160008019168152602001600081525090565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b610ab482610a6b565b810181811067ffffffffffffffff82111715610ad357610ad2610a7c565b5b80604052505050565b6000610ae6610a4d565b9050610af28282610aab565b919050565b600067ffffffffffffffff821115610b1257610b11610a7c565b5b610b1b82610a6b565b9050602081019050919050565b82818337600083830152505050565b6000610b4a610b4584610af7565b610adc565b905082815260208101848484011115610b6657610b65610a66565b5b610b71848285610b28565b509392505050565b600082601f830112610b8e57610b8d610a61565b5b8135610b9e848260208601610b37565b91505092915050565b60008060408385031215610bbe57610bbd610a57565b5b600083013567ffffffffffffffff811115610bdc57610bdb610a5c565b5b610be885828601610b79565b925050602083013567ffffffffffffffff811115610c0957610c08610a5c565b5b610c1585828601610b79565b9150509250929050565b60008115159050919050565b610c3481610c1f565b82525050565b6000602082019050610c4f6000830184610c2b565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610c8082610c55565b9050919050565b610c9081610c75565b8114610c9b57600080fd5b50565b600081359050610cad81610c87565b92915050565b60008060408385031215610cca57610cc9610a57565b5b6000610cd885828601610c9e565b925050602083013567ffffffffffffffff811115610cf957610cf8610a5c565b5b610d0585828601610b79565b9150509250929050565b610d1881610c75565b82525050565b6000602082019050610d336000830184610d0f565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610d73578082015181840152602081019050610d58565b60008484015250505050565b6000610d8a82610d39565b610d948185610d44565b9350610da4818560208601610d55565b610dad81610a6b565b840191505092915050565b6000604082019050610dcd6000830185610d0f565b8181036020830152610ddf8184610d7f565b90509392505050565b610df181610c1f565b8114610dfc57600080fd5b50565b600081519050610e0e81610de8565b92915050565b600060208284031215610e2a57610e29610a57565b5b6000610e3884828501610dff565b91505092915050565b600082825260208201905092915050565b7f4552525f5a45524f5f56414c494441544f520000000000000000000000000000600082015250565b6000610e88601283610e41565b9150610e9382610e52565b602082019050919050565b60006020820190508181036000830152610eb781610e7b565b9050919050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610f0282610ebe565b9150610f0d83610ebe565b9250828201905080821115610f2557610f24610ec8565b5b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b6000610f6582610ebe565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610f9757610f96610ec8565b5b600182019050919050565b6000819050919050565b610fb581610fa2565b82525050565b600060ff82169050919050565b610fd181610fbb565b82525050565b6000608082019050610fec6000830187610fac565b610ff96020830186610fc8565b6110066040830185610fac565b6110136060830184610fac565b95945050505050565b6000819050602082019050919050565b60007fffffffffffffffffffffffffffffffffffffffff00000000000000000000000082169050919050565b6000611064825161102c565b80915050919050565b60008160020a8302905092915050565b600061108882610d39565b826110928461101c565b905061109d81611058565b925060148210156110dd576110d87fffffffffffffffffffffffffffffffffffffffff0000000000000000000000008360140360080261106d565b831692505b5050919050565b60006110f08251610fa2565b80915050919050565b600061110482610d39565b8261110e8461101c565b9050611119816110e4565b92506020821015611159576111547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8360200360080261106d565b831692505b5050919050565b7f696e76616c6964207369676e6174757265206c656e6774680000000000000000600082015250565b6000611196601883610e41565b91506111a182611160565b602082019050919050565b600060208201905081810360008301526111c581611189565b905091905056fea26469706673582212207ae8dc44a3ce9f283d9d464907bd95ad3561172a33c9f30142d38568d93e17f664736f6c63430008130033
+\ No newline at end of file
diff --git a/python/eth_offline/data/Offline.json b/python/eth_offline/data/Offline.json
@@ -1 +1 @@
-[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"splitData","outputs":[{"components":[{"internalType":"address","name":"beneficiary","type":"address"},{"internalType":"bytes32","name":"domain","type":"bytes32"},{"internalType":"uint256","name":"value","type":"uint256"}],"internalType":"struct OfflineRubber.Instruction","name":"instruction","type":"tuple"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes","name":"_data","type":"bytes"},{"internalType":"bytes","name":"_signature","type":"bytes"}],"name":"verifyOfflineRequest","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}]
+[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"contractOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_validator","type":"address"},{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"isOfflineValidator","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"_data","type":"bytes"},{"internalType":"bytes","name":"_signature","type":"bytes"}],"name":"verifyOfflineRequest","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}]
diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py
@@ -52,14 +52,16 @@ class TestOfflineEth(EthTesterCase):
logg.debug('smart contract published with hash {} address {}'.format(r, self.address))
- def test_split(self):
+ def test_validator(self):
c = TxFactory(self.chain_spec)
j = JSONRPCRequest()
o = j.template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
- enc.method('splitData')
+ enc.method('isOfflineValidator')
+ enc.typ(ABIContractType.ADDRESS)
enc.typ(ABIContractType.BYTES)
+ enc.address(strip_0x(self.accounts[0]))
enc.bytes(strip_0x(self.accounts[1]) + '666f6f')
data = add_0x(enc.get())
tx = c.template(self.accounts[0], self.address)
@@ -67,9 +69,8 @@ class TestOfflineEth(EthTesterCase):
o['params'].append(c.normalize(tx))
o['params'].append('latest')
o = j.finalize(o)
-
- with self.assertRaises(JSONRPCException):
- self.rpc.do(o)
+ r = self.rpc.do(o)
+ self.assertEqual(int(r, 16), 0)
v = os.urandom(64)
c = TxFactory(self.chain_spec)
@@ -77,8 +78,10 @@ class TestOfflineEth(EthTesterCase):
o = j.template()
o['method'] = 'eth_call'
enc = ABIContractEncoder()
- enc.method('splitData')
+ enc.method('isOfflineValidator')
+ enc.typ(ABIContractType.ADDRESS)
enc.typ(ABIContractType.BYTES)
+ enc.address(strip_0x(self.accounts[2]))
enc.bytes(strip_0x(self.accounts[1]) + v.hex())
data = add_0x(enc.get())
tx = c.template(self.accounts[0], self.address)
@@ -89,19 +92,38 @@ class TestOfflineEth(EthTesterCase):
r = self.rpc.do(o)
r = strip_0x(r)
- self.assertTrue(same_hex(r[24:64], self.accounts[1]))
- self.assertTrue(same_hex(r[64:128], v[:32].hex()))
- self.assertTrue(same_hex(r[128:192], v[32:].hex()))
+ self.assertEqual(int(r, 16), 0)
+
+ c = TxFactory(self.chain_spec)
+ j = JSONRPCRequest()
+ o = j.template()
+ o['method'] = 'eth_call'
+ enc = ABIContractEncoder()
+ enc.method('isOfflineValidator')
+ enc.typ(ABIContractType.ADDRESS)
+ enc.typ(ABIContractType.BYTES)
+ enc.address(strip_0x(self.accounts[0]))
+ enc.bytes(strip_0x(self.accounts[1]) + v.hex())
+ data = add_0x(enc.get())
+ tx = c.template(self.accounts[0], self.address)
+ tx = c.set_code(tx, data)
+ o['params'].append(c.normalize(tx))
+ o['params'].append('latest')
+ o = j.finalize(o)
+
+ r = self.rpc.do(o)
+ r = strip_0x(r)
+ self.assertEqual(int(r, 16), 1)
+
-
- def test_verify(self):
+ def test_ok_verify(self):
beneficiary_bin = bytes.fromhex(strip_0x(self.accounts[2]))
- msg_bin = b'bar'
+ msg_bin = os.urandom(64)
msg_data = beneficiary_bin + msg_bin
sig = self.signer.sign_validator_message(self.accounts[0], self.address, msg_data)
sig = sig[:64] + (sig[64] + 27).to_bytes(1, byteorder='big')
- logg.debug('message is {} signed by {}'.format(msg_data.hex(), self.accounts[0]))
+ logg.debug('message is {} ({}) signed by {}'.format(msg_data.hex(), len(msg_data), self.accounts[0]))
c = TxFactory(self.chain_spec)
j = JSONRPCRequest()
@@ -127,13 +149,11 @@ class TestOfflineEth(EthTesterCase):
def test_verify_fail_owner(self):
- contract_bin = bytes.fromhex(strip_0x(self.address))
beneficiary_bin = bytes.fromhex(strip_0x(self.accounts[2]))
- msg_bin = b'bar'
+ msg_bin = os.urandom(64)
msg_data = beneficiary_bin + msg_bin
- msg_tosign = contract_bin + msg_data
- sig = self.signer.sign_ethereum_message(self.accounts[1], msg_tosign)
+ sig = self.signer.sign_ethereum_message(self.accounts[1], msg_data)
sig = sig[:64] + (sig[64] + 27).to_bytes(1, byteorder='big')
logg.debug('message is {} signed by {}'.format(msg_data.hex(), self.accounts[1]))
diff --git a/solidity/Offline.sol b/solidity/Offline.sol
@@ -12,22 +12,34 @@ contract OfflineRubber is Offline {
uint256 value;
}
- address contractOwner;
+ address public contractOwner;
constructor() {
contractOwner = msg.sender;
}
- function owner() public override view returns(address) {
- return contractOwner;
+ function isOfflineValidator(address _validator, bytes memory _data) external override view returns(bool) {
+ Instruction memory instruction;
+
+ instruction = splitData(_data);
+
+ require(_validator != address(0), 'ERR_ZERO_VALIDATOR');
+
+ if (instruction.beneficiary == address(0)) {
+ return false;
+ }
+
+ return contractOwner == _validator;
}
- function splitData(bytes memory _data) public pure returns(Instruction memory instruction) {
+ function splitData(bytes memory _data) private pure returns(Instruction memory instruction) {
bytes memory beneficiary;
bytes memory domain;
bytes memory value;
- require(_data.length == 84, 'ERR_CONTENT_LENGTH');
+ if(_data.length != 84) {
+ return instruction;
+ }
beneficiary = new bytes(20);
value = new bytes(32);
diff --git a/solidity/OfflineBase.sol b/solidity/OfflineBase.sol
@@ -4,15 +4,18 @@ pragma solidity ^0.8.0;
// Some methods are copied under other licenses, please see code comments for details
abstract contract Offline {
+ function isOfflineValidator(address _validator, bytes memory _data) external virtual view returns(bool);
+
function verifyOfflineRequest(bytes memory _data, bytes memory _signature) public view returns(bool) {
bytes memory message;
bytes32 messageDigest;
- address _owner = owner();
+ address _owner;
message = toValidatorMessage(_data);
messageDigest = keccak256(message);
+ _owner = recoverSigner(messageDigest, _signature);
- return _owner == recoverSigner(messageDigest, _signature);
+ return this.isOfflineValidator(_owner, _data);
}
function toValidatorMessage(bytes memory _data) private view returns(bytes memory) {
@@ -20,7 +23,7 @@ abstract contract Offline {
uint256 c;
bytes20 contractBytes;
- message = new bytes(22 + _data.length); //messageLength);
+ message = new bytes(22 + _data.length);
message[0] = 0x19;
c = 2;
@@ -28,17 +31,16 @@ abstract contract Offline {
for (uint256 i = 0; i < 20; i++) {
message[i+c] = contractBytes[i];
}
+
c += 20;
-
for (uint256 i = 0; i < _data.length; i++) {
message[i+c] = _data[i];
}
return message;
}
- function owner() public virtual view returns(address);
-
// from https://solidity-by-example.org/signature/
+ // Published under MIT license
function recoverSigner(bytes32 _ethSignedMessageHash, bytes memory _signature) private pure returns (address) {
(bytes32 r, bytes32 s, uint8 v) = splitSignature(_signature);
return ecrecover(_ethSignedMessageHash, v, r, s);
@@ -69,22 +71,4 @@ abstract contract Offline {
// implicitly return (r, s, v)
}
-
- // https://ethereum.stackexchange.com/questions/10811/solidity-concatenate-uint-into-a-string
- // Published under license CC BY-SA 3.0
- function uintToString(uint v) private pure returns (string memory str) {
- uint maxlength = 100;
- bytes memory reversed = new bytes(maxlength);
- uint i = 0;
- while (v != 0) {
- uint remainder = v % 10;
- v = v / 10;
- reversed[i++] = bytes1(uint8(48 + remainder));
- }
- bytes memory s = new bytes(i);
- for (uint j = 0; j < i; j++) {
- s[j] = reversed[i - 1 - j];
- }
- str = string(s);
- }
}