sign method
FutureOr<String>
sign({
- dynamic data,
- WalletStore? wallet,
- String? did,
- Map<
String, dynamic> ? jwk, - bool detached = false,
- dynamic jwsHeader,
override
Build a (detached) JWS
Either using a combination of wallet
and did
or by using a private JsonWebKey jwk
.
Implementation
@override
FutureOr<String> sign(
{dynamic data,
WalletStore? wallet,
String? did,
Map<String, dynamic>? jwk,
bool detached = false,
dynamic jwsHeader}) async {
Map<String, dynamic> header;
if (jwsHeader != null) {
header = credentialToMap(jwsHeader);
if (header['alg'] != 'EdDSA') {
throw Exception('Unsupported Signature Algorithm ${header['alg']}');
}
if (header['crv'] != 'Ed25519') {
throw Exception('Unsupported Curve ${header['crv']}');
}
} else {
header = {'alg': 'EdDSA', 'crv': 'Ed25519'};
}
String encodedHeader = removePaddingFromBase64(
base64UrlEncode(utf8.encode(jsonEncode(header))));
String encodedPayload = removePaddingFromBase64(
base64UrlEncode(utf8.encode(data is String ? data : jsonEncode(data))));
String signingInput = '$encodedHeader.$encodedPayload';
Map<String, dynamic>? key;
if (wallet != null && did != null) {
key = await wallet.getPrivateKeyForCredentialDidAsJwk(did);
key ??= await wallet.getPrivateKeyForConnectionDidAsJwk(did);
if (key == null) throw Exception('No key found in Wallet');
} else if (jwk != null) {
key = jwk;
} else {
throw Exception('No Private key given');
}
var privateKey =
ed.newKeyFromSeed(base64Decode(addPaddingToBase64(key['d'])));
var sig = ed.sign(privateKey, ascii.encode(signingInput));
String encodedSig = removePaddingFromBase64(base64UrlEncode(sig));
return detached
? '$encodedHeader..$encodedSig'
: '$signingInput.$encodedSig';
}