createWallet method

  1. @override
Future<WaasCreatedWallet> createWallet(
  1. String chain
)
override

Creates an embedded (WaaS) wallet for the given chain via 2-of-2 MPC in the engine, then refreshes the user so the new credential is visible. Chain values: 'EVM', 'SOL', 'BTC', 'SUI', 'TON', 'TRON'.

Implementation

@override
Future<WaasCreatedWallet> createWallet(String chain) async {
  Future<WaasCreatedWallet> instrumentationOperation() async {
    final operationScope = _state.flowScope.capture(replace: false);
    await operationScope.ready();
    if (!_state.initialized) {
      throw DynamicStateException("call initialize() before using the client");
    }
    final authToken = _state.token;
    if (authToken == null) {
      throw DynamicStateException("not authenticated — sign in first");
    }

    final waasSessionId = _state.user?.sessionId;
    if (waasSessionId == null) {
      throw DynamicStateException("no sessionId — sign in (or call refreshUser) first");
    }
    operationScope.check();
    final sessionSignature = await _sessionSigner.sign(waasSessionId);
    operationScope.check();
    operationScope.check();
    final nonceResponse = await _api.get<NonceResponse>(path: "/nonce", token: null, sessionPublicKey: _state.sessionPublicKey, parse: NonceResponse.fromJson);
    operationScope.check();
    final nonce = nonceResponse.nonce;
    operationScope.check();
    final nonceSignature = await _sessionSigner.sign(nonce);
    operationScope.check();
    final signedSessionId = "$sessionSignature/$nonceSignature/$nonce";

    if ((_waasState.waasBoundSessionId ?? waasSessionId) != waasSessionId) {
      operationScope.check();
      await _waasEngine.destroy();
      operationScope.check();
    }
    operationScope.check();
    await _waasEngine.initialize(_config.environmentId, (chain == "SOL" ? "SVM" : chain), authToken, _config.apiBaseUrl, _config.universalLink);
    operationScope.check();
    operationScope.commit(() {
        _waasState.waasBoundSessionId = waasSessionId;
        _waasState.waasBoundSessionIdChanged.emit(_waasState.waasBoundSessionId);
    });
    operationScope.check();
    final createdWallet = await _waasEngine.createWallet((chain == "SOL" ? "SVM" : chain), authToken, signedSessionId, "TWO_OF_TWO");
    operationScope.check();

    operationScope.check();
    final refreshedUser = await _api.get<SdkUser>(path: "/users", token: _state.token, sessionPublicKey: _state.sessionPublicKey, parse: SdkUser.fromJson);
    operationScope.check();
    operationScope.commit(() {
        _state.user = refreshedUser;
        _state.userChanged.emit(_state.user);
    });
    _state.walletCreated.emit(createdWallet);
    _logger.log("info", "waas wallet created");

    return createdWallet;
  }
  return await _api.instrumentation.runAsync("waas.createWallet", instrumentationOperation);
}