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

add improvements for privacy enhancement support #38

Merged
merged 7 commits into from
Jan 13, 2022
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: 6 additions & 2 deletions 7nodes-test/deployContractViaHttp-pe.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ const bytecodeWithInitParam = simpleContract
from: signAcct,
isPrivate: true,
privateFrom: "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=",
privateFor: ["QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc="],
privateFor: [
"QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc=",
"BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=",
],
nonce: txCount,
privacyFlag: 1,
privacyFlag: 2,
mandatoryFor: ["QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc="],
});

console.log("Contract address: ", tx.contractAddress);
Expand Down
83 changes: 83 additions & 0 deletions 7nodes-test/multi-tenant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const Web3 = require("web3");
const https = require("https");
const axios = require("axios");
const FormData = require("form-data");
const Web3HttpProvider = require("web3-providers-http");

const Web3Quorum = require("../src");

const PSI = "PS1"; // To change based on the private state identifier
const url = `https://localhost:22000?PSI=${PSI}`;
const oauthURL = "https://localhost:4445/clients";

const getAccessToken = async () => {
try {
const instance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
await instance.delete(`${oauthURL}/${PSI}`);
const args = {
client_id: PSI,
client_secret: "foofoo",
scope: `rpc://eth_* rpc://quorumExtension_* rpc://rpc_modules psi://${PSI}?self.eoa=0x0&node.eoa=0x0`,
};
await instance.post(oauthURL, {
grant_types: ["client_credentials"],
token_endpoint_auth_method: "client_secret_post",
audience: ["Node1"],
...args,
});

const body = new FormData();
body.append("grant_type", "client_credentials");
body.append("audience", "Node1");
Object.keys(args).forEach((key) => {
body.append(key, args[key]);
});

const { data } = await instance.post(
`https://localhost:4444/oauth2/token`,
body,
{
headers: {
...body.getHeaders(),
},
}
);
return data.access_token;
} catch (e) {
console.error(e.response.data);
}
return null;
};
(async () => {
try {
const accessToken = await getAccessToken();
const options = {
keepAlive: true,
headers: [{ name: "Authorization", value: `bearer ${accessToken}` }],
agent: {
https: https.Agent({
rejectUnauthorized: false,
}),
baseUrl: url,
},
};

const provider = new Web3HttpProvider(url, options);
const web3 = new Web3Quorum(
new Web3(provider),
{
privateUrl: "http://localhost:9081",
},
true
);
// Example of calling PSI specific API
const psi = await web3.eth.getPSI();
console.log(`You are connected to ${psi}`);
} catch (e) {
console.error(e);
}
})();
2 changes: 1 addition & 1 deletion docker/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

BESU_VERSION=latest
BESU_VERSION=21.10.6
QUORUM_TESSERA_VERSION=latest
QUORUM_ETHSIGNER_VERSION=latest
QUICKSTART_VERSION=$BESU_VERSION
Expand Down
112 changes: 101 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"eslint-plugin-jest": "^24.3.5",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-promise": "^4.3.1",
"form-data": "^4.0.0",
"husky": "^6.0.0",
"jest": "^26.6.3",
"jsdoc": "^3.6.7",
Expand All @@ -113,7 +114,8 @@
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"wait-for-expect": "^3.0.2",
"web3": "^1.5.2"
"web3": "^1.5.2",
"web3-providers-http": "^1.6.1"
},
"peerDependencies": {
"web3": "^1.x"
Expand Down
9 changes: 9 additions & 0 deletions src/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ function Eth(web3) {
call: "eth_getPrivateTransactionReceipt",
params: 1,
},
/**
* @function getPSI
* @return {String} the private state identifier (PSI)
*/
{
name: "getPSI",
call: "eth_getPSI",
params: 0,
},
],
});

Expand Down
22 changes: 6 additions & 16 deletions src/priv.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,7 @@ function Priv(web3) {
return `0x${serializedTx.toString("hex")}`;
};

const sendRawRequest = async (
payload,
privateFor,
privacyFlag = undefined
) => {
const privacyParams = {
privateFor,
};
if (typeof privacyFlag !== "undefined") {
privacyParams.privacyFlag = privacyFlag;
}
const sendRawRequest = async (payload, privacyParams) => {
const txHash = await web3.eth.sendRawPrivateTransaction(
payload,
privacyParams
Expand All @@ -326,11 +316,11 @@ function Priv(web3) {
);

const privateTx = web3.utils.setPrivate(serializedTx);
return sendRawRequest(
`0x${privateTx.toString("hex")}`,
options.privateFor,
options.privacyFlag
);
return sendRawRequest(`0x${privateTx.toString("hex")}`, {
privateFor: options.privateFor,
privacyFlag: options.privacyFlag,
mandatoryFor: options.mandatoryFor,
});
}
if (options.privacyGroupId && options.privateFor) {
throw Error("privacyGroupId and privateFor are mutually exclusive");
Expand Down