mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-25 11:05:10 +00:00
mapped json and client object
Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>
This commit is contained in:
parent
997518558d
commit
3a30596903
3 changed files with 97 additions and 53 deletions
|
|
@ -7,7 +7,7 @@
|
||||||
import { connect } from '@hyperledger/fabric-gateway';
|
import { connect } from '@hyperledger/fabric-gateway';
|
||||||
import crpto from 'crypto';
|
import crpto from 'crypto';
|
||||||
import { newGrpcConnection, newIdentity, newSigner, tlsCertPathOrg1, peerEndpointOrg1, peerNameOrg1, certPathOrg1, mspIdOrg1, keyDirectoryPathOrg1, tlsCertPathOrg2, peerEndpointOrg2, peerNameOrg2, certPathOrg2, mspIdOrg2, keyDirectoryPathOrg2 } from './connect';
|
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';
|
import { ContractWrapper } from './contractWrapper';
|
||||||
|
|
||||||
const channelName = 'mychannel';
|
const channelName = 'mychannel';
|
||||||
|
|
@ -183,7 +183,7 @@ main().catch(error => {
|
||||||
async function createAsset(contract: ContractWrapper, org: string): Promise<void> {
|
async function createAsset(contract: ContractWrapper, org: string): Promise<void> {
|
||||||
console.log(`${GREEN}--> Submit Transaction: CreateAsset, ${assetKey} as ${org} - endorsed by Org1${RESET}`);
|
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`);
|
console.log(`*** Result: committed, asset ${assetKey} is owned by Org1`);
|
||||||
}
|
}
|
||||||
|
|
@ -194,9 +194,9 @@ async function readAsset(contract: ContractWrapper, assetKey: string, ownerOrg:
|
||||||
try {
|
try {
|
||||||
const result = await contract.readAsset(assetKey);
|
const result = await contract.readAsset(assetKey);
|
||||||
if (result.ownerOrg === ownerOrg) {
|
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 {
|
} 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) {
|
}catch (e) {
|
||||||
console.log(`${RED}*** Failed evaluateTransaction readAsset - ${e}${RESET}`);
|
console.log(`${RED}*** Failed evaluateTransaction readAsset - ${e}${RESET}`);
|
||||||
|
|
@ -234,7 +234,7 @@ async function agreeToSell(contract: ContractWrapper, assetKey: string, org: str
|
||||||
try {
|
try {
|
||||||
console.log(`${GREEN}--> Submit Transaction: AgreeToSell, ${assetKey} as ${org} - endorsed by ${org}${RESET}`);
|
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}`);
|
console.log(`*** Result: committed, ${org} has agreed to sell asset ${assetKey} for ${price}`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
@ -246,7 +246,7 @@ async function verifyAssetProperties(contract: ContractWrapper, assetKey: string
|
||||||
try {
|
try {
|
||||||
console.log(`${GREEN}--> Evalute: VerifyAssetProperties, ${assetKey} as ${org} - endorsed by ${org}${RESET}`);
|
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) {
|
if (result) {
|
||||||
console.log(`*** Success VerifyAssetProperties, private information about asset ${assetKey} has been verified by ${org}`);
|
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 {
|
try {
|
||||||
console.log(`${GREEN}--> Submit Transaction: AgreeToBuy, ${assetKey} as ${org} - endorsed by ${org}${RESET}`);
|
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`);
|
console.log(`*** Result: committed, ${org} has agreed to buy asset ${assetKey} for 100`);
|
||||||
} catch (e) {
|
} 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}`);
|
console.log(`${GREEN}* Expected to fail as sell price and the bid price are not the same${RESET}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const assetProperties: AssetProperties =
|
const assetProperties: AssetPropertiesJSON =
|
||||||
{ object_type:'asset_properties',
|
{ objectType:'asset_properties',
|
||||||
asset_id:assetKey,
|
assetId:assetKey,
|
||||||
color:'blue',
|
color:'blue',
|
||||||
size:35,
|
size:35,
|
||||||
salt:randomBytes
|
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 ]);
|
await contract.transferAsset(buyerOrgID, assetProperties, assetPrice, [ mspIdOrg1, mspIdOrg2 ]);
|
||||||
|
|
||||||
console.log(`${GREEN}*** Result: committed, ${org} has transfered the asset ${assetKey} to ${mspIdOrg2} ${RESET}`);
|
console.log(`${GREEN}*** Result: committed, ${org} has transfered the asset ${assetKey} to ${mspIdOrg2} ${RESET}`);
|
||||||
|
|
|
||||||
|
|
@ -5,88 +5,114 @@
|
||||||
*/
|
*/
|
||||||
import { Contract } from '@hyperledger/fabric-gateway';
|
import { Contract } from '@hyperledger/fabric-gateway';
|
||||||
import { TextDecoder } from 'util';
|
import { TextDecoder } from 'util';
|
||||||
import { Asset, AssetPrice, AssetProperties, parse } from './utils';
|
import { Asset, AssetJSON, AssetPrice, AssetPriceJSON, AssetProperties, AssetPropertiesJSON, parse } from './utils';
|
||||||
|
|
||||||
export class ContractWrapper {
|
export class ContractWrapper {
|
||||||
|
|
||||||
contract?: Contract;
|
readonly #contract: Contract;
|
||||||
utf8Decoder: TextDecoder =new TextDecoder();
|
readonly #utf8Decoder = new TextDecoder();
|
||||||
|
|
||||||
public constructor(contract: Contract) {
|
public constructor(contract: Contract) {
|
||||||
this.contract = contract;
|
this.#contract = contract;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createAsset(org: string, assetKey: string, assetProperties: AssetProperties): Promise<void> {
|
public async createAsset(org: string, assetKey: string, assetProperties: AssetPropertiesJSON): Promise<void> {
|
||||||
await this.contract?.submit('CreateAsset', {
|
await this.#contract.submit('CreateAsset', {
|
||||||
arguments: [assetKey, `Asset ${assetKey} owned by ${org} is not for sale`],
|
arguments: [assetKey, `Asset ${assetKey} owned by ${org} is not for sale`],
|
||||||
transientData: { asset_properties: JSON.stringify(assetProperties)},
|
transientData: { asset_properties: JSON.stringify(assetProperties)},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async readAsset( assetKey: string): Promise<Asset> {
|
public async readAsset( assetKey: string): Promise<Asset> {
|
||||||
const resultBytes = await this.contract?.evaluateTransaction('ReadAsset', assetKey);
|
const resultBytes = await this.#contract.evaluateTransaction('ReadAsset', assetKey);
|
||||||
const result = this.utf8Decoder.decode(resultBytes);
|
const result = this.#utf8Decoder.decode(resultBytes);
|
||||||
if (result.length !== 0) {
|
if (result.length !== 0) {
|
||||||
return parse(result);
|
const json = parse<AssetJSON>(result);
|
||||||
|
return {
|
||||||
|
assetId: json.assetId,
|
||||||
|
ownerOrg: json.ownerOrg,
|
||||||
|
publicDescription: json.publicDescription
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error('No Asset Found');
|
throw new Error('No Asset Found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAssetPrivateProperties( assetKey: string ): Promise<AssetProperties> {
|
public async getAssetPrivateProperties( assetKey: string ): Promise<AssetProperties> {
|
||||||
const resultBytes = await this.contract?.evaluateTransaction('GetAssetPrivateProperties', assetKey);
|
const resultBytes = await this.#contract.evaluateTransaction('GetAssetPrivateProperties', assetKey);
|
||||||
const result = this.utf8Decoder.decode(resultBytes);
|
const result = this.#utf8Decoder.decode(resultBytes);
|
||||||
return parse<AssetProperties>(result);
|
const json = parse<AssetPropertiesJSON>(result);
|
||||||
|
return {
|
||||||
|
assetId: json.assetId,
|
||||||
|
color: json.color,
|
||||||
|
size: json.size,
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async changePublicDescription(assetKey: string, description: string): Promise<void> {
|
public async changePublicDescription(assetKey: string, description: string): Promise<void> {
|
||||||
await this.contract?.submit('ChangePublicDescription', {
|
await this.#contract.submit('ChangePublicDescription', {
|
||||||
arguments:[assetKey, description],
|
arguments:[assetKey, description],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async agreeToSell(asset_price: AssetPrice): Promise<void> {
|
public async agreeToSell(asset_price: AssetPriceJSON): Promise<void> {
|
||||||
await this.contract?.submit('AgreeToSell', {
|
await this.#contract.submit('AgreeToSell', {
|
||||||
arguments:[asset_price.asset_id],
|
arguments:[asset_price.assetId],
|
||||||
transientData: {asset_price: JSON.stringify(asset_price)}
|
transientData: {asset_price: JSON.stringify(asset_price)}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async verifyAssetProperties(assetProperties: AssetProperties, org: string): Promise<AssetProperties> {
|
public async verifyAssetProperties(assetProperties: AssetPropertiesJSON, org: string): Promise<AssetProperties> {
|
||||||
const resultBytes = await this.contract?.evaluate('VerifyAssetProperties', {
|
const resultBytes = await this.#contract.evaluate('VerifyAssetProperties', {
|
||||||
arguments:[assetProperties.asset_id],
|
arguments:[assetProperties.assetId],
|
||||||
transientData: {asset_properties: JSON.stringify(assetProperties)},
|
transientData: {asset_properties: JSON.stringify(assetProperties)},
|
||||||
});
|
});
|
||||||
const result = this.utf8Decoder.decode(resultBytes);
|
const result = this.#utf8Decoder.decode(resultBytes);
|
||||||
if (result.length !== 0) {
|
if (result.length !== 0) {
|
||||||
return parse<AssetProperties>(result);
|
const json = parse<AssetPropertiesJSON>(result);
|
||||||
|
return {
|
||||||
|
assetId: json.assetId,
|
||||||
|
color: json.color,
|
||||||
|
size: json.size
|
||||||
|
}
|
||||||
} else {
|
} 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> {
|
public async agreeToBuy(asset_price: AssetPriceJSON): Promise<void> {
|
||||||
await this.contract?.submit('AgreeToBuy', {
|
await this.#contract.submit('AgreeToBuy', {
|
||||||
arguments:[asset_price.asset_id],
|
arguments:[asset_price.assetId],
|
||||||
transientData: {asset_price: JSON.stringify(asset_price)}
|
transientData: {asset_price: JSON.stringify(asset_price)}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAssetSalesPrice(assetKey: string): Promise<Asset> {
|
public async getAssetSalesPrice(assetKey: string): Promise<Asset> {
|
||||||
const resultBytes = await this.contract?.evaluateTransaction('GetAssetSalesPrice', assetKey);
|
const resultBytes = await this.#contract.evaluateTransaction('GetAssetSalesPrice', assetKey);
|
||||||
const result = this.utf8Decoder.decode(resultBytes);
|
const result = this.#utf8Decoder.decode(resultBytes);
|
||||||
return parse<Asset>(result);
|
const json = parse<AssetJSON>(result);
|
||||||
|
return {
|
||||||
|
assetId: json.assetId,
|
||||||
|
ownerOrg: json.ownerOrg,
|
||||||
|
publicDescription: json.publicDescription
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAssetBidPrice( assetKey: string): Promise<AssetPrice> {
|
public async getAssetBidPrice( assetKey: string): Promise<AssetPrice> {
|
||||||
const resultBytes = await this.contract?.evaluateTransaction('GetAssetBidPrice', assetKey);
|
const resultBytes = await this.#contract.evaluateTransaction('GetAssetBidPrice', assetKey);
|
||||||
const result = this.utf8Decoder.decode(resultBytes);
|
const result = this.#utf8Decoder.decode(resultBytes);
|
||||||
return parse<AssetPrice>(result);
|
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> {
|
public async transferAsset(buyerOrgID: string, asset_properties: AssetPropertiesJSON, asset_price: AssetPriceJSON, endorsingOrganizations: string[]): Promise<void> {
|
||||||
await this.contract?.submit('TransferAsset', {
|
await this.#contract.submit('TransferAsset', {
|
||||||
arguments:[asset_properties.asset_id, buyerOrgID],
|
arguments:[asset_properties.assetId, buyerOrgID],
|
||||||
transientData: {
|
transientData: {
|
||||||
asset_properties: JSON.stringify(asset_properties),
|
asset_properties: JSON.stringify(asset_properties),
|
||||||
asset_price: JSON.stringify(asset_price)},
|
asset_price: JSON.stringify(asset_price)},
|
||||||
|
|
|
||||||
|
|
@ -8,25 +8,43 @@ export const RED = '\x1b[31m\n';
|
||||||
export const GREEN = '\x1b[32m\n';
|
export const GREEN = '\x1b[32m\n';
|
||||||
export const RESET = '\x1b[0m';
|
export const RESET = '\x1b[0m';
|
||||||
|
|
||||||
export interface Asset {
|
export interface AssetJSON {
|
||||||
objectType: string;
|
objectType: string;
|
||||||
assetID: string;
|
assetId: string;
|
||||||
ownerOrg: string;
|
ownerOrg: string;
|
||||||
publicDescription: string;
|
publicDescription: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AssetProperties {
|
export interface AssetPropertiesJSON {
|
||||||
object_type: string;
|
objectType: string;
|
||||||
asset_id: string;
|
assetId: string;
|
||||||
color: string;
|
color: string;
|
||||||
size: number;
|
size: number;
|
||||||
salt: string;
|
salt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AssetPrice {
|
export interface AssetPriceJSON {
|
||||||
asset_id: string;
|
assetId: string;
|
||||||
price: number;
|
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 {
|
export function parse<T>(data: string): T {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue