diff --git a/src/api/account.ts b/src/api/account.ts index 5e0ff34c5..24b75e5e2 100644 --- a/src/api/account.ts +++ b/src/api/account.ts @@ -918,7 +918,7 @@ export class Account { */ async verifySecp256k1Account(args: { message: HexInput; - signature: HexInput | Secp256k1Signature | AnySignature; + signature: Secp256k1Signature | AnySignature; recoveryBit?: number; accountAddress: AccountAddressInput; }): Promise { diff --git a/src/core/crypto/singleKey.ts b/src/core/crypto/singleKey.ts index c3b848c82..b66fd43ed 100644 --- a/src/core/crypto/singleKey.ts +++ b/src/core/crypto/singleKey.ts @@ -190,6 +190,27 @@ export class AnyPublicKey extends AccountPublicKey { static isInstance(publicKey: PublicKey): publicKey is AnyPublicKey { return "publicKey" in publicKey && "variant" in publicKey; } + + /** + * Recover a Secp256k1 AnyPublicKey from a signature and message. + * + * @param args.signature - The signature to recover the public key from. + * @param args.message - The message that was signed. + * @param args.recoveryBit - The recovery bit to use for the public key. + */ + static fromSecp256k1SignatureAndMessage(args: { + signature: AnySignature; + message: HexInput; + recoveryBit: number; + }): AnyPublicKey { + const { signature, message, recoveryBit } = args; + const publicKey = Secp256k1PublicKey.fromSignatureAndMessage({ + signature: toSecp256k1Signature(signature), + message, + recoveryBit, + }); + return new AnyPublicKey(publicKey); + } } /** @@ -276,26 +297,4 @@ export class AnySignature extends Signature { "toUint8Array" in signature.signature ); } - - /** - * Recover a Secp256k1 public key from a signature and message. - * - * @param args - The arguments for recovering the public key. - * @param args.signature - The signature to recover the public key from. - * @param args.message - The message that was signed. - * @param args.recoveryBit - The recovery bit to use for the public key. - */ - static fromSecp256k1SignatureAndMessage(args: { - signature: AnySignature; - message: HexInput; - recoveryBit: number; - }): AnyPublicKey { - const { signature, message, recoveryBit } = args; - const publicKey = Secp256k1PublicKey.fromSignatureAndMessage({ - signature: toSecp256k1Signature(signature), - message, - recoveryBit, - }); - return new AnyPublicKey(publicKey); - } } diff --git a/src/internal/account.ts b/src/internal/account.ts index 4dba85717..5faf559d5 100644 --- a/src/internal/account.ts +++ b/src/internal/account.ts @@ -29,14 +29,7 @@ import { } from "../types"; import { AccountAddress, AccountAddressInput } from "../core/accountAddress"; import { Account } from "../account"; -import { - AnyPublicKey, - AnySignature, - Ed25519PublicKey, - PrivateKey, - Secp256k1PublicKey, - Secp256k1Signature, -} from "../core/crypto"; +import { AnyPublicKey, AnySignature, Ed25519PublicKey, PrivateKey, Secp256k1Signature } from "../core/crypto"; import { queryIndexer } from "./general"; import { GetAccountCoinsCountQuery, @@ -64,7 +57,6 @@ import { CurrentFungibleAssetBalancesBoolExp } from "../types/generated/types"; import { getTableItem } from "./table"; import { APTOS_COIN } from "../utils"; import { AptosApiError } from "../errors"; -import { toSecp256k1Signature } from "../core/crypto/signatureUtils"; /** * Retrieves account information for a specified account address. @@ -840,12 +832,13 @@ export async function isAccountExist(args: { aptosConfig: AptosConfig; authKey: export async function verifySecp256k1Account(args: { aptosConfig: AptosConfig; message: HexInput; - signature: HexInput | Secp256k1Signature | AnySignature; - recoveryBit?: number; + signature: Secp256k1Signature | AnySignature; accountAddress: AccountAddressInput; + recoveryBit?: number; }): Promise { const { aptosConfig, message, recoveryBit, accountAddress } = args; - const signature = toSecp256k1Signature(args.signature); + const signature = AnySignature.isInstance(args.signature) ? args.signature : new AnySignature(args.signature); + const { authentication_key: authKeyString } = await getInfo({ aptosConfig, accountAddress, @@ -853,13 +846,11 @@ export async function verifySecp256k1Account(args: { const authKey = new AuthenticationKey({ data: authKeyString }); if (recoveryBit !== undefined) { - const publicKey = new AnyPublicKey( - Secp256k1PublicKey.fromSignatureAndMessage({ - signature, - message, - recoveryBit, - }), - ); + const publicKey = AnyPublicKey.fromSecp256k1SignatureAndMessage({ + signature, + message, + recoveryBit, + }); const derivedAuthKey = publicKey.authKey(); if (authKey.toStringWithoutPrefix() === derivedAuthKey.toStringWithoutPrefix()) { return publicKey; @@ -872,13 +863,11 @@ export async function verifySecp256k1Account(args: { for (let i = 0; i < 4; i += 1) { try { - const publicKey = new AnyPublicKey( - Secp256k1PublicKey.fromSignatureAndMessage({ - signature, - message, - recoveryBit: i, - }), - ); + const publicKey = AnyPublicKey.fromSecp256k1SignatureAndMessage({ + signature, + message, + recoveryBit: i, + }); const derivedAuthKey = publicKey.authKey(); if (authKey.toStringWithoutPrefix() === derivedAuthKey.toStringWithoutPrefix()) { return publicKey; diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 2a59d259b..d14f94582 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -160,4 +160,4 @@ export const isEncodedStruct = ( */ export function isHexInput(value: unknown): value is HexInput { return value instanceof Uint8Array || typeof value === "string"; -} \ No newline at end of file +}