code refactor

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>
This commit is contained in:
sapthasurendran 2022-05-18 17:19:47 +05:30
parent 9d2f8f0579
commit 201dd994f9
6 changed files with 215 additions and 203 deletions

View file

@ -51,7 +51,7 @@ Like other samples, the Fabric test network is used to deploy and run this sampl
npm start npm start
# To run the Javascript sample application # To run the Javascript sample application
cd application-gateway-javascript cd application-javascript
node app.js node app.js
``` ```

View file

@ -30,7 +30,10 @@
"sourceType": "module", "sourceType": "module",
"ecmaFeatures": { "ecmaFeatures": {
"impliedStrict": true "impliedStrict": true
} },
"project": [
"./tsconfig.json"
]
}, },
"plugins": [ "plugins": [
"@typescript-eslint" "@typescript-eslint"
@ -39,7 +42,61 @@
"eslint:recommended", "eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended" "plugin:@typescript-eslint/recommended"
] ],
"rules": {
"@typescript-eslint/comma-spacing": [
"error"
],
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowExpressions": true
}
],
"@typescript-eslint/func-call-spacing": [
"error"
],
"@typescript-eslint/member-delimiter-style": [
"error"
],
"@typescript-eslint/indent": [
"error",
4,
{
"SwitchCase": 0
}
],
"@typescript-eslint/prefer-nullish-coalescing": [
"error"
],
"@typescript-eslint/prefer-optional-chain": [
"error"
],
"@typescript-eslint/prefer-reduce-type-parameter": [
"error"
],
"@typescript-eslint/prefer-return-this-type": [
"error"
],
"@typescript-eslint/quotes": [
"error",
"single"
],
"@typescript-eslint/type-annotation-spacing": [
"error"
],
"@typescript-eslint/semi": [
"error"
],
"@typescript-eslint/space-before-function-paren": [
"error",
{
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}
]
}
} }
] ]
} }

View file

@ -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 { Asset, AssetPrice, AssetProperties, GREEN, RED, RESET } from './utils'; import { AssetPrice, AssetProperties, GREEN, RED, RESET } from './utils';
import { ContractWrapper } from './contractWrapper'; import { ContractWrapper } from './contractWrapper';
const channelName = 'mychannel'; const channelName = 'mychannel';
@ -69,13 +69,13 @@ async function main(): Promise<void> {
await readAsset(contractWrapperOrg2, assetKey, mspIdOrg1, mspIdOrg2); await readAsset(contractWrapperOrg2, assetKey, mspIdOrg1, mspIdOrg2);
// Org1 should be able to read the private data details of the asset. // Org1 should be able to read the private data details of the asset.
await readPrivateAsset(contractWrapperOrg1, assetKey, mspIdOrg1) await readPrivateAsset(contractWrapperOrg1, assetKey, mspIdOrg1);
// Org2 is not the owner and does not have the private details, read expected to fail. // Org2 is not the owner and does not have the private details, read expected to fail.
await readPrivateAsset(contractWrapperOrg2, assetKey, mspIdOrg2) await readPrivateAsset(contractWrapperOrg2, assetKey, mspIdOrg2);
// Org1 updates the assets public description. // Org1 updates the assets public description.
await changePublicDescription(contractWrapperOrg1, assetKey, mspIdOrg1, `Asset ${assetKey} owned by ${mspIdOrg1} is for sale`) await changePublicDescription(contractWrapperOrg1, assetKey, mspIdOrg1, `Asset ${assetKey} owned by ${mspIdOrg1} is for sale`);
// Read the public details by org1. // Read the public details by org1.
await readAsset(contractWrapperOrg1, assetKey, mspIdOrg1, mspIdOrg1); await readAsset(contractWrapperOrg1, assetKey, mspIdOrg1, mspIdOrg1);
@ -129,7 +129,7 @@ async function main(): Promise<void> {
await readAsset(contractWrapperOrg2, assetKey, mspIdOrg1, mspIdOrg2); await readAsset(contractWrapperOrg2, assetKey, mspIdOrg1, mspIdOrg2);
// Org1 should be able to read the private data details of the asset. // Org1 should be able to read the private data details of the asset.
await readPrivateAsset(contractWrapperOrg1, assetKey, mspIdOrg1,) await readPrivateAsset(contractWrapperOrg1, assetKey, mspIdOrg1,);
// Org1 should be able to read the sale price of this asset // Org1 should be able to read the sale price of this asset
await readSalePrice(contractWrapperOrg1, assetKey, mspIdOrg1); await readSalePrice(contractWrapperOrg1, assetKey, mspIdOrg1);
@ -183,10 +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.submit('CreateAsset', { await contract.createAsset(org, assetKey, { object_type: 'asset_properties', asset_id: assetKey, color: 'blue', size: 35, salt: randomBytes });
arguments: [assetKey,`Asset ${assetKey} owned by ${org} is not for sale`],
transientData: { asset_properties: AssetProperties.instance('asset_properties', assetKey, 'blue', 35, randomBytes)},
});
console.log(`*** Result: committed, asset ${assetKey} is owned by Org1`); console.log(`*** Result: committed, asset ${assetKey} is owned by Org1`);
} }
@ -194,32 +191,25 @@ async function createAsset(contract:ContractWrapper, org:string):Promise<void> {
async function readAsset(contract: ContractWrapper, assetKey: string, ownerOrg: string, org: string): Promise<void> { async function readAsset(contract: ContractWrapper, assetKey: string, ownerOrg: string, org: string): Promise<void> {
console.log(`${GREEN}--> Evaluate Transactions: ReadAsset as ${org}, - ${assetKey} should be owned by ${ownerOrg}${RESET}`); console.log(`${GREEN}--> Evaluate Transactions: ReadAsset as ${org}, - ${assetKey} should be owned by ${ownerOrg}${RESET}`);
try {
const resultString = await contract.evaluateTransaction('ReadAsset', assetKey); const result = await contract.readAsset(assetKey);
const result = Asset.parse(resultString); if (result.ownerOrg === ownerOrg) {
if (resultString.length !== 0) {
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) {
}else{ console.log(`${RED}*** Failed evaluateTransaction readAsset - ${e}${RESET}`);
console.log(`${RED}*** Failed ReadAsset ${RESET}`);
} }
} }
async function readPrivateAsset(contract: ContractWrapper, assetKey: string, org: string): Promise<void> { async function readPrivateAsset(contract: ContractWrapper, assetKey: string, org: string): Promise<void> {
try{ try{
console.log(`${GREEN}--> Evaluate Transaction: GetAssetPrivateProperties, - ${assetKey} from organization ${org}${RESET}`); console.log(`${GREEN}--> Evaluate Transaction: GetAssetPrivateProperties, - ${assetKey} from organization ${org}${RESET}`);
if(org === mspIdOrg2){ if(org === mspIdOrg2){
console.log(`${GREEN}* Expected to fail as ${org} is not the owner and does not have the private details${RESET}`) console.log(`${GREEN}* Expected to fail as ${org} is not the owner and does not have the private details${RESET}`);
} }
const resultString = await contract.evaluateTransaction('GetAssetPrivateProperties', assetKey); const result = await contract.getAssetPrivateProperties(assetKey);
const result = Asset.parse(resultString);
console.log('*** Result:', result); console.log('*** Result:', result);
} }
catch(e){ catch(e){
@ -231,13 +221,9 @@ async function changePublicDescription(contract:ContractWrapper, assetKey:string
try { try {
console.log(`${GREEN}--> Submit Transaction: ChangePublicDescription ${assetKey}, as ${org} - endorse by ${org}${RESET}`); console.log(`${GREEN}--> Submit Transaction: ChangePublicDescription ${assetKey}, as ${org} - endorse by ${org}${RESET}`);
if (org === mspIdOrg2) { if (org === mspIdOrg2) {
console.log(`${GREEN}* Expected to fail as ${org} is not the owner${RESET}`) console.log(`${GREEN}* Expected to fail as ${org} is not the owner${RESET}`);
} }
await contract.changePublicDescription(assetKey, description);
await contract.submit('ChangePublicDescription', {
arguments:[assetKey,description],
});
console.log(`*** Result: committed, asset ${assetKey} is now for sale by ${org}`); console.log(`*** Result: committed, asset ${assetKey} is now for sale by ${org}`);
} catch (e) { } catch (e) {
console.log(`${RED}*** Failed: ChangePublicDescription - ${e}${RESET}`); console.log(`${RED}*** Failed: ChangePublicDescription - ${e}${RESET}`);
@ -248,10 +234,7 @@ async function agreeToSell(contract:ContractWrapper, assetKey:string, org:string
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.submit('AgreeToSell',{ await contract.agreeToSell({asset_id:assetKey, price, trade_id:now.toString()});
arguments:[assetKey],
transientData: { asset_price: AssetPrice.instance(assetKey, price, 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) {
@ -263,21 +246,13 @@ 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 resultString = await contract.evaluate('VerifyAssetProperties',{ const result = await contract.verifyAssetProperties({object_type:'asset_properties', asset_id:assetKey, color:'blue', size:35, salt:randomBytes}, org);
arguments:[assetKey],
transientData: { asset_properties: AssetProperties.instance('asset_properties', assetKey, 'blue', 35, randomBytes) },
});
if (resultString.length !== 0) {
const result = Asset.parse(resultString);
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}`);
} else { } else {
console.log(`*** Failed: VerifyAssetProperties, private information about asset ${assetKey} has not been verified by ${org}`); console.log(`*** Failed: VerifyAssetProperties, private information about asset ${assetKey} has not been verified by ${org}`);
} }
} else {
console.log(`*** Failed: VerifyAssetProperties, private information about asset ${assetKey} has not been verified by ${org}`);
}
} catch (e) { } catch (e) {
console.log(`${RED}*** Failed: VerifyAssetProperties - ${e}${RESET}`); console.log(`${RED}*** Failed: VerifyAssetProperties - ${e}${RESET}`);
} }
@ -287,10 +262,7 @@ async function agreeToBuy(contract:ContractWrapper, assetKey:string, org:string,
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.submit('AgreeToBuy',{ await contract.agreeToBuy( {asset_id:assetKey, price, trade_id: now.toString()});
arguments:[assetKey],
transientData: { asset_price: AssetPrice.instance(assetKey,price,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) {
@ -302,11 +274,11 @@ async function readSalePrice(contract:ContractWrapper, assetKey:string, org:stri
try { try {
console.log(`${GREEN}--> Evaluate Transaction: GetAssetSalesPrice, - ${assetKey} from organization ${org}${RESET}`); console.log(`${GREEN}--> Evaluate Transaction: GetAssetSalesPrice, - ${assetKey} from organization ${org}${RESET}`);
if(org === mspIdOrg2){ if(org === mspIdOrg2){
console.log(`${GREEN}* Expected to fail as ${org} has not set a sale price${RESET}`) console.log(`${GREEN}* Expected to fail as ${org} has not set a sale price${RESET}`);
} }
const resultString = await contract.evaluateTransaction('GetAssetSalesPrice', assetKey); const result = await contract.getAssetSalesPrice(assetKey);
const result = Asset.parse(resultString);
console.log('*** Result: GetAssetSalesPrice', result); console.log('*** Result: GetAssetSalesPrice', result);
} catch (e) { } catch (e) {
console.log(`${RED}*** Failed evaluateTransaction GetAssetSalesPrice: ${e}${RESET}`); console.log(`${RED}*** Failed evaluateTransaction GetAssetSalesPrice: ${e}${RESET}`);
@ -317,11 +289,10 @@ async function readBidPrice(contract:ContractWrapper, assetKey:string, org:strin
try{ try{
console.log(`${GREEN}--> Evaluate Transaction: GetAssetBidPrice, - ${assetKey} from organization ${org}${RESET}`); console.log(`${GREEN}--> Evaluate Transaction: GetAssetBidPrice, - ${assetKey} from organization ${org}${RESET}`);
if(org === mspIdOrg1){ if(org === mspIdOrg1){
console.log(`${GREEN}* Expected to fail as Org1 has not agreed to buy${RESET}`) console.log(`${GREEN}* Expected to fail as Org1 has not agreed to buy${RESET}`);
} }
const resultString = await contract.evaluateTransaction('GetAssetBidPrice', assetKey); const result = await contract.getAssetBidPrice(assetKey);
const result = Asset.parse(resultString);
console.log('*** Result: GetAssetBidPrice', result); console.log('*** Result: GetAssetBidPrice', result);
} catch (e) { } catch (e) {
@ -331,21 +302,22 @@ async function readBidPrice(contract:ContractWrapper, assetKey:string, org:strin
async function transferAsset(contract: ContractWrapper, assetKey: string, org: string, buyerOrgID: string, price: number): Promise<void> { async function transferAsset(contract: ContractWrapper, assetKey: string, org: string, buyerOrgID: string, price: number): Promise<void> {
try { try {
console.log(`${GREEN}--> Submit Transaction: TransferAsset, ${assetKey} as ${org} - endorsed by ${org}${RESET}`); console.log(`${GREEN}--> Submit Transaction: TransferAsset, ${assetKey} as ${org} - endorsed by ${org}${RESET}`);
if(org === mspIdOrg2){ if(org === mspIdOrg2){
console.log(`${GREEN}* Expected to fail as the owner is Org1${RESET}`) console.log(`${GREEN}* Expected to fail as the owner is Org1${RESET}`);
}else if(price === 110){ }else if(price === 110){
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}`);
} }
await contract.submit('TransferAsset', { const assetProperties: AssetProperties =
arguments:[assetKey,buyerOrgID], { object_type:'asset_properties',
transientData: { asset_id:assetKey,
asset_properties: AssetProperties.instance('asset_properties', assetKey, 'blue', 35, randomBytes), color:'blue',
asset_price: AssetPrice.instance(assetKey, price, now.toString()) }, size:35,
endorsingOrganizations:[mspIdOrg1,mspIdOrg2] salt:randomBytes
}); };
const assetPrice: AssetPrice = {asset_id: assetKey, price, trade_id:now.toString()};
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}`);
} catch (e) { } catch (e) {
@ -357,10 +329,10 @@ async function readPrivateAssetAfterTransfer(contract:ContractWrapper, assetKey:
try{ try{
console.log(`${GREEN}--> Evaluate Transaction: GetAssetPrivateProperties, - ${assetKey} from organization ${org}${RESET}`); console.log(`${GREEN}--> Evaluate Transaction: GetAssetPrivateProperties, - ${assetKey} from organization ${org}${RESET}`);
if(org === mspIdOrg1) { if(org === mspIdOrg1) {
console.log(`${GREEN}* Expected to fail as ${org} is not the owner and does not have the private details${RESET}`) console.log(`${GREEN}* Expected to fail as ${org} is not the owner and does not have the private details${RESET}`);
} }
const resultString = await contract.evaluateTransaction('GetAssetPrivateProperties', assetKey);
const result = Asset.parse(resultString); const result = await contract.getAssetPrivateProperties(assetKey);
console.log('*** Result:', result); console.log('*** Result:', result);
} }
@ -373,12 +345,10 @@ async function changePublicDescriptionAfterTransfer(contract:ContractWrapper, as
try { try {
console.log(`${GREEN}--> Submit Transaction: changePublicDescription ${assetKey}, as ${org} - endorse by ${org}${RESET}`); console.log(`${GREEN}--> Submit Transaction: changePublicDescription ${assetKey}, as ${org} - endorse by ${org}${RESET}`);
if(org === mspIdOrg1) { if(org === mspIdOrg1) {
console.log(`${GREEN}* Expected to fail as ${org} is not the owner${RESET}`) console.log(`${GREEN}* Expected to fail as ${org} is not the owner${RESET}`);
} }
await contract.submit('ChangePublicDescription', { await contract.changePublicDescription(assetKey, description);
arguments:[assetKey,description],
});
console.log(`*** Result: committed, asset ${assetKey} is now for sale by ${org}`); console.log(`*** Result: committed, asset ${assetKey} is now for sale by ${org}`);
} catch (e) { } catch (e) {

View file

@ -15,7 +15,7 @@ export const mspIdOrg1 = 'Org1MSP';
export const mspIdOrg2 = 'Org2MSP'; export const mspIdOrg2 = 'Org2MSP';
// Path to org1 crypto materials. // Path to org1 crypto materials.
export const cryptoPathOrg1 = path.resolve(__dirname, '..', '..', '..', '../../sample-2.4.3/fabric-samples/test-network', 'organizations', 'peerOrganizations', 'org1.example.com'); export const cryptoPathOrg1 = path.resolve(__dirname, '..', '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com');
// Path to user private key directory. // Path to user private key directory.
export const keyDirectoryPathOrg1 = path.resolve(cryptoPathOrg1, 'users', 'User1@org1.example.com', 'msp', 'keystore'); export const keyDirectoryPathOrg1 = path.resolve(cryptoPathOrg1, 'users', 'User1@org1.example.com', 'msp', 'keystore');
@ -32,7 +32,7 @@ export const cryptoPathOrg2 = path.resolve(
'..', '..',
'..', '..',
'..', '..',
'../../sample-2.4.3/fabric-samples/test-network', 'test-network',
'organizations', 'organizations',
'peerOrganizations', 'peerOrganizations',
'org2.example.com' 'org2.example.com'

View file

@ -3,8 +3,9 @@
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
import { Contract, ProposalOptions } 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';
export class ContractWrapper { export class ContractWrapper {
@ -15,19 +16,81 @@ export class ContractWrapper {
this.contract = contract; this.contract = contract;
} }
public async submit(transactionName:string, options: ProposalOptions ) { public async createAsset(org: string, assetKey: string, assetProperties: AssetProperties): Promise<void> {
await this.contract?.submit(transactionName, options); await this.contract?.submit('CreateAsset', {
return; arguments: [assetKey, `Asset ${assetKey} owned by ${org} is not for sale`],
transientData: { asset_properties: JSON.stringify(assetProperties)},
});
} }
public async evaluate(transactionName:string, options: ProposalOptions): Promise<string> { public async readAsset( assetKey: string): Promise<Asset> {
const resultBytes = await this.contract?.evaluate(transactionName, options); const resultBytes = await this.contract?.evaluateTransaction('ReadAsset', assetKey);
const result = this.utf8Decoder.decode(resultBytes); const result = this.utf8Decoder.decode(resultBytes);
return result; if (result.length !== 0) {
return parse(result);
} else {
throw new Error('No Asset Found');
}
} }
public async evaluateTransaction(transactionName:string, ...args: (string | Uint8Array)[]): Promise<string> { public async getAssetPrivateProperties( assetKey: string ): Promise<AssetProperties> {
const resultBytes = await this.contract?.evaluateTransaction(transactionName, ...args); const resultBytes = await this.contract?.evaluateTransaction('GetAssetPrivateProperties', assetKey);
return this.utf8Decoder.decode(resultBytes); const result = this.utf8Decoder.decode(resultBytes);
return parse<AssetProperties>(result);
}
public async changePublicDescription(assetKey: string, description: string): Promise<void> {
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],
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],
transientData: {asset_properties: JSON.stringify(assetProperties)},
});
const result = this.utf8Decoder.decode(resultBytes);
if (result.length !== 0) {
return parse<AssetProperties>(result);
} else {
throw new Error(`Private information about asset ${assetProperties.asset_id} has not been verified by ${org}`);
}
}
public async agreeToBuy(asset_price: AssetPrice): Promise<void> {
await this.contract?.submit('AgreeToBuy', {
arguments:[asset_price.asset_id],
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);
}
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);
}
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],
transientData: {
asset_properties: JSON.stringify(asset_properties),
asset_price: JSON.stringify(asset_price)},
endorsingOrganizations:endorsingOrganizations
});
} }
} }

View file

@ -8,105 +8,27 @@ 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 Asset {
objectType: string, objectType: string;
assetID: string, assetID: string;
ownerOrg: string, ownerOrg: string;
publicDescription: string, publicDescription: string;
} }
export interface asset_properties { export interface AssetProperties {
object_type: string, object_type: string;
asset_id: string, asset_id: string;
color: string, color: string;
size: number, size: number;
salt: string salt: string;
} }
export interface asset_price { export interface AssetPrice {
asset_id: string, asset_id: string;
price: number, price: number;
trade_id: string trade_id: string;
}
export interface asset_price {
asset_id: string,
price: number,
trade_id: string
}
export class AssetProperties {
asset_properties: asset_properties;
private constructor (asset_properties: asset_properties) {
this.asset_properties = asset_properties;
}
static instance(object_type: string, asset_id: string, color: string, size: number, salt: string): string {
const asset_properties : asset_properties = {
object_type,
asset_id,
color,
size,
salt,
};
return new AssetProperties(asset_properties).serialize();
}
private serialize(): string {
return JSON.stringify(this.asset_properties);
}
}
export class Asset {
asset: asset;
private constructor (asset: asset) {
this.asset = asset;
}
static instance( objectType: string, assetID: string, ownerOrg: string, publicDescription: string): string {
const asset = {
objectType,
assetID,
ownerOrg,
publicDescription
}
return new Asset(asset).serialize();
}
private serialize(): string {
return JSON.stringify(this.asset);
}
static parse(asset: string): asset {
return JSON.parse(asset);
}
}
export class AssetPrice {
asset_price: asset_price;
constructor (asset_price : asset_price) {
this.asset_price = asset_price;
}
static instance(asset_id: string, price:number, trade_id: string): string {
const asset_price = {
asset_id,
price,
trade_id
}
return new AssetPrice(asset_price).serialize()
}
private serialize(): string {
return JSON.stringify(this.asset_price);
} }
export function parse<T>(data: string): T {
return JSON.parse(data);
} }