Skip to content

Commit

Permalink
organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Apr 11, 2024
1 parent ff5e305 commit 6be06b2
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 129 deletions.
6 changes: 6 additions & 0 deletions src/api/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { General } from "./general";
import { ANS } from "./ans";
import { Staking } from "./staking";
import { Transaction } from "./transaction";
import { ProofChallenge } from "./proofChallenge";

/**
* This class is the main entry point into Aptos's
Expand Down Expand Up @@ -43,6 +44,8 @@ export class Aptos {

readonly transaction: Transaction;

readonly proofChallenge: ProofChallenge;

constructor(settings?: AptosConfig) {
this.config = new AptosConfig(settings);
this.account = new Account(this.config);
Expand All @@ -55,6 +58,7 @@ export class Aptos {
this.general = new General(this.config);
this.staking = new Staking(this.config);
this.transaction = new Transaction(this.config);
this.proofChallenge = new ProofChallenge(this.config);
}
}

Expand All @@ -70,6 +74,7 @@ export interface Aptos
FungibleAsset,
General,
Staking,
ProofChallenge,
Omit<Transaction, "build" | "simulate" | "submit" | "batch"> {}

/**
Expand Down Expand Up @@ -103,3 +108,4 @@ applyMixin(Aptos, FungibleAsset, "fungibleAsset");
applyMixin(Aptos, General, "general");
applyMixin(Aptos, Staking, "staking");
applyMixin(Aptos, Transaction, "transaction");
applyMixin(Aptos, ProofChallenge, "proofChallenge");
11 changes: 1 addition & 10 deletions src/api/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { AptosConfig } from "./aptosConfig";
import {
createProofChallenge,
getBlockByHeight,
getBlockByVersion,
getChainTopUserTransactions,
Expand All @@ -22,12 +21,11 @@ import {
GraphqlQuery,
LedgerInfo,
LedgerVersionArg,
MoveFunctionId,
MoveValue,
TableItemRequest,
} from "../types";
import { ProcessorType } from "../utils/const";
import { InputViewFunctionData, ProofChallenge } from "../transactions";
import { InputViewFunctionData } from "../transactions";

/**
* A class to query all `General` Aptos related queries
Expand Down Expand Up @@ -203,11 +201,4 @@ export class General {
async getProcessorStatus(processorType: ProcessorType): Promise<GetProcessorStatusResponse[0]> {
return getProcessorStatus({ aptosConfig: this.config, processorType });
}

async createProofChallenge(args: { struct: MoveFunctionId; data: Array<any> }): Promise<ProofChallenge> {
return createProofChallenge({
config: this.config,
...args,
});
}
}
37 changes: 37 additions & 0 deletions src/api/proofChallenge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import { Account, Signature } from "../core";
import { createProofChallenge, signProofChallenge } from "../internal/proofChallenge";
import { MoveFunctionId } from "../types";
import { AptosConfig } from "./aptosConfig";
import { ProofChallenge as ProofChallengeInstance } from "../transactions/instances/proofChallenge";
import { EntryFunctionArgumentTypes, SimpleEntryFunctionArgumentTypes } from "../transactions/types";

/**
* A class to query all `General` Aptos related queries
*/
export class ProofChallenge {
readonly config: AptosConfig;

constructor(config: AptosConfig) {
this.config = config;
}

async createProofChallenge(args: {
struct: MoveFunctionId;
data: Array<EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes>;
}): Promise<ProofChallengeInstance> {
return createProofChallenge({
config: this.config,
...args,
});
}

// eslint-disable-next-line class-methods-use-this
signProofChallenge(args: { challenge: ProofChallengeInstance; signer: Account }): Signature {
return signProofChallenge({
...args,
});
}
}
11 changes: 1 addition & 10 deletions src/api/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ import {
publicPackageTransaction,
rotateAuthKey,
signAndSubmitTransaction,
signProofChallenge,
signTransaction,
} from "../internal/transactionSubmission";
import {
AccountAuthenticator,
AnyRawTransaction,
InputGenerateTransactionOptions,
InputGenerateTransactionPayloadData,
ProofChallenge,
} from "../transactions";
import { AccountAddressInput, Account, PrivateKey, Signature } from "../core";
import { AccountAddressInput, Account, PrivateKey } from "../core";
import { Build } from "./transactionSubmission/build";
import { Simulate } from "./transactionSubmission/simulate";
import { Submit } from "./transactionSubmission/submit";
Expand Down Expand Up @@ -265,13 +263,6 @@ export class Transaction {
});
}

// eslint-disable-next-line class-methods-use-this
signProofChallenge(args: { challenge: ProofChallenge; signer: Account }): Signature {
return signProofChallenge({
...args,
});
}

// TRANSACTION SUBMISSION //

/**
Expand Down
37 changes: 0 additions & 37 deletions src/internal/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
*/

import { AptosConfig } from "../api/aptosConfig";
import { MoveString } from "../bcs";
import { getAptosFullNode, postAptosFullNode, postAptosIndexer } from "../client";
import { AccountAddress } from "../core";
import { getFunctionParts } from "../transactions/transactionBuilder/helpers";
import { fetchStructFieldsAbi, convertArgument } from "../transactions/transactionBuilder/remoteAbi";
import { ProofChallenge } from "../transactions/instances";
import { EntryFunctionArgumentTypes } from "../transactions/types";
import {
AnyNumber,
Block,
Expand All @@ -24,7 +18,6 @@ import {
GraphqlQuery,
LedgerInfo,
LedgerVersionArg,
MoveFunctionId,
TableItemRequest,
} from "../types";
import { GetChainTopUserTransactionsQuery, GetProcessorStatusQuery } from "../types/generated/operations";
Expand Down Expand Up @@ -169,33 +162,3 @@ export async function getProcessorStatus(args: {

return data.processor_status[0];
}

export async function createProofChallenge(args: {
config: AptosConfig;
struct: MoveFunctionId;
data: Array<any>;
}): Promise<ProofChallenge> {
const { config, struct, data } = args;
const { moduleAddress, moduleName, functionName } = getFunctionParts(struct);
const structFieldsAbi = await fetchStructFieldsAbi(moduleAddress, moduleName, functionName, config);

// Check all BCS types, and convert any non-BCS types
const functionArguments: Array<EntryFunctionArgumentTypes> =
data.map((arg, i) => convertArgument(functionName, structFieldsAbi, arg, i, structFieldsAbi.parameters)) ?? [];

// Check that all arguments are accounted for
if (functionArguments.length !== structFieldsAbi.parameters.length) {
throw new Error(
// eslint-disable-next-line max-len
`Too few arguments for '${moduleAddress}::${moduleName}::${functionName}', expected ${structFieldsAbi.parameters.length} but got ${functionArguments.length}`,
);
}

const challenge = new ProofChallenge([
AccountAddress.from(moduleAddress),
new MoveString(moduleName),
new MoveString(functionName),
...functionArguments,
]);
return challenge;
}
21 changes: 21 additions & 0 deletions src/internal/proofChallenge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AptosConfig } from "../api/aptosConfig";
import { Account, Signature } from "../core";
import { EntryFunctionArgumentTypes, SimpleEntryFunctionArgumentTypes, generateProofChallenge } from "../transactions";
import { ProofChallenge } from "../transactions/instances/proofChallenge";
import { MoveFunctionId } from "../types";

export async function createProofChallenge(args: {
config: AptosConfig;
struct: MoveFunctionId;
data: Array<EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes>;
}): Promise<ProofChallenge> {
const challenge = generateProofChallenge({ ...args });
return challenge;
}

export function signProofChallenge(args: { challenge: ProofChallenge; signer: Account }): Signature {
const { challenge, signer } = args;
const challengeHex = challenge.bcsToBytes();
const signature = signer.sign(challengeHex);
return signature;
}
12 changes: 2 additions & 10 deletions src/internal/transactionSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { MoveVector, U8 } from "../bcs";
import { postAptosFullNode } from "../client";
import { Account } from "../core/account";
import { AccountAddress, AccountAddressInput } from "../core/accountAddress";
import { PrivateKey, Signature } from "../core/crypto";
import { PrivateKey } from "../core/crypto";
import { AccountAuthenticator } from "../transactions/authenticator/account";
import { ProofChallenge, RotationProofChallenge } from "../transactions/instances/rotationProofChallenge";
import { RotationProofChallenge } from "../transactions/instances/rotationProofChallenge";
import {
buildTransaction,
generateTransactionPayload,
Expand Down Expand Up @@ -206,13 +206,6 @@ export function signTransaction(args: { signer: Account; transaction: AnyRawTran
return accountAuthenticator;
}

export function signProofChallenge(args: { challenge: ProofChallenge; signer: Account }): Signature {
const { challenge, signer } = args;
const challengeHex = challenge.bcsToBytes();
const signature = signer.sign(challengeHex);
return signature;
}

/**
* Simulates a transaction before singing it.
*
Expand Down Expand Up @@ -355,7 +348,6 @@ export async function rotateAuthKey(args: {

// Sign the challenge
const challengeHex = challenge.bcsToBytes();

const proofSignedByCurrentPrivateKey = fromAccount.sign(challengeHex);
const proofSignedByNewPrivateKey = newAccount.sign(challengeHex);

Expand Down
16 changes: 16 additions & 0 deletions src/transactions/instances/proofChallenge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Serializable, Serializer } from "../../bcs";

export class ProofChallenge extends Serializable {
public readonly data: Serializable[];

constructor(data: Serializable[]) {
super();
this.data = data;
}

serialize(serializer: Serializer): void {
this.data.forEach((data) => {
serializer.serialize(data);
});
}
}
15 changes: 0 additions & 15 deletions src/transactions/instances/rotationProofChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import { AnyNumber } from "../../types";
import { PublicKey } from "../../core/crypto";
import { MoveString, MoveVector, U64, U8 } from "../../bcs";

export class ProofChallenge extends Serializable {
public readonly data: Serializable[];

constructor(data: Serializable[]) {
super();
this.data = data;
}

serialize(serializer: Serializer): void {
this.data.forEach((data) => {
serializer.serialize(data);
});
}
}

/**
* Representation of the challenge which is needed to sign by owner of the account
* to rotate the authentication key.
Expand Down
44 changes: 42 additions & 2 deletions src/transactions/transactionBuilder/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,22 @@ import {
InputViewFunctionDataWithRemoteABI,
InputViewFunctionDataWithABI,
FunctionABI,
SimpleEntryFunctionArgumentTypes,
} from "../types";
import { convertArgument, fetchEntryFunctionAbi, fetchViewFunctionAbi, standardizeTypeTags } from "./remoteAbi";
import {
convertArgument,
fetchEntryFunctionAbi,
fetchStructFieldsAbi,
fetchViewFunctionAbi,
standardizeTypeTags,
} from "./remoteAbi";
import { memoizeAsync } from "../../utils/memoize";
import { AnyNumber } from "../../types";
import { AnyNumber, MoveFunctionId } from "../../types";
import { getFunctionParts, isScriptDataInput } from "./helpers";
import { SimpleTransaction } from "../instances/simpleTransaction";
import { MultiAgentTransaction } from "../instances/multiAgentTransaction";
import { ProofChallenge } from "../instances/proofChallenge";
import { MoveString } from "../../bcs";

/**
* We are defining function signatures, each with its specific input and output.
Expand Down Expand Up @@ -688,3 +697,34 @@ async function fetchAbi<T extends FunctionABI>({
1000 * 60 * 5, // 5 minutes
)();
}

export async function generateProofChallenge(args: {
config: AptosConfig;
struct: MoveFunctionId;
data: Array<EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes>;
}) {
const { config, struct, data } = args;
const { moduleAddress, moduleName, functionName } = getFunctionParts(struct);
const structFieldsAbi = await fetchStructFieldsAbi(moduleAddress, moduleName, functionName, config);

// Check all BCS types, and convert any non-BCS types
// TODO repeated code, move to a central place
const functionArguments: Array<EntryFunctionArgumentTypes> =
data.map((arg, i) => convertArgument(functionName, structFieldsAbi, arg, i, structFieldsAbi.parameters)) ?? [];

// Check that all arguments are accounted for
if (functionArguments.length !== structFieldsAbi.parameters.length) {
throw new Error(
// eslint-disable-next-line max-len
`Too few arguments for '${moduleAddress}::${moduleName}::${functionName}', expected ${structFieldsAbi.parameters.length} but got ${functionArguments.length}`,
);
}

const challenge = new ProofChallenge([
AccountAddress.from(moduleAddress),
new MoveString(moduleName),
new MoveString(functionName),
...functionArguments,
]);
return challenge;
}
Loading

0 comments on commit 6be06b2

Please sign in to comment.