-
Notifications
You must be signed in to change notification settings - Fork 272
/
plugin-ledger-connector-quorum.ts
600 lines (534 loc) · 19.7 KB
/
plugin-ledger-connector-quorum.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import { Server } from "http";
import { Server as SecureServer } from "https";
import { Express } from "express";
import { promisify } from "util";
import { Optional } from "typescript-optional";
import Web3 from "web3";
// The strange way of obtaining the contract class here is like this because
// web3-eth internally sub-classes the Contract class at runtime
// @see https://stackoverflow.com/a/63639280/698470
const Contract = new Web3().eth.Contract;
import { ContractSendMethod } from "web3-eth-contract";
import { TransactionReceipt } from "web3-eth";
import {
ConsensusAlgorithmFamily,
IPluginLedgerConnector,
IWebServiceEndpoint,
IPluginWebService,
PluginAspect,
ICactusPlugin,
ICactusPluginOptions,
IPluginKeychain,
} from "@hyperledger/cactus-core-api";
import { PluginRegistry } from "@hyperledger/cactus-core";
import {
Checks,
Logger,
LoggerProvider,
LogLevelDesc,
} from "@hyperledger/cactus-common";
import { DeployContractSolidityBytecodeEndpoint } from "./web-services/deploy-contract-solidity-bytecode-endpoint";
import {
DeployContractSolidityBytecodeV1Request,
DeployContractSolidityBytecodeV1Response,
EthContractInvocationType,
InvokeContractV1Request,
InvokeContractV1Response,
InvokeContractV2Request,
InvokeContractV2Response,
RunTransactionRequest,
RunTransactionResponse,
Web3SigningCredentialGethKeychainPassword,
Web3SigningCredentialCactusKeychainRef,
Web3SigningCredentialPrivateKeyHex,
Web3SigningCredentialType,
} from "./generated/openapi/typescript-axios/";
import { RunTransactionEndpoint } from "./web-services/run-transaction-endpoint";
import { InvokeContractEndpoint } from "./web-services/invoke-contract-endpoint";
import { isWeb3SigningCredentialNone } from "./model-type-guards";
import { InvokeContractEndpointV2 } from "./web-services/invoke-contract-endpoint-v2";
import { PrometheusExporter } from "./prometheus-exporter/prometheus-exporter";
import {
GetPrometheusExporterMetricsEndpointV1,
IGetPrometheusExporterMetricsEndpointV1Options,
} from "./web-services/get-prometheus-exporter-metrics-endpoint-v1";
export interface IPluginLedgerConnectorQuorumOptions
extends ICactusPluginOptions {
rpcApiHttpHost: string;
logLevel?: LogLevelDesc;
prometheusExporter?: PrometheusExporter;
pluginRegistry: PluginRegistry;
}
export class PluginLedgerConnectorQuorum
implements
IPluginLedgerConnector<
DeployContractSolidityBytecodeV1Request,
DeployContractSolidityBytecodeV1Response,
RunTransactionRequest,
RunTransactionResponse
>,
ICactusPlugin,
IPluginWebService {
private readonly pluginRegistry: PluginRegistry;
public prometheusExporter: PrometheusExporter;
private readonly instanceId: string;
private readonly log: Logger;
private readonly web3: Web3;
private httpServer: Server | SecureServer | null = null;
private contracts: {
// @see https://stackoverflow.com/a/63639280/698470
[name: string]: InstanceType<typeof Contract>;
} = {};
private endpoints: IWebServiceEndpoint[] | undefined;
public static readonly CLASS_NAME = "PluginLedgerConnectorQuorum";
public get className(): string {
return PluginLedgerConnectorQuorum.CLASS_NAME;
}
constructor(public readonly options: IPluginLedgerConnectorQuorumOptions) {
const fnTag = `${this.className}#constructor()`;
Checks.truthy(options, `${fnTag} arg options`);
Checks.truthy(options.rpcApiHttpHost, `${fnTag} options.rpcApiHttpHost`);
Checks.truthy(options.instanceId, `${fnTag} options.instanceId`);
Checks.truthy(options.pluginRegistry, `${fnTag} options.pluginRegistry`);
const level = this.options.logLevel || "INFO";
const label = this.className;
this.log = LoggerProvider.getOrCreate({ level, label });
const web3Provider = new Web3.providers.HttpProvider(
this.options.rpcApiHttpHost,
);
this.web3 = new Web3(web3Provider);
this.instanceId = options.instanceId;
this.pluginRegistry = options.pluginRegistry;
this.prometheusExporter =
options.prometheusExporter ||
new PrometheusExporter({ pollingIntervalInMin: 1 });
Checks.truthy(
this.prometheusExporter,
`${fnTag} options.prometheusExporter`,
);
this.prometheusExporter.startMetricsCollection();
}
public getPrometheusExporter(): PrometheusExporter {
return this.prometheusExporter;
}
public async getPrometheusExporterMetrics(): Promise<string> {
const res: string = await this.prometheusExporter.getPrometheusMetrics();
this.log.debug(`getPrometheusExporterMetrics() response: %o`, res);
return res;
}
public getInstanceId(): string {
return this.instanceId;
}
public getHttpServer(): Optional<Server | SecureServer> {
return Optional.ofNullable(this.httpServer);
}
public async shutdown(): Promise<void> {
const serverMaybe = this.getHttpServer();
if (serverMaybe.isPresent()) {
const server = serverMaybe.get();
await promisify(server.close.bind(server))();
}
}
async registerWebServices(app: Express): Promise<IWebServiceEndpoint[]> {
const webServices = await this.getOrCreateWebServices();
webServices.forEach((ws) => ws.registerExpress(app));
return webServices;
}
public async getOrCreateWebServices(): Promise<IWebServiceEndpoint[]> {
if (Array.isArray(this.endpoints)) {
return this.endpoints;
}
const endpoints: IWebServiceEndpoint[] = [];
{
const endpoint = new DeployContractSolidityBytecodeEndpoint({
connector: this,
logLevel: this.options.logLevel,
});
endpoints.push(endpoint);
}
{
const endpoint = new RunTransactionEndpoint({
connector: this,
logLevel: this.options.logLevel,
});
endpoints.push(endpoint);
}
{
const endpoint = new InvokeContractEndpoint({
connector: this,
logLevel: this.options.logLevel,
});
endpoints.push(endpoint);
}
{
const endpoint = new InvokeContractEndpointV2({
connector: this,
logLevel: this.options.logLevel,
});
endpoints.push(endpoint);
}
{
const opts: IGetPrometheusExporterMetricsEndpointV1Options = {
connector: this,
logLevel: this.options.logLevel,
};
const endpoint = new GetPrometheusExporterMetricsEndpointV1(opts);
endpoints.push(endpoint);
}
this.endpoints = endpoints;
return endpoints;
}
public getPackageName(): string {
return `@hyperledger/cactus-plugin-ledger-connector-quorum`;
}
public getAspect(): PluginAspect {
return PluginAspect.LEDGER_CONNECTOR;
}
public async getConsensusAlgorithmFamily(): Promise<
ConsensusAlgorithmFamily
> {
return ConsensusAlgorithmFamily.AUTHORITY;
}
public async invokeContract(
req: InvokeContractV1Request,
): Promise<InvokeContractV1Response> {
const fnTag = `${this.className}#invokeContract()`;
const { invocationType } = req;
let abi;
if (typeof req.contractAbi === "string") {
abi = JSON.parse(req.contractAbi);
} else {
abi = req.contractAbi;
}
const { contractAddress } = req;
const aContract = new this.web3.eth.Contract(abi, contractAddress);
const methodRef = aContract.methods[req.methodName];
Checks.truthy(methodRef, `${fnTag} YourContract.${req.methodName}`);
const method: ContractSendMethod = methodRef(...req.params);
if (req.invocationType === EthContractInvocationType.CALL) {
const callOutput = await (method as any).call();
return { callOutput };
} else if (req.invocationType === EthContractInvocationType.SEND) {
if (isWeb3SigningCredentialNone(req.web3SigningCredential)) {
throw new Error(`${fnTag} Cannot deploy contract with pre-signed TX`);
}
const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialGethKeychainPassword
| Web3SigningCredentialPrivateKeyHex;
const payload = (method.send as any).request();
const { params } = payload;
const [transactionConfig] = params;
if (req.gas == undefined) {
req.gas = await this.web3.eth.estimateGas(transactionConfig);
}
transactionConfig.from = web3SigningCredential.ethAccount;
transactionConfig.gas = req.gas;
transactionConfig.gasPrice = req.gasPrice;
transactionConfig.value = req.value;
transactionConfig.nonce = req.nonce;
const txReq: RunTransactionRequest = {
transactionConfig,
web3SigningCredential,
timeoutMs: req.timeoutMs || 60000,
};
const out = await this.transact(txReq);
return out;
} else {
throw new Error(`${fnTag} Unsupported invocation type ${invocationType}`);
}
}
public async invokeContractV2(
req: InvokeContractV2Request,
): Promise<InvokeContractV2Response> {
const fnTag = `${this.className}#invokeContractV2()`;
const contractName = req.contractName;
if (req.keychainId != undefined) {
const networkId = await this.web3.eth.net.getId();
const keychainPlugin = this.pluginRegistry.findOneByKeychainId(
req.keychainId,
);
Checks.truthy(
keychainPlugin,
`${fnTag} keychain for ID:"${req.keychainId}"`,
);
if (!keychainPlugin.has(contractName)) {
throw new Error(
`${fnTag} Cannot create an instance of the contract because the contractName and the contractName of the JSON doesn't match`,
);
}
const contractJSON = (await keychainPlugin.get(contractName)) as any;
if (
contractJSON.networks === undefined ||
contractJSON.networks[networkId] === undefined ||
contractJSON.networks[networkId].address === undefined
) {
if (isWeb3SigningCredentialNone(req.signingCredential)) {
throw new Error(`${fnTag} Cannot deploy contract with pre-signed TX`);
}
const web3SigningCredential = req.signingCredential as
| Web3SigningCredentialPrivateKeyHex
| Web3SigningCredentialCactusKeychainRef;
const receipt = await this.transact({
transactionConfig: {
data: `0x${contractJSON.bytecode}`,
from: web3SigningCredential.ethAccount,
gas: req.gas,
gasPrice: req.gasPrice,
},
web3SigningCredential,
});
const address = {
address: receipt.transactionReceipt.contractAddress,
};
const network = { [networkId]: address };
contractJSON.networks = network;
keychainPlugin.set(contractName, contractJSON);
}
const contract = new this.web3.eth.Contract(
contractJSON.abi,
contractJSON.networks[networkId].address,
);
this.contracts[contractName] = contract;
} else {
throw new Error(
`${fnTag} Cannot invoke a contract without contract instance, the keychainId param is needed`,
);
}
const contractInstance = this.contracts[contractName];
const methodRef = contractInstance.methods[req.methodName];
Checks.truthy(methodRef, `${fnTag} YourContract.${req.methodName}`);
const method: ContractSendMethod = methodRef(...req.params);
if (req.invocationType === EthContractInvocationType.CALL) {
contractInstance.methods[req.methodName];
const callOutput = await (method as any).call();
const success = true;
return { success, callOutput };
} else if (req.invocationType === EthContractInvocationType.SEND) {
if (isWeb3SigningCredentialNone(req.signingCredential)) {
throw new Error(`${fnTag} Cannot deploy contract with pre-signed TX`);
}
const web3SigningCredential = req.signingCredential as
| Web3SigningCredentialPrivateKeyHex
| Web3SigningCredentialCactusKeychainRef;
const payload = (method.send as any).request();
const { params } = payload;
const [transactionConfig] = params;
if (req.gas == undefined) {
req.gas = await this.web3.eth.estimateGas(transactionConfig);
}
transactionConfig.from = web3SigningCredential.ethAccount;
transactionConfig.gas = req.gas;
transactionConfig.gasPrice = req.gasPrice;
transactionConfig.value = req.value;
transactionConfig.nonce = req.nonce;
const txReq: RunTransactionRequest = {
transactionConfig,
web3SigningCredential,
timeoutMs: req.timeoutMs || 60000,
};
const out = await this.transact(txReq);
const success = out.transactionReceipt.status;
const data = { success, out };
return data;
} else {
throw new Error(
`${fnTag} Unsupported invocation type ${req.invocationType}`,
);
}
}
public async transact(
req: RunTransactionRequest,
): Promise<RunTransactionResponse> {
const fnTag = `${this.className}#transact()`;
switch (req.web3SigningCredential.type) {
case Web3SigningCredentialType.CACTUSKEYCHAINREF: {
return this.transactCactusKeychainRef(req);
}
case Web3SigningCredentialType.GETHKEYCHAINPASSWORD: {
return this.transactGethKeychain(req);
}
case Web3SigningCredentialType.PRIVATEKEYHEX: {
return this.transactPrivateKey(req);
}
case Web3SigningCredentialType.NONE: {
if (req.transactionConfig.rawTransaction) {
return this.transactSigned(req.transactionConfig.rawTransaction);
} else {
throw new Error(
`${fnTag} Expected pre-signed raw transaction ` +
` since signing credential is specified as` +
`Web3SigningCredentialType.NONE`,
);
}
}
default: {
throw new Error(
`${fnTag} Unrecognized Web3SigningCredentialType: ` +
`${req.web3SigningCredential.type} Supported ones are: ` +
`${Object.values(Web3SigningCredentialType).join(";")}`,
);
}
}
}
public async transactSigned(
rawTransaction: string,
): Promise<RunTransactionResponse> {
const fnTag = `${this.className}#transactSigned()`;
const receipt = await this.web3.eth.sendSignedTransaction(rawTransaction);
if (receipt instanceof Error) {
this.log.debug(`${fnTag} Web3 sendSignedTransaction failed`, receipt);
throw receipt;
} else {
this.prometheusExporter.addCurrentTransaction();
return { transactionReceipt: receipt };
}
}
public async transactGethKeychain(
txIn: RunTransactionRequest,
): Promise<RunTransactionResponse> {
const fnTag = `${this.className}#transactGethKeychain()`;
const { sendTransaction } = this.web3.eth.personal;
const { transactionConfig, web3SigningCredential } = txIn;
const {
secret,
} = web3SigningCredential as Web3SigningCredentialGethKeychainPassword;
try {
const txHash = await sendTransaction(transactionConfig, secret);
const transactionReceipt = await this.pollForTxReceipt(txHash);
return { transactionReceipt };
} catch (ex) {
throw new Error(
`${fnTag} Failed to invoke web3.eth.personal.sendTransaction(). ` +
`InnerException: ${ex.stack}`,
);
}
}
public async transactPrivateKey(
req: RunTransactionRequest,
): Promise<RunTransactionResponse> {
const fnTag = `${this.className}#transactPrivateKey()`;
const { transactionConfig, web3SigningCredential } = req;
const {
secret,
} = web3SigningCredential as Web3SigningCredentialPrivateKeyHex;
const signedTx = await this.web3.eth.accounts.signTransaction(
transactionConfig,
secret,
);
if (signedTx.rawTransaction) {
return this.transactSigned(signedTx.rawTransaction);
} else {
throw new Error(
`${fnTag} Failed to sign eth transaction. ` +
`signedTransaction.rawTransaction is blank after .signTransaction().`,
);
}
}
public async transactCactusKeychainRef(
req: RunTransactionRequest,
): Promise<RunTransactionResponse> {
const fnTag = `${this.className}#transactCactusKeychainRef()`;
const { transactionConfig, web3SigningCredential } = req;
const {
ethAccount,
keychainEntryKey,
keychainId,
} = web3SigningCredential as Web3SigningCredentialCactusKeychainRef;
// locate the keychain plugin that has access to the keychain backend
// denoted by the keychainID from the request.
const keychainPlugin = this.pluginRegistry
.findManyByAspect<IPluginKeychain>(PluginAspect.KEYCHAIN)
.find((k) => k.getKeychainId() === keychainId);
Checks.truthy(keychainPlugin, `${fnTag} keychain for ID:"${keychainId}"`);
// Now use the found keychain plugin to actually perform the lookup of
// the private key that we need to run the transaction.
const privateKeyHex = await keychainPlugin?.get<string>(keychainEntryKey);
return this.transactPrivateKey({
transactionConfig,
web3SigningCredential: {
ethAccount,
type: Web3SigningCredentialType.PRIVATEKEYHEX,
secret: privateKeyHex,
},
});
}
public async pollForTxReceipt(
txHash: string,
timeoutMs = 60000,
): Promise<TransactionReceipt> {
const fnTag = `${this.className}#pollForTxReceipt()`;
let txReceipt;
let timedOut = false;
let tries = 0;
const startedAt = new Date();
do {
txReceipt = await this.web3.eth.getTransactionReceipt(txHash);
tries++;
timedOut = Date.now() >= startedAt.getTime() + timeoutMs;
} while (!timedOut && !txReceipt);
if (!txReceipt) {
throw new Error(`${fnTag} Timed out ${timeoutMs}ms, polls=${tries}`);
} else {
return txReceipt;
}
}
public async deployContract(
req: DeployContractSolidityBytecodeV1Request,
): Promise<RunTransactionResponse> {
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`);
}
if (req.keychainId != undefined && req.contractName != undefined) {
const keychainPlugin = this.pluginRegistry.findOneByKeychainId(
req.keychainId,
);
Checks.truthy(
keychainPlugin,
`${fnTag} keychain for ID:"${req.keychainId}"`,
);
if (!keychainPlugin.has(req.contractName)) {
throw new Error(
`${fnTag} Cannot create an instance of the contract because the contractName and the contractName of the JSON doesn't match`,
);
}
const networkId = await this.web3.eth.net.getId();
const web3SigningCredential = req.web3SigningCredential as
| Web3SigningCredentialGethKeychainPassword
| Web3SigningCredentialPrivateKeyHex;
const receipt = await this.transact({
transactionConfig: {
data: `0x${req.bytecode}`,
from: web3SigningCredential.ethAccount,
gas: req.gas,
gasPrice: req.gasPrice,
},
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;
keychainPlugin.set(req.contractName, contractJSON);
}
return receipt;
}
throw new Error(
`${fnTag} Cannot deploy contract without keychainId and the contractName`,
);
}
}