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

log hooks #46

Merged
merged 1 commit into from
Oct 18, 2018
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
8 changes: 4 additions & 4 deletions test/mockgateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ async function handleRequest (req) {
}
} else if (req.method == 'eth_getCode') {
obj.result = artifact.bytecode;
} else if (req.method == 'eth_getPastLogs') {
if (req.params[0] === responses.CONFIDENTIAL_GET_PAST_LOGS.address) {
obj.result = responses.CONFIDENTIAL_GET_PAST_LOGS;
}
} else if (req.method == 'eth_getLogs') {
if (req.params[0].address === responses.CONFIDENTIAL_GET_PAST_LOGS[0].address) {
obj.result = responses.CONFIDENTIAL_GET_PAST_LOGS;
}
}

return obj;
Expand Down
13 changes: 7 additions & 6 deletions test/web3c.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ describe('Web3', () => {
}).timeout(5000);

it ('should get confidential getPastLogs logs', async() => {
let counterContract = (new web3c(gw)).confidential.Contract(artifact.abi);
let client = new web3c(gw);
let counterContract = client.confidential.Contract(artifact.abi);
let instance;
try {
instance = await counterContract.deploy({
Expand All @@ -95,10 +96,10 @@ describe('Web3', () => {
gasPrice: '0x3b9aca00',
gasLimit: '0x100000',
});
// todo: this should use our confidential getPastLogs implementation
let logs = await (new web3c(gw)).eth.getPastLogs({
address: instance._address
});
// todo: assert the logs emitted the number 1

let logs = await instance.getPastEvents();
assert.equal(logs.length, 1);
// since the client will use a different ephemeral key each time, it
// won't be able to decrypt previous logs.
}).timeout(5000);
});
13 changes: 13 additions & 0 deletions web3c/confidential.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,24 @@ const Confidential = function (web3) {
let c = new web3.eth.Contract(abi, address, options);
c.setProvider(confidentialShim);

let keymanager = this.keyManager;
let boundEvent = c._decodeEventABI;
c._decodeEventABI = function (data) {
if (data.logIndex == 0 && data.topics &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to put these into constants and a method just so its obvious to readers whats going on here.

Suggested change
if (data.logIndex == 0 && data.topics &&
if (is_confidential_longterm_public_key(data))

And put the h256 and log index into a constant, e.g.,

function is_confidential_longterm_public_key(data) {
  return data.logIndex == LONGTERM_PUBLIC_KEY_INDEX && data.topics &&  LONGTERM_PUBLIC_KEY_TOPIC == data.topics[0];
}

data.topics[0] == '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') {
keymanager.add(data.address, data.data);
} else {
// decoding happens at requet manager.
}
return boundEvent.call(c, data);
};

// hook to ensure deployed contracts retain the confidential provider.
let boundClone = c.clone.bind(c);
c.clone = () => {
let cloned = boundClone();
cloned.setProvider(confidentialShim);
cloned._decodeEventABI = c._decodeEventABI;
return cloned;
};

Expand Down
37 changes: 36 additions & 1 deletion web3c/confidentialprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ ConfidentialProvider.send = function confidentialSend (payload, callback) {
transform.ethSendTransaction(payload, callback);
} else if (payload.method == 'eth_call') {
transform.ethCall(payload, callback);
} else if (payload.method == 'eth_getLogs') {
transform.ethLogs(payload, callback);
} else if (payload.method == 'eth_getTransactionReceipt') {
transform.ethTransactionReciept(payload, callback);
} else {
const provider = this.manager.provider;
return provider[provider.sendAsync ? 'sendAsync' : 'send'](payload, callback);
Expand Down Expand Up @@ -76,7 +80,38 @@ class ConfidentialSendTransform {
});
}

// TODO: get call data signed by the user wallet
async tryDecryptLogs(logs) {
for (let i = 0; i < logs.length; i++) {
try {
let plain = await this.keymanager.decrypt(logs[i].data);
logs[i].data = plain;
} catch (e) {
// not a log for us.
}
}
return logs;
}

ethLogs(payload, callback) {
return this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](payload, async (err, res) => {
if (!err) {
res.result = await this.tryDecryptLogs(res.result);
}
callback(err, res);
});
}

ethTransactionReciept(payload, callback) {
return this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](payload, async (err, res) => {
if (!err && res.result && res.result.logs && res.result.logs.lenght) {
res.result.logs = await this.tryDecryptLogs(res.result.logs);
}
callback(err, res);
});
}

//TODO: eth_getFilterChanges, eth_getFilterLogs

ethCall(payload, callback) {
const tx = payload.params[0];
this.encryptTx(tx, (err) => {
Expand Down