mirror of
https://github.com/status-im/contracts.git
synced 2025-02-23 20:18:42 +00:00
Split contracts into 2 for the time being.
This commit is contained in:
parent
e574f0ccec
commit
46b95636d6
154
contracts/erc_725.v.py
Normal file
154
contracts/erc_725.v.py
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
# Events of the identiy contract.
|
||||||
|
KeyAdded: __log__({key: indexed(address), type: indexed(num256)})
|
||||||
|
KeyRemoved: __log__({key: indexed(address), type: indexed(num256)})
|
||||||
|
ExecutionRequested: __log__({executionId: indexed(num256), to: indexed(address), value: indexed(num256), data: bytes <= 4096})
|
||||||
|
Executed: __log__({executionId: indexed(num256), to: indexed(address), value: indexed(num256), data: bytes <= 4096})
|
||||||
|
|
||||||
|
|
||||||
|
# Key management types
|
||||||
|
keys: address[20]
|
||||||
|
key_types: num[20]
|
||||||
|
key_count: num
|
||||||
|
execute_list: {
|
||||||
|
to: address,
|
||||||
|
data: bytes <= 8128,
|
||||||
|
value: num,
|
||||||
|
approved: bool
|
||||||
|
}[num]
|
||||||
|
execute_count: num
|
||||||
|
|
||||||
|
|
||||||
|
def __init__():
|
||||||
|
self.keys[0] = msg.sender
|
||||||
|
self.key_types[0] = 1
|
||||||
|
self.key_count = 1
|
||||||
|
log.KeyAdded(msg.sender, as_num256(1))
|
||||||
|
|
||||||
|
|
||||||
|
@constant
|
||||||
|
def getKeyType(_key: address) -> num256:
|
||||||
|
|
||||||
|
for i in range(0, 20):
|
||||||
|
|
||||||
|
if i >= self.key_count:
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.keys[i] == _key:
|
||||||
|
return as_num256(self.key_types[i])
|
||||||
|
|
||||||
|
return as_num256(0)
|
||||||
|
|
||||||
|
|
||||||
|
def getKeysByType(_type: num(num256)) -> address[20]:
|
||||||
|
assert _type >= 1
|
||||||
|
assert _type <= 4
|
||||||
|
|
||||||
|
outarr: address[20]
|
||||||
|
|
||||||
|
j = 0
|
||||||
|
for i in range(0, 20):
|
||||||
|
|
||||||
|
if i >= self.key_count:
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.key_types[i] == _type:
|
||||||
|
outarr[j] = self.keys[i]
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
return outarr
|
||||||
|
|
||||||
|
# Permission check function.
|
||||||
|
# Any modifying of keys should be handled by key holder of type 1 only.
|
||||||
|
@internal
|
||||||
|
def from_key_type(_from: address, key_type: num) -> bool:
|
||||||
|
|
||||||
|
for i in range(0, 20):
|
||||||
|
|
||||||
|
if self.keys[i] == _from and self.key_types[i] == key_type:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def addKey(_key: address, _type: num(num256)) -> bool:
|
||||||
|
assert _type >= 1
|
||||||
|
assert _type <= 4
|
||||||
|
assert self.key_count <= 20
|
||||||
|
|
||||||
|
if not self.from_key_type(msg.sender, 1):
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.keys[self.key_count] = _key
|
||||||
|
self.key_types[self.key_count] = 1
|
||||||
|
self.key_count += 1
|
||||||
|
log.KeyAdded(_key, as_num256(_type))
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def removeKey(_key: address) -> bool:
|
||||||
|
|
||||||
|
if not self.from_key_type(msg.sender, 1):
|
||||||
|
return False
|
||||||
|
|
||||||
|
for i in range(0, 20):
|
||||||
|
|
||||||
|
if i >= self.key_count:
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.keys[i] == _key:
|
||||||
|
self.keys[i] = 0x0000000000000000000000000000000000000000
|
||||||
|
log.KeyRemoved(_key, as_num256(self.key_types[i]))
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def replaceKey(_oldKey: address, _newKey: address) -> bool:
|
||||||
|
|
||||||
|
if not self.from_key_type(msg.sender, 1):
|
||||||
|
return False
|
||||||
|
|
||||||
|
for i in range(0, 20):
|
||||||
|
|
||||||
|
if i >= self.key_count:
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.keys[i] == _oldKey:
|
||||||
|
self.keys[i] = _newKey
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def execute(_to: address, _value: num(num256), _data: bytes <= 8128) -> num256:
|
||||||
|
self.execute_list[self.execute_count] = {
|
||||||
|
to: _to,
|
||||||
|
value: _value,
|
||||||
|
data: _data,
|
||||||
|
approved: False
|
||||||
|
}
|
||||||
|
|
||||||
|
log.ExecutionRequested(as_num256(self.execute_count), _to, as_num256(_value), _data)
|
||||||
|
self.execute_count += 1
|
||||||
|
return as_num256(0)
|
||||||
|
|
||||||
|
|
||||||
|
def approve(executeId: num(num256)) -> bool:
|
||||||
|
|
||||||
|
if not (self.from_key_type(msg.sender, 1) or self.from_key_type(msg.sender, 4)):
|
||||||
|
return False
|
||||||
|
|
||||||
|
e = self.execute_list[executeId]
|
||||||
|
e.approved = True
|
||||||
|
# execute action.
|
||||||
|
|
||||||
|
# log.Approved(executionId, e._to, e._value, e._data)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
# function execute(address _to, uint256 _value, bytes _data) returns (uint256 executionId)
|
||||||
|
# function approve(uint256 _id, bool _approve) returns (bool success)
|
||||||
|
|
||||||
|
# ({executionId: indexed(num256), to: indexed(address), value: indexed(num256), data: bytes <= 4096}
|
||||||
|
# {executionId: indexed(num256), to: indexed(address), value: indexed(num256), data: bytes <= 4096}
|
@ -1,8 +1,3 @@
|
|||||||
# Key management types
|
|
||||||
keys: address[20]
|
|
||||||
key_types: num[20]
|
|
||||||
key_count: num
|
|
||||||
|
|
||||||
# Claim types
|
# Claim types
|
||||||
claims: {
|
claims: {
|
||||||
claimId: bytes32,
|
claimId: bytes32,
|
||||||
@ -16,104 +11,6 @@ claims: {
|
|||||||
claim_count: num
|
claim_count: num
|
||||||
|
|
||||||
|
|
||||||
def __init__():
|
|
||||||
self.keys[0] = msg.sender
|
|
||||||
self.key_types[0] = 1
|
|
||||||
self.key_count = 1
|
|
||||||
|
|
||||||
|
|
||||||
@constant
|
|
||||||
def getKeyType(_key: address) -> num256:
|
|
||||||
|
|
||||||
for i in range(0, 20):
|
|
||||||
|
|
||||||
if i >= self.key_count:
|
|
||||||
break
|
|
||||||
|
|
||||||
if self.keys[i] == _key:
|
|
||||||
return as_num256(self.key_types[i])
|
|
||||||
|
|
||||||
return as_num256(0)
|
|
||||||
|
|
||||||
|
|
||||||
def getKeysByType(_type: num(num256)) -> address[20]:
|
|
||||||
assert _type >= 1
|
|
||||||
|
|
||||||
outarr: address[20]
|
|
||||||
|
|
||||||
j = 0
|
|
||||||
for i in range(0, 20):
|
|
||||||
|
|
||||||
if i >= self.key_count:
|
|
||||||
break
|
|
||||||
|
|
||||||
if self.key_types[i] == _type:
|
|
||||||
outarr[j] = self.keys[i]
|
|
||||||
j += 1
|
|
||||||
|
|
||||||
return outarr
|
|
||||||
|
|
||||||
# Permission check function.
|
|
||||||
# Any modifying of keys should be handled by key holder of type 1 only.
|
|
||||||
@internal
|
|
||||||
def from_key_type1(_from: address) -> bool:
|
|
||||||
|
|
||||||
for i in range(0, 20):
|
|
||||||
|
|
||||||
if self.keys[i] == _from and self.key_types[i] == 1:
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def addKey(_key: address, _type: num(num256)) -> bool:
|
|
||||||
assert _type >= 1
|
|
||||||
assert self.key_count <= 20
|
|
||||||
|
|
||||||
if not self.from_key_type1(msg.sender):
|
|
||||||
return False
|
|
||||||
|
|
||||||
self.keys[self.key_count] = _key
|
|
||||||
self.key_types[self.key_count] = 1
|
|
||||||
self.key_count += 1
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def removeKey(_key: address) -> bool:
|
|
||||||
|
|
||||||
if not self.from_key_type1(msg.sender):
|
|
||||||
return False
|
|
||||||
|
|
||||||
for i in range(0, 20):
|
|
||||||
|
|
||||||
if i >= self.key_count:
|
|
||||||
break
|
|
||||||
|
|
||||||
if self.keys[i] == _key:
|
|
||||||
self.keys[i] = 0x0000000000000000000000000000000000000000
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def replaceKey(_oldKey: address, _newKey: address) -> bool:
|
|
||||||
|
|
||||||
if not self.from_key_type1(msg.sender):
|
|
||||||
return False
|
|
||||||
|
|
||||||
for i in range(0, 20):
|
|
||||||
|
|
||||||
if i >= self.key_count:
|
|
||||||
break
|
|
||||||
|
|
||||||
if self.keys[i] == _oldKey:
|
|
||||||
self.keys[i] = _newKey
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
# The signature format is a compact form of:
|
# The signature format is a compact form of:
|
||||||
# {bytes32 r}{bytes32 s}{uint8 v}
|
# {bytes32 r}{bytes32 s}{uint8 v}
|
||||||
# Compact means, uint8 is not padded to 32 bytes.
|
# Compact means, uint8 is not padded to 32 bytes.
|
||||||
@ -176,6 +73,21 @@ def getClaim(_claimId: bytes32) -> (num256, address, num256, bytes <= 4096, byte
|
|||||||
return c.claimType, c.issuer, c.signatureType, c.signature, c.data, c.uri
|
return c.claimType, c.issuer, c.signatureType, c.signature, c.data, c.uri
|
||||||
|
|
||||||
|
|
||||||
|
def getClaimIdsByType(_claimType: num(num256)) -> bytes32[20]:
|
||||||
|
|
||||||
|
outarr: bytes32[20]
|
||||||
|
|
||||||
|
j = 0
|
||||||
|
for i in range(0, 20):
|
||||||
|
if i >= self.claim_count:
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.claims[i].claimType == _claimType:
|
||||||
|
outarr[j] = self.claims[i].claimId
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
return outarr
|
||||||
|
|
||||||
# Outstanding:
|
# Outstanding:
|
||||||
# function getClaimIdsByType(uint256 _claimType) constant returns(bytes32[] claimIds);
|
# function getClaimIdsByType(uint256 _claimType) constant returns(bytes32[] claimIds);
|
||||||
# function removeClaim(bytes32 _claimId) returns (bool success)
|
# function removeClaim(bytes32 _claimId) returns (bool success)
|
30
tests/test_erc725.py
Normal file
30
tests/test_erc725.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from ethereum import utils
|
||||||
|
from ethereum.tools import tester
|
||||||
|
|
||||||
|
from tests.setup_transaction_tests import assert_tx_failed, get_log
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def identiy_tester():
|
||||||
|
tester.s = tester.Chain()
|
||||||
|
from viper import compiler
|
||||||
|
tester.languages['viper'] = compiler.Compiler()
|
||||||
|
contract_code = open('contracts/erc_725.v.py').read()
|
||||||
|
tester.c = tester.s.contract(
|
||||||
|
contract_code,
|
||||||
|
language='viper',
|
||||||
|
args=[]
|
||||||
|
)
|
||||||
|
return tester
|
||||||
|
|
||||||
|
|
||||||
|
def sign(_hash, key):
|
||||||
|
v, r, s = utils.ecsign(_hash, key)
|
||||||
|
return utils.encode_int32(r) + utils.encode_int32(s) + utils.encode_int(v)
|
||||||
|
|
||||||
|
|
||||||
|
def test_initial_state(identiy_tester):
|
||||||
|
c = tester.c
|
||||||
|
assert c.getKeysByType(1)[0] == '0x' + tester.a0.hex()
|
@ -11,7 +11,7 @@ def identiy_tester():
|
|||||||
tester.s = tester.Chain()
|
tester.s = tester.Chain()
|
||||||
from viper import compiler
|
from viper import compiler
|
||||||
tester.languages['viper'] = compiler.Compiler()
|
tester.languages['viper'] = compiler.Compiler()
|
||||||
contract_code = open('contracts/erc725.v.py').read()
|
contract_code = open('contracts/erc_735.v.py').read()
|
||||||
tester.c = tester.s.contract(
|
tester.c = tester.s.contract(
|
||||||
contract_code,
|
contract_code,
|
||||||
language='viper',
|
language='viper',
|
||||||
@ -25,11 +25,6 @@ def sign(_hash, key):
|
|||||||
return utils.encode_int32(r) + utils.encode_int32(s) + utils.encode_int(v)
|
return utils.encode_int32(r) + utils.encode_int32(s) + utils.encode_int(v)
|
||||||
|
|
||||||
|
|
||||||
def test_initial_state(identiy_tester):
|
|
||||||
c = tester.c
|
|
||||||
assert c.getKeysByType(1)[0] == '0x' + tester.a0.hex()
|
|
||||||
|
|
||||||
|
|
||||||
def test_addClaim(identiy_tester):
|
def test_addClaim(identiy_tester):
|
||||||
c = tester.c
|
c = tester.c
|
||||||
issuer, k1 = tester.a1, tester.k1
|
issuer, k1 = tester.a1, tester.k1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user