@aptos-labs/ts-sdk
Version:
Aptos TypeScript SDK
170 lines • 6.46 kB
JavaScript
import { Deserializer } from "../../bcs/index.js";
import { EphemeralPublicKeyVariant, EphemeralSignatureVariant } from "../../types/index.js";
import { PublicKey } from "./publicKey.js";
import { Signature } from "./signature.js";
import { Ed25519PublicKey, Ed25519Signature } from "./ed25519.js";
import { Hex } from "../hex.js";
/**
* Represents ephemeral public keys for Aptos Keyless accounts.
*
* These keys are used only temporarily within Keyless accounts and are not utilized as public keys for account identification.
* @group Implementation
* @category Serialization
*/
export class EphemeralPublicKey extends PublicKey {
/**
* The public key itself
* @group Implementation
* @category Serialization
*/
publicKey;
/**
* An enum indicating the scheme of the ephemeral public key
* @group Implementation
* @category Serialization
*/
variant;
/**
* Creates an instance of EphemeralPublicKey using the provided public key.
* This constructor ensures that only supported signature types are accepted.
*
* @param publicKey - The public key to be used for the ephemeral public key.
* @throws Error if the signature type is unsupported.
* @group Implementation
* @category Serialization
*/
constructor(publicKey) {
super();
const publicKeyType = publicKey.constructor.name;
switch (publicKeyType) {
case Ed25519PublicKey.name:
this.publicKey = publicKey;
this.variant = EphemeralPublicKeyVariant.Ed25519;
break;
default:
throw new Error(`Unsupported key for EphemeralPublicKey - ${publicKeyType}`);
}
}
/**
* Verifies a signed message using the ephemeral public key.
*
* @param args - The arguments for the verification.
* @param args.message - The message that was signed.
* @param args.signature - The signature that was signed by the private key of the ephemeral public key.
* @returns true if the signature is valid, otherwise false.
* @group Implementation
* @category Serialization
*/
verifySignature(args) {
const { message, signature } = args;
return this.publicKey.verifySignature({ message, signature: signature.signature });
}
async verifySignatureAsync(args) {
return this.verifySignature(args);
}
/**
* Serializes the current instance, specifically handling the Ed25519 signature type.
* This function ensures that the signature is properly serialized using the provided serializer.
*
* @param serializer - The serializer instance used to serialize the signature.
* @throws Error if the signature type is unknown.
* @group Implementation
* @category Serialization
*/
serialize(serializer) {
if (this.publicKey instanceof Ed25519PublicKey) {
serializer.serializeU32AsUleb128(EphemeralPublicKeyVariant.Ed25519);
this.publicKey.serialize(serializer);
}
else {
throw new Error("Unknown public key type");
}
}
/**
* Deserializes an EphemeralSignature from the provided deserializer.
* This function allows you to retrieve an EphemeralSignature based on the deserialized data.
*
* @param deserializer - The deserializer instance used to read the serialized data.
* @group Implementation
* @category Serialization
*/
static deserialize(deserializer) {
const index = deserializer.deserializeUleb128AsU32();
switch (index) {
case EphemeralPublicKeyVariant.Ed25519:
return new EphemeralPublicKey(Ed25519PublicKey.deserialize(deserializer));
default:
throw new Error(`Unknown variant index for EphemeralPublicKey: ${index}`);
}
}
/**
* Determines if the provided public key is an instance of `EphemeralPublicKey`.
*
* @param publicKey - The public key to check.
* @returns A boolean indicating whether the public key is an ephemeral type.
* @group Implementation
* @category Serialization
*/
static isPublicKey(publicKey) {
return publicKey instanceof EphemeralPublicKey;
}
}
/**
* Represents ephemeral signatures used in Aptos Keyless accounts.
*
* These signatures are utilized within the KeylessSignature framework.
* @group Implementation
* @category Serialization
*/
export class EphemeralSignature extends Signature {
/**
* The signature signed by the private key of an EphemeralKeyPair
* @group Implementation
* @category Serialization
*/
signature;
constructor(signature) {
super();
const signatureType = signature.constructor.name;
switch (signatureType) {
case Ed25519Signature.name:
this.signature = signature;
break;
default:
throw new Error(`Unsupported signature for EphemeralSignature - ${signatureType}`);
}
}
/**
* Deserializes an ephemeral signature from a hexadecimal input.
* This function allows you to convert a hexadecimal representation of an ephemeral signature into its deserialized form for
* further processing.
*
* @param hexInput - The hexadecimal input representing the ephemeral signature.
* @group Implementation
* @category Serialization
*/
static fromHex(hexInput) {
const data = Hex.fromHexInput(hexInput);
const deserializer = new Deserializer(data.toUint8Array());
return EphemeralSignature.deserialize(deserializer);
}
serialize(serializer) {
if (this.signature instanceof Ed25519Signature) {
serializer.serializeU32AsUleb128(EphemeralSignatureVariant.Ed25519);
this.signature.serialize(serializer);
}
else {
throw new Error("Unknown signature type");
}
}
static deserialize(deserializer) {
const index = deserializer.deserializeUleb128AsU32();
switch (index) {
case EphemeralSignatureVariant.Ed25519:
return new EphemeralSignature(Ed25519Signature.deserialize(deserializer));
default:
throw new Error(`Unknown variant index for EphemeralSignature: ${index}`);
}
}
}
//# sourceMappingURL=ephemeral.js.map