Skip to content

Commit

Permalink
use anypubkey static func
Browse files Browse the repository at this point in the history
  • Loading branch information
heliuchuan committed Dec 3, 2024
1 parent c7d0f07 commit deab85a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ export class Account {
*/
async verifySecp256k1Account(args: {
message: HexInput;
signature: HexInput | Secp256k1Signature | AnySignature;
signature: Secp256k1Signature | AnySignature;
recoveryBit?: number;
accountAddress: AccountAddressInput;
}): Promise<AnyPublicKey> {
Expand Down
43 changes: 21 additions & 22 deletions src/core/crypto/singleKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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);
}
}
41 changes: 15 additions & 26 deletions src/internal/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -840,26 +832,25 @@ 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<AnyPublicKey> {
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,
});
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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ export const isEncodedStruct = (
*/
export function isHexInput(value: unknown): value is HexInput {
return value instanceof Uint8Array || typeof value === "string";
}
}

0 comments on commit deab85a

Please sign in to comment.