diff --git a/demo/src/network-score-test.ts b/demo/src/network-score-test.ts index 9767c763..f3dd118b 100644 --- a/demo/src/network-score-test.ts +++ b/demo/src/network-score-test.ts @@ -27,20 +27,20 @@ async function main() { const { mnemonic: chainSpaceAdminMnemonic, document: chainSpaceAdminDid } = await createDid(networkAuthorIdentity) - const chainSpaceAdminKeys = Cord.Utils.Keys.generateKeypairs(chainSpaceAdminMnemonic) + const chainSpaceAdminKeys = Cord.Utils.Keys.generateKeypairs(chainSpaceAdminMnemonic,"sr25519") console.log( `šŸ” Network Score Admin (${chainSpaceAdminDid.authentication[0].type}): ${chainSpaceAdminDid.uri}` ) const { mnemonic: networkProviderMnemonic, document: networkProviderDid } = await createDid(networkAuthorIdentity) - const networkProviderKeys = Cord.Utils.Keys.generateKeypairs(networkProviderMnemonic) + const networkProviderKeys = Cord.Utils.Keys.generateKeypairs(networkProviderMnemonic,"sr25519") console.log( `šŸ” Network Participant (Provider) (${networkProviderDid.authentication[0].type}): ${networkProviderDid.uri}` ) const { mnemonic: networkAuthorMnemonic, document: networkAuthorDid } = await createDid(networkAuthorIdentity) - const networkAuthorKeys = Cord.Utils.Keys.generateKeypairs(networkAuthorMnemonic) + const networkAuthorKeys = Cord.Utils.Keys.generateKeypairs(networkAuthorMnemonic,"sr25519") console.log( `šŸ” Network Author (API -> Node) (${networkAuthorDid.authentication[0].type}): ${networkAuthorDid.uri}` ) @@ -135,34 +135,36 @@ async function main() { console.log(`\nā³ Network Rating Transaction Flow`) console.log(`\nšŸ’  Write Rating - (Genesis) Credit Entry `) - let ratingContent: IRatingContent = { - entityUid: Cord.Utils.UUID.generate(), - entityId: 'Gupta Kirana Store', - providerUid: Cord.Utils.UUID.generate(), - providerId: 'GoFrugal', - entityType: Cord.EntityTypeOf.retail, - ratingType: Cord.RatingTypeOf.overall, - countOfTxn: 100, - totalRating: 320, + let ratingContent: any = { + entity_id: Cord.Utils.UUID.generate(), + entity_name: 'Gupta Kirana Store', + provider_id: 'GoFrugal', + rating_type: Cord.RatingTypeOf.overall, + count_of_txn: 100, + total_rating: 320, } - console.dir(ratingContent, { - depth: null, - colors: true, - }) - const entryDigest = Cord.Utils.Crypto.hashObjectAsHexStr(ratingContent); - const { totalRating, ...restOfRating} = ratingContent; + // console.dir(ratingContent, { + // depth: null, + // colors: true, + // }) + + + const entry_digest = Cord.Utils.Crypto.hashObjectAsHexStr(ratingContent); + const { total_rating, ...restOfRating} = ratingContent; - let transformedEntry: IRatingEntry = { + let transformedEntry: Cord.IRatingEntry = { entry: { ...restOfRating, - providerDid: networkProviderDid.uri.replace('did:cord:', ''), - totalEncodedRating: Math.round(totalRating * 10), + provider_did: networkProviderDid.uri.replace('did:cord:', ''), + total_encoded_rating: Math.round(total_rating * 10), }, - messageId: Cord.Utils.UUID.generate(), - entryDigest, + message_id: Cord.Utils.UUID.generate(), + entry_digest, }; + console.log('166 transformedEntry\n',transformedEntry) + console.log(`\nšŸŒ Rating Information to API endpoint (/write-ratings) `) console.dir(transformedEntry, { depth: null, @@ -200,7 +202,7 @@ async function main() { console.log(`\nšŸ’  Revoke Rating - Debit Entry `) const revokeInput = { entryUri: ratingUri, - entityUid: transformedEntry.entry.entityUid, + entity_id: transformedEntry.entry.entity_id, } console.dir(revokeInput, { depth: null, @@ -216,14 +218,14 @@ async function main() { const revokeDigest = Cord.Utils.Crypto.hashObjectAsHexStr(entryTransform) - const revokeRatingEntry: IRatingRevokeEntry = { + const revokeRatingEntry: Cord.IRatingRevokeEntry = { entry: { - messageId: msgId, - entryDigest: revokeDigest, - referenceId: ratingUri, + message_id: msgId, + entry_digest: revokeDigest, + reference_id: ratingUri, }, - entityUid: transformedEntry.entry.entityUid, - providerDid: networkProviderDid.uri, + entity_id: transformedEntry.entry.entity_id, + provider_did: networkProviderDid.uri, } console.log( @@ -268,10 +270,10 @@ async function main() { let revisedRatingContent = { ...ratingContent, - providerDid: transformedEntry.entry.providerDid, - referenceId: revokedRatingUri, - countOfTxn: 80, - totalRating: 280, + provider_did: transformedEntry.entry.providerDid, + reference_id: revokedRatingUri, + count_of_txn: 80, + total_rating: 280, } console.dir(revisedRatingContent, { @@ -284,12 +286,12 @@ async function main() { let transformedRevisedEntry = { entry: { ...revisedRatingContent, - referenceId: revokedRatingUri, - totalEncodedRating: Math.round(revisedRatingContent.totalRating * 10), + reference_id: revokedRatingUri, + total_encoded_rating: Math.round(revisedRatingContent.totalRating * 10), }, - messageId: Cord.Utils.UUID.generate(), - referenceId: revokedRatingUri, - entryDigest: revisedEntryDigest, + message_id: Cord.Utils.UUID.generate(), + reference_id: revokedRatingUri, + entry_digest: revisedEntryDigest, }; delete transformedRevisedEntry.entry.totalRating; @@ -342,7 +344,7 @@ async function main() { console.log(`\nšŸŒ Query From Chain - Aggregate Score `) const aggregateScoreFromChain = await Cord.Score.fetchEntityAggregateScorefromChain( - ratingContent.entityUid, + ratingContent.entity_id, Cord.RatingTypeOf.overall ) console.dir(aggregateScoreFromChain, { diff --git a/packages/network-score/src/Scoring.chain.ts b/packages/network-score/src/Scoring.chain.ts index 5b37206d..6d7f81c6 100644 --- a/packages/network-score/src/Scoring.chain.ts +++ b/packages/network-score/src/Scoring.chain.ts @@ -155,8 +155,8 @@ export async function dispatchRatingToChain( const tx = api.tx.networkScore.registerRating( ratingEntry.entry, - ratingEntry.entryDigest, - ratingEntry.messageId, + ratingEntry.entry_digest, + ratingEntry.message_id, authorizationId ) @@ -228,7 +228,7 @@ export async function dispatchRevokeRatingToChain( const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri) const exists = await isRatingStored( - ratingEntry.entry.referenceId as RatingEntryUri + ratingEntry.entry.reference_id as RatingEntryUri ) if (!exists) { @@ -236,13 +236,13 @@ export async function dispatchRevokeRatingToChain( } const ratingEntryId: RatingEntryId = uriToIdentifier( - ratingEntry.entry.referenceId + ratingEntry.entry.reference_id ) const tx = api.tx.networkScore.revokeRating( ratingEntryId, - ratingEntry.messageId, - ratingEntry.entryDigest, + ratingEntry.message_id, + ratingEntry.entry_digest, authorizationId ) @@ -313,7 +313,7 @@ export async function dispatchReviseRatingToChain( const api = ConfigService.get('api') const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri) const refEntryId: RatingEntryId = uriToIdentifier( - ratingEntry.entry.referenceId + ratingEntry.entry.reference_id ) const exists = await isRatingStored(ratingEntry.entryUri) @@ -323,8 +323,8 @@ export async function dispatchReviseRatingToChain( const tx = api.tx.networkScore.reviseRating( ratingEntry.entry, - ratingEntry.entryDigest, - ratingEntry.messageId, + ratingEntry.entry_digest, + ratingEntry.message_id, refEntryId, authorizationId ) @@ -365,10 +365,10 @@ function extractEnumIndex(enumObject: { index: number }): number { return enumObject.index } // TypeScript Enum Mappings -const EntityTypeMapping: Record = { - 0: EntityTypeOf.retail, - 1: EntityTypeOf.logistic, -} +// const EntityTypeMapping: Record = { +// 0: EntityTypeOf.retail, +// 1: EntityTypeOf.logistic, +// } const RatingTypeMapping: Record = { 0: RatingTypeOf.overall, @@ -391,12 +391,12 @@ const EntryTypeMapping: Record = { * * @internal */ -function decodeEntityType( - encodedType: PalletNetworkScoreEntityTypeOf -): EntityTypeOf { - const index = extractEnumIndex(encodedType) - return EntityTypeMapping[index] -} +// function decodeEntityType( +// encodedType: PalletNetworkScoreEntityTypeOf +// ): EntityTypeOf { +// const index = extractEnumIndex(encodedType) +// return EntityTypeMapping[index] +// } /** * Decodes an encoded rating type to its corresponding RatingTypeOf value. @@ -476,16 +476,15 @@ function decodeEntryDetailsfromChain( const chainEntry = encoded.unwrap() const encodedEntry = chainEntry.entry const decodedEntry: IRatingChainEntryDetails = { - entityUid: DecoderUtils.hexToString(encodedEntry.entityUid.toString()), - providerUid: DecoderUtils.hexToString(encodedEntry.providerUid.toString()), - entityType: decodeEntityType(encodedEntry.entityType), - ratingType: decodeRatingType(encodedEntry.ratingType), - countOfTxn: encodedEntry.countOfTxn.toNumber(), - totalRating: decodeRatingValue(encodedEntry.totalEncodedRating.toNumber()), + entity_id: DecoderUtils.hexToString(encodedEntry.entityUid.toString()), + provider_id: DecoderUtils.hexToString(encodedEntry.providerUid.toString()), + rating_type: decodeRatingType(encodedEntry.ratingType), + count_of_txn: encodedEntry.countOfTxn.toNumber(), + total_rating: decodeRatingValue(encodedEntry.totalEncodedRating.toNumber()), } - let referenceId: RatingEntryUri | undefined + let reference_id: RatingEntryUri | undefined if (chainEntry.referenceId.isSome) { - referenceId = identifierToUri( + reference_id = identifierToUri( DecoderUtils.hexToString(chainEntry.referenceId.unwrap().toString()) ) as RatingEntryUri } @@ -494,13 +493,12 @@ function decodeEntryDetailsfromChain( entryUri: identifierToUri(stmtUri) as RatingEntryUri, entry: decodedEntry, digest: chainEntry.digest.toHex(), - messageId: DecoderUtils.hexToString(chainEntry.messageId.toString()), + message_id: DecoderUtils.hexToString(chainEntry.messageId.toString()), space: identifierToUri( DecoderUtils.hexToString(chainEntry.space.toString()) ), creatorUri: Did.fromChain(chainEntry.creatorId), - entryType: decodeEntryType(chainEntry.entryType), - referenceId, + reference_id, createdAt: DataUtils.convertUnixTimeToDateTime( chainEntry.createdAt.toNumber(), timeZone @@ -608,10 +606,10 @@ export async function fetchEntityAggregateScorefromChain( if (!specificItem.isNone) { const value: PalletNetworkScoreAggregatedEntryOf = specificItem.unwrap() decodedEntries.push({ - entityUid: entity, - ratingType: ratingType.toString() as RatingTypeOf, - countOfTxn: value.countOfTxn.toNumber(), - totalRating: decodeRatingValue(value.totalEncodedRating.toNumber()), + entity_id: entity, + rating_type: ratingType.toString() as RatingTypeOf, + count_of_txn: value.countOfTxn.toNumber(), + total_rating: decodeRatingValue(value.totalEncodedRating.toNumber()), }) } } else { @@ -621,10 +619,10 @@ export async function fetchEntityAggregateScorefromChain( const value: PalletNetworkScoreAggregatedEntryOf = optionValue.unwrap() const [decodedEntityUri, decodedRatingType] = compositeKey.args decodedEntries.push({ - entityUid: DecoderUtils.hexToString(decodedEntityUri.toString()), - ratingType: decodeRatingType(decodedRatingType), - countOfTxn: value.countOfTxn.toNumber(), - totalRating: decodeRatingValue(value.totalEncodedRating.toNumber()), + entity_id: DecoderUtils.hexToString(decodedEntityUri.toString()), + rating_type: decodeRatingType(decodedRatingType), + count_of_txn: value.countOfTxn.toNumber(), + total_rating: decodeRatingValue(value.totalEncodedRating.toNumber()), }) } }) diff --git a/packages/network-score/src/Scoring.ts b/packages/network-score/src/Scoring.ts index 0bf337d3..8b937575 100644 --- a/packages/network-score/src/Scoring.ts +++ b/packages/network-score/src/Scoring.ts @@ -266,19 +266,19 @@ function validateHexString(entryDigest: string): void { * @internal */ async function createRatingObject( - entryDigest: HexString, - entityUid: string, - messageId: string, + entry_digest: HexString, + entity_id: string, + message_id: string, chainSpace: SpaceUri, - providerUri: DidUri, + provider_uri: DidUri, authorUri: DidUri ): Promise<{ uri: RatingEntryUri; details: any }> { const ratingUri = await getUriForRatingEntry( - entryDigest, - entityUid, - messageId, + entry_digest, + entity_id, + message_id, chainSpace, - providerUri + provider_uri ) return { @@ -286,8 +286,8 @@ async function createRatingObject( details: { entryUri: ratingUri, chainSpace, - messageId, - entryDigest, + message_id, + entry_digest, authorUri, }, } @@ -354,25 +354,32 @@ export async function buildFromRatingProperties( validateRequiredFields([ chainSpace, authorUri, - rating.messageId, - rating.entryDigest, + rating.message_id, + rating.entry_digest, + rating.entry.entity_id, + rating.entry.provider_id, + rating.entry.rating_type, + rating.entry.count_of_txn, + rating.entry.total_encoded_rating, + rating.entry.provider_did ]) - validateHexString(rating.entryDigest) + validateHexString(rating.entry_digest) const { uri, details } = await createRatingObject( - rating.entryDigest, - rating.entry.entityUid, - rating.messageId, + rating.entry_digest, + rating.entry.entity_id, + rating.message_id, chainSpace, - Did.getDidUri(rating.entry.providerDid), + Did.getDidUri(rating.entry.provider_did), authorUri ) - const { providerId, entityId, ...chainEntry } = rating.entry + const { provider_id, entity_id, ...chainEntry } = rating.entry details.entry = chainEntry - + console.log('\n\n\ndetails',details) return { uri, details } + } catch (error) { throw new SDKErrors.RatingPropertiesError( `Rating content transformation error: "${error}".` @@ -431,17 +438,17 @@ export async function buildFromRevokeRatingProperties( validateRequiredFields([ chainSpace, authorUri, - rating.entry.messageId, - rating.entry.entryDigest, + rating.entry.message_id, + rating.entry.entry_digest, ]) - validateHexString(rating.entry.entryDigest) + validateHexString(rating.entry.entry_digest) const { uri, details } = await createRatingObject( - rating.entry.entryDigest, - rating.entityUid, - rating.entry.messageId, + rating.entry.entry_digest, + rating.entity_id, + rating.entry.message_id, chainSpace, - Did.getDidUri(rating.providerDid), + Did.getDidUri(rating.provider_did), authorUri ) @@ -496,22 +503,22 @@ export async function buildFromReviseRatingProperties( validateRequiredFields([ chainSpace, authorUri, - rating.referenceId, - rating.entry.countOfTxn, - rating.entry.totalEncodedRating, + rating.reference_id, + rating.entry.count_of_txn, + rating.entry.total_encoded_rating, ]) - validateHexString(rating.entryDigest) + validateHexString(rating.entry_digest) const { uri, details } = await createRatingObject( - rating.entryDigest, - rating.entry.entityUid, - rating.messageId, + rating.entry_digest, + rating.entry.entity_id, + rating.message_id, chainSpace, - Did.getDidUri(rating.entry.providerDid), + Did.getDidUri(rating.entry.provider_did), authorUri ) - const { providerId, entityId, ...chainEntry } = rating.entry + const { provider_id, entity_id, ...chainEntry } = rating.entry details.entry = chainEntry return { uri, details } diff --git a/packages/types/src/Score.ts b/packages/types/src/Score.ts index 6c740508..0c708262 100644 --- a/packages/types/src/Score.ts +++ b/packages/types/src/Score.ts @@ -13,90 +13,77 @@ export enum RatingTypeOf { delivery = 'Delivery', } -export enum EntityTypeOf { - retail = 'Retail', - logistic = 'Logistic', -} - export enum EntryTypeOf { credit = 'Credit', debit = 'Debit', } export interface IRatingContent { - entityUid: string - entityId: string - providerUid: string - providerId: string - entityType: EntityTypeOf - ratingType: RatingTypeOf - referenceId?: string - countOfTxn: number - totalRating: number + entity_id: string + provider_id: string + rating_type: RatingTypeOf + reference_id?: string + count_of_txn: number + total_rating: number } export interface IRatingTransformed { - entityUid: string - entityId: string - providerUid: string - providerId: string - providerDid: CordAddress - entityType: EntityTypeOf - ratingType: RatingTypeOf - referenceId?: string - countOfTxn: number - totalEncodedRating: number + entity_id: string + provider_id: string + provider_did: CordAddress + rating_type: RatingTypeOf + reference_id?: string + count_of_txn: number + total_encoded_rating: number } export interface IRatingEntry { entry: IRatingTransformed - messageId: string - referenceId?: RatingEntryUri - entryDigest: HexString + message_id: string + reference_id?: RatingEntryUri + entry_digest: HexString } export type RatingPartialEntry = Omit export type IRatingChainEntry = Omit< IRatingTransformed, - 'providerId' | 'entityId' + 'provider_id' | 'entity_id' > export interface IRatingRevokeEntry { entry: RatingPartialEntry - entityUid: string - providerDid: DidUri + entity_id: string + provider_did: DidUri } export interface IRatingDispatch { entryUri: RatingEntryUri entry: IRatingChainEntry chainSpace: SpaceUri - messageId: string - entryDigest: HexString + message_id: string + entry_digest: HexString authorUri: DidUri - authorSignature: DidSignature } export interface IAggregateScore { - entityUid: string - ratingType: RatingTypeOf - countOfTxn: number - totalRating: number + entity_id: string + rating_type: RatingTypeOf + count_of_txn: number + total_rating: number } export type IRatingChainEntryDetails = Omit< IRatingContent, - 'providerId' | 'entityId' | 'providerDid' | 'referenceId' + 'provider_id' | 'entity_id' | 'provider_did' | 'reference_id' > export interface IRatingChainStatus { entryUri: RatingEntryUri entry: IRatingChainEntryDetails digest: HexString - messageId: string + message_id: string space: SpaceId creatorUri: DidUri - entryType: EntryTypeOf - referenceId?: RatingEntryUri + reference_id?: RatingEntryUri createdAt: string }