Commit 97502083 authored by Mark Harding's avatar Mark Harding

(feat): working trust contract

parent 31d733e7
No related merge requests found
pragma solidity >=0.4.21 <0.6.0;
import '../node_modules/ethr-did-registry/contracts/EthereumDIDRegistry.sol';
contract Trust {
EthereumDIDRegistry didRegistry;
struct TrustScore {
address from;
address to;
int8 score;
}
mapping(address => mapping(address => TrustScore)) public scores;
constructor(address didRegistryAddress) public {
didRegistry = EthereumDIDRegistry(didRegistryAddress);
}
// Test function REMOVE THIS
function sayHello() public view returns (string) {
return "hello";
}
function issueTrustViaDelegate(address from, address to, int8 score) public returns (bool) {
bool validated = didRegistry.validDelegate(from, 'DID-JWT', msg.sender);
require(validated);
_issueTrust(from, to, score);
}
function issueTrust(address to, int8 score) {
require(didRegistry.identityOwner(msg.sender) == msg.sender);
_issueTrust(msg.sender, to, score);
}
/**
* Issue trust (internal)
* @param from address
* @param to address
* @param score int8
* @return bool
*/
function _issueTrust(address from, address to, int8 score) internal returns (bool) {
TrustScore memory _trustScore = TrustScore(
from,
to,
score
);
scores[from][to] = _trustScore;
return true;
}
function getTrust(address from, address to) public view returns (int8) {
return scores[from][to].score;
}
}
\ No newline at end of file
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"ethr-did-registry": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/ethr-did-registry/-/ethr-did-registry-0.0.3.tgz",
"integrity": "sha512-4BPvMGkxAK9vTduCq6D5b8ZqjteD2cvDIPPriXP6nnmPhWKFSxypo+AFvyQ0omJGa0cGTR+dkdI/8jiF7U/qaw=="
}
}
}
const Trust = artifacts.require("Trust");
const EthereumDIDRegistry = artifacts.require("EthereumDIDRegistry");
contract("Trust", accounts => {
let trust;
let didRegistry;
beforeEach(async () => {
didRegistry = await EthereumDIDRegistry.new();
trust = await Trust.new(didRegistry.address);
});
it("should say hello", async () => {
const say = await trust.sayHello.call();
assert.equal(say, "hello");
});
it("should issue trust score", async () => {
await trust.issueTrust(accounts[2], 1);
assert.equal(await trust.getTrust(accounts[0], accounts[2]), 1);
});
it("should issue a negative trust score", async () => {
await trust.issueTrust(accounts[3], -1);
assert.equal(await trust.getTrust(accounts[0], accounts[3]), -1);
});
it("should issue multiple trust scores", async () => {
await trust.issueTrust(accounts[2], 1);
await trust.issueTrust(accounts[3], 1);
await trust.issueTrust(accounts[4], -1);
await trust.issueTrust(accounts[5], -1);
assert.equal(await trust.getTrust(accounts[0], accounts[2]), 1);
assert.equal(await trust.getTrust(accounts[0], accounts[3]), 1);
assert.equal(await trust.getTrust(accounts[0], accounts[4]), -1);
assert.equal(await trust.getTrust(accounts[0], accounts[5]), -1);
});
it("should issue trust score via a delegate", async () => {
// accounts[1] is the identity
// accounts[1] is also the owner
// accounts[2] will become the delegate
await didRegistry.addDelegate(
accounts[1],
web3.utils.fromAscii("DID-JWT"),
accounts[2],
86400 * 365,
{
from: accounts[1]
}
);
await trust.issueTrustViaDelegate(accounts[1], accounts[3], 1, {
from: accounts[2]
});
assert.equal(await trust.getTrust(accounts[1], accounts[3]), 1);
});
});
......@@ -47,33 +47,30 @@ module.exports = {
// port: 8545, // Standard Ethereum port (default: none)
// network_id: "*", // Any network (default: none)
// },
// Another network with more advanced options...
// advanced: {
// port: 8777, // Custom port
// network_id: 1342, // Custom network
// gas: 8500000, // Gas sent with each transaction (default: ~6700000)
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
// from: <address>, // Account to send txs from (default: accounts[0])
// websockets: true // Enable EventEmitter interface for web3 (default: false)
// port: 8777, // Custom port
// network_id: 1342, // Custom network
// gas: 8500000, // Gas sent with each transaction (default: ~6700000)
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
// from: <address>, // Account to send txs from (default: accounts[0])
// websockets: true // Enable EventEmitter interface for web3 (default: false)
// },
// Useful for deploying to a public network.
// NB: It's important to wrap the provider as a function.
// ropsten: {
// provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`),
// network_id: 3, // Ropsten's id
// gas: 5500000, // Ropsten has a lower block limit than mainnet
// confirmations: 2, // # of confs to wait between deployments. (default: 0)
// timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
// skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
// provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`),
// network_id: 3, // Ropsten's id
// gas: 5500000, // Ropsten has a lower block limit than mainnet
// confirmations: 2, // # of confs to wait between deployments. (default: 0)
// timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
// skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
// },
// Useful for private networks
// private: {
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
// network_id: 2111, // This network is yours, in the cloud.
// production: true // Treats this network as if it was a public net. (default: false)
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
// network_id: 2111, // This network is yours, in the cloud.
// production: true // Treats this network as if it was a public net. (default: false)
// }
},
......@@ -85,7 +82,7 @@ module.exports = {
// Configure your compilers
compilers: {
solc: {
// version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version)
version: "0.4.24" // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
// settings: { // See the solidity docs for advice about optimization and evmVersion
// optimizer: {
......@@ -96,4 +93,4 @@ module.exports = {
// }
}
}
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment