SuiAccount.fromPrivateKey constructor

SuiAccount.fromPrivateKey(
  1. String privateKey, [
  2. SignatureScheme? scheme
])

Implementation

factory SuiAccount.fromPrivateKey(String privateKey, [SignatureScheme? scheme]) {
  String sk = privateKey;
  SignatureScheme? se = scheme;
  if (privateKey.startsWith('suiprivkey')) {
    final (scheme, privKey) = decodeSuiPrivateKey(privateKey);
    se = scheme;
    sk = Hex.encode(privKey);
  }

  final privateKeyHex = Hex.trimHex(Hex.prefixHex(sk));
  if (privateKeyHex.length != 64 && privateKeyHex.length != 128) {
    throw ArgumentError("Invalid private key length ${privateKeyHex.length}");
  }
  SuiAccount account;
  switch (se) {
    case SignatureScheme.Secp256k1:
      account = SuiAccount(
          Secp256k1Keypair.fromSecretKey(Hex.decode(privateKeyHex)));
      break;
    case SignatureScheme.Secp256r1:
      account = SuiAccount(
          Secp256r1Keypair.fromSecretKey(Hex.decode(privateKeyHex)));
      break;
    case SignatureScheme.Ed25519:
      account =
          SuiAccount(Ed25519Keypair.fromSecretKey(Hex.decode(privateKeyHex)));
      break;
    default:
      throw ArgumentError('Undefined SignatureScheme $se');
  }
  return account;
}