-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
349 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as Cord from '@cord.network/sdk' | ||
import { IJournalContent, IRatingInput } from '@cord.network/types' | ||
|
||
/** | ||
* This function anchors the score on the blockchain | ||
* @param journalContent - Score entry details | ||
* @param registryAuthority - Registry authority | ||
* @param authorIdentity - The account that will be used to sign and submit the extrinsic. | ||
* @param authorDid - DID of the entity which anchors the transaction. | ||
* @param authorKeys - Keys which are used to sign. | ||
* @returns the hash of the score entry if the operation is executed successfully. | ||
*/ | ||
|
||
export async function updateScore( | ||
journalContent: IJournalContent, | ||
registryAuthority: String, | ||
authorIdentity: Cord.CordKeyringPair, | ||
authorDid: Cord.DidUri, | ||
authorKeys: Cord.CordKeyringPair | ||
) { | ||
const api = Cord.ConfigService.get('api') | ||
|
||
journalContent.rating = await Cord.Scoring.adjustAndRoundRating( | ||
journalContent.rating | ||
) | ||
const digest = await Cord.Scoring.generateDigestFromJournalContent( | ||
journalContent | ||
) | ||
const authorization = registryAuthority.replace('auth:cord:', '') | ||
const ratingInput: IRatingInput = { | ||
entry: journalContent, | ||
digest: digest, | ||
creator: authorIdentity.address, | ||
} | ||
const journalCreationExtrinsic = await api.tx.score.addRating( | ||
ratingInput, | ||
authorization | ||
) | ||
|
||
const authorizedStreamTx = await Cord.Did.authorizeTx( | ||
authorDid, | ||
journalCreationExtrinsic, | ||
async ({ data }) => ({ | ||
signature: authorKeys.assertionMethod.sign(data), | ||
keyType: authorKeys.assertionMethod.type, | ||
}), | ||
authorIdentity.address | ||
) | ||
|
||
try { | ||
await Cord.Chain.signAndSubmitTx(authorizedStreamTx, authorIdentity) | ||
return Cord.Scoring.getUriForScore(journalContent) | ||
} catch (error) { | ||
return error.message | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
ScoreType, | ||
IScoreDetails, | ||
SCORE_MODULUS, | ||
IJournalContent, | ||
} from '@cord.network/types' | ||
import { ConfigService } from '@cord.network/config' | ||
import { Identifier, SDKErrors } from '@cord.network/utils' | ||
import * as Did from '@cord.network/did' | ||
|
||
export async function fetchJournalFromChain( | ||
scoreId: string, | ||
scoreType: ScoreType | ||
): Promise<IJournalContent | null> { | ||
const api = ConfigService.get('api') | ||
const cordScoreId = Identifier.uriToIdentifier(scoreId) | ||
const encodedScoreEntry = await api.query.score.journal( | ||
cordScoreId, | ||
scoreType | ||
) | ||
const decodedScoreEntry = fromChain(encodedScoreEntry) | ||
if (decodedScoreEntry === null) { | ||
throw new SDKErrors.ScoreMissingError( | ||
`There is not a Score of type ${scoreType} with the provided ID "${scoreId}" on chain.` | ||
) | ||
} else return decodedScoreEntry | ||
} | ||
|
||
export function fromChain( | ||
encodedEntry: any | ||
): IJournalContent | null { | ||
if (encodedEntry.isSome) { | ||
const unwrapped = encodedEntry.unwrap() | ||
return { | ||
entity: Did.fromChain(unwrapped.entry.entity), | ||
tid: JSON.stringify(unwrapped.entry.tid.toHuman()), | ||
collector: Did.fromChain(unwrapped.entry.collector), | ||
rating_type: unwrapped.entry.ratingType.toString(), | ||
rating: parseInt(unwrapped.entry.rating.toString()) / SCORE_MODULUS, | ||
entry_type: unwrapped.entry.entryType.toString(), | ||
count: parseInt(unwrapped.entry.count.toString()), | ||
} | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
export async function fetchScore( | ||
entityUri: string, | ||
scoreType: ScoreType | ||
): Promise<IScoreDetails> { | ||
const api = ConfigService.get('api') | ||
const encoded = await api.query.score.scores(entityUri, scoreType) | ||
if (encoded.isSome) { | ||
const decoded = encoded.unwrap() | ||
return { | ||
rating: JSON.parse(decoded.rating.toString()), | ||
count: JSON.parse(decoded.count.toString()), | ||
} | ||
} else | ||
throw new SDKErrors.ScoreMissingError( | ||
`There is not a Score of type ${scoreType} with the provided ID "${entityUri}" on chain.` | ||
) | ||
} | ||
|
Oops, something went wrong.