Public Key Service
Public Key Service Implementation
typescript
Public Key Service Implementation
class PublicKeyService {
constructor(
private provider: JSONRpcProvider,
private network: Network
) {}
async getInfo(address: string, isContract: boolean = false): Promise<Address> {
return this.provider.getPublicKeyInfo(address, isContract);
}
async getBatchInfo(
addresses: string[],
isContract: boolean = false
): Promise<AddressesInfo> {
return this.provider.getPublicKeysInfo(addresses, isContract, true);
}
async getRawInfo(address: string): Promise<PublicKeyInfo | null> {
const result = await this.provider.getPublicKeysInfoRaw([address]);
const info = result[address];
if ('error' in info) {
return null;
}
return info;
}
async getAllFormats(address: string): Promise<Record<string, string>> {
const info = await this.getRawInfo(address);
if (!info) {
throw new Error('Address not found');
}
const formats: Record<string, string> = {};
if (info.p2tr) formats.p2tr = info.p2tr;
if (info.p2op) formats.p2op = info.p2op;
if (info.p2wpkh) formats.p2wpkh = info.p2wpkh;
if (info.p2pkh) formats.p2pkh = info.p2pkh;
if (info.p2shp2wpkh) formats.p2shp2wpkh = info.p2shp2wpkh;
return formats;
}
validate(address: string): AddressTypes | null {
return this.provider.validateAddress(address, this.network);
}
isValid(address: string): boolean {
return this.validate(address) !== null;
}
getCSV1(address: Address): IP2WSHAddress {
return this.provider.getCSV1ForAddress(address);
}
async hasQuantumKey(address: string): Promise<boolean> {
const info = await this.getRawInfo(address);
return info?.mldsaHashedPublicKey !== undefined;
}
}
// Usage
const pkService = new PublicKeyService(provider, network);
// Validate address
if (pkService.isValid(address)) {
console.log('Valid address');
}
// Get all formats
const formats = await pkService.getAllFormats(address);
console.log('Address formats:', formats);
// Check quantum key
const hasQuantum = await pkService.hasQuantumKey(address);
console.log('Has ML-DSA key:', hasQuantum);