Skip to content

Commit

Permalink
build(deps): upgrade to TS 4.4 test-plugin-ledger-connector-besu
Browse files Browse the repository at this point in the history
WORK IN PROGRESS

Fixes hyperledger#1747

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Oct 18, 2023
1 parent bff6978 commit 43450fc
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 84 deletions.
2 changes: 2 additions & 0 deletions packages/cactus-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@
"express": "4.17.3",
"express-jwt-authz": "2.4.1",
"express-openapi-validator": "5.0.4",
"http-errors": "2.0.0",
"safe-stable-stringify": "2.4.3",
"typescript-optional": "2.0.1"
},
"devDependencies": {
"@types/express": "4.17.13",
"@types/http-errors": "2.0.2",
"uuid": "8.3.2"
},
"engines": {
Expand Down
5 changes: 5 additions & 0 deletions packages/cactus-core/src/main/typescript/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ export {
GetOpenApiSpecV1EndpointBase,
IGetOpenApiSpecV1EndpointBaseOptions,
} from "./web-services/get-open-api-spec-v1-endpoint-base";

export {
IHandleRestEndpointExceptionOptions,
handleRestEndpointException,
} from "./web-services/handle-rest-endpoint-exception";
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
Logger,
createRuntimeErrorWithCause,
} from "@hyperledger/cactus-common";
import type { Response } from "express";
import createHttpError from "http-errors";

export interface IHandleRestEndpointExceptionOptions {
readonly errorMsg: string;
readonly log: Logger;
readonly error: unknown;
readonly res: Response;
}

export function handleRestEndpointException(
ctx: Readonly<IHandleRestEndpointExceptionOptions>,
): void {
if (createHttpError.isHttpError(ctx.error)) {
ctx.log.debug(ctx.errorMsg, ctx.error);
ctx.res.status(ctx.error.statusCode);
if (ctx.error.expose) {
ctx.res.json({
message: ctx.error.message,
error: ctx.error,
});
}
} else {
ctx.log.error(ctx.errorMsg, ctx.error);
const rex = createRuntimeErrorWithCause(ctx.errorMsg, ctx.error);
ctx.res.status(500).json({
message: "Internal Server Error",
error: rex,
});
}
}
2 changes: 2 additions & 0 deletions packages/cactus-plugin-ledger-connector-besu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@hyperledger/cactus-core-api": "2.0.0-alpha.2",
"axios": "0.21.4",
"express": "4.17.3",
"http-errors": "2.0.0",
"joi": "17.9.1",
"openapi-types": "9.1.0",
"prom-client": "13.2.0",
Expand All @@ -76,6 +77,7 @@
"@hyperledger/cactus-plugin-keychain-memory": "2.0.0-alpha.2",
"@hyperledger/cactus-test-tooling": "2.0.0-alpha.2",
"@types/express": "4.17.13",
"@types/http-errors": "2.0.2",
"key-encoder": "2.0.3",
"socket.io": "4.5.4",
"web3-core": "1.6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Server as SocketIoServer } from "socket.io";
import type { Socket as SocketIoSocket } from "socket.io";
import type { Express } from "express";
import { Optional } from "typescript-optional";
import createHttpError from "http-errors";

import OAS from "../json/openapi.json";

Expand Down Expand Up @@ -814,93 +815,101 @@ export class PluginLedgerConnectorBesu
const fnTag = `${this.className}#deployContract()`;
Checks.truthy(req, `${fnTag} req`);
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,
);
Checks.truthy(
keychainPlugin,
`${fnTag} keychain for ID:"${req.keychainId}"`,
throw createHttpError[400](
`${fnTag} Cannot deploy contract with pre-signed TX`,
);
if (!keychainPlugin.has(req.contractName)) {
throw new Error(
`${fnTag} Cannot create an instance of the contract because the contractName and the contractName on the keychain does not match`,
);
}
const networkId = await this.web3.eth.net.getId();
}
const { keychainId, contractName } = req;
if (!keychainId || !req.contractName) {
const errorMessage = `${fnTag} Cannot deploy contract without keychainId and the contractName.`;
throw createHttpError[400](errorMessage);
}

const tmpContract = new this.web3.eth.Contract(req.contractAbi);
const deployment = tmpContract.deploy({
data: req.bytecode,
arguments: req.constructorArgs,
});
const keychainPlugin = this.pluginRegistry.findOneByKeychainId(keychainId);

const abi = deployment.encodeABI();
const data = abi.startsWith("0x") ? abi : `0x${abi}`;
this.log.debug(`Deploying "${req.contractName}" with data %o`, data);
if (!keychainPlugin) {
const errorMessage =
`${fnTag} The plugin registry does not contain` +
` a keychain plugin for ID:"${req.keychainId}"`;
throw createHttpError[400](errorMessage);
}

const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialPrivateKeyHex
| Web3SigningCredentialCactusKeychainRef;
if (!keychainPlugin.has(contractName)) {
const errorMessage =
`${fnTag} Cannot create an instance of the contract instance because` +
`the contractName in the request does not exist on the keychain`;
throw new createHttpError[400](errorMessage);
}

const runTxResponse = await this.transact({
transactionConfig: {
data,
from: web3SigningCredential.ethAccount,
gas: req.gas,
gasPrice: req.gasPrice,
},
consistencyStrategy: {
blockConfirmations: 0,
receiptType: ReceiptType.NodeTxPoolAck,
timeoutMs: req.timeoutMs || 60000,
},
web3SigningCredential,
privateTransactionConfig: req.privateTransactionConfig,
});
const networkId = await this.web3.eth.net.getId();

const keychainHasContract = await keychainPlugin.has(contractName);
if (keychainHasContract) {
this.log.debug(`Keychain has the contract, updating networks...`);

const { transactionReceipt: receipt } = runTxResponse;
const { status, contractAddress } = receipt;

if (status && contractAddress) {
const networkInfo = { address: contractAddress };
const contractStr = await keychainPlugin.get(contractName);
const contractJSON = JSON.parse(contractStr);
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 tmpContract = new this.web3.eth.Contract(req.contractAbi);
const deployment = tmpContract.deploy({
data: req.bytecode,
arguments: req.constructorArgs,
});

const network = { [networkId]: networkInfo };
contractJSON.networks = network;
const abi = deployment.encodeABI();
const data = abi.startsWith("0x") ? abi : `0x${abi}`;
this.log.debug(`Deploying "${req.contractName}" with data %o`, data);

await keychainPlugin.set(contractName, JSON.stringify(contractJSON));
}
} else {
throw new Error(
`${fnTag} Cannot create an instance of the contract because the contractName and the contractName on the keychain does not match`,
);
}
const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialPrivateKeyHex
| Web3SigningCredentialCactusKeychainRef;

// creating solidity byte code response
const deployResponse: DeployContractSolidityBytecodeV1Response = {
transactionReceipt: runTxResponse.transactionReceipt,
};
const runTxResponse = await this.transact({
transactionConfig: {
data,
from: web3SigningCredential.ethAccount,
gas: req.gas,
gasPrice: req.gasPrice,
},
consistencyStrategy: {
blockConfirmations: 0,
receiptType: ReceiptType.NodeTxPoolAck,
timeoutMs: req.timeoutMs || 60000,
},
web3SigningCredential,
privateTransactionConfig: req.privateTransactionConfig,
});

const keychainHasContract = await keychainPlugin.has(contractName);

return deployResponse;
if (!keychainHasContract) {
const errorMessage =
`${fnTag} Cannot create an instance of the contract instance because` +
`the contractName in the request does not exist on the keychain`;
throw new createHttpError[400](errorMessage);
}
throw new Error(
`${fnTag} Cannot deploy contract without keychainId and the contractName`,
);

this.log.debug(`Keychain has the contract, updating networks...`);

const { transactionReceipt: receipt } = runTxResponse;
const { status, contractAddress } = receipt;

if (status && contractAddress) {
const networkInfo = { address: contractAddress };
const contractStr = await keychainPlugin.get(contractName);
const contractJSON = JSON.parse(contractStr);
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;

await keychainPlugin.set(contractName, JSON.stringify(contractJSON));
}

// creating solidity byte code response
const deployResponse: DeployContractSolidityBytecodeV1Response = {
transactionReceipt: runTxResponse.transactionReceipt,
};

return deployResponse;
}

public async signTransaction(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Express, Request, Response } from "express";
import type { Express, Request, Response } from "express";

import {
IWebServiceEndpoint,
Expand All @@ -14,7 +14,10 @@ import {
IAsyncProvider,
} from "@hyperledger/cactus-common";

import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import {
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";

import { PluginLedgerConnectorBesu } from "../plugin-ledger-connector-besu";
import { DeployContractSolidityBytecodeV1Request } from "../generated/openapi/typescript-axios";
Expand Down Expand Up @@ -86,18 +89,16 @@ export class DeployContractSolidityBytecodeEndpoint
}

public async handleRequest(req: Request, res: Response): Promise<void> {
const fnTag = `${this.className}#handleRequest()`;
const reqTag = `${this.getVerbLowerCase()} - ${this.getPath()}`;
this.log.debug(reqTag);
const reqBody: DeployContractSolidityBytecodeV1Request = req.body;
try {
const resBody = await this.options.connector.deployContract(reqBody);
res.json(resBody);
} catch (ex) {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex?.stack || ex?.message,
});
const errorMsg = `${reqTag} ${fnTag} Failed to deploy contract:`;
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
5 changes: 4 additions & 1 deletion packages/cactus-plugin-ledger-connector-besu/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"declarationDir": "dist/lib",
"resolveJsonModule": true,
"rootDir": "./src",
"tsBuildInfoFile": "../../.build-cache/cactus-plugin-ledger-connector-besu.tsbuildinfo"
"tsBuildInfoFile": "../../.build-cache/cactus-plugin-ledger-connector-besu.tsbuildinfo",
"alwaysStrict": true,
"strict": true,
"useUnknownInCatchVariables": false
},
"include": [
"./src",
Expand Down
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6987,9 +6987,11 @@ __metadata:
"@hyperledger/cactus-common": 2.0.0-alpha.2
"@hyperledger/cactus-core-api": 2.0.0-alpha.2
"@types/express": 4.17.13
"@types/http-errors": 2.0.2
express: 4.17.3
express-jwt-authz: 2.4.1
express-openapi-validator: 5.0.4
http-errors: 2.0.0
safe-stable-stringify: 2.4.3
typescript-optional: 2.0.1
uuid: 8.3.2
Expand Down Expand Up @@ -7603,8 +7605,10 @@ __metadata:
"@hyperledger/cactus-plugin-keychain-memory": 2.0.0-alpha.2
"@hyperledger/cactus-test-tooling": 2.0.0-alpha.2
"@types/express": 4.17.13
"@types/http-errors": 2.0.2
axios: 0.21.4
express: 4.17.3
http-errors: 2.0.0
joi: 17.9.1
key-encoder: 2.0.3
openapi-types: 9.1.0
Expand Down Expand Up @@ -12755,6 +12759,13 @@ __metadata:
languageName: node
linkType: hard

"@types/http-errors@npm:2.0.2":
version: 2.0.2
resolution: "@types/http-errors@npm:2.0.2"
checksum: d7f14045240ac4b563725130942b8e5c8080bfabc724c8ff3f166ea928ff7ae02c5194763bc8f6aaf21897e8a44049b0492493b9de3e058247e58fdfe0f86692
languageName: node
linkType: hard

"@types/http-proxy@npm:^1.17.8":
version: 1.17.9
resolution: "@types/http-proxy@npm:1.17.9"
Expand Down

0 comments on commit 43450fc

Please sign in to comment.