fromRawBytes static method

Future<Ed25519PrivateKey> fromRawBytes(
  1. Uint8List bytes
)

Creates an Ed25519PrivateKey from raw bytes

Implementation

static Future<Ed25519PrivateKey> fromRawBytes(Uint8List bytes) async {
  if (bytes.length != 32 && bytes.length != 64) {
    throw FormatException('Ed25519 private key must be 32 or 64 bytes');
  }

  final algorithm = crypto.Ed25519();

  if (bytes.length == 64) {
    // The format is 32 bytes private key followed by 32 bytes public key
    // Since we can't verify the private key (cryptography package limitation),
    // we'll just use the public key part and generate a new keypair
    final publicKeyBytes = bytes.sublist(32);

    // Create a new keypair
    final keyPair = await algorithm.newKeyPair();

    // Create the public key from the bytes in the marshaled data
    final publicKey = Ed25519PublicKey(
      crypto.SimplePublicKey(publicKeyBytes, type: crypto.KeyPairType.ed25519)
    );

    return Ed25519PrivateKey.withPublicKey(keyPair, publicKey);
  } else {
    // Just 32 bytes - we'll treat this as a seed for a new keypair
    final keyPair = await algorithm.newKeyPairFromSeed(bytes);
    final publicKeyObj = await keyPair.extractPublicKey();

    return Ed25519PrivateKey.withPublicKey(
      keyPair,
      Ed25519PublicKey(publicKeyObj as crypto.SimplePublicKey),
      bytes
    );
  }
}