Skip to content

Commit

Permalink
Merge branch 'develop' into masterismail/issue213
Browse files Browse the repository at this point in the history
  • Loading branch information
masterismail committed Apr 30, 2024
2 parents 0708c96 + 7b2d45d commit f202375
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 53 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
4 changes: 2 additions & 2 deletions demo/src/welcome-developer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function main() {

const authorIdentity = Cord.Utils.Crypto.makeKeypairFromUri(
authorAnchorUri,
"ed25519"
"sr25519"
);

await addNetworkMember(authorityAuthorIdentity, authorIdentity.address);
Expand All @@ -56,7 +56,7 @@ async function main() {
);
const issuerKeys = Cord.Utils.Keys.generateKeypairs(
issuerMnemonic,
"ed25519"
"sr25519"
);

const { mnemonic: delegateTwoMnemonic, document: delegateTwoDid } =
Expand Down
152 changes: 101 additions & 51 deletions packages/asset/src/Asset.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,52 @@ export async function isAssetStored(assetUri: AssetUri): Promise<boolean> {
}
}

export async function dispatchCreateToChain(
export async function prepareCreateExtrinsic(
assetEntry: IAssetEntry,
authorAccount: CordKeyringPair,
authorizationUri: AuthorizationUri,
signCallback: SignExtrinsicCallback
): Promise<AssetUri> {
): Promise<SubmittableExtrinsic> {
try {
const api = ConfigService.get('api')
const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri)
const api = ConfigService.get('api');
const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri);

const tx = api.tx.asset.create(
assetEntry.entry,
assetEntry.digest,
authorizationId
)
);

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

return extrinsic;
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : JSON.stringify(error);
throw new SDKErrors.CordDispatchError(
`Error preparing extrinsic: "${errorMessage}".`
);
}
}

export async function dispatchCreateToChain(
assetEntry: IAssetEntry,
authorAccount: CordKeyringPair,
authorizationUri: AuthorizationUri,
signCallback: SignExtrinsicCallback
): Promise<AssetUri> {
try {

const extrinsic = await prepareCreateExtrinsic(
assetEntry,
authorAccount,
authorizationUri,
signCallback
)

await Chain.signAndSubmitTx(extrinsic, authorAccount)
Expand Down Expand Up @@ -308,16 +333,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 @@ -328,58 +357,69 @@ 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,
Expand All @@ -399,52 +439,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 f202375

Please sign in to comment.