Skip to content

Commit 705a448

Browse files
committed
feat(satp-hermes): bungee & ethereum bridge
* added logic for satp-bridge supporting the ethereum connector * finished the integration with bungee * changed common stringify for safeStableStringify across the package * added test for the new bridge, and end-to-end tests for the new features * fabric-connector: fix bug handling empty read-write sets * bungee: strategy fabric fix bug handling empty read-write sets * bungee: hole package introduce safeStableStringify * ethereum-connector: bump web3 packages versions to fix (web3/web3.js#6798) Signed-off-by: Eduardo Vasques <[email protected]>
1 parent dd47d6a commit 705a448

File tree

66 files changed

+4013
-270
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+4013
-270
lines changed

examples/cactus-example-cbdc-bridging-backend/src/main/typescript/infrastructure/cbdc-bridging-app-dummy-infrastructure.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import {
7676
AdminApi,
7777
TransactionApi,
7878
} from "@hyperledger/cactus-plugin-satp-hermes/src/main/typescript/generated/gateway-client/typescript-axios/api";
79+
import { ClaimFormat } from "@hyperledger/cactus-plugin-satp-hermes/src/main/typescript/generated/proto/cacti/satp/v02/common/message_pb";
7980

8081
export interface ICbdcBridgingAppDummyInfrastructureOptions {
8182
logLevel?: LogLevelDesc;
@@ -439,6 +440,7 @@ export class CbdcBridgingAppDummyInfrastructure {
439440
contractName: CbdcBridgingAppDummyInfrastructure.SATP_WRAPPER,
440441
options: this.fabricConnectorBridgeOptions,
441442
bungeeOptions: pluginBungeeFabricOptions,
443+
claimFormat: ClaimFormat.DEFAULT,
442444
} as FabricConfig;
443445

444446
const besuConfig = {
@@ -454,6 +456,7 @@ export class CbdcBridgingAppDummyInfrastructure {
454456
options: this.besuOptions,
455457
bungeeOptions: pluginBungeeBesuOptions,
456458
gas: 999999999999999,
459+
claimFormat: ClaimFormat.DEFAULT,
457460
};
458461

459462
const besuGatewayOptions: SATPGatewayConfig = {

packages/cactus-plugin-bungee-hermes/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"http-errors-enhanced-cjs": "2.0.1",
7373
"key-encoder": "2.0.3",
7474
"merkletreejs": "0.3.11",
75+
"safe-stable-stringify": "2.5.0",
7576
"typescript-optional": "2.0.1",
7677
"uuid": "10.0.0",
7778
"web3": "1.6.1",

packages/cactus-plugin-bungee-hermes/src/main/typescript/plugin-bungee-hermes.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
LoggerProvider,
88
Secp256k1Keys,
99
} from "@hyperledger/cactus-common";
10+
import { stringify as safeStableStringify } from "safe-stable-stringify";
11+
1012
import { v4 as uuidV4 } from "uuid";
1113
import {
1214
ICactusPlugin,
@@ -231,8 +233,8 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
231233
view.setCreator(this.pubKeyBungee);
232234
view.setKey(uuidV4());
233235
return {
234-
view: JSON.stringify(view),
235-
signature: this.sign(JSON.stringify(view)),
236+
view: safeStableStringify(view),
237+
signature: this.sign(safeStableStringify(view)),
236238
};
237239
}
238240
onMergeViews(request: MergeViewsRequest): MergeViewsResponse {
@@ -267,7 +269,7 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
267269
request.policyArguments ? request.policyArguments : [],
268270
);
269271
return {
270-
integratedView: JSON.stringify(integratedView),
272+
integratedView: safeStableStringify(integratedView),
271273
signature: integratedView.signature,
272274
};
273275
}
@@ -287,7 +289,7 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
287289
this.logger.info("Generating view for request: ", request);
288290
const response = this.generateView(snapshot, ti, tf, request.viewID);
289291
return {
290-
view: JSON.stringify(response.view),
292+
view: safeStableStringify(response.view),
291293
signature: response.signature,
292294
};
293295
}
@@ -308,7 +310,7 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
308310
const view = new View(this.pubKeyBungee, tI, tF, snapshot, id);
309311
snapshot.pruneStates(tI, tF);
310312

311-
const signature = this.sign(JSON.stringify(view));
313+
const signature = this.sign(safeStableStringify(view));
312314

313315
return { view: view, signature: signature };
314316
}
@@ -414,7 +416,7 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
414416
integratedView: integratedView,
415417
//The paper specs suggest the integratedView should be jointly signed by all participants.
416418
//That process is left to be addressed in the future
417-
signature: this.sign(JSON.stringify(integratedView)),
419+
signature: this.sign(safeStableStringify(integratedView)),
418420
};
419421
}
420422

packages/cactus-plugin-bungee-hermes/src/main/typescript/strategy/strategy-besu.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
Logger,
66
LoggerProvider,
77
} from "@hyperledger/cactus-common";
8+
import { stringify as safeStableStringify } from "safe-stable-stringify";
9+
810
import {
911
DefaultApi as BesuApi,
1012
EthContractInvocationType,
@@ -170,7 +172,7 @@ export class StrategyBesu implements ObtainLedgerStrategy {
170172
"Transaction: " +
171173
log.transactionHash +
172174
"\nData: " +
173-
JSON.stringify(log.data) +
175+
safeStableStringify(log.data) +
174176
"\n =========== \n",
175177
);
176178
const proof = new Proof({
@@ -185,7 +187,7 @@ export class StrategyBesu implements ObtainLedgerStrategy {
185187
transaction.setTarget(networkDetails.contractAddress as string);
186188
transaction.setPayload(txTx.input ? txTx.input : ""); //FIXME: payload = transaction input ?
187189
transactions.push(transaction);
188-
values.push(JSON.stringify(log.data));
190+
values.push(safeStableStringify(log.data));
189191

190192
blocks.set(transaction.getId(), txBlock);
191193
}

packages/cactus-plugin-bungee-hermes/src/main/typescript/strategy/strategy-ethereum.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
LoggerProvider,
55
Checks,
66
} from "@hyperledger/cactus-common";
7+
import { stringify as safeStableStringify } from "safe-stable-stringify";
8+
79
import {
810
Web3SigningCredential,
911
DefaultApi as EthereumApi,
@@ -214,7 +216,7 @@ export class StrategyEthereum implements ObtainLedgerStrategy {
214216
"Transaction: " +
215217
log.transactionHash +
216218
"\nData: " +
217-
JSON.stringify(log.data) +
219+
safeStableStringify(log.data) +
218220
"\n =========== \n",
219221
);
220222
const proof = new Proof({
@@ -229,7 +231,7 @@ export class StrategyEthereum implements ObtainLedgerStrategy {
229231
transaction.setTarget(networkDetails.contractAddress as string);
230232
transaction.setPayload(txTx.data.input ? txTx.data.input : ""); //FIXME: payload = transaction input ?
231233
transactions.push(transaction);
232-
values.push(JSON.stringify(log.data));
234+
values.push(safeStableStringify(log.data));
233235

234236
blocks.set(transaction.getId(), txBlock.data);
235237
}

packages/cactus-plugin-bungee-hermes/src/main/typescript/strategy/strategy-fabric.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Logger,
1414
LoggerProvider,
1515
} from "@hyperledger/cactus-common";
16+
import { stringify as safeStableStringify } from "safe-stable-stringify";
1617
import { Transaction } from "../view-creation/transaction";
1718
import { State } from "../view-creation/state";
1819
import { StateProof } from "../view-creation/state-proof";
@@ -101,7 +102,11 @@ export class StrategyFabric implements ObtainLedgerStrategy {
101102
mspid: receipt.transactionCreator.mspid,
102103
}),
103104
);
104-
assetValues.push(JSON.parse(receipt.rwsetWriteData).Value.toString());
105+
if (!receipt.rwsetWriteData) {
106+
assetValues.push("");
107+
} else {
108+
assetValues.push(JSON.parse(receipt.rwsetWriteData).Value.toString());
109+
}
105110
tx.setStateId(assetKey);
106111
tx.setTarget(receipt.channelID + ": " + receipt.chainCodeName);
107112

@@ -135,10 +140,11 @@ export class StrategyFabric implements ObtainLedgerStrategy {
135140
//only adding last block for each state, in the state proof
136141
stateProof.addBlock({
137142
blockHash: block.hash,
138-
blockCreator: JSON.stringify({
139-
mspid: last_receipt.blockMetaData.mspid,
140-
id: last_receipt.blockMetaData.blockCreatorID,
141-
}),
143+
blockCreator:
144+
safeStableStringify({
145+
mspid: last_receipt.blockMetaData.mspid,
146+
id: last_receipt.blockMetaData.blockCreatorID,
147+
}) ?? "",
142148
blockSigners: block.signers,
143149
});
144150

@@ -170,7 +176,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
170176
if (!response) {
171177
throw new InternalServerError(`${fn} response is falsy`);
172178
}
173-
const receiptLockRes = JSON.stringify(response);
179+
const receiptLockRes = safeStableStringify(response);
174180
if (!receiptLockRes) {
175181
throw new InternalServerError(`${fn} receiptLockRes is falsy`);
176182
}
@@ -199,7 +205,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
199205
throw new InternalServerError(`${fn} response.data is falsy`);
200206
}
201207

202-
const receiptLockRes = JSON.stringify(data);
208+
const receiptLockRes = safeStableStringify(data);
203209
if (!receiptLockRes) {
204210
throw new InternalServerError(`${fn} receiptLockRes is falsy`);
205211
}
@@ -275,7 +281,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
275281
);
276282
}
277283

278-
const block = JSON.parse(JSON.stringify(block_data)).decodedBlock;
284+
const block = JSON.parse(safeStableStringify(block_data)).decodedBlock;
279285

280286
const blockSig = block.metadata.metadata[0].signatures;
281287
const sigs = [];
@@ -289,7 +295,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
289295
},
290296
signature: Buffer.from(sig.signature.data).toString("hex"),
291297
};
292-
sigs.push(JSON.stringify(decoded));
298+
sigs.push(safeStableStringify(decoded));
293299
}
294300
return {
295301
hash: Buffer.from(block.header.data_hash.data).toString("hex"),
@@ -440,7 +446,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
440446
ts,
441447
new TransactionProof(new Proof({ creator: "" }), txId), //transaction proof details are set in function 'generateLedgerStates'
442448
);
443-
transaction.setPayload(JSON.stringify(tx.value));
449+
transaction.setPayload(safeStableStringify(tx.value) ?? "");
444450
transactions.push(transaction);
445451
}
446452
return transactions.reverse();

packages/cactus-plugin-bungee-hermes/src/main/typescript/view-creation/proof.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Proof is a general purpose type, used to represent signatures of diverse elements.
22
// Proof may be used, for example in Fabric, to represent an transaction endorsement
33
// Or simply the signature of a transaction upon is creation (in Besu)
4+
import { stringify as safeStableStringify } from "safe-stable-stringify";
5+
46
export class Proof {
57
// The term creator refers to the ID of the entity who created the signature
68
// For example endorserID in Fabric (when Proof represents an endorsement)
@@ -36,7 +38,7 @@ export class Proof {
3638
mspid: this.mspid,
3739
signature: this.signature,
3840
};
39-
return JSON.stringify(proof);
41+
return safeStableStringify(proof);
4042
}
4143

4244
public getCreator(): string {

packages/cactus-plugin-bungee-hermes/src/main/typescript/view-creation/snapshot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { State } from "./state";
2+
import { stringify as safeStableStringify } from "safe-stable-stringify";
23

34
export class Snapshot {
45
private id: string;
@@ -99,7 +100,7 @@ export class Snapshot {
99100
stateBins: this.stateBins,
100101
};
101102

102-
return JSON.stringify(snapshotJson);
103+
return safeStableStringify(snapshotJson);
103104
}
104105

105106
public removeState(stateId: string) {

packages/cactus-plugin-bungee-hermes/src/main/typescript/view-creation/state.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { StateProof } from "./state-proof";
22
import { Transaction } from "./transaction";
3+
import { stringify as safeStableStringify } from "safe-stable-stringify";
34

45
export class State {
56
private id: string;
@@ -36,7 +37,7 @@ export class State {
3637

3738
for (const tx of this.transactions) {
3839
txs.push(tx.getTxJson());
39-
txEndorsements.push(JSON.stringify(tx.getProof()));
40+
txEndorsements.push(safeStableStringify(tx.getProof()));
4041
}
4142

4243
const jsonSnap = {
@@ -47,7 +48,7 @@ export class State {
4748
proofs: txEndorsements,
4849
};
4950

50-
return JSON.stringify(jsonSnap);
51+
return safeStableStringify(jsonSnap);
5152
}
5253

5354
public getTransactions() {

packages/cactus-plugin-bungee-hermes/src/main/typescript/view-creation/transaction-proof.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Proof } from "./proof";
2+
import { stringify as safeStableStringify } from "safe-stable-stringify";
23

34
export class TransactionProof {
45
private transactionCreator: Proof;
@@ -27,7 +28,7 @@ export class TransactionProof {
2728
transactionCreator: this.transactionCreator,
2829
endorsements: this.endorsements,
2930
};
30-
return JSON.stringify(proof);
31+
return safeStableStringify(proof);
3132
}
3233

3334
public getCreator(): Proof {

packages/cactus-plugin-bungee-hermes/src/main/typescript/view-creation/transaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Proof } from "./proof";
22
import { TransactionProof } from "./transaction-proof";
3+
import { stringify as safeStableStringify } from "safe-stable-stringify";
34

45
export class Transaction {
56
private id: string;
@@ -52,7 +53,7 @@ export class Transaction {
5253

5354
public getTxJson(): string {
5455
const tx = { Id: this.id, TimeStamp: this.timeStamp };
55-
return JSON.stringify(tx);
56+
return safeStableStringify(tx);
5657
}
5758

5859
public getProof(): TransactionProof {

packages/cactus-plugin-bungee-hermes/src/main/typescript/view-creation/view.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Transaction } from "./transaction";
55
import { IPrivacyPolicy, IPrivacyPolicyValue } from "./privacy-policies";
66
import { PrivacyPolicyOpts } from "../generated/openapi/typescript-axios";
77
import { JsObjectSigner } from "@hyperledger/cactus-common";
8+
import { stringify as safeStableStringify } from "safe-stable-stringify";
89

910
export interface IViewMetadata {
1011
viewId: string;
@@ -87,9 +88,9 @@ export class View {
8788
const transactions: string[] = [];
8889

8990
for (const state of this.snapshot.getStateBins()) {
90-
states.push(JSON.stringify(state.getStateProof()));
91+
states.push(safeStableStringify(state.getStateProof()));
9192
for (const transaction of state.getTransactions()) {
92-
transactions.push(JSON.stringify(transaction.getProof()));
93+
transactions.push(safeStableStringify(transaction.getProof()));
9394
}
9495
}
9596

@@ -112,7 +113,7 @@ export class View {
112113
tF: this.tF,
113114
snapshot: this.snapshot,
114115
};
115-
return JSON.stringify(viewStr);
116+
return safeStableStringify(viewStr);
116117
// return this.snapshot.getSnapshotJson();
117118
}
118119
public getViewProof(): {

packages/cactus-plugin-bungee-hermes/src/main/typescript/view-merging/integrated-view.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Transaction } from "../view-creation/transaction";
88
import { IViewMetadata } from "../view-creation/view";
99
import { MergePolicyOpts } from "../generated/openapi/typescript-axios";
1010
import { JsObjectSigner } from "@hyperledger/cactus-common";
11+
import { stringify as safeStableStringify } from "safe-stable-stringify";
1112

1213
export class IntegratedView {
1314
private id: string;
@@ -114,11 +115,11 @@ export class IntegratedView {
114115
const states: string[] = [];
115116
const transactions: string[] = [];
116117
this.getAllTransactions().forEach((transaction) => {
117-
transactions.push(JSON.stringify(transaction.getProof()));
118+
transactions.push(safeStableStringify(transaction.getProof()));
118119
});
119120

120121
this.getAllStates().forEach((state) => {
121-
states.push(JSON.stringify(state.getStateProof()));
122+
states.push(safeStableStringify(state.getStateProof()));
122123
});
123124
const statesTree = new MerkleTree(states, undefined, {
124125
sort: true,
@@ -129,7 +130,7 @@ export class IntegratedView {
129130
hashLeaves: true,
130131
});
131132
const viewsTree = new MerkleTree(
132-
this.viewsMetadata.map((x) => JSON.stringify(x)),
133+
this.viewsMetadata.map((x) => safeStableStringify(x)),
133134
undefined,
134135
{
135136
sort: true,

packages/cactus-plugin-ledger-connector-ethereum/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@
8282
"sanitize-html": "2.12.1",
8383
"socket.io-client-fixed-types": "4.5.4",
8484
"typescript-optional": "2.0.1",
85-
"web3": "4.4.0",
86-
"web3-eth": "4.4.0",
87-
"web3-eth-contract": "4.2.0"
85+
"web3": "4.14.0",
86+
"web3-eth": "4.10.0",
87+
"web3-eth-contract": "4.7.0"
8888
},
8989
"devDependencies": {
9090
"@hyperledger/cactus-plugin-keychain-memory": "2.0.0-rc.7",

packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/common/get-transaction-receipt-by-tx-id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export async function getTransactionReceiptByTxID(
157157
if (!extensionNsRwset.rwset) continue;
158158

159159
const rwset = extensionNsRwset.rwset;
160-
if (!rwset.writes) continue;
160+
if (!rwset.writes || rwset.writes.length === 0) continue;
161161
const rwsetWrite = rwset.writes;
162162
if (!rwsetWrite[0].key) continue;
163163
const rwsetKey = rwsetWrite[0].key;

0 commit comments

Comments
 (0)