importPrivateKey method

  1. @override
Future<WaasCreatedWallet> importPrivateKey(
  1. String chain,
  2. String privateKey,
  3. bool? isRawScalarImport
)
override

Imports a private key through MPC, refreshes the user, and emits walletCreated. Supports EVM, SOL, BTC, SUI, TON, and TRON; no elevation is required. Set isRawScalarImport only for raw Ed25519 scalars on SOL/SUI/TON, not seeds; omit it for other chains.

Implementation

@override
Future<WaasCreatedWallet> importPrivateKey(String chain, String privateKey, bool? isRawScalarImport) 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 importedWallet = await _waasEngine.importPrivateKey((chain == "SOL" ? "SVM" : chain), privateKey, "TWO_OF_TWO", authToken, signedSessionId, isRawScalarImport);
    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(importedWallet);
    _logger.log("info", "waas private key imported");

    return importedWallet;
  }
  return await _api.instrumentation.runAsync("waas.importPrivateKey", instrumentationOperation);
}