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 support for pmt sendTransaction #25

Merged
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
58 changes: 58 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Common code.
* @module common
*/

// Perform the specified operation which requires waiting for a transaction to be minted.
// Returns a promise which waits for the operation to complete.
const waitForTransactionWithRetries = (operation, txHash, retries, delay) => {
/* eslint-disable promise/param-names */
/* eslint-disable promise/avoid-new */

const waitFor = (ms) => {
return new Promise((r) => {
return setTimeout(r, ms);
});
};

let notified = false;
const retryOperation = (operationToRetry, times) => {
return new Promise((resolve, reject) => {
return operationToRetry()
.then((result) => {
if (result == null) {
if (!notified) {
console.log("Waiting for transaction to be mined ...");
notified = true;
}
if (delay === 0) {
throw new Error(
`Timed out after ${retries} attempts waiting for transaction to be mined`
);
} else {
const waitInSeconds = (retries * delay) / 1000;
throw new Error(
`Timed out after ${waitInSeconds}s waiting for transaction to be mined`
);
}
} else {
return resolve(result);
}
})
.catch((reason) => {
if (times - 1 > 0) {
// eslint-disable-next-line promise/no-nesting
return waitFor(delay)
.then(retryOperation.bind(null, operationToRetry, times - 1))
.then(resolve)
.catch(reject);
}
return reject(reason);
});
});
};

return retryOperation(operation, retries);
};

exports.waitForTransactionWithRetries = waitForTransactionWithRetries;
78 changes: 78 additions & 0 deletions src/eth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const common = require("./common");

/**
* For more details about the {@link https://docs.goquorum.consensys.net/en/stable/Reference/APIs/PrivacyAPI Quorum Privacy APIs}
* @module eth
Expand Down Expand Up @@ -127,6 +129,82 @@ function Eth(web3) {
},
],
});

// Use the web3 provider to directly call eth_sendTransaction in the node.
// This is necessary as web3.eth.sendTransaction doesn't work with Privacy Marker Transactions.
const sendTransactionUsingProvider = async (txnObject, callback) => {
const provider = web3.eth.currentProvider;

const jsonrpcPayload = {
jsonrpc: "2.0",
id: 3,
method: "eth_sendTransaction",
params: [txnObject],
};

const callSend = (sendParam) => {
return new Promise((resolve, reject) => {
try {
provider.send(sendParam, (err, data) => {
if (err) {
reject(err);
}
resolve(data.result);

if (callback != null) {
if (data.error) {
callback(data.error);
} else {
callback(undefined, data.result);
}
}
});
} catch (error) {
reject(error);
callback(error);
}
});
};
return callSend(jsonrpcPayload);
};

// Get the transaction Receipt, waiting until the receipt is ready.
// If it's a Privacy Marker Transaction then return the receipt for the inner private transaction.
const waitForTransactionReceipt = (txHash, retries = 300, delay = 1000) => {
const operation = () => {
return web3.eth.getTransactionReceipt(txHash);
};

return common
.waitForTransactionWithRetries(operation, txHash, retries, delay)
.then((receipt) => {
if (!receipt.isPrivacyMarkerTransaction) {
return receipt;
}
return web3.eth.getPrivateTransactionReceipt(txHash);
});
};

/**
* Submit a transaction.
* This method is similar to `web3.eth.sendTransaction()`, however it adds support for Privacy Marker Transactions.
* If the transaction is a Privacy Marker, then the promise will return the receipt for the inner private transaction,
* rather than the receipt for the Privacy Marker Transaction.
* Note that this method does not currently support `PromiEvent` events that are returned by `web3.eth.sendTransaction()`.
* @function sendGoQuorumTransaction
* @param {Object} transaction The transaction object to send (see `web3.eth.sendTransaction()` for object details)
* @param {Function} [callback] (optional) Optional callback, returns an error object as first parameter and the transaction hash as second.
* @returns {Promise<T>} Resolves when the transaction receipt is available.
*/
const sendGoQuorumTransaction = async (txnObject, callback) => {
const txHash = await sendTransactionUsingProvider(txnObject, callback);
return waitForTransactionReceipt(txHash);
};

Object.assign(web3.eth, {
sendGoQuorumTransaction,
});

return web3;
}

Expand Down
73 changes: 13 additions & 60 deletions src/priv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const PrivateTransaction = require("./privateTransaction");
const { privateToAddress } = require("./util/custom-ethjs-util");
const { PrivateSubscription } = require("./privateSubscription");
const { intToHex } = require("./util");
const common = require("./common");

/**
* @module priv
Expand Down Expand Up @@ -247,60 +248,6 @@ function Priv(web3) {
],
});

const getMarkerTransaction = (txHash, retries, delay) => {
/* eslint-disable promise/param-names */
/* eslint-disable promise/avoid-new */

const waitFor = (ms) => {
return new Promise((r) => {
return setTimeout(r, ms);
});
};

let notified = false;
const retryOperation = (operation, times) => {
return new Promise((resolve, reject) => {
return operation()
.then((result) => {
if (result == null) {
if (!notified) {
console.log("Waiting for transaction to be mined ...");
notified = true;
}
if (delay === 0) {
throw new Error(
`Timed out after ${retries} attempts waiting for transaction to be mined`
);
} else {
const waitInSeconds = (retries * delay) / 1000;
throw new Error(
`Timed out after ${waitInSeconds}s waiting for transaction to be mined`
);
}
} else {
return resolve(result);
}
})
.catch((reason) => {
if (times - 1 > 0) {
// eslint-disable-next-line promise/no-nesting
return waitFor(delay)
.then(retryOperation.bind(null, operation, times - 1))
.then(resolve)
.catch(reject);
}
return reject(reason);
});
});
};

const operation = () => {
return web3.eth.getTransactionReceipt(txHash);
};

return retryOperation(operation, retries);
};

/**
* Get the private transaction Receipt with waiting until the receipt is ready.
* @function waitForTransactionReceipt
Expand All @@ -310,12 +257,18 @@ function Priv(web3) {
* @returns {Promise<T>}
*/
const waitForTransactionReceipt = (txHash, retries = 300, delay = 1000) => {
return getMarkerTransaction(txHash, retries, delay).then((receipt) => {
if (web3.isQuorum) {
return receipt;
}
return web3.priv.getTransactionReceipt(txHash);
});
const operation = () => {
return web3.eth.getTransactionReceipt(txHash);
};

return common
.waitForTransactionWithRetries(operation, txHash, retries, delay)
.then((receipt) => {
if (web3.isQuorum) {
return receipt;
}
return web3.priv.getTransactionReceipt(txHash);
});
};

const getTransactionPayload = (options) => {
Expand Down