Skip to content

Commit 9bd4b03

Browse files
committed
organize code
1 parent fea5e18 commit 9bd4b03

File tree

12 files changed

+236
-128
lines changed

12 files changed

+236
-128
lines changed

src/api/aptos.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { General } from "./general";
1212
import { ANS } from "./ans";
1313
import { Staking } from "./staking";
1414
import { Transaction } from "./transaction";
15+
import { ProofChallenge } from "./proofChallenge";
1516

1617
/**
1718
* This class is the main entry point into Aptos's
@@ -47,6 +48,8 @@ export class Aptos {
4748

4849
readonly transaction: Transaction;
4950

51+
readonly proofChallenge: ProofChallenge;
52+
5053
constructor(settings?: AptosConfig) {
5154
this.config = new AptosConfig(settings);
5255
this.account = new Account(this.config);
@@ -59,6 +62,7 @@ export class Aptos {
5962
this.general = new General(this.config);
6063
this.staking = new Staking(this.config);
6164
this.transaction = new Transaction(this.config);
65+
this.proofChallenge = new ProofChallenge(this.config);
6266
}
6367
}
6468

@@ -74,6 +78,7 @@ export interface Aptos
7478
FungibleAsset,
7579
General,
7680
Staking,
81+
ProofChallenge,
7782
Omit<Transaction, "build" | "simulate" | "submit" | "batch"> {}
7883

7984
/**
@@ -107,3 +112,4 @@ applyMixin(Aptos, FungibleAsset, "fungibleAsset");
107112
applyMixin(Aptos, General, "general");
108113
applyMixin(Aptos, Staking, "staking");
109114
applyMixin(Aptos, Transaction, "transaction");
115+
applyMixin(Aptos, ProofChallenge, "proofChallenge");

src/api/general.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import { AptosConfig } from "./aptosConfig";
55
import {
6-
createProofChallenge,
76
getBlockByHeight,
87
getBlockByVersion,
98
getChainTopUserTransactions,
@@ -22,12 +21,11 @@ import {
2221
GraphqlQuery,
2322
LedgerInfo,
2423
LedgerVersionArg,
25-
MoveFunctionId,
2624
MoveValue,
2725
TableItemRequest,
2826
} from "../types";
2927
import { ProcessorType } from "../utils/const";
30-
import { InputViewFunctionData, ProofChallenge } from "../transactions";
28+
import { InputViewFunctionData } from "../transactions";
3129

3230
/**
3331
* A class to query all `General` Aptos related queries
@@ -226,11 +224,4 @@ export class General {
226224
async getProcessorStatus(processorType: ProcessorType): Promise<GetProcessorStatusResponse[0]> {
227225
return getProcessorStatus({ aptosConfig: this.config, processorType });
228226
}
229-
230-
async createProofChallenge(args: { struct: MoveFunctionId; data: Array<any> }): Promise<ProofChallenge> {
231-
return createProofChallenge({
232-
config: this.config,
233-
...args,
234-
});
235-
}
236227
}

src/api/proofChallenge.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright © Aptos Foundation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { Account, Signature } from "../core";
5+
import { createProofChallenge, signProofChallenge } from "../internal/proofChallenge";
6+
import { MoveFunctionId } from "../types";
7+
import { AptosConfig } from "./aptosConfig";
8+
import { ProofChallenge as ProofChallengeInstance } from "../transactions/instances/proofChallenge";
9+
import { EntryFunctionArgumentTypes, SimpleEntryFunctionArgumentTypes } from "../transactions/types";
10+
11+
/**
12+
* A class for all `ProofChallenge` Aptos related operations
13+
*/
14+
export class ProofChallenge {
15+
readonly config: AptosConfig;
16+
17+
constructor(config: AptosConfig) {
18+
this.config = config;
19+
}
20+
21+
/**
22+
* Creates a generic proof challenge
23+
*
24+
* @param args.struct The struct address of the challenge
25+
* @param args.data The struct arguments
26+
*
27+
* @returns ProofChallenge
28+
*/
29+
async createProofChallenge(args: {
30+
struct: MoveFunctionId;
31+
data: Array<EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes>;
32+
}): Promise<ProofChallengeInstance> {
33+
return createProofChallenge({
34+
config: this.config,
35+
...args,
36+
});
37+
}
38+
39+
/**
40+
* Signs a generic proof challenge
41+
*
42+
* @param args.challenge The generated challenge
43+
* @param args.signer The signer account
44+
*
45+
* @returns Signature
46+
*/
47+
// eslint-disable-next-line class-methods-use-this
48+
signProofChallenge(args: { challenge: ProofChallengeInstance; signer: Account }): Signature {
49+
return signProofChallenge({
50+
...args,
51+
});
52+
}
53+
}

src/api/transaction.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ import {
2525
publicPackageTransaction,
2626
rotateAuthKey,
2727
signAndSubmitTransaction,
28-
signProofChallenge,
2928
signTransaction,
3029
} from "../internal/transactionSubmission";
3130
import {
3231
AccountAuthenticator,
3332
AnyRawTransaction,
3433
InputGenerateTransactionOptions,
3534
InputGenerateTransactionPayloadData,
36-
ProofChallenge,
3735
} from "../transactions";
38-
import { AccountAddressInput, Account, PrivateKey, Signature } from "../core";
36+
import { AccountAddressInput, Account, PrivateKey } from "../core";
3937
import { Build } from "./transactionSubmission/build";
4038
import { Simulate } from "./transactionSubmission/simulate";
4139
import { Submit } from "./transactionSubmission/submit";
@@ -308,13 +306,6 @@ export class Transaction {
308306
});
309307
}
310308

311-
// eslint-disable-next-line class-methods-use-this
312-
signProofChallenge(args: { challenge: ProofChallenge; signer: Account }): Signature {
313-
return signProofChallenge({
314-
...args,
315-
});
316-
}
317-
318309
// TRANSACTION SUBMISSION //
319310

320311
/**

src/internal/general.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@
99
*/
1010

1111
import { AptosConfig } from "../api/aptosConfig";
12-
import { MoveString } from "../bcs";
1312
import { getAptosFullNode, postAptosFullNode, postAptosIndexer } from "../client";
14-
import { AccountAddress } from "../core";
15-
import { getFunctionParts } from "../transactions/transactionBuilder/helpers";
16-
import { fetchStructFieldsAbi, convertArgument } from "../transactions/transactionBuilder/remoteAbi";
17-
import { ProofChallenge } from "../transactions/instances";
18-
import { EntryFunctionArgumentTypes } from "../transactions/types";
1913
import {
2014
AnyNumber,
2115
Block,
@@ -24,7 +18,6 @@ import {
2418
GraphqlQuery,
2519
LedgerInfo,
2620
LedgerVersionArg,
27-
MoveFunctionId,
2821
TableItemRequest,
2922
} from "../types";
3023
import { GetChainTopUserTransactionsQuery, GetProcessorStatusQuery } from "../types/generated/operations";
@@ -169,33 +162,3 @@ export async function getProcessorStatus(args: {
169162

170163
return data.processor_status[0];
171164
}
172-
173-
export async function createProofChallenge(args: {
174-
config: AptosConfig;
175-
struct: MoveFunctionId;
176-
data: Array<any>;
177-
}): Promise<ProofChallenge> {
178-
const { config, struct, data } = args;
179-
const { moduleAddress, moduleName, functionName } = getFunctionParts(struct);
180-
const structFieldsAbi = await fetchStructFieldsAbi(moduleAddress, moduleName, functionName, config);
181-
182-
// Check all BCS types, and convert any non-BCS types
183-
const functionArguments: Array<EntryFunctionArgumentTypes> =
184-
data.map((arg, i) => convertArgument(functionName, structFieldsAbi, arg, i, structFieldsAbi.parameters)) ?? [];
185-
186-
// Check that all arguments are accounted for
187-
if (functionArguments.length !== structFieldsAbi.parameters.length) {
188-
throw new Error(
189-
// eslint-disable-next-line max-len
190-
`Too few arguments for '${moduleAddress}::${moduleName}::${functionName}', expected ${structFieldsAbi.parameters.length} but got ${functionArguments.length}`,
191-
);
192-
}
193-
194-
const challenge = new ProofChallenge([
195-
AccountAddress.from(moduleAddress),
196-
new MoveString(moduleName),
197-
new MoveString(functionName),
198-
...functionArguments,
199-
]);
200-
return challenge;
201-
}

src/internal/proofChallenge.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { AptosConfig } from "../api/aptosConfig";
2+
import { Account, Signature } from "../core";
3+
import { EntryFunctionArgumentTypes, SimpleEntryFunctionArgumentTypes, generateProofChallenge } from "../transactions";
4+
import { ProofChallenge } from "../transactions/instances/proofChallenge";
5+
import { MoveFunctionId } from "../types";
6+
7+
export async function createProofChallenge(args: {
8+
config: AptosConfig;
9+
struct: MoveFunctionId;
10+
data: Array<EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes>;
11+
}): Promise<ProofChallenge> {
12+
const challenge = generateProofChallenge({ ...args });
13+
return challenge;
14+
}
15+
16+
export function signProofChallenge(args: { challenge: ProofChallenge; signer: Account }): Signature {
17+
const { challenge, signer } = args;
18+
const challengeHex = challenge.bcsToBytes();
19+
const signature = signer.sign(challengeHex);
20+
return signature;
21+
}

src/internal/transactionSubmission.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { MoveVector, U8 } from "../bcs";
1010
import { postAptosFullNode } from "../client";
1111
import { Account } from "../core/account";
1212
import { AccountAddress, AccountAddressInput } from "../core/accountAddress";
13-
import { PrivateKey, Signature } from "../core/crypto";
13+
import { PrivateKey } from "../core/crypto";
1414
import { AccountAuthenticator } from "../transactions/authenticator/account";
15-
import { ProofChallenge, RotationProofChallenge } from "../transactions/instances/rotationProofChallenge";
15+
import { RotationProofChallenge } from "../transactions/instances/rotationProofChallenge";
1616
import {
1717
buildTransaction,
1818
generateTransactionPayload,
@@ -208,13 +208,6 @@ export function signTransaction(args: { signer: Account; transaction: AnyRawTran
208208
return accountAuthenticator;
209209
}
210210

211-
export function signProofChallenge(args: { challenge: ProofChallenge; signer: Account }): Signature {
212-
const { challenge, signer } = args;
213-
const challengeHex = challenge.bcsToBytes();
214-
const signature = signer.sign(challengeHex);
215-
return signature;
216-
}
217-
218211
/**
219212
* Simulates a transaction before singing it.
220213
*
@@ -357,7 +350,6 @@ export async function rotateAuthKey(args: {
357350

358351
// Sign the challenge
359352
const challengeHex = challenge.bcsToBytes();
360-
361353
const proofSignedByCurrentPrivateKey = fromAccount.sign(challengeHex);
362354
const proofSignedByNewPrivateKey = newAccount.sign(challengeHex);
363355

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Serializable, Serializer } from "../../bcs";
2+
3+
export class ProofChallenge extends Serializable {
4+
public readonly data: Serializable[];
5+
6+
constructor(data: Serializable[]) {
7+
super();
8+
this.data = data;
9+
}
10+
11+
serialize(serializer: Serializer): void {
12+
this.data.forEach((data) => {
13+
serializer.serialize(data);
14+
});
15+
}
16+
}

src/transactions/instances/rotationProofChallenge.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@ import { AnyNumber } from "../../types";
77
import { PublicKey } from "../../core/crypto";
88
import { MoveString, MoveVector, U64, U8 } from "../../bcs";
99

10-
export class ProofChallenge extends Serializable {
11-
public readonly data: Serializable[];
12-
13-
constructor(data: Serializable[]) {
14-
super();
15-
this.data = data;
16-
}
17-
18-
serialize(serializer: Serializer): void {
19-
this.data.forEach((data) => {
20-
serializer.serialize(data);
21-
});
22-
}
23-
}
24-
2510
/**
2611
* Representation of the challenge which is needed to sign by owner of the account
2712
* to rotate the authentication key.

src/transactions/transactionBuilder/transactionBuilder.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,22 @@ import {
7171
InputViewFunctionDataWithRemoteABI,
7272
InputViewFunctionDataWithABI,
7373
FunctionABI,
74+
SimpleEntryFunctionArgumentTypes,
7475
} from "../types";
75-
import { convertArgument, fetchEntryFunctionAbi, fetchViewFunctionAbi, standardizeTypeTags } from "./remoteAbi";
76+
import {
77+
convertArgument,
78+
fetchEntryFunctionAbi,
79+
fetchStructFieldsAbi,
80+
fetchViewFunctionAbi,
81+
standardizeTypeTags,
82+
} from "./remoteAbi";
7683
import { memoizeAsync } from "../../utils/memoize";
84+
import { AnyNumber, MoveFunctionId } from "../../types";
7785
import { getFunctionParts, isScriptDataInput } from "./helpers";
7886
import { SimpleTransaction } from "../instances/simpleTransaction";
7987
import { MultiAgentTransaction } from "../instances/multiAgentTransaction";
88+
import { ProofChallenge } from "../instances/proofChallenge";
89+
import { MoveString } from "../../bcs";
8090

8191
/**
8292
* We are defining function signatures, each with its specific input and output.
@@ -649,3 +659,34 @@ async function fetchAbi<T extends FunctionABI>({
649659
1000 * 60 * 5, // 5 minutes
650660
)();
651661
}
662+
663+
export async function generateProofChallenge(args: {
664+
config: AptosConfig;
665+
struct: MoveFunctionId;
666+
data: Array<EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes>;
667+
}) {
668+
const { config, struct, data } = args;
669+
const { moduleAddress, moduleName, functionName } = getFunctionParts(struct);
670+
const structFieldsAbi = await fetchStructFieldsAbi(moduleAddress, moduleName, functionName, config);
671+
672+
// Check all BCS types, and convert any non-BCS types
673+
// TODO repeated code, move to a central place
674+
const functionArguments: Array<EntryFunctionArgumentTypes> =
675+
data.map((arg, i) => convertArgument(functionName, structFieldsAbi, arg, i, structFieldsAbi.parameters)) ?? [];
676+
677+
// Check that all arguments are accounted for
678+
if (functionArguments.length !== structFieldsAbi.parameters.length) {
679+
throw new Error(
680+
// eslint-disable-next-line max-len
681+
`Too few arguments for '${moduleAddress}::${moduleName}::${functionName}', expected ${structFieldsAbi.parameters.length} but got ${functionArguments.length}`,
682+
);
683+
}
684+
685+
const challenge = new ProofChallenge([
686+
AccountAddress.from(moduleAddress),
687+
new MoveString(moduleName),
688+
new MoveString(functionName),
689+
...functionArguments,
690+
]);
691+
return challenge;
692+
}

0 commit comments

Comments
 (0)