moved try catch from contract wrapper to app

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>
This commit is contained in:
sapthasurendran 2022-05-20 15:18:01 +05:30
parent 52a3e655a4
commit 7792806482
2 changed files with 184 additions and 195 deletions

View file

@ -8,6 +8,7 @@ import { connect } from '@hyperledger/fabric-gateway';
import { newGrpcConnection, newIdentity, newSigner, tlsCertPathOrg1, peerEndpointOrg1, peerNameOrg1, certPathOrg1, mspIdOrg1, keyDirectoryPathOrg1, tlsCertPathOrg2, peerEndpointOrg2, peerNameOrg2, certPathOrg2, mspIdOrg2, keyDirectoryPathOrg2 } from './connect';
import { ContractWrapper } from './contractWrapper';
import { RED, RESET } from './utils';
const channelName = 'mychannel';
const chaincodeName = 'secured';
@ -70,7 +71,11 @@ async function main(): Promise<void> {
await contractWrapperOrg1.getAssetPrivateProperties(assetKey, mspIdOrg1);
// Org2 is not the owner and does not have the private details, read expected to fail.
await contractWrapperOrg2.getAssetPrivateProperties(assetKey, mspIdOrg1);
try {
await contractWrapperOrg2.getAssetPrivateProperties(assetKey, mspIdOrg1);
} catch(e) {
console.log(`${RED}*** Failed: getAssetPrivateProperties - ${e}${RESET}`);
}
// Org1 updates the assets public description.
await contractWrapperOrg1.changePublicDescription({AssetId: assetKey,
@ -84,10 +89,14 @@ async function main(): Promise<void> {
await contractWrapperOrg2.readAsset(assetKey, mspIdOrg1);
// This is an update to the public state and requires the owner(Org1) to endorse and sent by the owner org client (Org1).
// Since the client is from Org2, which is not the owner, this will fail
await contractWrapperOrg2.changePublicDescription({AssetId: assetKey,
OwnerOrg: mspIdOrg1,
PublicDescription: `Asset ${assetKey} owned by ${mspIdOrg2} is NOT for sale`});
// Since the client is from Org2, which is not the owner, this will fail.
try{
await contractWrapperOrg2.changePublicDescription({AssetId: assetKey,
OwnerOrg: mspIdOrg1,
PublicDescription: `Asset ${assetKey} owned by ${mspIdOrg2} is NOT for sale`});
} catch(e) {
console.log(`${RED}*** Failed: changePublicDescription - ${e}${RESET}`);
}
// Read the public details by org1.
await contractWrapperOrg1.readAsset(assetKey, mspIdOrg1);
@ -115,18 +124,28 @@ async function main(): Promise<void> {
await contractWrapperOrg1.getAssetSalesPrice(assetKey, mspIdOrg1);
// Org2 has not set a sale price and this should fail.
await contractWrapperOrg2.getAssetSalesPrice(assetKey, mspIdOrg1);
try{
await contractWrapperOrg2.getAssetSalesPrice(assetKey, mspIdOrg1);
} catch(e) {
console.log(`${RED}*** Failed: getAssetSalesPrice - ${e}${RESET}`);
}
// Org1 has not agreed to buy so this should fail.
await contractWrapperOrg1.getAssetBidPrice(assetKey, mspIdOrg2);
try{
await contractWrapperOrg1.getAssetBidPrice(assetKey, mspIdOrg2);
} catch(e) {
console.log(`${RED}*** Failed: getAssetBidPrice - ${e}${RESET}`);
}
// Org2 should be able to see the price it has agreed.
await contractWrapperOrg2.getAssetBidPrice(assetKey, mspIdOrg2);
// Org1 will try to transfer the asset to Org2
// This will fail due to the sell price and the bid price are not the same.
await contractWrapperOrg1.transferAsset({ObjectType: 'asset_properties', Color: 'blue', Size: 35}, { AssetId: assetKey, Price: 110, TradeId: now}, [ mspIdOrg1, mspIdOrg2 ], mspIdOrg1, mspIdOrg2);
try{
await contractWrapperOrg1.transferAsset({ObjectType: 'asset_properties', Color: 'blue', Size: 35}, { AssetId: assetKey, Price: 110, TradeId: now}, [ mspIdOrg1, mspIdOrg2 ], mspIdOrg1, mspIdOrg2);
} catch(e) {
console.log(`${RED}*** Failed: transferAsset - ${e}${RESET}`);
}
// Agree to a sell by Org1, the seller will agree to the bid price of Org2.
await contractWrapperOrg1.agreeToSell({AssetId:assetKey, Price:100, TradeId:now});
@ -147,7 +166,11 @@ async function main(): Promise<void> {
// Org2 user will try to transfer the asset to Org1.
// This will fail as the owner is Org1.
await contractWrapperOrg2.transferAsset({ObjectType: 'asset_properties', Color: 'blue', Size: 35}, { AssetId: assetKey, Price: 100, TradeId: now}, [ mspIdOrg1, mspIdOrg2 ], mspIdOrg1, mspIdOrg2);
try{
await contractWrapperOrg2.transferAsset({ObjectType: 'asset_properties', Color: 'blue', Size: 35}, { AssetId: assetKey, Price: 100, TradeId: now}, [ mspIdOrg1, mspIdOrg2 ], mspIdOrg1, mspIdOrg2);
} catch(e) {
console.log(`${RED}*** Failed: transferAsset - ${e}${RESET}`);
}
// Org1 will transfer the asset to Org2.
// This will now complete as the sell price and the bid price are the same.
@ -163,7 +186,11 @@ async function main(): Promise<void> {
await contractWrapperOrg2.getAssetPrivateProperties(assetKey, mspIdOrg2);
// Org1 should not be able to read the private data details of this asset, expected to fail.
await contractWrapperOrg1.getAssetPrivateProperties(assetKey, mspIdOrg2);
try{
await contractWrapperOrg1.getAssetPrivateProperties(assetKey, mspIdOrg2);
} catch(e) {
console.log(`${RED}*** Failed: getAssetPrivateProperties - ${e}${RESET}`);
}
// This is an update to the public state and requires only the owner to endorse.
// Org2 wants to indicate that the items is no longer for sale.

View file

@ -24,243 +24,205 @@ export class ContractWrapper {
public async createAsset(asset: Asset, privateData: AssetPrivateData): Promise<void> {
console.log(`${GREEN}--> Submit Transaction: CreateAsset, ${asset.AssetId} as ${asset.OwnerOrg} - endorsed by Org1${RESET}`);
try {
const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties',
assetID: asset.AssetId,
color: privateData.Color,
size: privateData.Size,
salt: this.#randomBytes });
const assetPropertiesJSON: AssetPropertiesJSON = {
objectType: 'asset_properties',
assetID: asset.AssetId,
color: privateData.Color,
size: privateData.Size,
salt: this.#randomBytes };
await this.#contract.submit('CreateAsset', {
arguments: [asset.AssetId, asset.PublicDescription],
transientData: { asset_properties: JSON.stringify(assetPropertiesJSON)},
});
await this.#contract.submit('CreateAsset', {
arguments: [asset.AssetId, asset.PublicDescription],
transientData: { asset_properties: JSON.stringify(assetPropertiesJSON)},
});
console.log(`*** Result: committed, asset ${asset.AssetId} is owned by Org1`);
}catch (e) {
console.log(`${RED}*** Failed: createAsset - ${e}${RESET}`);
}
console.log(`*** Result: committed, asset ${asset.AssetId} is owned by Org1`);
}
public async readAsset( assetKey: string, ownerOrg: string): Promise<void> {
public async readAsset(assetKey: string, ownerOrg: string): Promise<void> {
console.log(`${GREEN}--> Evaluate Transactions: ReadAsset as ${this.#org}, - ${assetKey} should be owned by ${ownerOrg}${RESET}`);
try {
const resultBytes = await this.#contract.evaluateTransaction('ReadAsset', assetKey);
const resultBytes = await this.#contract.evaluateTransaction('ReadAsset', assetKey);
const result = this.#utf8Decoder.decode(resultBytes);
if (result.length !== 0) {
const json = parse<AssetJSON>(result);
if (json.ownerOrg === ownerOrg) {
console.log(`*** Result from ${this.#org} - asset ${json.assetID} owned by ${json.ownerOrg} DESC: ${json.publicDescription}`);
} else {
console.log(`${RED}*** Failed owner check from ${this.#org} - asset ${json.assetID} owned by ${json.ownerOrg} DESC:${json.publicDescription}${RESET}`);
}
const result = this.#utf8Decoder.decode(resultBytes);
if (result.length !== 0) {
const json = parse<AssetJSON>(result);
if (json.ownerOrg === ownerOrg) {
console.log(`*** Result from ${this.#org} - asset ${json.assetID} owned by ${json.ownerOrg} DESC: ${json.publicDescription}`);
} else {
throw new Error('No Asset Found');
console.log(`${RED}*** Failed owner check from ${this.#org} - asset ${json.assetID} owned by ${json.ownerOrg} DESC:${json.publicDescription}${RESET}`);
}
}catch (e) {
console.log(`${RED}*** Failed evaluateTransaction readAsset - ${e}${RESET}`);
} else {
throw new Error('No Asset Found');
}
}
public async getAssetPrivateProperties(assetKey: string, ownerOrg: string): Promise<void> {
try{
console.log(`${GREEN}--> Evaluate Transaction: GetAssetPrivateProperties, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== ownerOrg) {
console.log(`${GREEN}* Expected to fail as ${this.#org} is not the owner and does not have the private details${RESET}`);
}
const resultBytes = await this.#contract.evaluateTransaction('GetAssetPrivateProperties', assetKey);
const resultString = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetPropertiesJSON>(resultString);
const result: AssetProperties = {
AssetId: json.assetID,
Color: json.color,
Size: json.size,
};
console.log('*** Result:', result);
}
catch(e){
console.log(`${RED}*** Failed evaluateTransaction readPrivateAsset: ${e}${RESET}`);
console.log(`${GREEN}--> Evaluate Transaction: GetAssetPrivateProperties, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== ownerOrg) {
console.log(`${GREEN}* Expected to fail as ${this.#org} is not the owner and does not have the private details.${RESET}`);
}
const resultBytes = await this.#contract.evaluateTransaction('GetAssetPrivateProperties', assetKey);
const resultString = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetPropertiesJSON>(resultString);
const result: AssetProperties = {
AssetId: json.assetID,
Color: json.color,
Size: json.size,
};
console.log('*** Result:', result);
}
public async changePublicDescription(asset: Asset): Promise<void> {
try {
console.log(`${GREEN}--> Submit Transaction: ChangePublicDescription ${asset.AssetId}, as ${this.#org} - endorse by ${this.#org} ${RESET}`);
if (asset.OwnerOrg !== this.#org) {
console.log(`${GREEN}* Expected to fail as ${this.#org} is not the owner${RESET}`);
}
await this.#contract.submit('ChangePublicDescription', {
arguments:[asset.AssetId, asset.PublicDescription],
});
console.log(`*** Result: committed, Desc: ${asset.PublicDescription}`);
} catch (e) {
console.log(`${RED}*** Failed: ChangePublicDescription - ${e}${RESET}`);
console.log(`${GREEN}--> Submit Transaction: ChangePublicDescription ${asset.AssetId}, as ${this.#org} - endorse by ${this.#org} ${RESET}`);
if (asset.OwnerOrg !== this.#org) {
console.log(`${GREEN}* Expected to fail as ${this.#org} is not the owner.${RESET}`);
}
await this.#contract.submit('ChangePublicDescription', {
arguments:[asset.AssetId, asset.PublicDescription],
});
console.log(`*** Result: committed, Desc: ${asset.PublicDescription}`);
}
public async agreeToSell(assetPrice: AssetPrice): Promise<void> {
try {
console.log(`${GREEN}--> Submit Transaction: AgreeToSell, ${assetPrice.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPriceJSON: AssetPriceJSON = {
assetID:assetPrice.AssetId,
price:assetPrice.Price,
tradeID:assetPrice.TradeId
};
await this.#contract.submit('AgreeToSell', {
arguments:[assetPrice.AssetId],
transientData: {asset_price: JSON.stringify(assetPriceJSON)}
});
console.log(`${GREEN}--> Submit Transaction: AgreeToSell, ${assetPrice.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPriceJSON: AssetPriceJSON = {
assetID:assetPrice.AssetId,
price:assetPrice.Price,
tradeID:assetPrice.TradeId
};
console.log(`*** Result: committed, ${this.#org} has agreed to sell asset ${assetPrice.AssetId} for ${assetPrice.Price}`);
} catch (e) {
console.log(`${RED}*** Failed: AgreeToSell - ${e}${RESET}`);
}
await this.#contract.submit('AgreeToSell', {
arguments:[assetPrice.AssetId],
transientData: {asset_price: JSON.stringify(assetPriceJSON)}
});
console.log(`*** Result: committed, ${this.#org} has agreed to sell asset ${assetPrice.AssetId} for ${assetPrice.Price}`);
}
public async verifyAssetProperties(assetProperties: AssetProperties): Promise<void> {
try {
console.log(`${GREEN}--> Evalute: VerifyAssetProperties, ${assetProperties.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties',
assetID: assetProperties.AssetId,
color: assetProperties.Color,
size: assetProperties.Size,
salt: this.#randomBytes });
console.log(`${GREEN}--> Evalute: VerifyAssetProperties, ${assetProperties.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties',
assetID: assetProperties.AssetId,
color: assetProperties.Color,
size: assetProperties.Size,
salt: this.#randomBytes });
const resultBytes = await this.#contract.evaluate('VerifyAssetProperties', {
arguments:[assetPropertiesJSON.assetID],
transientData: {asset_properties: JSON.stringify(assetPropertiesJSON)},
});
const resultString = this.#utf8Decoder.decode(resultBytes);
if (resultString.length !== 0) {
const json = parse<AssetPropertiesJSON>(resultString);
const result: AssetProperties = {
AssetId: json.assetID,
Color: json.color,
Size: json.size
};
if (result) {
console.log(`*** Success VerifyAssetProperties, private information about asset ${assetProperties.AssetId} has been verified by ${this.#org}`);
} else {
console.log(`*** Failed: VerifyAssetProperties, private information about asset ${assetProperties.AssetId} has not been verified by ${this.#org}`);
}
const resultBytes = await this.#contract.evaluate('VerifyAssetProperties', {
arguments:[assetPropertiesJSON.assetID],
transientData: {asset_properties: JSON.stringify(assetPropertiesJSON)},
});
const resultString = this.#utf8Decoder.decode(resultBytes);
if (resultString.length !== 0) {
const json = parse<AssetPropertiesJSON>(resultString);
const result: AssetProperties = {
AssetId: json.assetID,
Color: json.color,
Size: json.size
};
if (result) {
console.log(`*** Success VerifyAssetProperties, private information about asset ${assetProperties.AssetId} has been verified by ${this.#org}`);
} else {
throw new Error(`Private information about asset ${assetProperties.AssetId} has not been verified by ${this.#org}`);
console.log(`*** Failed: VerifyAssetProperties, private information about asset ${assetProperties.AssetId} has not been verified by ${this.#org}`);
}
} catch (e) {
console.log(`${RED}*** Failed: VerifyAssetProperties - ${e}${RESET}`);
} else {
throw new Error(`Private information about asset ${assetProperties.AssetId} has not been verified by ${this.#org}`);
}
}
public async agreeToBuy(assetPrice: AssetPrice, ): Promise<void> {
try {
console.log(`${GREEN}--> Submit Transaction: AgreeToBuy, ${assetPrice.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPriceJSON: AssetPriceJSON = {
assetID: assetPrice.AssetId,
price: assetPrice.Price,
tradeID: assetPrice.TradeId
};
await this.#contract.submit('AgreeToBuy', {
arguments:[assetPrice.AssetId],
transientData: {asset_price: JSON.stringify(assetPriceJSON)}
});
console.log(`${GREEN}--> Submit Transaction: AgreeToBuy, ${assetPrice.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPriceJSON: AssetPriceJSON = {
assetID: assetPrice.AssetId,
price: assetPrice.Price,
tradeID: assetPrice.TradeId
};
console.log(`*** Result: committed, ${this.#org} has agreed to buy asset ${assetPrice.AssetId} for 100`);
} catch (e) {
console.log(`${RED}*** Failed: AgreeToBuy - ${e}${RESET}`);
}
await this.#contract.submit('AgreeToBuy', {
arguments:[assetPrice.AssetId],
transientData: {asset_price: JSON.stringify(assetPriceJSON)}
});
console.log(`*** Result: committed, ${this.#org} has agreed to buy asset ${assetPrice.AssetId} for 100`);
}
public async getAssetSalesPrice(assetKey: string, ownerOrg: string): Promise<void> {
try {
console.log(`${GREEN}--> Evaluate Transaction: GetAssetSalesPrice, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== ownerOrg) {
console.log(`${GREEN}* Expected to fail as ${this.#org} has not set a sale price${RESET}`);
}
const resultBytes = await this.#contract.evaluateTransaction('GetAssetSalesPrice', assetKey);
const resultString = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetPriceJSON>(resultString);
const result: AssetPrice = {
AssetId: json.assetID,
Price: json.price,
TradeId: json.tradeID
};
console.log('*** Result: GetAssetSalesPrice', result);
} catch (e) {
console.log(`${RED}*** Failed evaluateTransaction GetAssetSalesPrice: ${e}${RESET}`);
console.log(`${GREEN}--> Evaluate Transaction: GetAssetSalesPrice, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== ownerOrg) {
console.log(`${GREEN}* Expected to fail as ${this.#org} has not set a sale price.${RESET}`);
}
const resultBytes = await this.#contract.evaluateTransaction('GetAssetSalesPrice', assetKey);
const resultString = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetPriceJSON>(resultString);
const result: AssetPrice = {
AssetId: json.assetID,
Price: json.price,
TradeId: json.tradeID
};
console.log('*** Result: GetAssetSalesPrice', result);
}
public async getAssetBidPrice(assetKey: string, buyerOrgID: string): Promise<void> {
try{
console.log(`${GREEN}--> Evaluate Transaction: GetAssetBidPrice, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== buyerOrgID){
console.log(`${GREEN}* Expected to fail as ${this.#org} has not agreed to buy${RESET}`);
}
const resultBytes = await this.#contract.evaluateTransaction('GetAssetBidPrice', assetKey);
const resultString = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetPriceJSON>(resultString);
const result: AssetPrice = {
AssetId: json.assetID,
Price: json.price,
TradeId: json.tradeID,
};
console.log('*** Result: GetAssetBidPrice', result);
} catch (e) {
console.log(`${RED}*** Failed evaluateTransaction GetAssetBidPrice: ${e}${RESET}`);
console.log(`${GREEN}--> Evaluate Transaction: GetAssetBidPrice, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== buyerOrgID){
console.log(`${GREEN}* Expected to fail as ${this.#org} has not agreed to buy.${RESET}`);
}
const resultBytes = await this.#contract.evaluateTransaction('GetAssetBidPrice', assetKey);
const resultString = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetPriceJSON>(resultString);
const result: AssetPrice = {
AssetId: json.assetID,
Price: json.price,
TradeId: json.tradeID,
};
console.log('*** Result: GetAssetBidPrice', result);
}
public async transferAsset( privateData: AssetPrivateData, assetPrice: AssetPrice, endorsingOrganizations: string[], ownerOrgID: string, buyerOrgID: string): Promise<void> {
try {
console.log(`${GREEN}--> Submit Transaction: TransferAsset, ${assetPrice.AssetId} as ${this.#org } - endorsed by ${this.#org}${RESET}`);
if (this.#org !== ownerOrgID) {
console.log(`${GREEN}* Expected to fail as the owner is ${ownerOrgID}${RESET}`);
} else if (assetPrice.Price === 110) {
console.log(`${GREEN}* Expected to fail as sell price and the bid price are not the same${RESET}`);
}
console.log(`${GREEN}--> Submit Transaction: TransferAsset, ${assetPrice.AssetId} as ${this.#org } - endorsed by ${this.#org}${RESET}`);
const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties',
assetID: assetPrice.AssetId,
color: privateData.Color,
size: privateData.Size,
salt: this.#randomBytes });
const assetPriceJSON: AssetPriceJSON = { assetID: assetPrice.AssetId, price:assetPrice.Price, tradeID:assetPrice.TradeId};
await this.#contract.submit('TransferAsset', {
arguments:[assetPropertiesJSON.assetID, buyerOrgID],
transientData: {
asset_properties: JSON.stringify(assetPropertiesJSON),
asset_price: JSON.stringify(assetPriceJSON)},
endorsingOrganizations:endorsingOrganizations
});
console.log(`${GREEN}*** Result: committed, ${this.#org} has transfered the asset ${assetPrice.AssetId} to ${buyerOrgID} ${RESET}`);
} catch (e) {
console.log(`${RED}*** Failed: TransferAsset - ${e}${RESET}`);
if (this.#org !== ownerOrgID) {
console.log(`${GREEN}* Expected to fail as the owner is ${ownerOrgID}.${RESET}`);
} else if (assetPrice.Price === 110) {
console.log(`${GREEN}* Expected to fail as sell price and the bid price are not the same.${RESET}`);
}
const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties',
assetID: assetPrice.AssetId,
color: privateData.Color,
size: privateData.Size,
salt: this.#randomBytes });
const assetPriceJSON: AssetPriceJSON = { assetID: assetPrice.AssetId, price:assetPrice.Price, tradeID:assetPrice.TradeId};
await this.#contract.submit('TransferAsset', {
arguments:[assetPropertiesJSON.assetID, buyerOrgID],
transientData: {
asset_properties: JSON.stringify(assetPropertiesJSON),
asset_price: JSON.stringify(assetPriceJSON)},
endorsingOrganizations:endorsingOrganizations
});
console.log(`${GREEN}*** Result: committed, ${this.#org} has transfered the asset ${assetPrice.AssetId} to ${buyerOrgID} ${RESET}`);
}
}