PrivateKey Signer
The PrivateKeySigner class implements the MSI and represents a signer that uses a private key to sign payloads. It is the most basic signer.
Creating a PrivateKeySigner
You can create a PrivateKeySigner instance using one of the following methods:
- From an Existing EthPrivateKey
import 'package:web3dart/web3dart.dart';
final ethPrivateKey = EthPrivateKey.fromHex('your_private_key_hex');
final password = 'your_password';
final random = Random.secure();
final privateKeySigner = PrivateKeySigner.create(ethPrivateKey, password, random);
// Create a signer for AlchemyLightAccount
final prefix = const SignatureOptions(prefix: [0]);
final privateKeySigner = PrivateKeySigner.create(ethPrivateKey, password, random, prefix);- With a Randomly Generated EthPrivateKey
final password = 'your_password';
final privateKeySigner = PrivateKeySigner.createRandom(password);
// Create a signer for AlchemyLightAccount
final privateKeySigner = PrivateKeySigner.createRandom(password, prefix);- From a JSON Representation
final sourceJson = '{...your_private_key_encrypted...}';
final password = 'your_password';
final privateKeySigner = PrivateKeySigner.fromJson(sourceJson, password);
// Create a signer for AlchemyLightAccount
final privateKeySigner = PrivateKeySigner.fromJson(sourceJson, password, prefix);Properties
address: Returns the Ethereum address associated with thePrivateKeySigner.publicKey: Returns the public key associated with thePrivateKeySigner.
Methods
getAddress: Returns the Ethereum address as a hexadecimal string.personalSign: Signs the provided hash using the private key and returns the signature as aUint8List.signToEc: Signs the provided hash using the private key and returns an instance ofMsgSignaturecontaining ther,s, andvvalues of the signature.toJson(): Returns the JSON representation of thePrivateKeySigner.
Usage
// Create a PrivateKeySigner instance
final privateKeySigner = PrivateKeySigner.createRandom('your_password');
// Get the Ethereum address
final address = privateKeySigner.address;
print('Address: $address');
// Sign a payload
final payload = Uint8List.fromList([0x12, 0x34, 0x56, 0x78]);
final signature = await privateKeySigner.personalSign(payload);
print('Signature: $signature');