signCredential function
Future<String>
signCredential(
- WalletStore wallet,
- dynamic credentialToSign, {
- String? challenge,
- Signer? signer,
- dynamic loadDocumentFunction(
- Uri url,
- LoadDocumentOptions? options
Signs a W3C-Standard conform credentialToSign
with the private key for issuer-did in the credential using linked data proof.
Implementation
Future<String> signCredential(WalletStore wallet, dynamic credentialToSign,
{String? challenge,
Signer? signer,
Function(Uri url, LoadDocumentOptions? options) loadDocumentFunction =
loadDocumentStrict}) async {
Map<String, dynamic> credential;
if (credentialToSign is VerifiableCredential) {
credential = credentialToSign.toJson();
} else {
credential = credentialToMap(credentialToSign);
}
String issuerDid = getIssuerDidFromCredential(credential);
if (issuerDid == '') {
throw Exception('Could not examine IssuerDID');
}
var contextIri = _findContext(issuerDid);
List? context = credential['@context'];
if (context == null) {
throw Exception('Unable to sign: No @context field found.');
}
if (!context.contains(contextIri)) {
context.add(contextIri);
}
signer ??= _determineSignerForDid(issuerDid, loadDocumentFunction);
credential['proof'] = await signer.buildProof(credential, wallet, issuerDid,
challenge: challenge);
return jsonEncode(credential);
}