Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix federated keyless constructor's address derivation. #533

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
All notable changes to the Aptos TypeScript SDK will be captured in this file. This changelog is written by hand for now. It adheres to the format set out by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Unreleased
- Fix the `FederatedKeylessAccount` constructor to derive the correct address.

# 1.29.0 (2024-10-04)

Expand Down
5 changes: 3 additions & 2 deletions src/account/AbstractKeylessAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export abstract class AbstractKeylessAccount extends Serializable implements Acc
// Use the static constructor 'create' instead.
protected constructor(args: {
address?: AccountAddress;
publicKey: KeylessPublicKey | FederatedKeylessPublicKey;
ephemeralKeyPair: EphemeralKeyPair;
iss: string;
uidKey: string;
Expand All @@ -107,9 +108,9 @@ export abstract class AbstractKeylessAccount extends Serializable implements Acc
jwt: string;
}) {
super();
const { address, ephemeralKeyPair, uidKey, uidVal, aud, pepper, proof, proofFetchCallback, jwt } = args;
const { address, ephemeralKeyPair, publicKey, uidKey, uidVal, aud, pepper, proof, proofFetchCallback, jwt } = args;
this.ephemeralKeyPair = ephemeralKeyPair;
this.publicKey = KeylessPublicKey.create(args);
this.publicKey = publicKey;
this.accountAddress = address ? AccountAddress.from(address) : this.publicKey.authKey().derivedAddress();
this.uidKey = uidKey;
this.uidVal = uidVal;
Expand Down
5 changes: 3 additions & 2 deletions src/account/FederatedKeylessAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export class FederatedKeylessAccount extends AbstractKeylessAccount {
proofFetchCallback?: ProofFetchCallback;
jwt: string;
}) {
super(args);
this.publicKey = FederatedKeylessPublicKey.create(args);
const publicKey = FederatedKeylessPublicKey.create(args);
super({ publicKey, ...args });
this.publicKey = publicKey;
}

serialize(serializer: Serializer): void {
Expand Down
11 changes: 9 additions & 2 deletions src/account/KeylessAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { JwtPayload, jwtDecode } from "jwt-decode";
import { HexInput } from "../types";
import { AccountAddress } from "../core/accountAddress";
import { ZeroKnowledgeSig } from "../core/crypto";
import { KeylessPublicKey, ZeroKnowledgeSig } from "../core/crypto";

import { EphemeralKeyPair } from "./EphemeralKeyPair";
import { Deserializer, Serializer } from "../bcs";
Expand All @@ -21,6 +21,11 @@ import { AbstractKeylessAccount, ProofFetchCallback } from "./AbstractKeylessAcc
* EphemeralKeyPair, and corresponding proof.
*/
export class KeylessAccount extends AbstractKeylessAccount {
/**
* The KeylessPublicKey associated with the account
*/
readonly publicKey: KeylessPublicKey;

// Use the static constructor 'create' instead.
private constructor(args: {
address?: AccountAddress;
Expand All @@ -34,7 +39,9 @@ export class KeylessAccount extends AbstractKeylessAccount {
proofFetchCallback?: ProofFetchCallback;
jwt: string;
}) {
super(args);
const publicKey = KeylessPublicKey.create(args);
super({ publicKey, ...args });
this.publicKey = publicKey;
}

serialize(serializer: Serializer): void {
Expand Down
20 changes: 3 additions & 17 deletions tests/e2e/api/keyless.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import {
Account,
FederatedKeylessAccount,
FederatedKeylessPublicKey,
KeylessAccount,
KeylessPublicKey,
ProofFetchStatus,
} from "../../../src";
import { Account, FederatedKeylessAccount, KeylessAccount, ProofFetchStatus } from "../../../src";
import { FUND_AMOUNT, TRANSFER_AMOUNT } from "../../unit/helper";
import { getAptosClient } from "../helper";
import { EPHEMERAL_KEY_PAIR, simpleCoinTransactionHeler as simpleCoinTransactionHelper } from "../transaction/helper";
Expand Down Expand Up @@ -127,19 +120,12 @@ describe("keyless api", () => {
"creates the keyless account via the static constructor and submits a transaction",
async () => {
const pepper = await aptos.getPepper({ jwt, ephemeralKeyPair });
const publicKey =
jwkAddress === undefined
? KeylessPublicKey.fromJwtAndPepper({ jwt, pepper })
: FederatedKeylessPublicKey.fromJwtAndPepper({ jwt, pepper, jwkAddress });
const address = await aptos.lookupOriginalAccountAddress({
authenticationKey: publicKey.authKey().derivedAddress(),
});
const proof = await aptos.getProof({ jwt, ephemeralKeyPair, pepper });

const account =
jwkAddress === undefined
? KeylessAccount.create({ address, proof, jwt, ephemeralKeyPair, pepper })
: FederatedKeylessAccount.create({ address, proof, jwt, ephemeralKeyPair, pepper, jwkAddress });
? KeylessAccount.create({ proof, jwt, ephemeralKeyPair, pepper })
: FederatedKeylessAccount.create({ proof, jwt, ephemeralKeyPair, pepper, jwkAddress });
const recipient = Account.generate();
await simpleCoinTransactionHelper(aptos, account, recipient);
},
Expand Down
Loading