Skip to content

Commit

Permalink
update package scoring to match specs
Browse files Browse the repository at this point in the history
Signed-off-by: Adi Bhagavath <[email protected]>
  • Loading branch information
adi-a11y committed Apr 2, 2024
1 parent 0040114 commit 8756962
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 154 deletions.
82 changes: 42 additions & 40 deletions demo/src/network-score-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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, {
Expand All @@ -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;
Expand Down Expand Up @@ -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, {
Expand Down
74 changes: 36 additions & 38 deletions packages/network-score/src/Scoring.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -228,21 +228,21 @@ 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) {
throw new SDKErrors.CordDispatchError(`Rating Entry not found on chain.`)
}

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
)

Expand Down Expand Up @@ -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)
Expand All @@ -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
)
Expand Down Expand Up @@ -365,10 +365,10 @@ function extractEnumIndex(enumObject: { index: number }): number {
return enumObject.index
}
// TypeScript Enum Mappings
const EntityTypeMapping: Record<number, EntityTypeOf> = {
0: EntityTypeOf.retail,
1: EntityTypeOf.logistic,
}
// const EntityTypeMapping: Record<number, EntityTypeOf> = {
// 0: EntityTypeOf.retail,
// 1: EntityTypeOf.logistic,
// }

const RatingTypeMapping: Record<number, RatingTypeOf> = {
0: RatingTypeOf.overall,
Expand All @@ -391,12 +391,12 @@ const EntryTypeMapping: Record<number, EntryTypeOf> = {
*
* @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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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()),
})
}
})
Expand Down
Loading

0 comments on commit 8756962

Please sign in to comment.