Skip to content

Commit

Permalink
network-score: change methods to suit the need of API spec (#176)
Browse files Browse the repository at this point in the history
with more discussions, it was aggreed that it is entity writing to
chain's task to build the payload.

In some cases, as in the example script, the validation of source
is done by the application itself (example, the API for ONDC score
may be done by Beckn signature check methods).

Signed-off-by: Amar Tumballi <amar@dhiway.com>
  • Loading branch information
amarts authored Mar 1, 2024
1 parent 7fc25c0 commit 55a8273
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 356 deletions.
71 changes: 35 additions & 36 deletions demo/src/network-score-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { addNetworkMember } from './utils/createAuthorities'
import { createAccount } from './utils/createAccount'

async function main() {
const networkAddress = 'ws://127.0.0.1:9944'
const networkAddress = process.env.NETWORK_ADDRESS ? process.env.NETWORK_ADDRESS : 'ws://127.0.0.1:9944';
Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK })
await Cord.connect(networkAddress)
const devAuthorIdentity = Cord.Utils.Crypto.makeKeypairFromUri(
'//Alice',
process.env.ANCHOR_URI ? process.env.ANCHOR_URI : '//Alice',
'sr25519'
)
console.log(`\n🌐 Network Score Initial Setup`)
Expand Down Expand Up @@ -135,7 +135,7 @@ async function main() {
console.log(`\n⏳ Network Rating Transaction Flow`)

console.log(`\n💠 Write Rating - (Genesis) Credit Entry `)
let ratingContent: Cord.IRatingContent = {
let ratingContent: IRatingContent = {
entityUid: Cord.Utils.UUID.generate(),
entityId: 'Gupta Kirana Store',
providerUid: Cord.Utils.UUID.generate(),
Expand All @@ -150,17 +150,19 @@ async function main() {
depth: null,
colors: true,
})
const entryDigest = Cord.Utils.Crypto.hashObjectAsHexStr(ratingContent);
const { totalRating, ...restOfRating} = ratingContent;

let transformedEntry: IRatingEntry = {
entry: {
...restOfRating,
providerDid: networkProviderDid.uri.replace('did:cord:', ''),
totalEncodedRating: Math.round(totalRating * 10),
},
messageId: Cord.Utils.UUID.generate(),
entryDigest,
};

let transformedEntry = await Cord.Score.buildFromContentProperties(
ratingContent,
networkProviderDid.uri,
async ({ data }) => ({
signature: networkProviderKeys.assertionMethod.sign(data),
keyType: networkProviderKeys.assertionMethod.type,
keyUri: `${networkProviderDid.uri}${networkProviderDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
})
)
console.log(`\n🌐 Rating Information to API endpoint (/write-ratings) `)
console.dir(transformedEntry, {
depth: null,
Expand All @@ -171,12 +173,6 @@ async function main() {
transformedEntry,
chainSpace.uri,
networkAuthorDid.uri,
async ({ data }) => ({
signature: networkAuthorKeys.assertionMethod.sign(data),
keyType: networkAuthorKeys.assertionMethod.type,
keyUri: `${networkAuthorDid.uri}${networkAuthorDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
})
)

console.log(`\n🌐 Rating Information to Ledger (API -> Ledger) `)
Expand Down Expand Up @@ -210,17 +206,26 @@ async function main() {
depth: null,
colors: true,
})
const revokeRatingEntry = await Cord.Score.buildFromRevokeProperties(
ratingUri,
transformedEntry.entry.entityUid,
networkProviderDid.uri,
async ({ data }) => ({
signature: networkProviderKeys.assertionMethod.sign(data),
keyType: networkProviderKeys.assertionMethod.type,
keyUri: `${networkProviderDid.uri}${networkProviderDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
})
)

/* msgId can be decided by application */
const msgId = `msg-${Cord.Utils.UUID.generate()}`
const transactionTime = new Date().toISOString()

/* this is used for digest, but its again eco-system policy */
const entryTransform = { entryUri: ratingUri, msgId, provider: networkProviderDid.uri, transactionTime }

const revokeDigest = Cord.Utils.Crypto.hashObjectAsHexStr(entryTransform)

const revokeRatingEntry: IRatingRevokeEntry = {
entry: {
messageId: msgId,
entryDigest: revokeDigest,
referenceId: ratingUri,
},
entityUid: transformedEntry.entry.entityUid,
providerDid: networkProviderDid.uri,
}

console.log(
`\n🌐 Rating Revoke (Debit) Information to API endpoint (/amend-ratings) `
)
Expand All @@ -234,12 +239,6 @@ async function main() {
revokeRatingEntry,
chainSpace.uri,
networkAuthorDid.uri,
async ({ data }) => ({
signature: networkAuthorKeys.assertionMethod.sign(data),
keyType: networkAuthorKeys.assertionMethod.type,
keyUri: `${networkAuthorDid.uri}${networkAuthorDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
})
)
console.log(
`\n🌐 Rating Revoke (Debit) Information to Ledger (API -> Ledger) `
Expand Down
6 changes: 0 additions & 6 deletions packages/network-score/src/Scoring.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import { uriToIdentifier, identifierToUri } from '@cord.network/identifier'
import { Chain } from '@cord.network/network'
import { ConfigService } from '@cord.network/config'
import { SDKErrors, DecoderUtils, DataUtils } from '@cord.network/utils'
import { verifySignature } from './Scoring.js'

/**
* Checks if a specific rating is stored in the blockchain.
Expand Down Expand Up @@ -225,11 +224,6 @@ export async function dispatchRevokeRatingToChain(
): Promise<RatingEntryUri> {
try {
const api = ConfigService.get('api')
verifySignature(
ratingEntry.entryDigest,
ratingEntry.authorSignature,
Did.getDidUri(ratingEntry.authorUri)
)

const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri)

Expand Down
Loading

0 comments on commit 55a8273

Please sign in to comment.