toDID method

Map<String, dynamic> toDID()

Implementation

Map<String, dynamic> toDID() {
  final String address = crypto.deriveAddress(uint8ListToHex(seed!), 0);
  final List<Map<String, dynamic>> verificationMethods =
      List<Map<String, dynamic>>.empty(growable: true);
  final List<String> authentications = List<String>.empty(growable: true);

  services!.forEach((String serviceName, Service service) {
    final String purpose = service.derivationPath!
        .split('/')
        .map((String e) => e.replaceAll("'", ''))
        .elementAt(1);

    // Only support of archethic derivation scheme for now
    if (purpose == '650') {
      final KeyPair keyPair = deriveArchethicKeypair(
          seed, service.derivationPath!, 0,
          curve: service.curve!);

      verificationMethods.add(<String, dynamic>{
        'id': 'did:archethic:$address#$serviceName',
        'type': 'JsonWebKey2020',
        'publicKeyJwk': keyToJWK(keyPair.publicKey, serviceName).toJson(),
        'controller': 'did:archethic:$address'
      });

      authentications.add('did:archethic:$address#$serviceName');
    } else {
      throw 'Purpose \'$purpose\' is not yet supported';
    }
  });

  return <String, dynamic>{
    '@context': <String>[
      'https://www.w3.org/ns/did/v1',
    ],
    'id': 'did:archethic:$address',
    'authentication': authentications,
    'verificationMethod': verificationMethods
  };
}