fetchAgent static method

Future<List<Object>> fetchAgent(
  1. Map<String, String> queryParams,
  2. bool local,
  3. String canisterId,
  4. Service idLService,
)

Implementation

static Future<List<Object>> fetchAgent(Map<String, String> queryParams,
    bool local, String canisterId, Service idLService) async {
  try {
    String delegationString = queryParams['del'].toString();

    String decodedDelegation = Uri.decodeComponent(delegationString);
    DelegationChain delegationChain =
        DelegationChain.fromJSON(jsonDecode(decodedDelegation));
    DelegationIdentity delegationIdentity =
        DelegationIdentity(_newIdentity!, delegationChain);

    // Storing keys in local for autoLogin
    SecureStore.writeSecureData(
        "pubKey", bytesToHex(_newIdentity!.getKeyPair().publicKey.toDer()));
    SecureStore.writeSecureData(
        "privKey", bytesToHex(_newIdentity!.getKeyPair().secretKey));
    SecureStore.writeSecureData("delegation", delegationString);

    _principalId = delegationIdentity.getPrincipal().toText();

    newAgent = local
        ? HttpAgent(
            options: HttpAgentOptions(
              identity: delegationIdentity,
            ),
            defaultHost: 'localhost',
            defaultPort: 4943,
            defaultProtocol: 'http',
          )
        : HttpAgent(
            options: HttpAgentOptions(
              identity: delegationIdentity,
              host: 'icp-api.io',
            ),
          );

    CanisterActor delegatedActor = getActor(canisterId, idLService);

    var hexPrincipalId =
        await delegatedActor.getFunc(FieldsMethod.whoAmI)?.call([]);

    return [hexPrincipalId, newAgent!, delegationIdentity];
  } catch (e) {
    log("Fetch Agent Error: $e");
    return [];
  }
}