Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit message payload #2

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ ape compile

```shell
poetry shell
ape test
# TODO: there is a bug in pytest/ape test that it does not find the tests
# unless you explicitly pass it the tests folder
ape test tests
```

## Deploying
Expand Down
14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

29 changes: 21 additions & 8 deletions src/TermsOfService.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.0;
pragma solidity ^0.8.23;

import "@openzeppelin/access/Ownable.sol";

Expand All @@ -9,8 +9,6 @@ import "@openzeppelin/access/Ownable.sol";
import "@openzeppelin/utils/cryptography/SignatureChecker.sol";




/**
* Minimal MVP interface
*/
Expand All @@ -26,6 +24,7 @@ interface ITermsOfService {
*/
contract TermsOfService is Ownable, ITermsOfService {

// OpenZeppelin lib supporting smart contract based signing as opposite to EOA signing
using SignatureChecker for address;

// Terms of service acceptances
Expand All @@ -39,19 +38,26 @@ contract TermsOfService is Ownable, ITermsOfService {
//
// Published terms of services
//
//
// Terms of service versions, starting from 1 and
// increased with one for the each iteration.
//
mapping(uint16 version => bytes32 acceptanceMessageHash) public versions;

//
// What is the hash of currently active terms of service version
//
bytes32 public latestAcceptanceMessageHash;

//
// Terms of service versions, starting from 1 and
// increased with one for the each iteration.
// What is the version numbe of current terms of service version
//
uint16 public latestTermsOfServiceVersion;

// Add a new terms of service version
event UpdateTermsOfService(uint16 version, bytes32 acceptanceMessageHash);
event UpdateTermsOfService(uint16 version, bytes32 acceptanceMessageHash, string acceptanceMessage);

// User accepted a specific terms of service version
event Signed(address signer, uint16 version, bytes32 hash, bytes metadata);

constructor() Ownable() {
Expand All @@ -71,13 +77,20 @@ contract TermsOfService is Ownable, ITermsOfService {
return hasAcceptedHash(account, hash);
}

function updateTermsOfService(uint16 version, bytes32 acceptanceMessageHash) public onlyOwner {
/**
* Update to the new terms of service version.
*
* We do not check whether acceptance message hash matches the message,
* as the message string here is just for the event log keeping.
*
*/
function updateTermsOfService(uint16 version, bytes32 acceptanceMessageHash, string calldata acceptanceMessage) public onlyOwner {
require(version == latestTermsOfServiceVersion + 1, "Versions must be updated incrementally");
require(acceptanceMessageHash != latestAcceptanceMessageHash, "Setting the same terms of service twice");
latestAcceptanceMessageHash = acceptanceMessageHash;
latestTermsOfServiceVersion = version;
versions[version] = acceptanceMessageHash;
emit UpdateTermsOfService(version, acceptanceMessageHash);
emit UpdateTermsOfService(version, acceptanceMessageHash, acceptanceMessage);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/test_sign_terms_of_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_not_signed(
):
new_version = 1
new_hash = random.randbytes(32)
tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, "", sender=deployer)

assert not tos.hasAcceptedHash(random_user, new_hash)
assert not tos.hasAcceptedVersion(random_user, 1)
Expand All @@ -56,7 +56,7 @@ def test_signed(
)
new_hash = get_signing_hash(signing_content)

tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, signing_content, sender=deployer)

assert not tos.hasAcceptedHash(random_user, new_hash)
assert not tos.hasAcceptedVersion(random_user, 1)
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_sign_twice(
)
new_hash = get_signing_hash(signing_content)

tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, signing_content, sender=deployer)

signature = random_user.sign_message(signing_content).encode_rsv()

Expand All @@ -120,7 +120,7 @@ def test_sign_no_version(
)
new_hash = get_signing_hash(signing_content)

tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, signing_content, sender=deployer)

signature = random_user.sign_message(signing_content).encode_rsv()

Expand All @@ -144,7 +144,7 @@ def test_sign_wrong_signature(
)
new_hash = get_signing_hash(signing_content)

tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, signing_content, sender=deployer)

with reverts("Signature is not valid"):
tos.signTermsOfServiceOwn(new_hash, random.randbytes(32), b"", sender=random_user)
14 changes: 7 additions & 7 deletions tests/test_update_terms_of_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_start_zero_version(tos: ContractInstance):
def test_first_terms_of_service(tos: ContractInstance, deployer: TestAccount):
new_version = 1
new_hash = random.randbytes(32)
tx = tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tx = tos.updateTermsOfService(new_version, new_hash, "", sender=deployer)
assert tos.latestTermsOfServiceVersion() == 1
assert tos.latestAcceptanceMessageHash() == new_hash

Expand All @@ -48,13 +48,13 @@ def test_first_terms_of_service(tos: ContractInstance, deployer: TestAccount):
def test_second_terms_of_service(tos: ContractInstance, deployer: TestAccount):
new_version = 1
new_hash = random.randbytes(32)
tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, "", sender=deployer)
assert tos.latestTermsOfServiceVersion() == 1
assert tos.latestAcceptanceMessageHash() == new_hash

new_version = 2
new_hash = random.randbytes(32)
tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, "", sender=deployer)
assert tos.latestTermsOfServiceVersion() == 2
assert tos.latestAcceptanceMessageHash() == new_hash

Expand All @@ -64,7 +64,7 @@ def test_wrong_owner(tos: ContractInstance, random_user: TestAccount):
new_hash = random.randbytes(32)

with pytest.raises(ContractLogicError):
tos.updateTermsOfService(new_version, new_hash, sender=random_user)
tos.updateTermsOfService(new_version, new_hash, "", sender=random_user)

assert tos.latestTermsOfServiceVersion() == 0

Expand All @@ -74,19 +74,19 @@ def test_wrong_version(tos: ContractInstance, deployer: TestAccount):
new_hash = random.randbytes(32)

with pytest.raises(ContractLogicError):
tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, "", sender=deployer)

assert tos.latestTermsOfServiceVersion() == 0


def test_hash_reuse(tos: ContractInstance, deployer: TestAccount):
new_version = 1
new_hash = random.randbytes(32)
tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, "", sender=deployer)

new_version = 2
with pytest.raises(ContractLogicError):
tos.updateTermsOfService(new_version, new_hash, sender=deployer)
tos.updateTermsOfService(new_version, new_hash, "", sender=deployer)


def test_transfer_ownership(tos: ContractInstance, deployer: TestAccount, random_user: TestAccount):
Expand Down