Skip to content

Commit

Permalink
fix(connector-besu): network update only if present in keychain
Browse files Browse the repository at this point in the history
Only attempt to set/update the networks object on the contract JSON if
it was present in the keychain, leave it alone
otherwise.

Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed Apr 28, 2021
1 parent 888f85a commit 8ac2444
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
3 changes: 2 additions & 1 deletion packages/cactus-plugin-ledger-connector-besu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"openapi-types": "7.0.1",
"prom-client": "13.1.0",
"typescript-optional": "2.0.1",
"web3": "1.2.7"
"web3": "1.2.7",
"web3-utils": "1.2.7"
},
"devDependencies": {
"@hyperledger/cactus-plugin-keychain-memory": "0.4.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Express } from "express";
import { promisify } from "util";
import { Optional } from "typescript-optional";
import Web3 from "web3";
import { AbiItem } from "web3-utils";

import { Contract, ContractSendMethod } from "web3-eth-contract";
import { TransactionReceipt } from "web3-eth";
Expand Down Expand Up @@ -548,6 +549,7 @@ export class PluginLedgerConnectorBesu
if (isWeb3SigningCredentialNone(req.web3SigningCredential)) {
throw new Error(`${fnTag} Cannot deploy contract with pre-signed TX`);
}
const { contractName } = req;
if (req.keychainId != undefined && req.contractName != undefined) {
const keychainPlugin = this.pluginRegistry.findOneByKeychainId(
req.keychainId,
Expand All @@ -569,14 +571,14 @@ export class PluginLedgerConnectorBesu
arguments: req.constructorArgs,
});

this.log.debug(`Deployment object of contract: %o`, deployment);
const encodedAbi = deployment.encodeABI();
const data = `0x${encodedAbi}`;
const abi = deployment.encodeABI();
const data = abi.startsWith("0x") ? abi : `0x${abi}`;
this.log.debug(`Deploying "${req.contractName}" with data %o`, data);

const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialPrivateKeyHex
| Web3SigningCredentialCactusKeychainRef;
const receipt = await this.transact({
const runTxResponse = await this.transact({
transactionConfig: {
data,
from: web3SigningCredential.ethAccount,
Expand All @@ -590,29 +592,38 @@ export class PluginLedgerConnectorBesu
},
web3SigningCredential,
});
if (
receipt.transactionReceipt.status &&
receipt.transactionReceipt.contractAddress != undefined &&
receipt.transactionReceipt.contractAddress != null
) {
const address = { address: receipt.transactionReceipt.contractAddress };
const contractJSON = (await keychainPlugin.get(
req.contractName,
)) as any;
this.log.info(JSON.stringify(contractJSON));
const contract = new this.web3.eth.Contract(
contractJSON.abi,
receipt.transactionReceipt.contractAddress,
);
this.contracts[req.contractName] = contract;

const network = { [networkId]: address };
contractJSON.networks = network;
const keychainHasContract = await keychainPlugin.has(contractName);
if (keychainHasContract) {
this.log.debug(`Keychain has the contract, updating networks...`);

keychainPlugin.set(req.contractName, contractJSON);
}
const { transactionReceipt: receipt } = runTxResponse;
const { status, contractAddress } = receipt;

if (status && contractAddress) {
const networkInfo = { address: contractAddress };

return receipt;
type SolcJson = {
abi: AbiItem[];
networks: unknown;
};
const contractJSON = await keychainPlugin.get<SolcJson>(contractName);

this.log.debug("Contract JSON: \n%o", JSON.stringify(contractJSON));

const contract = new this.web3.eth.Contract(
contractJSON.abi,
contractAddress,
);
this.contracts[contractName] = contract;

const network = { [networkId]: networkInfo };
contractJSON.networks = network;

keychainPlugin.set(contractName, contractJSON);
}
}
return runTxResponse;
}
throw new Error(
`${fnTag} Cannot deploy contract without keychainId and the contractName`,
Expand Down

0 comments on commit 8ac2444

Please sign in to comment.