Skip to content

Commit

Permalink
asset: Fix errors & implement status_change
Browse files Browse the repository at this point in the history
Signed-off-by: Shreevatsa N <vatsa@dhiway.com>
  • Loading branch information
vatsa287 committed Apr 30, 2024
1 parent 6f274c1 commit 60a4c7b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 49 deletions.
17 changes: 17 additions & 0 deletions demo/src/asset-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ async function main() {
)

console.log("✅ Asset transferred!");

// Step 5: Change status of Asset
console.log(`\n❄️ Change status of Asset from 'Active' to 'Inactive' Action`);

const statusChangeExtrinsic = await Cord.Asset.dispatchAssetStatusChangeToChain(
assetEntry.uri,
issuerDid.uri,
networkAuthorityIdentity,
Cord.AssetStatusOf.inactive,
async ({ data }) => ({
signature: issuerKeys.authentication.sign(data),
keyType: issuerKeys.authentication.type,
}),
assetIssuance.uri
)

console.log("✅ Asset status changed!");
}
main()
.then(() => console.log("\nBye! 👋 👋 👋 "))
Expand Down
17 changes: 17 additions & 0 deletions demo/src/asset-vc-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,23 @@ async function main() {
)

console.log("✅ Asset transferred!");

// Step 5: Change status of Asset
console.log(`\n❄️ Change status of Asset from 'Active' to 'Inactive' Action`);

const statusChangeExtrinsic = await Cord.Asset.dispatchAssetStatusChangeVcToChain(
assetVcEntry.uri,
issuerDid.uri,
networkAuthorityIdentity,
Cord.AssetStatusOf.inactive,
async ({ data }) => ({
signature: issuerKeys.authentication.sign(data),
keyType: issuerKeys.authentication.type,
}),
assetIssuance.uri
)

console.log("✅ Asset status changed!");
}
main()
.then(() => console.log("\nBye! 👋 👋 👋 "))
Expand Down
127 changes: 78 additions & 49 deletions packages/asset/src/Asset.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,20 @@ export async function dispatchTransferVcToChain(
}

export async function dispatchAssetStatusChangeToChain(
assetId: AssetUri,
assetUri: AssetUri,
assetIssuerDidUri: DidUri,
authorAccount: CordKeyringPair,
newStatus: PalletAssetAssetStatusOf,
signCallback: SignExtrinsicCallback,
assetInstanceId?: string
): Promise<void> {
try {
const api = ConfigService.get('api')
let tx
const api = ConfigService.get("api");
let tx;
const assetId = uriToIdentifier(assetUri);
const assetIssuerDid = Did.toChain(assetIssuerDidUri);

assetInstanceId = assetInstanceId?.split(":").pop();

/* Check if assetStatusType is undefined */
if (newStatus === undefined) {
Expand All @@ -305,68 +309,83 @@ export async function dispatchAssetStatusChangeToChain(
let encodedAssetInstanceDetail = await api.query.asset.issuance(
assetId,
assetInstanceId
)
);
if (encodedAssetInstanceDetail.isNone) {
throw new SDKErrors.AssetInstanceNotFound(
`Error: Assset Instance Not Found`
)
`Error: Asset Instance Not Found`
);
}
let assetInstanceDetail = JSON.parse(
encodedAssetInstanceDetail.toString()
)
if (assetIssuerDidUri !== assetInstanceDetail.assetInstanceIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Assset issuer mismatch`)
);
if (assetIssuerDid !== assetInstanceDetail.assetInstanceIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Asset issuer mismatch`);
}
if (assetInstanceDetail.assetInstanceStatus === newStatus) {

if (
assetInstanceDetail.assetInstanceStatus?.toLowerCase() ===
String(newStatus)?.toLowerCase()
) {
throw new SDKErrors.AssetStatusError(
`Error: Asset Instance is already in the ${newStatus} state`
)
);
}
tx = api.tx.asset.statusChange(assetId, assetInstanceId, newStatus)
tx = api.tx.asset.statusChange(assetId, assetInstanceId, newStatus);
} else {
let encodedAssetDetail = await api.query.asset.assets(assetId)
let encodedAssetDetail = await api.query.asset.assets(assetId);
if (encodedAssetDetail.isNone) {
throw new SDKErrors.AssetNotFound(`Error: Assset Not Found`)
throw new SDKErrors.AssetNotFound(`Error: Asset Not Found`);
}
let assetDetail = JSON.parse(encodedAssetDetail.toString())
if (assetIssuerDidUri !== assetDetail.assetIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Assset issuer mismatch`)
let assetDetail = JSON.parse(encodedAssetDetail.toString());

if (assetIssuerDid !== assetDetail.assetIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Asset issuer mismatch`);
}
if (assetDetail.assetInstanceStatus === newStatus) {

if (
assetDetail.assetStatus?.toLowerCase() ===
String(newStatus)?.toLowerCase()
) {
throw new SDKErrors.AssetStatusError(
`Error: Asset is already in the ${newStatus} state`
)
);
}
tx = api.tx.asset.statusChange(assetId, null, newStatus)
tx = api.tx.asset.statusChange(assetId, null, newStatus);
}

const extrinsic = await Did.authorizeTx(
assetIssuerDidUri,
tx,
signCallback,
authorAccount.address
)
);

await Chain.signAndSubmitTx(extrinsic, authorAccount)
await Chain.signAndSubmitTx(extrinsic, authorAccount);
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error);
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${error}".`
)
`Error dispatching to chain: "${errorMessage}".`
);
}
}

export async function dispatchAssetStatusChangeVcToChain(
assetId: AssetUri,
assetUri: AssetUri,
assetIssuerDidUri: DidUri,
authorAccount: CordKeyringPair,
newStatus: PalletAssetAssetStatusOf,
signCallback: SignExtrinsicCallback,
assetInstanceId?: string
): Promise<void> {
try {
const api = ConfigService.get('api')
let tx

const api = ConfigService.get("api");
let tx;
const assetId = uriToIdentifier(assetUri);
const assetIssuerDid = Did.toChain(assetIssuerDidUri);

assetInstanceId = assetInstanceId?.split(":").pop();

/* Check if assetStatusType is undefined */
if (newStatus === undefined) {
throw new SDKErrors.InvalidAssetStatus("Asset status is undefined.");
Expand All @@ -376,52 +395,62 @@ export async function dispatchAssetStatusChangeVcToChain(
let encodedAssetInstanceDetail = await api.query.asset.vcIssuance(
assetId,
assetInstanceId
)
);
if (encodedAssetInstanceDetail.isNone) {
throw new SDKErrors.AssetInstanceNotFound(
`Error: Assset Instance Not Found`
)
`Error: Asset Instance Not Found`
);
}
let assetInstanceDetail = JSON.parse(
encodedAssetInstanceDetail.toString()
)
if (assetIssuerDidUri !== assetInstanceDetail.assetInstanceIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Assset issuer mismatch`)
);
if (assetIssuerDid !== assetInstanceDetail.assetInstanceIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Asset issuer mismatch`);
}
if (assetInstanceDetail.assetInstanceStatus === newStatus) {
if (
assetInstanceDetail.assetInstanceStatus?.toLowerCase() ===
String(newStatus)?.toLowerCase()
) {
throw new SDKErrors.AssetStatusError(
`Error: Asset Instance is already in the ${newStatus} state`
)
);
}
tx = api.tx.asset.statusChange(assetId, assetInstanceId, newStatus)
tx = api.tx.asset.statusChange(assetId, assetInstanceId, newStatus);
} else {
let encodedAssetDetail = await api.query.asset.assets(assetId)
let encodedAssetDetail = await api.query.asset.assets(assetId);

if (encodedAssetDetail.isNone) {
throw new SDKErrors.AssetNotFound(`Error: Assset Not Found`)
throw new SDKErrors.AssetNotFound(`Error: Asset Not Found`);
}
let assetDetail = JSON.parse(encodedAssetDetail.toString())
if (assetIssuerDidUri !== assetDetail.assetIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Assset issuer mismatch`)
let assetDetail = JSON.parse(encodedAssetDetail.toString());

if (assetIssuerDid !== assetDetail.assetIssuer) {
throw new SDKErrors.AssetIssuerMismatch(`Error: Asset issuer mismatch`);
}
if (assetDetail.assetInstanceStatus === newStatus) {
if (
assetDetail.assetStatus?.toLowerCase() ===
String(newStatus)?.toLowerCase()
) {
throw new SDKErrors.AssetStatusError(
`Error: Asset is already in the ${newStatus} state`
)
);
}
tx = api.tx.asset.statusChange(assetId, null, newStatus)
tx = api.tx.asset.statusChange(assetId, null, newStatus);
}

const extrinsic = await Did.authorizeTx(
assetIssuerDidUri,
tx,
signCallback,
authorAccount.address
)
);

await Chain.signAndSubmitTx(extrinsic, authorAccount)
await Chain.signAndSubmitTx(extrinsic, authorAccount);
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error);
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${error}".`
)
`Error dispatching to chain: "${errorMessage}".`
);
}
}

0 comments on commit 60a4c7b

Please sign in to comment.