createUserService function

Future<User> createUserService({
  1. required String user,
  2. Wallet? wallet,
  3. required String publicKey,
  4. required String encryptedPrivateKey,
})

Implementation

Future<User> createUserService({
  required String user,
  Wallet? wallet,
  required String publicKey,
  required String encryptedPrivateKey,
}) async {
  wallet ??= getCachedWallet();

  // if (wallet == null || wallet.signer == null) {
  //   throw Exception('Provide signer');
  // }

  final requestPath = '/v2/users/';

  if (isValidCAIP10NFTAddress(user)) {
    final epoch = DateTime.now().millisecondsSinceEpoch ~/ 1000;
    if (user.split(':').length != 6) {
      user = '$user:$epoch';
    }
  }

  final data = {
    'caip10': walletToPCAIP10(user),
    'did': walletToPCAIP10(user),
    'publicKey': publicKey,
    'encryptedPrivateKey': encryptedPrivateKey,
  };

  final hash = generateHash(data);

  final signatureObj = await getEip191Signature(wallet!, hash, version: 'v2');

  final body = {
    ...data,
    ...signatureObj,
  };

  final responseData = await http.post(path: requestPath, data: body);

  if (responseData != null) {
    return User.fromJson(responseData);
  } else {
    throw Exception('[Push SDK] - API $requestPath: Error');
  }
}