mapped json and client object

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>
This commit is contained in:
sapthasurendran 2022-05-18 20:06:53 +05:30
parent 997518558d
commit 3a30596903
3 changed files with 97 additions and 53 deletions

View file

@ -7,7 +7,7 @@
import { connect } from '@hyperledger/fabric-gateway';
import crpto from 'crypto';
import { newGrpcConnection, newIdentity, newSigner, tlsCertPathOrg1, peerEndpointOrg1, peerNameOrg1, certPathOrg1, mspIdOrg1, keyDirectoryPathOrg1, tlsCertPathOrg2, peerEndpointOrg2, peerNameOrg2, certPathOrg2, mspIdOrg2, keyDirectoryPathOrg2 } from './connect';
import { AssetPrice, AssetProperties, GREEN, RED, RESET } from './utils';
import { AssetPriceJSON, AssetPropertiesJSON, GREEN, RED, RESET } from './utils';
import { ContractWrapper } from './contractWrapper';
const channelName = 'mychannel';
@ -183,7 +183,7 @@ main().catch(error => {
async function createAsset(contract: ContractWrapper, org: string): Promise<void> {
console.log(`${GREEN}--> Submit Transaction: CreateAsset, ${assetKey} as ${org} - endorsed by Org1${RESET}`);
await contract.createAsset(org, assetKey, { object_type: 'asset_properties', asset_id: assetKey, color: 'blue', size: 35, salt: randomBytes });
await contract.createAsset(org, assetKey, { objectType: 'asset_properties', assetId: assetKey, color: 'blue', size: 35, salt: randomBytes });
console.log(`*** Result: committed, asset ${assetKey} is owned by Org1`);
}
@ -194,9 +194,9 @@ async function readAsset(contract: ContractWrapper, assetKey: string, ownerOrg:
try {
const result = await contract.readAsset(assetKey);
if (result.ownerOrg === ownerOrg) {
console.log(`*** Result from ${org} - asset ${result.assetID} owned by ${result.ownerOrg} DESC:${result.publicDescription}`);
console.log(`*** Result from ${org} - asset ${result.assetId} owned by ${result.ownerOrg} DESC:${result.publicDescription}`);
} else {
console.log(`${RED}*** Failed owner check from ${org} - asset ${result.assetID} owned by ${result.ownerOrg} DESC:${result.publicDescription}${RESET}`);
console.log(`${RED}*** Failed owner check from ${org} - asset ${result.assetId} owned by ${result.ownerOrg} DESC:${result.publicDescription}${RESET}`);
}
}catch (e) {
console.log(`${RED}*** Failed evaluateTransaction readAsset - ${e}${RESET}`);
@ -234,7 +234,7 @@ async function agreeToSell(contract: ContractWrapper, assetKey: string, org: str
try {
console.log(`${GREEN}--> Submit Transaction: AgreeToSell, ${assetKey} as ${org} - endorsed by ${org}${RESET}`);
await contract.agreeToSell({asset_id:assetKey, price, trade_id:now.toString()});
await contract.agreeToSell({assetId:assetKey, price, tradeId:now.toString()});
console.log(`*** Result: committed, ${org} has agreed to sell asset ${assetKey} for ${price}`);
} catch (e) {
@ -246,7 +246,7 @@ async function verifyAssetProperties(contract: ContractWrapper, assetKey: string
try {
console.log(`${GREEN}--> Evalute: VerifyAssetProperties, ${assetKey} as ${org} - endorsed by ${org}${RESET}`);
const result = await contract.verifyAssetProperties({object_type:'asset_properties', asset_id:assetKey, color:'blue', size:35, salt:randomBytes}, org);
const result = await contract.verifyAssetProperties({objectType:'asset_properties', assetId:assetKey, color:'blue', size:35, salt:randomBytes}, org);
if (result) {
console.log(`*** Success VerifyAssetProperties, private information about asset ${assetKey} has been verified by ${org}`);
@ -262,7 +262,7 @@ async function agreeToBuy(contract: ContractWrapper, assetKey: string, org: stri
try {
console.log(`${GREEN}--> Submit Transaction: AgreeToBuy, ${assetKey} as ${org} - endorsed by ${org}${RESET}`);
await contract.agreeToBuy( {asset_id:assetKey, price, trade_id: now.toString()});
await contract.agreeToBuy( {assetId:assetKey, price, tradeId: now.toString()});
console.log(`*** Result: committed, ${org} has agreed to buy asset ${assetKey} for 100`);
} catch (e) {
@ -309,14 +309,14 @@ async function transferAsset(contract: ContractWrapper, assetKey: string, org: s
console.log(`${GREEN}* Expected to fail as sell price and the bid price are not the same${RESET}`);
}
const assetProperties: AssetProperties =
{ object_type:'asset_properties',
asset_id:assetKey,
const assetProperties: AssetPropertiesJSON =
{ objectType:'asset_properties',
assetId:assetKey,
color:'blue',
size:35,
salt:randomBytes
};
const assetPrice: AssetPrice = {asset_id: assetKey, price, trade_id:now.toString()};
const assetPrice: AssetPriceJSON = { assetId: assetKey, price, tradeId:now.toString()};
await contract.transferAsset(buyerOrgID, assetProperties, assetPrice, [ mspIdOrg1, mspIdOrg2 ]);
console.log(`${GREEN}*** Result: committed, ${org} has transfered the asset ${assetKey} to ${mspIdOrg2} ${RESET}`);

View file

@ -5,88 +5,114 @@
*/
import { Contract } from '@hyperledger/fabric-gateway';
import { TextDecoder } from 'util';
import { Asset, AssetPrice, AssetProperties, parse } from './utils';
import { Asset, AssetJSON, AssetPrice, AssetPriceJSON, AssetProperties, AssetPropertiesJSON, parse } from './utils';
export class ContractWrapper {
contract?: Contract;
utf8Decoder: TextDecoder =new TextDecoder();
readonly #contract: Contract;
readonly #utf8Decoder = new TextDecoder();
public constructor(contract: Contract) {
this.contract = contract;
this.#contract = contract;
}
public async createAsset(org: string, assetKey: string, assetProperties: AssetProperties): Promise<void> {
await this.contract?.submit('CreateAsset', {
public async createAsset(org: string, assetKey: string, assetProperties: AssetPropertiesJSON): Promise<void> {
await this.#contract.submit('CreateAsset', {
arguments: [assetKey, `Asset ${assetKey} owned by ${org} is not for sale`],
transientData: { asset_properties: JSON.stringify(assetProperties)},
});
}
public async readAsset( assetKey: string): Promise<Asset> {
const resultBytes = await this.contract?.evaluateTransaction('ReadAsset', assetKey);
const result = this.utf8Decoder.decode(resultBytes);
const resultBytes = await this.#contract.evaluateTransaction('ReadAsset', assetKey);
const result = this.#utf8Decoder.decode(resultBytes);
if (result.length !== 0) {
return parse(result);
const json = parse<AssetJSON>(result);
return {
assetId: json.assetId,
ownerOrg: json.ownerOrg,
publicDescription: json.publicDescription
}
} else {
throw new Error('No Asset Found');
}
}
public async getAssetPrivateProperties( assetKey: string ): Promise<AssetProperties> {
const resultBytes = await this.contract?.evaluateTransaction('GetAssetPrivateProperties', assetKey);
const result = this.utf8Decoder.decode(resultBytes);
return parse<AssetProperties>(result);
const resultBytes = await this.#contract.evaluateTransaction('GetAssetPrivateProperties', assetKey);
const result = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetPropertiesJSON>(result);
return {
assetId: json.assetId,
color: json.color,
size: json.size,
}
}
public async changePublicDescription(assetKey: string, description: string): Promise<void> {
await this.contract?.submit('ChangePublicDescription', {
await this.#contract.submit('ChangePublicDescription', {
arguments:[assetKey, description],
});
}
public async agreeToSell(asset_price: AssetPrice): Promise<void> {
await this.contract?.submit('AgreeToSell', {
arguments:[asset_price.asset_id],
public async agreeToSell(asset_price: AssetPriceJSON): Promise<void> {
await this.#contract.submit('AgreeToSell', {
arguments:[asset_price.assetId],
transientData: {asset_price: JSON.stringify(asset_price)}
});
}
public async verifyAssetProperties(assetProperties: AssetProperties, org: string): Promise<AssetProperties> {
const resultBytes = await this.contract?.evaluate('VerifyAssetProperties', {
arguments:[assetProperties.asset_id],
public async verifyAssetProperties(assetProperties: AssetPropertiesJSON, org: string): Promise<AssetProperties> {
const resultBytes = await this.#contract.evaluate('VerifyAssetProperties', {
arguments:[assetProperties.assetId],
transientData: {asset_properties: JSON.stringify(assetProperties)},
});
const result = this.utf8Decoder.decode(resultBytes);
const result = this.#utf8Decoder.decode(resultBytes);
if (result.length !== 0) {
return parse<AssetProperties>(result);
const json = parse<AssetPropertiesJSON>(result);
return {
assetId: json.assetId,
color: json.color,
size: json.size
}
} else {
throw new Error(`Private information about asset ${assetProperties.asset_id} has not been verified by ${org}`);
throw new Error(`Private information about asset ${assetProperties.assetId} has not been verified by ${org}`);
}
}
public async agreeToBuy(asset_price: AssetPrice): Promise<void> {
await this.contract?.submit('AgreeToBuy', {
arguments:[asset_price.asset_id],
public async agreeToBuy(asset_price: AssetPriceJSON): Promise<void> {
await this.#contract.submit('AgreeToBuy', {
arguments:[asset_price.assetId],
transientData: {asset_price: JSON.stringify(asset_price)}
});
}
public async getAssetSalesPrice(assetKey: string): Promise<Asset> {
const resultBytes = await this.contract?.evaluateTransaction('GetAssetSalesPrice', assetKey);
const result = this.utf8Decoder.decode(resultBytes);
return parse<Asset>(result);
const resultBytes = await this.#contract.evaluateTransaction('GetAssetSalesPrice', assetKey);
const result = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetJSON>(result);
return {
assetId: json.assetId,
ownerOrg: json.ownerOrg,
publicDescription: json.publicDescription
}
}
public async getAssetBidPrice( assetKey: string): Promise<AssetPrice> {
const resultBytes = await this.contract?.evaluateTransaction('GetAssetBidPrice', assetKey);
const result = this.utf8Decoder.decode(resultBytes);
return parse<AssetPrice>(result);
const resultBytes = await this.#contract.evaluateTransaction('GetAssetBidPrice', assetKey);
const result = this.#utf8Decoder.decode(resultBytes);
const json = parse<AssetPriceJSON>(result);
return {
assetId: json.assetId,
price: json.price,
tradeId: json.tradeId,
}
}
public async transferAsset(buyerOrgID: string, asset_properties: AssetProperties, asset_price: AssetPrice, endorsingOrganizations: string[]): Promise<void> {
await this.contract?.submit('TransferAsset', {
arguments:[asset_properties.asset_id, buyerOrgID],
public async transferAsset(buyerOrgID: string, asset_properties: AssetPropertiesJSON, asset_price: AssetPriceJSON, endorsingOrganizations: string[]): Promise<void> {
await this.#contract.submit('TransferAsset', {
arguments:[asset_properties.assetId, buyerOrgID],
transientData: {
asset_properties: JSON.stringify(asset_properties),
asset_price: JSON.stringify(asset_price)},

View file

@ -8,25 +8,43 @@ export const RED = '\x1b[31m\n';
export const GREEN = '\x1b[32m\n';
export const RESET = '\x1b[0m';
export interface Asset {
export interface AssetJSON {
objectType: string;
assetID: string;
assetId: string;
ownerOrg: string;
publicDescription: string;
}
export interface AssetProperties {
object_type: string;
asset_id: string;
export interface AssetPropertiesJSON {
objectType: string;
assetId: string;
color: string;
size: number;
salt: string;
}
export interface AssetPrice {
asset_id: string;
export interface AssetPriceJSON {
assetId: string;
price: number;
trade_id: string;
tradeId: string;
}
export interface Asset {
assetId: string;
ownerOrg: string;
publicDescription: string;
}
export interface AssetProperties {
assetId: string;
color: string;
size: number;
}
export interface AssetPrice {
assetId: string;
price: number;
tradeId: string;
}
export function parse<T>(data: string): T {